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