All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParticleInventory.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // ParticleInventory.h
3 // Provide a single interface for building and accessing truth information from events for backtracking services.
4 //
5 // author jason.stock@mines.sdsmt.edu
6 // Based on the original BackTracker by Brian Rebel (brebel@fnal.gov)
7 //
8 // This module may look strange at first glance beacause of the mutable
9 // object in const functions whose sole purpose is to change the mutable
10 // objects.
11 // This is done because the returns from the ParticleInventory really
12 // should be const, and anything called from them must then also be const,
13 // and finally we get to the mutables. These are cached objects to prevent
14 // repeated and costly access to objects in the event.
15 //
16 ////////////////////////////////////////////////////////////////////////
17 
18 ////////////////////////////////////////////////////////////////////////
19 //DOXYGEN DOCUMENTATION
20 ////////////////////////////////////////////////////////////////////////
21 /**
22  * \file ParticleInventory.h
23  * \author jason.stock@mines.sdsmt.edu
24  * \brief Header for the ParticleInvenotry Service Provider.
25  *
26  * The ParticleInventory is an art independent service provider
27  * for retreiving truth information about tracks and particles.
28  * The ParticleInventory, BackTracker, and PhotonBackTracker make
29  * a complete toolset for retreiving truth information from an event.
30  */
31 
32 /** \struct cheat::ParticleInventory::ParticleInventoryConfig
33  * \brief FHICL Validation Object
34  * This struct is used for loading the fhicl configuration.
35  */
36 /** \var cheat::ParticleInventory::ParticleInventoryConfig::G4ModuleLabel
37  * \brief An atom.
38  * FHICL Atom for retreiving the module label to be used in retreiving
39  * information from the art event.
40  * */
41 /** \struct cheat::ParticleInventory::MCTObjects
42  * \brief A simple struct to contain the MC Truth information.
43  * \var cheat::ParticleInventory::MCTObjects::fMCTruthList;
44  * \brief A vector containing the MCTruth objects
45  * \var cheat::ParticleInventory::MCTobjects::fTrackIdToMCTruthIndex
46  * \brief a map linking trackIds to the location of MCTruth for fast lookup.
47  * */
48 
49 /** \fn void cheat::ParticleInventory::PrepEvent ( const Evt& evt )
50  * \brief Function to set up the ParticleInventory state for an event.
51  * This is a function to tell the ParticleInventory to prepare itself to work with a particular event.
52  * @param evt
53  * \brief The event the ParticleInventory should work with. *Note. This use breaks the multithreading model because the service has a "state".
54  * */
55 
56 /** \fn bool cheat::ParticleInventory::ParticleListReady() const
57  * \brief A simple check to determine if the ParticleList has already been prepared for this event or not.
58  * */
59 /** \fn bool cheat::ParticleInventory::MCTruthListReady() const
60  * \brief A simple check to determine if the MCTruthList has already been prepared and cached or not.
61  * */
62 /** \fn bool cheat::ParticleInventory::TrackIdToMCTruthReady() const
63  * \brief A simple check to determine if the TrackIdToMCTruth map has been prepared or not.
64  * */
65 /** \fn void cheat::ParticleInventory::PrepParticleList (const Evt& evt ) const
66  * \brief A function to load the ParticleList and cache it
67  * This function will find the particle list and load it for later use.
68  * Ideally this would would be used for a "lazy" loading of the backtracker,
69  * but this does not work in the current setup of art.
70  * */
71 /** \fn void cheat::ParticleInventory::PrepTrackIdToMCTruthIndex(const Evt& evt ) const
72  * \brief A function to prepare and cache a map of TrackIds and MCTruth object indicies from fMCTruthList.
73  * */
74 /** \fn void cheat::ParticleInventory::PrepMCTruthList (const Evt& evt ) const
75  * \brief A function to load and cache the MCTruthList of the event.
76  * */
77 /** \fn void cheat::ParticleInventory::PrepMCTruthListAndTrackIdToMCTruthIndex(const Evt& evt ) const
78  * \brief A function to make both PrepTrackIdToMCTruthIndex and PrepMCTruthList run when both are needed.
79  * */
80 /** \fn bool cheat::ParticleInventory::CanRun(const Evt& evt) const
81  * \brief A short function to check if use of the backtracker is appropriate or not based on the type of input file.
82  * This function simply checks to see if the file loaded is real data, or MC Simulation, as backtracking on real data makes no sense.
83  * If one does try to backtrack real data, this will throw and exception.
84  * */
85 /** \fn const sim::ParticleList& ParticleList() const
86  * \brief Get the ParticleList from an event.
87  * */
88 /** \fn void SetEveIdCalculator(sim::EveIdCalculator *ec)
89  * \brief Set the EveIdCalculator to use for this ParticleList.
90  * Set the EveIdCalculator to use for this ParticleList. If you are
91  * going to over-ride the default EveIdCalculator, you must do it for
92  * EVERY event (as a new particle list is adopted for each event.
93  */
94 /** \fn const std::vector< art::Ptr<simb::MCTruth> >& MCTruthList() const
95  * \brief Return a const reference to the list of MCTruth information from the event.
96  * This function provides a safe way for the user to access the complete MCTruth list
97  * as retrieved from the event.
98  */
99 /** \fn const std::map<unsigned short, unsigned short >& TrackIdToMCTruthIndex() const
100  * \brief A map of TrackIds to Their coresponding MCTruth information.
101  * This returns the ParticleInventories internally maintained map of TrackID information to the index of the stored MCTruth information. While I toyed with using pointers or other similarly explicit references to the MCTruth information, this method won out for ease of use and low memory requirement.
102  */
103 /** \fn void ClearEvent()
104  * \brief This function clears previosly cached information from the previous event. *Note, manually calling this function can cause the ParticleInventory and services depending on the ParticleInventory to behave incorrectly.
105  */
106 /** \fn const simb::MCParticle* TrackIdToParticle_P(int const& id) const
107  * \brief Return a pointer to an MCParticle in the event that produced a given track.
108  * Returns a pointer (not an art::Ptr) to a particle in the event.
109  */
110 /** \fn simb::MCParticle TrackIdToParticle(int const& id) const
111  * \brief Return a copy of the MCParticle that produced a given track.
112  * Return a copy of the MCParticle that produced a given track. To conserve memory
113  * users are encouraged to use TrackIdToParticle_P
114  */
115 /** \fn const simb::MCParticle* TrackIdToMotherParticle_P(int const& id) const
116  * \brief Return a pointer to the MCParticle that is mother to the particle which created a given track.
117  */
118 /** \fn simb::MCParticle TrackIdToMotherParticle(int const& id) const
119  * \brief Returns a copy of the MCParticle that is mother to the particle which created a given track.
120  * Returns a copy of the MCParticle that is mother to the particle which created a given track. Due to
121  * memory conservation, users are encouraged to use TrackIdToMotherParticle_P
122  */
123 /** \fn const art::Ptr<simb::MCTruth>& TrackIdToMCTruth_P(int const& id) const
124  * \brief Return an art::Ptr to an MCTruth object in the event that caused a given Track.
125  *
126  */
127 /** \fn simb::MCTruth TrackIdToMCTruth (int const& id) const
128  * \brief Return a copy of an MCTruth object in the event that caused a given Track.
129  * Return a copy of an MCTruth object in the event that caused a given Track. Users are encouraged
130  * to instead use TrackIdToMCTruth_P
131  */
132 /** \fn int TrackIdToEveTrackId(const int& tid) const { return fParticleList.EveId(tid)
133  * \brief Return the TrackId of the primary that ultimately created the particle that made the given TrackId.
134  */
135 /** \fn const art::Ptr<simb::MCTruth>& ParticleToMCTruth_P(const simb::MCParticle* p) const
136  * \brief Return an art::Ptr to the simb::MCTruth object that ultimately made the given particle
137  */
138 /** \fn simb::MCTruth ParticleToMCTruth (const simb::MCParticle* p) const
139  * \brief Return a copy of the MCTruth object that ultimately resulted in the given particle
140  */
141 /** \fn const std::vector< art::Ptr<simb::MCTruth> >& MCTruthVector_Ps() const
142  * \brief Get a list of pointers to the MCTruth objects in the event
143  */
144 /** \fn const std::vector<const simb::MCParticle*> MCTruthToParticles_Ps(art::Ptr<simb::MCTruth> const& mct) const
145  * \brief Get pointers to all particles that resulted from the MCTruth object in the given art::Ptr
146  */
147 /** \fn std::set<int> GetSetOfTrackIds() const
148  * \brief Get all TrackIds in the event
149  */
150 /** \fn std::set<int> GetSetOfEveIds() const
151  * \brief Get all TrackIds of Primary particles in the event
152  */
153 
154 #ifndef CHEAT_PARTICLEINVENTORY_H
155 #define CHEAT_PARTICLEINVENTORY_H
156 
157 #include <map>
158 #include <vector>
159 
160 #include "canvas/Persistency/Common/Ptr.h"
161 #include "canvas/Utilities/InputTag.h"
162 #include "fhiclcpp/types/Atom.h"
163 namespace fhicl { class ParameterSet; }
164 
165 namespace simb { class MCParticle; }
166 #include "nusimdata/SimulationBase/MCTruth.h"
167 #include "nug4/ParticleNavigation/ParticleList.h"
168 
169 namespace cheat{
171  {
172  public:
174  fhicl::Atom<art::InputTag> G4ModuleLabel{
175  fhicl::Name("G4ModuleLabel"),
176  fhicl::Comment("The label of the LArG4 module used to produce the art file we will be examining"),
177  "largeant"};
178  fhicl::Atom<std::string> EveIdCalculator{
179  fhicl::Name("EveIdCalculator"),
180  fhicl::Comment("For selecting which EveID caclulator to use at initialization."),
181  "EmEveIdCalculator"};
182  fhicl::Atom<bool> OverrideRealData{
183  fhicl::Name("OverrideRealData"),
184  fhicl::Comment("Option when overlaying simulation on real data, to tell the backtracker to continue even if event looks like data."),
185  false};
186  };
187 
188  //using provider_type = ParticleInventory;
189  //cheat::ParticleInventory const* provider() const
190  //{ return static_cast<cheat::ParticleInventory const*>(this); }
191 
192  ///////////Constructor///////////////
193  ParticleInventory(const ParticleInventoryConfig& config );
194  ParticleInventory(const fhicl::ParameterSet& pSet );
195  ParticleInventory(ParticleInventory const&) = delete;
196 
197  template<typename Evt> //Template must be decalred and defined outside of the .cpp file.
198  void PrepEvent ( const Evt& evt );
199 
200  bool ParticleListReady() const { return !( fParticleList.empty() ); }
201  bool MCTruthListReady() const { return !( (fMCTObj.fMCTruthList).empty() ); }
202  bool TrackIdToMCTruthReady() const { return !(fMCTObj.fTrackIdToMCTruthIndex.empty());}
203 
204  template<typename Evt>
205  void PrepParticleList (const Evt& evt ) const;
206  template<typename Evt>
207  void PrepTrackIdToMCTruthIndex(const Evt& evt ) const;
208  template<typename Evt>
209  void PrepMCTruthList (const Evt& evt ) const;
210  template<typename Evt>
211  void PrepMCTruthListAndTrackIdToMCTruthIndex(const Evt& evt ) const ;
212  template<typename Evt>
213  bool CanRun(const Evt& evt) const;
214 
215  const sim::ParticleList& ParticleList() const { return fParticleList; }
216  void SetEveIdCalculator(sim::EveIdCalculator *ec) { fParticleList.AdoptEveIdCalculator(ec); }
217 
218  const std::vector< art::Ptr<simb::MCTruth> >& MCTruthList() const { return fMCTObj.fMCTruthList;}
219 
220  const std::map< int, int >& TrackIdToMCTruthIndex() const {return fMCTObj.fTrackIdToMCTruthIndex;}
221 
222  void ClearEvent();
223 
224  const simb::MCParticle* TrackIdToParticle_P(int const& id) const;
225  simb::MCParticle TrackIdToParticle(int const& id) const
226  { return *(this->TrackIdToParticle_P(id)); }//Users are encouraged to use TrackIdToParticleP
227 
228  const simb::MCParticle* TrackIdToMotherParticle_P(int const& id) const;
229  simb::MCParticle TrackIdToMotherParticle(int const& id) const//Users are encouraged to use TrackIdToMotherParticleP
230  { return *(this->TrackIdToMotherParticle_P(id)); }
231 
232  const art::Ptr<simb::MCTruth>& TrackIdToMCTruth_P(int const& id) const;
233  simb::MCTruth TrackIdToMCTruth (int const& id) const//Users are encouraged to use TrackIdToMCTruthP
234  { return *(this->TrackIdToMCTruth_P(id)); }
235 
236  //New Functions go here.
237  //TrackIdToEveId.
238  int TrackIdToEveTrackId(const int& tid) const { return fParticleList.EveId(tid);}
239 
240  const art::Ptr<simb::MCTruth>& ParticleToMCTruth_P(const simb::MCParticle* p) const; //Users are encouraged to use ParticleToMCTruthP
241  simb::MCTruth ParticleToMCTruth (const simb::MCParticle* p) const
242  { return *(this->ParticleToMCTruth_P(p)); }
243 
244  const std::vector< art::Ptr<simb::MCTruth> >& MCTruthVector_Ps() const; //I don't want this to be able to return a vector of copies. Too much chance of significant memory usage.
245 
246  std::vector<const simb::MCParticle*> MCTruthToParticles_Ps(art::Ptr<simb::MCTruth> const& mct) const; //I don't want this to be able to return a vector of copies. Too much chance of significant memory usage.
247 
248  std::set<int> GetSetOfTrackIds() const;
249  std::set<int> GetSetOfEveIds() const;
250 
251 
252  private:
253  mutable sim::ParticleList fParticleList;
254  struct MCTObjects{
255  std::vector< art::Ptr<simb::MCTruth> > fMCTruthList; //there is some optimization that can be done here.
256  std::map< int, int > fTrackIdToMCTruthIndex;
257  };
259  //For fhicl validation, makea config struct
260  art::InputTag fG4ModuleLabel;
261  //std::string fEveIdCalculatorName;
262  //enum EveIdCalculator
263  //{
264  // EmEveIdCalculator,
265  // EveIdCalculator
266  //}
267  std::string fEveIdCalculator;
269 
270 
271 
272 
273 
274  };//class ParticleInventory
275 
276 }//namespace
277 
278 #include "ParticleInventory.tcc"
279 
280 #endif //CHEAT_PARTICLEINVENTORY_H
void PrepParticleList(const Evt &evt) const
A function to load the ParticleList and cache it This function will find the particle list and load i...
simb::MCParticle TrackIdToParticle(int const &id) const
sim::ParticleList fParticleList
const std::map< int, int > & TrackIdToMCTruthIndex() const
bool MCTruthListReady() const
A simple check to determine if the MCTruthList has already been prepared and cached or not...
FHICL Validation Object This struct is used for loading the fhicl configuration.
pdgs p
Definition: selectors.fcl:22
bool TrackIdToMCTruthReady() const
A simple check to determine if the TrackIdToMCTruth map has been prepared or not. ...
fhicl::Atom< art::InputTag > G4ModuleLabel
An atom. FHICL Atom for retreiving the module label to be used in retreiving information from the art...
simb::MCParticle TrackIdToMotherParticle(int const &id) const
void PrepEvent(const Evt &evt)
Function to set up the ParticleInventory state for an event. This is a function to tell the ParticleI...
bool CanRun(const Evt &evt) const
A short function to check if use of the backtracker is appropriate or not based on the type of input ...
const std::vector< art::Ptr< simb::MCTruth > > & MCTruthVector_Ps() const
void PrepTrackIdToMCTruthIndex(const Evt &evt) const
A function to prepare and cache a map of TrackIds and MCTruth object indicies from fMCTruthList...
void PrepMCTruthList(const Evt &evt) const
A function to load and cache the MCTruthList of the event.
void SetEveIdCalculator(sim::EveIdCalculator *ec)
void PrepMCTruthListAndTrackIdToMCTruthIndex(const Evt &evt) const
A function to make both PrepTrackIdToMCTruthIndex and PrepMCTruthList run when both are needed...
std::vector< const simb::MCParticle * > MCTruthToParticles_Ps(art::Ptr< simb::MCTruth > const &mct) const
A simple struct to contain the MC Truth information.
const simb::MCParticle * TrackIdToParticle_P(int const &id) const
std::set< int > GetSetOfTrackIds() const
BEGIN_PROLOG vertical distance to the surface Name
std::set< int > GetSetOfEveIds() const
const art::Ptr< simb::MCTruth > & ParticleToMCTruth_P(const simb::MCParticle *p) const
simb::MCTruth TrackIdToMCTruth(int const &id) const
simb::MCTruth ParticleToMCTruth(const simb::MCParticle *p) const
const simb::MCParticle * TrackIdToMotherParticle_P(int const &id) const
bool ParticleListReady() const
A simple check to determine if the ParticleList has already been prepared for this event or not...
std::vector< art::Ptr< simb::MCTruth > > fMCTruthList
A vector containing the MCTruth objects.
ParticleInventory(const ParticleInventoryConfig &config)
TCEvent evt
Definition: DataStructs.cxx:8
int TrackIdToEveTrackId(const int &tid) const
bool empty(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:555
const art::Ptr< simb::MCTruth > & TrackIdToMCTruth_P(int const &id) const
const std::vector< art::Ptr< simb::MCTruth > > & MCTruthList() const
const sim::ParticleList & ParticleList() const