EDepSimPersistencyManager.hh
Go to the documentation of this file.
1 // Provide output the Geant4 way.
2 //
3 #ifndef EDepSim_PersistencyManager_h
4 #define EDepSim_PersistencyManager_h 1
5 
6 #include "TG4Event.h"
7 
8 #include <G4VPersistencyManager.hh>
9 #include <G4StepStatus.hh>
10 
11 #include <vector>
12 #include <map>
13 
14 class G4Event;
15 class G4Run;
16 class G4PrimaryVertex;
17 class G4VPhysicalVolume;
18 class G4VTrajectory;
19 class G4TrajectoryContainer;
20 class G4VHitsCollection;
21 
22 class TPRegexp;
23 
24 namespace EDepSim {class PersistencyMessenger;}
25 
26 /// The class is `singleton', with access via
27 /// G4VPersistencyManager::GetPersistencyManager(). You need to create a
28 /// single persistency manager for GEANT4 to work right. It must derive from
29 /// G4VPersistencyManager which will make this object available to the
30 /// G4RunManager as a singleton. As a singleton, there can only be one
31 /// persistency manager active at a time. The Store methods will then be
32 /// called by the G4RunManager::AnalyzeEvent method (the
33 /// G4RunManager::AnalyzeMethod is protected).
34 ///
35 /// The persistency manager doesn't *have* to be derived from
36 /// EDepSim::PersistencyManager, but a lot of the trajectory and hit handling
37 /// functionality is currently handled by this class, and as of 4.10.2,
38 /// persistency managers seem to be the only viable hook to allow the event to
39 /// be summarized after it is fully generated. An alternative might be to
40 /// summarize the information would be in the
41 /// G4UserEventAction::EndOfEventAction method and then keep the summary data
42 /// in the EventUserInformation which is thread local.
43 namespace EDepSim {class PersistencyManager;}
44 class EDepSim::PersistencyManager : public G4VPersistencyManager {
45 public:
46  /// Creates a root persistency manager. Through the "magic" of
47  /// G4VPersistencyManager the ultimate base class, this declared to the G4
48  /// persistency management system. You can only have one active
49  /// persistency class at any give moment.
51  virtual ~PersistencyManager();
52 
53  /// stores anEvent and the associated objects into database.
54  virtual G4bool Store(const G4Event* anEvent);
55  virtual G4bool Store(const G4Run* aRun);
56  virtual G4bool Store(const G4VPhysicalVolume* aWorld);
57 
58  /// Retrieve information from a file. These are not implemented.
59  virtual G4bool Retrieve(G4Event *&e) {e=NULL; return false;}
60  virtual G4bool Retrieve(G4Run* &r) {r=NULL; return false;}
61  virtual G4bool Retrieve(G4VPhysicalVolume* &w) {w=NULL; return false;}
62 
63  /// A public accessor to the summarized event. The primaries are
64  /// summarized during by a call to UpdateSummaries. The
65  /// EDepSim::PersistencyManager::Store(event) method is called from
66  /// G4RunManager::AnalyzeEvent and will call the UpdateSummaries() method.
67  /// Alternatively, the UpdateSummarize method can be called by a method of
68  /// a derived class (e.g. in its Store method). The fEventSummary field
69  /// is protected so derived classes can directly access it.
70  const TG4Event& GetEventSummary();
71 
72  /// A public accessor to the summarized primaries. The primaries are
73  /// summarized during by a call to UpdateSummaries. The
74  /// EDepSim::PersistencyManager::Store(event) method is called from
75  /// G4RunManager::AnalyzeEvent and will call the UpdateSummaries() method.
76  /// Alternatively, the UpdateSummarize method can be called by a method of
77  /// a derived class (e.g. in its Store method).
78  const std::vector<TG4PrimaryVertex>& GetPrimaries() const;
79 
80  /// A public accessor to the summarized trajectories. See the
81  /// documentation for the GetPrimaries() method.
82  const std::vector<TG4Trajectory>& GetTrajectories() const;
83 
84  /// A public accessor to the summarized hit segment detectors. See the
85  /// documentation for the GetPrimaries() method.
87 
88  /// Open the output (ie database) file. This is used by the persistency
89  /// messenger to open files using the G4 macro language. It can be an
90  /// empty method.
91  virtual G4bool Open(G4String filename);
92 
93  /// Make sure the output file is closed. This is used to make sure that
94  /// any information being summarized has been saved.
95  virtual G4bool Close(void);
96 
97  /// Return the output file name.
98  virtual G4String GetFilename(void) const {return fFilename;}
99 
100  /// Set the threshold for length in a sensitive detector above which a
101  /// trajectory will be saved. If a trajectory created this much track
102  /// inside a sensitive detector, then it is saved.
103  virtual void SetLengthThreshold(G4double thresh) {
104  fLengthThreshold = thresh;
105  }
106 
107  /// Get the threshold for length in a sensitive detector.
108  virtual G4double GetLengthThreshold() const {return fLengthThreshold;}
109 
110  /// Set the momentum threshold required to save a gamma-ray as a
111  /// trajectory.
112  virtual void SetGammaThreshold(G4double thresh) {
113  fGammaThreshold = thresh;
114  }
115 
116  /// Get the momentum threshold required to save a gamma-ray as a
117  /// trajectory. Gamma rays are rejected if the gamma-ray momentum is
118  /// below either GetGammaThreshold() or GetMomentumThreshold().
119  virtual G4double GetGammaThreshold() const {return fGammaThreshold;}
120 
121  /// Set the momentum threshold required to save a neutron as a
122  /// trajectory.
123  virtual void SetNeutronThreshold(G4double thresh) {
124  fNeutronThreshold = thresh;
125  }
126 
127  /// Get the momentum threshold required to save a neutron as a
128  /// trajectory.
129  virtual G4double GetNeutronThreshold() const {return fNeutronThreshold;}
130 
131  /// Get the required trajectory accuracy. Trajectory points are saved so
132  /// that the interpolated position of a trajectory is never more than this
133  /// distance from the unsaved points.
134  virtual void SetTrajectoryPointAccuracy(double acc) {
136  }
137 
138  /// Get the required trajectory accuracy. Trajectory points are saved so
139  /// that the interpolated position of a trajectory is never more than this
140  /// distance from the unsaved points.
141  virtual double GetTrajectoryPointAccuracy(void) const {
143  }
144 
145  /// Get the minimum energy deposition for which a trajectory point will be
146  /// saved. This is the trajectory point process deposition
147  /// (GetProcessDeposit()).
148  virtual void SetTrajectoryPointDeposit(double dep) {
150  }
151 
152  /// Get the minimum energy deposition for which a trajectory point will be
153  /// saved. This is the trajectory point process deposition
154  /// (GetProcessDeposit()).
155  virtual double GetTrajectoryPointDeposit(void) const {
157  }
158 
159  /// Set the flag to save primary particle trajectories. If this flag is
160  /// true, then the trajectories for primary particles are saved even if
161  /// they don't ultimately cause an energy deposition in a sensitive
162  /// detector. If this flag is false, the trajectories for primary
163  /// particles are only saved if they, or one of their children, deposit
164  /// energy in a sensitive detector.
165  virtual void SetSaveAllPrimaryTrajectories(bool val) {
167  }
168 
169  /// Get the flag to save primary particle trajectories. If this flag is
170  /// true, then the trajectories for primary particles are saved even if
171  /// they don't ultimately cause an energy deposition in a sensitive
172  /// detector. If this flag is false, the trajectories for primary
173  /// particles are only saved if they, or one of their children, deposit
174  /// energy in a sensitive detector.
175  virtual bool GetSaveAllPrimaryTrajectories(void) const {
177  }
178 
179  /// Add a regular expression to define a volume boundary at which a
180  /// trajectory point should be saved. The volume names are defined by the
181  /// constructors which built the volume. Most of the names can be found
182  /// by looking at the geant command list. The names are not the same as
183  /// the root volume names.
184  virtual void AddTrajectoryBoundary(const G4String& boundary);
185 
186  /// Clear the list of volume boundaries which will cause a trajectory
187  /// point to be saved.
188  virtual void ClearTrajectoryBoundaries();
189 
190  /// Set the detector mask.
192 
193  /// Get the detector partition.
195 
196 protected:
197  /// Set the output filename. This can be used by the derived classes to
198  /// inform the base class of the output file name.
199  void SetFilename(G4String file) {
200  fFilename = file;
201  }
202 
203  /// Update the event summary fields.
204  void UpdateSummaries(const G4Event* event);
205 
206  /// A summary of the primary vertices in the event.
208 
209 private:
210  // Fill the vertex container. This allows informational vertices to be
211  // filled.
212  void SummarizePrimaries(std::vector<TG4PrimaryVertex>& primaries,
213  const G4PrimaryVertex* event);
214 
215  /// Fill the trajectory container.
216  void SummarizeTrajectories(std::vector<TG4Trajectory>& trajectories,
217  const G4Event* event);
218 
219  /// Mark the G4 Trajectories that should be saved.
220  void MarkTrajectories(const G4Event* event);
221 
222  /// sensitive detector.
223  void SummarizeSegmentDetectors(TG4HitSegmentDetectors& segmentDetectors,
224  const G4Event* event);
225 
226  /// Fill a container of hit segments.
227  void SummarizeHitSegments(std::vector<TG4HitSegment>& segments,
228  G4VHitsCollection* hits);
229 
230  /// Fill the map of sensitive detectors which use hit segments as the
231  /// Copy and map the contributing trajectories from the vector of
232  /// contributors for the EDepSim::HitSegment into the vector of contributors
233  /// for the TG4HitSegment.
234  void CopyHitContributors(std::vector<int>& dest,
235  const std::vector<int>& src);
236 
237  /// Copy the trajectory points into the trajectory summary. This uses
238  /// SelectTrajectoryPoints to pick out the points that should appear in
239  /// the summary.
241  G4VTrajectory* g4Traj);
242 
243  /// Find the maximum deviation of a trajectory point from the interpolated
244  /// path between point 1 and point 2
245  double FindTrajectoryAccuracy(G4VTrajectory* traj,
246  int point1, int point2);
247 
248  /// Save another trajectory point between point 1 and point to optimize
249  /// the trajectory accuracy.
250  int SplitTrajectory(G4VTrajectory* traj, int point1, int point2);
251 
252  /// Fill a vector with the indices of trajectory points that should be
253  /// copied to the output file.
254  void SelectTrajectoryPoints(std::vector<int>& selected,
255  G4VTrajectory* g4Traj);
256 
257  /// Return true if a trajectory point should be saved. The decision is
258  /// based on the stepping status (must be fGeomBoundary), the current and
259  /// the previous volume name (one must match a trajectory boundary regexp,
260  /// the other must not).
261  bool SaveTrajectoryBoundary(G4VTrajectory* g4Traj,
262  G4StepStatus status,
263  G4String currentVolume,
264  G4String prevVolume);
265 
266  /// The mapping between the internal G4 TrackID number and the external
267  /// TrackId number. The G4 TrackID value is based on the order that the
268  /// particle is placed onto the stack, not all TrackID value exist and the
269  /// ordering in the vector is not monotonic. The external TrackId is the
270  /// same as the location of the trajectory in the output array and can be
271  /// used as an index so parent trajectories are "easy" to find. This is
272  /// filled in SummarizeTrajectories, and used to remap TrackId in other
273  /// methods.
274  typedef std::map<int,int> TrackIdMap;
275  TrackIdMap fTrackIdMap;
276 
277  /// The filename of the output file.
278  G4String fFilename;
279 
280  /// If a trajectory left this much track in a sensitive detector then it
281  /// should be saved.
283 
284  /// Threshold momentum above which gamma-ray trajectories are written.
285  /// Gamma-rays are rejected if the momentum is less than either
286  /// fGammaThreshold or fMomentumThreshold.
287  G4double fGammaThreshold;
288 
289  /// The threshold momentum below which a neutron will be rejected.
291 
292  /// The maximum distance between the interpolated trajectory position and
293  /// an unsaved trajectory point.
295 
296  /// The minimum energy for the trajectory point process deposit
297  /// (GetProcessDeposit()) to be saved. Points with less than this energy
298  /// are not selected.
300 
301  /// Flag to determine if all primary trajectories are saved, or only those
302  /// that ultimately create energy in a sensitive detector. The primary
303  /// particles are always saved.
305 
306  /// A list of detectors for which trajectory points should be saved as
307  /// particles enter and exit.
308  std::vector<TPRegexp*> fTrajectoryBoundaries;
309 
310  /// The detector partition
312 
313  // A pointer to the messenger.
315 };
316 #endif
void SummarizeTrajectories(std::vector< TG4Trajectory > &trajectories, const G4Event *event)
Fill the trajectory container.
void SetDetectorPartition(int partition)
Set the detector mask.
G4double fNeutronThreshold
The threshold momentum below which a neutron will be rejected.
virtual G4String GetFilename(void) const
Return the output file name.
void CopyTrajectoryPoints(TG4Trajectory &traj, G4VTrajectory *g4Traj)
int SplitTrajectory(G4VTrajectory *traj, int point1, int point2)
G4String fFilename
The filename of the output file.
bool SaveTrajectoryBoundary(G4VTrajectory *g4Traj, G4StepStatus status, G4String currentVolume, G4String prevVolume)
virtual void AddTrajectoryBoundary(const G4String &boundary)
int fDetectorPartition
The detector partition.
virtual void SetTrajectoryPointAccuracy(double acc)
std::map< std::string, TG4HitSegmentContainer > TG4HitSegmentDetectors
Definition: TG4HitSegment.h:18
virtual bool GetSaveAllPrimaryTrajectories(void) const
void CopyHitContributors(std::vector< int > &dest, const std::vector< int > &src)
string filename
Definition: train.py:213
virtual G4double GetNeutronThreshold() const
const std::vector< TG4Trajectory > & GetTrajectories() const
const TG4HitSegmentDetectors & GetSegmentDetectors() const
void SummarizeHitSegments(std::vector< TG4HitSegment > &segments, G4VHitsCollection *hits)
Fill a container of hit segments.
virtual double GetTrajectoryPointAccuracy(void) const
virtual void SetNeutronThreshold(G4double thresh)
const double e
virtual double GetTrajectoryPointDeposit(void) const
double FindTrajectoryAccuracy(G4VTrajectory *traj, int point1, int point2)
Construct a module from components.
Definition: TG4HitSegment.h:10
void SummarizeSegmentDetectors(TG4HitSegmentDetectors &segmentDetectors, const G4Event *event)
sensitive detector.
int GetDetectorPartition() const
Get the detector partition.
void SelectTrajectoryPoints(std::vector< int > &selected, G4VTrajectory *g4Traj)
TG4Event fEventSummary
A summary of the primary vertices in the event.
virtual G4double GetGammaThreshold() const
virtual G4bool Retrieve(G4VPhysicalVolume *&w)
virtual G4bool Open(G4String filename)
void SummarizePrimaries(std::vector< TG4PrimaryVertex > &primaries, const G4PrimaryVertex *event)
virtual void SetLengthThreshold(G4double thresh)
virtual void SetGammaThreshold(G4double thresh)
virtual void SetSaveAllPrimaryTrajectories(bool val)
virtual void SetTrajectoryPointDeposit(double dep)
const std::vector< TG4PrimaryVertex > & GetPrimaries() const
virtual G4bool Retrieve(G4Event *&e)
Retrieve information from a file. These are not implemented.
void MarkTrajectories(const G4Event *event)
Mark the G4 Trajectories that should be saved.
virtual G4bool Retrieve(G4Run *&r)
const TG4Event & GetEventSummary()
virtual G4bool Close(void)
Make sure the output file is closed.
virtual G4bool Store(const G4Event *anEvent)
stores anEvent and the associated objects into database.
EDepSim::PersistencyMessenger * fPersistencyMessenger
std::vector< TPRegexp * > fTrajectoryBoundaries
Event finding and building.
virtual G4double GetLengthThreshold() const
Get the threshold for length in a sensitive detector.
void UpdateSummaries(const G4Event *event)
Update the event summary fields.