Versions Compared

Key

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

...

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
//-----------------
// C/C++ Headers --
//-----------------
#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 << "Start main()" << endl;

  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); // Gaus(0, 1) generates a random number 
                                     // for normal distribution with mean=0 and sigma=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

...

Code Block
//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();
}

...

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