LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RiseTimeGaussFit_tool.cc
Go to the documentation of this file.
1 
13 #include "fhiclcpp/types/Atom.h"
15 
16 #include "RiseTimeCalculatorBase.h"
17 
18 // ROOT includes
19 #include "TF1.h"
20 #include "TH1F.h"
21 
22 namespace pmtana {
23 
25 
26  public:
27  //Configuration parameters
28  struct Config {
29 
34  };
35 
36  // Default constructor
37  explicit RiseTimeGaussFit(art::ToolConfigTable<Config> const& config);
38 
39  // Method to calculate the OpFlash t0
40  double RiseTime(const pmtana::Waveform_t& wf_pulse,
41  const pmtana::PedestalMean_t& ped_pulse,
42  bool _positive) const override;
43  // Method to fit the first local max of the wvf above fixed threshold
44  std::size_t findFirstMax(const std::vector<double>& arr, double threshold) const;
45 
46  private:
47  double fMinAmp;
48  double fInitSigma;
49  double fTolerance;
50  int fNbins;
51  };
52 
54  : fMinAmp{config().MinAmp()}
55  , fInitSigma{config().InitSigma()}
56  , fTolerance{config().Tolerance()}
57  , fNbins{config().Nbins()}
58  {}
59 
61  const pmtana::PedestalMean_t& ped_pulse,
62  bool _positive) const
63  {
64 
65  // Pedestal-subtracted pulse
66  std::vector<double> wf_aux(ped_pulse);
67  if (_positive) {
68  for (size_t ix = 0; ix < wf_aux.size(); ix++) {
69  wf_aux[ix] = ((double)wf_pulse[ix]) - wf_aux[ix];
70  }
71  }
72  else {
73  for (size_t ix = 0; ix < wf_aux.size(); ix++) {
74  wf_aux[ix] = wf_aux[ix] - ((double)wf_pulse[ix]);
75  }
76  }
77 
78  // Find first local maximum
79  size_t first_max = findFirstMax(wf_aux, fMinAmp);
80 
81  // Create & fill th1
82  TH1F* h_aux = new TH1F("aux", "aux", wf_aux.size(), -0.5, wf_aux.size() - 0.5);
83  for (long unsigned int j = 0; j < wf_aux.size(); j++)
84  h_aux->SetBinContent(j + 1, wf_aux[j]);
85 
86  // Function & initial values for the fit, ROOT Gaussian: [p0]*e**(-0.5 (x-[p1])**2 / [p2]**2)
87  TF1* f = new TF1("f", "gaus", double(first_max) - fNbins, double(first_max) + fNbins);
88  f->SetParameters(wf_aux[first_max], first_max, fInitSigma);
89  // Fit
90  h_aux->Fit(f, "q", "SAME", first_max - fNbins, first_max + fNbins);
91  double t_fit = f->GetParameter(1);
92  double peak_time;
93  if (
94  std::abs(t_fit - first_max) <
95  fTolerance) { //check fit is close in distance to the original max, use max peak as time otherwise
96  peak_time = t_fit;
97  }
98  else {
99  peak_time = first_max;
100  mf::LogInfo("RiseTimeGaussFit") << "No good fit found, keeping 1st max bin instead";
101  }
102 
103  return peak_time;
104  }
105 
106  std::size_t RiseTimeGaussFit::findFirstMax(const std::vector<double>& arr, double threshold) const
107  {
111  double max = arr[0];
112  for (std::size_t i = 1, n = arr.size(); i < n; ++i) {
113  if (arr[i] >= max)
114  max = arr[i];
115 
116  else if (max < threshold) //didn't pass the threshold, keep searching
117  max = arr[i];
118 
119  else
120  return i - 1;
121  }
122 
123  //No local maxima found.
124  mf::LogInfo("RiseTimeGaussFit") << "No local max found above fixed threshold: " << threshold;
125  return 0;
126  }
127 }
128 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
Interfacce class for a tool to calculate the pulse rise time.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
constexpr auto abs(T v)
Returns the absolute value of the argument.
std::size_t findFirstMax(const std::vector< double > &arr, double threshold) const
TFile f
Definition: plotHisto.C:6
std::vector< short > Waveform_t
double RiseTime(const pmtana::Waveform_t &wf_pulse, const pmtana::PedestalMean_t &ped_pulse, bool _positive) const override
Char_t n[5]
RiseTimeGaussFit(art::ToolConfigTable< Config > const &config)
std::vector< double > PedestalMean_t