Logo ROOT  
Reference Guide
combinedFit.C File Reference

Detailed Description

View in nbviewer Open in SWAN Combined (simultaneous) fit of two histogram with separate functions and some common parameters

See http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=11740#p50908 for a modified version working with Fumili or GSLMultiFit

N.B. this macro must be compiled with ACliC

****************************************
Minimizer is Minuit2 / Migrad
Chi2 = 131.104
NDf = 115
Edm = 2.11602e-08
NCalls = 225
Par_0 = 5.5396 +/- 0.0354094
Par_1 = 4.66089 +/- 0.050106
Par_2 = -0.0514037 +/- 0.00108539 (limited)
Par_3 = 77.2733 +/- 3.93105 (limited)
Par_4 = 30 (fixed)
Par_5 = 4.864 +/- 0.243005
#include "Fit/Fitter.h"
#include "Fit/BinData.h"
#include "Fit/Chi2FCN.h"
#include "TH1.h"
#include "TList.h"
#include "HFitInterface.h"
#include "TCanvas.h"
#include "TStyle.h"
// definition of shared parameter
// background function
int iparB[2] = { 0, // exp amplitude in B histo
2 // exp common parameter
};
// signal + background function
int iparSB[5] = { 1, // exp amplitude in S+B histo
2, // exp common parameter
3, // gaussian amplitude
4, // gaussian mean
5 // gaussian sigma
};
// Create the GlobalCHi2 structure
struct GlobalChi2 {
fChi2_1(&f1), fChi2_2(&f2) {}
// parameter vector is first background (in common 1 and 2)
// and then is signal (only in 2)
double operator() (const double *par) const {
double p1[2];
for (int i = 0; i < 2; ++i) p1[i] = par[iparB[i] ];
double p2[5];
for (int i = 0; i < 5; ++i) p2[i] = par[iparSB[i] ];
return (*fChi2_1)(p1) + (*fChi2_2)(p2);
}
};
void combinedFit() {
TH1D * hB = new TH1D("hB","histo B",100,0,100);
TH1D * hSB = new TH1D("hSB","histo S+B",100, 0,100);
TF1 * fB = new TF1("fB","expo",0,100);
fB->SetParameters(1,-0.05);
hB->FillRandom("fB");
TF1 * fS = new TF1("fS","gaus",0,100);
fS->SetParameters(1,30,5);
hSB->FillRandom("fB",2000);
hSB->FillRandom("fS",1000);
// perform now global fit
TF1 * fSB = new TF1("fSB","expo + gaus(2)",0,100);
// set the data range
rangeB.SetRange(10,90);
ROOT::Fit::BinData dataB(opt,rangeB);
ROOT::Fit::FillData(dataB, hB);
rangeSB.SetRange(10,50);
ROOT::Fit::BinData dataSB(opt,rangeSB);
ROOT::Fit::FillData(dataSB, hSB);
ROOT::Fit::Chi2Function chi2_B(dataB, wfB);
ROOT::Fit::Chi2Function chi2_SB(dataSB, wfSB);
GlobalChi2 globalChi2(chi2_B, chi2_SB);
const int Npar = 6;
double par0[Npar] = { 5,5,-0.1,100, 30,10};
// create before the parameter settings in order to fix or set range on them
fitter.Config().SetParamsSettings(6,par0);
// fix 5-th parameter
fitter.Config().ParSettings(4).Fix();
// set limits on the third and 4-th parameter
fitter.Config().ParSettings(2).SetLimits(-10,-1.E-4);
fitter.Config().ParSettings(3).SetLimits(0,10000);
fitter.Config().ParSettings(3).SetStepSize(5);
fitter.Config().SetMinimizer("Minuit2","Migrad");
// fit FCN function directly
// (specify optionally data size and flag to indicate that is a chi2 fit)
fitter.FitFCN(6,globalChi2,0,dataB.Size()+dataSB.Size(),true);
ROOT::Fit::FitResult result = fitter.Result();
result.Print(std::cout);
TCanvas * c1 = new TCanvas("Simfit","Simultaneous fit of two histograms",
10,10,700,700);
c1->Divide(1,2);
c1->cd(1);
gStyle->SetOptFit(1111);
fB->SetFitResult( result, iparB);
fB->SetRange(rangeB().first, rangeB().second);
fB->SetLineColor(kBlue);
hB->GetListOfFunctions()->Add(fB);
hB->Draw();
c1->cd(2);
fSB->SetFitResult( result, iparSB);
fSB->SetRange(rangeSB().first, rangeSB().second);
fSB->SetLineColor(kRed);
hSB->GetListOfFunctions()->Add(fSB);
hSB->Draw();
}
Author
Lorenzo Moneta

Definition in file combinedFit.C.

ROOT::Math::WrappedMultiTF1Templ
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
Definition: WrappedMultiTF1.h:57
ROOT::Fit::FitResult::Print
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
Definition: FitResult.cxx:438
ROOT::Fit::FitConfig::SetMinimizer
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
Definition: FitConfig.h:186
first
Definition: first.py:1
ROOT::Fit::DataOptions
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:38
ROOT::Fit::Chi2FCN
Chi2FCN class for binnned fits using the least square methods.
Definition: Chi2FCN.h:49
ROOT::Fit::BinData
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:62
ROOT::Fit::FillData
void FillData(BinData &dv, const TH1 *hist, TF1 *func=0)
fill the data vector from a TH1.
Definition: HFitInterface.cxx:117
HFitInterface.h
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
TStyle.h
operator()
TRObject operator()(const T1 &t1) const
Definition: TRFunctionImport__oprtr.h:14
TList.h
TH1::GetListOfFunctions
TList * GetListOfFunctions() const
Definition: TH1.h:240
TCanvas.h
ROOT::Fit::FitConfig::ParSettings
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
Definition: FitConfig.h:81
ROOT::Fit::ParameterSettings::Fix
void Fix()
fix the parameter
Definition: ParameterSettings.h:151
ROOT::Math::MinimizerOptions::SetPrintLevel
void SetPrintLevel(int level)
set print level
Definition: MinimizerOptions.h:131
TStyle::SetOptFit
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:1544
ROOT::Fit::DataRange::SetRange
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:134
TF1::SetParameters
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:640
TF1::SetFitResult
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:3376
Chi2FCN.h
ROOT::Fit::Fitter::FitFCN
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:610
ROOT::Fit::ParameterSettings::SetStepSize
void SetStepSize(double err)
set the step size
Definition: ParameterSettings.h:157
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
ROOT::Fit::Fitter
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
ROOT::Fit::ParameterSettings::SetLimits
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 The c...
Definition: ParameterSettings.h:162
ROOT::Fit::FitResult
class containg the result of the fit and all the related information (fitted parameter values,...
Definition: FitResult.h:52
ROOT::Fit::DataRange
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:45
ROOT::Fit::FitConfig::SetParamsSettings
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:145
ROOT::Fit::FitConfig::MinimizerOptions
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
Definition: FitConfig.h:172
kRed
@ kRed
Definition: Rtypes.h:66
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3444
TF1::SetRange
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition: TF1.cxx:3540
Fitter.h
ROOT::Fit::Fitter::Config
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:412
f1
TF1 * f1
Definition: legend1.C:11
TCanvas
Definition: TCanvas.h:23
BinData.h
ROOT::Fit::Fitter::Result
const FitResult & Result() const
get fit result
Definition: Fitter.h:384
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
WrappedMultiTF1.h
kBlue
@ kBlue
Definition: Rtypes.h:66
ROOT::Math::IBaseFunctionMultiDimTempl
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
TF1
1-Dim function class
Definition: TF1.h:212
TGeant4Unit::second
static constexpr double second
Definition: TGeant4SystemOfUnits.h:157
TH1.h
TMath::E
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:102
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
c1
return c1
Definition: legend1.C:41