MdGroundPropagator.cc
Go to the documentation of this file.
1 #include "MdGroundPropagator.h"
3 #include "PhysicsList.h"
4 #include "SoilConstruction.h"
5 #include "PrimaryGenerator.h"
6 #include "SteppingAction.h"
7 
8 //Geant4 Managers
9 #include <G4RunManager.hh>
10 #include <G4UImanager.hh>
11 #include <G4VisManager.hh>
12 #include <G4VisExecutive.hh>
13 
14 #include <G4GeometryManager.hh>
15 #include <G4PhysicalVolumeStore.hh>
16 #include <G4LogicalVolumeStore.hh>
17 #include <G4SolidStore.hh>
18 
19 #include <fwk/CentralConfig.h>
20 #include <fwk/RandomEngineRegistry.h>
21 
22 #include <evt/Event.h>
23 #include <mevt/CounterSimData.h>
24 #include <mdet/Counter.h>
25 #include <det/Detector.h>
26 
27 #include <utl/ErrorLogger.h>
28 #include <utl/Reader.h>
29 #include <utl/Particle.h>
30 #include <utl/ShowerParticleIterator.h>
31 #include <utl/TimeDistribution.h>
32 #include <utl/TimeDistributionAlgorithm.h>
33 #include <utl/AugerUnits.h>
34 
35 #include <cstddef>
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
39 
40 #include <CLHEP/Random/Random.h>
41 
42 #include <tls/Geant4Manager.h>
43 
44 using namespace utl;
45 using namespace fwk;
46 using namespace std;
47 using namespace evt;
48 using namespace mevt;
49 using namespace mdet;
50 using namespace det;
51 using namespace GroundPropagatorAG;
52 
53 const mdet::Counter* GroundPropagator::fCurrentDetectorCounter = 0;
54 MEvent::CounterIterator GroundPropagator::fCurrentEventCounterIt;
55 CounterSimData::GrdParticleIterator GroundPropagator::fCurrentParticleIt;
56 
57 GroundPropagator::GroundPropagator() :
58  fEventId(""),
59  fUImanager(0),
60  fVisManager(0),
61  fSteppingAction(0)
62 {
63  fGeomVisOn = false;
64  fTrajVisOn = false;
65  fUseGlobalPhysicsList = false;
66 
67 }
68 
70 
73 {
74  INFO("\n++++\nReading XML parameters");
75  //Top branch
76  Branch topB = CentralConfig::GetInstance()->GetTopBranch("MdGroundPropagator");
77 
78  int verbosity = 0;
79  string filename;
80  string physList;
81  //Verbosity branch
82  Branch verB = topB.GetChild("verbosity");
83  verB.GetChild("level").GetData(verbosity);
84 #ifdef AMIGA_DEBUG
85  verB.GetChild("statsfile").GetData(filename);
86  fStatFile.open( filename.c_str() );
87  if( !fStatFile ) {
88  G4cerr << "Error opening file " << filename << G4endl;
89  return eFailure;
90  }
91  string strfname( filename );
92  size_t found = strfname.rfind ( ".ascii" );
93  if( found != string::npos )
94  strfname = strfname.substr( 0, found) + "_undergrd" + strfname.substr( found , string::npos);
95  cout << "Ascii Underground filename " << strfname << endl;
96  fStatUFile.open ( strfname.c_str() , ios_base::out );
97  if( ! fStatUFile.is_open() ) {
98  cout << "Error opening file: " << strfname << endl;
99  return eFailure;
100  }
101  fStatUFile << "#Id PartName Energy(MeV) PDGCode Time(ns) TrackId X Y Z Parent\n";
102 #endif
103  //Soil branch
104  Branch grmB = topB.GetChild("soil");
105  grmB.GetChild("grammage").GetData(fGrammage);
106  G4cerr << "fGrammage " << fGrammage/(utl::g/utl::cm2) << G4endl;
107 
108  //Visualization branch
109  Branch visB = topB.GetChild("visualization");
110  visB.GetChild("geometry").GetData(fGeomVisOn);
111  visB.GetChild("trajectories").GetData(fTrajVisOn);
112  //Physics list branch
113  Branch phyB = topB.GetChild("physics");
114  phyB.GetChild("list").GetData(physList);
115 
116  Branch globalPhysicsListB = topB.GetChild("globalPhysicsList");
117  globalPhysicsListB.GetData(fUseGlobalPhysicsList);
118 
119 
120  if ( (fGeomVisOn || fTrajVisOn) && !fVisManager){
121  fVisManager = new G4VisExecutive();
122  }
123  if (!fRunManager) {
125  fRunManager = tls::Geant4Manager::GetInstance().GetRunManager();
126  else
127  fRunManager = new G4RunManager;
128  }
129 
130  fUImanager = G4UImanager::GetUIpointer();
131  stringstream oss (ostringstream::out);
132 
133  oss << "/run/verbose " << verbosity;
134  fUImanager->ApplyCommand( oss.str() );
135 
136  oss.str("");
137  oss << "/event/verbose " << verbosity;
138  fUImanager->ApplyCommand( oss.str() );
139 
140  oss.str("");
141  oss << "/tracking/verbose " << verbosity;
142  fUImanager->ApplyCommand( oss.str() );
143 
145  tls::Geant4Manager::GetInstance().AddVisManager(fVisManager);
146 
147  INFO("using global PhysicsList from Geant4Manager");
148  PhysicsListCustomization* physicsListCustomization = new PhysicsListCustomization();
149 
150  INFO("\n++++\nConstructing G4 ground");
152 
153  tls::Geant4Customization custom("MdGroundPropagator", soil, physicsListCustomization);
154 
155  INFO("\n++++\nInitializing G4 stepping action");
156  fSteppingAction = new SteppingAction(soil->GetDepth());
158 
159  INFO("\n++++\nInitializing G4 primary generator");
160  custom.SetPrimaryGenerator(new PrimaryGenerator(filename));
161 
162  tls::Geant4Manager::GetInstance().AddCustomization(custom);
163 
164  }else{
165  INFO("using PhysicsList from MdGroundPropagator");
166  fRunManager->SetUserInitialization(new PhysicsList);
167 
168  INFO("\n++++\nConstructing G4 ground");
170  fRunManager->SetUserInitialization(soil);
171 
172  INFO("\n++++\nInitializing G4 stepping action");
173  fSteppingAction = new SteppingAction(soil->GetDepth());
174  fRunManager->SetUserAction(fSteppingAction);
175 
176  INFO("\n++++\nInitializing G4 primary generator");
177  fRunManager->SetUserAction(new PrimaryGenerator(filename));
178 
179  fUImanager->ApplyCommand("/physlist/CutsAll 1 mm");
180 
181  //configure physicsList
182  oss.str("");
183  oss << "/physlist/Physics " << physList;
184  fUImanager->ApplyCommand( oss.str() );
185  }
186 
187  if ( fGeomVisOn || fTrajVisOn ) {
188  fVisManager->Initialize();
189  fUImanager->ApplyCommand("/vis/open HepRepFile");
190  fUImanager->ApplyCommand("/vis/scene/create");
191  fUImanager->ApplyCommand("/vis/sceneHandler/attach");
192  fUImanager->ApplyCommand("/vis/scene/add/volume");
193  fUImanager->ApplyCommand("/vis/drawVolume");
194  fUImanager->ApplyCommand("/vis/scene/notifyHandlers");
195  fUImanager->ApplyCommand("/vis/viewer/update");
196  }
197  if (fTrajVisOn) {
198  fUImanager->ApplyCommand("/tracking/storeTrajectory 1");
199  fUImanager->ApplyCommand("/vis/scene/add/trajectories rich");
200  }
201 
202  return eSuccess;
203 }
204 
207 {
208  INFO("\n++++\nStarting MdGroundPropagator run");
209 
210  if (!event.HasMEvent()) {
211  ERROR("\n++++\nMEvent does not exist.");
212  return eFailure;
213  }
214 
215  // Get the MEvent
216  MEvent& mEvent = event.GetMEvent();
217 
218  if (mEvent.GetNumberOfCounters() > 0) {
219  INFO("\n++++\nActivating G4 for this run");
221  fRunManager->Initialize();
222  } else {
223  tls::Geant4Manager::GetInstance().Customize("MdGroundPropagator");
224  }
225  }
226 
227  // Get the MDetector
228  const MDetector& mDetector = Detector::GetInstance().GetMDetector();
229 
230  for (MEvent::CounterIterator cIt = mEvent.CountersBegin();
231  cIt != mEvent.CountersEnd(); ++cIt) {
232 
233  if (cIt->HasSimData()) {
234 
235  CounterSimData& cSimData = cIt->GetSimData();
236 
237  const int counterId = cIt->GetId();
238  const unsigned long numParticles = cSimData.GetNumberOfGrdParticles();
239 
240  G4cerr << numParticles << " particles in Counter Id :" << counterId << G4endl;
241 
242  if (numParticles)
243  G4cerr << "Id "<< counterId << "->inj. part " << numParticles << " : " << flush;
244  else
245  continue;
246 
248  fCurrentDetectorCounter = &mDetector.GetCounter(counterId);
249 
251  pIt != cSimData.GrdParticlesEnd(); ++pIt) {
252 
253  fCurrentParticleIt = pIt;
254  //utl::Particle * parent = new Particle(*fCurrentParticleIt);
255  smartPointer parent( new Particle(*fCurrentParticleIt) );
257 
258  //Run G4 simulation
259  fRunManager->BeamOn(1);
260  }//end loop on particles
261 
263  //Remove particles on-ground (NO MORE NECESSARY WE HAVE TWO LISTS NOW)
264  //cSimData.ClearGrdParticleList();
265 
266  //Loop on particles that reached the MD underground level
267  //and store them into the CounterSimData class
268  cout << "Counter " << counterId << " has " << fSteppingAction->fPartColl.size() << " particles on top\n";
270  for(pCollIt = fSteppingAction->fPartColl.begin(); pCollIt != fSteppingAction->fPartColl.end(); ++pCollIt)
271  {
272  PartData pData = (*pCollIt);
273  /*
274  cout << "pData.Pname " << pData.Pname << " - " << pData.KinE/CLHEP::MeV << " (MeV)" << endl;
275  cout << "pData.PDGCode " << pData.PDGCode << " - " << pData.Time/CLHEP::ns << " (ns)" << endl;
276  cout << "pData.PParent->GetType() " << pData.PParent->GetType() << " - " << (pData.PParent->GetType()==utl::Particle::eMuon||pData.PParent->GetType()==utl::Particle::eAntiMuon?"SHOWER MUON!!!!":"OTHER PARTICLE")<< endl;
277  cout << "pData.TrackID " << pData.TrackID << endl;
278 
279  cout << "pData.x " << pData.x/CLHEP::m << endl;
280  cout << "pData.y " << pData.y/CLHEP::m << endl;
281  cout << "pData.z " << pData.z/CLHEP::m << endl;
282 
283  cout << "pData.px " << pData.px << endl;
284  cout << "pData.py " << pData.py << endl;
285  cout << "pData.pz " << pData.pz << endl;
286  */
287 #ifdef AMIGA_DEBUG
288  fStatUFile << counterId << " "
289  << pData.Pname << " "
290  << pData.KinE/CLHEP::MeV << " "
291  << pData.PDGCode << " "
292  << pData.Time/CLHEP::ns << " "
293  << pData.TrackID << " "
294  << pData.x/CLHEP::m << " "
295  << pData.y/CLHEP::m << " "
296  << pData.z/CLHEP::m << " "
297  << pData.PParent->GetType() << " "
298  << endl;
299 #endif
300 
301  Point pPos( pData.x/CLHEP::m * utl::m, pData.y/CLHEP::m * utl::m, pData.z/CLHEP::m * utl::m, CounterCS);
302  Vector pDir( pData.px, pData.py, pData.pz, CounterCS );
303  //Creates new particle
304  Particle part( pData.PDGCode, utl::Particle::eShower, pPos, pDir, pData.Time/CLHEP::ns*utl::ns, 1, pData.KinE/CLHEP::MeV * utl::MeV );
305  part.SetParent( *pData.PParent );
306  cSimData.AddUGrdParticle( part );
307  }
308 
309  fSteppingAction->fPartColl.clear();
310  }//end if cIt->HasSimData()
311  }//end loop on counters with particles on ground
312 
313  return eSuccess;
314 }
315 
318 {
319 #ifdef AMIGA_DEBUG
320  fStatFile.close();
321  fStatUFile.close();
322 #endif
323  if (fUseGlobalPhysicsList) {
324  tls::Geant4Manager::GetInstance().NotifyDelete();
325  } else {
326  if (fRunManager) {
327  delete fRunManager;
328  fRunManager = 0;
329  }
330  }
331 
332  return eSuccess;
333 }
Branch GetTopBranch() const
Definition: Branch.cc:63
boost::indirect_iterator< InternalGrdParticleIterator, utl::Particle & > GrdParticleIterator
int GetNumberOfCounters() const
Definition: MEvent.h:46
CounterConstIterator CountersBegin() const
Definition: MEvent.h:49
Point object.
Definition: Point.h:32
void AddUGrdParticle(const utl::Particle &particle)
void SetPrimaryGenerator(G4VUserPrimaryGeneratorAction *const action)
bool HasMEvent() const
Report success to RunController.
Definition: VModule.h:62
GrdParticleIterator GrdParticlesBegin()
boost::shared_ptr< utl::Particle > smartPointer
Describes a particle for Simulation.
Definition: Particle.h:26
static mevt::MEvent::CounterIterator fCurrentEventCounterIt
utl::CoordinateSystemPtr GetLocalCoordinateSystem() const
Local system based on position and configured rotations.
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
fwk::VModule::ResultFlag Finish()
Finish: invoked at end of the run (NOT end of the event)
vector< t2list > out
output of the algorithm: a list of clusters
Definition: XbAlgo.cc:32
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
Detector associated to muon detector hierarchy.
Definition: MDetector.h:32
constexpr double MeV
Definition: AugerUnits.h:184
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
fwk::VModule::ResultFlag Run(evt::Event &theEvent)
Run: invoked once per event.
Class representing a document branch.
Definition: Branch.h:107
const double ns
static mevt::CounterSimData::GrdParticleIterator fCurrentParticleIt
static const mdet::Counter * fCurrentDetectorCounter
constexpr double g
Definition: AugerUnits.h:200
G4RunManager * fRunManager
Private G4 members.
Data structure to hold the different Geant4 global objects required to run a single module...
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
Root detector of the muon detector hierarchy.
Counter level simulation data.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
InternalCounterCollection::ComponentIterator CounterIterator
Definition: MEvent.h:31
allow customization of the standard global PhysicsList that are handled by the Geant4Manager ...
Vector object.
Definition: Vector.h:30
Report failure to RunController, causing RunController to terminate execution.
Definition: VModule.h:64
CounterConstIterator CountersEnd() const
Definition: MEvent.h:52
constexpr double ns
Definition: AugerUnits.h:162
void SetParent(Particle &parent)
Definition: Particle.h:150
char * filename
Definition: dump1090.h:266
const Counter & GetCounter(int id) const
Retrieve Counter by id.
Definition: MDetector.h:68
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
constexpr double m
Definition: AugerUnits.h:121
Root of the Muon event hierarchy.
Definition: MEvent.h:25
void SetSteppingAction(G4UserSteppingAction *const action)
GrdParticleIterator GrdParticlesEnd()
fwk::VModule::ResultFlag Init()
Initialize: invoked at beginning of run (NOT beginning of event)
constexpr double cm2
Definition: AugerUnits.h:118
unsigned int GetNumberOfGrdParticles() const

, generated on Tue Sep 26 2023.