npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
MinuitDensityFitFcn1D.hh
Go to the documentation of this file.
1 #ifndef NPSI_MINUITDENSITYFITFCN1D_HH_
2 #define NPSI_MINUITDENSITYFITFCN1D_HH_
3 
4 /*!
5 // \file MinuitDensityFitFcn1D.hh
6 //
7 // \brief Binned fit of a parametric density by maximum likelihood
8 //
9 // Author: I. Volobouev
10 //
11 // September 2010
12 */
13 
14 #include <cfloat>
15 
16 #include "npstat/stat/HistoND.hh"
17 #include "npstat/stat/FitUtils.hh"
18 
19 #include "Minuit2/FCNBase.h"
20 
21 namespace npsi {
22  /**
23  // Target minimization function adapter class for running maximum
24  // likelihood density fits to histogrammed data by Minuit2.
25  //
26  // DensityConstructor is a functor which creates the necessary
27  // density function on the stack out of a vector of parameters.
28  // Must have "operator()(const std::vector<double>&) const"
29  // which returns an object (or a reference) of some class which
30  // was derived from AbsDistribution1D.
31  */
32  template
33  <
34  class Numeric,
35  class DensityConstructor,
36  class Axis = npstat::HistoAxis
37  >
38  class MinuitDensityFitFcn1D : public ROOT::Minuit2::FCNBase
39  {
40  public:
41  /**
42  // This class will not assume ownership of any pointers or references.
43  //
44  // Elements of "binMask" should be set to 1 for bins used in the fit
45  // and to 0 for bins that are to be ignored.
46  //
47  // "nQuad" is the number of quadrature points to use for calculating
48  // the density integral inside each bin (should be supported by
49  // GaussLegendreQuadrature class). If this parameter is set to 0,
50  // cumulative density function will be used.
51  */
53  const unsigned char* binMask,
54  const unsigned maskLength,
55  const DensityConstructor& densityMaker,
56  const double minlog=log(DBL_MIN),
57  const double up=0.05,
58  const unsigned nQuad=6U)
59  : histo_(histo),
60  densityMaker_(densityMaker),
61  binMask_(binMask),
62  workspace_(histo_.nBins()),
63  minlog_(minlog),
64  up_(up),
65  densityArea_(0.0),
66  enabledBinCount_(0.0),
67  nquad_(nQuad)
68  {
69  if (histo_.dim() != 1U) throw std::invalid_argument(
70  "In npsi::MinuitDensityFitFcn1D constructor : "
71  "only 1-d histograms can be used with this class");
72 
73  assert(binMask);
74 
75  if (histo_.nBins() != maskLength) throw std::invalid_argument(
76  "In npsi::MinuitDensityFitFcn1D constructor : "
77  "bin mask is not compatible with histogram binning");
78  }
79 
80  inline virtual ~MinuitDensityFitFcn1D() {}
81 
82  /** This method returns negative log likelihood */
83  inline virtual double operator()(const std::vector<double>& x) const
84  {
86  histo_, binMask_, histo_.nBins(), densityMaker_(x),
87  minlog_, &workspace_[0], workspace_.size(),
88  nquad_, &densityArea_, &enabledBinCount_);
89  }
90 
91  inline double Up() const {return up_;}
92 
93  /** Area of the density inside enabled bins */
94  inline double enabledArea() const {return densityArea_;}
95 
96  /** Event count for enabled bins */
97  inline double enabledBinCount() const {return enabledBinCount_;}
98 
99  private:
100  const npstat::HistoND<Numeric,Axis>& histo_;
101  const DensityConstructor& densityMaker_;
102  const unsigned char* binMask_;
103  mutable std::vector<double> workspace_;
104  double minlog_;
105  double up_;
106  mutable double densityArea_;
107  mutable double enabledBinCount_;
108  unsigned nquad_;
109  };
110 }
111 
112 #endif // NPSI_MINUITDENSITYFITFCN1D_HH_
Utility functions which facilitate fitting of parametric distributions.
Arbitrary-dimensional histogram template.
Definition: MinuitDensityFitFcn1D.hh:39
double enabledBinCount() const
Definition: MinuitDensityFitFcn1D.hh:97
virtual double operator()(const std::vector< double > &x) const
Definition: MinuitDensityFitFcn1D.hh:83
MinuitDensityFitFcn1D(const npstat::HistoND< Numeric, Axis > &histo, const unsigned char *binMask, const unsigned maskLength, const DensityConstructor &densityMaker, const double minlog=log(DBL_MIN), const double up=0.05, const unsigned nQuad=6U)
Definition: MinuitDensityFitFcn1D.hh:52
double enabledArea() const
Definition: MinuitDensityFitFcn1D.hh:94
Definition: HistoAxis.hh:31
Definition: HistoND.hh:46
Definition: fitCompositeJohnson.hh:16
double densityFitLogLikelihood1D(const Histo &histo, const unsigned char *binMask, unsigned maskLength, const AbsDistribution1D &density, double minlog, double *workBuffer, unsigned workBufferLength, unsigned nQuad=6U, double *densityArea=0, double *enabledBinCount=0)