Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 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
16TList *InitGraphs(TNtuple *nt, TH1F *histo, Bool_t is_top)
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) g->SetPoint(g->GetN(), py[ie], px[ie]);
40 }
41
42 return list;
43}
44
45void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
46{
47 auto histo = dynamic_cast<TH1F *>(obj);
48 if(!histo) return;
49
50 TCanvas *Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
51 if (!Canvas2) return;
52 TGraph *gcommon = (TGraph *)Canvas2->FindObject("Graph");
53 if (!gcommon) return;
54
55 TList *list = nullptr;
56 if (!strcmp(histo->GetName(), "histo1"))
57 list = list1;
58 else if (!strcmp(histo->GetName(), "histo2"))
59 list = list2;
60 if (!list) return;
61 TGraph *g = (TGraph *)list->At(xhb);
62 if (!g) return;
63
64 TVirtualPad *savepad = gPad;
65 Canvas2->cd();
66 gcommon->Draw("AP");
67 //gcommon->SetTitle(TString::Format("%d / %d", g->GetN(), gcommon->GetN()));
68 if (histo->IsHighlight()) // don't draw g after highlight disabled
69 if (g->GetN() > 0)
70 g->Draw("P");
71 Canvas2->Update();
72 if (savepad) savepad->cd();
73}
74
75void hlHisto3()
76{
77 auto dir = gROOT->GetTutorialDir();
78 dir.Append("/hsimple.C");
79 dir.ReplaceAll("/./","/");
80 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
81 auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
82 if (!file) return;
83
84 TNtuple *ntuple;
85 file->GetObject("ntuple", ntuple);
86 if (!ntuple) return;
87 const char *cut = "pz > 3.0";
88
89 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 700, 500);
90 Canvas1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)");
91 Canvas1->Divide(1, 2);
92
93 TCanvas *Canvas2 = new TCanvas("Canvas2", "Canvas2", 705, 0, 500, 500);
94
95 // Case1, histo1, pz distribution
96 Canvas1->cd(1);
97 ntuple->Draw("pz>>histo1(100, 2.0, 12.0)", cut);
98 auto histo1 = (TH1F *)gPad->FindObject("histo1");
99 auto info1 = new TText(7.0, histo1->GetMaximum()*0.6,
100 "please move the mouse over the frame");
101 info1->SetTextColor(histo1->GetLineColor());
102 info1->SetBit(kCannotPick);
103 info1->Draw();
104
105 // Case2, histo2, px*py*pz distribution
106 Canvas1->cd(2);
107 ntuple->Draw("(px*py*pz)>>histo2(100, -50.0, 50.0)", cut);
108 auto histo2 = (TH1F *)gPad->FindObject("histo2");
109 histo2->SetLineColor(kGreen+2);
110 auto info2 = new TText(10.0, histo2->GetMaximum()*0.6, info1->GetTitle());
111 info2->SetTextColor(histo2->GetLineColor());
112 info2->SetBit(kCannotPick);
113 info2->Draw();
114 Canvas1->Update();
115
116 // set highlight after first drawing completed
117 histo1->SetHighlight();
118 histo2->SetHighlight();
119
120 // Common graph (all entries, all histo bins)
121 Canvas2->cd();
122 ntuple->Draw("px:py", cut);
123 auto gcommon = (TGraph *)gPad->FindObject("Graph");
124 gcommon->SetBit(kCanDelete, kFALSE); // will be redraw
125 auto htemp = (TH2F *)gPad->FindObject("htemp");
126 gcommon->SetTitle(htemp->GetTitle());
127 gcommon->GetXaxis()->SetTitle(htemp->GetXaxis()->GetTitle());
128 gcommon->GetYaxis()->SetTitle(htemp->GetYaxis()->GetTitle());
129 gcommon->Draw("AP");
130
131 // Must be last
132 ntuple->Draw("px:py:pz", cut, "goff");
133 list1 = InitGraphs(ntuple, histo1, true);
134 list2 = InitGraphs(ntuple, histo2, false);
135}
#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:101
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:80
@ kGreen
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define gInterpreter
@ kCanDelete
Definition TObject.h:369
@ kCannotPick
Definition TObject.h:374
#define gROOT
Definition TROOT.h:406
#define gPad
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:33
The Canvas class.
Definition TCanvas.h:23
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition TCanvas.cxx:1629
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2483
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
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:2319
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
TAxis * GetXaxis() const
Get x axis of the graph.
Definition TGraph.cxx:1544
TAxis * GetYaxis() const
Get y axis of the graph.
Definition TGraph.cxx:1553
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TGraph.cxx:1089
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2374
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
virtual Int_t GetNbinsX() const
Definition TH1.h:297
virtual Bool_t IsHighlight() const
Definition TH1.h:338
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:3668
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:295
A doubly linked list.
Definition TList.h:38
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:355
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
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
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:780
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1153
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2604
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
virtual Long64_t GetSelectedRows()
Definition TTree.h:513
void Draw(Option_t *opt) override
Default Draw method for all objects.
Definition TTree.h:431
virtual Double_t * GetV3()
Definition TTree.h:540
virtual Double_t * GetV1()
Definition TTree.h:536
virtual Double_t * GetV2()
Definition TTree.h:538
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0