ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hsimple.C
Go to the documentation of this file.
1 #include <TFile.h>
2 #include <TNtuple.h>
3 #include <TH2.h>
4 #include <TProfile.h>
5 #include <TCanvas.h>
6 #include <TFrame.h>
7 #include <TROOT.h>
8 #include <TSystem.h>
9 #include <TRandom3.h>
10 #include <TBenchmark.h>
11 #include <TInterpreter.h>
12 
14 {
15 // This program creates :
16 // - a one dimensional histogram
17 // - a two dimensional histogram
18 // - a profile histogram
19 // - a memory-resident ntuple
20 //
21 // These objects are filled with some random numbers and saved on a file.
22 // If get=1 the macro returns a pointer to the TFile of "hsimple.root"
23 // if this file exists, otherwise it is created.
24 // The file "hsimple.root" is created in $ROOTSYS/tutorials if the caller has
25 // write access to this directory, otherwise the file is created in $PWD
26 
27  TString filename = "hsimple.root";
28  TString dir = gSystem->UnixPathName(__FILE__);
29  dir.ReplaceAll("hsimple.C","");
30  dir.ReplaceAll("/./","/");
31  TFile *hfile = 0;
32  if (get) {
33  // if the argument get =1 return the file "hsimple.root"
34  // if the file does not exist, it is created
35  TString fullPath = dir+"hsimple.root";
36  if (!gSystem->AccessPathName(fullPath,kFileExists)) {
37  hfile = TFile::Open(fullPath); //in $ROOTSYS/tutorials
38  if (hfile) return hfile;
39  }
40  //otherwise try $PWD/hsimple.root
41  if (!gSystem->AccessPathName("hsimple.root",kFileExists)) {
42  hfile = TFile::Open("hsimple.root"); //in current dir
43  if (hfile) return hfile;
44  }
45  }
46  //no hsimple.root file found. Must generate it !
47  //generate hsimple.root in current directory if we have write access
49  printf("you must run the script in a directory with write access\n");
50  return 0;
51  }
52  hfile = (TFile*)gROOT->FindObject(filename); if (hfile) hfile->Close();
53  hfile = new TFile(filename,"RECREATE","Demo ROOT file with histograms");
54 
55  // Create some histograms, a profile histogram and an ntuple
56  TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
57  hpx->SetFillColor(48);
58  TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
59  TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
60  TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
61 
62  gBenchmark->Start("hsimple");
63 
64  // Create a new canvas.
65  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
66  c1->SetFillColor(42);
67  c1->GetFrame()->SetFillColor(21);
68  c1->GetFrame()->SetBorderSize(6);
69  c1->GetFrame()->SetBorderMode(-1);
70 
71 
72  // Fill histograms randomly
74  Float_t px, py, pz;
75  const Int_t kUPDATE = 1000;
76  for (Int_t i = 0; i < 25000; i++) {
77  random.Rannor(px,py);
78  pz = px*px + py*py;
79  Float_t rnd = random.Rndm(1);
80  hpx->Fill(px);
81  hpxpy->Fill(px,py);
82  hprof->Fill(px,pz);
83  ntuple->Fill(px,py,pz,rnd,i);
84  if (i && (i%kUPDATE) == 0) {
85  if (i == kUPDATE) hpx->Draw();
86  c1->Modified();
87  c1->Update();
88  if (gSystem->ProcessEvents())
89  break;
90  }
91  }
92  gBenchmark->Show("hsimple");
93 
94  // Save all objects in this file
95  hpx->SetFillColor(0);
96  hfile->Write();
97  hpx->SetFillColor(48);
98  c1->Modified();
99  return hfile;
100 
101 // Note that the file is automatically close when application terminates
102 // or when the file destructor is called.
103 }
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:1213
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
tuple ntuple
Definition: hsimple.py:39
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:420
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:460
Random number generator class based on M.
Definition: TRandom3.h:29
Float_t pz
Definition: hprod.C:33
tuple hfile
Definition: hsimple.py:30
tuple random
Definition: hsimple.py:62
float Float_t
Definition: RtypesCore.h:53
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:62
TCanvas * c1
Definition: legend1.C:2
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
TH2F * hpxpy
Definition: hcons.C:33
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
static const char * filename()
#define gROOT
Definition: TROOT.h:344
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
TProfile * hprof
Definition: hcons.C:34
Float_t py
Definition: hprod.C:33
Profile Historam.
Definition: TProfile.h:34
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1020
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:3851
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom3.cxx:94
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
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:2248
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
Int_t Fill(const Double_t *v)
Definition: TProfile.h:56
The Canvas class.
Definition: TCanvas.h:48
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void dir(char *path=0)
Definition: rootalias.C:30
virtual Int_t Fill()
[fNvar] Array of variables
Definition: TNtuple.cxx:168
Float_t px
Definition: hprod.C:33
TFile * hsimple(Int_t get=0)
Definition: hsimple.C:13
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
TH1F * hpx
Definition: hcons.C:32
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
void Modified(Bool_t flag=1)
Definition: TPad.h:407
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898