Basic root tutorial

Login to a machine that has root installed (and where you have data that you can use), example:

ssh -Y YourLogin@nyx

Start root:

root

Useful links

Some useful commands

  • .ls : shows contents of the currently open file
  • CollectionTree->Show(10); : shows contents of entry number 10
  • CollectionTree->Scan(); : scans ntuple contents (prints consecutively the events with all the corresponding variables and its entries. An argument can be given, and then only this variable will be scanned for all the events, example: CollectionTree->Scan("T2CaEta");
  • CollectionTree->Print(); : prints tree contents (the names of the ntuple variables, etc)

Ex1: Plot variables from an ntuple, apply simple cuts

You can copy paste the following lines into the root command line. Lines started with // are commented out.

//Open the ntuple
TFile f("/usr/local/public/tmp/data/ntuple.zee.root");
// create pointer to TTree
  TTree *t = (TTree*)f->Get("/usr/local/public/tmp/data/ntuple.zee.root");

//Plot some variables

//L2
CollectionTree->Draw("T2CaNclus");
CollectionTree->Draw("T2CaEta");
CollectionTree->Draw("T2CaPhi");

//Simple operations can be performed, for example:
CollectionTree->Draw("T2IdPhi0 - T2CaPhi");
// to sumperimpose a plot
CollectionTree->Draw("T2IdPhic - T2CaPhi","same");
//To compute ET
CollectionTree->Draw("T2CaEmE /cosh(abs(T2CaEta))");

//Cuts can be applied in the following way:
CollectionTree->Draw("T2IdPt");
// Plot only events with a Pt smaller than a thousand
CollectionTree->Draw("T2IdPt","abs(T2IdPt)<200000");
// Plot only events with absolute value of eta > 1.5 (barrel region)
CollectionTree->Draw("T2IdPt","abs(T2CaEta)<1.5");
// More than one cut can be performed simultaneously use logic operators, example: &&
CollectionTree->Draw("T2IdPt","abs(T2IdPt)<200000 && abs(T2CaEta)<1.5");


//2d plots:
CollectionTree->Draw("T2CaEmE:T2CaEta");
CollectionTree->Draw("abs(T2IdPt):T2CaEmE /cosh(abs(T2CaEta))","abs(T2IdPt)<200000");

//Example of offline reconstructed varible (transverse energy in EM calorimeter)
CollectionTree->Draw("Ele_cl_et");

Ex2: Use TriggerDecision to check if a trigger signature was satisfied:

//TriggerDecision
// Number of egamma candidates per event in EF for events that satisfy e25i signature
CollectionTree->Draw("eg_nc_EF","Trig_EF_e25i");
// Number of calorimeter clusters per event at L2 for events that satisfy e25i signature at L2
CollectionTree->Draw("T2CaNclus","Trig_L2_e25i");
// Number of RoI at L1 for events that satisfy e25i signature at L1
CollectionTree->Draw("L1Em_nRoI","Trig_L1_EM25I");
// Transverse energy at L2 for events that satisfy e25i signature
CollectionTree->Draw("T2CaEmE /cosh(abs(T2CaEta))","Trig_EF_e25i");

// Available trigger decission variables for release 12
CollectionTree->Draw("Trig_EF_e25i");
CollectionTree->Draw("Trig_EF_e15iEF_e15i");
CollectionTree->Draw("Trig_EF_e60");
CollectionTree->Draw("Trig_L2_e25i");
CollectionTree->Draw("Trig_L2_e15iL2_e15i");
CollectionTree->Draw("Trig_L2_e60");
CollectionTree->Draw("Trig_L1_2EM15I");
CollectionTree->Draw("Trig_L1_EM25I");
CollectionTree->Draw("Trig_L1_EM60");

Ex3: Load the ntuple variables, loop over the events and fill a histogram

Warning, it doesn't work with the most recent root versions (like 5.16....):w

Vamos a usar un macro Macro es un fichero con codigo root, que se corre desde root usando el comando .x Crea un fichero, por ejemplo llamalo: ejemplo1.cxx Copia en este fichero las siguientes lineas:

{
// LA NTUPLA CON LA Q QUERES TRABAJAR
TChain nSignal("CollectionTree");
//cuando quieras cambiar ntupla cambias el path aqui
nSignal.AddFile("/usr/local/public/tmp/data/ntuple.zee.root");// ntuple input
TChain* sg_ch;
sg_ch = &nSignal;

//Asi almacenas en la variable "entries_sg" el numero de entradas de la ntupla
Int_t entries_sg=sg_ch->GetEntries();
//imprimes el numero de entradas de la ntupla
cout <<"entries_sg: " << entries_sg << endl;

//una manera de definir un parametro:
#define NROI 5000;

//Asi cargas una variable de la ntupla
//donde nRoiL1 es el nombre de la variable en la ntupla
// y eroiL1 es el nombre que tu le quieres dar y que tienes que definir previamente.
// hay que anhadir & para un tipo especial de variables, (creo para enteros...)
Int_t eroiL1;
sg_ch->SetBranchAddress("L1Em_nRoI",&eroiL1);
//otro ejemplo para variables que sean vectores:
Float_t l1var1[NROI];   //L1 signal Calo-varibles
sg_ch->SetBranchAddress("L1Em_EmClus",l1var1);

//Ejemplo definir histograma:
//creas un histogramade 1 dimension con new TH1F debes dar los siguientes parametros:
// nombre de tu histograma (sera usado si lo almacenas e intentas recuperarlo)
// titulo largo que te ayuda a identificar lo que hay
// numero de bines del histograma
// valor inicial
// valor final
TH1F *chile = new TH1F("chile","mi primer histograma",100,0.,100.);
TH1F *eL1 = new TH1F("eL1","number of RoI at L1", 20, 0., 20.);
//esto es equivalente a lo anterior pero usando parametros:
Float_t bEt =0.5;
Float_t eEt =99.5;
Float_t iEt =1;
Int_t nbEt = (Int_t) ((eEt-bEt)/iEt) + 1;
TH1F *hL1pEtCS= new TH1F("hL1pEtCS","e+ Et cs (e- cs)",nbEt,(bEt-iEt/2.),(eEt+iEt/2.));

//loop sobre los eventos que hay en la ntupla
for(Int_t k=1; k<entries_sg;k++){
  //esto te carga en las variables los valores para un evento concreto
  sg_ch->GetEntry(k);
  eL1->Fill(eroiL1);
 //loop sobre los L1 !RoI
  for(Int_t j=0; j<eroiL1;j++){  
   //como llenar el histograma
   chile->Fill(l1var1[j]/1000.);
  }
}

chile->Draw();
eL1->Draw();
//macro end

}

En la linea de comandos de root ejecutalo usando:

.x ejemplo1.cxx

Ex4 Load ntuple, fill histograms

Copy the following lines (you can download the file below as well) into a file called for example ej4.cxx execute it in root using:
.x ej4.cxx

gROOT->Reset();

#include <vector>
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1.h>
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

  TFile* m_file = new TFile("/usr/local/public/tmp/data/ntuple.zee.root");
  TTree* fChain = (TTree*)m_file->Get("CollectionTree");

//Asi almacenas en la variable "entries_sg" el numero de entradas de la ntupla
Int_t entries_sg=fChain->GetEntries();
//imprimes el numero de entradas de la ntupla
cout <<"entries_sg: " << entries_sg << endl;

//Asi cargas una variable de la ntupla
//observa que hay dos tipos de variables entero, y un vector (cuya dimension viene dada por el entero anterior)
   Int_t           L1Em_nRoI;
   vector<float>   *L1Em_EmClus;
   TBranch        *b_L1Em_nRoI;   //!
   TBranch        *b_L1Em_EmClus;   //!
   L1Em_EmClus = 0;
   fChain->SetBranchAddress("L1Em_nRoI", &L1Em_nRoI, &b_L1Em_nRoI);
   fChain->SetBranchAddress("L1Em_EmClus", &L1Em_EmClus, &b_L1Em_EmClus);

//Ejemplo definir histograma:
//creas un histogramade 1 dimension con new TH1F debes dar los siguientes parametros:
// nombre de tu histograma (sera usado si lo almacenas e intentas recuperarlo)
// titulo largo que te ayuda a identificar lo que hay
// numero de bines del histograma
// valor inicial
// valor final
TH1F *eL1 = new TH1F("eL1","number of RoI at L1", 20, 0., 20.);
// pretend to be fancy, use parameters in your histo creation (equivalent to previous one):
Float_t bEt = 0.;
Float_t eEt = 100.;
Int_t nbEt = 100;
TH1F *EtL1 = new TH1F("EtL1","transverse energy at L1", nbEt, bEt, eEt);
//loop sobre los eventos que hay en la ntupla
for(Int_t k=1; k<entries_sg;k++){
  //esto te carga en las variables los valores para un evento concreto
  fChain->GetEntry(k);
  //como llenar el histograma para una variable entera, hay una por evento
  eL1->Fill(L1Em_nRoI); // fill the histogram
  if (k< 10) cout << L1Em_nRoI << endl; //ejemplo de debug printing
  // loop sobre los RoI del L1
  for(int j=0; j<L1Em_nRoI;j++){
    EtL1->Fill( (*L1Em_EmClus)[j]/1000. ); //fill histogram for a variable that is a vector
    if (k<10) cout << (*L1Em_EmClus)[j] << endl; //ejemplo de debug printing
  }
}

eL1->Draw();
EtL1->Draw();

//macro end
}

Ex5 Load some ntuple variables, apply some cuts fill histogram

{
// This example uses ntuple variables
// produces two histograms, one before any cuts
// other after reproducing L1 trigger cuts
//
gROOT->Reset();

#include <vector>
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1.h>
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

  TFile* m_file = new TFile("/usr/local/public/tmp/data/ntuple.zee.root");
  TTree* fChain = (TTree*)m_file->Get("CollectionTree");

//Asi almacenas en la variable "entries_sg" el numero de entradas de la ntupla
Int_t entries_sg=fChain->GetEntries();
//imprimes el numero de entradas de la ntupla
cout <<"entries_sg: " << entries_sg << endl;

//Asi cargas una variable de la ntupla
//observa que hay dos tipos de variables entero, y un vector (cuya dimension viene dada por el entero anterior)
   Int_t           L1Em_nRoI;
   vector<float>   *L1Em_EmClus;
   vector<float>   *L1Em_EmIsol;
   vector<float>   *L1Em_HdIsol;
   vector<float>   *L1Em_HdCore;
   vector<float>   *L1Em_eta;
   vector<float>   *L1Em_phi;

   TBranch        *b_L1Em_nRoI;   //!
   TBranch        *b_L1Em_EmClus;   //!
   TBranch        *b_L1Em_EmIsol;   //!
   TBranch        *b_L1Em_HdIsol;   //!
   TBranch        *b_L1Em_HdCore;   //!
   TBranch        *b_L1Em_eta;   //!
   TBranch        *b_L1Em_phi;   //!

// initialize
   L1Em_EmClus = 0;
   L1Em_EmIsol = 0;
   L1Em_HdIsol = 0;
   L1Em_HdCore = 0;
   L1Em_eta = 0;
   L1Em_phi = 0;

   fChain->SetBranchAddress("L1Em_nRoI", &L1Em_nRoI, &b_L1Em_nRoI);
   fChain->SetBranchAddress("L1Em_EmClus", &L1Em_EmClus, &b_L1Em_EmClus);
   fChain->SetBranchAddress("L1Em_EmIsol", &L1Em_EmIsol, &b_L1Em_EmIsol);
   fChain->SetBranchAddress("L1Em_HdIsol", &L1Em_HdIsol, &b_L1Em_HdIsol);
   fChain->SetBranchAddress("L1Em_HdCore", &L1Em_HdCore, &b_L1Em_HdCore);
   fChain->SetBranchAddress("L1Em_eta", &L1Em_eta, &b_L1Em_eta);
   fChain->SetBranchAddress("L1Em_phi", &L1Em_phi, &b_L1Em_phi);

//Ejemplo definir histograma:
//creas un histogramade 1 dimension con new TH1F debes dar los siguientes parametros:
// nombre de tu histograma (sera usado si lo almacenas e intentas recuperarlo)
// titulo largo que te ayuda a identificar lo que hay
// numero de bines del histograma
// valor inicial
// valor final
TH1F *eL1 = new TH1F("eL1","number of RoI at L1", 20, 0., 20.);
// pretend to be fancy, use parameters in your histo creation (equivalent to previous one):
Float_t bEt = 0.;
Float_t eEt = 100.;
Int_t nbEt = 100;
TH1F *EtL1 = new TH1F("EtL1","transverse energy at L1", nbEt, bEt, eEt);

Float_t bEta = -4.;
Float_t eEta = 4.;
Int_t nbEta = 80;
TH1F *EtaL1 = new TH1F("EtaL1","Eta at L1 (before cuts)", nbEta, bEta, eEta);
TH1F *EtaL1c = new TH1F("EtaL1c","Eta at L1 (after cuts)", nbEta, bEta, eEta);

//parameters for L1 cuts
Float_t CutEtL1 = 21500.;
Float_t CutEMIsolL1 = 3000;

//loop sobre los eventos que hay en la ntupla
for(Int_t k=1; k<entries_sg;k++){
  //esto te carga en las variables los valores para un evento concreto
  fChain->GetEntry(k);
  //como llenar el histograma para una variable entera, hay una por evento
  eL1->Fill(L1Em_nRoI); // fill the histogram
  if (k< 10) cout << L1Em_nRoI << endl; //ejemplo de debug printing
  // loop sobre los RoI del L1
  for(int j=0; j<L1Em_nRoI;j++){
    EtL1->Fill( (*L1Em_EmClus)[j]/1000. ); //fill histogram for a variable that is a vector
    EtaL1->Fill( (*L1Em_eta)[j] );
    if (k<10) cout << (*L1Em_EmClus)[j] << endl; //ejemplo de debug printing
    if ((*L1Em_EmClus)[j] <= CutEtL1) {
      if (k< 10) cout << "L1 Et cut not satisfied: " << (*L1Em_EmClus)[j] << " < " << CutEtL1 << endl; //ejemplo de debug printing
      EtaL1c->Fill( (*L1Em_eta)[j] );
    }
  }
}

eL1->Draw();
EtL1->Draw();
EtaL1->Draw();
EtaL1c->Draw();

//macro end
}

Ex6 Write histograms into a file

//creas/sobreescribes un fichero donde almacenar histograms
{
TFile f("AllMyHistos1.root", "recreate");
eL1->Write();
EtL1->Write();
EtaL1->Write();
EtaL1c->Write();
}

---++ Ex 7 retrieve histograms from a file, do some plots, divide two histograms

<verbatim>
{
//retrieve histograms
//opens file
  gROOT->Reset();
  TFile *f2 = new TFile("AllMyHistos1.root");
  f2->ls();
//Retrieves histograms
  TH1F *h1= (TH1F *)f2->Get("eL1");
  TH1F *h2= (TH1F *)f2->Get("EtL1");
  TH1F *h3= (TH1F *)f2->Get("EtaL1");
  TH1F *h4= (TH1F *)f2->Get("EtaL1c");

//superimpose two plots
h3->SetLineColor(4);
h3->Draw();
h4->SetLineColor(2);
h4->Draw("same");

// create a new file, fill it with the division of other two files:
Float_t bEta = -4.;
Float_t eEta = 4.;
Int_t nbEta = 80;
TH1F *EtaL1d = new TH1F("EtaL1d","Eta at L1 after cuts/before", nbEta, bEta, eEta);
EtaL1d->Divide(EtaL1c,EtaL1,1.,1.,option="B");
EtaL1d->Draw();
}

</verbatim>
---++ Ex8 Produce a .ps, .eps and .pdf file with your plots
//creat .ps, .eps, .pdf file
{
gStyle->SetOptStat(111111);

TCanvas *cIdeal = new TCanvas("cIdeal", "L1 to begin with");

cIdeal->Divide(2,2);

cIdeal->cd(1);
//h0negL2->DrawNormalized();
eL1->Draw();
cIdeal->cd(2);
EtL1->Draw();
cIdeal->cd(3);
EtaL1->Draw();
cIdeal->cd(4);
EtaL1c->Draw();

//Printing to  .ps file

cIdeal->Print("plotsL1.ps", "Landscape");
cIdeal->Print("plotsL1.eps");
cIdeal->Print("plotsL1.pdf");

//invoke ggv
//gSystem->Exec("ggv Ideal_histos2.ps");


}


-- TeresaFonsecaMartin - 19 Nov 2007
  • ej4.cxx: Load ntuple variables, fill histograms

  • ej5.cxx: Load ntuple variables, apply cut, fill histograms

  • ej6.cxx: write histograms into a file
Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatcxx ej4.cxx r1 manage 2.2 K 2007-11-23 - 19:38 TeresaFonsecaMartin Load ntuple variables, fill histograms
Unknown file formatcxx ej5.cxx r1 manage 3.7 K 2007-11-23 - 19:39 TeresaFonsecaMartin Load ntuple variables, apply cut, fill histograms
Unknown file formatcxx ej6.cxx r1 manage 0.2 K 2007-11-23 - 19:39 TeresaFonsecaMartin write histograms into a file
Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r4 - 2007-11-23 - TeresaFonsecaMartin
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright &© 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback