Skip to content

Instantly share code, notes, and snippets.

@ram1123
Last active July 4, 2018 11:15
Show Gist options
  • Select an option

  • Save ram1123/906af346d4d7383732b77a7deea57d56 to your computer and use it in GitHub Desktop.

Select an option

Save ram1123/906af346d4d7383732b77a7deea57d56 to your computer and use it in GitHub Desktop.
Root Small Macros

(ROOT) Read Root TTree using TTreeReader and make histogram

#include "TFile.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
void TTreeReader_Macro() {
   // Create a histogram for the values we read.
   TH1F *h1 = new TH1F("h1", "ntuple", 100, 0, 2400);
   // Open the file containing the tree.
   TFile *myFile = TFile::Open("root:://cmseos.fnal.gov//eos/uscms/store/user/rasharma/SecondStep/WWTree_2018_01_03_14h54/HaddedFiles/WplusTo2JWminusToLNuJJ_EWK_LO_SM.root");
   // Create a TTreeReader for the tree, for instance by passing the
   // TTree's name and the TDirectory / TFile it is in. (otree is the name of tree)
   TTreeReader myReader("otree", myFile);
   // The branch "px" contains floats; access them as myPx.
   TTreeReaderValue<Float_t> myPx(myReader, "mass_lvj_type0");
   TTreeReaderValue<Float_t> btag(myReader, "btag0Wgt");
  
   // Loop over all entries of the TTree or TChain.
   while (myReader.Next()) {
      // Just access the data as if myPx and btag were iterators (note the '*'
      // in front of them):
      h1->Fill(*myPx);
      cout<<*btag<<endl;
   }
   h1->Draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment