Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hsimple.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This program creates :

  • a one dimensional histogram
  • a two dimensional histogram
  • a profile histogram
  • a memory-resident ntuple

These objects are filled with some random numbers and saved on a file. If get=1 the macro returns a pointer to the TFile of "hsimple.root" if this file exists, otherwise it is created. The file "hsimple.root" is created in $ROOTSYS/tutorials if the caller has write access to this directory, otherwise the file is created in $PWD

(TFile *) 0x5633bbc67480
#include <TFile.h>
#include <TNtuple.h>
#include <TH2.h>
#include <TProfile.h>
#include <TCanvas.h>
#include <TFrame.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TRandom3.h>
#include <TBenchmark.h>
#include <TInterpreter.h>
{
TString filename = "hsimple.root";
TString dir = gROOT->GetTutorialDir();
dir.ReplaceAll("/./","/");
TFile *hfile = nullptr;
if (getFile) {
// if the argument getFile =1 return the file "hsimple.root"
// if the file does not exist, it is created
TString fullPath = dir+"hsimple.root";
if (!gSystem->AccessPathName(fullPath,kFileExists)) {
hfile = TFile::Open(fullPath); //in $ROOTSYS/tutorials
if (hfile) return hfile;
}
//otherwise try $PWD/hsimple.root
if (!gSystem->AccessPathName("hsimple.root",kFileExists)) {
hfile = TFile::Open("hsimple.root"); //in current dir
if (hfile) return hfile;
}
}
//no hsimple.root file found. Must generate it !
//generate hsimple.root in current directory if we have write access
if (gSystem->AccessPathName(".",kWritePermission)) {
printf("you must run the script in a directory with write access\n");
return nullptr;
}
hfile = (TFile*)gROOT->FindObject(filename); if (hfile) hfile->Close();
hfile = new TFile(filename,"RECREATE","Demo ROOT file with histograms");
// Create some histograms, a profile histogram and an ntuple, add them to hfile
hpx->SetDirectory(hfile);
hpx->SetFillColor(48);
TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
hpxpy->SetDirectory(hfile);
TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
hprof->SetDirectory(hfile);
// Set to "true" if you want to see in the output how much time it took to fill the histogram:
const bool doBenchmark = false;
if (doBenchmark)
gBenchmark->Start("hsimple");
// Create a new canvas.
TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
c1->SetFillColor(42);
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(6);
c1->GetFrame()->SetBorderMode(-1);
// Fill histograms randomly
TRandom3 randomNum;
Float_t px, py, pz;
const Int_t kUPDATE = 1000;
for (Int_t i = 0; i < 25000; i++) {
randomNum.Rannor(px,py);
pz = px*px + py*py;
Float_t rnd = randomNum.Rndm();
hpx->Fill(px);
hpxpy->Fill(px,py);
hprof->Fill(px,pz);
ntuple->Fill(px,py,pz,rnd,i);
if (i && (i%kUPDATE) == 0) {
if (i == kUPDATE) hpx->Draw();
c1->Modified();
c1->Update();
if (gSystem->ProcessEvents())
break;
}
}
if (doBenchmark)
gBenchmark->Show("hsimple");
// Save all objects in this file
hpx->SetFillColor(0);
hfile->Write();
hpx->SetFillColor(48);
c1->Modified();
return hfile;
// Note that the file is automatically close when application terminates
// or when the file destructor is called.
}
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
Author
Rene Brun

Definition in file hsimple.C.