Skip to content

Commit

Permalink
output: add option for ntuple naming by volume
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Nov 15, 2024
1 parent 3461710 commit 5a77fa8
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 189 deletions.
23 changes: 18 additions & 5 deletions docs/rmg-commands.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions include/RMGManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class RMGManager {
[[nodiscard]] inline bool GetOutputNtuplePerDetector() const {
return fOutputNtuplePerDetector;
}
[[nodiscard]] inline bool GetOutputNtupleUseVolumeName() const {
return fOutputNtupleUseVolumeName;
}

// setters
inline void SetUserInit(G4RunManager* g4_manager) {
Expand Down Expand Up @@ -151,6 +154,7 @@ class RMGManager {

std::string fOutputFile = "detector-hits.root";
bool fOutputNtuplePerDetector = true;
bool fOutputNtupleUseVolumeName = false;
std::string fOutputNtupleDirectory = "stp";
// track internal id of detector NTuples
static G4ThreadLocal std::map<int, int> fNtupleIDs;
Expand Down
10 changes: 8 additions & 2 deletions include/RMGVOutputScheme.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,27 @@ class RMGVOutputScheme {
}
virtual inline std::optional<bool> StackingActionNewStage(const int) { return std::nullopt; }

// hook into G4TrackingAction
// hook into G4TrackingAction.
virtual inline void TrackingActionPre(const G4Track*) {};

// only to be called by the manager, before calling AssignOutputNames.
inline void SetNtuplePerDetector(bool ntuple_per_det) { fNtuplePerDetector = ntuple_per_det; }
inline void SetNtupleUseVolumeName(bool use_vol_name) { fNtupleUseVolumeName = use_vol_name; }

protected:

[[nodiscard]] virtual inline std::string GetNtupleName(RMGDetectorMetadata det) const {
if (fNtuplePerDetector) {
return !det.name.empty() ? det.name : fmt::format("det{:03}", det.uid);
if (!det.name.empty() && fNtupleUseVolumeName) { return det.name; }
return fmt::format("det{:03}", det.uid);
}
return GetNtuplenameFlat();
}
[[nodiscard]] virtual inline std::string GetNtuplenameFlat() const {
throw new std::logic_error("GetNtuplenameFlat not implemented");
}

// helper functions for output schemes.
inline void CreateNtupleFOrDColumn(G4AnalysisManager* ana_man, int nt, std::string name,
bool use_float) {
if (use_float) ana_man->CreateNtupleFColumn(nt, name);
Expand All @@ -78,7 +82,9 @@ class RMGVOutputScheme {
else ana_man->FillNtupleDColumn(nt, col, val);
}

// global options injected by manager.
bool fNtuplePerDetector = true;
bool fNtupleUseVolumeName = false;
};

#endif
Expand Down
14 changes: 10 additions & 4 deletions src/RMGManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,24 @@ void RMGManager::DefineCommands() {
fOutputMessenger->DeclareProperty("NtuplePerDetector", fOutputNtuplePerDetector)
.SetGuidance("Create a ntuple for each sensitive detector to store hits. Otherwise, store "
"all hits of one detector type in one ntuple.")
.SetParameterName("tree_per_det", false)
.SetParameterName("nt_per_det", false)
.SetStates(G4State_PreInit, G4State_Idle);

fOutputMessenger->DeclareProperty("NtupleUseVolumeName", fOutputNtupleUseVolumeName)
.SetGuidance("Use the sensitive volume name to name output ntuples.")
.SetGuidance("note: this only works if `NtuplePerDetector` is set to true.")
.SetParameterName("nt_vol_name", false)
.SetStates(G4State_PreInit, G4State_Idle);

fOutputMessenger->DeclareMethod("ActivateOutputScheme", &RMGManager::ActivateOptionalOutputScheme)
.SetGuidance("Activates the output scheme that had been registered under the given name.")
.SetParameterName("tree_per_det", false)
.SetParameterName("oscheme", false)
.SetStates(G4State_PreInit, G4State_Idle);

fOutputMessenger->DeclareMethod("OutputNtupleDirectory", &RMGManager::SetOutputNtupleDirectory)
fOutputMessenger->DeclareMethod("NtupleDirectory", &RMGManager::SetOutputNtupleDirectory)
.SetGuidance("Change the default output directory/group for ntuples in output files.")
.SetGuidance("note: This setting is not respected by all output formats.")
.SetParameterName("ntuple_directory", false)
.SetParameterName("nt_directory", false)
.SetStates(G4State_PreInit, G4State_Idle);
}

Expand Down
1 change: 1 addition & 0 deletions src/RMGRunAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void RMGRunAction::SetupAnalysisManager() {
fOutputDataFields.emplace_back(oscheme);

oscheme->SetNtuplePerDetector(rmg_man->GetOutputNtuplePerDetector());
oscheme->SetNtupleUseVolumeName(rmg_man->GetOutputNtupleUseVolumeName());
oscheme->AssignOutputNames(ana_man);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/output/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ foreach(_file ${_aux})
configure_file(${PROJECT_SOURCE_DIR}/${_file} ${PROJECT_BINARY_DIR}/${_file} COPYONLY)
endforeach()

set(_macros ntuple-per-det.mac ntuple-flat.mac)
set(_macros ntuple-per-det.mac ntuple-per-det-vol.mac ntuple-flat.mac)

foreach(_mac ${_macros})
add_test(NAME output/hdf5-${_mac} COMMAND ./run-test-hdf5.sh $<TARGET_FILE:remage-cli>
Expand Down
160 changes: 160 additions & 0 deletions tests/output/macros/ntuple-per-det-vol.hdf5ls
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
default_histograms
header
stp
stp/det1
stp/det1/columns
stp/det1/edep_in_keV
stp/det1/edep_in_keV/entries
stp/det1/edep_in_keV/pages
stp/det1/entries
stp/det1/evtid
stp/det1/evtid/entries
stp/det1/evtid/pages
stp/det1/forms
stp/det1/names
stp/det1/particle
stp/det1/particle/entries
stp/det1/particle/pages
stp/det1/time_in_ns
stp/det1/time_in_ns/entries
stp/det1/time_in_ns/pages
stp/det1/xloc_in_m
stp/det1/xloc_in_m/entries
stp/det1/xloc_in_m/pages
stp/det1/yloc_in_m
stp/det1/yloc_in_m/entries
stp/det1/yloc_in_m/pages
stp/det1/zloc_in_m
stp/det1/zloc_in_m/entries
stp/det1/zloc_in_m/pages
stp/det2
stp/det2/columns
stp/det2/edep_in_keV
stp/det2/edep_in_keV/entries
stp/det2/edep_in_keV/pages
stp/det2/entries
stp/det2/evtid
stp/det2/evtid/entries
stp/det2/evtid/pages
stp/det2/forms
stp/det2/names
stp/det2/particle
stp/det2/particle/entries
stp/det2/particle/pages
stp/det2/time_in_ns
stp/det2/time_in_ns/entries
stp/det2/time_in_ns/pages
stp/det2/xloc_in_m
stp/det2/xloc_in_m/entries
stp/det2/xloc_in_m/pages
stp/det2/yloc_in_m
stp/det2/yloc_in_m/entries
stp/det2/yloc_in_m/pages
stp/det2/zloc_in_m
stp/det2/zloc_in_m/entries
stp/det2/zloc_in_m/pages
stp/scint1
stp/scint1/columns
stp/scint1/edep_in_keV
stp/scint1/edep_in_keV/entries
stp/scint1/edep_in_keV/pages
stp/scint1/entries
stp/scint1/evtid
stp/scint1/evtid/entries
stp/scint1/evtid/pages
stp/scint1/forms
stp/scint1/names
stp/scint1/particle
stp/scint1/particle/entries
stp/scint1/particle/pages
stp/scint1/time_in_ns
stp/scint1/time_in_ns/entries
stp/scint1/time_in_ns/pages
stp/scint1/v_post_in_m\ns
stp/scint1/v_post_in_m\ns/entries
stp/scint1/v_post_in_m\ns/pages
stp/scint1/v_pre_in_m\ns
stp/scint1/v_pre_in_m\ns/entries
stp/scint1/v_pre_in_m\ns/pages
stp/scint1/xloc_post_in_m
stp/scint1/xloc_post_in_m/entries
stp/scint1/xloc_post_in_m/pages
stp/scint1/xloc_pre_in_m
stp/scint1/xloc_pre_in_m/entries
stp/scint1/xloc_pre_in_m/pages
stp/scint1/yloc_post_in_m
stp/scint1/yloc_post_in_m/entries
stp/scint1/yloc_post_in_m/pages
stp/scint1/yloc_pre_in_m
stp/scint1/yloc_pre_in_m/entries
stp/scint1/yloc_pre_in_m/pages
stp/scint1/zloc_post_in_m
stp/scint1/zloc_post_in_m/entries
stp/scint1/zloc_post_in_m/pages
stp/scint1/zloc_pre_in_m
stp/scint1/zloc_pre_in_m/entries
stp/scint1/zloc_pre_in_m/pages
stp/scint2
stp/scint2/columns
stp/scint2/edep_in_keV
stp/scint2/edep_in_keV/entries
stp/scint2/edep_in_keV/pages
stp/scint2/entries
stp/scint2/evtid
stp/scint2/evtid/entries
stp/scint2/evtid/pages
stp/scint2/forms
stp/scint2/names
stp/scint2/particle
stp/scint2/particle/entries
stp/scint2/particle/pages
stp/scint2/time_in_ns
stp/scint2/time_in_ns/entries
stp/scint2/time_in_ns/pages
stp/scint2/v_post_in_m\ns
stp/scint2/v_post_in_m\ns/entries
stp/scint2/v_post_in_m\ns/pages
stp/scint2/v_pre_in_m\ns
stp/scint2/v_pre_in_m\ns/entries
stp/scint2/v_pre_in_m\ns/pages
stp/scint2/xloc_post_in_m
stp/scint2/xloc_post_in_m/entries
stp/scint2/xloc_post_in_m/pages
stp/scint2/xloc_pre_in_m
stp/scint2/xloc_pre_in_m/entries
stp/scint2/xloc_pre_in_m/pages
stp/scint2/yloc_post_in_m
stp/scint2/yloc_post_in_m/entries
stp/scint2/yloc_post_in_m/pages
stp/scint2/yloc_pre_in_m
stp/scint2/yloc_pre_in_m/entries
stp/scint2/yloc_pre_in_m/pages
stp/scint2/zloc_post_in_m
stp/scint2/zloc_post_in_m/entries
stp/scint2/zloc_post_in_m/pages
stp/scint2/zloc_pre_in_m
stp/scint2/zloc_pre_in_m/entries
stp/scint2/zloc_pre_in_m/pages
stp/vertices
stp/vertices/columns
stp/vertices/entries
stp/vertices/evtid
stp/vertices/evtid/entries
stp/vertices/evtid/pages
stp/vertices/forms
stp/vertices/n_part
stp/vertices/n_part/entries
stp/vertices/n_part/pages
stp/vertices/names
stp/vertices/time_in_ns
stp/vertices/time_in_ns/entries
stp/vertices/time_in_ns/pages
stp/vertices/xloc_in_m
stp/vertices/xloc_in_m/entries
stp/vertices/xloc_in_m/pages
stp/vertices/yloc_in_m
stp/vertices/yloc_in_m/entries
stp/vertices/yloc_in_m/pages
stp/vertices/zloc_in_m
stp/vertices/zloc_in_m/entries
stp/vertices/zloc_in_m/pages
50 changes: 50 additions & 0 deletions tests/output/macros/ntuple-per-det-vol.hdf5ls-lh5
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
stp : struct{det1,det2,scint1,scint2,vertices}
stp/det1 : table{evtid,particle,edep,time,xloc,yloc,zloc}
stp/det1/edep : array<1>{real}
stp/det1/evtid : array<1>{real}
stp/det1/particle : array<1>{real}
stp/det1/time : array<1>{real}
stp/det1/xloc : array<1>{real}
stp/det1/yloc : array<1>{real}
stp/det1/zloc : array<1>{real}
stp/det2 : table{evtid,particle,edep,time,xloc,yloc,zloc}
stp/det2/edep : array<1>{real}
stp/det2/evtid : array<1>{real}
stp/det2/particle : array<1>{real}
stp/det2/time : array<1>{real}
stp/det2/xloc : array<1>{real}
stp/det2/yloc : array<1>{real}
stp/det2/zloc : array<1>{real}
stp/scint1 : table{evtid,particle,edep,time,xloc_pre,yloc_pre,zloc_pre,xloc_post,yloc_post,zloc_post,v_pre,v_post}
stp/scint1/edep : array<1>{real}
stp/scint1/evtid : array<1>{real}
stp/scint1/particle : array<1>{real}
stp/scint1/time : array<1>{real}
stp/scint1/v_post : array<1>{real}
stp/scint1/v_pre : array<1>{real}
stp/scint1/xloc_post : array<1>{real}
stp/scint1/xloc_pre : array<1>{real}
stp/scint1/yloc_post : array<1>{real}
stp/scint1/yloc_pre : array<1>{real}
stp/scint1/zloc_post : array<1>{real}
stp/scint1/zloc_pre : array<1>{real}
stp/scint2 : table{evtid,particle,edep,time,xloc_pre,yloc_pre,zloc_pre,xloc_post,yloc_post,zloc_post,v_pre,v_post}
stp/scint2/edep : array<1>{real}
stp/scint2/evtid : array<1>{real}
stp/scint2/particle : array<1>{real}
stp/scint2/time : array<1>{real}
stp/scint2/v_post : array<1>{real}
stp/scint2/v_pre : array<1>{real}
stp/scint2/xloc_post : array<1>{real}
stp/scint2/xloc_pre : array<1>{real}
stp/scint2/yloc_post : array<1>{real}
stp/scint2/yloc_pre : array<1>{real}
stp/scint2/zloc_post : array<1>{real}
stp/scint2/zloc_pre : array<1>{real}
stp/vertices : table{evtid,time,xloc,yloc,zloc,n_part}
stp/vertices/evtid : array<1>{real}
stp/vertices/n_part : array<1>{real}
stp/vertices/time : array<1>{real}
stp/vertices/xloc : array<1>{real}
stp/vertices/yloc : array<1>{real}
stp/vertices/zloc : array<1>{real}
19 changes: 19 additions & 0 deletions tests/output/macros/ntuple-per-det-vol.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/RMG/Output/NtuplePerDetector true
/RMG/Output/NtupleUseVolumeName true

/RMG/Geometry/RegisterDetector Scintillator scint1 1
/RMG/Geometry/RegisterDetector Scintillator scint2 2
/RMG/Geometry/RegisterDetector Germanium det1 11
/RMG/Geometry/RegisterDetector Germanium det2 12

/control/execute macros/_init.mac

/RMG/Generator/Confine UnConfined

/RMG/Generator/Select GPS
/gps/position 0 0 0
/gps/ang/type iso
/gps/particle gamma
/gps/energy 200 keV

/run/beamOn 50
Loading

0 comments on commit 5a77fa8

Please sign in to comment.