Skip to content

Commit

Permalink
allow to re-use uids for multiple sensitive detectors (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu authored May 6, 2024
1 parent acc2a76 commit 6d98850
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion include/RMGHardware.hh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class RMGHardware : public G4VUserDetectorConstruction {
int uid;
};

void RegisterDetector(DetectorType type, const std::string& pv_name, int uid, int copy_nr = 0);
void RegisterDetector(DetectorType type, const std::string& pv_name, int uid, int copy_nr = 0,
bool allow_uid_reuse = false);
inline const auto& GetDetectorMetadataMap() { return fDetectorMetadata; }
inline const auto& GetDetectorMetadata(const std::pair<std::string, int>& det) {
return fDetectorMetadata.at(det);
Expand Down
7 changes: 7 additions & 0 deletions src/RMGGermaniumOutputScheme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "RMGGermaniumOutputScheme.hh"

#include <set>

#include "G4AnalysisManager.hh"
#include "G4Event.hh"
#include "G4HCtable.hh"
Expand All @@ -34,9 +36,14 @@ void RMGGermaniumOutputScheme::AssignOutputNames(G4AnalysisManager* ana_man) {
const auto det_cons = rmg_man->GetDetectorConstruction();
const auto detectors = det_cons->GetDetectorMetadataMap();

std::set<int> registered_uids;
for (auto&& det : detectors) {
if (det.second.type != RMGHardware::kGermanium) continue;

// do not register the ntuple twice if two detectors share their uid.
auto had_uid = registered_uids.emplace(det.second.uid);
if (!had_uid.second) continue;

auto id = rmg_man->RegisterNtuple(det.second.uid);
ana_man->CreateNtuple(this->GetNtupleName(det.second.uid), "Event data");

Expand Down
18 changes: 10 additions & 8 deletions src/RMGHardware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,20 @@ void RMGHardware::ConstructSDandField() {
}

void RMGHardware::RegisterDetector(DetectorType type, const std::string& pv_name, int uid,
int copy_nr) {
int copy_nr, bool allow_uid_reuse) {
if (fActiveDetectorsInitialized) {
RMGLog::Out(RMGLog::error,
"Active detectors cannot be mutated after constructing the detector.");
return;
}

// sanity check
for (const auto& [k, v] : fDetectorMetadata) {
if (v.uid == uid) {
RMGLog::OutFormat(RMGLog::error, "UID {} has already been assigned", uid);
return;
// sanity check for duplicate uids.
if (!allow_uid_reuse) {
for (const auto& [k, v] : fDetectorMetadata) {
if (v.uid == uid) {
RMGLog::OutFormat(RMGLog::error, "UID {} has already been assigned", uid);
return;
}
}
}

Expand All @@ -187,8 +189,8 @@ void RMGHardware::RegisterDetector(DetectorType type, const std::string& pv_name
copy_nr);
} else {
RMGLog::OutFormat(RMGLog::detail,
"Registered physical volume '{}' (copy nr. {}) as {} detector type", pv_name, copy_nr,
magic_enum::enum_name(type));
"Registered physical volume '{}' (copy nr. {}) as {} detector type (uid={})", pv_name,
copy_nr, magic_enum::enum_name(type), uid);
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/RMGHardwareMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "RMGHardwareMessenger.hh"

#include "G4Tokenizer.hh"
#include "G4UIcommand.hh"

#include "RMGHardware.hh"
#include "RMGTools.hh"
Expand All @@ -42,6 +43,12 @@ RMGHardwareMessenger::RMGHardwareMessenger(RMGHardware* hw) : fHardware(hw) {
p_copy->SetDefaultValue("0");
fRegisterCmd->SetParameter(p_copy);

auto p_reuse = new G4UIparameter("allow_id_reuse", 'b', true);
p_reuse->SetGuidance(
"append this volume to a previously allocated unique detector id, instead of erroring out.");
p_reuse->SetDefaultValue("false");
fRegisterCmd->SetParameter(p_reuse);

fRegisterCmd->AvailableForStates(G4State_PreInit);
}

Expand All @@ -61,8 +68,11 @@ void RMGHardwareMessenger::RegisterDetectorCmd(const std::string& parameters) {
int copy_nr = 0;
auto copy_nr_str = next();
if (!copy_nr_str.empty()) copy_nr = std::stoi(copy_nr_str);
bool allow_reuse = false;
auto allow_reuse_str = next();
if (!allow_reuse_str.empty()) allow_reuse = G4UIcommand::ConvertToBool(allow_reuse_str);

fHardware->RegisterDetector(type, pv_name, uid, copy_nr);
fHardware->RegisterDetector(type, pv_name, uid, copy_nr, allow_reuse);
}

// vim: tabstop=2 shiftwidth=2 expandtab
7 changes: 7 additions & 0 deletions src/RMGOpticalOutputScheme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "RMGOpticalOutputScheme.hh"

#include <set>

#include "G4AnalysisManager.hh"
#include "G4Event.hh"
#include "G4HCtable.hh"
Expand All @@ -34,9 +36,14 @@ void RMGOpticalOutputScheme::AssignOutputNames(G4AnalysisManager* ana_man) {
const auto det_cons = rmg_man->GetDetectorConstruction();
const auto detectors = det_cons->GetDetectorMetadataMap();

std::set<int> registered_uids;
for (auto&& det : detectors) {
if (det.second.type != RMGHardware::kOptical) continue;

// do not register the ntuple twice if two detectors share their uid.
auto had_uid = registered_uids.emplace(det.second.uid);
if (!had_uid.second) continue;

auto id = rmg_man->RegisterNtuple(det.second.uid);
ana_man->CreateNtuple(this->GetNtupleName(det.second.uid), "Event data");

Expand Down

0 comments on commit 6d98850

Please sign in to comment.