Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
14 TList *list1 = 0;
15 TList *list2 = 0;
16 
17 void InitGraphs(TNtuple *nt, TH1F *histo);
18 void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
19 
20 
21 void 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 
84 void 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 
120 void 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 }