Access a TTree with the TTreeReader


Open a file which is located on the web. Build a TTreeReader and two TTreeReader values, one for the tracks and one for the event number.


In [1]:
auto f = TFile::Open("http://indico.cern.ch/event/395198/material/0/0.root");

TTreeReader myReader("events",f);
TTreeReaderValue<vector<ROOT::Math::PxPyPzEVector>> tracksRV(myReader, "tracks");
TTreeReaderValue<int> eventNumRV(myReader, "evtNum");

Loop over the events stored in the tree. Analyse the transverse momentum of tracks and identify the maximum one. Print the result every one hundred events.


In [2]:
double maxPt;
while(myReader.Next()){
   auto evtNum (*eventNumRV);

   auto tracks = *tracksRV;
   maxPt = -1;
   for (auto&& track : tracks){
      auto pt = track.Pt();
      if (pt>maxPt) maxPt = pt;
   }
   if (evtNum %100 == 0) {
      std::cout << "Processing event number " << evtNum << std::endl;
      std::cout << "Max pt is " << maxPt << std::endl;
   }

}


Processing event number 0
Max pt is 37.5882
Processing event number 100
Max pt is 32.405
Processing event number 200
Max pt is 34.6248
Processing event number 300
Max pt is 37.0227
Processing event number 400
Max pt is 35.2018
Processing event number 500
Max pt is 35.2578
Processing event number 600
Max pt is 43.0203
Processing event number 700
Max pt is 36.7313
Processing event number 800
Max pt is 34.0827
Processing event number 900
Max pt is 34.7578