ROOT logo

From $ROOTSYS/tutorials/tree/tree4.C

#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
#include "TClassTable.h"
#include "TSystem.h"
#include "TROOT.h"
#if defined(__CINT__) && !defined(__MAKECINT__) 
#include "../test/libEvent.so"
#else 
#include "../test/Event.h"
#endif

// 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:  .x tree4.C    using the CINT interpreter
//  way2:  .L tree4.C
//          tree4()
//  way3:  .L ../test/libEvent.so
//         .x tree4.C++   using ACLIC
// 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();

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


void tree4r()
{
  // check to see if the event class is in the dictionary
  // if it is not load the definition in libEvent.so
  if (!TClassTable::GetDict("Event")) {
    gSystem->Load("$ROOTSYS/test/libEvent");
  }    
   
  // 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();
}   

void tree4() {
   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();
}
 tree4.C:1
 tree4.C:2
 tree4.C:3
 tree4.C:4
 tree4.C:5
 tree4.C:6
 tree4.C:7
 tree4.C:8
 tree4.C:9
 tree4.C:10
 tree4.C:11
 tree4.C:12
 tree4.C:13
 tree4.C:14
 tree4.C:15
 tree4.C:16
 tree4.C:17
 tree4.C:18
 tree4.C:19
 tree4.C:20
 tree4.C:21
 tree4.C:22
 tree4.C:23
 tree4.C:24
 tree4.C:25
 tree4.C:26
 tree4.C:27
 tree4.C:28
 tree4.C:29
 tree4.C:30
 tree4.C:31
 tree4.C:32
 tree4.C:33
 tree4.C:34
 tree4.C:35
 tree4.C:36
 tree4.C:37
 tree4.C:38
 tree4.C:39
 tree4.C:40
 tree4.C:41
 tree4.C:42
 tree4.C:43
 tree4.C:44
 tree4.C:45
 tree4.C:46
 tree4.C:47
 tree4.C:48
 tree4.C:49
 tree4.C:50
 tree4.C:51
 tree4.C:52
 tree4.C:53
 tree4.C:54
 tree4.C:55
 tree4.C:56
 tree4.C:57
 tree4.C:58
 tree4.C:59
 tree4.C:60
 tree4.C:61
 tree4.C:62
 tree4.C:63
 tree4.C:64
 tree4.C:65
 tree4.C:66
 tree4.C:67
 tree4.C:68
 tree4.C:69
 tree4.C:70
 tree4.C:71
 tree4.C:72
 tree4.C:73
 tree4.C:74
 tree4.C:75
 tree4.C:76
 tree4.C:77
 tree4.C:78
 tree4.C:79
 tree4.C:80
 tree4.C:81
 tree4.C:82
 tree4.C:83
 tree4.C:84
 tree4.C:85
 tree4.C:86
 tree4.C:87
 tree4.C:88
 tree4.C:89
 tree4.C:90
 tree4.C:91
 tree4.C:92
 tree4.C:93
 tree4.C:94
 tree4.C:95
 tree4.C:96
 tree4.C:97
 tree4.C:98
 tree4.C:99
 tree4.C:100
 tree4.C:101
 tree4.C:102
 tree4.C:103
 tree4.C:104
 tree4.C:105
 tree4.C:106
 tree4.C:107
 tree4.C:108
 tree4.C:109
 tree4.C:110
 tree4.C:111
 tree4.C:112
 tree4.C:113
 tree4.C:114
 tree4.C:115
 tree4.C:116
 tree4.C:117
 tree4.C:118
 tree4.C:119
 tree4.C:120
 tree4.C:121
 tree4.C:122
 tree4.C:123
 tree4.C:124
 tree4.C:125
 tree4.C:126
 tree4.C:127
 tree4.C:128
 tree4.C:129
 tree4.C:130
 tree4.C:131
 tree4.C:132
 tree4.C:133
 tree4.C:134
 tree4.C:135
 tree4.C:136
 tree4.C:137
 tree4.C:138
 tree4.C:139
 tree4.C:140
 tree4.C:141
 tree4.C:142
 tree4.C:143
 tree4.C:144
 tree4.C:145
 tree4.C:146
 tree4.C:147
 tree4.C:148
 tree4.C:149
 tree4.C:150
 tree4.C:151
 tree4.C:152
 tree4.C:153
 tree4.C:154
 tree4.C:155
 tree4.C:156
 tree4.C:157
 tree4.C:158
 tree4.C:159
 tree4.C:160
 tree4.C:161
 tree4.C:162
 tree4.C:163
 tree4.C:164
 tree4.C:165
 tree4.C:166
 tree4.C:167
 tree4.C:168