CountEvents.C
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
3 #include "TBranch.h"
4 #include "TTreeReader.h"
5 #include "TTreeReaderValue.h"
6 
7 void CountEvents()
8 {
9  // Variables used to store the data
10  Int_t totalSize = 0; // Sum of data size (in bytes) of all events
11 
12  // open the file
13  TFile *f = TFile::Open("http://lcg-heppkg.web.cern.ch/lcg-heppkg/ROOT/eventdata.root");
14  if (f == 0) {
15  // if we cannot open the file, print an error message and return immediatly
16  printf("Error: cannot open http://lcg-heppkg.web.cern.ch/lcg-heppkg/ROOT/eventdata.root!\n");
17  return;
18  }
19 
20  // Create a tree reader (of type Int_t) on the branch "fEventSize"
21  TTreeReader myReader("EventTree", f);
22  TTreeReaderValue<Int_t> eventSize(myReader, "fEventSize");
23 
24  // Loop over all entries of the TTree or TChain.
25  while (myReader.Next()) {
26  // Get the data from the current TTree entry by getting
27  // the value from the connected reader (eventSize):
28  totalSize += *eventSize;
29  }
30 
31  Int_t sizeInMB = totalSize/1024/1024;
32  printf("Total size of all events: %d MB\n", sizeInMB);
33 }
34 
FILE * f
Definition: loadlibs.C:26
void CountEvents()
Definition: CountEvents.C:7