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

Detailed Description

View in nbviewer Open in SWAN
Illustrates how to use the TH1::FitSlicesY function It uses the TH2F histogram generated in macro hsimple.C It invokes FitSlicesY and draw the fitted "mean" and "sigma" in 2 sepate pads.

This macro shows also how to annotate a picture, change some pad parameters.

#include <TCanvas.h>
#include <TFile.h>
#include <TH2F.h>
#include <TROOT.h>
#include <TStyle.h>
void fitslicesy() {
// Change some default parameters in the current style
gStyle->SetLabelSize(0.06,"x");
gStyle->SetLabelSize(0.06,"y");
// Connect the input file and get the 2-d histogram in memory
TString dir = gROOT->GetTutorialDir();
dir.Append("/hsimple.C");
dir.ReplaceAll("/./","/");
if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
TFile *hsimpleFile = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
if (!hsimpleFile) return;
TH2F *hpxpy = (TH2F*)hsimpleFile->Get("hpxpy");
// Create a canvas and divide it
TCanvas *c1 = new TCanvas("c1","c1",700,500);
c1->SetFillColor(42);
c1->Divide(2,1);
TPad *leftPad = (TPad*)c1->cd(1);;
leftPad->Divide(1,2);
// Draw 2-d original histogram
leftPad->cd(1);
gPad->SetTopMargin(0.12);
gPad->SetFillColor(33);
hpxpy->Draw();
hpxpy->GetXaxis()->SetLabelSize(0.06);
hpxpy->GetYaxis()->SetLabelSize(0.06);
hpxpy->SetMarkerColor(kYellow);
// Fit slices projected along Y fron bins in X [7,32] with more than 20 bins in Y filled
hpxpy->FitSlicesY(nullptr,7,32,20);
// Show fitted "mean" for each slice
leftPad->cd(2);
gPad->SetFillColor(33);
TH2F *hpxpy_0 = (TH2F*)hsimpleFile->Get("hpxpy_0");
hpxpy_0->Draw();
TPad *rightPad = (TPad*)c1->cd(2);
rightPad->Divide(1,2);
rightPad->cd(1);
gPad->SetTopMargin(0.12);
gPad->SetLeftMargin(0.15);
gPad->SetFillColor(33);
TH2F *hpxpy_1 = (TH2F*)hsimpleFile->Get("hpxpy_1");
hpxpy_1->Draw();
// Show fitted "sigma" for each slice
rightPad->cd(2);
gPad->SetTopMargin(0.12);
gPad->SetLeftMargin(0.15);
gPad->SetFillColor(33);
TH2F *hpxpy_2 = (TH2F*)hsimpleFile->Get("hpxpy_2");
hpxpy_2->SetMinimum(0.8);
hpxpy_2->Draw();
//attributes
hpxpy_0->SetLineColor(5);
hpxpy_1->SetLineColor(5);
hpxpy_2->SetLineColor(5);
hpxpy_0->SetMarkerColor(2);
hpxpy_1->SetMarkerColor(2);
hpxpy_2->SetMarkerColor(2);
hpxpy_0->SetMarkerStyle(21);
hpxpy_1->SetMarkerStyle(21);
hpxpy_2->SetMarkerStyle(21);
hpxpy_0->SetMarkerSize(0.6);
hpxpy_1->SetMarkerSize(0.6);
hpxpy_2->SetMarkerSize(0.6);
}
@ kYellow
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gInterpreter
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
The most important graphics class in the ROOT system.
Definition TPad.h:28
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
Author
Rene Brun

Definition in file fitslicesy.C.