// @(#)root/hist:$Id$
// Author: Frank Filthaut F.Filthaut@science.ru.nl  20/05/2002

#ifndef ROOT_TFractionFitter
#define ROOT_TFractionFitter

#ifndef ROOT_TVirtualFitter
# include "TVirtualFitter.h"
#endif

#ifndef ROOT_TObjArray
# include "TObjArray.h"
#endif

#ifndef ROOT_TFitResultPtr
#include "TFitResultPtr.h"
#endif

#include <vector>

class TH1;

namespace ROOT {
   namespace Fit  {
      class Fitter;
   }
}

///////////////////////////////////////////////////////////////////////////////
// TFractionFitter
//
// Fits MC fractions to data histogram (a la HMCMLL, see R. Barlow and C. Beeston,
// Comp. Phys. Comm. 77 (1993) 219-228, and http://www.hep.man.ac.uk/~roger/hfrac.f).
//
///////////////////////////////////////////////////////////////////////////////

class TFractionFitter: public TObject {
public:
   TFractionFitter();
   TFractionFitter(TH1* data, TObjArray *MCs, Option_t *option="");
   virtual ~TFractionFitter();

   //TVirtualFitter* GetFitter() const;
   ROOT::Fit::Fitter* GetFitter() const;
   void ErrorAnalysis(Double_t UP);
   void SetRangeX(Int_t low, Int_t high);
   void ReleaseRangeX();
   void SetRangeY(Int_t low, Int_t high);
   void ReleaseRangeY();
   void SetRangeZ(Int_t low, Int_t high);
   void ReleaseRangeZ();
   void ExcludeBin(Int_t bin);
   void IncludeBin(Int_t bin);
   void Constrain(Int_t parm, Double_t low, Double_t high);
   void UnConstrain(Int_t parm);
   void SetData(TH1 *data);
   void SetMC(Int_t parm, TH1 *MC);
   void SetWeight(Int_t parm, TH1* weight);
   TFitResultPtr Fit();

   void GetResult(Int_t parm, Double_t& value, Double_t& error) const;
   TH1* GetPlot();

   // This global function needs access to computeFCN()
   //friend void TFractionFitFCN(Int_t& npar, Double_t* gin, Double_t& f, Double_t* par, Int_t flag);

   // Goodness of fit
   Double_t GetChisquare() const;
   Int_t GetNDF() const;
   Double_t GetProb() const;

   // MC predictions (smeared templates)
   TH1* GetMCPrediction(Int_t parm) const;

   // FCN evaluation
   Double_t EvaluateFCN(const Double_t * par) {
      Double_t f = 0;
      ComputeFCN(f, par, 0);
      return f;
   }

private:
   void CheckParNo(Int_t parm) const;
   void CheckConsistency();
   void FindPrediction(int bin, double& t_i, int& k_0, double& A_ki) const;
   void ComputeFCN(Double_t& f, const Double_t* par, Int_t flag);
   void GetRanges(Int_t& minX, Int_t& maxX, Int_t& minY, Int_t& maxY,
                  Int_t& minZ, Int_t& maxZ) const;
   void ComputeChisquareLambda();
   bool IsExcluded(Int_t bin) const;

protected:
   Bool_t   fFitDone;             // flags whether a valid fit has been performed
   Int_t    fLowLimitX;           // first bin in X dimension
   Int_t    fHighLimitX;          // last  bin in X dimension
   Int_t    fLowLimitY;           // first bin in Y dimension
   Int_t    fHighLimitY;          // last  bin in Y dimension
   Int_t    fLowLimitZ;           // first bin in Z dimension
   Int_t    fHighLimitZ;          // last  bin in Z dimension
   std::vector<Int_t> fExcludedBins; // bins excluded from the fit

   Int_t    fNpfits;              // Number of points used in the fit
   Int_t    fNDF;                 // Number of degrees of freedom in the fit
   Double_t fChisquare;           // Template fit chisquare

   TObjArray fAji;                // array of pointers to predictions of real template distributions

   // Histograms
   TH1*      fData;               // pointer to the "data" histogram to be fitted to
   TObjArray fMCs;                // array of pointers to template histograms
   TObjArray fWeights;            // array of pointers to corresponding weight factors (may be null)
   Double_t  fIntegralData;       // "data" histogram content integral over allowed fit range
   Double_t* fIntegralMCs;        // same for template histograms (weights not taken into account)
   Double_t* fFractions;          // template fractions scaled to the "data" histogram statistics
   TH1*      fPlot;               // pointer to histogram containing summed template predictions
   ROOT::Fit::Fitter *fFractionFitter;  // pointer to Fitter class

   Int_t     fNpar;               // number of fit parameters

   ClassDef(TFractionFitter, 1)   // Fits MC fractions to data histogram
};

//
//  TFractionFitFCN
//
//  Computes negative log-likelihood for TFractionFitter
//

void TFractionFitFCN(Int_t& npar, Double_t* gin, Double_t& f, Double_t* par, Int_t flag);

#endif // ROOT_TFractionFitter
 TFractionFitter.h:1
 TFractionFitter.h:2
 TFractionFitter.h:3
 TFractionFitter.h:4
 TFractionFitter.h:5
 TFractionFitter.h:6
 TFractionFitter.h:7
 TFractionFitter.h:8
 TFractionFitter.h:9
 TFractionFitter.h:10
 TFractionFitter.h:11
 TFractionFitter.h:12
 TFractionFitter.h:13
 TFractionFitter.h:14
 TFractionFitter.h:15
 TFractionFitter.h:16
 TFractionFitter.h:17
 TFractionFitter.h:18
 TFractionFitter.h:19
 TFractionFitter.h:20
 TFractionFitter.h:21
 TFractionFitter.h:22
 TFractionFitter.h:23
 TFractionFitter.h:24
 TFractionFitter.h:25
 TFractionFitter.h:26
 TFractionFitter.h:27
 TFractionFitter.h:28
 TFractionFitter.h:29
 TFractionFitter.h:30
 TFractionFitter.h:31
 TFractionFitter.h:32
 TFractionFitter.h:33
 TFractionFitter.h:34
 TFractionFitter.h:35
 TFractionFitter.h:36
 TFractionFitter.h:37
 TFractionFitter.h:38
 TFractionFitter.h:39
 TFractionFitter.h:40
 TFractionFitter.h:41
 TFractionFitter.h:42
 TFractionFitter.h:43
 TFractionFitter.h:44
 TFractionFitter.h:45
 TFractionFitter.h:46
 TFractionFitter.h:47
 TFractionFitter.h:48
 TFractionFitter.h:49
 TFractionFitter.h:50
 TFractionFitter.h:51
 TFractionFitter.h:52
 TFractionFitter.h:53
 TFractionFitter.h:54
 TFractionFitter.h:55
 TFractionFitter.h:56
 TFractionFitter.h:57
 TFractionFitter.h:58
 TFractionFitter.h:59
 TFractionFitter.h:60
 TFractionFitter.h:61
 TFractionFitter.h:62
 TFractionFitter.h:63
 TFractionFitter.h:64
 TFractionFitter.h:65
 TFractionFitter.h:66
 TFractionFitter.h:67
 TFractionFitter.h:68
 TFractionFitter.h:69
 TFractionFitter.h:70
 TFractionFitter.h:71
 TFractionFitter.h:72
 TFractionFitter.h:73
 TFractionFitter.h:74
 TFractionFitter.h:75
 TFractionFitter.h:76
 TFractionFitter.h:77
 TFractionFitter.h:78
 TFractionFitter.h:79
 TFractionFitter.h:80
 TFractionFitter.h:81
 TFractionFitter.h:82
 TFractionFitter.h:83
 TFractionFitter.h:84
 TFractionFitter.h:85
 TFractionFitter.h:86
 TFractionFitter.h:87
 TFractionFitter.h:88
 TFractionFitter.h:89
 TFractionFitter.h:90
 TFractionFitter.h:91
 TFractionFitter.h:92
 TFractionFitter.h:93
 TFractionFitter.h:94
 TFractionFitter.h:95
 TFractionFitter.h:96
 TFractionFitter.h:97
 TFractionFitter.h:98
 TFractionFitter.h:99
 TFractionFitter.h:100
 TFractionFitter.h:101
 TFractionFitter.h:102
 TFractionFitter.h:103
 TFractionFitter.h:104
 TFractionFitter.h:105
 TFractionFitter.h:106
 TFractionFitter.h:107
 TFractionFitter.h:108
 TFractionFitter.h:109
 TFractionFitter.h:110
 TFractionFitter.h:111
 TFractionFitter.h:112
 TFractionFitter.h:113
 TFractionFitter.h:114
 TFractionFitter.h:115
 TFractionFitter.h:116
 TFractionFitter.h:117
 TFractionFitter.h:118
 TFractionFitter.h:119
 TFractionFitter.h:120
 TFractionFitter.h:121
 TFractionFitter.h:122
 TFractionFitter.h:123
 TFractionFitter.h:124
 TFractionFitter.h:125
 TFractionFitter.h:126
 TFractionFitter.h:127
 TFractionFitter.h:128
 TFractionFitter.h:129
 TFractionFitter.h:130
 TFractionFitter.h:131