Logo ROOT  
Reference Guide
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
13TH2F *h2;
14TH1D * projh2X;
15TH1D * projh2Y;
16TPad *right_pad, *top_pad;
17
18void h2proj()
19{
20 auto c1 = new TCanvas("c1", "c1",900,900);
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();
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()");
62}
63
64void 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}
int Int_t
Definition: RtypesCore.h:45
float Float_t
Definition: RtypesCore.h:57
@ kBlue
Definition: Rtypes.h:66
float xmin
Definition: THbookFile.cxx:95
float ymin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
float ymax
Definition: THbookFile.cxx:95
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:47
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition: TAxis.cxx:519
Int_t GetLast() const
Return last bin on the axis i.e.
Definition: TAxis.cxx:470
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates, that is,...
Definition: TAxis.cxx:979
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition: TAxis.cxx:529
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition: TAxis.cxx:459
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:28
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:617
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:319
TAxis * GetYaxis()
Definition: TH1.h:320
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
TList * GetListOfFunctions() const
Definition: TH1.h:242
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:257
TH1D * ProjectionY(const char *name="_py", Int_t firstxbin=0, Int_t lastxbin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along Y.
Definition: TH2.cxx:2429
TH1D * ProjectionX(const char *name="_px", Int_t firstybin=0, Int_t lastybin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along X.
Definition: TH2.cxx:2389
Int_t Fill(Double_t) override
Invalid Fill method.
Definition: TH2.cxx:358
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:1941
void Add(TObject *obj) override
Definition: TList.h:81
The most important graphics class in the ROOT system.
Definition: TPad.h:26
void Modified(Bool_t flag=1) override
Definition: TPad.h:413
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:605
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1291
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:500
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:1590
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1783
return c1
Definition: legend1.C:41
Double_t ex[n]
Definition: legend1.C:17