Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist045_Graphics_highlight_ntuple.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 different selections. Moving
6/// the mouse over 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 = nullptr, *list2 = nullptr;
15
17{
18 Long64_t nev = nt->GetSelectedRows();
19 Double_t *px = nt->GetV1();
20 Double_t *py = nt->GetV2();
21 Double_t *pz = nt->GetV3();
22
23 auto list = new TList();
24
25 Int_t nbins = histo->GetNbinsX();
26 for (Int_t bin = 0; bin < nbins; bin++) {
27 auto g = new TGraph();
28 g->SetName(TString::Format("g%sbin_%d", histo->GetName(), bin + 1));
29 g->SetBit(kCannotPick);
30 g->SetMarkerStyle(25);
31 g->SetMarkerColor(histo->GetLineColor());
32 list->Add(g);
33 }
34
35 for (Long64_t ie = 0; ie < nev; ie++) {
36 Double_t value = is_top ? pz[ie] : px[ie] * py[ie] * pz[ie];
37 Int_t bin = histo->FindBin(value) - 1;
38 auto g = dynamic_cast<TGraph *>(list->At(bin));
39 if (g)
40 g->SetPoint(g->GetN(), py[ie], px[ie]);
41 }
42
43 return list;
44}
45
47{
48 auto histo = dynamic_cast<TH1F *>(obj);
49 if (!histo)
50 return;
51
52 TCanvas *Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
53 if (!Canvas2)
54 return;
55 TGraph *gcommon = (TGraph *)Canvas2->FindObject("Graph");
56 if (!gcommon)
57 return;
58
59 TList *list = nullptr;
60 if (!strcmp(histo->GetName(), "histo1"))
61 list = list1;
62 else if (!strcmp(histo->GetName(), "histo2"))
63 list = list2;
64 if (!list)
65 return;
66 TGraph *g = (TGraph *)list->At(xhb);
67 if (!g)
68 return;
69
71 Canvas2->cd();
72 gcommon->Draw("AP");
73 // gcommon->SetTitle(TString::Format("%d / %d", g->GetN(), gcommon->GetN()));
74 if (histo->IsHighlight()) // don't draw g after highlight disabled
75 if (g->GetN() > 0)
76 g->Draw("P");
77 Canvas2->Update();
78 if (savepad)
79 savepad->cd();
80}
81
83{
84 auto dir = gROOT->GetTutorialDir();
85 dir.Append("/hsimple.C");
86 dir.ReplaceAll("/./", "/");
87 if (!gInterpreter->IsLoaded(dir.Data()))
88 gInterpreter->LoadMacro(dir.Data());
89 auto file = (TFile *)gROOT->ProcessLineFast("hsimple(1)");
90 if (!file)
91 return;
92
94 file->GetObject("ntuple", ntuple);
95 if (!ntuple)
96 return;
97 const char *cut = "pz > 3.0";
98
99 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 700, 500);
100 Canvas1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)");
101 Canvas1->Divide(1, 2);
102
103 TCanvas *Canvas2 = new TCanvas("Canvas2", "Canvas2", 705, 0, 500, 500);
104
105 // Case1, histo1, pz distribution
106 Canvas1->cd(1);
107 ntuple->Draw("pz>>histo1(100, 2.0, 12.0)", cut);
108 auto histo1 = (TH1F *)gPad->FindObject("histo1");
109 auto info1 = new TText(7.0, histo1->GetMaximum() * 0.6, "please move the mouse over the frame");
110 info1->SetTextColor(histo1->GetLineColor());
111 info1->SetBit(kCannotPick);
112 info1->Draw();
113
114 // Case2, histo2, px*py*pz distribution
115 Canvas1->cd(2);
116 ntuple->Draw("(px*py*pz)>>histo2(100, -50.0, 50.0)", cut);
117 auto histo2 = (TH1F *)gPad->FindObject("histo2");
118 histo2->SetLineColor(kGreen + 2);
119 auto info2 = new TText(10.0, histo2->GetMaximum() * 0.6, info1->GetTitle());
120 info2->SetTextColor(histo2->GetLineColor());
121 info2->SetBit(kCannotPick);
122 info2->Draw();
123 Canvas1->Update();
124
125 // set highlight after first drawing completed
126 histo1->SetHighlight();
127 histo2->SetHighlight();
128
129 // Common graph (all entries, all histo bins)
130 Canvas2->cd();
131 ntuple->Draw("px:py", cut);
132 auto gcommon = (TGraph *)gPad->FindObject("Graph");
133 gcommon->SetBit(kCanDelete, kFALSE); // will be redraw
134 auto htemp = (TH2F *)gPad->FindObject("htemp");
135 gcommon->SetTitle(htemp->GetTitle());
136 gcommon->GetXaxis()->SetTitle(htemp->GetXaxis()->GetTitle());
137 gcommon->GetYaxis()->SetTitle(htemp->GetYaxis()->GetTitle());
138 gcommon->Draw("AP");
139
140 // Must be last
141 ntuple->Draw("px:py:pz", cut, "goff");
142 list1 = InitGraphs(ntuple, histo1, true);
143 list2 = InitGraphs(ntuple, histo2, false);
144}
#define g(i)
Definition RSha256.hxx:105
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:69
@ kGreen
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define gInterpreter
@ kCanDelete
Definition TObject.h:367
@ kCannotPick
Definition TObject.h:372
#define gROOT
Definition TROOT.h:406
#define gPad
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:35
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:131
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2347
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
virtual Int_t GetNbinsX() const
Definition TH1.h:313
virtual Bool_t IsHighlight() const
Definition TH1.h:354
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:3649
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
A doubly linked list.
Definition TList.h:38
const char * GetName() const override
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:41
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:2378
Base class for several text objects.
Definition TText.h:22
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51