Logo ROOT  
Reference Guide
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

hsimple : Real Time = 0.07 seconds Cpu Time = 0.07 seconds
(TFile *) 0x55afcf8c3180
#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>
TFile *hsimple(Int_t getFile=0)
{
TString filename = "hsimple.root";
TString dir = gROOT->GetTutorialDir();
dir.ReplaceAll("/./","/");
TFile *hfile = 0;
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
printf("you must run the script in a directory with write access\n");
return 0;
}
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
TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
hpx->SetFillColor(48);
TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
TProfile *hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
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();
break;
}
}
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.
}
Author
Rene Brun

Definition in file hsimple.C.

hsimple
Definition: hsimple.py:1
TNtuple
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
TBenchmark::Start
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
Float_t
float Float_t
Definition: RtypesCore.h:57
TFile::Open
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:3994
Int_t
int Int_t
Definition: RtypesCore.h:45
TCanvas.h
TProfile.h
TString
Basic string class.
Definition: TString.h:136
TSystem::AccessPathName
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:1294
TFile::Write
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsiz=0) override
Write memory objects to this file.
Definition: TFile.cxx:2349
TFile.h
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TNtuple.h
TROOT.h
TRandom::Rannor
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:500
TSystem.h
TRandom3
Random number generator class based on M.
Definition: TRandom3.h:27
kWritePermission
@ kWritePermission
Definition: TSystem.h:46
gBenchmark
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
TProfile::Fill
Int_t Fill(const Double_t *v)
Definition: TProfile.h:55
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3327
TRandom3.h
TBenchmark.h
TH2.h
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:294
TProfile
Profile Histogram.
Definition: TProfile.h:32
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TRandom3::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom3.cxx:99
TInterpreter.h
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
TFile::Close
void Close(Option_t *option="") override
Close a file.
Definition: TFile.cxx:876
kFileExists
@ kFileExists
Definition: TSystem.h:44
TFrame.h
kUPDATE
@ kUPDATE
Definition: TFitParametersDialog.cxx:45
TBenchmark::Show
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
TSystem::ProcessEvents
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:417
TNtuple::Fill
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:169
gROOT
#define gROOT
Definition: TROOT.h:406
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050
c1
return c1
Definition: legend1.C:41