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 using namespace std;
89 
90 #include <algorithm>
91 #include <iostream>
92 #include <vector>
93 
95 
100 
114  Initialize();
115 
116  if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
117 }
118 
123  delete fInputEvent;
124  delete fOutputEvent;
125 }
126 
130 void TRestRawCommonNoiseReductionProcess::LoadDefaultConfig() { SetTitle("Default config"); }
131 
137  SetSectionName(this->ClassName());
138  SetLibraryVersion(LIBRARY_VERSION);
139 
140  fInputEvent = nullptr;
141  fOutputEvent = new TRestRawSignalEvent();
142 }
143 
156 void TRestRawCommonNoiseReductionProcess::LoadConfig(const string& configFilename, const string& name) {
157  if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
158 }
159 
164 
169  fInputEvent = (TRestRawSignalEvent*)inputEvent;
170 
171  if (fInputEvent->GetNumberOfSignals() < fMinSignalsRequired) {
172  for (int sgnl = 0; sgnl < fInputEvent->GetNumberOfSignals(); sgnl++) {
173  fOutputEvent->AddSignal(*fInputEvent->GetSignal(sgnl));
174  }
175  return fOutputEvent;
176  }
177 
178  // Event base line determination.
179  Double_t baseLineMean = 0;
180  for (int sgnl = 0; sgnl < fInputEvent->GetNumberOfSignals(); sgnl++) {
181  fInputEvent->GetSignal(sgnl)->CalculateBaseLine(20, 150);
182  Double_t baseline = fInputEvent->GetSignal(sgnl)->GetBaseLine();
183  baseLineMean += baseline;
184  }
185  Double_t Baseline = baseLineMean / fInputEvent->GetNumberOfSignals();
186 
187  if (fBlocks == 0) {
188  Int_t N = fInputEvent->GetNumberOfSignals();
189 
190  // if (GetVerboseLevel() >= REST_Debug) N = 1;
191  for (int sgnl = 0; sgnl < N; sgnl++) {
192  fOutputEvent->AddSignal(*fInputEvent->GetSignal(sgnl));
193  }
194 
195  Int_t nBins = fInputEvent->GetSignal(0)->GetNumberOfPoints();
196  vector<Double_t> sgnlValues(N, 0.0);
197 
198  for (Int_t bin = 0; bin < nBins; bin++) {
199  for (Int_t sgnl = 0; sgnl < N; sgnl++) {
200  sgnlValues[sgnl] = fOutputEvent->GetSignal(sgnl)->GetRawData(bin);
201  }
202 
203  std::sort(sgnlValues.begin(), sgnlValues.end());
204 
205  // Sorting the different methods
206  Int_t begin = 0, middle = 0, end = 0;
207  middle = (Int_t)N / 2;
208  Double_t norm = 1.0;
209 
210  if (fMode == 0) {
211  // We take only the middle one
212  begin = (Int_t)((double_t)N / 2.0);
213  end = begin;
214  norm = 1.;
215  } else if (fMode == 1) {
216  // We take the average of the TRestDetectorSignals at the Center
217  begin = middle - (Int_t)(N * fCenterWidth * 0.01);
218  end = middle + (Int_t)(N * fCenterWidth * 0.01);
219  norm = (Double_t)end - begin;
220  }
221 
222  // Calculation of the correction to be made to each TRestRawSignal
223  Double_t binCorrection = 0.0;
224  for (Int_t i = begin; i <= end; i++) binCorrection += sgnlValues[i];
225 
226  binCorrection = binCorrection / norm;
227 
228  // Correction applied.
229  for (Int_t sgnl = 0; sgnl < N; sgnl++)
230  fOutputEvent->GetSignal(sgnl)->IncreaseBinBy(bin, Baseline - binCorrection);
231  }
232 
233  return fOutputEvent;
234  } else if (fBlocks == 1) {
235  Int_t N = 68;
236  Int_t nBlocks = 8;
237  Int_t firstID = 578;
238  Int_t gap = 4;
239 
240  Int_t firstInBlock;
241  Int_t nSign;
242  Int_t sigID;
243 
244  for (int block = 0; block < nBlocks; block++) {
245  firstInBlock = firstID + block * (N + gap);
246  nSign = 0;
247  // if (GetVerboseLevel() >= REST_Debug) N = 1;
248 
249  for (Int_t sgnl = 0; sgnl < N; sgnl++) {
250  sigID = firstInBlock + sgnl;
251  fInputEvent->GetSignalById(sigID)->CalculateBaseLine(20, 500);
252  if (fInputEvent->GetSignalById(sigID)->GetBaseLineSigma() >= 3.3) {
253  // debug << "Baseline1: " <<
254  // fInputEvent->GetSignalById(sigID)->GetBaseLineSigma() <<
255  // endl;
256  fOutputEvent->AddSignal(*fInputEvent->GetSignalById(sigID));
257  nSign++;
258  }
259  }
260 
261  Int_t nBins = fInputEvent->GetSignal(0)->GetNumberOfPoints();
262  vector<Double_t> sgnlValues(nSign, 0.0);
263 
264  // debug << "nSign: " << nSign << endl;
265 
266  for (Int_t bin = 0; bin < nBins; bin++) {
267  int i = 0;
268  for (Int_t sgnl = 0; sgnl < N; sgnl++) {
269  sigID = firstInBlock + sgnl;
270  if (fInputEvent->GetSignalById(sigID)->GetBaseLineSigma() >= 3.3) {
271  // debug << "Baseline2: " <<
272  // fInputEvent->GetSignalById(sigID)->GetBaseLineSigma() <<
273  // endl;
274  // debug << fOutputEvent->GetSignalById(sigID)->GetRawData(bin) <<
275  // endl;
276  sgnlValues[i] = fOutputEvent->GetSignalById(sigID)->GetRawData(bin);
277  i++;
278  }
279  }
280 
281  std::sort(sgnlValues.begin(), sgnlValues.end());
282 
283  // Sorting the different methods
284  Int_t begin = 0, middle = 0, end = 0;
285  middle = (Int_t)nSign / 2;
286  Double_t norm = 1.0;
287 
288  if (fMode == 0) {
289  // We take only the middle one
290  begin = (Int_t)((double_t)nSign / 2.0);
291  end = begin;
292  norm = 1.;
293  } else if (fMode == 1) {
294  // We take the average of the TRestDetectorSignals at the Center
295  begin = middle - (Int_t)(nSign * fCenterWidth * 0.01);
296  end = middle + (Int_t)(nSign * fCenterWidth * 0.01);
297  norm = (Double_t)end - begin;
298  }
299 
300  // Calculation of the correction to be made to each TRestRawSignal
301  Double_t binCorrection = 0.0;
302  for (Int_t i = begin; i <= end; i++) binCorrection += sgnlValues[i];
303 
304  binCorrection = binCorrection / norm;
305 
306  // Correction applied.
307  for (Int_t sgnl = 0; sgnl < N; sgnl++) {
308  if (fInputEvent->GetSignalById(firstInBlock + sgnl)->GetBaseLineSigma() >= 3.3) {
309  fOutputEvent->GetSignalById(firstInBlock + sgnl)
310  ->IncreaseBinBy(bin, Baseline - binCorrection);
311  }
312  }
313  }
314  for (int sgnl = 0; sgnl < N; sgnl++) {
315  if (fInputEvent->GetSignalById(firstInBlock + sgnl)->GetBaseLineSigma() < 3.3) {
316  fOutputEvent->AddSignal(*fInputEvent->GetSignalById(firstInBlock + sgnl));
317  }
318  }
319  }
320  return fOutputEvent;
321  }
322  return nullptr;
323 }
324 
330  // Function to be executed once at the end of the process
331  // (after all events have been processed)
332 
333  // Start by calling the EndProcess function of the abstract class.
334  // Comment this if you don't want it.
335  // TRestEventProcess::EndProcess();
336 }
A base class for any REST event.
Definition: TRestEvent.h:38
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.
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.
An event container for time rawdata signals with fixed length.