Tree 4

This example writes a tree with objects of the class Event. It is a simplified version of $ROOTSYS/test/MainEvent.cxx to write the tree, and $ROOTSYS/test/eventb.C It shows:

  • how to fill a Tree with an event class containing these data members:
    char           fType[20];
    Int_t          fNtrack;
    Int_t          fNseg;
    Int_t          fNvertex;
    UInt_t         fFlag;
    Float_t        fTemperature;
    EventHeader    fEvtHdr;
    TClonesArray  *fTracks;            //->
    TH1F          *fH;                 //->
    Int_t          fMeasures[10];
    Float_t        fMatrix[4][4];
    Float_t       *fClosestDistance;   //[fNvertex]
  • the difference in splitting or not splitting a branch
  • how to read selected branches of the tree, and print the first entry with less than 587 tracks.
  • how to browse and analyze the Tree via the TBrowser and TTreeViewer

This example can be run in many different ways:

  • way1 using the Cling interpreter:
    .x tree4.C
  • way2 using the Cling interpreter:
    .L tree4.C
    tree4()
  • way3 using ACLIC:
    .L ../test/libEvent.so
    .x tree4.C++
    One can also run the write and read parts in two separate sessions. For example following one of the sessions above, one can start the session:
    .L tree4.C
    tree4r();

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, February 16, 2020 at 04:35 AM.

In [1]:
R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)

#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
#include "TClassTable.h"
#include "TSystem.h"
#include "TROOT.h"
In [2]:
TString dir = "$ROOTSYS/test/Event.h";
gSystem->ExpandPathName(dir);
TString includeCommand = TString::Format("#include \"%s\"" , dir.Data());
gROOT->ProcessLine(includeCommand);

A helper function is created:

In [3]:
%%cpp -d
void tree4w()
{

  //create a Tree file tree4.root
  TFile f("tree4.root","RECREATE");

  // Create a ROOT Tree
  TTree t4("t4","A Tree with Events");

  // Create a pointer to an Event object
  Event *event = new Event();

  // Create two branches, split one.
  t4.Branch("event_split", &event,16000,99);
  t4.Branch("event_not_split", &event,16000,0);

  // a local variable for the event type
  char etype[20];

  // Fill the tree
  for (Int_t ev = 0; ev <100; ev++) {
    Float_t sigmat, sigmas;
    gRandom->Rannor(sigmat,sigmas);
    Int_t ntrack   = Int_t(600 + 600 *sigmat/120.);
    Float_t random = gRandom->Rndm(1);
    sprintf(etype,"type%d",ev%5);
    event->SetType(etype);
    event->SetHeader(ev, 200, 960312, random);
    event->SetNseg(Int_t(10*ntrack+20*sigmas));
    event->SetNvertex(Int_t(1+20*gRandom->Rndm()));
    event->SetFlag(UInt_t(random+0.5));
    event->SetTemperature(random+20.);

    for(UChar_t m = 0; m < 10; m++) {
      event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
    }

    // fill the matrix
    for(UChar_t i0 = 0; i0 < 4; i0++) {
      for(UChar_t i1 = 0; i1 < 4; i1++) {
        event->SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
      }
    }

    //  Create and fill the Track objects
    for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);

    // Fill the tree
    t4.Fill();

    // Clear the event before reloading it
    event->Clear();
  }

  // Write the file header
  f.Write();

  // Print the tree contents
  t4.Print();
}

A helper function is created:

In [4]:
%%cpp -d
void tree4r()
{
  // read the tree generated with tree4w

  //note that we use "new" to create the TFile and TTree objects !
  //because we want to keep these objects alive when we leave this function.
  TFile *f = new TFile("tree4.root");
  TTree *t4 = (TTree*)f->Get("t4");

  // create a pointer to an event object. This will be used
  // to read the branch values.
  Event *event = new Event();

  // get two branches and set the branch address
  TBranch *bntrack = t4->GetBranch("fNtrack");
  TBranch *branch  = t4->GetBranch("event_split");
  branch->SetAddress(&event);

  Long64_t nevent = t4->GetEntries();
  Int_t nselected = 0;
  Int_t nb = 0;
  for (Long64_t i=0;i<nevent;i++) {
    //read branch "fNtrack"only
    bntrack->GetEntry(i);

    //reject events with more than 587 tracks
    if (event->GetNtrack() > 587)continue;

    //read complete accepted event in memory
    nb += t4->GetEntry(i);
    nselected++;

    //print the first accepted event
    if (nselected == 1) t4->Show();

    //clear tracks array
    event->Clear();
  }

  if (gROOT->IsBatch()) return;
  new TBrowser();
  t4->StartViewer();
}
In [5]:
Event::Reset(); // Allow for re-run this script by cleaning static variables.
tree4w();
Event::Reset(); // Allow for re-run this script by cleaning static variables.
tree4r();
******************************************************************************
*Tree    :t4        : A Tree with Events                                     *
*Entries :      100 : Total =        15238524 bytes  File  Size =    7003179 *
*        :          : Tree compression factor =   2.17                       *
******************************************************************************
*Branch  :event_split                                                        *
*Entries :      100 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :fUniqueID : UInt_t                                                 *
*Entries :      100 : Total  Size=        987 bytes  File Size  =         97 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.89     *
*............................................................................*
*Br    1 :fBits     : UInt_t                                                 *
*Entries :      100 : Total  Size=       1375 bytes  File Size  =        285 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   3.08     *
*............................................................................*
*Br    2 :fType[20] : Char_t                                                 *
*Entries :      100 : Total  Size=       2991 bytes  File Size  =        357 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   6.95     *
*............................................................................*
*Br    3 :fEventName : char*                                                 *
*Entries :      100 : Total  Size=       1400 bytes  File Size  =        288 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   3.07     *
*............................................................................*
*Br    4 :fNtrack   : Int_t                                                  *
*Entries :      100 : Total  Size=        977 bytes  File Size  =        234 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.02     *
*............................................................................*
*Br    5 :fNseg     : Int_t                                                  *
*Entries :      100 : Total  Size=        967 bytes  File Size  =        289 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.63     *
*............................................................................*
*Br    6 :fNvertex  : Int_t                                                  *
*Entries :      100 : Total  Size=        982 bytes  File Size  =        226 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.09     *
*............................................................................*
*Br    7 :fFlag     : UInt_t                                                 *
*Entries :      100 : Total  Size=        967 bytes  File Size  =        151 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   3.11     *
*............................................................................*
*Br    8 :fTemperature : Double32_t                                          *
*Entries :      100 : Total  Size=       1002 bytes  File Size  =        462 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.03     *
*............................................................................*
*Br    9 :fMeasures[10] : Int_t                                              *
*Entries :      100 : Total  Size=       5011 bytes  File Size  =       1572 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.85     *
*............................................................................*
*Br   10 :fMatrix[4][4] : Double32_t                                         *
*Entries :      100 : Total  Size=       7409 bytes  File Size  =       4283 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.61     *
*............................................................................*
*Br   11 :fClosestDistance : Double32_t fClosestDistance[fNvertex]           *
*Entries :      100 : Total  Size=       4667 bytes  File Size  =       2452 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.64     *
*............................................................................*
*Br   12 :fEvtHdr   :                                                        *
*Entries :      100 : Total  Size=       2193 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      16000 bytes  Compression=   1.00     *
*............................................................................*
*Br   13 :fEvtHdr.fEvtNum : Int_t                                            *
*Entries :      100 : Total  Size=       1017 bytes  File Size  =        252 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.90     *
*............................................................................*
*Br   14 :fEvtHdr.fRun : Int_t                                               *
*Entries :      100 : Total  Size=       1002 bytes  File Size  =        104 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.59     *
*............................................................................*
*Br   15 :fEvtHdr.fDate : Int_t                                              *
*Entries :      100 : Total  Size=       1007 bytes  File Size  =        105 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.55     *
*............................................................................*
*Br   16 :fTracks   : Int_t fTracks_                                         *
*Entries :      100 : Total  Size=      24287 bytes  File Size  =        234 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.02     *
*............................................................................*
*Br   17 :fTracks.fUniqueID : UInt_t fUniqueID[fTracks_]                     *
*Entries :      100 : Total  Size=     242771 bytes  File Size  =      86240 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   2.81     *
*............................................................................*
*Br   18 :fTracks.fBits : UInt_t fBits[fTracks_]                             *
*Entries :      100 : Total  Size=     363563 bytes  File Size  =       5023 *
*Baskets :       25 : Basket Size=      16000 bytes  Compression=  72.18     *
*............................................................................*
*Br   19 :fTracks.fPx : Float_t fPx[fTracks_]                                *
*Entries :      100 : Total  Size=     242645 bytes  File Size  =     224693 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   1.08     *
*............................................................................*
*Br   20 :fTracks.fPy : Float_t fPy[fTracks_]                                *
*Entries :      100 : Total  Size=     242645 bytes  File Size  =     224605 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   1.08     *
*............................................................................*
*Br   21 :fTracks.fPz : Float_t fPz[fTracks_]                                *
*Entries :      100 : Total  Size=     242645 bytes  File Size  =     216001 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   1.12     *
*............................................................................*
*Br   22 :fTracks.fRandom : Float_t fRandom[fTracks_]                        *
*Entries :      100 : Total  Size=     242729 bytes  File Size  =       4364 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=  55.43     *
*............................................................................*
*Br   23 :fTracks.fMass2 : Float16_t fMass2[fTracks_]                        *
*Entries :      100 : Total  Size=     182266 bytes  File Size  =       2981 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=  60.89     *
*............................................................................*
*Br   24 :fTracks.fBx : Float16_t fBx[fTracks_]                              *
*Entries :      100 : Total  Size=     182215 bytes  File Size  =     136329 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.33     *
*............................................................................*
*Br   25 :fTracks.fBy : Float16_t fBy[fTracks_]                              *
*Entries :      100 : Total  Size=     182215 bytes  File Size  =     136216 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.33     *
*............................................................................*
*Br   26 :fTracks.fMeanCharge : Float_t fMeanCharge[fTracks_]                *
*Entries :      100 : Total  Size=     242813 bytes  File Size  =     218499 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   1.11     *
*............................................................................*
*Br   27 :fTracks.fXfirst : Float16_t fXfirst[fTracks_]                      *
*Entries :      100 : Total  Size=     182283 bytes  File Size  =     149583 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.21     *
*............................................................................*
*Br   28 :fTracks.fXlast : Float16_t fXlast[fTracks_]                        *
*Entries :      100 : Total  Size=     182266 bytes  File Size  =     149550 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.21     *
*............................................................................*
*Br   29 :fTracks.fYfirst : Float16_t fYfirst[fTracks_]                      *
*Entries :      100 : Total  Size=     182283 bytes  File Size  =     149608 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.21     *
*............................................................................*
*Br   30 :fTracks.fYlast : Float16_t fYlast[fTracks_]                        *
*Entries :      100 : Total  Size=     182266 bytes  File Size  =     149632 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.21     *
*............................................................................*
*Br   31 :fTracks.fZfirst : Float16_t fZfirst[fTracks_]                      *
*Entries :      100 : Total  Size=     182283 bytes  File Size  =     122031 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.49     *
*............................................................................*
*Br   32 :fTracks.fZlast : Float16_t fZlast[fTracks_]                        *
*Entries :      100 : Total  Size=     182266 bytes  File Size  =     114160 *
*Baskets :       13 : Basket Size=      16000 bytes  Compression=   1.59     *
*............................................................................*
*Br   33 :fTracks.fCharge : Double32_t fCharge[fTracks_]                     *
*Entries :      100 : Total  Size=     242729 bytes  File Size  =      35423 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   6.83     *
*............................................................................*
*Br   34 :fTracks.fVertex[3] : Double32_t fVertex[fTracks_]                  *
*Entries :      100 : Total  Size=     726562 bytes  File Size  =     427459 *
*Baskets :       50 : Basket Size=      16000 bytes  Compression=   1.70     *
*............................................................................*
*Br   35 :fTracks.fNpoint : Int_t fNpoint[fTracks_]                          *
*Entries :      100 : Total  Size=     242729 bytes  File Size  =      54334 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   4.45     *
*............................................................................*
*Br   36 :fTracks.fValid : Short_t fValid[fTracks_]                          *
*Entries :      100 : Total  Size=     121733 bytes  File Size  =      19658 *
*Baskets :        8 : Basket Size=      16000 bytes  Compression=   6.16     *
*............................................................................*
*Br   37 :fTracks.fNsp : Int_t fNsp[fTracks_]                                *
*Entries :      100 : Total  Size=     242666 bytes  File Size  =      35312 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   6.85     *
*............................................................................*
*Br   38 :fTracks.fPointValue : Double32_t fPointValue[fTracks_]             *
*Entries :      100 : Total  Size=     303081 bytes  File Size  =      42058 *
*Baskets :       20 : Basket Size=      16000 bytes  Compression=   7.18     *
*............................................................................*
*Br   39 :fTracks.fTriggerBits.fUniqueID : UInt_t fUniqueID[fTracks_]        *
*Entries :      100 : Total  Size=     243018 bytes  File Size  =       3697 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=  65.50     *
*............................................................................*
*Br   40 :fTracks.fTriggerBits.fBits : UInt_t fBits[fTracks_]                *
*Entries :      100 : Total  Size=     242934 bytes  File Size  =       3863 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=  62.67     *
*............................................................................*
*Br   41 :fTracks.fTriggerBits.fNbits : UInt_t fNbits[fTracks_]              *
*Entries :      100 : Total  Size=     242955 bytes  File Size  =      75345 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   3.21     *
*............................................................................*
*Br   42 :fTracks.fTriggerBits.fNbytes : UInt_t fNbytes[fTracks_]            *
*Entries :      100 : Total  Size=     242976 bytes  File Size  =      46214 *
*Baskets :       17 : Basket Size=      16000 bytes  Compression=   5.24     *
*............................................................................*
*Br   43 :fTracks.fTriggerBits.fAllBits : UChar_t fAllBits[fTracks_]         *
*Entries :      100 : Total  Size=     751152 bytes  File Size  =     275745 *
*Baskets :       50 : Basket Size=      16000 bytes  Compression=   2.72     *
*............................................................................*
*Br   44 :fHighPt   : TRefArray*                                             *
*Entries :      100 : Total  Size=     152073 bytes  File Size  =      60267 *
*Baskets :       10 : Basket Size=      16000 bytes  Compression=   2.51     *
*............................................................................*
*Br   45 :fMuons    : TRefArray*                                             *
*Entries :      100 : Total  Size=       5480 bytes  File Size  =        413 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=  12.06     *
*............................................................................*
*Br   46 :fLastTrack : TRef                                                  *
*Entries :      100 : Total  Size=       2200 bytes  File Size  =        645 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.61     *
*............................................................................*
*Br   47 :fWebHistogram : TRef                                               *
*Entries :      100 : Total  Size=       2215 bytes  File Size  =        315 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   5.35     *
*............................................................................*
*Br   48 :fH        : TH1F*                                                  *
*Entries :      100 : Total  Size=      96634 bytes  File Size  =       9568 *
*Baskets :        7 : Basket Size=      16000 bytes  Compression=  10.05     *
*............................................................................*
*Br   49 :fTriggerBits :                                                     *
*Entries :      100 : Total  Size=       3973 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      16000 bytes  Compression=   1.00     *
*............................................................................*
*Br   50 :fTriggerBits.fUniqueID : UInt_t                                    *
*Entries :      100 : Total  Size=       1052 bytes  File Size  =        110 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.43     *
*............................................................................*
*Br   51 :fTriggerBits.fBits : UInt_t                                        *
*Entries :      100 : Total  Size=       1440 bytes  File Size  =        298 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.99     *
*............................................................................*
*Br   52 :fTriggerBits.fNbits : UInt_t                                       *
*Entries :      100 : Total  Size=       1037 bytes  File Size  =        110 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.40     *
*............................................................................*
*Br   53 :fTriggerBits.fNbytes : UInt_t                                      *
*Entries :      100 : Total  Size=       1042 bytes  File Size  =        111 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   4.37     *
*............................................................................*
*Br   54 :fTriggerBits.fAllBits : UChar_t fTriggerBits.fAllBits[fNbytes]     *
*Entries :      100 : Total  Size=       1283 bytes  File Size  =        269 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   2.21     *
*............................................................................*
*Br   55 :fIsValid  : Bool_t                                                 *
*Entries :      100 : Total  Size=        682 bytes  File Size  =         94 *
*Baskets :        1 : Basket Size=      16000 bytes  Compression=   1.84     *
*............................................................................*
*Br   56 :event_not_split : Event                                            *
*Entries :      100 : Total  Size=    7874675 bytes  File Size  =    3799779 *
*Baskets :      100 : Basket Size=      16000 bytes  Compression=   2.07     *
*............................................................................*
======> EVENT:67
 event_split     = (Event*)0x7fe4edc273f0
 fUniqueID       = 0
 fBits           = 50331648
 fType[20]       = t , y , p , e , 2 ,