Logo ROOT   6.14/05
Reference Guide
fit2d.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 /// Example illustrating how to fit a 2-d histogram of type y=f(x)
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 ///
10 /// \author Rene Brun
11 
12 void fit2d()
13 {
14  // generate a 2-d histogram using a TCutG
15  const Int_t n = 6;
16  Float_t x[n] = {0.092,0.83,0.94,0.81,0.12,0.1};
17  Float_t y[n] = {0.71,9.4,9,8,0.3,0.71};
18  TCutG *cut = new TCutG("cut",n,x,y);
19  TH2F *h2 = new TH2F("h2","h2",40,0,1,40,0,10);
20  Float_t u,v;
21  for (Int_t i=0;i<100000;i++) {
22  u = gRandom->Rndm();
23  v = 10*gRandom->Rndm();
24  if (cut->IsInside(u,v)) h2->Fill(u,v);
25  }
26  TCanvas *c1 = new TCanvas("c1","show profile",600,900);
27  c1->Divide(1,2);
28  c1->cd(1);
29  h2->Draw();
30  c1->cd(2);
31 
32  //use a TProfile to convert the 2-d to 1-d problem
33  TProfile *prof = h2->ProfileX();
34  prof->Fit("pol1");
35 }
36 
float Float_t
Definition: RtypesCore.h:53
return c1
Definition: legend1.C:41
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
int Int_t
Definition: RtypesCore.h:41
Profile Histogram.
Definition: TProfile.h:32
Graphical cut class.
Definition: TCutG.h:20
Double_t x[n]
Definition: legend1.C:17
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
SVector< double, 2 > v
Definition: Dict.h:5
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:31
TProfile * ProfileX(const char *name="_pfx", Int_t firstybin=1, Int_t lastybin=-1, Option_t *option="") const
Project a 2-D histogram into a profile histogram along X.
Definition: TH2.cxx:1991
Double_t y[n]
Definition: legend1.C:17
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:1162
virtual Int_t IsInside(Double_t x, Double_t y) const
Return 1 if the point (x,y) is inside the polygon defined by the graph vertices 0 otherwise...
Definition: TGraph.cxx:1823
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:291
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.
Definition: TH1.cxx:3695
const Int_t n
Definition: legend1.C:16