PrimaryParticleInformation.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file PrimaryParticleInformation.h
3 //
4 /// \version $Id: PrimaryParticleInformation.h,v 1.7 2012-09-20 21:47:05 greenc Exp $
5 /// \author seligman@nevis.columbia.edu, brebel@fnal.gov
6 ////////////////////////////////////////////////////////////////////////
7 /// PrimaryParticleInformation
8 /// 10-Sep-2007 Bill Seligman
9 ///
10 /// 11-Feb-2009 <seligman@nevis.columbia.edu> Revised for LArSoft
11 ///
12 /// Purpose: This class is "attached" to the G4PrimaryParticle. It's used to
13 /// save the MCTruth object associated with the event.
14 ///
15 /// Background: Read this carefully, because this class probably
16 /// doesn't do what you think it does.
17 ///
18 /// Geant4 has various "truth" classes: G4Event, G4Track,
19 /// G4PrimaryVertex, G4PrimaryParticle, etc. For all of these
20 /// classes, Geant4 provides a facility for the user to include
21 /// additional information that's "attached" to the class in question.
22 ///
23 /// In this case, this class defines additional information to
24 /// included with the G4PrimaryParticle class. In particular, it
25 /// stores the pointer to the simb::MCTruth object that was the
26 /// source of the G4PrimaryParticle information.
27 ///
28 /// The reason why this class is necessary for the G4Base application
29 /// is that it allows the ParticleListAction class access to the
30 /// MCTruth pointer during Geant4's tracking.
31 
32 #ifndef G4BASE_PrimaryParticleInformation_h
33 #define G4BASE_PrimaryParticleInformation_h
34 
35 // nutools
36 #include "nusimdata/SimulationBase/simb.h" // simb::GeneratedParticleIndex_t
37 
38 // G4 Includes
39 #include "Geant4/G4VUserPrimaryParticleInformation.hh"
40 #include "Geant4/G4Allocator.hh"
41 
42 // ART Includes
45 
46 // C++ standard library
47 #include <limits> // std::numeric_limits<>
48 
49 // Forward declaration for this namespace.
50 namespace simb {
51  class MCTruth;
52  class MCParticle;
53 }
54 
55 namespace g4b {
56 
57  class PrimaryParticleInformation : public G4VUserPrimaryParticleInformation {
58 
59  public:
60 
61  /// Type of the stored index of particle within the truth record.
63 
64  inline void* operator new(size_t);
65  inline void operator delete(void*);
66 
67  // Accessors:
68  const simb::MCTruth* GetMCTruth() const { return fMCTruth; }
69  size_t const& MCTruthIndex() const { return fMCTIndex; }
70 
71  /**
72  * @brief Returns the index of the corresponding particle in truth record.
73  * @return an index within truth record, `NotInMCTruth` otherwise
74  * @see IsInMCTruth(), GetMCTruth()
75  *
76  * This method returns the index in the truth record pointed by
77  * `GetMCTruth()` of the particle (`simb::MCParticle`) corresponding to this
78  * object. If this information is not set, or if there is no such a
79  * particle at all, the special value `NotInMCTruth` is returned.
80  * This can be checked with the `IsInMCTruth()` method. T
81  */
83  { return fMCParticleIndex; }
84 
85  /// Returns the original particle in the truth record.
86  /// @return pointer to the original particle, or `nullptr` if not available
87  /// @see MCParticleIndex(), GetMCTruth()
88  simb::MCParticle const* GetMCParticle() const;
89 
90  /// Returns whether this particle has a corresponding truth record item.
91  /// @see MCParticleIndex(), GetMCTruth()
92  bool IsInMCTruth() const; // inline implementation below
93 
94  void SetMCTruth(const simb::MCTruth* m,
95  size_t idx=0,
97  );
98 
99  // Required by Geant4:
100  virtual void Print() const override;
101 
102  private:
103 
104  // The MCTruth object associated with the G4PrimaryParticle. If
105  // this is zero, then there is no MCTruth object for this
106  // particle (although in that case it's more likely that a
107  // G4Base::PrimaryParticleInformation object would not have been
108  // created in the first place.)
109  // The MCTIndex is the index of the MCTruth object in the vector
110  // of the ConvertMCTruthToG4 creating this object
111  const simb::MCTruth* fMCTruth = nullptr;
112  size_t fMCTIndex = 0;
113  /// Index within the truth record.
115  };
116 
117  // It's not likely, but there could be memory issues with these
118  // PrimaryParticleInformation objects. To make things work more smoothly
119  // and quickly, use Geant4's memory allocation mechanism.
120 
121  extern G4Allocator<PrimaryParticleInformation> PrimaryParticleInformationAllocator;
122 
123  inline bool PrimaryParticleInformation::IsInMCTruth() const
124  { return MCParticleIndex() != simb::NoGeneratedParticleIndex; }
125 
126 
127  inline void PrimaryParticleInformation::SetMCTruth(
128  const simb::MCTruth* m,
129  size_t idx /* = 0 */,
130  GeneratedParticleIndex_t indexInTruth /* = simb::NoGeneratedParticleIndex */
131  )
132  {
133  fMCTruth = m;
134  fMCTIndex = idx;
135  fMCParticleIndex = indexInTruth;
136  }
137 
138  inline void* PrimaryParticleInformation::operator new(size_t)
139  {
140  void *aPrimaryParticleInformation;
141  aPrimaryParticleInformation = (void *) PrimaryParticleInformationAllocator.MallocSingle();
142  return aPrimaryParticleInformation;
143  }
144 
145  inline void PrimaryParticleInformation::operator delete(void *aPrimaryParticleInformation)
146  {
147  PrimaryParticleInformationAllocator.FreeSingle((PrimaryParticleInformation*) aPrimaryParticleInformation);
148  }
149 
150 }
151 
152 #endif // G4BASE_PrimaryParticleInformation_h
const simb::MCTruth * GetMCTruth() const
static const double m
Definition: Units.h:79
constexpr GeneratedParticleIndex_t NoGeneratedParticleIndex
Constant representing the absence of generator truth information.
Definition: simb.h:34
simb::GeneratedParticleIndex_t GeneratedParticleIndex_t
Type of the stored index of particle within the truth record.
G4Allocator< PrimaryParticleInformation > PrimaryParticleInformationAllocator
basic interface to Geant4 for ART-based software
Base utilities and modules for event generation and detector simulation.
Event generator information.
Definition: MCTruth.h:32
Common type definitions for data products (and a bit beyond).
GeneratedParticleIndex_t MCParticleIndex() const
Returns the index of the corresponding particle in truth record.
std::size_t GeneratedParticleIndex_t
Type of particle index in the generator truth record (simb::MCTruth).
Definition: simb.h:30