G4Gen_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief A module for propagating particles through Geant4 and
3 /// generating energy depositions
4 /// \author messier@indiana.edu brebel@fnal.gov rhatcher@fnal.gov
5 ////////////////////////////////////////////////////////////////////////
6 
7 
8 // NOvA includes
9 #include "g4nova/G4Alg.h"
10 #include "g4nova/PrintTables.h"
12 #include "Simulation/FLSHitList.h"
13 #include "Simulation/Particle.h"
14 #include "Simulation/Simulation.h"
15 #include "Simulation/TrueEnergy.h"
16 #include "Simulation/G4Status.h"
18 #include "Utilities/AssociationUtil.h"
19 
20 #include <cassert>
21 #include <cstdlib>
22 #include <string>
23 #include <sstream>
24 #include <sys/stat.h>
25 #include <vector>
26 #include <map>
27 #include <unistd.h>
28 
29 // ROOT include
30 #include <TDatabasePDG.h>
31 #include <TSystem.h>
32 #include <TStopwatch.h>
33 
34 // Framework includes
37 #include "fhiclcpp/ParameterSet.h"
41 #include "cetlib/search_path.h"
44 
46 
47 namespace art {
48  class Event;
49  class ParameterSet;
50 }
51 
52 namespace g4n {
53 
54  /// A module to pass interaction information to Geant4 and
55  /// create hits and particle lists
56  class G4Gen : public art::EDProducer {
57  public:
58  explicit G4Gen(fhicl::ParameterSet const &pset);
59  virtual ~G4Gen();
60 
61  void produce (art::Event& evt);
62  void beginRun(art::Run& run);
63  void endJob();
64 
65  private:
67 
68  g4n::G4Alg* fG4Alg; ///< G4Helper object
69  fhicl::ParameterSet fG4AlgPSet; ///< parameter set to configure the G4Alg object
70  TStopwatch fStopwatch; ///< keep track of how long it takes to run the job
71 
72  bool fPrintTables; ///< Print the dE/dx tables and other info for debugging etc.?
73 
74  //bool fDoFLSHitListDebug; ///< Are we going to debug the FLSHitList?
75 
76  };
77 }
78 
79 namespace g4n {
80 
81  //___________________________________________________________________________
82  // Constructor
83  G4Gen::G4Gen(fhicl::ParameterSet const& pset)
84  : fG4Alg(0)
85  , fG4AlgPSet(pset.get< fhicl::ParameterSet >("G4AlgPSet") )
86  {
87  fGeneratorLabel = fG4AlgPSet.get<std::string>("GenModuleLabel");
88  fPrintTables = fG4AlgPSet.get<bool> ("PrintTables" );
89 
90  // get the random number seed, use a random default if not specified
91  // in the configuration file.
92  unsigned int seed = pset.get< unsigned int >("Seed", sim::GetRandomNumberSeed());
93  // setup the random number service for Geant4, the "G4Engine" label is a
94  // special tag setting up a global engine for use by Geant4/CLHEP
95  createEngine(seed, "G4Engine");
96 
97  // Start counting the time
98  fStopwatch.Start();
99 
100  produces< std::vector<sim::G4Status> >();
101  produces< std::vector<sim::FLSHitList > >();
102  produces< std::vector<sim::Particle> >();
103  produces< art::Assns<sim::Particle, simb::MCTruth> >();
104  produces< std::vector<sim::TrueEnergy> >();
105  produces< art::Assns<sim::TrueEnergy, sim::Particle> >();
106 
107  /// Setup the FLSHitList debugger
108  //fDoFLSHitListDebug = doG4Debug<FLSHitListDebug>();
109  // if(fDoFLSHitListDebug){
110 // mf::LogVerbatim("G4Gen") << "Will be Debugging FLSHitLists";
111 // }
112 // else{
113 // mf::LogVerbatim("G4Gen") << "Will NOT be Debugging FLSHitLists";
114 // }
115 
116 
117  }// end of constructor
118 
119  //___________________________________________________________________________
120  // Destructor
122  {
123  fStopwatch.Stop();
124  mf::LogVerbatim("G4Gen") << "real time to run through Geant4: "
125  << fStopwatch.RealTime();
126  if(fG4Alg) delete fG4Alg;
127  }// end of destructor
128 
129  //___________________________________________________________________________
131  {
132  fG4Alg = new G4Alg(fG4AlgPSet);
133 
134  return;
135  }// end of begin job
136 
137  //___________________________________________________________________________
139  {
140  if(fPrintTables) {
141  PrintTables PT;
142  PT.Materials();
143  PT.dEdx();
144  }
145 
146  return;
147  }// end of endJob
148 
149  //___________________________________________________________________________
151  {
152  // need to instantiate the G4Helper in order to make the
153  // G4RunManager and pass it the physics list - G4 really dislikes
154  // it if you try to make a G4UserRunAction (ie UserActionManager)
155  // before passing a physics list to the run manager
156 
157  // Define the FLSHitList and Particle vectors.
158  std::unique_ptr<std::vector<sim::G4Status> > g4status(new std::vector<sim::G4Status>(1) );
159  std::unique_ptr<std::vector<sim::FLSHitList> > flshlcol(new std::vector<sim::FLSHitList> );
160  std::unique_ptr<std::vector<sim::Particle> > pcol (new std::vector<sim::Particle> );
161  std::unique_ptr< art::Assns<sim::Particle, simb::MCTruth> > tpassn (new art::Assns<sim::Particle, simb::MCTruth>);
162 
163  std::unique_ptr<std::vector<sim::TrueEnergy> > trueens (new std::vector<sim::TrueEnergy>);
164  std::unique_ptr<art::Assns<sim::TrueEnergy, sim::Particle> > treassn (new art::Assns<sim::TrueEnergy, sim::Particle>);
165 
166  //look to see if there is any MCTruth information for this event
168  evt.getByLabel(fGeneratorLabel, mclist);
169 
170  // the next steps are a little clunky. Make a vector of art::Ptr<MCTruth>
171  // objects to use when making the associations and another of just
172  // const* MCTruth objects to pass to G4Alg
173  std::vector< art::Ptr< simb::MCTruth > > mctp;
174  std::vector< const simb::MCTruth* > mct;
175 
176  for(size_t i = 0; i < mclist->size(); ++i){
177  art::Ptr<simb::MCTruth> ptr(mclist, i);
178  mctp.push_back(ptr);
179  mct .push_back(ptr.get());
180  }
181 
182  // make a map to keep track of which G4 track ID goes with which MCTruth object
183  std::map<int, size_t> trackIDToMCTruthIndex;
184  fG4Alg->RunGeant(mct, *flshlcol, *pcol, trackIDToMCTruthIndex);
185 
186  // Check to see if Geant4 aborted during processing
187  g4status->at(0).SetAborted(fG4Alg->IsAborted());
188  if (g4status->at(0).IsAborted()) {
189  std::cout << "Geant4 aborted during processing. Flagged in G4Status data product." << std::endl;
190  }
191 
192  // Karl::Containment -- Let's look at the map which I have just made...
193  // --- Make an iterator to loop over fTrueDepMap
194  std::map< int, std::vector<float> >::iterator TrueMapIt;
195  // --- Iterate through my fTrueDepMap and create assns to the MCParticle list.
196  for (TrueMapIt = ParticleListAction::fTrueDepMap.begin(); TrueMapIt != ParticleListAction::fTrueDepMap.end(); ++TrueMapIt) {
197  // --- Loop through pcol until I find the right MCParticle.
198  for (unsigned int pp=0; pp<pcol->size(); ++pp) {
199  // --- Check if I have the correct MCParticle.
200  if ( (*pcol)[pp].TrackId() != TrueMapIt->first ) continue;
201  // --- If I'm here I should have the right particle so lets get it
202  sim::Particle &part = (*pcol)[ pp ];
203  // --- Check that I have done this correctly.
204  if (part.TrackId() != TrueMapIt->first) {
205  mf::LogVerbatim("G4Gen") << "Why doesn't TrueMapIt->first " << TrueMapIt->first << " equal part.TrackId() " << part.TrackId() << std::endl;
206  continue;
207  }
208  // Make a sim::TrueEnergy( TrackID, Entering Energy, Escaping Energy, Total Deposited Energy, Total Escaping energy )
209  sim::TrueEnergy ThisEn(TrueMapIt->first, TrueMapIt->second[0], TrueMapIt->second[1], TrueMapIt->second[2], TrueMapIt->second[3]);
210  (*trueens).push_back( ThisEn );
211  // And make the association!
212  util::CreateAssn( *this, evt, *treassn, *trueens, *pcol, pp, (trueens->size()-1) );
213  // --- Don't want to carry on looping through all of the particles!
214  break;
215  } // Loop through particles.
216  } // Iterator through my map.
217  // Karl::Containment -- Let's look at the map which I have just made...
218 
219  // make associations for the particles and MCTruth objects
220  int trackID = INT_MAX;
221  size_t mctidx = 0;
222 
223  for(size_t p = 0; p < pcol->size(); ++p){
224 
225  sim::Particle &part = (*pcol)[p];
226  trackID = part.TrackId();
227 
228  if( trackIDToMCTruthIndex.count(trackID) > 0){
229  mctidx = trackIDToMCTruthIndex.find(trackID)->second;
230  util::CreateAssn(*this, evt, *pcol, mctp[mctidx], *tpassn, p);
231  }
232  else
233  throw cet::exception("G4Gen") << "Cannot find MCTruth for Track Id: "
234  << trackID
235  << " to create association between Particle and MCTruth";
236 
237  }// end loop over handles
238 
239  // Put the data products into the event
240  evt.put(std::move(g4status));
241  evt.put(std::move(flshlcol));
242  evt.put(std::move(pcol));
243  evt.put(std::move(tpassn));
244 
245  evt.put(std::move(trueens));
246  evt.put(std::move(treassn));
247 
248  return;
249  }// end of produce
250 
251 } // end namespace
252 
253 namespace g4n { DEFINE_ART_MODULE(G4Gen) }
TruthSlim – remove generated objects that don&#39;t contribute.
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
void RunGeant(std::vector< art::Handle< std::vector< simb::MCTruth > > > &mclists, std::vector< sim::FLSHitList > &flshitlist, std::vector< sim::Particle > &particlelist, std::vector< std::vector< std::pair< size_t, size_t > > > &pListLimits)
Definition: G4Alg.cxx:280
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
bool fPrintTables
Print the dE/dx tables and other info for debugging etc.?
Definition: G4Gen_module.cc:72
const char * p
Definition: xmltok.h:285
base_engine_t & createEngine(seed_t seed)
bool IsAborted()
Definition: G4Alg.cxx:375
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
DEFINE_ART_MODULE(TestTMapFile)
static std::map< int, std::vector< float > > fTrueDepMap
Definition: Run.h:31
int TrackId() const
Definition: MCParticle.h:209
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
TString part[npart]
Definition: Style.C:32
unsigned int seed
Definition: runWimpSim.h:102
g4n::G4Alg * fG4Alg
G4Helper object.
Definition: G4Gen_module.cc:68
virtual ~G4Gen()
void produce(art::Event &evt)
T get(std::string const &key) const
Definition: ParameterSet.h:231
void endJob()
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
unsigned int GetRandomNumberSeed()
Definition: Simulation.cxx:13
void beginRun(art::Run &run)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T const * get() const
Definition: Ptr.h:321
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
An algorithm to pass interaction information to Geant4 and create hits and particle lists...
Definition: G4Alg.h:32
TStopwatch fStopwatch
keep track of how long it takes to run the job
Definition: G4Gen_module.cc:70
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
Int_t trackID
Definition: plot.C:84
std::string fGeneratorLabel
Definition: G4Gen_module.cc:66
fhicl::ParameterSet fG4AlgPSet
parameter set to configure the G4Alg object
Definition: G4Gen_module.cc:69