Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist019_TH2_projection.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/// \date January 2023
12/// \author Olivier Couet
13
14TH2F *h2;
18
20{
21 auto c1 = new TCanvas("c1", "c1", 900, 900);
23
24 TPad *center_pad = new TPad("center_pad", "center_pad", 0.0, 0.0, 0.6, 0.6);
25 center_pad->Draw();
26
27 right_pad = new TPad("right_pad", "right_pad", 0.55, 0.0, 1.0, 0.6);
28 right_pad->Draw();
29
30 top_pad = new TPad("top_pad", "top_pad", 0.0, 0.55, 0.6, 1.0);
31 top_pad->Draw();
32
33 h2 = new TH2F("h2", "", 40, -4, 4, 40, -20, 20);
34 Float_t px, py;
35 for (Int_t i = 0; i < 25000; i++) {
36 gRandom->Rannor(px, py);
37 h2->Fill(px, 5 * py);
38 }
39 projh2X = h2->ProjectionX();
40 projh2Y = h2->ProjectionY();
41
42 center_pad->cd();
44 h2->Draw("COL");
45
46 top_pad->cd();
47 projh2X->SetFillColor(kBlue + 1);
48 projh2X->Draw("bar");
49
50 right_pad->cd();
51 projh2Y->SetFillColor(kBlue - 2);
52 projh2Y->Draw("hbar");
53
54 c1->cd();
55 TLatex t;
56 t.SetTextFont(42);
57 t.SetTextSize(0.02);
58 t.DrawLatex(0.6, 0.88, "This example demonstrates how to display");
59 t.DrawLatex(0.6, 0.85, "a histogram and its two projections.");
60
61 auto ex = new TExec("zoom", "ZoomExec()");
62 h2->GetListOfFunctions()->Add(ex);
63}
64
65void ZoomExec()
66{
67 int xfirst = h2->GetXaxis()->GetFirst();
68 int xlast = h2->GetXaxis()->GetLast();
69 double xmin = h2->GetXaxis()->GetBinLowEdge(xfirst);
70 double xmax = h2->GetXaxis()->GetBinUpEdge(xlast);
71 projh2X->GetXaxis()->SetRangeUser(xmin, xmax);
72 top_pad->Modified();
73
74 int yfirst = h2->GetYaxis()->GetFirst();
75 int ylast = h2->GetYaxis()->GetLast();
76 double ymin = h2->GetYaxis()->GetBinLowEdge(yfirst);
77 double ymax = h2->GetYaxis()->GetBinUpEdge(ylast);
78 projh2Y->GetXaxis()->SetRangeUser(ymin, ymax);
79 right_pad->Modified();
80}
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
@ kBlue
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
float xmin
float ymin
float xmax
float ymax
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:48
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:49
The Canvas class.
Definition TCanvas.h:23
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad.
Definition TExec.h:26
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
To draw Mathematical Formula.
Definition TLatex.h:18
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
Definition TLatex.cxx:1943
The most important graphics class in the ROOT system.
Definition TPad.h:28
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:507
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1642
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1890
return c1
Definition legend1.C:41
Double_t ex[n]
Definition legend1.C:17