Page History
...
Data accumulation interface in ROOT
Root class headers
To accumulate statistics using ROOT in a standalone application one have to add headers
Code Block |
---|
// ROOT Class Headers -- #include "root/TFile.h" #include "root/TH1D.h" #include "root/TH2D.h" #include "root/TTree.h" #include "root/TBranch.h" #include "root/TRandom.h" // For random number generators. |
Histograms
A minimal interface for accumulation of histograms in ROOT is shown below.
Code Block |
---|
// Open output file TFile* pfile = new TFile("file.root", "RECREATE", "Created for you by RootManager" ); // Create histograms TH1D *pHis1 = new TH1D("pHis1","My comment to TH1D", 100, 0.5, 100.5); TH2D *pHis2 = new TH2D("pHis2","My comment to TH1D", 100, 0.5, 100.5, 200, 0.5, 200.5); // Fill histograms in each event pHis1 -> Fill( value, [weight] ); pHis2 -> Fill( x, y, [weight] ); // Write histograms in file in the very end pHis1 -> Write(); pHis2 -> Write(); // Close file pfile -> Close(); |
This interface allows to
- open file
file.root
in modeRECREATE
. - create 1d histogram with name
pHis1
, title"My comment to TH1D"
, with 100 bins in the range from 0.5 to 100.5; histogram is addressed by their pointer*pHis1
. - create 2d histogram, which is very similar to 1d, but, in addition, defines a second axis with 200 bins in the range from 0.5 to 200.5.
- fill histograms in each event; we assume that parameters
value
,x
,y
, and optionalweight
are defined. - save histogram(s) in file by the command like
pHis1 -> Write();
at the end of job. - and close file with accumulated histograms.
NTuple
Assuming that file is already open,
Code Block |
---|
// Define some structures for TTree typedef struct {float x,y,z;} POINT; static POINT point; float new_v; // Create TTree TTree * ptree = new TTree("ptree", "My comment to TTree"); // Create branch(es) TBranch *pbranch = ptree->Branch("new_v", &new_v, "new_v/F"); // or create branch without pointer ptree->Branch("point",&point,"x:y:z"); // Fill data structures for each event new_v = gRandom->Gaus(0, 1); point.x = gRandom->Gaus(1, 1); point.y = gRandom->Gaus(2, 1); point.z = gRandom->Gaus(3, 1); // Add an event record to the tree ptree->Fill(); // Write the tree in the file ptree -> Write(); |
These commands allow to
- create the "Tree" object with name
ptree
, title"My comment to TTree"
, and pointer*ptree
. - create the "Branch" object, representing a single variable of data structure, with name
new_v
, address of the data structure&new_v
, and the variable name/typenew_v/F
. - fill variables for each event, using commands like
new_v = gRandom->Gaus(0, 1);
. - save all the parameters in the Tree using command
ptree->Fill();
. - save the Tree in ROOT file by the command
ptree -> Write();
.
Code examples
Accumulation of data and saving in ROOT file
...
Overview
Content Tools