Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
h1ReadAndDraw.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// Read a 1-D histogram from a ROOT File and draw it.
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 h1ReadAndDraw()
27{
28 // Fetch and open the hsimple.root File
29 TFile *example = TFile::Open(gROOT->GetTutorialDir() + "/hsimple.root");
30
31 example->ls(); // Show the file contents
32 // Get the histogram from the ROOT File
33 TH1 *hpx = nullptr; // pointer to base class TH1 is enough
34 example->GetObject("hpx", hpx);
35
36 TCanvas *c1 = new TCanvas("c1","Histogram Drawing Options",200,10,700,900);
37 TPad *pad1 = new TPad("pad1",
38 "The pad with the function",0.03,0.62,0.50,0.92);
39 TPad *pad2 = new TPad("pad2",
40 "The pad with the histogram",0.51,0.62,0.98,0.92);
41 TPad *pad3 = new TPad("pad3",
42 "The pad with the histogram",0.03,0.02,0.97,0.57);
43 pad1->Draw();
44 pad2->Draw();
45 pad3->Draw();
46
47 // Draw a global picture title
48 TPaveLabel *title = new TPaveLabel(0.1,0.94,0.9,0.98,
49 "Drawing options for one dimensional histograms");
50 title->SetTextFont(52);
51 title->Draw();
52
53 // Draw histogram hpx in first pad with the default option.
54 pad1->cd();
55 pad1->GetFrame()->SetFillColor(18);
56 hpx->SetFillColor(45);
57 hpx->DrawCopy();
58 TPaveLabel *label1 = new TPaveLabel(-3.5,700,-1,800,"Default option");
59 label1->Draw();
60
61 // Draw hpx as a lego. Clicking on the lego area will show
62 // a "transparent cube" to guide you rotating the lego in real time.
63 pad2->cd();
64 hpx->DrawCopy("lego1");
65 TPaveLabel *label2 = new TPaveLabel(-0.72,0.74,-0.22,0.88,"option Lego1");
66 label2->Draw();
67 TPaveLabel *label2a = new TPaveLabel(-0.93,-1.08,0.25,-0.92,
68 "Click on lego to rotate");
69 label2a->Draw();
70
71 // Draw hpx with its errors and a marker.
72 pad3->cd();
73 pad3->SetGridx();
74 pad3->SetGridy();
75 hpx->SetMarkerStyle(21);
76 hpx->Draw("e1p");
77 TPaveLabel *label3 = new TPaveLabel(2,600,3.5,650,"option e1p");
78 label3->Draw();
79
80 // The following illustrates how to add comments using a PaveText.
81 // Attributes of text/lines/boxes added to a PaveText can be modified.
82 // The AddText function returns a pointer to the added object.
83 TPaveText *pave = new TPaveText(-3.78,500,-1.2,750);
84 TText *t1=pave->AddText("You can move");
85 t1->SetTextColor(4);
86 t1->SetTextSize(0.05);
87 pave->AddText("Title and Stats pads");
88 pave->AddText("X and Y axis");
89 pave->AddText("You can modify bin contents");
90 pave->Draw();
91 c1->Update();
92}
#define gROOT
Definition TROOT.h:406
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 SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4089
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3113
The most important graphics class in the ROOT system.
Definition TPad.h:28
void SetGridx(Int_t value=1) override
Definition TPad.h:333
void SetGridy(Int_t value=1) override
Definition TPad.h:334
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:597
TFrame * GetFrame() override
Get frame.
Definition TPad.cxx:2859
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1268
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
void Draw(Option_t *option="") override
Draw this pavelabel with its current attributes.
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.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
Base class for several text objects.
Definition TText.h:22
return c1
Definition legend1.C:41
auto * t1
Definition textangle.C:20