Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFitToT.hh
Go to the documentation of this file.
1 #ifndef __JCALIBRATE__JFITTOT__
2 #define __JCALIBRATE__JFITTOT__
3 
4 #include <limits>
5 
6 #include "TMath.h"
7 #include "TString.h"
8 #include "TF1.h"
9 #include "TH1.h"
10 #include "TFitResult.h"
11 
12 #include "JTools/JRange.hh"
13 
16 
17 #include "JROOT/JRootToolkit.hh"
18 
19 
20 /**
21  * \author mkarel, bjung
22  */
23 
24 namespace JCALIBRATE {}
25 namespace JPP { using namespace JCALIBRATE; }
26 
27 namespace JCALIBRATE {
28 
29  using JTOOLS::JRange;
33 
35 
36  static const double FITTOT_GAIN_MIN = 0.25; //!< Minimal gain
37  static const double FITTOT_GAIN_MAX = 2.00; //!< Maximal gain
38 
39  static const double FITTOT_GAINSPREAD_MIN = 0.05; //!< Minimal gain spread
40  static const double FITTOT_GAINSPREAD_MAX = 1.00; //!< Maximal gain spread
41 
42  static const double FITTOT_TOT_LMARGIN = 15.00; //!< Fit-region left of peak [ns]
43  static const double FITTOT_TOT_RMARGIN = 10.00; //!< Fit-region right of peak [ns]
44 
45  static const std::string FITTOT_FNAME = "fittot";
46 
47  /**
48  * Fit parameters for two-fold coincidence rate due to K40.
49  */
51  /**
52  * Constructor.
53  *
54  * \param parameters PMT parameters
55  */
57  gain (parameters.gain),
58  gainSpread (parameters.gainSpread)
59  {}
60 
61 
62  /**
63  * Copy constructor.
64  *
65  * \param data data
66  */
67  JFitToTParameters(const Double_t* data) :
68  gain (0.0),
69  gainSpread (0.0)
70  {
71  if (data != NULL) {
72  setModelParameters(data);
73  }
74  }
75 
76 
77  /**
78  * Get number of model parameters.
79  *
80  * \return number of parameters
81  */
83  {
84  return sizeof(JFitToTParameters) / sizeof(Double_t);
85  }
86 
87 
88  /**
89  * Get model parameters.
90  *
91  * \return pointer to parameters
92  */
93  const Double_t* getModelParameters() const
94  {
95  return &this->gain;
96  }
97 
98 
99  /**
100  * Get model parameters.
101  *
102  * \return pointer to parameters
103  */
104  Double_t* getModelParameters()
105  {
106  return &this->gain;
107  }
108 
109 
110  /**
111  * Set model parameter.
112  *
113  * \param i parameter index
114  * \param value parameter value
115  */
116  void setModelParameter(const int i, const Double_t value)
117  {
118  getModelParameters()[i] = value;
119  }
120 
121 
122  /**
123  * Set model parameters.
124  *
125  * \param data pointer to parameters
126  */
127  void setModelParameters(const Double_t* data)
128  {
129  for (Int_t i = 0; i != getNumberOfModelParameters(); ++i) {
130  setModelParameter(i, data[i]);
131  }
132  }
133 
134 
135  /**
136  * Get model parameter.
137  *
138  * \param i parameter index
139  * \return parameter value
140  */
141  const Double_t getModelParameter(const int i) const
142  {
143  return getModelParameters()[i];
144  }
145 
146 
147  /**
148  * Get model parameter.
149  *
150  * \param p pointer to data member
151  * \return parameter index and value
152  */
154  {
155  const Int_t i = &(this->*p) - getModelParameters();
156 
157  return JFitParameter_t(i, getModelParameter(i));
158  }
159 
160 
161  // fit parameters
162 
163  Double_t gain; //!< PMT gain
164  Double_t gainSpread; //!< PMT gain spread
165  };
166 
167 
168  /**
169  * Parametrisation of time-over-threshold distribution.
170  *
171  * Note that for use in ROOT fit operations, the member method JFitToT::getValue is static.\n
172  */
173  struct JFitToT :
174  public JFitToTParameters,
175  public TF1
176  {
177 
178  /**
179  * Constructor.
180  *
181  * \param parameters parameters
182  * \param range abscissa range
183  */
184  JFitToT(const JPMTParameters& parameters, const JRange_t& range) :
185 
186  JFitToTParameters(parameters),
187 
188  TF1 (FITTOT_FNAME.c_str(),
189  this,
190  &JFitToT::getValue,
191  range.getLowerLimit(),
192  range.getUpperLimit(),
194 
195  fitRange (range),
196  WfitRange (1.0),
197 
198  NPE (1)
199  {
200  getInstance().setPMTParameters(parameters);
201 
204  }
205 
206 
207  /**
208  * Fit initializer
209  *
210  * \param h1 ROOT 1D-histogram
211  * \param gradientThreshold treshold gradient for search of maximum
212  */
213  void fitInit(TH1& h1, const double gradientThreshold = -0.005)
214  {
215  using namespace std;
216  using namespace JPP;
217 
218  // Find gain and gainspread initial values
219  const Int_t N = h1.GetXaxis()->GetNbins();
220 
221  double max = h1.GetBinContent(N);
222  double mode = h1.GetBinCenter(N);
223 
224  for (int i = h1.GetBinCenter(N-1);
225  i > h1.GetBinCenter(h1.FindBin(getInstance().mean_ns));
226  --i) {
227 
228  const double x = h1.GetBinCenter (i);
229  const double y = h1.GetBinContent(i);
230 
231  const double gradient = ( (h1.GetBinContent(i-1) - h1.GetBinContent(i+1)) /
232  (h1.GetBinCenter (i+1) - h1.GetBinCenter (i-1)) );
233 
234  if (y > max) {
235 
236  mode = x;
237  max = y;
238 
239  } else if (gradient < gradientThreshold) {
240 
241  break;
242  }
243  }
244 
245  init_gain = getInstance().getNPE(mode);
247  init_gain / 3.0 : FITTOT_GAINSPREAD_MIN ); // Range rule of thumb
248 
249 
250  // Set parameter names and initial values
251  const Int_t Ngain = this->getModelParameter(&JFitToT::gain);
252  const Int_t NgainSpread = this->getModelParameter(&JFitToT::gainSpread);
253 
254  this->SetParName(Ngain, MAKE_CSTRING("gain"));
255  this->SetParName(NgainSpread, MAKE_CSTRING("gainSpread"));
256 
259 
261  setModelParameter(NgainSpread, init_gainSpread);
262 
263  this->SetParameters(this->getModelParameters());
264 
265 
266  // Set parameter ranges
269 
270 
271  // Set fit range and compute corresponding histogram weight
272  if (!fitRange.is_valid()) {
274  mode + FITTOT_TOT_RMARGIN);
275  }
276 
277  fitRange.setRange(std::max(h1.GetXaxis()->GetXmin(), fitRange.getLowerLimit()),
278  std::min(h1.GetXaxis()->GetXmax(), fitRange.getUpperLimit()));
279 
280  WfitRange = h1.Integral(h1.FindBin(fitRange.getLowerLimit()),
281  h1.FindBin(fitRange.getUpperLimit()));
282 
283  this->SetRange(fitRange.getLowerLimit(), fitRange.getUpperLimit());
284  }
285 
286 
287  /**
288  * Fit histogram.
289  *
290  * Note that the PMT parameters which are part of the model are reset before the fit according the status of each PMT and
291  * the obtained fit parameters are copied back to the model parameters after the fit.
292  *
293  * \param h1 ROOT 1D-histogram
294  * \param minWeight minimum histogram weight in fit-range
295  * \param option fit option
296  */
297  TFitResultPtr operator()(TH1& h1, const double minWeight, const std::string& option)
298  {
299  using namespace std;
300  using namespace JPP;
301 
302  fitInit(h1);
303 
304  if (WfitRange > minWeight &&
306 
307  const TFitResultPtr result = h1.Fit(this, option.c_str());
308 
309  this->setModelParameters(this->GetParameters());
310 
311  const double ToTcheck = getInstance().getTimeOverThreshold(gain);
312 
313  return (fitRange(ToTcheck) ? result : TFitResultPtr());
314 
315  } else {
316 
317  return TFitResultPtr();
318  }
319  }
320 
321 
322  /**
323  * Get rate as a function of the fit parameters.
324  *
325  * \param x pointer to abscissa values
326  * \param par pointer to parameter values
327  * \return rate distribution at specified time-over-threshold [Hz/ns]
328  */
329  Double_t getValue(Double_t* x, Double_t* par)
330  {
331  const Double_t tot_ns = x[0];
332 
333  getInstance().gain = par[0];
334  getInstance().gainSpread = par[1];
335 
336  // Compute total weight (= histogram weight within fit range
337  // plus pdf weight outside fit range)
338  const double Wtot = (WfitRange +
341  NPE));
342 
343  return Wtot * getInstance().getTimeOverThresholdProbability(tot_ns, NPE);
344  }
345 
346 
347  /**
348  * Retrieve initial gain
349  *
350  *\return initial gain setting
351  */
352  const double getInitGain() const {
353  return init_gain;
354  }
355 
356 
357  /**
358  * Retrieve initial gainspread
359  *
360  *\return initial gainspread setting
361  */
362  const double getInitGainSpread() const {
363  return init_gainSpread;
364  }
365 
366 
367  private:
368 
369  /**
370  * Get unique instance of PMT analogue signal processor.
371  *
372  * \return reference to PMT analogue signal processor.
373  */
375  {
376  static JPMTAnalogueSignalProcessor object;
377 
378  return object;
379  }
380 
381  JRange_t fitRange; //!< fit-range [ns]
382  double WfitRange; //!< Cumulative weight within fit-range
383 
384  double init_gain; //!< Initial input value for gain
385  double init_gainSpread; //!< Initial input value for gainSpread
386 
387  const int NPE; //!< True number of photo-electrons
388  };
389 }
390 
391 #endif
static Int_t getNumberOfModelParameters()
Get number of model parameters.
Definition: JFitToT.hh:82
double gain
gain [unit]
JFitParameter_t getModelParameter(Double_t JFitToTParameters::*p) const
Get model parameter.
Definition: JFitToT.hh:153
const Double_t getModelParameter(const int i) const
Get model parameter.
Definition: JFitToT.hh:141
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
TFitResultPtr operator()(TH1 &h1, const double minWeight, const std::string &option)
Fit histogram.
Definition: JFitToT.hh:297
const double getInitGainSpread() const
Retrieve initial gainspread.
Definition: JFitToT.hh:362
double gainSpread
gain spread [unit]
static JPMTAnalogueSignalProcessor & getInstance()
Get unique instance of PMT analogue signal processor.
Definition: JFitToT.hh:374
JFitToTParameters(const Double_t *data)
Copy constructor.
Definition: JFitToT.hh:67
static const double FITTOT_GAIN_MAX
Maximal gain.
Definition: JFitToT.hh:37
Double_t gainSpread
PMT gain spread.
Definition: JFitToT.hh:164
static const double FITTOT_GAIN_MIN
Minimal gain.
Definition: JFitToT.hh:36
void setPMTParameters(const JPMTParameters &parameters)
Set PMT parameters.
Double_t getValue(Double_t *x, Double_t *par)
Get rate as a function of the fit parameters.
Definition: JFitToT.hh:329
virtual double getTimeOverThreshold(const double npe) const
Get time-over-threshold (ToT).
Double_t * getModelParameters()
Get model parameters.
Definition: JFitToT.hh:104
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
Parametrisation of time-over-threshold distribution.
Definition: JFitToT.hh:173
Fit parameters for two-fold coincidence rate due to K40.
Definition: JFitToT.hh:50
void fitInit(TH1 &h1, const double gradientThreshold=-0.005)
Fit initializer.
Definition: JFitToT.hh:213
const Double_t * getModelParameters() const
Get model parameters.
Definition: JFitToT.hh:93
Auxiliary data structure for a parameter index and its value.
JFitToTParameters(const JPMTParameters &parameters)
Constructor.
Definition: JFitToT.hh:56
const double getInitGain() const
Retrieve initial gain.
Definition: JFitToT.hh:352
static const double FITTOT_GAINSPREAD_MAX
Maximal gain spread.
Definition: JFitToT.hh:40
double init_gain
Initial input value for gain.
Definition: JFitToT.hh:384
void setRange(const range_type &range)
Set range.
Definition: JRange.hh:139
return result
Definition: JPolint.hh:695
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
Double_t gain
PMT gain.
Definition: JFitToT.hh:163
Range of values.
Definition: JRange.hh:34
bool setParLimits(TF1 &f1, const Int_t index, Double_t xmin, Double_t xmax)
Set fit parameter limits.
double init_gainSpread
Initial input value for gainSpread.
Definition: JFitToT.hh:385
JRange< Double_t > JRange_t
Definition: JFitToT.hh:34
JFitToT(const JPMTParameters &parameters, const JRange_t &range)
Constructor.
Definition: JFitToT.hh:184
virtual double getNPE(const double tot_ns, const double eps=1.0e-3) const
Get number of photo-electrons.
bool is_valid() const
Check validity of range.
Definition: JRange.hh:324
Auxiliary class to define a range between two values.
void setModelParameters(const Double_t *data)
Set model parameters.
Definition: JFitToT.hh:127
JRange_t fitRange
fit-range [ns]
Definition: JFitToT.hh:381
void setModelParameter(const int i, const Double_t value)
Set model parameter.
Definition: JFitToT.hh:116
double getIntegralOfTimeOverThresholdProbability(const double Tmin, const double Tmax, const int NPE) const
Get cumulative probability of time-over-threshold distribution.
double getTimeOverThresholdProbability(const double tot_ns, const int NPE) const
Get probability of having a pulse with specific time-over-threshold.
PMT analogue signal processor.
const int NPE
True number of photo-electrons.
Definition: JFitToT.hh:387
static const double FITTOT_GAINSPREAD_MIN
Minimal gain spread.
Definition: JFitToT.hh:39
double WfitRange
Cumulative weight within fit-range.
Definition: JFitToT.hh:382
Data structure for PMT parameters.
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
static const double FITTOT_TOT_RMARGIN
Fit-region right of peak [ns].
Definition: JFitToT.hh:43
static const std::string FITTOT_FNAME
Definition: JFitToT.hh:45
static const double FITTOT_TOT_LMARGIN
Fit-region left of peak [ns].
Definition: JFitToT.hh:42