REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRawSignalConvolutionFittingProcess.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 
110 
111 #include "TRestRawSignalConvolutionFittingProcess.h"
112 
113 using namespace std;
114 
116 
121 
135  Initialize();
136 
137  if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
138 }
139 
144 
149 
155  SetSectionName(this->ClassName());
156  SetLibraryVersion(LIBRARY_VERSION);
157 
158  fRawSignalEvent = nullptr;
159 }
160 
173 void TRestRawSignalConvolutionFittingProcess::LoadConfig(const string& configFilename, const string& name) {
174  if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
175 }
176 
181  // fSignalFittingObservables = TRestEventProcess::ReadObservables();
182 }
183 
188  // no need for verbose copy now
189  fRawSignalEvent = (TRestRawSignalEvent*)inputEvent;
190 
191  RESTDebug << "TRestRawSignalConvolutionFittingProcess::ProcessEvent. Event ID : "
192  << fRawSignalEvent->GetID() << RESTendl;
193 
194  Double_t SigmaMean = 0;
195  vector<Double_t> Sigma(fRawSignalEvent->GetNumberOfSignals());
196  Double_t RatioSigmaMaxPeakMean = 0;
197  vector<Double_t> RatioSigmaMaxPeak(fRawSignalEvent->GetNumberOfSignals());
198  Double_t ChiSquareMean = 0;
199  vector<Double_t> ChiSquare(fRawSignalEvent->GetNumberOfSignals());
200 
201  map<int, Double_t> amplitudeFit;
202  map<int, Double_t> shapingtimeFit;
203  map<int, Double_t> peakpositionFit;
204  map<int, Double_t> variancegaussFit;
205 
206  amplitudeFit.clear();
207  shapingtimeFit.clear();
208  peakpositionFit.clear();
209  variancegaussFit.clear();
210 
211  int MinBinRange = 25;
212  int MaxBinRange = 45;
213 
214  for (int s = 0; s < fRawSignalEvent->GetNumberOfSignals(); s++) {
215  TRestRawSignal* singleSignal = fRawSignalEvent->GetSignal(s);
216  singleSignal->CalculateBaseLine(20, 150);
217  int MaxPeakBin = singleSignal->GetMaxPeakBin();
218 
219  // ShaperSin function (AGET theoretical curve) times logistic function
220  TF1* f = new TF1("Aget",
221  "[0]*TMath::Exp(-3. * (x-[2])/[1]) * (x-[2])/[1] "
222  "* (x-[2])/[1] * (x-[2])/[1] * "
223  " sin((x-[2])/[1])/(1+TMath::Exp(-10000*(x-[2])))",
224  0, 511);
225  f->SetParameters(0., 300., 42., 20.); // Initial values adjusted from Desmos
226  f->SetParNames("Amplitude", "ShapingTime", "PeakPosition");
227 
228  // Gaussian pulse
229  TF1* g = new TF1("pulse", "exp(-0.5*((x)/[0])*((x)/[0]))", 0, 511);
230  g->SetParameters(0, 7.);
231 
232  // Convolution of AGET and gaussian functions
233  TF1Convolution* conv = new TF1Convolution("Aget", "pulse", 0, 511, true);
234  conv->SetRange(0, 511);
235  conv->SetNofPointsFFT(10000);
236 
237  TF1* fit_conv = new TF1("fit", *conv, 0, 511, conv->GetNpar());
238  fit_conv->SetParNames("Amplitude", "ShapingTime", "PeakPosition", "VarianceGauss");
239 
240  // Create histogram from signal
241  Int_t nBins = singleSignal->GetNumberOfPoints();
242  TH1D* h = new TH1D("histo", "Signal to histo", nBins, 0, nBins);
243 
244  for (int i = 0; i < nBins; i++) {
245  h->Fill(i, singleSignal->GetRawData(i) - singleSignal->GetBaseLine());
246  h->SetBinError(i, singleSignal->GetBaseLineSigma());
247  }
248 
249  // Fit histogram with convolution
250  fit_conv->SetParameters(singleSignal->GetData(MaxPeakBin), 25., MaxPeakBin - 25., 8.);
251  fit_conv->FixParameter(0, singleSignal->GetData(MaxPeakBin));
252 
253  h->Fit(fit_conv, "LRNQ", "", MaxPeakBin - MinBinRange, MaxPeakBin + MaxBinRange);
254  // Options: L->Likelihood minimization, R->fit in range, N->No draw,
255  // Q->Quiet
256 
257  Double_t sigma = 0;
258  for (int j = MaxPeakBin - MinBinRange; j < MaxPeakBin + MaxBinRange; j++) {
259  sigma += (singleSignal->GetData(j) - fit_conv->Eval(j)) *
260  (singleSignal->GetData(j) - fit_conv->Eval(j));
261  }
262  Sigma[s] = TMath::Sqrt(sigma / (MinBinRange + MaxBinRange));
263  RatioSigmaMaxPeak[s] = Sigma[s] / singleSignal->GetData(MaxPeakBin);
264  RatioSigmaMaxPeakMean += RatioSigmaMaxPeak[s];
265  SigmaMean += Sigma[s];
266  ChiSquare[s] = fit_conv->GetChisquare();
267  ChiSquareMean += ChiSquare[s];
268 
269  amplitudeFit[singleSignal->GetID()] = fit_conv->GetParameter(0);
270  shapingtimeFit[singleSignal->GetID()] = fit_conv->GetParameter(1);
271  peakpositionFit[singleSignal->GetID()] = fit_conv->GetParameter(2);
272  variancegaussFit[singleSignal->GetID()] = fit_conv->GetParameter(3);
273 
274  h->Delete();
275  }
276 
278  SetObservableValue("FitAmplitude_map", amplitudeFit);
279  SetObservableValue("FitShapingTime_map", shapingtimeFit);
280  SetObservableValue("FitPeakPosition_map", peakpositionFit);
281  SetObservableValue("FitVarianceGauss_map", variancegaussFit);
282 
284  SigmaMean = SigmaMean / (fRawSignalEvent->GetNumberOfSignals());
285  SetObservableValue("FitSigmaMean", SigmaMean);
286 
288  Double_t sigmaMeanStdDev = 0;
289  for (int k = 0; k < fRawSignalEvent->GetNumberOfSignals(); k++) {
290  sigmaMeanStdDev += (Sigma[k] - SigmaMean) * (Sigma[k] - SigmaMean);
291  }
292  Double_t SigmaMeanStdDev = TMath::Sqrt(sigmaMeanStdDev / fRawSignalEvent->GetNumberOfSignals());
293  SetObservableValue("FitSigmaStdDev", SigmaMeanStdDev);
294 
296  ChiSquareMean = ChiSquareMean / fRawSignalEvent->GetNumberOfSignals();
297  SetObservableValue("FitChiSquareMean", ChiSquareMean);
298 
300  RatioSigmaMaxPeakMean = RatioSigmaMaxPeakMean / fRawSignalEvent->GetNumberOfSignals();
301  SetObservableValue("FitRatioSigmaMaxPeakMean", RatioSigmaMaxPeakMean);
302 
303  RESTDebug << "SigmaMean: " << SigmaMean << RESTendl;
304  RESTDebug << "SigmaMeanStdDev: " << SigmaMeanStdDev << RESTendl;
305  RESTDebug << "ChiSquareMean: " << ChiSquareMean << RESTendl;
306  RESTDebug << "RatioSigmaMaxPeakMean: " << RatioSigmaMaxPeakMean << RESTendl;
307  for (int k = 0; k < fRawSignalEvent->GetNumberOfSignals(); k++) {
308  RESTDebug << "Standard deviation of signal number " << k << ": " << Sigma[k] << RESTendl;
309  RESTDebug << "Chi square of fit signal number " << k << ": " << ChiSquare[k] << RESTendl;
310  RESTDebug << "Sandard deviation divided by amplitude of signal number " << k << ": "
311  << RatioSigmaMaxPeak[k] << RESTendl;
312  }
313 
316  // This will affect the calculation of observables, but not the stored
317  // TRestRawSignal data.
318  // fRawSignalEvent->SetBaseLineRange(fBaseLineRange);
319  // fRawSignalEvent->SetRange(fIntegralRange);
320 
321  /* Perhaps we want to identify the points over threshold where to apply the
322  fitting?
323  * Then, we might need to initialize points over threshold
324  *
325  for (int s = 0; s < fRawSignalEvent->GetNumberOfSignals(); s++) {
326  TRestRawSignal* sgnl = fRawSignalEvent->GetSignal(s);
327 
329  TRestRawSignal
330  sgnl->InitializePointsOverThreshold(TVector2(fPointThreshold,
331  fSignalThreshold),
332  fNPointsOverThreshold);
333 
334  }
335  */
336 
337  // If cut condition matches the event will be not registered.
338  if (ApplyCut()) return nullptr;
339 
340  return fRawSignalEvent;
341 }
342 
348  // Function to be executed once at the end of the process
349  // (after all events have been processed)
350 
351  // Start by calling the EndProcess function of the abstract class.
352  // Comment this if you don't want it.
353  // TRestEventProcess::EndProcess();
354 }
355 
360  /* Parameters to initialize from RML
361  fBaseLineRange = StringTo2DVector(GetParameter("baseLineRange", "(5,55)"));
362  fIntegralRange = StringTo2DVector(GetParameter("integralRange", "(10,500)"));
363  fPointThreshold = StringToDouble(GetParameter("pointThreshold", "2"));
364  fNPointsOverThreshold = StringToInteger(GetParameter("pointsOverThreshold",
365  "5"));
366  fSignalThreshold = StringToDouble(GetParameter("signalThreshold", "5"));
367  */
368 }
A base class for any REST event.
Definition: TRestEvent.h:38
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void EndProcess() override
Function to include required actions after all events have been processed. This method will write the...
void Initialize() override
Function to initialize input/output event members and define the section name.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
void InitFromConfigFile() override
Function to read input parameters.
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.
Double_t GetBaseLineSigma() const
Int_t GetID() const
Returns the value of signal ID.
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 ...
Double_t GetBaseLine() const
Double_t GetRawData(Int_t n) const
It returns the original data value of point n without baseline correction.
Double_t GetData(Int_t n) const
It returns the data value of point n including baseline correction.
Int_t GetMaxPeakBin()
It returns the bin at which the maximum peak amplitude happens.
Int_t GetNumberOfPoints() const
Returns the actual number of points, or size of the signal.