remage
Simulation framework for HPGe-based experiments
 
Loading...
Searching...
No Matches
RMGVOutputScheme.hh
1// Copyright (C) 2022 Luigi Pertoldi <https://orcid.org/0000-0002-0467-2571>
2//
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef _RMG_V_OUTPUT_SCHEME_HH_
17#define _RMG_V_OUTPUT_SCHEME_HH_
18
19#include <optional>
20#include <string>
21
22#include "G4AnalysisManager.hh"
23#include "G4Event.hh"
24#include "G4Run.hh"
25#include "G4Track.hh"
26#include "G4UserStackingAction.hh"
27
28#include "RMGDetectorMetadata.hh"
29
30#include "fmt/core.h"
31
40class RMGVOutputScheme {
41
42 public:
43
44 RMGVOutputScheme() = default;
45 virtual ~RMGVOutputScheme() = default;
46
47 // initialization.
54 virtual void AssignOutputNames(G4AnalysisManager*) {}
55
56 // functions for individual events.
63 virtual void ClearBeforeEvent() {}
72 virtual bool ShouldDiscardEvent(const G4Event*) { return false; }
82 [[nodiscard]] virtual bool StoreAlways() const { return false; }
89 virtual void StoreEvent(const G4Event*) {}
90
91 // hook into RMGStackingAction.
103 virtual std::optional<G4ClassificationOfNewTrack> StackingActionClassify(const G4Track*, const int) {
104 return std::nullopt;
105 }
106
116 virtual std::optional<bool> StackingActionNewStage(const int) { return std::nullopt; }
117
118 // hook into G4TrackingAction.
124 virtual void TrackingActionPre(const G4Track*) {};
125
131 virtual void TrackingActionPost(const G4Track*) {};
132
133 // hook into G4SteppingAction.
139 virtual void SteppingAction(const G4Step*) {};
140
146 virtual void EndOfRunAction(const G4Run*) {};
147
148 // only to be called by the manager, before calling @ref AssignOutputNames.
155 void SetNtuplePerDetector(bool ntuple_per_det) { fNtuplePerDetector = ntuple_per_det; }
162 void SetNtupleUseVolumeName(bool use_vol_name) { fNtupleUseVolumeName = use_vol_name; }
163
164 void SetEventIDOffset(int offset) { fEventIDOffset = offset; }
165
166 static inline std::string fUIDKeyFormatString = "det{:03}";
167
168 protected:
169
170 [[nodiscard]] virtual std::string GetNtupleName(RMGDetectorMetadata det) const {
171 if (fNtuplePerDetector) {
172 if (!det.name.empty() && fNtupleUseVolumeName) {
173 return !det.ntuple_name.empty() ? det.ntuple_name : det.name;
174 }
175 return fmt::format(fmt::runtime(fUIDKeyFormatString), det.uid);
176 }
177 return GetNtupleNameFlat();
178 }
179 [[nodiscard]] virtual std::string GetNtupleNameFlat() const {
180 throw new std::logic_error("GetNtupleNameFlat not implemented");
181 }
182
183 // helper functions for output schemes.
184 void CreateNtupleFOrDColumn(G4AnalysisManager* ana_man, int nt, std::string name, bool use_float) {
185 if (use_float) ana_man->CreateNtupleFColumn(nt, name);
186 else ana_man->CreateNtupleDColumn(nt, name);
187 }
188 void FillNtupleFOrDColumn(G4AnalysisManager* ana_man, int nt, int col, double val, bool use_float) {
189 if (use_float)
190 ana_man->FillNtupleFColumn(nt, col, val); // NOLINT(cppcoreguidelines-narrowing-conversions)
191 else ana_man->FillNtupleDColumn(nt, col, val);
192 }
193
194 [[nodiscard]] int GetEventIDForStorage(const G4Event* evt) const {
195 return fEventIDOffset + evt->GetEventID();
196 }
197
198 // global options injected by manager.
199 bool fNtuplePerDetector = true;
200 bool fNtupleUseVolumeName = false;
201
202 int fEventIDOffset = 0;
203};
204
205#endif
206
207// vim: tabstop=2 shiftwidth=2 expandtab
virtual void ClearBeforeEvent()
Clear any event-specific data.
Definition RMGVOutputScheme.hh:63
void SetNtuplePerDetector(bool ntuple_per_det)
Specify whether to create separate ntuples for each detector.
Definition RMGVOutputScheme.hh:155
virtual std::optional< bool > StackingActionNewStage(const int)
Hook for transitioning to a new stacking stage.
Definition RMGVOutputScheme.hh:116
virtual void SteppingAction(const G4Step *)
Hook called after each step.
Definition RMGVOutputScheme.hh:139
virtual void EndOfRunAction(const G4Run *)
Perform final actions at the end of a run.
Definition RMGVOutputScheme.hh:146
virtual void TrackingActionPost(const G4Track *)
Hook called after tracking a new particle.
Definition RMGVOutputScheme.hh:131
virtual void TrackingActionPre(const G4Track *)
Hook called before tracking a new particle.
Definition RMGVOutputScheme.hh:124
virtual bool StoreAlways() const
Indicates whether the output scheme always stores event data.
Definition RMGVOutputScheme.hh:82
virtual bool ShouldDiscardEvent(const G4Event *)
Decide whether to discard the current event.
Definition RMGVOutputScheme.hh:72
void SetNtupleUseVolumeName(bool use_vol_name)
Specify whether to use the physical volume name for naming ntuples.
Definition RMGVOutputScheme.hh:162
virtual std::optional< G4ClassificationOfNewTrack > StackingActionClassify(const G4Track *, const int)
Hook for classifying new tracks during the stacking phase.
Definition RMGVOutputScheme.hh:103
virtual void AssignOutputNames(G4AnalysisManager *)
Initialize ntuple column names for this output scheme.
Definition RMGVOutputScheme.hh:54
virtual void StoreEvent(const G4Event *)
Store the event data.
Definition RMGVOutputScheme.hh:89
std::string ntuple_name
ntuple name override or empty string
Definition RMGDetectorMetadata.hh:41
int uid
detector (unique) identifier
Definition RMGDetectorMetadata.hh:35
std::string name
name of the referenced physical volume
Definition RMGDetectorMetadata.hh:37