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 #ifndef PARTICLELISTACTION_SERVICE_H
12 #define PARTICLELISTACTION_SERVICE_H
13 
18 
19 #include "nug4/ParticleNavigation/ParticleList.h"
22 #include "nusimdata/SimulationBase/simb.h" // simb::GeneratedParticleIndex_t
23 
24 #include "artg4tk/actionBase/EventActionBase.hh"
25 #include "artg4tk/actionBase/SteppingActionBase.hh"
26 #include "artg4tk/actionBase/TrackingActionBase.hh"
27 
29 
30 #include "Geant4/globals.hh"
31 #include <map>
32 
33 // Forward declarations.
34 class G4Event;
35 class G4Track;
36 class G4Step;
37 
38 namespace sim {
39  class ParticleList;
40 }
41 
42 namespace larg4 {
43 
44  class ParticleListActionService : public artg4tk::EventActionBase,
45  public artg4tk::TrackingActionBase,
46  public artg4tk::SteppingActionBase {
47  public:
48  // Standard constructors and destructors;
50 
51  // UserActions method that we'll override, to obtain access to
52  // Geant4's particle tracks and trajectories.
53  void preUserTrackingAction(const G4Track*) override;
54  void postUserTrackingAction(const G4Track*) override;
55  void userSteppingAction(const G4Step*) override;
56 
57  /// Returns the index of primary truth (`sim::NoGeneratorIndex` if none).
58  simb::GeneratedParticleIndex_t GetPrimaryTruthIndex(int trackId) const;
59 
60  // Called at the beginning of each event (note that this is after the
61  // primaries have been generated and sent to the event manager)
62  void beginOfEventAction(const G4Event*) override;
63 
64  // Called at the end of each event, right before GEANT's state switches
65  // out of event processing and into closed geometry (last chance to access
66  // the current event).
67  void endOfEventAction(const G4Event*) override;
68 
69  // Set/get the current Art event
70  void
71  setInputCollections(std::vector<art::Handle<std::vector<simb::MCTruth>>> const& mclists)
72  {
73  fMCLists = &mclists;
74  }
75 
76  void
78  art::EDProductGetter const* productGetter)
79  {
80  pid_ = pid;
81  productGetter_ = productGetter;
82  }
83 
84  std::unique_ptr<std::vector<simb::MCParticle>>
86  {
87  return std::move(partCol_);
88  }
89 
90  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>
92  {
93  return std::move(tpassn_);
94  }
95 
96  private:
97  struct ParticleInfo_t {
98  simb::MCParticle* particle = nullptr; ///< simple structure representing particle
99  bool keepFullTrajectory = false; ///< if there was decision to keep
100 
101  /// Index of the particle in the original generator truth record.
103 
104  /// Resets the information (does not release memory it does not own)
105  void
107  {
108  particle = nullptr;
109  keepFullTrajectory = false;
110  truthIndex = simb::NoGeneratedParticleIndex;
111  }
112 
113  /// Returns whether there is a particle
114  bool
115  hasParticle() const
116  {
117  return particle;
118  }
119 
120  /// Returns whether there is a particle
121  bool
122  isPrimary() const
123  {
124  return simb::isGeneratedParticleIndex(truthIndex);
125  }
126 
127  /// Returns the index of the particle in the generator truth record.
130  {
131  return truthIndex;
132  }
133 
134  }; // ParticleInfo_t
135 
136  // Yields the ParticleList accumulated during the current event.
137  sim::ParticleList&& YieldList();
138 
139  // this method will loop over the fParentIDMap to get the
140  // parentage of the provided trackid
141  int GetParentage(int trackid) const;
142 
143  G4double fenergyCut; ///< The minimum energy for a particle to
144  ///< be included in the list.
145  ParticleInfo_t fCurrentParticle; ///< information about the particle currently being simulated
146  ///< for a single particle.
147  sim::ParticleList fParticleList; ///< The accumulated particle information for
148  ///< all particles in the event.
149  G4bool fstoreTrajectories; ///< Whether to store particle trajectories with each particle.
150  std::vector<std::string>
151  fkeepGenTrajectories; ///< List of generators for which fstoreTrajectories applies.
152  /// if not provided and storeTrajectories is true, then all
153  /// trajectories for all generators will be stored. If
154  /// storeTrajectories is set to false, this list is ignored
155  /// and all additional trajectory points are not stored.
156  std::map<int, int> fParentIDMap; ///< key is current track ID, value is parent ID
157  int fCurrentTrackID; ///< track ID of the current particle, set to eve ID
158  ///< for EM shower particles
159  mutable int fTrackIDOffset; ///< offset added to track ids when running over
160  ///< multiple MCTruth objects.
161  bool fKeepEMShowerDaughters; ///< whether to keep EM shower secondaries, tertiaries, etc
162  std::vector<std::string> fNotStoredPhysics; ///< Physics processes that will not be stored
163  bool fkeepOnlyPrimaryFullTraj; ///< Whether to store trajectories only for primaries and
164  /// their descendants with MCTruth process = "primary"
165  bool fSparsifyTrajectories; ///< help reduce the number of trajectory points.
166  double fSparsifyMargin; ///< set the sparsification margin
167  bool fKeepTransportation; ///< tell whether or not to keep the transportation process
168  bool fKeepSecondToLast; ///< tell whether or not to force keeping the second to last point
169 
170  std::vector<art::Handle<std::vector<simb::MCTruth>>> const*
171  fMCLists; ///< MCTruthCollection input lists
172 
173  /// Map: particle track ID -> index of primary information in MC truth.
174  std::map<int, simb::GeneratedParticleIndex_t> fPrimaryTruthMap;
175 
176  /// Map: particle track ID -> index of primary parent in std::vector<simb::MCTruth> object
177  std::map<int, size_t> fMCTIndexMap;
178 
179  /// Map: particle trakc ID -> boolean decision to keep or not full trajectory points
180  std::map<int, bool> fMCTPrimProcessKeepMap;
181 
182  /// Map: MCTruthIndex -> generator, input label of generator and keepGenerator decision
183  std::map<size_t, std::pair<std::string, G4bool>> fMCTIndexToGeneratorMap;
184 
185  /// Map: not stored process and counter
186  std::unordered_map<std::string, int> fNotStoredCounterUMap;
187 
188  std::unique_ptr<std::vector<simb::MCParticle>> partCol_;
189  std::unique_ptr<art::Assns<simb::MCTruth, simb::MCParticle, sim::GeneratedParticleInfo>>
192  art::EDProductGetter const* productGetter_{nullptr};
193  /// Adds a trajectory point to the current particle, and runs the filter
194  void AddPointToCurrentParticle(TLorentzVector const& pos,
195  TLorentzVector const& mom,
196  std::string const& process);
197  };
198 
199 } // namespace larg4
200 
202 
203 #endif // PARTICLELISTACTION_SERVICE_H
bool fKeepEMShowerDaughters
whether to keep EM shower secondaries, tertiaries, etc
std::string string
Definition: nybbler.cc:12
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_
struct vector vector
Geant4 interface.
double fSparsifyMargin
set the sparsification margin
std::unique_ptr< std::vector< simb::MCParticle > > ParticleCollection()
Particle class.
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 -> index of primary parent in std::vector<simb::MCTruth> object.
def process(f, kind)
Definition: search.py:254
constexpr GeneratedParticleIndex_t NoGeneratedParticleIndex
Constant representing the absence of generator truth information.
Definition: simb.h:34
bool fSparsifyTrajectories
help reduce the number of trajectory points.
void setInputCollections(std::vector< art::Handle< std::vector< simb::MCTruth >>> const &mclists)
#define DECLARE_ART_SERVICE(svc, scope)
def move(depos, offset)
Definition: depos.py:107
std::map< size_t, std::pair< std::string, G4bool > > fMCTIndexToGeneratorMap
Map: MCTruthIndex -> generator, input label of generator and keepGenerator decision.
std::unordered_map< std::string, int > fNotStoredCounterUMap
Map: not stored process and counter.
bool isGeneratedParticleIndex(GeneratedParticleIndex_t index)
Returns whether the specified one is an acceptable generator index.
Definition: simb.h:37
Code to link reconstructed objects back to the MC truth information.
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.
Contains data associated to particles from detector simulation.
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 -> boolean decision to keep or not full trajectory points. ...
bool hasParticle() const
Returns whether there is a particle.
G4bool fstoreTrajectories
Whether to store particle trajectories with each particle.
static constexpr ProductID invalid() noexcept
Definition: ProductID.h:26
std::map< int, simb::GeneratedParticleIndex_t > fPrimaryTruthMap
Map: particle track ID -> index of primary information in MC truth.
std::vector< art::Handle< std::vector< simb::MCTruth > > > const * fMCLists
MCTruthCollection input lists.
void setPtrInfo(art::ProductID pid, art::EDProductGetter const *productGetter)
Common type definitions for data products (and a bit beyond).
std::size_t GeneratedParticleIndex_t
Type of particle index in the generator truth record (simb::MCTruth).
Definition: simb.h:30