Logo ROOT   6.18/05
Reference Guide
hlHisto3.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3///
4/// This tutorial demonstrates how the highlight mechanism can be used on a ntuple.
5/// The ntuple in `hsimple.root` is drawn with three differents selection. Moving
6/// the mouse ove the two 1D representation display the on 2D plot the events
7/// contributing to the highlighted bin.
8///
9/// \macro_code
10///
11/// \date March 2018
12/// \author Jan Musinsky
13
14TList *list1 = 0;
15TList *list2 = 0;
16
17void InitGraphs(TNtuple *nt, TH1F *histo);
18void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
19
20
21void hlHisto3()
22{
23 auto dir = gROOT->GetTutorialDir();
24 dir.Append("/hsimple.C");
25 dir.ReplaceAll("/./","/");
26 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
27 auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
28 if (!file) return;
29
30 TNtuple *ntuple;
31 file->GetObject("ntuple", ntuple);
32 if (!ntuple) return;
33 const char *cut = "pz > 3.0";
34
35 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 700, 500);
36 Canvas1->Divide(1, 2);
37 TCanvas *Canvas2 = new TCanvas("Canvas2", "Canvas2", 705, 0, 500, 500);
38
39 // Case1, histo1, pz distribution
40 Canvas1->cd(1);
41 ntuple->Draw("pz>>histo1(100, 2.0, 12.0)", cut);
42 auto histo1 = (TH1F *)gPad->FindObject("histo1");
43 auto info1 = new TText(7.0, histo1->GetMaximum()*0.6,
44 "please move the mouse over the frame");
45 info1->SetTextColor(histo1->GetLineColor());
46 info1->SetBit(kCannotPick);
47 info1->Draw();
48
49 // Case2, histo2, px*py*pz distribution
50 Canvas1->cd(2);
51 ntuple->Draw("(px*py*pz)>>histo2(100, -50.0, 50.0)", cut);
52 auto histo2 = (TH1F *)gPad->FindObject("histo2");
53 histo2->SetLineColor(kGreen+2);
54 auto info2 = new TText(10.0, histo2->GetMaximum()*0.6, info1->GetTitle());
55 info2->SetTextColor(histo2->GetLineColor());
56 info2->SetBit(kCannotPick);
57 info2->Draw();
58 Canvas1->Update();
59
60 histo1->SetHighlight();
61 histo2->SetHighlight();
62 Canvas1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)");
63
64 // Common graph (all entries, all histo bins)
65 Canvas2->cd();
66 ntuple->Draw("px:py", cut);
67 auto gcommon = (TGraph *)gPad->FindObject("Graph");
68 gcommon->SetBit(kCanDelete, kFALSE); // will be redraw
69 auto htemp = (TH2F *)gPad->FindObject("htemp");
70 gcommon->SetTitle(htemp->GetTitle());
71 gcommon->GetXaxis()->SetTitle(htemp->GetXaxis()->GetTitle());
72 gcommon->GetYaxis()->SetTitle(htemp->GetYaxis()->GetTitle());
73 gcommon->Draw("AP");
74
75 // Must be last
76 ntuple->Draw("px:py:pz", cut, "goff");
77 histo1->SetUniqueID(1); // mark as case1
78 histo2->SetUniqueID(2); // mark as case2
79 InitGraphs(ntuple, histo1);
80 InitGraphs(ntuple, histo2);
81}
82
83
84void InitGraphs(TNtuple *nt, TH1F *histo)
85{
86 Long64_t nev = nt->GetSelectedRows();
87 Double_t *px = nt->GetV1();
88 Double_t *py = nt->GetV2();
89 Double_t *pz = nt->GetV3();
90
91 auto list = new TList();
92 if (histo->GetUniqueID() == 1) list1 = list;
93 else if (histo->GetUniqueID() == 2) list2 = list;
94 else return;
95
96 Int_t nbins = histo->GetNbinsX();
97 Int_t bin;
98 TGraph *g;
99 for (bin = 0; bin < nbins; bin++) {
100 g = new TGraph();
101 g->SetName(TString::Format("g%sbin_%d", histo->GetName(), bin+1));
102 g->SetBit(kCannotPick);
103 g->SetMarkerStyle(25);
104 g->SetMarkerColor(histo->GetLineColor());
105 list->Add(g);
106 }
107
108 Double_t value = 0.0;
109 for (Long64_t ie = 0; ie < nev; ie++) {
110 if (histo->GetUniqueID() == 1) value = pz[ie];
111 if (histo->GetUniqueID() == 2) value = px[ie]*py[ie]*pz[ie];
112 bin = histo->FindBin(value) - 1;
113 g = (TGraph *)list->At(bin);
114 if (!g) continue; // under/overflow
115 g->SetPoint(g->GetN(), py[ie], px[ie]); // reverse as px:py
116 }
117}
118
119
120void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
121{
122 auto histo = (TH1F *)obj;
123 if(!histo) return;
124
125 TCanvas *Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
126 if (!Canvas2) return;
127 TGraph *gcommon = (TGraph *)Canvas2->FindObject("Graph");
128 if (!gcommon) return;
129
130 TList *list = 0;
131 if (histo->GetUniqueID() == 1) list = list1; // case1
132 else if (histo->GetUniqueID() == 2) list = list2; // case2
133 if (!list) return;
134 TGraph *g = (TGraph *)list->At(xhb);
135 if (!g) return;
136
137 TVirtualPad *savepad = gPad;
138 Canvas2->cd();
139 gcommon->Draw("AP");
140 //gcommon->SetTitle(TString::Format("%d / %d", g->GetN(), gcommon->GetN()));
141 if (histo->IsHighlight()) // don't draw g after highlight disabled
142 if (g->GetN() > 0) g->Draw("P");
143 Canvas2->Update();
144 savepad->cd();
145}
#define g(i)
Definition: RSha256.hxx:105
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
@ kGreen
Definition: Rtypes.h:64
#define gInterpreter
Definition: TInterpreter.h:553
@ kCanDelete
Definition: TObject.h:339
@ kCannotPick
Definition: TObject.h:344
#define gROOT
Definition: TROOT.h:414
#define gPad
Definition: TVirtualPad.h:286
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
The Canvas class.
Definition: TCanvas.h:31
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition: TCanvas.cxx:1564
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2286
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:693
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual Int_t GetNbinsX() const
Definition: TH1.h:292
virtual Bool_t IsHighlight() const
Definition: TH1.h:330
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
Definition: TH1.cxx:3579
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:248
A doubly linked list.
Definition: TList.h:44
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:354
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
Mother of all ROOT objects.
Definition: TObject.h:37
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:375
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1166
virtual TObject * FindObject(const char *name) const
Search if object named name is inside this pad or in pads inside this pad.
Definition: TPad.cxx:2589
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2311
Base class for several text objects.
Definition: TText.h:23
virtual Long64_t GetSelectedRows()
Definition: TTree.h:452
virtual Double_t * GetV3()
Definition: TTree.h:479
virtual Double_t * GetV1()
Definition: TTree.h:475
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:371
virtual Double_t * GetV2()
Definition: TTree.h:477
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:50
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Definition: file.py:1