Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
fitslicesy.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 /// Illustrates how to use the TH1::FitSlicesY function
5 /// It uses the TH2F histogram generated in macro hsimple.C
6 /// It invokes FitSlicesY and draw the fitted "mean" and "sigma"
7 /// in 2 sepate pads.
8 /// This macro shows also how to annotate a picture, change
9 /// some pad parameters.
10 ///
11 /// \macro_image
12 /// \macro_output
13 /// \macro_code
14 ///
15 /// \author Rene Brun
16 
17 void fitslicesy() {
18 // Change some default parameters in the current style
19  gStyle->SetLabelSize(0.06,"x");
20  gStyle->SetLabelSize(0.06,"y");
21  gStyle->SetFrameFillColor(38);
22  gStyle->SetTitleW(0.6);
23  gStyle->SetTitleH(0.1);
24 
25 // Connect the input file and get the 2-d histogram in memory
26  TString dir = gROOT->GetTutorialDir();
27  dir.Append("/hsimple.C");
28  dir.ReplaceAll("/./","/");
29  if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
30  TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
31  if (!hsimpleFile) return;
32  TH2F *hpxpy = (TH2F*)hsimpleFile->Get("hpxpy");
33 
34 // Create a canvas and divide it
35  TCanvas *c1 = new TCanvas("c1","c1",700,500);
36  c1->SetFillColor(42);
37  c1->Divide(2,1);
38  TPad *leftPad = (TPad*)c1->cd(1);;
39  leftPad->Divide(1,2);
40 
41 // Draw 2-d original histogram
42  leftPad->cd(1);
43  gPad->SetTopMargin(0.12);
44  gPad->SetFillColor(33);
45  hpxpy->Draw();
46  hpxpy->GetXaxis()->SetLabelSize(0.06);
47  hpxpy->GetYaxis()->SetLabelSize(0.06);
48  hpxpy->SetMarkerColor(kYellow);
49 
50 // Fit slices projected along Y fron bins in X [7,32] with more than 20 bins in Y filled
51  hpxpy->FitSlicesY(0,7,32,20);
52 
53 // Show fitted "mean" for each slice
54  leftPad->cd(2);
55  gPad->SetFillColor(33);
56  TH2F *hpxpy_0 = (TH2F*)hsimpleFile->Get("hpxpy_0");
57  hpxpy_0->Draw();
58  TPad *rightPad = (TPad*)c1->cd(2);
59  rightPad->Divide(1,2);
60  rightPad->cd(1);
61  gPad->SetTopMargin(0.12);
62  gPad->SetLeftMargin(0.15);
63  gPad->SetFillColor(33);
64  TH2F *hpxpy_1 = (TH2F*)hsimpleFile->Get("hpxpy_1");
65  hpxpy_1->Draw();
66 
67 // Show fitted "sigma" for each slice
68  rightPad->cd(2);
69  gPad->SetTopMargin(0.12);
70  gPad->SetLeftMargin(0.15);
71  gPad->SetFillColor(33);
72  TH2F *hpxpy_2 = (TH2F*)hsimpleFile->Get("hpxpy_2");
73  hpxpy_2->SetMinimum(0.8);
74  hpxpy_2->Draw();
75 
76 //attributes
77  hpxpy_0->SetLineColor(5);
78  hpxpy_1->SetLineColor(5);
79  hpxpy_2->SetLineColor(5);
80  hpxpy_0->SetMarkerColor(2);
81  hpxpy_1->SetMarkerColor(2);
82  hpxpy_2->SetMarkerColor(2);
83  hpxpy_0->SetMarkerStyle(21);
84  hpxpy_1->SetMarkerStyle(21);
85  hpxpy_2->SetMarkerStyle(21);
86  hpxpy_0->SetMarkerSize(0.6);
87  hpxpy_1->SetMarkerSize(0.6);
88  hpxpy_2->SetMarkerSize(0.6);
89 }