CoEPP RC
 

This is an old revision of the document!


Tips for fast processing of ROOT data

Only read data that will be used

Below is TTree processing code written in C++. The program produces a ROOT file that contains a histogram of the “MET” (Missing Transverse Momentum) distribution in top-quark pair events at the LHC.

#ifndef __CINT__

#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TChain.h"
#include "TString.h"

#include <vector>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>

#include "TFile.h"
#include "TH1D.h"
#include "TVectorF.h"


int main(int argc, char *argv[])
#else
int mytest5(void)
#endif
{


  //ADD root files
  TObjArray* datasetList = new TObjArray();
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000001.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000002.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000003.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000004.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000005.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000006.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000007.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000008.root.1"));
  datasetList->Add(new TObjString("root://xrdsydsr.syd.coepp.org.au///coepp/local/antonio/tmp8/mc11_7TeV.105200.T1_McAtNlo_Jimmy.merge.NTUP_TOP.e835_s1272_s1274_r3043_r2993_p937_tid758240_00/NTUP_TOP.758240._000009.root.1"));

  gROOT->ProcessLine("#include <vector>");

  //Declare and instantiate histograms
  TH1D* h_Met   = new TH1D("MET","E^{T}_{miss} [GeV]",  20,      0.   ,   200.    );

  //Load chain
  TChain*  myChain = new TChain("physics");

  //Add root files to my chain
  for (int d = 0; d < datasetList->GetEntries(); d++) {
    TString InputFileName((((TObjString*)((*datasetList)[d]))->GetString()));
    TFile *InFile = TFile::Open(InputFileName);
    std::cout << "Opening " << InputFileName << std::endl;

    if (gROOT->FindObject(myChain->GetName()) == 0) {
      std::cerr << myChain->GetName() << " is not found in :" << InputFileName << std::endl;
      return 1;
    }
    myChain->AddFile(InputFileName);
  }

  Long64_t nentries = myChain->GetEntries();
  //Turn off loading of all branches
  myChain->SetBranchStatus("*",0);

  //Only load branches used in the analysis
  Float_t mymet = 0;
  myChain->SetBranchStatus("MET_RefFinal_em_tightpp_et",1);
  myChain->SetBranchAddress("MET_RefFinal_em_tightpp_et",&mymet);

  //LOOP OVER EVENTS
  for (int i = 0; i < nentries  ; i++) {
    myChain->GetEntry(i);
    h_Met   -> Fill( (mymet)/1000.   , 1.0);
  }

  // Print the user's name:
  TFile OutFile("mytest.root","RECREATE");
  h_Met  ->Write();
  OutFile.Close("R");

  return 0;
}
tier3/faq/fast_rootdata_processing_tips.1395017515.txt.gz · Last modified: 2014/03/17 11:51 by antonio
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki