Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree120_ntuple.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook
4/// Simple tree analysis.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author Rene Brun
10
11#include "TCanvas.h"
12#include "TFile.h"
13#include "TNtuple.h"
14#include "TProfile.h"
15#include "TBenchmark.h"
16#include "TStyle.h"
17#include "TPaveText.h"
18#include "TFrame.h"
19#include "TF1.h"
20#include "TROOT.h"
21#include "TSystem.h"
22#include "TInterpreter.h"
23
24void tree120_ntuple()
25{
26 //just in case this script is executed multiple times
27 delete gROOT->GetListOfFiles()->FindObject("hsimple.root");
28 delete gROOT->GetListOfCanvases()->FindObject("c1");
29
30 gBenchmark->Start("ntuple1");
31 //
32 // Connect ROOT histogram/ntuple demonstration file
33 // generated by example $ROOTSYS/tutorials/hsimple.C.
34 TString dir = gROOT->GetTutorialDir();
35 dir.Append("/hsimple.C");
36 dir.ReplaceAll("/./","/");
37 if (gBenchmark->GetBench("hsimple") < 0)
38 gInterpreter->LoadMacro(dir.Data());
39 TFile *f1 = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
40 if (!f1)
41 return;
42 //
43 // Create a canvas, with 4 pads
44 //
45 auto c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
46 auto pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.48,0.98,21);
47 auto pad2 = new TPad("pad2","This is pad2",0.52,0.52,0.98,0.98,21);
48 auto pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.48,0.48,21);
49 auto pad4 = new TPad("pad4","This is pad4",0.52,0.02,0.98,0.48,1);
50 pad1->Draw();
51 pad2->Draw();
52 pad3->Draw();
53 pad4->Draw();
54 //
55 // Change default style for the statistics box
56 gStyle->SetStatW(0.30);
57 gStyle->SetStatH(0.20);
58 //
59 // Display a function of one ntuple column imposing a condition
60 // on another column.
61 pad1->cd();
62 pad1->SetGrid();
63 pad1->SetLogy();
64 auto ntuple = f1->Get<TNtuple>("ntuple");
65 ntuple->SetLineColor(1);
66 ntuple->SetFillStyle(1001);
67 ntuple->SetFillColor(45);
68 ntuple->Draw("3*px+2","px**2+py**2>1");
69 ntuple->SetFillColor(38);
70 ntuple->Draw("2*px+2","pz>2","same");
71 ntuple->SetFillColor(5);
72 ntuple->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
73 pad1->RedrawAxis();
74 //
75 // Display the profile of two columns
76 // The profile histogram produced is saved in the current directory with
77 // the name hprofs
78 pad2->cd();
79 pad2->SetGrid();
80 ntuple->Draw("pz:px>>hprofs","","goffprofs");
81 TProfile *hprofs = (TProfile*)gDirectory->Get("hprofs");
82 hprofs->SetMarkerColor(5);
83 hprofs->SetMarkerSize(0.7);
84 hprofs->SetMarkerStyle(21);
85 hprofs->Fit("pol2");
86 // Get pointer to fitted function and modify its attributes
87 TF1 *fpol2 = hprofs->GetFunction("pol2");
88 fpol2->SetLineWidth(4);
89 fpol2->SetLineColor(2);
90 //
91 // Display a scatter plot of two columns with a selection.
92 // Superimpose the result of another cut with a different marker color
93 pad3->cd();
94 pad3->GetFrame()->SetBorderSize(8);
95 ntuple->SetMarkerColor(1);
96 ntuple->Draw("py:px","pz>1");
97 ntuple->SetMarkerColor(2);
98 ntuple->Draw("py:px","pz<1","same");
99 //
100 // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
101 pad4->cd();
102 ntuple->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
103 ntuple->SetMarkerColor(4);
104 ntuple->Draw("pz:py:px","pz<6 && pz>4","same");
105 ntuple->SetMarkerColor(5);
106 ntuple->Draw("pz:py:px","pz<4 && pz>3","same");
107 auto l4 = new TPaveText(-0.9,0.5,0.9,0.95);
108 l4->SetFillColor(42);
109 l4->SetTextAlign(12);
110 l4->AddText("You can interactively rotate this view in 2 ways:");
111 l4->AddText(" - With the RotateCube in clicking in this pad");
112 l4->AddText(" - Selecting View with x3d in the View menu");
113 l4->Draw();
114 //
115 c1->cd();
116 c1->Update();
117 gStyle->SetStatColor(19);
118 gBenchmark->Show("ntuple1");
119}
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
#define gDirectory
Definition TDirectory.h:384
#define gInterpreter
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:45
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:39
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:41
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:46
Int_t GetBench(const char *name) const
Returns index of Benchmark name.
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:233
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:3893
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition TH1.cxx:9071
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
The most important graphics class in the ROOT system.
Definition TPad.h:28
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
Profile Histogram.
Definition TProfile.h:32
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 SetStatH(Float_t h=0.1)
Definition TStyle.h:400
void SetStatColor(Color_t color=19)
Definition TStyle.h:390
void SetStatW(Float_t w=0.19)
Definition TStyle.h:399
return c1
Definition legend1.C:41
TF1 * f1
Definition legend1.C:11