REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRawCommonNoiseReductionProcess.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
86#include "TRestRawCommonNoiseReductionProcess.h"
87
88#include <algorithm>
89#include <iostream>
90#include <vector>
91
92using namespace std;
93
95
100
114 Initialize();
115
116 if (LoadConfigFromFile(configFilename)) {
118 }
119}
120
125 delete fInputEvent;
126 delete fOutputEvent;
127}
128
132void TRestRawCommonNoiseReductionProcess::LoadDefaultConfig() { SetTitle("Default config"); }
133
139 SetSectionName(this->ClassName());
140 SetLibraryVersion(LIBRARY_VERSION);
141
142 fInputEvent = nullptr;
144}
145
158void TRestRawCommonNoiseReductionProcess::LoadConfig(const string& configFilename, const string& name) {
159 if (LoadConfigFromFile(configFilename, name)) {
161 }
162}
163
168
173 fInputEvent = dynamic_cast<TRestRawSignalEvent*>(inputEvent);
174
175 const auto run = GetRunInfo();
176 if (run != nullptr) {
178 }
179
180 if (fInputEvent->GetNumberOfSignals() < fMinSignalsRequired) {
181 for (int signal = 0; signal < fInputEvent->GetNumberOfSignals(); signal++) {
182 fOutputEvent->AddSignal(*fInputEvent->GetSignal(signal));
183 }
184 return fOutputEvent;
185 }
186
187 if (fReadoutMetadata == nullptr) {
188 fReadoutMetadata = fInputEvent->GetReadoutMetadata();
189 }
190
191 if (fReadoutMetadata == nullptr && !fChannelType.empty()) {
192 cerr << "TRestRawCommonNoiseReductionProcess::ProcessEvent: readout metadata is null, cannot filter "
193 "the process by signal type"
194 << endl;
195 exit(1);
196 }
197
198 vector<TRestRawSignal*> signalsToProcess;
199 vector<TRestRawSignal*> signalsToIgnore;
200
201 if (fChannelType.empty()) {
202 for (int signal = 0; signal < fInputEvent->GetNumberOfSignals(); signal++) {
203 signalsToProcess.push_back(fInputEvent->GetSignal(signal));
204 }
205 } else {
206 for (int signal = 0; signal < fInputEvent->GetNumberOfSignals(); signal++) {
207 TRestRawSignal* signalPtr = fInputEvent->GetSignal(signal);
208 const UShort_t signalId = signalPtr->GetSignalID();
209 const string channelType = fReadoutMetadata->GetTypeForChannelDaqId(signalId);
210
211 if (fChannelType == channelType) {
212 signalsToProcess.push_back(signalPtr);
213 } else {
214 signalsToIgnore.push_back(signalPtr);
215 }
216 }
217 }
218
219 // add signals to ignore to the event
220 for (auto signal : signalsToIgnore) {
221 fOutputEvent->AddSignal(*signal);
222 }
223
224 auto eventToProcess = TRestRawSignalEvent();
225 for (auto signal : signalsToProcess) {
226 eventToProcess.AddSignal(*signal);
227 }
228
229 // Event baseline determination.
230 Double_t baseLineMean = 0;
231 for (int signal = 0; signal < eventToProcess.GetNumberOfSignals(); signal++) {
232 eventToProcess.GetSignal(signal)->CalculateBaseLine(20, 150);
233 Double_t baseline = eventToProcess.GetSignal(signal)->GetBaseLine();
234 baseLineMean += baseline;
235 }
236 Double_t Baseline = baseLineMean / eventToProcess.GetNumberOfSignals();
237
238 if (fBlocks == 0) {
239 Int_t N = eventToProcess.GetNumberOfSignals();
240
241 // if (GetVerboseLevel() >= REST_Debug) N = 1;
242 for (int signal = 0; signal < N; signal++) {
243 fOutputEvent->AddSignal(*eventToProcess.GetSignal(signal));
244 }
245
246 Int_t nBins = eventToProcess.GetSignal(0)->GetNumberOfPoints();
247 vector<Double_t> signalValues(N, 0.0);
248
249 for (Int_t bin = 0; bin < nBins; bin++) {
250 for (Int_t signal = 0; signal < N; signal++) {
251 signalValues[signal] = fOutputEvent->GetSignal(signal)->GetRawData(bin);
252 }
253
254 std::sort(signalValues.begin(), signalValues.end());
255
256 // Sorting the different methods
257 Int_t begin = 0, middle = 0, end = 0;
258 middle = (Int_t)N / 2;
259 Double_t norm = 1.0;
260
261 if (fMode == 0) {
262 // We take only the middle one
263 begin = (Int_t)((double_t)N / 2.0);
264 end = begin;
265 norm = 1.;
266 } else if (fMode == 1) {
267 // We take the average of the TRestDetectorSignals at the Center
268 begin = middle - (Int_t)(N * fCenterWidth * 0.01);
269 end = middle + (Int_t)(N * fCenterWidth * 0.01);
270 norm = (Double_t)end - begin;
271 }
272
273 // Calculation of the correction to be made to each TRestRawSignal
274 Double_t binCorrection = 0.0;
275 for (Int_t i = begin; i <= end; i++) binCorrection += signalValues[i];
276
277 binCorrection = binCorrection / norm;
278
279 // Correction applied.
280 for (Int_t signal = 0; signal < N; signal++)
281 fOutputEvent->GetSignal(signal)->IncreaseBinBy(bin, Baseline - binCorrection);
282 }
283
284 return fOutputEvent;
285 } else if (fBlocks == 1) {
286 Int_t N = 68;
287 Int_t nBlocks = 8;
288 Int_t firstID = 578;
289 Int_t gap = 4;
290
291 Int_t firstInBlock;
292 Int_t nSign;
293 Int_t sigID;
294
295 for (int block = 0; block < nBlocks; block++) {
296 firstInBlock = firstID + block * (N + gap);
297 nSign = 0;
298 // if (GetVerboseLevel() >= REST_Debug) N = 1;
299
300 for (Int_t signal = 0; signal < N; signal++) {
301 sigID = firstInBlock + signal;
302 eventToProcess.GetSignalById(sigID)->CalculateBaseLine(20, 500);
303 if (eventToProcess.GetSignalById(sigID)->GetBaseLineSigma() >= 3.3) {
304 // debug << "Baseline1: " <<
305 // eventToProcess.GetSignalById(sigID)->GetBaseLineSigma() <<
306 // endl;
307 fOutputEvent->AddSignal(*eventToProcess.GetSignalById(sigID));
308 nSign++;
309 }
310 }
311
312 Int_t nBins = eventToProcess.GetSignal(0)->GetNumberOfPoints();
313 vector<Double_t> signalValues(nSign, 0.0);
314
315 // debug << "nSign: " << nSign << endl;
316
317 for (Int_t bin = 0; bin < nBins; bin++) {
318 int i = 0;
319 for (Int_t signal = 0; signal < N; signal++) {
320 sigID = firstInBlock + signal;
321 if (eventToProcess.GetSignalById(sigID)->GetBaseLineSigma() >= 3.3) {
322 // debug << "Baseline2: " <<
323 // eventToProcess.GetSignalById(sigID)->GetBaseLineSigma() <<
324 // endl;
325 // debug << fOutputEvent->GetSignalById(sigID)->GetRawData(bin) <<
326 // endl;
327 signalValues[i] = fOutputEvent->GetSignalById(sigID)->GetRawData(bin);
328 i++;
329 }
330 }
331
332 std::sort(signalValues.begin(), signalValues.end());
333
334 // Sorting the different methods
335 Int_t begin = 0, middle = 0, end = 0;
336 middle = (Int_t)nSign / 2;
337 Double_t norm = 1.0;
338
339 if (fMode == 0) {
340 // We take only the middle one
341 begin = (Int_t)((double_t)nSign / 2.0);
342 end = begin;
343 norm = 1.;
344 } else if (fMode == 1) {
345 // We take the average of the TRestDetectorSignals at the Center
346 begin = middle - (Int_t)(nSign * fCenterWidth * 0.01);
347 end = middle + (Int_t)(nSign * fCenterWidth * 0.01);
348 norm = (Double_t)end - begin;
349 }
350
351 // Calculation of the correction to be made to each TRestRawSignal
352 Double_t binCorrection = 0.0;
353 for (Int_t i = begin; i <= end; i++) {
354 binCorrection += signalValues[i];
355 }
356
357 binCorrection = binCorrection / norm;
358
359 // Correction applied.
360 for (Int_t signal = 0; signal < N; signal++) {
361 if (eventToProcess.GetSignalById(firstInBlock + signal)->GetBaseLineSigma() >= 3.3) {
362 fOutputEvent->GetSignalById(firstInBlock + signal)
363 ->IncreaseBinBy(bin, Baseline - binCorrection);
364 }
365 }
366 }
367 for (int signal = 0; signal < N; signal++) {
368 if (eventToProcess.GetSignalById(firstInBlock + signal)->GetBaseLineSigma() < 3.3) {
369 fOutputEvent->AddSignal(*eventToProcess.GetSignalById(firstInBlock + signal));
370 }
371 }
372 }
373 return fOutputEvent;
374 }
375 return nullptr;
376}
377
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
A base class for any REST event.
Definition: TRestEvent.h:38
virtual void InitializeReferences(TRestRun *run)
Initialize dynamical references when loading the event from a root file.
Definition: TRestEvent.cxx:175
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
A process to subtract the common channels noise from RawSignal type.
void Initialize() override
Function to initialize input/output event members and define the section name.
Int_t fBlocks
Common noise to all signals or by groups (It can be 0 or 1).
TRestRawSignalEvent * fInputEvent
A pointer to the specific TRestRawSignalEvent input.
Int_t fMinSignalsRequired
Minimum number of signals required to apply the process.
TRestRawSignalEvent * fOutputEvent
A pointer to the specific TRestRawSignalEvent output.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void EndProcess() override
Function to include required actions after all events have been processed. This method will write the...
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void InitProcess() override
Process initialization.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
Int_t fMode
The mode defines the method to be used (It can be 0 or 1).
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 CalculateBaseLine(Int_t startBin, Int_t endBin, const std::string &option="")
This method calculates the average and fluctuation of the baseline in the specified range and writes ...
void IncreaseBinBy(Int_t bin, Double_t data)
It adds the content of data to fSignalData[bin].
Double_t GetRawData(Int_t n) const
It returns the original data value of point n without baseline correction.
Int_t GetSignalID() const
Returns the value of signal ID.