REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDetectorSignalToRawSignalProcess.cxx
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
116
117#include "TRestDetectorSignalToRawSignalProcess.h"
118
119using namespace std;
120
122
127
141 Initialize();
142 LoadConfig(configFilename);
143}
144
150}
151
164void TRestDetectorSignalToRawSignalProcess::LoadConfig(const string& configFilename, const string& name) {
165 LoadConfigFromFile(configFilename, name);
166}
167
173 SetSectionName(this->ClassName());
174 SetLibraryVersion(LIBRARY_VERSION);
175
176 fInputSignalEvent = nullptr;
178}
179
184 if (fTriggerMode == "fixed") {
186 fTimeEnd = fTimeStart + fNPoints * fSampling;
187 }
188
189 const set<string> validTriggerModes = {"firstDeposit", "integralThreshold", "fixed"};
190 if (validTriggerModes.count(fTriggerMode.Data()) == 0) {
191 RESTError << "Trigger mode set to: '" << fTriggerMode
192 << "' which is not a valid trigger mode. Please use one of the following trigger modes: ";
193 for (const auto& triggerMode : validTriggerModes) {
194 RESTError << triggerMode << " ";
195 }
196 RESTError << RESTendl;
197 exit(1);
198 }
199
200 if (IsLinearCalibration()) {
201 const auto range = numeric_limits<Short_t>::max() - numeric_limits<Short_t>::min();
204 fCalibrationOffset = range * (fCalibrationRange.X() - fCalibrationGain * fCalibrationEnergy.X()) +
205 numeric_limits<Short_t>::min();
206 }
207}
208
214
215 if (fInputSignalEvent->GetNumberOfSignals() <= 0) {
216 return nullptr;
217 }
218
220 fOutputRawSignalEvent->PrintEvent();
221 }
222
224 fOutputRawSignalEvent->SetSubID(fInputSignalEvent->GetSubID());
225 fOutputRawSignalEvent->SetTimeStamp(fInputSignalEvent->GetTimeStamp());
226 fOutputRawSignalEvent->SetSubEventTag(fInputSignalEvent->GetSubEventTag());
227
228 if (fTriggerMode == "firstDeposit") {
229 fTimeStart = fInputSignalEvent->GetMinTime() - fTriggerDelay * fSampling;
230 fTimeEnd = fInputSignalEvent->GetMinTime() + (fNPoints - fTriggerDelay) * fSampling;
231 } else if (fTriggerMode == "integralThreshold") {
232 bool thresholdReached = false;
233 for (Double_t t = fInputSignalEvent->GetMinTime() - fNPoints * fSampling;
234 t <= fInputSignalEvent->GetMaxTime() + fNPoints * fSampling; t = t + 0.5) {
235 Double_t energy = fInputSignalEvent->GetIntegralWithTime(t, t + (fSampling * fNPoints) / 2.);
236
237 if (energy > fIntegralThreshold) {
238 fTimeStart = t - fTriggerDelay * fSampling;
239 fTimeEnd = t + (fNPoints - fTriggerDelay) * fSampling;
240 thresholdReached = true;
241 }
242 }
243 if (!thresholdReached) {
244 RESTWarning << "Integral threshold for trigger not reached" << RESTendl;
245 fTimeStart = 0;
246 fTimeEnd = fNPoints * fSampling;
247 }
248 }
249
250 RESTDebug << "fTimeStart : " << fTimeStart << " us " << RESTendl;
251 RESTDebug << "fTimeEnd : " << fTimeEnd << " us " << RESTendl;
252
253 for (int n = 0; n < fInputSignalEvent->GetNumberOfSignals(); n++) {
254 vector<Double_t> data(fNPoints, fCalibrationOffset);
255 TRestDetectorSignal* signal = fInputSignalEvent->GetSignal(n);
256 Int_t signalID = signal->GetSignalID();
257
258 for (int m = 0; m < signal->GetNumberOfPoints(); m++) {
259 Double_t t = signal->GetTime(m);
260 Double_t d = signal->GetData(m);
261
263 cout << "Signal : " << n << " Sample : " << m << " T : " << t << " Data : " << d << endl;
264 }
265
266 if (t > fTimeStart && t < fTimeEnd) {
267 // convert physical time (in us) to timeBin
268 Int_t timeBin = (Int_t)round((t - fTimeStart) / fSampling);
269
270 if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning) {
271 if (timeBin < 0 || timeBin > fNPoints) {
272 cout << "Time bin out of range!!! bin value : " << timeBin << endl;
273 timeBin = 0;
274 }
275 }
276
277 RESTDebug << "Adding data : " << signal->GetData(m) << " to Time Bin : " << timeBin
278 << RESTendl;
279 data[timeBin] += fCalibrationGain * signal->GetData(m);
280 }
281 }
282
283 if (IsShapingEnabled()) {
284 const auto shapingFunction = [](Double_t t) -> Double_t {
285 if (t <= 0) {
286 return 0;
287 }
288 // function is normalized such that its absolute maximum is 1.0
289 // max is at x = 1.1664004483744728
290 return TMath::Exp(-3.0 * t) * TMath::Power(t, 3.0) * TMath::Sin(t) * 22.68112123672292;
291 };
292
293 vector<Double_t> dataAfterShaping(fNPoints, fCalibrationOffset);
294 for (int i = 0; i < fNPoints; i++) {
295 const Double_t value = data[i] - fCalibrationOffset;
296 if (value <= 0) {
297 // Only positive values are possible, 0 means no signal in this bin
298 continue;
299 }
300 for (int j = 0; j < fNPoints; j++) {
301 dataAfterShaping[j] += value * shapingFunction(((j - i) * fSampling) / fShapingTime);
302 }
303 }
304 data = dataAfterShaping;
305 }
306
307 TRestRawSignal rawSignal;
308 rawSignal.SetSignalID(signalID);
309 for (int x = 0; x < fNPoints; x++) {
310 double value = round(data[x]);
311 if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning) {
312 if (value < numeric_limits<Short_t>::min() || value > numeric_limits<Short_t>::max()) {
313 RESTWarning << "value (" << value << ") is outside short range ("
314 << numeric_limits<Short_t>::min() << ", " << numeric_limits<Short_t>::max()
315 << ")" << RESTendl;
316 }
317 }
318
319 if (value < numeric_limits<Short_t>::min()) {
320 value = numeric_limits<Short_t>::min();
321 fOutputRawSignalEvent->SetOK(false);
322 } else if (value > numeric_limits<Short_t>::max()) {
323 value = numeric_limits<Short_t>::max();
324 fOutputRawSignalEvent->SetOK(false);
325 }
326 rawSignal.AddPoint((Short_t)value);
327 }
328
330 rawSignal.Print();
331 }
332 RESTDebug << "Adding signal to raw signal event" << RESTendl;
333
334 fOutputRawSignalEvent->AddSignal(rawSignal);
335 }
336
337 RESTDebug << "TRestDetectorSignalToRawSignalProcess. Returning event with N signals "
338 << fOutputRawSignalEvent->GetNumberOfSignals() << RESTendl;
339
341}
A process to convert a TRestDetectorSignalEvent into a TRestRawSignalEvent.
TVector2 fCalibrationEnergy
two distinct energy values used for calibration
TRestDetectorSignalEvent * fInputSignalEvent
A pointer to the specific TRestDetectorSignalEvent input.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
Double_t fSampling
The sampling time from the binned raw output signal.
void Initialize() override
Function to initialize input/output event members and define the section name.
void InitProcess() override
Some actions taken before start the event data processing.
TRestRawSignalEvent * fOutputRawSignalEvent
A pointer to the specific TRestRawSignalEvent input.
TVector2 fCalibrationRange
position in the range corresponding to the energy in 'fCalibrationEnergy'. Values between 0 and 1
Double_t fIntegralThreshold
This parameter is used by integralWindow trigger mode to define the acquisition window.
TString fTriggerMode
It is used to define the way the time start will be fixed.
Int_t fNPoints
The number of points of the resulting output signal.
Int_t fTriggerFixedStartTime
The starting time for the "fixed" trigger mode (can be offset by the trigger delay)
Int_t fTriggerDelay
The number of time bins the time start is delayed in the resulting output signal.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
A base class for any REST event.
Definition: TRestEvent.h:38
endl_t RESTendl
Termination flag object for TRestStringOutput.
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
void SetLibraryVersion(TString version)
Set the library version of this metadata class.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
void SetSectionName(std::string sName)
set the section name, clear the section content
An event container for time rawdata signals with fixed length.
It defines a Short_t array with a physical parameter that evolves in time using a fixed time bin.
void Print() const
It prints the signal data on screen.
void SetSignalID(Int_t sID)
It sets the id number of the signal.
void AddPoint(Short_t)
Adds a new point to the end of the signal data array.
@ REST_Debug
+show the defined debug messages