Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ntuple1.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 
24 void ntuple1() {
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) gInterpreter->LoadMacro(dir.Data());
38  TFile *f1 = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
39  if (!f1) return;
40  //
41  // Create a canvas, with 4 pads
42  //
43  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
44  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.48,0.98,21);
45  TPad *pad2 = new TPad("pad2","This is pad2",0.52,0.52,0.98,0.98,21);
46  TPad *pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.48,0.48,21);
47  TPad *pad4 = new TPad("pad4","This is pad4",0.52,0.02,0.98,0.48,1);
48  pad1->Draw();
49  pad2->Draw();
50  pad3->Draw();
51  pad4->Draw();
52  //
53  // Change default style for the statistics box
54  gStyle->SetStatW(0.30);
55  gStyle->SetStatH(0.20);
56  //
57  // Display a function of one ntuple column imposing a condition
58  // on another column.
59  pad1->cd();
60  pad1->SetGrid();
61  pad1->SetLogy();
62  TNtuple *ntuple = (TNtuple*)f1->Get("ntuple");
63  ntuple->SetLineColor(1);
64  ntuple->SetFillStyle(1001);
65  ntuple->SetFillColor(45);
66  ntuple->Draw("3*px+2","px**2+py**2>1");
67  ntuple->SetFillColor(38);
68  ntuple->Draw("2*px+2","pz>2","same");
69  ntuple->SetFillColor(5);
70  ntuple->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
71  pad1->RedrawAxis();
72  //
73  // Display the profile of two columns
74  // The profile histogram produced is saved in the current directory with
75  // the name hprofs
76  pad2->cd();
77  pad2->SetGrid();
78  ntuple->Draw("pz:px>>hprofs","","goffprofs");
79  TProfile *hprofs = (TProfile*)gDirectory->Get("hprofs");
80  hprofs->SetMarkerColor(5);
81  hprofs->SetMarkerSize(0.7);
82  hprofs->SetMarkerStyle(21);
83  hprofs->Fit("pol2");
84  // Get pointer to fitted function and modify its attributes
85  TF1 *fpol2 = hprofs->GetFunction("pol2");
86  fpol2->SetLineWidth(4);
87  fpol2->SetLineColor(2);
88  //
89  // Display a scatter plot of two columns with a selection.
90  // Superimpose the result of another cut with a different marker color
91  pad3->cd();
92  pad3->GetFrame()->SetBorderSize(8);
93  ntuple->SetMarkerColor(1);
94  ntuple->Draw("py:px","pz>1");
95  ntuple->SetMarkerColor(2);
96  ntuple->Draw("py:px","pz<1","same");
97  //
98  // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
99  pad4->cd();
100  ntuple->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
101  ntuple->SetMarkerColor(4);
102  ntuple->Draw("pz:py:px","pz<6 && pz>4","same");
103  ntuple->SetMarkerColor(5);
104  ntuple->Draw("pz:py:px","pz<4 && pz>3","same");
105  TPaveText *l4 = new TPaveText(-0.9,0.5,0.9,0.95);
106  l4->SetFillColor(42);
107  l4->SetTextAlign(12);
108  l4->AddText("You can interactively rotate this view in 2 ways:");
109  l4->AddText(" - With the RotateCube in clicking in this pad");
110  l4->AddText(" - Selecting View with x3d in the View menu");
111  l4->Draw();
112  //
113  c1->cd();
114  c1->Update();
115  gStyle->SetStatColor(19);
116  gBenchmark->Show("ntuple1");
117 }