Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
combinedFit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook
4/// Combined (simultaneous) fit of two histogram with separate functions
5/// and some common parameters
6///
7/// See http://root.cern/phpBB3//viewtopic.php?f=3&t=11740#p50908
8/// for a modified version working with Fumili or GSLMultiFit
9///
10/// N.B. this macro must be compiled with ACliC
11///
12/// \macro_image
13/// \macro_output
14/// \macro_code
15///
16/// \author Lorenzo Moneta
17
18#include <Fit/Fitter.h>
19#include <Fit/BinData.h>
20#include <Fit/Chi2FCN.h>
21#include <TH1.h>
23#include <HFitInterface.h>
24#include <TCanvas.h>
25#include <TStyle.h>
26
27// definition of shared parameter
28// background function
29int iparB[2] = {
30 0, // exp amplitude in B histo
31 2 // exp common parameter
32};
33
34// signal + background function
35int iparSB[5] = {
36 1, // exp amplitude in S+B histo
37 2, // exp common parameter
38 3, // Gaussian amplitude
39 4, // Gaussian mean
40 5 // Gaussian sigma
41};
42
43// Create the GlobalCHi2 structure
44
45struct GlobalChi2 {
47
48 // parameter vector is first background (in common 1 and 2)
49 // and then is signal (only in 2)
50 double operator()(const double *par) const
51 {
52 double p1[2];
53 for (int i = 0; i < 2; ++i)
54 p1[i] = par[iparB[i]];
55
56 double p2[5];
57 for (int i = 0; i < 5; ++i)
58 p2[i] = par[iparSB[i]];
59
60 return (*fChi2_1)(p1) + (*fChi2_2)(p2);
61 }
62
65};
66
67void combinedFit()
68{
69
70 TH1D *hB = new TH1D("hB", "histo B", 100, 0, 100);
71 TH1D *hSB = new TH1D("hSB", "histo S+B", 100, 0, 100);
72
73 // Create functions (not adding them to ROOT's global list,
74 // because we want to add them to the histograms later)
75 TF1 *fB = new TF1("fB", "expo", 0, 100, TF1::EAddToList::kNo);
76 fB->SetParameters(1, -0.05);
77 hB->FillRandom(fB);
78
79 TF1 *fS = new TF1("fS", "gaus", 0, 100, TF1::EAddToList::kNo);
80 fS->SetParameters(1, 30, 5);
81
82 hSB->FillRandom(fB, 2000);
83 hSB->FillRandom(fS, 1000);
84
85 // perform now global fit
86
87 TF1 *fSB = new TF1("fSB", "expo + gaus(2)", 0, 100, TF1::EAddToList::kNo);
88
91
94 // set the data range
95 rangeB.SetRange(10, 90);
98
100 rangeSB.SetRange(10, 50);
103
106
108
109 ROOT::Fit::Fitter fitter;
110
111 const int Npar = 6;
112 double par0[Npar] = {5, 5, -0.1, 100, 30, 10};
113
114 // create before the parameter settings in order to fix or set range on them
115 fitter.Config().SetParamsSettings(6, par0);
116 // fix 5-th parameter
117 fitter.Config().ParSettings(4).Fix();
118 // set limits on the third and 4-th parameter
119 fitter.Config().ParSettings(2).SetLimits(-10, -1.E-4);
120 fitter.Config().ParSettings(3).SetLimits(0, 10000);
121 fitter.Config().ParSettings(3).SetStepSize(5);
122
123 fitter.Config().MinimizerOptions().SetPrintLevel(0);
124 fitter.Config().SetMinimizer("Minuit2", "Migrad");
125
126 // fit FCN function directly
127 // (specify optionally data size and flag to indicate that is a chi2 fit)
128 fitter.FitFCN(6, globalChi2, nullptr, dataB.Size() + dataSB.Size(), true);
130 result.Print(std::cout);
131
132 std::cout << "Combined fit Chi2 = " << result.Chi2() << std::endl;
133
134 TCanvas *c1 = new TCanvas("Simfit", "Simultaneous fit of two histograms", 10, 10, 700, 700);
135 c1->Divide(1, 2);
136 c1->cd(1);
137 gStyle->SetOptFit(1111);
138
140 fB->SetRange(rangeB().first, rangeB().second);
141 fB->SetLineColor(kBlue);
142 hB->GetListOfFunctions()->Add(fB);
143 hB->Draw();
144
145 c1->cd(2);
146 fSB->SetFitResult(result, iparSB);
147 fSB->SetRange(rangeSB().first, rangeSB().second);
148 fSB->SetLineColor(kRed);
149 hSB->GetListOfFunctions()->Add(fSB);
150 hSB->Draw();
151}
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
TRObject operator()(const T1 &t1) const
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition DataRange.h:35
class containing the result of the fit and all the related information (fitted parameter values,...
Definition FitResult.h:44
Fitter class, entry point for performing all type of fits.
Definition Fitter.h:78
const FitResult & Result() const
get fit result
Definition Fitter.h:400
bool FitFCN(unsigned int npar, Function &fcn, const double *params=nullptr, unsigned int dataSize=0, int fitType=0)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
Definition Fitter.h:267
const FitConfig & Config() const
access to the fit configuration (const method)
Definition Fitter.h:428
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:63
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:182
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition TF1.cxx:3583
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
virtual void SetFitResult(const ROOT::Fit::FitResult &result, const Int_t *indpar=nullptr)
Set the result from the fit parameter values, errors, chi2, etc... Optionally a pointer to a vector (...
Definition TF1.cxx:3410
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
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:1594
return c1
Definition legend1.C:41
TF1 * f1
Definition legend1.C:11
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.
DataOptions : simple structure holding the options on how the data are filled.
Definition DataOptions.h:28