|
| 1 | +/* -------------------------------------------------- |
| 2 | + Copyright (C): OpenGATE Collaboration |
| 3 | + This software is distributed under the terms |
| 4 | + of the GNU Lesser General Public Licence (LGPL) |
| 5 | + See LICENSE.md for further details |
| 6 | + -------------------------------------------------- */ |
| 7 | + |
| 8 | +#include "GateDigitizerDeadTimeActor.h" |
| 9 | +#include "../GateHelpersDict.h" |
| 10 | +#include "GateHelpers.h" |
| 11 | +#include "GateHelpersDigitizer.h" |
| 12 | +#include <memory> |
| 13 | + |
| 14 | +GateDigitizerDeadTimeActor::GateDigitizerDeadTimeActor(py::dict &user_info) |
| 15 | + : GateVDigitizerWithOutputActor(user_info, true) { |
| 16 | + fActions.insert("EndOfEventAction"); |
| 17 | + fActions.insert("EndOfRunAction"); |
| 18 | +} |
| 19 | + |
| 20 | +GateDigitizerDeadTimeActor::~GateDigitizerDeadTimeActor() = default; |
| 21 | + |
| 22 | +void GateDigitizerDeadTimeActor::InitializeUserInfo(py::dict &user_info) { |
| 23 | + |
| 24 | + GateVDigitizerWithOutputActor::InitializeUserInfo(user_info); |
| 25 | + if (py::len(user_info) > 0 && user_info.contains("dead_time")) { |
| 26 | + fDeadTime = DictGetDouble(user_info, "dead_time"); // nanoseconds |
| 27 | + } |
| 28 | + if (py::len(user_info) > 0 && user_info.contains("policy")) { |
| 29 | + const auto policy_str = DictGetStr(user_info, "policy"); |
| 30 | + if (policy_str == "NonParalyzable") { |
| 31 | + fPolicy = DeadTimePolicy::NonParalyzable; |
| 32 | + } else if (policy_str == "Paralyzable") { |
| 33 | + fPolicy = DeadTimePolicy::Paralyzable; |
| 34 | + } else { |
| 35 | + Fatal("Unknown dead time policy '" + policy_str + "'"); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (py::len(user_info) > 0 && user_info.contains("sorting_time")) { |
| 40 | + fSortingTime = DictGetDouble(user_info, "sorting_time"); // nanoseconds |
| 41 | + } |
| 42 | + fGroupVolumeDepth = -1; |
| 43 | + fInputDigiCollectionName = DictGetStr(user_info, "input_digi_collection"); |
| 44 | +} |
| 45 | + |
| 46 | +void GateDigitizerDeadTimeActor::BeginOfRunActionMasterThread(int run_id) { |
| 47 | + |
| 48 | + fTimeSorter = std::make_unique<GateTimeSorter>(fOutputDigiCollectionName); |
| 49 | + fTimeSorter->Init(fInputDigiCollection); |
| 50 | + fTimeSorter->SetSortingWindow(fSortingTime); |
| 51 | + fTimeSorter->SetMaxSize(fClearEveryNEvents); |
| 52 | + |
| 53 | + auto &outputIter = fTimeSorter->OutputIterator(); |
| 54 | + outputIter.TrackAttribute("GlobalTime", &fTimeSorterOutputTime); |
| 55 | + outputIter.TrackAttribute("PreStepUniqueVolumeID", &fTimeSorterOutputVolID); |
| 56 | + |
| 57 | + fillerOut = std::make_unique<GateDigiAttributesFiller>( |
| 58 | + fTimeSorter->OutputCollection(), fOutputDigiCollection, |
| 59 | + fTimeSorter->OutputCollection()->GetDigiAttributeNames()); |
| 60 | + |
| 61 | + fVolumeEndOfDeadTimeInterval.clear(); |
| 62 | +} |
| 63 | + |
| 64 | +void GateDigitizerDeadTimeActor::SetGroupVolumeDepth(const int depth) { |
| 65 | + fGroupVolumeDepth = depth; |
| 66 | +} |
| 67 | + |
| 68 | +void GateDigitizerDeadTimeActor::DigitInitialize( |
| 69 | + const std::vector<std::string> &attributes_not_in_filler) { |
| 70 | + |
| 71 | + auto a = attributes_not_in_filler; |
| 72 | + GateVDigitizerWithOutputActor::DigitInitialize(a); |
| 73 | + |
| 74 | + fOutputDigiCollection->RootInitializeTupleForWorker(); |
| 75 | +} |
| 76 | + |
| 77 | +void GateDigitizerDeadTimeActor::EndOfEventAction(const G4Event *) { |
| 78 | + |
| 79 | + fTimeSorter->OnEndOfEventAction([this]() { ProcessTimeSortedDigis(); }); |
| 80 | +} |
| 81 | + |
| 82 | +void GateDigitizerDeadTimeActor::EndOfRunAction(const G4Run *) { |
| 83 | + |
| 84 | + fTimeSorter->OnEndOfRunAction( |
| 85 | + [this]() { fOutputDigiCollection->FillToRootIfNeeded(true); }, |
| 86 | + [this]() { ProcessTimeSortedDigis(); }); |
| 87 | +} |
| 88 | + |
| 89 | +void GateDigitizerDeadTimeActor::ProcessTimeSortedDigis() { |
| 90 | + |
| 91 | + auto &iter = fTimeSorter->OutputIterator(); |
| 92 | + iter.GoToBegin(); |
| 93 | + while (!iter.IsAtEnd()) { |
| 94 | + |
| 95 | + const auto volHash = |
| 96 | + fTimeSorterOutputVolID->get()->GetIdUpToDepthAsHash(fGroupVolumeDepth); |
| 97 | + std::optional<double> endTime{}; |
| 98 | + auto it = fVolumeEndOfDeadTimeInterval.find(volHash); |
| 99 | + if (it != fVolumeEndOfDeadTimeInterval.end()) { |
| 100 | + endTime = it->second; |
| 101 | + } |
| 102 | + |
| 103 | + const auto currentTime = *fTimeSorterOutputTime; |
| 104 | + if (!endTime.has_value() || currentTime > *endTime) { |
| 105 | + // Digi goes to the output. |
| 106 | + fillerOut->Fill(iter.fIndex); |
| 107 | + // Update end of dead time interval. |
| 108 | + fVolumeEndOfDeadTimeInterval[volHash] = currentTime + fDeadTime; |
| 109 | + } else { |
| 110 | + // Digi is dropped because it arrived during a dead time interval. |
| 111 | + if (fPolicy == DeadTimePolicy::NonParalyzable) { |
| 112 | + // End of dead time interval does not change. |
| 113 | + } else if (fPolicy == DeadTimePolicy::Paralyzable) { |
| 114 | + // End of dead time interval is updated. |
| 115 | + fVolumeEndOfDeadTimeInterval[volHash] = currentTime + fDeadTime; |
| 116 | + } else { |
| 117 | + Fatal("Unknown dead time policy"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + iter++; |
| 122 | + } |
| 123 | + fTimeSorter->MarkOutputAsProcessed(); |
| 124 | +} |
0 commit comments