ParticleListAction.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file ParticleListAction.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 /// This class implements the LArG4::UserAction interface in order to
9 /// accumulate a list of particles modeled by Geant4.
10 //
11 /// It uses multiple inheritance: it inherits from LArG4::UserAction,
12 /// in order to take advantage of Geant4's user hooks; it also
13 /// inherits from cfg::Observer, because it accesses a parameter from
14 /// an XML configuration file.
15 
16 #ifndef LArG4_ParticleListAction_h
17 #define LArG4_ParticleListAction_h
18 
19 #include "Geant4/G4Types.hh"
20 #include "TLorentzVector.h"
21 
22 #include "cetlib/exempt_ptr.h"
23 #include "larcorealg/CoreUtils/ParticleFilters.h" // util::PositionInVolumeFilter
24 #include "nug4/G4Base/UserAction.h"
25 #include "nusimdata/SimulationBase/simb.h" // simb::GeneratedParticleIndex_t
26 
27 #include <map>
28 #include <memory>
29 
30 // Forward declarations.
31 class G4Event;
32 class G4Track;
33 class G4Step;
34 
35 namespace sim {
36  class ParticleList;
37 }
38 
39 namespace simb {
40  class MCParticle;
41 }
42 
43 namespace larg4 {
44 
45  class ParticleListAction : public g4b::UserAction {
46  public:
48 
49  struct ParticleInfo_t {
50 
51  cet::exempt_ptr<simb::MCParticle> particle; ///< Object representing particle.
52  bool keep = false; ///< if there was decision to keep
53  /// Index of the particle in the original generator truth record.
55 
56  /// Resets the information (does not release memory it does not own)
57  void
59  {
60  particle = nullptr;
61  keep = false;
62  truthIndex = simb::NoGeneratedParticleIndex;
63  }
64 
65  /// Returns whether there is a particle
66  bool
67  hasParticle() const
68  {
69  return !particle.empty();
70  }
71 
72  /// Returns whether there is a particle
73  bool
74  isPrimary() const
75  {
76  return simb::isGeneratedParticleIndex(truthIndex);
77  }
78 
79  /// Rerturns whether there is a particle known to be kept
80  bool
81  keepParticle() const
82  {
83  return hasParticle() && keep;
84  }
85 
86  /// Returns the index of the particle in the generator truth record.
89  {
90  return truthIndex;
91  }
92 
93  }; // ParticleInfo_t
94 
95  // Standard constructors and destructors;
96  ParticleListAction(double energyCut,
97  bool storeTrajectories = false,
98  bool keepEMShowerDaughters = false,
99  bool keepMCParticleList = true);
100 
101  // UserActions method that we'll override, to obtain access to
102  // Geant4's particle tracks and trajectories.
103  virtual void BeginOfEventAction(const G4Event*);
104  virtual void EndOfEventAction(const G4Event*);
105  virtual void PreTrackingAction(const G4Track*);
106  virtual void PostTrackingAction(const G4Track*);
107  virtual void SteppingAction(const G4Step*);
108 
109  /// Grabs a particle filter
110  void
111  ParticleFilter(std::unique_ptr<util::PositionInVolumeFilter>&& filter)
112  {
113  fFilter = std::move(filter);
114  }
115 
116  // TrackID of the current particle, EveID if the particle is from an EM shower
117  static int
119  {
120  return fCurrentTrackID;
121  }
122  static int
124  {
125  return fCurrentPdgCode;
126  }
127 
128  void
130  {
131  fTrackIDOffset = 0;
132  }
133 
134  // Returns the ParticleList accumulated during the current event.
135  const sim::ParticleList* GetList() const;
136 
137  /// Returns a map of truth record information index for each of the primary
138  /// particles (by track ID).
139  std::map<int, GeneratedParticleIndex_t> const&
141  {
142  return fPrimaryTruthMap;
143  }
144 
145  /// Returns whether a particle list is being kept.
146  bool
147  hasList() const
148  {
149  return static_cast<bool>(fparticleList);
150  }
151 
152  /// Returns the index of primary truth (`sim::NoGeneratorIndex` if none).
153  GeneratedParticleIndex_t GetPrimaryTruthIndex(int trackId) const;
154 
155  // Yields the ParticleList accumulated during the current event.
156  sim::ParticleList&& YieldList();
157 
158  /// returns whether the specified particle has been marked as dropped
159  static bool isDropped(simb::MCParticle const* p);
160 
161  private:
162  // this method will loop over the fParentIDMap to get the
163  // parentage of the provided trackid
164  int GetParentage(int trackid) const;
165 
166  G4double fenergyCut; ///< The minimum energy for a particle to
167  ///< be included in the list.
168  ParticleInfo_t fCurrentParticle; ///< information about the particle currently being simulated
169  ///< for a single particle.
170  std::unique_ptr<sim::ParticleList> fparticleList; ///< The accumulated particle information for
171  ///< all particles in the event.
172  G4bool fstoreTrajectories; ///< Whether to store particle trajectories with each particle.
173  std::map<int, int> fParentIDMap; ///< key is current track ID, value is parent ID
174  static int fCurrentTrackID; ///< track ID of the current particle, set to eve ID
175  ///< for EM shower particles
176  static int fCurrentPdgCode; ///< pdg code of current particle
177  static int fTrackIDOffset; ///< offset added to track ids when running over
178  ///< multiple MCTruth objects.
179  bool fKeepEMShowerDaughters; ///< whether to keep EM shower secondaries, tertiaries, etc
180 
181  std::unique_ptr<util::PositionInVolumeFilter> fFilter; ///< filter for particles to be kept
182 
183  /// Map: particle track ID -> index of primary information in MC truth.
184  std::map<int, GeneratedParticleIndex_t> fPrimaryTruthMap;
185 
186  /// Adds a trajectory point to the current particle, and runs the filter
187  void AddPointToCurrentParticle(TLorentzVector const& pos,
188  TLorentzVector const& mom,
189  std::string const& process);
190  };
191 
192 } // namespace LArG4
193 
194 #endif // LArG4_ParticleListAction_h
G4bool fstoreTrajectories
Whether to store particle trajectories with each particle.
static int fCurrentPdgCode
pdg code of current particle
std::map< int, GeneratedParticleIndex_t > fPrimaryTruthMap
Map: particle track ID -> index of primary information in MC truth.
std::string string
Definition: nybbler.cc:12
bool keepParticle() const
Rerturns whether there is a particle known to be kept.
Geant4 interface.
constexpr bool empty() const noexcept
Definition: exempt_ptr.h:153
GeneratedParticleIndex_t truthInfoIndex() const
Returns the index of the particle in the generator truth record.
std::map< int, int > fParentIDMap
key is current track ID, value is parent ID
def process(f, kind)
Definition: search.py:254
constexpr GeneratedParticleIndex_t NoGeneratedParticleIndex
Constant representing the absence of generator truth information.
Definition: simb.h:34
void clear()
Resets the information (does not release memory it does not own)
bool fKeepEMShowerDaughters
whether to keep EM shower secondaries, tertiaries, etc
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
simb::GeneratedParticleIndex_t GeneratedParticleIndex_t
void ParticleFilter(std::unique_ptr< util::PositionInVolumeFilter > &&filter)
Grabs a particle filter.
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.
Base utilities and modules for event generation and detector simulation.
std::unique_ptr< util::PositionInVolumeFilter > fFilter
filter for particles to be kept
std::unique_ptr< sim::ParticleList > fparticleList
bool isPrimary() const
Returns whether there is a particle.
cet::exempt_ptr< simb::MCParticle > particle
Object representing particle.
bool hasList() const
Returns whether a particle list is being kept.
bool hasParticle() const
Returns whether there is a particle.
Common type definitions for data products (and a bit beyond).
std::map< int, GeneratedParticleIndex_t > const & GetPrimaryTruthMap() const
std::size_t GeneratedParticleIndex_t
Type of particle index in the generator truth record (simb::MCTruth).
Definition: simb.h:30