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