Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include <TCanvas.h>
18#include <TFile.h>
19#include <TH2F.h>
20#include <TROOT.h>
21#include <TStyle.h>
22
23void fitslicesy() {
24// Change some default parameters in the current style
25 gStyle->SetLabelSize(0.06,"x");
26 gStyle->SetLabelSize(0.06,"y");
28 gStyle->SetTitleW(0.6);
29 gStyle->SetTitleH(0.1);
30
31// Connect the input file and get the 2-d histogram in memory
32 TString dir = gROOT->GetTutorialDir();
33 dir.Append("/hsimple.C");
34 dir.ReplaceAll("/./","/");
35 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
36 TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
37 if (!hsimpleFile) return;
38 TH2F *hpxpy = (TH2F*)hsimpleFile->Get("hpxpy");
39
40// Create a canvas and divide it
41 TCanvas *c1 = new TCanvas("c1","c1",700,500);
42 c1->SetFillColor(42);
43 c1->Divide(2,1);
44 TPad *leftPad = (TPad*)c1->cd(1);;
45 leftPad->Divide(1,2);
46
47// Draw 2-d original histogram
48 leftPad->cd(1);
49 gPad->SetTopMargin(0.12);
50 gPad->SetFillColor(33);
51 hpxpy->Draw();
52 hpxpy->GetXaxis()->SetLabelSize(0.06);
53 hpxpy->GetYaxis()->SetLabelSize(0.06);
54 hpxpy->SetMarkerColor(kYellow);
55
56// Fit slices projected along Y fron bins in X [7,32] with more than 20 bins in Y filled
57 hpxpy->FitSlicesY(nullptr,7,32,20);
58
59// Show fitted "mean" for each slice
60 leftPad->cd(2);
61 gPad->SetFillColor(33);
62 TH2F *hpxpy_0 = (TH2F*)hsimpleFile->Get("hpxpy_0");
63 hpxpy_0->Draw();
64 TPad *rightPad = (TPad*)c1->cd(2);
65 rightPad->Divide(1,2);
66 rightPad->cd(1);
67 gPad->SetTopMargin(0.12);
68 gPad->SetLeftMargin(0.15);
69 gPad->SetFillColor(33);
70 TH2F *hpxpy_1 = (TH2F*)hsimpleFile->Get("hpxpy_1");
71 hpxpy_1->Draw();
72
73// Show fitted "sigma" for each slice
74 rightPad->cd(2);
75 gPad->SetTopMargin(0.12);
76 gPad->SetLeftMargin(0.15);
77 gPad->SetFillColor(33);
78 TH2F *hpxpy_2 = (TH2F*)hsimpleFile->Get("hpxpy_2");
79 hpxpy_2->SetMinimum(0.8);
80 hpxpy_2->Draw();
81
82//attributes
83 hpxpy_0->SetLineColor(5);
84 hpxpy_1->SetLineColor(5);
85 hpxpy_2->SetLineColor(5);
86 hpxpy_0->SetMarkerColor(2);
87 hpxpy_1->SetMarkerColor(2);
88 hpxpy_2->SetMarkerColor(2);
89 hpxpy_0->SetMarkerStyle(21);
90 hpxpy_1->SetMarkerStyle(21);
91 hpxpy_2->SetMarkerStyle(21);
92 hpxpy_0->SetMarkerSize(0.6);
93 hpxpy_1->SetMarkerSize(0.6);
94 hpxpy_2->SetMarkerSize(0.6);
95}
@ kYellow
Definition Rtypes.h:66
#define gInterpreter
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:203
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition TAttPad.cxx:129
The Canvas class.
Definition TCanvas.h:23
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
TAxis * GetXaxis()
Definition TH1.h:324
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:405
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
virtual void FitSlicesY(TF1 *f1=nullptr, Int_t firstxbin=0, Int_t lastxbin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray *arr=nullptr)
Project slices along Y in case of a 2-D histogram, then fit each slice with function f1 and make a hi...
Definition TH2.cxx:1081
The most important graphics class in the ROOT system.
Definition TPad.h:28
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1249
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:693
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
void SetFrameFillColor(Color_t color=1)
Definition TStyle.h:372
void SetTitleW(Float_t w=0)
Definition TStyle.h:415
void SetTitleH(Float_t h=0)
Definition TStyle.h:416
void SetLabelSize(Float_t size=0.04, Option_t *axis="X")
Set size of axis labels.
Definition TStyle.cxx:1444
return c1
Definition legend1.C:41