Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In PSANA framework we need in package which allows to accumulate data in form of histograms and tuples and save them in file for further analysis. An official package which performs this job is PSHist. PSHist provides an abstract interface to the selected set of methods. Actual implementation of these methods is provided in the RootHist package, which is based on CERN's ROOT library. In case if user would need in full brut force of Root, we could suggest to use it directly. In this note we present only the basic interface,
equivalent to the PSHist, which allows to accumulate data save the file and browse this file interactively in Root.

Data accumulation interface in

...

ROOT

Root class 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"

...

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();

Code examples

Accumulation of data and saving in

...

ROOT-file

Code Block
//-----------------
// C/C++ Headers --
//-----------------
//#include <string>
#include <iostream>

using std::cout;
using std::endl;

//----------------------
// Root Class Headers --
//----------------------

#include "root/TROOT.h"
#include "root/TFile.h"
#include "root/TH1D.h"
#include "root/TTree.h"
#include "root/TBranch.h"
#include "root/TRandom.h"

int main ()
{
  cout << "StarsStart main()" << endl;

  //initialiseRoot();

  TFile *pfile = new TFile("file.root", "RECREATE", "Created for you by RootManager" );
  cout << "Open Root file with name : " << pfile ->GetName() << endl;


  cout << "Create histogram" << endl;
  TH1D *pHis1 = new TH1D("pHis1","My comment to TH1D", 100, 0.5, 100+0.5);


  cout << "Reset and fill histogram" << endl;
        pHis1 -> Reset();
        for (int i=0 ;i<10000;i++)
          {
            double random = 100 * gRandom->Rndm(1);
            //pHis1 -> Fill( double(i), 0.1*i );
            pHis1 -> Fill( random );

          }

  cout << "Write histogram in file" << endl;
        pHis1 -> Write();


// Define some simple structures
   typedef struct {float x,y,z;} POINT;
   static POINT point;

  cout << "Create tree" << endl;
//TTree *t3 = (TTree*)->Get("t3"); // if tuple existed
  TTree *ptree = new TTree("ptree", "My comment to TTree");

  cout << "Create  a couple of branches" << endl;
  float new_v;
  TBranch *pbranch = ptree->Branch("new_v", &new_v, "new_v/F");
                     ptree->Branch("point",&point,"x:y:z");

  cout << "Fill branch" << endl;
  for (int i = 0; i < 10000; i++){

      new_v   = gRandom->Gaus(0, 1);

      point.x = gRandom->Gaus(1, 1);
      point.y = gRandom->Gaus(2, 1);
      point.z = gRandom->Gaus(3, 1);

      ptree->Fill();
  }

  cout << "Write tree in file" << endl;
  ptree -> Write();

  cout << "Close file" << endl;
  pfile -> Close();

  return 0;

}

Execution of this program produces the Root ROOT file with name "file.root".

...

In order to plot histograms and data from Ntuple, accumulated in the file.root
one has to run Root ROOT interactively, or use a script. For example, we provide a script with name proc.C:

...