Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
h2proj.C
Go to the documentation of this file.
1 // \file
2 /// \ingroup tutorial_hist
3 /// \notebook -js
4 /// This example demonstrates how to display a histogram and its two projections.
5 /// A TExec allows to redraw automatically the projections when a zoom is performed
6 /// on the 2D histogram.
7 ///
8 /// \macro_image
9 /// \macro_code
10 ///
11 /// \author Olivier Couet
12 
13 TH2F *h2;
14 TH1D * projh2X;
15 TH1D * projh2Y;
16 TPad *right_pad, *top_pad;
17 
18 void h2proj()
19 {
20  auto c1 = new TCanvas("c1", "c1",900,900);
21  gStyle->SetOptStat(0);
22 
23  TPad *center_pad = new TPad("center_pad", "center_pad",0.0,0.0,0.6,0.6);
24  center_pad->Draw();
25 
26  right_pad = new TPad("right_pad", "right_pad",0.55,0.0,1.0,0.6);
27  right_pad->Draw();
28 
29  top_pad = new TPad("top_pad", "top_pad",0.0,0.55,0.6,1.0);
30  top_pad->Draw();
31 
32  h2 = new TH2F("h2","",40,-4,4,40,-20,20);
33  Float_t px, py;
34  for (Int_t i = 0; i < 25000; i++) {
35  gRandom->Rannor(px,py);
36  h2->Fill(px,5*py);
37  }
38  projh2X = h2->ProjectionX();
39  projh2Y = h2->ProjectionY();
40 
41  center_pad->cd();
42  gStyle->SetPalette(1);
43  h2->Draw("COL");
44 
45  top_pad->cd();
46  projh2X->SetFillColor(kBlue+1);
47  projh2X->Draw("bar");
48 
49  right_pad->cd();
50  projh2Y->SetFillColor(kBlue-2);
51  projh2Y->Draw("hbar");
52 
53  c1->cd();
54  TLatex *t = new TLatex();
55  t->SetTextFont(42);
56  t->SetTextSize(0.02);
57  t->DrawLatex(0.6,0.88,"This example demonstrates how to display");
58  t->DrawLatex(0.6,0.85,"a histogram and its two projections.");
59 
60  auto ex = new TExec("zoom","ZoomExec()");
61  h2->GetListOfFunctions()->Add(ex);
62 }
63 
64 void ZoomExec()
65 {
66  int xfirst = h2->GetXaxis()->GetFirst();
67  int xlast = h2->GetXaxis()->GetLast();
68  double xmin = h2->GetXaxis()->GetBinLowEdge(xfirst);
69  double xmax = h2->GetXaxis()->GetBinUpEdge(xlast);
70  projh2X->GetXaxis()->SetRangeUser(xmin, xmax);
71  top_pad->Modified();
72 
73  int yfirst = h2->GetYaxis()->GetFirst();
74  int ylast = h2->GetYaxis()->GetLast();
75  double ymin = h2->GetYaxis()->GetBinLowEdge(yfirst);
76  double ymax = h2->GetYaxis()->GetBinUpEdge(ylast);
77  projh2Y->GetXaxis()->SetRangeUser(ymin, ymax);
78  right_pad->Modified();
79 }