REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
src/TRestRawSignalRangeReductionProcess.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
23#include "TRestRawSignalRangeReductionProcess.h"
24
25using namespace std;
26
28
29TRestRawSignalRangeReductionProcess::TRestRawSignalRangeReductionProcess() { Initialize(); }
30
31TRestRawSignalRangeReductionProcess::TRestRawSignalRangeReductionProcess(const char* configFilename) {
32 Initialize();
33 if (LoadConfigFromFile(configFilename) == -1) {
34 LoadDefaultConfig();
35 }
36}
37
38TRestRawSignalRangeReductionProcess::~TRestRawSignalRangeReductionProcess() { delete fOutputRawSignalEvent; }
39
40void TRestRawSignalRangeReductionProcess::LoadDefaultConfig() {
41 SetName("rawSignalRangeReductionProcess-default");
42 SetTitle("Default config");
43}
44
46 SetSectionName(this->ClassName());
47 SetLibraryVersion(LIBRARY_VERSION);
48
49 fInputRawSignalEvent = nullptr;
50 fOutputRawSignalEvent = new TRestRawSignalEvent();
51}
52
53void TRestRawSignalRangeReductionProcess::LoadConfig(const string& configFilename, const string& name) {
54 LoadConfigFromFile(configFilename, name);
55}
56
58 const UShort_t resolutionInBits =
59 (UShort_t)StringToDouble(GetParameter("resolutionInBits", fResolutionInBits));
60 SetResolutionInNumberOfBits(resolutionInBits);
61
62 const TVector2 DigitizationRange = Get2DVectorParameterWithUnits("inputRange", fDigitizationInputRange);
63 SetDigitizationInputRange(DigitizationRange);
64}
65
67 fDigitizationOutputRange = {0, TMath::Power(2, fResolutionInBits) - 1};
68}
69
71 fInputRawSignalEvent = (TRestRawSignalEvent*)inputEvent;
72
73 if (fInputRawSignalEvent->GetNumberOfSignals() <= 0) {
74 return nullptr;
75 }
76
77 for (int n = 0; n < fInputRawSignalEvent->GetNumberOfSignals(); n++) {
78 const TRestRawSignal* inputSignal = fInputRawSignalEvent->GetSignal(n);
79 TRestRawSignal signal;
80 signal.SetSignalID(inputSignal->GetSignalID());
81
82 for (int i = 0; i < inputSignal->GetNumberOfPoints(); i++) {
83 const Double_t value = (Double_t)inputSignal->GetData(i);
84 Double_t newValue = ConvertFromStartingRangeToTargetRange(value);
85 signal.AddPoint(newValue);
86 }
87
88 fOutputRawSignalEvent->AddSignal(signal);
89 }
90
91 return fOutputRawSignalEvent;
92}
93
94void TRestRawSignalRangeReductionProcess::SetResolutionInNumberOfBits(UShort_t nBits) {
95 if (nBits < 0 || nBits > 16) {
96 RESTWarning << "Number of bits must be between 1 and 16. Setting it to " << fResolutionInBits
97 << " bits" << RESTendl;
98 return;
99 }
100 fResolutionInBits = nBits;
101}
102
103void TRestRawSignalRangeReductionProcess::SetDigitizationInputRange(const TVector2& range) {
104 fDigitizationInputRange = range;
105
106 const auto limitMin = std::numeric_limits<Short_t>::min();
107 const auto limitMax = std::numeric_limits<Short_t>::max();
108 if (range.X() < limitMin) {
109 RESTWarning << "TRestRawSignalRangeReductionProcess::SetDigitizationRange - user set start of "
110 "Digitization range to "
111 << range.X() << " which is below the limit of " << limitMin << ". Setting range start to "
112 << limitMin << RESTendl;
113 fDigitizationInputRange = TVector2(limitMin, range.Y());
114 }
115 if (range.Y() > limitMax) {
116 RESTWarning << "TRestRawSignalRangeReductionProcess::SetDigitizationRange - user set end of "
117 "Digitization range to "
118 << range.Y() << " which is above the limit of " << limitMax << ". Setting end start to "
119 << limitMax << RESTendl;
120 fDigitizationInputRange = TVector2(range.X(), limitMax);
121 }
122}
123
124Double_t TRestRawSignalRangeReductionProcess::ConvertFromStartingRangeToTargetRange(Double_t value) const {
125 const Double_t conversionFactor = (fDigitizationOutputRange.Y() - fDigitizationOutputRange.X()) /
126 (fDigitizationInputRange.Y() - fDigitizationInputRange.X());
127 const Double_t newValue =
128 round(fDigitizationOutputRange.X() + (value - fDigitizationInputRange.X()) * conversionFactor);
129
130 if (newValue < fDigitizationOutputRange.X()) {
131 return fDigitizationOutputRange.X();
132 } else if (newValue > fDigitizationOutputRange.Y()) {
133 return fDigitizationOutputRange.Y();
134 }
135
136 return newValue;
137}
138
139Double_t TRestRawSignalRangeReductionProcess::ConvertFromTargetRangeToStartingRange(Double_t value) const {
140 const Double_t conversionFactor = (fDigitizationInputRange.Y() - fDigitizationInputRange.X()) /
141 (fDigitizationOutputRange.Y() - fDigitizationOutputRange.X());
142 const Double_t newValue =
143 round(fDigitizationInputRange.X() + (value - fDigitizationOutputRange.X()) * conversionFactor);
144
145 if (newValue < fDigitizationInputRange.X()) {
146 return fDigitizationInputRange.X();
147 }
148 if (newValue > fDigitizationInputRange.Y()) {
149 return fDigitizationInputRange.Y();
150 }
151
152 return newValue;
153}
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.
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
An event container for time rawdata signals with fixed length.
void Initialize() override
Making default settings.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
It defines a Short_t array with a physical parameter that evolves in time using a fixed time bin.
void SetSignalID(Int_t sID)
It sets the id number of the signal.
Double_t GetData(Int_t n) const
It returns the data value of point n including baseline correction.
void AddPoint(Short_t)
Adds a new point to the end of the signal data array.
Int_t GetSignalID() const
Returns the value of signal ID.
Int_t GetNumberOfPoints() const
Returns the actual number of points, or size of the signal.
Double_t StringToDouble(std::string in)
Gets a double from a string.