All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParticleListAction_service.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file ParticleListAction_service.h
3 /// \brief Use Geant4's user "hooks" to maintain a list of particles generated by Geant4.
4 ///
5 /// \author seligman@nevis.columbia.edu
6 ////////////////////////////////////////////////////////////////////////
7 //
8 // accumulate a list of particles modeled by Geant4.
9 //
10 
11 // Include guard
12 #ifndef PARTICLELISTACTION_SERVICE_H
13 #define PARTICLELISTACTION_SERVICE_H
14 
15 #include "art/Framework/Principal/Handle.h"
16 #include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
17 
19 
20 #include "artg4tk/actionBase/EventActionBase.hh"
21 #include "artg4tk/actionBase/SteppingActionBase.hh"
22 #include "artg4tk/actionBase/TrackingActionBase.hh"
23 
24 #include "nug4/ParticleNavigation/ParticleList.h"
25 
26 #include "nusimdata/SimulationBase/MCParticle.h"
27 #include "nusimdata/SimulationBase/MCTruth.h"
28 #include "nusimdata/SimulationBase/simb.h" // simb::GeneratedParticleIndex_t
29 
30 #include "art/Framework/Principal/Handle.h"
31 #include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
32 
33 #include "canvas/Persistency/Common/Assns.h"
34 #include "canvas/Persistency/Common/Ptr.h"
35 #include "canvas/Persistency/Provenance/ProductID.h"
36 
37 namespace art {
38  class EDProductGetter;
39 }
40 
41 #include "messagefacility/MessageLogger/MessageLogger.h"
42 
43 #include "Geant4/globals.hh"
44 
45 class G4Event;
46 class G4Track;
47 class G4Step;
48 
49 class TLorentzVector;
50 
51 #include <map>
52 #include <memory>
53 #include <set>
54 #include <string>
55 #include <unordered_map>
56 #include <utility>
57 #include <vector>
58 
59 namespace larg4 {
60 
61  class ParticleListActionService : public artg4tk::EventActionBase,
62  public artg4tk::TrackingActionBase,
63  public artg4tk::SteppingActionBase {
64  public:
65  // Standard constructors and destructors;
66  explicit ParticleListActionService(fhicl::ParameterSet const&);
67 
68  // UserActions method that we'll override, to obtain access to
69  // Geant4's particle tracks and trajectories.
70  void preUserTrackingAction(const G4Track*) override;
71  void postUserTrackingAction(const G4Track*) override;
72  void userSteppingAction(const G4Step*) override;
73 
74  /// Returns the index of primary truth (`sim::NoGeneratorIndex` if none).
75  simb::GeneratedParticleIndex_t GetPrimaryTruthIndex(int trackId) const;
76 
77  // Called at the beginning of each event (note that this is after the
78  // primaries have been generated and sent to the event manager)
79  void beginOfEventAction(const G4Event*) override;
80 
81  // Called at the end of each event, right before GEANT's state switches
82  // out of event processing and into closed geometry (last chance to access
83  // the current event).
84  void endOfEventAction(const G4Event*) override;
85 
86  // Set/get the current Art event
87  void
88  setInputCollections(std::vector<art::Handle<std::vector<simb::MCTruth>>> const& mclists)
89  {
90  fMCLists = &mclists;
91  }
92 
93  void
94  setPtrInfo(art::ProductID pid,
95  art::EDProductGetter const* productGetter)
96  {
97  pid_ = pid;
98  productGetter_ = productGetter;
99  }
100 
101  std::unique_ptr<std::vector<simb::MCParticle>> ParticleCollection()
102  {
103  return std::move(partCol_);
104  }
105  std::unique_ptr<std::map<int,std::set<int>>> DroppedTracksCollection()
106  {
107  return std::move(droppedCol_);
108  }
109 
110  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>
112  {
113  return std::move(tpassn_);
114  }
115 
116  std::map<int, int> GetTargetIDMap() {return fTargetIDMap;}
117  private:
118  struct ParticleInfo_t {
119  simb::MCParticle* particle = nullptr; ///< simple structure representing particle
120  bool keepFullTrajectory = false; ///< if there was decision to keep
121 
122  /// Index of the particle in the original generator truth record.
123  simb::GeneratedParticleIndex_t truthIndex = simb::NoGeneratedParticleIndex;
124 
125  /// Resets the information (does not release memory it does not own)
126  void
128  {
129  particle = nullptr;
130  keepFullTrajectory = false;
131  truthIndex = simb::NoGeneratedParticleIndex;
132  }
133 
134  /// Returns whether there is a particle
135  bool
136  hasParticle() const
137  {
138  return particle;
139  }
140 
141  /// Returns whether there is a particle
142  bool
143  isPrimary() const
144  {
145  return simb::isGeneratedParticleIndex(truthIndex);
146  }
147 
148  /// Returns the index of the particle in the generator truth record.
149  simb::GeneratedParticleIndex_t
151  {
152  return truthIndex;
153  }
154 
155  }; // ParticleInfo_t
156 
157  // Yields the ParticleList accumulated during the current event.
158  sim::ParticleList&& YieldList();
159 
160  // this method will loop over the fParentIDMap to get the
161  // parentage of the provided trackid
162  int GetParentage(int trackid) const;
163 
164  G4double fenergyCut; ///< The minimum energy for a particle to
165  ///< be included in the list.
166  ParticleInfo_t fCurrentParticle; ///< information about the particle currently being simulated
167  ///< for a single particle.
168  sim::ParticleList fParticleList; ///< The accumulated particle information for
169  ///< all particles in the event.
170  G4bool fstoreTrajectories; ///< Whether to store particle trajectories with each particle.
171  std::vector<std::string>
172  fkeepGenTrajectories; ///< List of generators for which fstoreTrajectories applies.
173  /// if not provided and storeTrajectories is true, then all
174  /// trajectories for all generators will be stored. If
175  /// storeTrajectories is set to false, this list is ignored
176  /// and all additional trajectory points are not stored.
177  std::map<int, int> fParentIDMap; ///< key is current track ID, value is parent ID
178  std::map<int, int> fTargetIDMap; ///< key is original track ID, value is ID to assign for downstream objs (e.g. SimEdeps)
179  int fCurrentTrackID; ///< track ID of the current particle, set to eve ID
180  ///< for EM shower particles
181  mutable int fTrackIDOffset; ///< offset added to track ids when running over
182  ///< multiple MCTruth objects.
183  bool fKeepEMShowerDaughters; ///< whether to keep EM shower secondaries, tertiaries, etc
184  std::vector<std::string> fNotStoredPhysics; ///< Physics processes that will not be stored
185  bool fkeepOnlyPrimaryFullTraj; ///< Whether to store trajectories only for primaries and
186  /// their descendants with MCTruth process = "primary"
187  bool fSparsifyTrajectories; ///< help reduce the number of trajectory points.
188  double fSparsifyMargin; ///< set the sparsification margin
189  bool fKeepTransportation; ///< tell whether or not to keep the transportation process
190  bool fKeepSecondToLast; ///< tell whether or not to force keeping the second to last point
191 
192  std::vector<art::Handle<std::vector<simb::MCTruth>>> const*
193  fMCLists; ///< MCTruthCollection input lists
194 
195  /// Map: particle track ID -> index of primary information in MC truth.
196  std::map<int, simb::GeneratedParticleIndex_t> fPrimaryTruthMap;
197 
198  /// Map: particle track ID -> index of primary parent in std::vector<simb::MCTruth> object
199  std::map<int, size_t> fMCTIndexMap;
200 
201  /// Map: particle trakc ID -> boolean decision to keep or not full trajectory points
202  std::map<int, bool> fMCTPrimProcessKeepMap;
203 
204  /// Map: MCTruthIndex -> generator, input label of generator and keepGenerator decision
205  std::map<size_t, std::pair<std::string, G4bool>> fMCTIndexToGeneratorMap;
206 
207  /// Map: not stored process and counter
208  std::unordered_map<std::string, int> fNotStoredCounterUMap;
209 
210  /// map <ParentID, set: list of track ids for which no MCParticle was created>
211  std::map<int,std::set<int> > fdroppedTracksMap;
212 
213  std::unique_ptr<std::vector<simb::MCParticle>> partCol_;
214  std::unique_ptr<std::map<int,std::set<int> > > droppedCol_;
215  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>
217  art::ProductID pid_{art::ProductID::invalid()};
218  art::EDProductGetter const* productGetter_{nullptr};
219  /// Adds a trajectory point to the current particle, and runs the filter
220  void AddPointToCurrentParticle(TLorentzVector const& pos,
221  TLorentzVector const& mom,
222  std::string const& process);
223  };
224 
225 } // namespace larg4
226 
227 DECLARE_ART_SERVICE(larg4::ParticleListActionService, LEGACY)
228 
229 #endif // PARTICLELISTACTION_SERVICE_H
simb::MCParticle * particle
simple structure representing particle
void userSteppingAction(const G4Step *) override
ParticleListActionService(fhicl::ParameterSet const &)
void beginOfEventAction(const G4Event *) override
void preUserTrackingAction(const G4Track *) override
bool fKeepEMShowerDaughters
whether to keep EM shower secondaries, tertiaries, etc
bool fKeepSecondToLast
tell whether or not to force keeping the second to last point
std::unique_ptr< art::Assns< simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo > > tpassn_
int GetParentage(int trackid) const
double fSparsifyMargin
set the sparsification margin
Contains data associated to particles from detector simulation.
void AddPointToCurrentParticle(TLorentzVector const &pos, TLorentzVector const &mom, std::string const &process)
Adds a trajectory point to the current particle, and runs the filter.
void postUserTrackingAction(const G4Track *) override
std::unique_ptr< std::vector< simb::MCParticle > > ParticleCollection()
simb::GeneratedParticleIndex_t GetPrimaryTruthIndex(int trackId) const
Returns the index of primary truth (sim::NoGeneratorIndex if none).
std::unique_ptr< std::map< int, std::set< int > > > droppedCol_
bool fKeepTransportation
tell whether or not to keep the transportation process
std::unique_ptr< art::Assns< simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo > > AssnsMCTruthToMCParticle()
std::unique_ptr< std::vector< simb::MCParticle > > partCol_
std::map< int, size_t > fMCTIndexMap
Map: particle track ID -&gt; index of primary parent in std::vector&lt;simb::MCTruth&gt; object.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
bool fSparsifyTrajectories
help reduce the number of trajectory points.
void setInputCollections(std::vector< art::Handle< std::vector< simb::MCTruth >>> const &mclists)
std::map< size_t, std::pair< std::string, G4bool > > fMCTIndexToGeneratorMap
Map: MCTruthIndex -&gt; generator, input label of generator and keepGenerator decision.
std::unordered_map< std::string, int > fNotStoredCounterUMap
Map: not stored process and counter.
std::map< int, int > fTargetIDMap
key is original track ID, value is ID to assign for downstream objs (e.g. SimEdeps) ...
void endOfEventAction(const G4Event *) override
bool isPrimary() const
Returns whether there is a particle.
std::vector< std::string > fNotStoredPhysics
Physics processes that will not be stored.
std::map< int, int > fParentIDMap
key is current track ID, value is parent ID
simb::GeneratedParticleIndex_t truthInfoIndex() const
Returns the index of the particle in the generator truth record.
art::EDProductGetter const * productGetter_
void clear()
Resets the information (does not release memory it does not own)
std::vector< std::string > fkeepGenTrajectories
std::map< int, bool > fMCTPrimProcessKeepMap
Map: particle trakc ID -&gt; boolean decision to keep or not full trajectory points. ...
simb::GeneratedParticleIndex_t truthIndex
Index of the particle in the original generator truth record.
bool hasParticle() const
Returns whether there is a particle.
std::unique_ptr< std::map< int, std::set< int > > > DroppedTracksCollection()
G4bool fstoreTrajectories
Whether to store particle trajectories with each particle.
std::map< int, simb::GeneratedParticleIndex_t > fPrimaryTruthMap
Map: particle track ID -&gt; index of primary information in MC truth.
std::map< int, std::set< int > > fdroppedTracksMap
map &lt;ParentID, set: list of track ids for which no MCParticle was created&gt;
std::vector< art::Handle< std::vector< simb::MCTruth > > > const * fMCLists
MCTruthCollection input lists.
void setPtrInfo(art::ProductID pid, art::EDProductGetter const *productGetter)