Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hsimple.C
Go to the documentation of this file.
1/// \file
2/// \ingroup Tutorials
3/// \notebook
4/// This program creates :
5/// - a one dimensional histogram
6/// - a two dimensional histogram
7/// - a profile histogram
8/// - a memory-resident ntuple
9///
10/// These objects are filled with some random numbers and saved on a file.
11/// If get=1 the macro returns a pointer to the TFile of "hsimple.root"
12/// if this file exists, otherwise it is created.
13/// The file "hsimple.root" is created in $ROOTSYS/tutorials if the caller has
14/// write access to this directory, otherwise the file is created in $PWD
15///
16/// \macro_image
17/// \macro_output
18/// \macro_code
19///
20/// \author Rene Brun
21
22#include <TFile.h>
23#include <TNtuple.h>
24#include <TH2.h>
25#include <TProfile.h>
26#include <TCanvas.h>
27#include <TFrame.h>
28#include <TROOT.h>
29#include <TSystem.h>
30#include <TRandom3.h>
31#include <TBenchmark.h>
32#include <TInterpreter.h>
33
35{
36 TString filename = "hsimple.root";
37 TString dir = gROOT->GetTutorialDir();
38 dir.ReplaceAll("/./","/");
39 TFile *hfile = nullptr;
40 if (getFile) {
41 // if the argument getFile =1 return the file "hsimple.root"
42 // if the file does not exist, it is created
43 TString fullPath = dir+"hsimple.root";
44 if (!gSystem->AccessPathName(fullPath,kFileExists)) {
45 hfile = TFile::Open(fullPath); //in $ROOTSYS/tutorials
46 if (hfile) return hfile;
47 }
48 //otherwise try $PWD/hsimple.root
49 if (!gSystem->AccessPathName("hsimple.root",kFileExists)) {
50 hfile = TFile::Open("hsimple.root"); //in current dir
51 if (hfile) return hfile;
52 }
53 }
54 //no hsimple.root file found. Must generate it !
55 //generate hsimple.root in current directory if we have write access
56 if (gSystem->AccessPathName(".",kWritePermission)) {
57 printf("you must run the script in a directory with write access\n");
58 return nullptr;
59 }
60 hfile = (TFile*)gROOT->FindObject(filename); if (hfile) hfile->Close();
61 hfile = new TFile(filename,"RECREATE","Demo ROOT file with histograms");
62
63 // Create some histograms, a profile histogram and an ntuple, add them to hfile
65 hpx->SetDirectory(hfile);
66 hpx->SetFillColor(48);
67 TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
68 hpxpy->SetDirectory(hfile);
69 TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
70 hprof->SetDirectory(hfile);
72
73 // Set to "true" if you want to see in the output how much time it took to fill the histogram:
74 const bool doBenchmark = false;
75
76 if (doBenchmark)
77 gBenchmark->Start("hsimple");
78
79 // Create a new canvas.
80 TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
81 c1->SetFillColor(42);
82 c1->GetFrame()->SetFillColor(21);
83 c1->GetFrame()->SetBorderSize(6);
84 c1->GetFrame()->SetBorderMode(-1);
85
86
87 // Fill histograms randomly
88 TRandom3 randomNum;
89 Float_t px, py, pz;
90 const Int_t kUPDATE = 1000;
91 for (Int_t i = 0; i < 25000; i++) {
92 randomNum.Rannor(px,py);
93 pz = px*px + py*py;
94 Float_t rnd = randomNum.Rndm();
95 hpx->Fill(px);
96 hpxpy->Fill(px,py);
97 hprof->Fill(px,pz);
98 ntuple->Fill(px,py,pz,rnd,i);
99 if (i && (i%kUPDATE) == 0) {
100 if (i == kUPDATE) hpx->Draw();
101 c1->Modified();
102 c1->Update();
103 if (gSystem->ProcessEvents())
104 break;
105 }
106 }
107 if (doBenchmark)
108 gBenchmark->Show("hsimple");
109
110 // Save all objects in this file
111 hpx->SetFillColor(0);
112 hfile->Write();
113 hpx->SetFillColor(48);
114 c1->Modified();
115 return hfile;
116
117 // Note that the file is automatically close when application terminates
118 // or when the file destructor is called.
119}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
externTBenchmark * gBenchmark
Definition TBenchmark.h:59
#define gROOT
Definition TROOT.h:417
@ kFileExists
Definition TSystem.h:52
@ kWritePermission
Definition TSystem.h:54
externTSystem * gSystem
Definition TSystem.h:582
The Canvas class.
Definition TCanvas.h:23
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) override
Write memory objects to this file.
Definition TFile.cxx:2488
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:3787
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:981
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:9074
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:363
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Profile Histogram.
Definition TProfile.h:32
Int_t Fill(const Double_t *v)
Definition TProfile.h:55
Random number generator class based on M.
Definition TRandom3.h:27
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom3.cxx:107
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:506
Basic string class.
Definition TString.h:138
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:713
return c1
Definition legend1.C:41