ARTEMIS-CRIB
 
Loading...
Searching...
No Matches
TMUXCalibrationProcessor.cc
Go to the documentation of this file.
1/**
2 * @file TMUXCalibrationProcessor.cc
3 * @brief Implementation of the TMUXCalibrationProcessor class for calibrating timing, charge, and position data.
4 * @author Kodai Okawa<okawa@cns.s.u-tokyo.ac.jp>
5 * @date 2022-01-30 11:09:46
6 * @note last modified: 2025-03-05 18:33:27
7 * @details
8 */
9
11#include "../TProcessorUtil.h"
12
13#include "TMUXData.h"
15#include <TAffineConverter.h>
16#include <TRandom.h>
17#include <TTimingChargeData.h>
18#include <constant.h>
19
20/// ROOT macro for class implementation
22
23namespace art::crib {
24
25namespace {
26/**
27 * @brief Constant representing no conversion.
28 */
29const TString kNoConversion = "no_conversion";
30/**
31 * @brief Threshold for applying reflection to the detector ID.
32 */
33constexpr int kReflectionThreshold = 8;
34} // namespace
35
37 : fInData(nullptr), fOutData(nullptr),
38 fTimingConverterArray(nullptr), fChargeConverterArray(nullptr),
39 fPositionConverterArray(nullptr) {
40 RegisterInputCollection("InputCollection", "Array of TMUXData objects",
41 fInputColName, TString("mux_raw"));
42 RegisterOutputCollection("OutputCollection", "Output array of TTimingChargeData objects",
43 fOutputColName, TString("mux_cal"));
44
45 RegisterProcessorParameter("TimingConverterArray",
46 "Timing parameter object of TAffineConverter",
47 fTimingConverterArrayName, kNoConversion);
48 RegisterProcessorParameter("ChargeConverterArray",
49 "Energy parameter object of TAffineConverter",
50 fChargeConverterArrayName, kNoConversion);
51 RegisterProcessorParameter("PositionConverterArray",
52 "Position parameter object of TMUXPositionConverter",
53 fPositionConverterArrayName, kNoConversion);
54
55 RegisterProcessorParameter("HasReflection", "Reverse strip order (0--7) if true",
56 fHasReflection, kFALSE);
57 RegisterProcessorParameter("InputIsDigital", "Add randomness if true",
58 fInputIsDigital, kTRUE);
59}
60
65
66/**
67 * @details
68 * This method initializes the processor by:
69 * - Retrieving the input and output data collections based on user configuration.
70 * - Setting up the converter arrays (`fTimingConverterArray`, `fChargeConverterArray`, `fPositionConverterArray`)
71 * based on the specified parameters.
72 * - Logging warnings if parameters are not set or if required converters are missing.
73 *
74 * If the required position converter array is not provided, the processor is put into an error state.
75 */
76void TMUXCalibrationProcessor::Init(TEventCollection *col) {
77 // lambda function for initialize converter arrays
78 auto initConverterArray = [this, col](const TString &name, TClonesArray *&array, const char *paramName) {
79 if (name == kNoConversion) {
80 Warning("Init", "Parameter '%s' is not set. Using no conversion.", paramName);
81 array = nullptr;
82 } else {
83 // need to inherit from art::TConverterBase
85 col, name, "TClonesArray", "art::TConverterBase");
86 if (std::holds_alternative<TString>(result)) {
87 Warning("Init", std::get<TString>(result));
88 array = nullptr;
89 } else {
90 array = std::get<TClonesArray *>(result);
91 }
92 }
93 };
94
96 col, fInputColName, "TClonesArray", "art::crib::TMUXData");
97
98 if (std::holds_alternative<TString>(result)) {
99 SetStateError(std::get<TString>(result));
100 return;
101 }
102 fInData = std::get<TClonesArray **>(result);
103 Info("Init", "%s => %s", fInputColName.Data(), fOutputColName.Data());
104
105 initConverterArray(fTimingConverterArrayName, fTimingConverterArray, "TimingConverterArray");
106 initConverterArray(fChargeConverterArrayName, fChargeConverterArray, "ChargeConverterArray");
107 initConverterArray(fPositionConverterArrayName, fPositionConverterArray, "PositionConverterArray");
109 SetStateError("Position parameters are required");
110 return;
111 }
112
113 fOutData = new TClonesArray("art::TTimingChargeData");
114 fOutData->SetName(fOutputColName);
115 col->Add(fOutputColName, fOutData, fOutputIsTransparent);
116}
117
118/**
119 * @details
120 * The `Process` method performs the following steps:
121 * 1. Clears the output data collection to prepare for new entries.
122 * 2. Checks the validity of the input data:
123 * - If no data is present (`nData == 0`), the method exits as this is a valid condition.
124 * - If multiple data entries are found, a warning is logged and the method exits, as multi-data processing is not supported.
125 * 3. Retrieves the first entry in the input collection and casts it to `TMUXData`.
126 * 4. Converts the position data (`P1`) to a detector ID.
127 * 5. Applies reflection to the detector ID if `fHasReflection` is enabled.
128 * 6. Calibrates the charge and timing values using the respective converter arrays.
129 * 7. Stores the calibrated data in the output collection.
130 */
132 fOutData->Clear("C");
133 if (!fInData) {
134 Warning("Process", "No Input Data object");
135 return;
136 }
137
138 const int nData = (*fInData)->GetEntriesFast();
139 if (nData == 0)
140 return;
141 else if (nData > 1) {
142 Warning("Process", "It doesn't support multi-data currently");
143 return;
144 }
145
146 const auto *data = dynamic_cast<const TMUXData *>((*fInData)->At(0));
147 if (!data)
148 return;
149
150 // int mux_detid = data->GetID();
151 int hit1_detid = ConvertPosition(data->GetP1(), 0);
152 // int hit2_detid = ConvertPosition(data->GetP2(), 1); // not implemented
153
154 if (!IsValid(hit1_detid))
155 return;
156
157 if (fHasReflection && hit1_detid >= 0 && hit1_detid < kReflectionThreshold) {
158 hit1_detid = kReflectionThreshold - 1 - hit1_detid;
159 }
160
161 auto *outData = static_cast<TTimingChargeData *>(fOutData->ConstructedAt(0));
162 outData->SetID(hit1_detid);
163 outData->SetCharge(CalibrateValue(data->GetE1(), hit1_detid, fChargeConverterArray));
164 outData->SetTiming(CalibrateValue(data->GetTrig(), hit1_detid, fTimingConverterArray));
165}
166
167/**
168 * @details
169 * Converts a raw position value to a detector ID by using the position converter array.
170 * If the position converter array is not available or the conversion fails, an invalid value (`kInvalidD`) is returned.
171 */
172double TMUXCalibrationProcessor::ConvertPosition(double pos, int id) const {
174 return kInvalidD;
175 auto *converter = static_cast<TMUXPositionConverter *>(fPositionConverterArray->At(id));
176 return converter ? converter->Convert(pos) : kInvalidD;
177}
178
179/**
180 * @details
181 * Calibrates a raw value (such as timing or charge) using the specified converter array.
182 * If the input is digital (`fInputIsDigital`), a random value is added to the raw input to simulate analog noise.
183 * If the converter array is not available or the conversion fails, the raw value is returned unchanged.
184 */
185double TMUXCalibrationProcessor::CalibrateValue(double raw, int id, const TClonesArray *converterArray) const {
186 raw += (fInputIsDigital ? gRandom->Uniform() : 0);
187 if (!converterArray)
188 return raw;
189 auto *converter = static_cast<TAffineConverter *>(converterArray->At(id));
190 return converter ? converter->Convert(raw) : raw;
191}
192
193} // namespace art::crib
ClassImp(art::crib::TMUXCalibrationProcessor)
ROOT macro for class implementation.
Processor for calibrating timing, charge, and position data in the MUX system.
Header file for the TMUXData class.
Utility functions for handling input and parameter objects in TEventCollection.
Handles the calibration of timing, charge, and position data in the MUX system.
Bool_t fHasReflection
Indicates whether to apply reflection to the detector ID.
TClonesArray * fChargeConverterArray
! Pointer to the charge converter array.
TClonesArray * fTimingConverterArray
! Pointer to the timing converter array.
double CalibrateValue(double raw, int id, const TClonesArray *converterArray) const
Calibrates a raw value (e.g., timing or charge) using the specified converter array.
Bool_t fInputIsDigital
Indicates whether the input data is digital.
TClonesArray * fOutData
! Pointer to the output data collection.
TString fPositionConverterArrayName
Name of the position converter array parameter.
TClonesArray ** fInData
! Pointer to the input data collection.
TString fChargeConverterArrayName
Name of the charge converter array parameter.
TString fTimingConverterArrayName
Name of the timing converter array parameter.
void Process() override
Processes the input data, applies calibration, and stores the results.
TString fInputColName
Name of the input collection.
double ConvertPosition(double pos, int id) const
Converts a raw position value to a detector ID using the position converter array.
TString fOutputColName
Name of the output collection.
void Init(TEventCollection *col) override
Initializes the processor by setting up input and output collections.
TClonesArray * fPositionConverterArray
! Pointer to the position converter array.
Represents data from the MUX module (E1, E2, P1, P2, T).
Definition TMUXData.h:25
A class for loading and converting MUX position parameters.
Double_t Convert(Double_t val) const override
Converts a value based on the loaded parameters.
std::enable_if_t< std::is_base_of_v< TObject, T >, std::variant< T **, TString > > GetInputObject(TEventCollection *col, const TString &name, const TString &expectedTypeName, const TString &elementTypeName="TObject")
Retrieve an object from TEventCollection with type validation.
std::enable_if_t< std::is_base_of_v< TObject, T >, std::variant< T *, TString > > GetParameterObject(TEventCollection *col, const TString &name, const TString &expectedTypeName, const TString &elementTypeName="art::TParameterObject")
Retrieves a parameter object from a TEventCollection.
return to the guide