GArG4Ana_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file GArG4.cxx
3 /// \brief Use Geant4 to run the GArSoft detector simulation
4 ///
5 /// \author brebel@fnal.gov
6 ////////////////////////////////////////////////////////////////////////
7 #ifndef GARG4GArG4ANA_H
8 #define GARG4GArG4ANA_H
9 
10 /// Framework includes
14 #include "fhiclcpp/ParameterSet.h"
17 #include "art_root_io/TFileService.h"
18 #include "art_root_io/TFileDirectory.h"
22 #include "cetlib_except/exception.h"
23 
24 // nutools includes
25 #include "nug4/ParticleNavigation/ParticleList.h"
26 
27 // GArSoft Includes
28 #include "MCCheater/BackTracker.h"
30 #include "Geometry/GeometryGAr.h"
31 #include "CoreUtils/ServiceUtil.h"
32 
33 // ROOT includes
34 #include <TTree.h>
35 
36 // C++ Includes
37 #include <iostream>
38 #include <cstring>
39 #include <sys/stat.h>
40 
41 typedef struct{
42  int run;
43  int subrun;
44  int event;
45 } EventInfo;
46 
47 typedef struct{
48  int trackID;
49  int pdg;
50  float x;
51  float y;
52  float z;
53  float e;
54  float dX;
55  float t;
56 } EnergyDep;
57 
58 typedef struct{
59  int trackID;
60  int pdg;
61  float length;
62  float energy;
63 } ParticleInfo;
64 
65 namespace simb{
66  class MCTruth;
67 }
68 
69 namespace sim{
70  class ParticleList;
71 }
72 
73 ///Geant4 interface
74 namespace gar {
75  namespace garg4 {
76 
77  class GArG4Ana : public ::art::EDAnalyzer{
78  public:
79 
80  /// Standard constructor and destructor for an FMWK module.
81  explicit GArG4Ana(fhicl::ParameterSet const& pset);
82  virtual ~GArG4Ana();
83 
85  void beginJob();
86  void reconfigure(fhicl::ParameterSet const& pset);
87 
88  private:
89 
90  std::string fG4ModuleLabel; ///< module label for the Geant
91  std::string fTruthModuleLabel; ///< module label for the Geant
92 
93  TTree* fEDepTree; ///< Tree to keep track of sanity check info
94  TTree* fParticleTree; ///< Tree to keep track of sanity check info
95  EventInfo fEvt; ///< Struct containing event identification
96  EnergyDep fEDep; ///< Struct containing energy deposition info
97  ParticleInfo fPartInfo; ///< Struct containing particle info
98  };
99 
100  } // namespace garg4
101 
102  namespace garg4 {
103 
104  //-----------------------------------------------------------------------
105  // Constructor
106  GArG4Ana::GArG4Ana(fhicl::ParameterSet const& pset)
107  : EDAnalyzer(pset)
108  {
109  this->reconfigure(pset);
110  }
111 
112  //-----------------------------------------------------------------------
113  // Destructor
115  {
116  }
117 
118  //-----------------------------------------------------------------------
120  {
122 
123  fEDepTree = tfs->make<TTree>("EDepTree", "EDepTree" );
124  fParticleTree = tfs->make<TTree>("ParticleTree", "ParicleTree");
125 
126  std::string description("run/I:subrun/I:event/I");
127 
128  fEDepTree ->Branch("info", &fEvt, description.c_str());
129  fParticleTree->Branch("info", &fEvt, description.c_str());
130 
131  description = "trackID/I:pdg/I:x/F:y/F:z/F:e/F:dX/F:t/F";
132 
133  fEDepTree->Branch("edep", &fEDep, description.c_str());
134 
135  description = "trackID/I:pdg/I:length/F:energy/F";
136 
137  fParticleTree->Branch("part", &fPartInfo, description.c_str());
138 
139  }
140 
141  //-----------------------------------------------------------------------
143  {
144  fG4ModuleLabel = p.get< std::string >("GeantModuleLabel");
145  fTruthModuleLabel = p.get< std::string >("TruthModuleLabel");
146 
147  return;
148  }
149 
150  //-----------------------------------------------------------------------
152  {
153  // set the event id info
154  fEvt.run = evt.run();
155  fEvt.subrun = evt.subRun();
156  fEvt.event = evt.id().event();
157 
158  // get the list of particles from this event
160 
161  // get the energy depositions for this event
162  auto edepsCol = evt.getValidHandle<std::vector<gar::sdp::EnergyDeposit> >(fG4ModuleLabel);
163 
164  if( edepsCol.failedToGet() ){
165  MF_LOG_VERBATIM("GArG4Ana")
166  << "failed to get energy depositions, bail";
167  return;
168  }
169 
170  sim::ParticleList* pList = bt->GetParticleList();
171 
172  sim::ParticleList::iterator itr = pList->begin();
173  for (; itr!=pList->end(); ++itr) {
174  auto part = itr->second;
175  if( !part ) continue;
176 
177  fPartInfo.trackID = part->TrackId();
178  fPartInfo.pdg = part->PdgCode();
179  fPartInfo.energy = part->E();
180  fPartInfo.length = part->Trajectory().TotalLength();
181 
182  MF_LOG_VERBATIM("GArG4Ana")
183  << part->TrackId()
184  << " "
185  << part->PdgCode()
186  << " "
187  << part->E()
188  << " "
189  << part->NumberTrajectoryPoints()
190  << " "
191  << part->Trajectory().TotalLength();
192 
193  fParticleTree->Fill();
194  }
195 
196  // loop over the energy deposition collections to fill the tree
197  for(auto edep : *edepsCol){
198 
199  // get the MCParticle for this track ID
200  auto part = bt->TrackIDToParticle(edep.TrackID());
201 
202  fEDep.trackID = edep.TrackID();
203  fEDep.pdg = part->PdgCode();
204  fEDep.x = edep.X();
205  fEDep.y = edep.Y();
206  fEDep.z = edep.Z();
207  fEDep.dX = edep.dX();
208  fEDep.t = edep.Time();
209  fEDep.e = edep.Energy();
210 
211  MF_LOG_DEBUG("GArG4Ana")
212  << "pos: ("
213  << fEDep.x
214  << ", "
215  << fEDep.y
216  << ", "
217  << fEDep.z
218  << ") e: "
219  << fEDep.e
220  << " t: "
221  << fEDep.t
222  << " dX: "
223  << fEDep.dX;
224 
225  // make the tree flat in terms of the energy depositions
226  fEDepTree->Fill();
227 
228  } // end loop over collection of EnergyDeposits
229 
230  return;
231  } // analyze
232 
233  } // namespace garg4
234 
235  namespace garg4 {
236 
238 
239  } // namespace garg4
240 
241 } // gar
242 
243 #endif // GARG4GARG4H
244 
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:69
intermediate_table::iterator iterator
void analyze(const ::art::Event &evt)
std::string string
Definition: nybbler.cc:12
TTree * fEDepTree
Tree to keep track of sanity check info.
EnergyDep fEDep
Struct containing energy deposition info.
ParticleInfo fPartInfo
Struct containing particle info.
std::string fTruthModuleLabel
module label for the Geant
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
bt
Definition: tracks.py:83
void beginJob()
Definition: Breakpoints.cc:14
virtual void reconfigure(fhicl::ParameterSet const &pset)
T get(std::string const &key) const
Definition: ParameterSet.h:271
Framework includes.
p
Definition: test.py:223
Code to link reconstructed objects back to the MC truth information.
Base utilities and modules for event generation and detector simulation.
std::string fG4ModuleLabel
module label for the Geant
void reconfigure(fhicl::ParameterSet const &pset)
General GArSoft Utilities.
#define MF_LOG_VERBATIM(category)
Definition: types.h:32
#define MF_LOG_DEBUG(id)
TCEvent evt
Definition: DataStructs.cxx:7
art framework interface to geometry description
TTree * fParticleTree
Tree to keep track of sanity check info.
EventInfo fEvt
Struct containing event identification.