Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist019_TH2_projection.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This example demonstrates how to display a histogram and its two projections.

A TExec allows to redraw automatically the projections when a zoom is performed on the 2D histogram.

TH2F *h2;
TH1D *projh2X;
TH1D *projh2Y;
TPad *right_pad, *top_pad;
void hist019_TH2_projection()
{
auto c1 = new TCanvas("c1", "c1", 900, 900);
gStyle->SetOptStat(0);
TPad *center_pad = new TPad("center_pad", "center_pad", 0.0, 0.0, 0.6, 0.6);
center_pad->Draw();
right_pad = new TPad("right_pad", "right_pad", 0.55, 0.0, 1.0, 0.6);
right_pad->Draw();
top_pad = new TPad("top_pad", "top_pad", 0.0, 0.55, 0.6, 1.0);
top_pad->Draw();
h2 = new TH2F("h2", "", 40, -4, 4, 40, -20, 20);
Float_t px, py;
for (Int_t i = 0; i < 25000; i++) {
gRandom->Rannor(px, py);
h2->Fill(px, 5 * py);
}
projh2X = h2->ProjectionX();
projh2Y = h2->ProjectionY();
center_pad->cd();
gStyle->SetPalette(1);
h2->Draw("COL");
top_pad->cd();
projh2X->SetFillColor(kBlue + 1);
projh2X->Draw("bar");
right_pad->cd();
projh2Y->SetFillColor(kBlue - 2);
projh2Y->Draw("hbar");
c1->cd();
TLatex t;
t.SetTextFont(42);
t.SetTextSize(0.02);
t.DrawLatex(0.6, 0.88, "This example demonstrates how to display");
t.DrawLatex(0.6, 0.85, "a histogram and its two projections.");
auto ex = new TExec("zoom", "ZoomExec()");
}
void ZoomExec()
{
int xfirst = h2->GetXaxis()->GetFirst();
int xlast = h2->GetXaxis()->GetLast();
double xmin = h2->GetXaxis()->GetBinLowEdge(xfirst);
double xmax = h2->GetXaxis()->GetBinUpEdge(xlast);
projh2X->GetXaxis()->SetRangeUser(xmin, xmax);
top_pad->Modified();
int yfirst = h2->GetYaxis()->GetFirst();
int ylast = h2->GetYaxis()->GetLast();
double ymin = h2->GetYaxis()->GetBinLowEdge(yfirst);
double ymax = h2->GetYaxis()->GetBinUpEdge(ylast);
projh2Y->GetXaxis()->SetRangeUser(ymin, ymax);
right_pad->Modified();
}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
@ kBlue
Definition Rtypes.h:67
float xmin
float ymin
float xmax
float ymax
externTRandom * gRandom
Definition TRandom.h:62
externTStyle * gStyle
Definition TStyle.h:442
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:52
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:522
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:473
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:1090
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:532
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition TAxis.cxx:462
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:926
TAxis * GetXaxis()
Definition TH1.h:571
TAxis * GetYaxis()
Definition TH1.h:572
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
TList * GetListOfFunctions() const
Definition TH1.h:488
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
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 (integration along X).
Definition TH2.cxx:2463
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 (integration along Y).
Definition TH2.cxx:2420
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:363
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
void Add(TObject *obj) override
Definition TList.h:81
The most important graphics class in the ROOT system.
Definition TPad.h:28
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7351
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:694
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1512
return c1
Definition legend1.C:41
Double_t ex[n]
Definition legend1.C:17
Date
January 2023
Author
Olivier Couet

Definition in file hist019_TH2_projection.C.