Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
fit2a.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Fitting a 2-D histogram (a variant) This tutorial illustrates :

  • how to create a 2-d function
  • fill a 2-d histogram randomly from this function
  • fit the histogram
  • display the fitted function on top of the histogram (lego-plot) using a surface plot in a sub-range of the histogram.

This example can be executed via the interpreter or/and the compiler

root > .x fit2a.C
root > .x fit2a.C++
****************************************
Minimizer is Minuit2 / Migrad
Chi2 = 1048.29
NDf = 1139
Edm = 3.90056e-06
NCalls = 519
p0 = 392.558 +/- 2.07088
p1 = -2.99838 +/- 0.0116072
p2 = 2.98484 +/- 0.00840711
p3 = -3.00201 +/- 0.0115172
p4 = 2.97271 +/- 0.00841038
p5 = 601.133 +/- 10.5562
p6 = 0.00614073 +/- 0.0119548
p7 = 0.81626 +/- 0.0107847
p8 = -0.000781266 +/- 0.0134062
p9 = 0.911288 +/- 0.0119899
p10 = 146.899 +/- 5.12261
p11 = 3.9882 +/- 0.0182639
p12 = 0.727561 +/- 0.0142962
p13 = 4.02637 +/- 0.0175896
p14 = 0.703077 +/- 0.0140242
(TCanvas *) 0x55a9b34df990
#include "TF2.h"
#include "TH2.h"
#include "TCutG.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TStyle.h"
double g2(double *x, double *par) {
double r1 = double((x[0]-par[1])/par[2]);
double r2 = double((x[1]-par[3])/par[4]);
return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
}
double fun2(double *x, double *par) {
double *p1 = &par[0];
double *p2 = &par[5];
double *p3 = &par[10];
double result = g2(x,p1) + g2(x,p2) + g2(x,p3);
return result;
}
TCanvas *fit2a() {
TCanvas *c = new TCanvas();
const int npar = 15;
double f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
auto f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
f2->SetParameters(f2params);
//Create an histogram and fill it randomly with f2
auto h2 = new TH2F("h2","From f2",40,-10,10,40,-10,10);
int nentries = 100000;
h2->FillRandom("f2",nentries);
//Fit h2 with original function f2
float ratio = 4*nentries/100000;
f2params[ 0] *= ratio;
f2params[ 5] *= ratio;
f2params[10] *= ratio;
f2->SetParameters(f2params);
h2->Fit("f2","N");
auto cutg = new TCutG("cutg",5);
cutg->SetPoint(0,-7,-7);
cutg->SetPoint(1, 2,-7);
cutg->SetPoint(2, 2, 2);
cutg->SetPoint(3,-7, 2);
cutg->SetPoint(4,-7,-7);
h2->Draw("lego2 0");
h2->SetFillColor(38);
f2->SetNpx(80);
f2->SetNpy(80);
f2->Draw("surf1 same bb [cutg]");
return c;
}
#define c(i)
Definition RSha256.hxx:101
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
int nentries
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
The Canvas class.
Definition TCanvas.h:23
Graphical cut class.
Definition TCutG.h:20
A 2-Dim function with parameters.
Definition TF2.h:29
2-D histogram with a float per channel (see TH1 documentation)}
Definition TH2.h:258
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1636
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1884
Double_t x[n]
Definition legend1.C:17
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Definition TMath.h:709
Author
Rene Brun

Definition in file fit2a.C.