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.

...

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"

Histograms

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+0.5);
TH2D *pHis2 = new TH2D("pHis2","My comment to TH1D", 100, 0.5, 100+0.5,
                                                     200, 0.5, 200+0.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();

...

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 branchesbranch(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 toin the file
ptree -> Write();

...

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 << "Stars 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 file with name "file.root".

Browse the Root-file interactively and plot histograms

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

Code Block
Code Block

// To run this script use command: root -q -f proc.C

//void proc(int Nplot=1)
{
  // Settings for good style
   gStyle -> SetPadColor(3);
   gStyle -> SetPadBorderSize(0);
   gStyle -> SetPadBorderMode(0);
   gStyle -> SetTitleXSize(0.05); // set size of axes titles
   gStyle -> SetTitleYSize(0.05);
   gStyle -> SetTitleH(0.1); // set size of the title in top box
 
  TFile *f = new TFile("file.root");

  f->ls();
  ptree->Print();

  c1 = new TCanvas("c1","",0,0,500,500);
  c1->Divide(2,3);

  c1->cd(1); pHis1->Draw();
  c1->cd(2); ptree->Draw("new_v");
  c1->cd(3); ptree->Draw("x");
  c1->cd(4); ptree->Draw("y");
  c1->cd(5); ptree->Draw("z");

  gPad -> Update();

   c1->Print("test-histograms.gif");

  cout << "Sleep for 10 sec..." << endl;
  gSystem->Sleep(10*1000);
  cout << "Wake up!" << endl;

  f -> Close();
}

To run this script use command:

Code Block

root -q -f proc.C

This script draws a few histograms, as shown below, and save this plot in the test-histograms.gif file.