51double gauss2D(
double *
x,
double *par) {
52 double z1 =
double((
x[0]-par[1])/par[2]);
53 double z2 =
double((
x[1]-par[3])/par[4]);
54 return par[0]*
exp(-0.5*(z1*z1+z2*z2));
56double my2Dfunc(
double *
x,
double *par) {
57 return gauss2D(
x,&par[0]) + gauss2D(
x,&par[5]);
68 MyFcn(TH2D * _h1, TH2D * _h2) :
87 for (
int ix = 1; ix <= nbinX1; ++ix) {
89 for (
int iy = 1; iy <= nbinY1; ++iy) {
98 for (
int ix = 1; ix <= nbinX2; ++ix) {
100 for (
int iy = 1; iy <= nbinY2; ++iy) {
113void FillHisto(
TH2D *
h,
int n,
double * p) {
116 const double mx1 = p[1];
117 const double my1 = p[3];
118 const double sx1 = p[2];
119 const double sy1 = p[4];
120 const double mx2 = p[6];
121 const double my2 = p[8];
122 const double sx2 = p[7];
123 const double sy2 = p[9];
125 const double w1 = 0.5;
128 for (
int i = 0; i <
n; ++i) {
149int fit2dHist(
int option=1) {
166 auto h1 =
new TH2D(
"h1",
"core",nbx1,xlow1,xup1,nby1,ylow1,yup1);
167 auto h2 =
new TH2D(
"h2",
"tails",nbx2,xlow2,xup2,nby2,ylow2,yup2);
169 double iniParams[10] = { 100, 6., 2., 7., 3, 100, 12., 3., 11., 2. };
171 TF2 * func =
new TF2(
"func",my2Dfunc,xlow2,xup2,ylow2,yup2, 10);
177 FillHisto(
h1,n1,iniParams);
178 FillHisto(h2,n2,iniParams);
181 double dx1 = (xup1-xlow1)/
double(nbx1);
182 double dy1 = (yup1-ylow1)/
double(nby1);
183 double dx2 = (xup2-xlow2)/
double(nbx2);
184 double dy2 = (yup2-ylow2)/
double(nby2);
187 h2->
Scale( (
double(n1) * dx1 * dy1 ) / (
double(n2) * dx2 * dy2 ) );
190 if (option > 10) global =
true;
194 std::cout <<
"Do global fit" << std::endl;
205 for (
int i = 0; i < 10; ++i) {
211 Error(
"fit2DHist",
"Fit Failed to converge");
216 double minParams[10];
217 double parErrors[10];
218 for (
int i = 0; i < 10; ++i) {
224 int npfits = myFcn.npfits;
233 h1->GetListOfFunctions()->Add(func);
243 TCanvas *
c1 =
new TCanvas(
"c1",
"Two HIstogram Fit example",100,10,900,800);
250 func->
SetRange(xlow1,ylow1,xup1,yup1);
256 func->
SetRange(xlow2,ylow2,xup2,yup2);
262 func->
Draw(
"surf1 same");
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
TRObject operator()(const T1 &t1) const
void SetMinimizer(const char *type, const char *algo=nullptr)
set minimizer type and algorithm
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
double Error(unsigned int i) const
parameter error by index
double MinFcnValue() const
Return value of the objective function (chi2 or likelihood) used in the fit.
double Edm() const
Expected distance from minimum.
unsigned int NFreeParameters() const
get total number of free parameters
double Parameter(unsigned int i) const
parameter value by index
Fitter class, entry point for performing all type of fits.
const FitResult & Result() const
get fit result
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 *)...
const FitConfig & Config() const
access to the fit configuration (const method)
bool SetFCN(unsigned int npar, Function &fcn, const double *params=nullptr, unsigned int dataSize=0, int fitType=0)
Set a generic FCN function as a C++ callable object implementing double () (const double *) Note that...
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
virtual void SetNDF(Int_t ndf)
virtual void SetChisquare(Double_t chi2)
virtual const char * GetParName(Int_t ipar) const
virtual void SetParameters(const Double_t *params)
virtual void SetParErrors(const Double_t *errors)
virtual Double_t GetParameter(Int_t ipar) const
TF1 * DrawCopy(Option_t *option="") const override
void Draw(Option_t *option="") override
Default Draw method for all objects.
void SetRange(Double_t xmin, Double_t xmax) override
virtual Int_t GetNbinsY() const
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
virtual Int_t GetNbinsX() const
void Draw(Option_t *option="") override
Draw this histogram with options.
TList * GetListOfFunctions() const
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
2-D histogram with a double per channel (see TH1 documentation)
Double_t GetBinContent(Int_t binx, Int_t biny) const override
void Add(TObject *obj) override
RVec< PromoteType< T > > exp(const RVec< T > &v)