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
34TFile *hsimple(Int_t getFile=0)
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";
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
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
64 TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
65 hpx->SetFillColor(48);
66 TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
67 TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
68 TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
69
70 // Set to "true" if you want to see in the output how much time it took to fill the histogram:
71 const bool doBenchmark = false;
72
73 if (doBenchmark)
74 gBenchmark->Start("hsimple");
75
76 // Create a new canvas.
77 TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
78 c1->SetFillColor(42);
79 c1->GetFrame()->SetFillColor(21);
80 c1->GetFrame()->SetBorderSize(6);
81 c1->GetFrame()->SetBorderMode(-1);
82
83
84 // Fill histograms randomly
86 Float_t px, py, pz;
87 const Int_t kUPDATE = 1000;
88 for (Int_t i = 0; i < 25000; i++) {
89 randomNum.Rannor(px,py);
90 pz = px*px + py*py;
91 Float_t rnd = randomNum.Rndm();
92 hpx->Fill(px);
93 hpxpy->Fill(px,py);
94 hprof->Fill(px,pz);
95 ntuple->Fill(px,py,pz,rnd,i);
96 if (i && (i%kUPDATE) == 0) {
97 if (i == kUPDATE) hpx->Draw();
98 c1->Modified();
99 c1->Update();
100 if (gSystem->ProcessEvents())
101 break;
102 }
103 }
104 if (doBenchmark)
105 gBenchmark->Show("hsimple");
106
107 // Save all objects in this file
108 hpx->SetFillColor(0);
109 hfile->Write();
110 hpx->SetFillColor(48);
111 c1->Modified();
112 return hfile;
113
114 // Note that the file is automatically close when application terminates
115 // or when the file destructor is called.
116}
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gROOT
Definition TROOT.h:414
@ kFileExists
Definition TSystem.h:52
@ kWritePermission
Definition TSystem.h:54
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
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:130
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:3764
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Profile Histogram.
Definition TProfile.h:32
Random number generator class based on M.
Definition TRandom3.h:27
Basic string class.
Definition TString.h:138
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:713
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1307
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:414
return c1
Definition legend1.C:41