BOSS 7.1.2
BESIII Offline Software System
Loading...
Searching...
No Matches
Ext_track.cxx
Go to the documentation of this file.
1//
2// File: Ext_track.cc
3//
4
5#include <cstring>
6#include <iostream>
7
9
10#include "G4ParticleTable.hh"
11#include "G4Navigator.hh"
12#include "G4VPhysicalVolume.hh"
13#include "G4VSolid.hh"
14#include "G4GeometryManager.hh"
15#include "G4RegionStore.hh"
16#include "G4ProductionCuts.hh"
17#include "G4Region.hh"
18#include "G4LogicalVolume.hh"
19#include "G4TransportationManager.hh"
20#include "G4PrimaryParticle.hh"
21#include "G4DynamicParticle.hh"
22#include "G4SDManager.hh"
23#include "G4SteppingManager.hh"
24//#include "BesSensitiveManager.hh"
25//#include "BesDetectorConstruction.hh"
28#include "G4RunManagerKernel.hh"
29
30using namespace std;
31
32/*
33 Constructor
34 */
35//Ext_track::Ext_track() : myMsgFlag(true),myBFieldOn(true),myGeomOptimization(true),m_dir(0),m_tofversion(2),myUseMucKal(true),myMucWindow(6)
36Ext_track::Ext_track() : myMsgFlag(true),myBFieldOn(true),myGeomOptimization(true),m_dir(0),m_detVer(1),myUseMucKal(true),myMucWindow(6)
37{
38 // BesSensitiveManager *besSensitiveManager = new BesSensitiveManager;
39 bes3DetectorConstruction = new ExtBesDetectorConstruction(myBFieldOn,m_detVer);
40 bes3WorldVolume = bes3DetectorConstruction->Construct();
41 extPhysicsList = new ExtPhysicsList;
42 extTrack = new G4Track;
43
44 //for geant4.8.1, move this line to Initialization, extSteppingAction = new ExtSteppingAction;
45 //extTrackingManager = new G4TrackingManager;
46
47 //RunManagerKernel for geant4.9.0
48 extRunManagerKernel = new G4RunManagerKernel;
49 extRunManagerKernel->DefineWorldVolume(bes3WorldVolume);
50 extRunManagerKernel->SetPhysics(extPhysicsList);
51 extTrackingManager=extRunManagerKernel->GetTrackingManager();
52}
53
54Ext_track::Ext_track(const bool msgFlag, const bool BFieldOn, const bool GeomOptimization, const int m_detVer,const bool aUseMucKal,const int aMucWindow) : myMsgFlag(msgFlag),myBFieldOn(BFieldOn),myGeomOptimization(GeomOptimization),m_dir(0),myUseMucKal(aUseMucKal),myMucWindow(aMucWindow)
55{
56 // BesSensitiveManager *besSensitiveManager = new BesSensitiveManager;
57 bes3DetectorConstruction = new ExtBesDetectorConstruction(myBFieldOn,m_detVer);
58 bes3WorldVolume = bes3DetectorConstruction->Construct();
59 extPhysicsList = new ExtPhysicsList;
60 extTrack = new G4Track;
61
62 //for geant4.8.1, move this line to Initialization, extSteppingAction = new ExtSteppingAction;
63 //extTrackingManager = new G4TrackingManager;
64 //RunManagerKernel for geant4.9.0
65 extRunManagerKernel = new G4RunManagerKernel;
66 extTrackingManager=extRunManagerKernel->GetTrackingManager();
67 extRunManagerKernel->DefineWorldVolume(bes3WorldVolume);
68 extRunManagerKernel->SetPhysics(extPhysicsList);
69 extTrackingManager=extRunManagerKernel->GetTrackingManager();
70}
71/*
72 Destructor
73 */
75{
76 //cout<<"delete 1"<<endl;
77 //if(extRunManagerKernel) delete extRunManagerKernel;
78 //if(extTrackingManager) delete extTrackingManager;
79 // if(extSteppingAction) delete extSteppingAction;
80 //cout<<"delete 2"<<endl;
81 if(extTrack) delete extTrack;
82 //cout<<"delete 3"<<endl;
83 if(bes3DetectorConstruction) delete bes3DetectorConstruction;
84 //cout<<"delete 4"<<endl;
85 if(extPhysicsList) delete extPhysicsList;
86 //cout<<"delete 5"<<endl;
87
88 // open geometry for deletion
89 G4GeometryManager::GetInstance()->OpenGeometry();
90
91 // deletion of Geant4 kernel classes
92 G4SDManager* fSDM = G4SDManager::GetSDMpointerIfExist();
93 if(fSDM)
94 {
95 delete fSDM;
96 }
97 //cout<<"delete 6"<<endl;
98}
99
100/*
101 Initialization member function.
102 */
103
104void Ext_track::Initialization( const bool aMsgFlag,const bool Bfield,const bool GeomOptimization,const bool aUseMucKal,const int aMucWindow)
105{
106 myMsgFlag=aMsgFlag;
107 myGeomOptimization = GeomOptimization;
108 myBFieldOn=Bfield,
109 myUseMucKal=aUseMucKal;
110 myMucWindow = aMucWindow;
111 //add for geant4.8.1
112 //G4ParticleTable::GetParticleTable()->SetReadiness();
113 //extPhysicsList->ConstructParticle();
114
115 if(myMsgFlag) cout << "Ext_track::Init will execute geant initialization." << endl;
116 //if(!GeometryInitialization()) cout << "Error in Ext_track::GeometryInitialization()" << endl;
117 //PhysicsInitialization();
118 extRunManagerKernel->InitializePhysics();
119 G4cout<<"dzy add output before extRunManagerKernel->RunInitialization()"<<G4endl;
120 extRunManagerKernel->RunInitialization();
121 G4cout<<"dzy add output after extRunManagerKernel->RunInitialization()"<<G4endl;
122
123 extSteppingAction = new ExtSteppingAction;
124 extSteppingAction->SetMsgFlag(aMsgFlag);
125 extSteppingAction->SetMucKalFlag(aUseMucKal);
126 extSteppingAction->SetMucWindow(aMucWindow);
127 //Set extSteppingAction
128 extTrackingManager->SetUserAction(extSteppingAction);
129
130
131
132}
133
134
135
136////////////////////////////////////////////////////////////////
137bool Ext_track::GeometryInitialization()
138{
139 if(myMsgFlag) cout << "Ext_track::GeometryInitialization()." << endl;
140
141 //for geant4.9.0, DefaultRegionForTheWorld has been defined in G4RunManagerKernel.
142 /*G4RegionStore* rStore = G4RegionStore::GetInstance();
143 G4Region* defaultRegion=rStore->GetRegion("DefaultRegionForTheWorld",false);
144 if(!defaultRegion)
145 {
146 defaultRegion=new G4Region("DefaultRegionForTheWorld");
147 defaultRegion->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
148 }*/
149 G4Region *defaultRegion=new G4Region("DefaultRegionForBesWorld");
150 defaultRegion->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
151
152 // The world volume MUST NOT have a region defined by the user
153 if(bes3WorldVolume->GetLogicalVolume()->GetRegion())
154 {
155 if(bes3WorldVolume->GetLogicalVolume()->GetRegion()!=defaultRegion)
156 {
157 cout << "The world volume has a user-defined region <"
158 << bes3WorldVolume->GetLogicalVolume()->GetRegion()->GetName()
159 << ">." << G4endl;
160 return 0;
161 }
162 }
163
164 // Remove old world logical volume from the default region, if exist
165 if(defaultRegion->GetNumberOfRootVolumes())
166 {
167 if(defaultRegion->GetNumberOfRootVolumes()>size_t(1))
168 {
169 cout <<"DefaultRegionHasMoreThanOneVolume,Default world region should have a unique logical volume."<<endl;
170 return 0;
171 }
172 std::vector<G4LogicalVolume*>::iterator lvItr = defaultRegion->GetRootLogicalVolumeIterator();
173 defaultRegion->RemoveRootLogicalVolume(*lvItr);
174 cout << (*lvItr)->GetName()
175 << " is removed from the default region." << endl;
176 }
177
178 // Set the default region to the world
179 G4LogicalVolume* bes3WorldLog = bes3WorldVolume->GetLogicalVolume();
180 bes3WorldLog->SetRegion(defaultRegion);
181 defaultRegion->AddRootLogicalVolume(bes3WorldLog);
182
183 // Set the world volume, notify the Navigator and reset its state
184 G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->SetWorldVolume(bes3WorldVolume);
185
186 return 1;
187}
188
189
190
191bool Ext_track::PhysicsInitialization()
192{
193 if(myMsgFlag) cout<<"Ext_track::PhysicsInitialization()."<<endl;
194 // Following line is tentatively moved from SetPhysics method
195 // G4ParticleTable::GetParticleTable()->SetReadiness();
196
197 // extPhysicsList->ConstructParticle();
198 extPhysicsList->Construct();
199 extPhysicsList->CheckParticleList();
200 extPhysicsList->SetCuts();
201
202 CheckRegions();
203
204 //update region
205 G4RegionStore::GetInstance()->UpdateMaterialList(bes3WorldVolume);
206 G4ProductionCutsTable::GetProductionCutsTable()->UpdateCoupleTable(bes3WorldVolume);
207
208 //Build PhysicsTables
209 if(myMsgFlag) cout<<"Build PhysicsTables"<<endl;
210 extPhysicsList->BuildPhysicsTable();
211 if(myMsgFlag) cout<<"Build PhysicsTables end."<<endl;
212 G4ProductionCutsTable::GetProductionCutsTable()->PhysicsTableUpdated();
213 extPhysicsList->DumpCutValuesTableIfRequested();
214
215 //Geometry Optimization
216 if(myGeomOptimization)
217 {
218 cout<<"Geometry Optimization,please wait for a few minutes."<<endl;
219 G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
220 geomManager->OpenGeometry();
221 geomManager->CloseGeometry(true, false);
222 }
223
224 return 1;
225}
226
227
228
229
230void Ext_track::CheckRegions()
231{
232 //add for geant4.8.1
233 G4RegionStore::GetInstance()->SetWorldVolume();
234
235 for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
236 {
237 G4Region* region = (*(G4RegionStore::GetInstance()))[i];
238 //add for geant4.8.1
239 if(region->GetWorldPhysical()!=bes3WorldVolume) continue;
240 G4ProductionCuts* cuts = region->GetProductionCuts();
241 if(!cuts)
242 {
243 region->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
244 }
245 }
246}
247
248
249
250/*
251 Set member function.
252 */
253
254bool Ext_track::Set( const Hep3Vector &xv3, const Hep3Vector &pv3,const HepSymMatrix &err, const std::string &particleName, const double pathInMDC, const double tofInMdc)
255{
256 if( err.num_row() != 6 ){ // ?static const int Ndim_err=6, see Ext_errmx.h line58
257 std::cerr << "%ERROR at Ext_track::Set. Dimension of error matrix: "
258 << err.num_row() << " should be 6" << std::endl;
259 exit( 0 );
260 }
261
262 m_vect[0] = xv3.x();// ?set starting position,private data
263 m_vect[1] = xv3.y();
264 m_vect[2] = xv3.z();
265
266 // m_errskip_flag = 0;
267 // m_errskip_level = 0;
268
269
270 // Check the starting point is inside the setup.
271 if(!CheckVertexInsideWorld(xv3)) return 0;
272
273 float p( pv3.mag() );
274 m_vect[3] = pv3.x()/p; //?set direction of momentum
275 m_vect[4] = pv3.y()/p;
276 m_vect[5] = pv3.z()/p;
277 m_vect[6] = p;
278
279 // check Particlename
280 if(particleName!="e+"&&particleName!="e-"&&
281 particleName!="mu+"&&particleName!="mu-"&&
282 particleName!="pi+"&&particleName!="pi-"&&
283 particleName!="kaon+"&&particleName!="kaon-"&&
284 particleName!="proton"&&particleName!="anti_proton"&&
285 particleName!="gamma")
286 {
287 std::cerr <<"Unknown or unconstructed Particle."<< std::endl;
288 return 0;
289 }
290
291
292 double mass;
293 double Q;
294
295 G4ParticleDefinition* particleDefinition=G4ParticleTable::GetParticleTable()->FindParticle(particleName);
296 Q = particleDefinition->GetPDGCharge();
297 mass = particleDefinition->GetPDGMass();
298
299
300 Hep3Vector xv( m_vect[0], m_vect[1], m_vect[2] );
301 Hep3Vector pv(m_vect[3]*m_vect[6], m_vect[4]*m_vect[6], m_vect[5]*m_vect[6]);
302
303 m_xp_err.set_err( err, xv, pv, Q, mass ); // Set error matrix.
304
305 extSteppingAction->SetXpErrPointer(&m_xp_err);
306 extSteppingAction->SetInitialPath(pathInMDC);
307 extSteppingAction->SetInitialTof(tofInMdc);
308
309 double betaInMDC = p/sqrt(mass*mass+p*p);//velocity
310 extSteppingAction->SetBetaInMDC(betaInMDC);
311 // double tofInMDC = pathInMDC/(betaInMDC*299.792458);
312 // if(myMsgFlag) cout<<"TOF in MDC: "<<tofInMDC<<endl;
313
314 // extSteppingAction->Reset();
315 extSteppingAction->MucReset();
316 // extTrack Initialization.
317
318 /* // comment 2008.04.07 due to memory loss
319 // Initialize a G4PrimaryParticle.
320 G4PrimaryParticle* primaryParticle = new G4PrimaryParticle(particleDefinition,pv3.x(),pv3.y(),pv3.z());
321 primaryParticle->SetMass(mass);
322 primaryParticle->SetCharge(Q);
323
324 // Initialize a G4DynamicParticle.
325 // G4DynamicParticle* DP = new G4DynamicParticle(particleDefinition,primaryParticle->GetMomentum());
326 // DP->SetPrimaryParticle(primaryParticle);
327 */
328 G4DynamicParticle* DP = new G4DynamicParticle(particleDefinition,pv);
329
330 delete extTrack; // add on 2008.04.07 to avoid memory loss
331 extTrack = new G4Track(DP,0.0,xv3);
332 // extTrack->CopyTrackInfo(G4Track::G4Track(DP,0.0,xv3));
333
334 // Reset navigator
335 Hep3Vector center(0,0,0);
336 G4Navigator* navigator= G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
337 navigator->LocateGlobalPointAndSetup(center,0,false);
338
339 return 1;
340}
341
342
343// check a position is in the setup
344bool Ext_track::CheckVertexInsideWorld(const Hep3Vector& pos)
345{
346 G4Navigator* navigator= G4TransportationManager::GetTransportationManager()-> GetNavigatorForTracking();
347
348 G4VPhysicalVolume* world= navigator-> GetWorldVolume();
349 G4VSolid* solid= world-> GetLogicalVolume()-> GetSolid();
350 EInside qinside= solid-> Inside(pos);
351
352 if( qinside != kInside) return false;
353 else return true;
354}
355
356
357
358
359
360
361//TrackExtrapotation
363{
364 extTrackingManager->ProcessOneTrack(extTrack);
365}
366
367
double mass
void SetMsgFlag(bool aMsgFalg)
void SetMucWindow(int aMucWindow)
void SetBetaInMDC(double aBeta)
void SetMucKalFlag(bool aMucKalFlag)
void SetInitialPath(double aPath)
void SetInitialTof(double aTof)
void SetXpErrPointer(Ext_xp_err *xpErr)
void TrackExtrapotation()
void Initialization(const bool aMsgFlag, const bool Bfield, const bool GeomOptimization, const bool aUseMucKal, const int aMucWindow)
bool Set(const Hep3Vector &xv3, const Hep3Vector &pv3, const HepSymMatrix &err, const std::string &particleName, const double pathInMDC, const double tofInMdc)
void set_err(const HepSymMatrix &err, const Hep3Vector &xv, const Hep3Vector &pv, const double &q, const double &mass)