Logo ROOT  
Reference Guide
h1draw.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// 1-D histogram drawing options.
5/// We attach (or generate) the ROOT file in `$ROOTSYS/tutorials/hsimple.root`
6/// or `$PWD/hsimple.root`
7/// We draw one histogram in different formats.
8///
9/// \macro_image
10/// \macro_code
11///
12/// \author Rene Brun
13
14#include "TInterpreter.h"
15#include "TCanvas.h"
16#include "TSystem.h"
17#include "TFile.h"
18#include "TH2.h"
19#include "TNtuple.h"
20#include "TPaveLabel.h"
21#include "TPaveText.h"
22#include "TFrame.h"
23#include "TSystem.h"
24#include "TInterpreter.h"
25
26void h1draw()
27{
28 TString dir = gROOT->GetTutorialDir();
29 dir.Append("/hsimple.C");
30 dir.ReplaceAll("/./","/");
31 if (gBenchmark->GetBench("hsimple") < 0) gInterpreter->LoadMacro(dir.Data());
32 TFile *example = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
33 if (!example) return;
34
35 example->ls();
36 TH1 *hpx = (TH1*)example->Get("hpx");
37
38 TCanvas *c1 = new TCanvas("c1","Histogram Drawing Options",200,10,700,900);
39 TPad *pad1 = new TPad("pad1",
40 "The pad with the function",0.03,0.62,0.50,0.92);
41 TPad *pad2 = new TPad("pad2",
42 "The pad with the histogram",0.51,0.62,0.98,0.92);
43 TPad *pad3 = new TPad("pad3",
44 "The pad with the histogram",0.03,0.02,0.97,0.57);
45 pad1->Draw();
46 pad2->Draw();
47 pad3->Draw();
48
49 // Draw a global picture title
50 TPaveLabel *title = new TPaveLabel(0.1,0.94,0.9,0.98,
51 "Drawing options for one dimensional histograms");
52 title->SetTextFont(52);
53 title->Draw();
54
55 // Draw histogram hpx in first pad with the default option.
56 pad1->cd();
57 pad1->GetFrame()->SetFillColor(18);
58 hpx->SetFillColor(45);
59 hpx->DrawCopy();
60 TPaveLabel *label1 = new TPaveLabel(-3.5,700,-1,800,"Default option");
61 label1->Draw();
62
63 // Draw hpx as a lego. Clicking on the lego area will show
64 // a "transparent cube" to guide you rotating the lego in real time.
65 pad2->cd();
66 hpx->DrawCopy("lego1");
67 TPaveLabel *label2 = new TPaveLabel(-0.72,0.74,-0.22,0.88,"option Lego1");
68 label2->Draw();
69 TPaveLabel *label2a = new TPaveLabel(-0.93,-1.08,0.25,-0.92,
70 "Click on lego to rotate");
71 label2a->Draw();
72
73 // Draw hpx with its errors and a marker.
74 pad3->cd();
75 pad3->SetGridx();
76 pad3->SetGridy();
77 hpx->SetMarkerStyle(21);
78 hpx->Draw("e1p");
79 TPaveLabel *label3 = new TPaveLabel(2,600,3.5,650,"option e1p");
80 label3->Draw();
81
82 // The following illustrates how to add comments using a PaveText.
83 // Attributes of text/lines/boxes added to a PaveText can be modified.
84 // The AddText function returns a pointer to the added object.
85 TPaveText *pave = new TPaveText(-3.78,500,-1.2,750);
86 TText *t1=pave->AddText("You can move");
87 t1->SetTextColor(4);
88 t1->SetTextSize(0.05);
89 pave->AddText("Title and Stats pads");
90 pave->AddText("X and Y axis");
91 pave->AddText("You can modify bin contents");
92 pave->Draw();
93 c1->Update();
94}
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
#define gInterpreter
Definition: TInterpreter.h:555
#define gROOT
Definition: TROOT.h:415
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
Int_t GetBench(const char *name) const
Returns index of Benchmark name.
Definition: TBenchmark.cxx:108
The Canvas class.
Definition: TCanvas.h:31
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
The TH1 histogram class.
Definition: TH1.h:56
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:3045
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
The most important graphics class in the ROOT system.
Definition: TPad.h:29
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:331
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:332
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1282
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:591
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2841
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:182
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:233
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TString & Append(const char *cs)
Definition: TString.h:559
Base class for several text objects.
Definition: TText.h:23
return c1
Definition: legend1.C:41
Definition: h1draw.py:1
auto * t1
Definition: textangle.C:20