REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
src/TRestRawSignalShapingProcess.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 
83 #include "TRestRawSignalShapingProcess.h"
84 
85 using namespace std;
86 
87 #include <TFile.h>
88 #include <TMath.h>
89 
91 
96 
110  Initialize();
111  LoadConfigFromFile(configFilename);
112 }
113 
118 
124  SetSectionName(this->ClassName());
125  SetLibraryVersion(LIBRARY_VERSION);
126 
127  fInputSignalEvent = nullptr;
128  fOutputSignalEvent = new TRestRawSignalEvent();
129 }
130 
143 void TRestRawSignalShapingProcess::LoadConfig(const string& configFilename, const string& name) {
144  LoadConfigFromFile(configFilename, name);
145 }
146 
153  /*
154  * NOT IMPLEMENTED. TODO To use a generic response from a
155  * predefined TRestDetectorSignal
156  *
157  * For the moment we do only a gaussian shaping"
158  * /
159 
160  responseSignal = new TRestRawSignal();
161 
162  if( fShapingType == "responseFile" )
163  {
164  TString fullPathName = (TString) getenv("REST_PATH") + "/data/signal/" +
165  fResponseFilename; TFile *f = new TFile(fullPathName); responseSignal =
166  (TRestRawSignal *) f->Get("signal Response"); f->Close();
167  }
168 
169  if( GetVerboseLevel() >= REST_Debug )
170  {
171  CreateCanvas();
172  fCanvas->Draw();
173  }
174 
175  if( fShapingType == "gaus" )
176  {
177  responseSignal->InitGaussian( 100, 100, 30, 200 );
178 
179  if( GetVerboseLevel() >= REST_Debug )
180  {
181  responseSignal->GetGraph()->Draw();
182  fCanvas->Update();
183  GetChar();
184  }
185  }
186  */
187 }
188 
193  fInputSignalEvent = (TRestRawSignalEvent*)inputEvent;
194 
195  if (fInputSignalEvent->GetNumberOfSignals() <= 0) {
196  return nullptr;
197  }
198 
199  std::vector<double> response;
200  Int_t Nr = 0;
201 
204  if (fShapingType == "gaus") {
205  Int_t cBin = (Int_t)(fShapingTime * 3.5);
206  Nr = 2 * cBin;
207  Double_t sigma = fShapingTime;
208 
209  response.resize(Nr);
210 
211  for (int i = 0; i < Nr; i++) {
212  response[i] = TMath::Exp(-0.5 * (i - cBin) * (i - cBin) / sigma / sigma);
213  response[i] = response[i] / TMath::Sqrt(2 * M_PI) / sigma;
214  }
215  } else if (fShapingType == "exponential") {
216  Nr = (Int_t)(5 * fShapingTime);
217 
218  response.resize(Nr);
219 
220  for (int i = 0; i < Nr; i++) {
221  Double_t coeff = ((Double_t)i) / fShapingTime;
222  response[i] = TMath::Exp(-coeff);
223  }
224  } else if (fShapingType == "shaper") {
225  Nr = (Int_t)(5 * fShapingTime);
226 
227  response.resize(Nr);
228 
229  for (int i = 0; i < Nr; i++) {
230  Double_t coeff = ((Double_t)i) / fShapingTime;
231  response[i] = TMath::Exp(-3. * coeff) * coeff * coeff * coeff;
232  }
233  } else if (fShapingType == "shaperSin") {
234  Nr = (Int_t)(5 * fShapingTime);
235 
236  response.resize(Nr);
237 
238  for (int i = 0; i < Nr; i++) {
239  Double_t coeff = ((Double_t)i) / fShapingTime;
240  response[i] = TMath::Exp(-3. * coeff) * coeff * coeff * coeff * sin(coeff);
241  }
242  } else {
243  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning)
244  cout << "REST WARNING. Shaping type : " << fShapingType << " is not defined!!" << endl;
245  return nullptr;
246  }
247 
248  // Making sure that response integral is 1, and applying the gain
249  Double_t sum = 0;
250  for (int n = 0; n < Nr; n++) sum += response[n];
251  for (int n = 0; n < Nr; n++) response[n] = response[n] * fShapingGain / sum;
252 
253  for (int n = 0; n < fInputSignalEvent->GetNumberOfSignals(); n++) {
254  TRestRawSignal shapingSignal = TRestRawSignal();
255  TRestRawSignal inSignal = *fInputSignalEvent->GetSignal(n);
256  Int_t nBins = inSignal.GetNumberOfPoints();
257 
258  vector<double> out(nBins);
259  for (int m = 0; m < nBins; m++) out[m] = 0;
260 
261  for (int m = 0; m < nBins; m++) {
262  if (inSignal.GetData(m) >= 0) {
263  if (fShapingType == "gaus") {
264  for (int n = -Nr / 2; m + n < nBins && n < Nr / 2; n++)
265  if (m + n >= 0) out[m + n] += response[n + Nr / 2] * inSignal.GetData(m);
266  } else
267  for (int n = 0; m + n < nBins && n < Nr; n++)
268  out[m + n] += response[n] * inSignal.GetData(m);
269  }
270  }
271 
272  for (int i = 0; i < nBins; i++) {
273  shapingSignal.AddPoint((Short_t)round(out[i]));
274  }
275  shapingSignal.SetSignalID(inSignal.GetSignalID());
276 
277  fOutputSignalEvent->AddSignal(shapingSignal);
278  }
279 
280  return fOutputSignalEvent;
281 }
282 
288  // Function to be executed once at the end of the process
289  // (after all events have been processed)
290 
291  // Start by calling the EndProcess function of the abstract class.
292  // Comment this if you don't want it.
293  // TRestEventProcess::EndProcess();
294 }
A base class for any REST event.
Definition: TRestEvent.h:38
An event container for time rawdata signals with fixed length.
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.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
void InitProcess() override
Process initialization. Observable names are interpreted and auxiliar observable members,...
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.