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 ////////////////////////////////////////////////////////////////////////
6 
7 /// This class implements the garg4::UserAction interface in order to
8 /// accumulate a list of particles modeled by Geant4.
9 //
10 /// It uses multiple inheritance: it inherits from garg4::UserAction,
11 /// in order to take advantage of Geant4's user hooks; it also
12 /// inherits from cfg::Observer, because it accesses a parameter from
13 /// an XML configuration file.
14 
15 #ifndef GARG4ParticleListAction_h
16 #define GARG4ParticleListAction_h
17 
18 #include "GArG4/ParticleFilters.h"
19 
21 #include "nug4/G4Base/UserAction.h"
22 #include "nug4/ParticleNavigation/ParticleList.h"
23 
24 #include "Geant4/globals.hh"
25 #include <map>
26 
27 // Forward declarations.
28 class G4Event;
29 class G4Track;
30 class G4Step;
31 
32 namespace sim {
33  class ParticleList;
34 }
35 
36 namespace gar {
37  namespace garg4 {
38 
39  class ParticleListAction : public g4b::UserAction
40  {
41  public:
42  struct ParticleInfo_t {
43 
44  simb::MCParticle* particle = nullptr; ///< simple structure representing particle
45  bool keep = false; ///< if there was decision to keep
46 
47  /// Resets the information (does not release memory it does not own)
48  void clear() { particle = nullptr; keep = false; }
49 
50  /// Returns whether there is a particle
51  bool hasParticle() const { return particle; }
52 
53  /// Rerturns whether there is a particle known to be kept
54  bool keepParticle() const { return hasParticle() && keep; }
55 
56  }; // ParticleInfo_t
57 
58  // Standard constructors and destructors;
59  ParticleListAction(double energyCut,
60  bool storeTrajectories = false,
61  bool keepEMShowerDaughters = false,
62  std::string EMShowerDaughterMatRegex = ".*");
63  virtual ~ParticleListAction();
64 
65  // UserActions method that we'll override, to obtain access to
66  // Geant4's particle tracks and trajectories.
67  virtual void BeginOfEventAction(const G4Event*);
68  virtual void EndOfEventAction (const G4Event*);
69  virtual void PreTrackingAction (const G4Track*);
70  virtual void PostTrackingAction(const G4Track*);
71  virtual void SteppingAction (const G4Step* );
72 
73  /// Grabs a particle filter
74  void ParticleFilter(std::unique_ptr<PositionInVolumeFilter>&& filter)
75  { fFilter = std::move(filter); }
76 
77 
78  // TrackID of the current particle, EveID if the particle is from an EM shower
79  static int GetCurrentTrackID() { return fCurrentTrackID; }
80  static int GetTrackIDOffset() { return fTrackIDOffset; }
81 
82  void ResetTrackIDOffset() { fTrackIDOffset = 0; }
83 
84  std::map<int, size_t> TrackIDToMCTruthIndexMap() const;
85 
86  // Returns the ParticleList accumulated during the current event.
87  sim::ParticleList* GetList() const;
88 
89  // Yields the ParticleList accumulated during the current event.
90  sim::ParticleList&& YieldList();
91 
92  /// returns whether the specified particle has been marked as dropped
93  static bool IsDropped(simb::MCParticle const* p);
94 
95  private:
96 
97  // this method will loop over the fParentIDMap to get the
98  // parentage of the provided trackid
99  int GetParentage(int trackid) const;
100 
101  G4double fEnergyCut; ///< The minimum energy for a particle to
102  ///< be included in the list.
103  ParticleInfo_t fCurrentParticle; ///< information about the particle currently being simulated
104  ///< for a single particle.
105  sim::ParticleList* fParticleList; ///< The accumulated particle information for
106  ///< all particles in the event.
107  G4bool fstoreTrajectories; ///< Whether to store particle trajectories with each particle.
108  std::map<int, int> fParentIDMap; ///< key is current track ID, value is parent ID
109  std::map<int, size_t> fTrackIDToMCTruthIndex; ///< map track ID to index of MCTruth in input list
110  static int fCurrentTrackID; ///< track ID of the current particle, set to eve ID
111  ///< for EM shower particles
112  static int fTrackIDOffset; ///< offset added to track ids when running over
113  ///< multiple MCTruth objects.
114  bool fKeepEMShowerDaughters; ///< whether to keep EM shower secondaries, tertiaries, etc
115  std::string fEMShowerDaughterMatRegex; ///< if keeping EM shower daughters, save only in media matching this
116 
117  std::unique_ptr<PositionInVolumeFilter> fFilter; ///< filter for particles to be kept
118 
119  /// Adds a trajectory point to the current particle, and runs the filter
120  void AddPointToCurrentParticle(TLorentzVector const& pos,
121  TLorentzVector const& mom,
122  std::string const& process);
123 
124  };
125 
126  } // namespace garg4
127 
128 } // namespace gar
129 
130 #endif // GARG4ParticleListAction_h
bool keepParticle() const
Rerturns whether there is a particle known to be kept.
std::string string
Definition: nybbler.cc:12
std::map< int, int > fParentIDMap
key is current track ID, value is parent ID
std::unique_ptr< PositionInVolumeFilter > fFilter
filter for particles to be kept
Particle class.
std::string fEMShowerDaughterMatRegex
if keeping EM shower daughters, save only in media matching this
G4bool fstoreTrajectories
Whether to store particle trajectories with each particle.
def process(f, kind)
Definition: search.py:254
def move(depos, offset)
Definition: depos.py:107
std::map< int, size_t > fTrackIDToMCTruthIndex
map track ID to index of MCTruth in input list
p
Definition: test.py:223
void clear()
Resets the information (does not release memory it does not own)
Code to link reconstructed objects back to the MC truth information.
General GArSoft Utilities.
bool fKeepEMShowerDaughters
whether to keep EM shower secondaries, tertiaries, etc
void ParticleFilter(std::unique_ptr< PositionInVolumeFilter > &&filter)
Grabs a particle filter.
bool hasParticle() const
Returns whether there is a particle.