ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
combinedFit.C
Go to the documentation of this file.
1 //+ Combined (simultaneous) fit of two histogram with separate functions
2 // and some common parameters
3 //
4 // See http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=11740#p50908
5 // for a modified version working with Fumili or GSLMultiFit
6 //
7 // N.B. this macro must be compiled with ACliC
8 //
9 //Author: L. Moneta - Dec 2010
10 
11 #include "Fit/Fitter.h"
12 #include "Fit/BinData.h"
13 #include "Fit/Chi2FCN.h"
14 #include "TH1.h"
15 #include "TList.h"
16 #include "Math/WrappedMultiTF1.h"
17 #include "HFitInterface.h"
18 #include "TCanvas.h"
19 #include "TStyle.h"
20 
21 
22 // definition of shared parameter
23 // background function
24 int iparB[2] = { 0, // exp amplitude in B histo
25  2 // exp common parameter
26 };
27 
28 // signal + background function
29 int iparSB[5] = { 1, // exp amplitude in S+B histo
30  2, // exp common parameter
31  3, // gaussian amplitude
32  4, // gaussian mean
33  5 // gaussian sigma
34 };
35 
36 struct GlobalChi2 {
37  GlobalChi2( ROOT::Math::IMultiGenFunction & f1,
39  fChi2_1(&f1), fChi2_2(&f2) {}
40 
41  // parameter vector is first background (in common 1 and 2)
42  // and then is signal (only in 2)
43  double operator() (const double *par) const {
44  double p1[2];
45  for (int i = 0; i < 2; ++i) p1[i] = par[iparB[i] ];
46 
47  double p2[5];
48  for (int i = 0; i < 5; ++i) p2[i] = par[iparSB[i] ];
49 
50  return (*fChi2_1)(p1) + (*fChi2_2)(p2);
51  }
52 
53  const ROOT::Math::IMultiGenFunction * fChi2_1;
54  const ROOT::Math::IMultiGenFunction * fChi2_2;
55 };
56 
57 void combinedFit() {
58 
59  TH1D * hB = new TH1D("hB","histo B",100,0,100);
60  TH1D * hSB = new TH1D("hSB","histo S+B",100, 0,100);
61 
62  TF1 * fB = new TF1("fB","expo",0,100);
63  fB->SetParameters(1,-0.05);
64  hB->FillRandom("fB");
65 
66  TF1 * fS = new TF1("fS","gaus",0,100);
67  fS->SetParameters(1,30,5);
68 
69  hSB->FillRandom("fB",2000);
70  hSB->FillRandom("fS",1000);
71 
72  // perform now global fit
73 
74  TF1 * fSB = new TF1("fSB","expo + gaus(2)",0,100);
75 
76  ROOT::Math::WrappedMultiTF1 wfB(*fB,1);
77  ROOT::Math::WrappedMultiTF1 wfSB(*fSB,1);
78 
80  ROOT::Fit::DataRange rangeB;
81  // set the data range
82  rangeB.SetRange(10,90);
83  ROOT::Fit::BinData dataB(opt,rangeB);
84  ROOT::Fit::FillData(dataB, hB);
85 
86  ROOT::Fit::DataRange rangeSB;
87  rangeSB.SetRange(10,50);
88  ROOT::Fit::BinData dataSB(opt,rangeSB);
89  ROOT::Fit::FillData(dataSB, hSB);
90 
91  ROOT::Fit::Chi2Function chi2_B(dataB, wfB);
92  ROOT::Fit::Chi2Function chi2_SB(dataSB, wfSB);
93 
94  GlobalChi2 globalChi2(chi2_B, chi2_SB);
95 
96  ROOT::Fit::Fitter fitter;
97 
98  const int Npar = 6;
99  double par0[Npar] = { 5,5,-0.1,100, 30,10};
100 
101  // create before the parameter settings in order to fix or set range on them
102  fitter.Config().SetParamsSettings(6,par0);
103  // fix 5-th parameter
104  fitter.Config().ParSettings(4).Fix();
105  // set limits on the third and 4-th parameter
106  fitter.Config().ParSettings(2).SetLimits(-10,-1.E-4);
107  fitter.Config().ParSettings(3).SetLimits(0,10000);
108  fitter.Config().ParSettings(3).SetStepSize(5);
109 
110  fitter.Config().MinimizerOptions().SetPrintLevel(0);
111  fitter.Config().SetMinimizer("Minuit2","Migrad");
112 
113  // fit FCN function directly
114  // (specify optionally data size and flag to indicate that is a chi2 fit)
115  fitter.FitFCN(6,globalChi2,0,dataB.Size()+dataSB.Size(),true);
117  result.Print(std::cout);
118 
119  TCanvas * c1 = new TCanvas("Simfit","Simultaneous fit of two histograms",
120  10,10,700,700);
121  c1->Divide(1,2);
122  c1->cd(1);
123  gStyle->SetOptFit(1111);
124 
125  fB->SetFitResult( result, iparB);
126  fB->SetRange(rangeB().first, rangeB().second);
127  fB->SetLineColor(kBlue);
128  hB->GetListOfFunctions()->Add(fB);
129  hB->Draw();
130 
131  c1->cd(2);
132  fSB->SetFitResult( result, iparSB);
133  fSB->SetRange(rangeSB().first, rangeSB().second);
134  fSB->SetLineColor(kRed);
135  hSB->GetListOfFunctions()->Add(fSB);
136  hSB->Draw();
137 
138 
139 }
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:382
TList * GetListOfFunctions() const
Definition: TH1.h:244
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
void SetPrintLevel(int level)
set print level
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:61
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
tuple f2
Definition: surfaces.py:24
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition: TF1.cxx:3200
bool FitFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
Definition: Fitter.h:538
void combinedFit()
Definition: combinedFit.C:57
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
Definition: FitConfig.h:138
unsigned int Size() const
return number of fit points
Definition: BinData.h:447
double par0[8]
const FitResult & Result() const
get fit result
Definition: Fitter.h:354
static double p2(double t, double a, double b, double c)
void Fix()
fix the parameter
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
Chi2FCN class for binnned fits using the least square methods.
Definition: Chi2FCN.h:68
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
Definition: FitConfig.h:152
void SetStepSize(double err)
set the step size
void FillData(BinData &dv, const TH1 *hist, TF1 *func=0)
fill the data vector from a TH1.
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
int iparSB[5]
Definition: combinedFit.C:29
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
void SetRange(unsigned int icoord, double xmin, double xmax)
set a range [xmin,xmax] for the new coordinate icoord If more range exists for other coordinates...
Definition: DataRange.cxx:124
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:94
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
Definition: FitConfig.h:80
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1204
bool first
Definition: line3Dfit.C:48
Double_t E()
Definition: TMath.h:54
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
static double p1(double t, double a, double b)
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
The Canvas class.
Definition: TCanvas.h:48
class containg the result of the fit and all the related information (fitted parameter values...
Definition: FitResult.h:52
TRObject operator()(const T1 &t1) const
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:34
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
Definition: FitResult.cxx:446
int iparB[2]
Definition: combinedFit.C:24
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
virtual void Add(TObject *obj)
Definition: TList.h:81
void SetParamsSettings(unsigned int npar, const double *params, const double *vstep=0)
set the parameter settings from number of parameters and a vector of values and optionally step value...
Definition: FitConfig.cxx:136
1-Dim function class
Definition: TF1.h:149
virtual void SetFitResult(const ROOT::Fit::FitResult &result, const Int_t *indpar=0)
Set the result from the fit parameter values, errors, chi2, etc...
Definition: TF1.cxx:3038
TF1 * f1
Definition: legend1.C:11
void SetLimits(double low, double up)
set a double side limit, if low == up the parameter is fixed if low > up the limits are removed ...
double result[121]
Definition: Rtypes.h:61
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63