Logo ROOT   6.14/05
Reference Guide
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 
34 TFile *hsimple(Int_t getFile=0)
35 {
36  TString filename = "hsimple.root";
37  TString dir = gROOT->GetTutorialDir();
38  dir.ReplaceAll("/./","/");
39  TFile *hfile = 0;
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
57  printf("you must run the script in a directory with write access\n");
58  return 0;
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  gBenchmark->Start("hsimple");
71 
72  // Create a new canvas.
73  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
74  c1->SetFillColor(42);
75  c1->GetFrame()->SetFillColor(21);
76  c1->GetFrame()->SetBorderSize(6);
77  c1->GetFrame()->SetBorderMode(-1);
78 
79 
80  // Fill histograms randomly
81  TRandom3 randomNum;
82  Float_t px, py, pz;
83  const Int_t kUPDATE = 1000;
84  for (Int_t i = 0; i < 25000; i++) {
85  randomNum.Rannor(px,py);
86  pz = px*px + py*py;
87  Float_t rnd = randomNum.Rndm();
88  hpx->Fill(px);
89  hpxpy->Fill(px,py);
90  hprof->Fill(px,pz);
91  ntuple->Fill(px,py,pz,rnd,i);
92  if (i && (i%kUPDATE) == 0) {
93  if (i == kUPDATE) hpx->Draw();
94  c1->Modified();
95  c1->Update();
96  if (gSystem->ProcessEvents())
97  break;
98  }
99  }
100  gBenchmark->Show("hsimple");
101 
102  // Save all objects in this file
103  hpx->SetFillColor(0);
104  hfile->Write();
105  hpx->SetFillColor(48);
106  c1->Modified();
107  return hfile;
108 
109  // Note that the file is automatically close when application terminates
110  // or when the file destructor is called.
111 }
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:1276
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:424
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:481
Random number generator class based on M.
Definition: TRandom3.h:27
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom3.cxx:100
float Float_t
Definition: RtypesCore.h:53
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:49
return c1
Definition: legend1.C:41
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
#define gROOT
Definition: TROOT.h:410
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
Basic string class.
Definition: TString.h:131
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
int Int_t
Definition: RtypesCore.h:41
Profile Histogram.
Definition: TProfile.h:32
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2823
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:50
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2336
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
Int_t Fill(const Double_t *v)
Definition: TProfile.h:54
The Canvas class.
Definition: TCanvas.h:31
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:170
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:291
void Modified(Bool_t flag=1)
Definition: TPad.h:414
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:917