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