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 <vector>
158 
161 #include "fhiclcpp/types/Atom.h"
162 namespace fhicl { class ParameterSet; }
163 
165 #include "nug4/ParticleNavigation/ParticleList.h"
168 
169 
170 namespace cheat{
172  {
173  public:
176  fhicl::Name("G4ModuleLabel"),
177  fhicl::Comment("The label of the LArG4 module used to produce the art file we will be examining"),
178  "largeant"};
179  fhicl::Atom<std::string> EveIdCalculator{
180  fhicl::Name("EveIdCalculator"),
181  fhicl::Comment("For selecting which EveID caclulator to use at initialization."),
182  "EmEveIdCalculator"};
183  fhicl::Atom<bool> OverrideRealData{
184  fhicl::Name("OverrideRealData"),
185  fhicl::Comment("Option when overlaying simulation on real data, to tell the backtracker to continue even if event looks like data."),
186  false};
187  };
188 
189  //using provider_type = ParticleInventory;
190  //cheat::ParticleInventory const* provider() const
191  //{ return static_cast<cheat::ParticleInventory const*>(this); }
192 
193  ///////////Constructor///////////////
196  ParticleInventory(ParticleInventory const&) = delete;
197 
198  template<typename Evt> //Template must be decalred and defined outside of the .cpp file.
199  void PrepEvent ( const Evt& evt );
200 
201  bool ParticleListReady() const { return !( fParticleList.empty() ); }
202  bool MCTruthListReady() const { return !( (fMCTObj.fMCTruthList).empty() ); }
203  bool TrackIdToMCTruthReady() const { return !(fMCTObj.fTrackIdToMCTruthIndex.empty());}
204 
205  template<typename Evt>
206  void PrepParticleList (const Evt& evt ) const;
207  template<typename Evt>
208  void PrepTrackIdToMCTruthIndex(const Evt& evt ) const;
209  template<typename Evt>
210  void PrepMCTruthList (const Evt& evt ) const;
211  template<typename Evt>
212  void PrepMCTruthListAndTrackIdToMCTruthIndex(const Evt& evt ) const ;
213  template<typename Evt>
214  bool CanRun(const Evt& evt) const;
215 
216  const sim::ParticleList& ParticleList() const { return fParticleList; }
217  void SetEveIdCalculator(sim::EveIdCalculator *ec) { fParticleList.AdoptEveIdCalculator(ec); }
218 
219  const std::vector< art::Ptr<simb::MCTruth> >& MCTruthList() const { return fMCTObj.fMCTruthList;}
220 
221  const std::map< int, int >& TrackIdToMCTruthIndex() const {return fMCTObj.fTrackIdToMCTruthIndex;}
222 
223  void ClearEvent();
224 
225  const simb::MCParticle* TrackIdToParticle_P(int const& id) const;
226  simb::MCParticle TrackIdToParticle(int const& id) const
227  { return *(this->TrackIdToParticle_P(id)); }//Users are encouraged to use TrackIdToParticleP
228 
229  const simb::MCParticle* TrackIdToMotherParticle_P(int const& id) const;
230  simb::MCParticle TrackIdToMotherParticle(int const& id) const//Users are encouraged to use TrackIdToMotherParticleP
231  { return *(this->TrackIdToMotherParticle_P(id)); }
232 
233  const art::Ptr<simb::MCTruth>& TrackIdToMCTruth_P(int const& id) const;
234  simb::MCTruth TrackIdToMCTruth (int const& id) const//Users are encouraged to use TrackIdToMCTruthP
235  { return *(this->TrackIdToMCTruth_P(id)); }
236 
237  //New Functions go here.
238  //TrackIdToEveId.
239  int TrackIdToEveTrackId(const int& tid) const { return fParticleList.EveId(tid);}
240 
241  const art::Ptr<simb::MCTruth>& ParticleToMCTruth_P(const simb::MCParticle* p) const; //Users are encouraged to use ParticleToMCTruthP
243  { return *(this->ParticleToMCTruth_P(p)); }
244 
245  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.
246 
247  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.
248 
249  std::set<int> GetSetOfTrackIds() const;
250  std::set<int> GetSetOfEveIds() const;
251 
252 
253  private:
254  mutable sim::ParticleList fParticleList;
255  struct MCTObjects{
256  std::vector< art::Ptr<simb::MCTruth> > fMCTruthList; //there is some optimization that can be done here.
257  std::map< int, int > fTrackIdToMCTruthIndex;
258  };
260  //For fhicl validation, makea config struct
262  //std::string fEveIdCalculatorName;
263  //enum EveIdCalculator
264  //{
265  // EmEveIdCalculator,
266  // EveIdCalculator
267  //}
270 
271 
272 
273 
274 
275  };//class ParticleInventory
276 
277 }//namespace
278 
279 #include "ParticleInventory.tcc"
280 
281 #endif //CHEAT_PARTICLEINVENTORY_H
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.
std::string string
Definition: nybbler.cc:12
bool TrackIdToMCTruthReady() const
A simple check to determine if the TrackIdToMCTruth map has been prepared or not. ...
ChannelGroupService::Name Name
simb::MCParticle TrackIdToMotherParticle(int const &id) const
Particle class.
void SetEveIdCalculator(sim::EveIdCalculator *ec)
A simple struct to contain the MC Truth information.
static Config * config
Definition: config.cpp:1054
p
Definition: test.py:223
simb::MCTruth TrackIdToMCTruth(int const &id) const
simb::MCTruth ParticleToMCTruth(const simb::MCParticle *p) const
code to link reconstructed objects back to the MC truth information
Definition: BackTracker.cc:22
Contains data associated to particles from detector simulation.
#define Comment
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.
TCEvent evt
Definition: DataStructs.cxx:7
Event generator information.
Definition: MCTruth.h:32
int TrackIdToEveTrackId(const int &tid) const
const std::vector< art::Ptr< simb::MCTruth > > & MCTruthList() const
const sim::ParticleList & ParticleList() const
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97