ROOT logo
// @(#)root/mathcore:$Id: PoissonLikelihoodFCN.h 39382 2011-05-26 08:31:43Z moneta $
// Author: L. Moneta Fri Aug 17 14:29:24 2007

/**********************************************************************
 *                                                                    *
 * Copyright (c) 2007  LCG ROOT Math Team, CERN/PH-SFT                *
 *                                                                    *
 *                                                                    *
 **********************************************************************/

// Header file for class PoissonLikelihoodFCN

#ifndef ROOT_Fit_PoissonLikelihoodFCN
#define ROOT_Fit_PoissonLikelihoodFCN

#ifndef ROOT_Math_FitMethodunction
#include "Math/FitMethodFunction.h"
#endif

#ifndef ROOT_Math_IParamFunction
#include "Math/IParamFunction.h"
#endif

#ifndef ROOT_Fit_BinData
#include "Fit/BinData.h"
#endif

#ifndef ROOT_Fit_FitUtil
#include "Fit/FitUtil.h"
#endif

//#define PARALLEL
// #ifdef PARALLEL
// #ifndef ROOT_Fit_FitUtilParallel
// #include "Fit/FitUtilParallel.h"
// #endif
// #endif

namespace ROOT {

   namespace Fit {


//___________________________________________________________________________________
/**
   class evaluating the log likelihood
   for binned Poisson likelihood fits
   it is template to distinguish gradient and non-gradient case

   @ingroup  FitMethodFunc
*/
template<class FunType>
class PoissonLikelihoodFCN : public ::ROOT::Math::BasicFitMethodFunction<FunType>  {

public:


   typedef  ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
   typedef typename  BaseObjFunction::BaseFunction BaseFunction;

   typedef  ::ROOT::Math::IParamMultiFunction IModelFunction;


   /**
      Constructor from unbin data set and model function (pdf)
   */
   PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0) :
      BaseObjFunction(func.NPar(), data.Size() ),
      fData(data),
      fFunc(func),
      fNEffPoints(0),
      fWeight(weight),
      fGrad ( std::vector<double> ( func.NPar() ) )
   { }


   /**
      Destructor (no operations)
   */
   ~PoissonLikelihoodFCN () {}

private:
   // usually copying is non trivial, so we declare but don't implement them

   /**
      Copy constructor
   */
   PoissonLikelihoodFCN(const PoissonLikelihoodFCN &);

   /**
      Assignment operator
   */
   PoissonLikelihoodFCN & operator = (const PoissonLikelihoodFCN &);

public:

   /// clone the function (need to return Base for Windows)
   virtual BaseFunction * Clone() const { return new  PoissonLikelihoodFCN(fData,fFunc,fWeight); }

   // effective points used in the fit
   virtual unsigned int NFitPoints() const { return fNEffPoints; }

   /// i-th likelihood element and its gradient
   virtual double DataElement(const double * x, unsigned int i, double * g) const {
      if (i==0) this->UpdateNCalls();
      return FitUtil::EvaluatePoissonBinPdf(fFunc, fData, x, i, g);
   }

   /// evaluate gradient
   virtual void Gradient(const double *x, double *g) const {
      // evaluate the chi2 gradient
      FitUtil::EvaluatePoissonLogLGradient(fFunc, fData, x, g );
   }

   /// get type of fit method function
   virtual  typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }

   /// access to const reference to the data
   virtual const BinData & Data() const { return fData; }

   /// access to const reference to the model function
   virtual const IModelFunction & ModelFunction() const { return fFunc; }

   bool IsWeighted() const { return (fWeight != 0); }

   // Use the weights in evaluating the likelihood 
   void UseSumOfWeights() { 
      if (fWeight == 0) return; // do nothing if it was not weighted 
      fWeight = 1;
   }

   // Use sum of the weight squared in evaluating the likelihood 
   // (this is needed for calculating the errors)
   void UseSumOfWeightSquare() { 
      if (fWeight == 0) return; // do nothing if it was not weighted 
      fWeight = 2;
   }


protected:


private:

   /**
      Evaluation of the  function (required by interface)
    */
   virtual double DoEval (const double * x) const {
      this->UpdateNCalls();
      return FitUtil::EvaluatePoissonLogL(fFunc, fData, x, fWeight, fNEffPoints);
   }

   // for derivatives
   virtual double  DoDerivative(const double * x, unsigned int icoord ) const {
      Gradient(x, &fGrad[0]);
      return fGrad[icoord];
   }


      //data member


   const BinData & fData;
   const IModelFunction & fFunc;

   mutable unsigned int fNEffPoints;  // number of effective points used in the fit

   int fWeight;  // flag to indicate if needs to evaluate using weight or weight squared 

   mutable std::vector<double> fGrad; // for derivatives

};

      // define useful typedef's
      typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGenFunction> PoissonLLFunction;
      typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGradFunction> PoissonLLGradFunction;


   } // end namespace Fit

} // end namespace ROOT


#endif /* ROOT_Fit_PoissonLikelihoodFCN */
 PoissonLikelihoodFCN.h:1
 PoissonLikelihoodFCN.h:2
 PoissonLikelihoodFCN.h:3
 PoissonLikelihoodFCN.h:4
 PoissonLikelihoodFCN.h:5
 PoissonLikelihoodFCN.h:6
 PoissonLikelihoodFCN.h:7
 PoissonLikelihoodFCN.h:8
 PoissonLikelihoodFCN.h:9
 PoissonLikelihoodFCN.h:10
 PoissonLikelihoodFCN.h:11
 PoissonLikelihoodFCN.h:12
 PoissonLikelihoodFCN.h:13
 PoissonLikelihoodFCN.h:14
 PoissonLikelihoodFCN.h:15
 PoissonLikelihoodFCN.h:16
 PoissonLikelihoodFCN.h:17
 PoissonLikelihoodFCN.h:18
 PoissonLikelihoodFCN.h:19
 PoissonLikelihoodFCN.h:20
 PoissonLikelihoodFCN.h:21
 PoissonLikelihoodFCN.h:22
 PoissonLikelihoodFCN.h:23
 PoissonLikelihoodFCN.h:24
 PoissonLikelihoodFCN.h:25
 PoissonLikelihoodFCN.h:26
 PoissonLikelihoodFCN.h:27
 PoissonLikelihoodFCN.h:28
 PoissonLikelihoodFCN.h:29
 PoissonLikelihoodFCN.h:30
 PoissonLikelihoodFCN.h:31
 PoissonLikelihoodFCN.h:32
 PoissonLikelihoodFCN.h:33
 PoissonLikelihoodFCN.h:34
 PoissonLikelihoodFCN.h:35
 PoissonLikelihoodFCN.h:36
 PoissonLikelihoodFCN.h:37
 PoissonLikelihoodFCN.h:38
 PoissonLikelihoodFCN.h:39
 PoissonLikelihoodFCN.h:40
 PoissonLikelihoodFCN.h:41
 PoissonLikelihoodFCN.h:42
 PoissonLikelihoodFCN.h:43
 PoissonLikelihoodFCN.h:44
 PoissonLikelihoodFCN.h:45
 PoissonLikelihoodFCN.h:46
 PoissonLikelihoodFCN.h:47
 PoissonLikelihoodFCN.h:48
 PoissonLikelihoodFCN.h:49
 PoissonLikelihoodFCN.h:50
 PoissonLikelihoodFCN.h:51
 PoissonLikelihoodFCN.h:52
 PoissonLikelihoodFCN.h:53
 PoissonLikelihoodFCN.h:54
 PoissonLikelihoodFCN.h:55
 PoissonLikelihoodFCN.h:56
 PoissonLikelihoodFCN.h:57
 PoissonLikelihoodFCN.h:58
 PoissonLikelihoodFCN.h:59
 PoissonLikelihoodFCN.h:60
 PoissonLikelihoodFCN.h:61
 PoissonLikelihoodFCN.h:62
 PoissonLikelihoodFCN.h:63
 PoissonLikelihoodFCN.h:64
 PoissonLikelihoodFCN.h:65
 PoissonLikelihoodFCN.h:66
 PoissonLikelihoodFCN.h:67
 PoissonLikelihoodFCN.h:68
 PoissonLikelihoodFCN.h:69
 PoissonLikelihoodFCN.h:70
 PoissonLikelihoodFCN.h:71
 PoissonLikelihoodFCN.h:72
 PoissonLikelihoodFCN.h:73
 PoissonLikelihoodFCN.h:74
 PoissonLikelihoodFCN.h:75
 PoissonLikelihoodFCN.h:76
 PoissonLikelihoodFCN.h:77
 PoissonLikelihoodFCN.h:78
 PoissonLikelihoodFCN.h:79
 PoissonLikelihoodFCN.h:80
 PoissonLikelihoodFCN.h:81
 PoissonLikelihoodFCN.h:82
 PoissonLikelihoodFCN.h:83
 PoissonLikelihoodFCN.h:84
 PoissonLikelihoodFCN.h:85
 PoissonLikelihoodFCN.h:86
 PoissonLikelihoodFCN.h:87
 PoissonLikelihoodFCN.h:88
 PoissonLikelihoodFCN.h:89
 PoissonLikelihoodFCN.h:90
 PoissonLikelihoodFCN.h:91
 PoissonLikelihoodFCN.h:92
 PoissonLikelihoodFCN.h:93
 PoissonLikelihoodFCN.h:94
 PoissonLikelihoodFCN.h:95
 PoissonLikelihoodFCN.h:96
 PoissonLikelihoodFCN.h:97
 PoissonLikelihoodFCN.h:98
 PoissonLikelihoodFCN.h:99
 PoissonLikelihoodFCN.h:100
 PoissonLikelihoodFCN.h:101
 PoissonLikelihoodFCN.h:102
 PoissonLikelihoodFCN.h:103
 PoissonLikelihoodFCN.h:104
 PoissonLikelihoodFCN.h:105
 PoissonLikelihoodFCN.h:106
 PoissonLikelihoodFCN.h:107
 PoissonLikelihoodFCN.h:108
 PoissonLikelihoodFCN.h:109
 PoissonLikelihoodFCN.h:110
 PoissonLikelihoodFCN.h:111
 PoissonLikelihoodFCN.h:112
 PoissonLikelihoodFCN.h:113
 PoissonLikelihoodFCN.h:114
 PoissonLikelihoodFCN.h:115
 PoissonLikelihoodFCN.h:116
 PoissonLikelihoodFCN.h:117
 PoissonLikelihoodFCN.h:118
 PoissonLikelihoodFCN.h:119
 PoissonLikelihoodFCN.h:120
 PoissonLikelihoodFCN.h:121
 PoissonLikelihoodFCN.h:122
 PoissonLikelihoodFCN.h:123
 PoissonLikelihoodFCN.h:124
 PoissonLikelihoodFCN.h:125
 PoissonLikelihoodFCN.h:126
 PoissonLikelihoodFCN.h:127
 PoissonLikelihoodFCN.h:128
 PoissonLikelihoodFCN.h:129
 PoissonLikelihoodFCN.h:130
 PoissonLikelihoodFCN.h:131
 PoissonLikelihoodFCN.h:132
 PoissonLikelihoodFCN.h:133
 PoissonLikelihoodFCN.h:134
 PoissonLikelihoodFCN.h:135
 PoissonLikelihoodFCN.h:136
 PoissonLikelihoodFCN.h:137
 PoissonLikelihoodFCN.h:138
 PoissonLikelihoodFCN.h:139
 PoissonLikelihoodFCN.h:140
 PoissonLikelihoodFCN.h:141
 PoissonLikelihoodFCN.h:142
 PoissonLikelihoodFCN.h:143
 PoissonLikelihoodFCN.h:144
 PoissonLikelihoodFCN.h:145
 PoissonLikelihoodFCN.h:146
 PoissonLikelihoodFCN.h:147
 PoissonLikelihoodFCN.h:148
 PoissonLikelihoodFCN.h:149
 PoissonLikelihoodFCN.h:150
 PoissonLikelihoodFCN.h:151
 PoissonLikelihoodFCN.h:152
 PoissonLikelihoodFCN.h:153
 PoissonLikelihoodFCN.h:154
 PoissonLikelihoodFCN.h:155
 PoissonLikelihoodFCN.h:156
 PoissonLikelihoodFCN.h:157
 PoissonLikelihoodFCN.h:158
 PoissonLikelihoodFCN.h:159
 PoissonLikelihoodFCN.h:160
 PoissonLikelihoodFCN.h:161
 PoissonLikelihoodFCN.h:162
 PoissonLikelihoodFCN.h:163
 PoissonLikelihoodFCN.h:164
 PoissonLikelihoodFCN.h:165
 PoissonLikelihoodFCN.h:166
 PoissonLikelihoodFCN.h:167
 PoissonLikelihoodFCN.h:168
 PoissonLikelihoodFCN.h:169
 PoissonLikelihoodFCN.h:170
 PoissonLikelihoodFCN.h:171
 PoissonLikelihoodFCN.h:172
 PoissonLikelihoodFCN.h:173
 PoissonLikelihoodFCN.h:174
 PoissonLikelihoodFCN.h:175
 PoissonLikelihoodFCN.h:176
 PoissonLikelihoodFCN.h:177
 PoissonLikelihoodFCN.h:178
 PoissonLikelihoodFCN.h:179
 PoissonLikelihoodFCN.h:180
 PoissonLikelihoodFCN.h:181
 PoissonLikelihoodFCN.h:182
 PoissonLikelihoodFCN.h:183
 PoissonLikelihoodFCN.h:184