CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/TopQuarkAnalysis/Examples/bin/TopElecFWLiteAnalyzer.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <string>
00003 #include <cstdlib>
00004 #include <sstream>
00005 #include <fstream>
00006 #include <iostream>
00007 
00008 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00009 #include "DataFormats/PatCandidates/interface/Electron.h"
00010 
00011 #include "TopQuarkAnalysis/Examples/bin/NiceStyle.cc"
00012 #include "TopQuarkAnalysis/Examples/interface/RootSystem.h"
00013 #include "TopQuarkAnalysis/Examples/interface/RootHistograms.h"
00014 #include "TopQuarkAnalysis/Examples/interface/RootPostScript.h"
00015 
00016 
00017 int main(int argc, char* argv[]) 
00018 {
00019   if( argc<3 ){
00020     // ------------------------------------------------- 
00021     std::cerr << "ERROR:: " 
00022               << "Wrong number of arguments! Please specify:" << std::endl
00023               << "        * filepath" << std::endl
00024               << "        * process name" << std::endl; 
00025     // -------------------------------------------------  
00026     return -1;
00027   }
00028 
00029   // load framework libraries
00030   gSystem->Load( "libFWCoreFWLite" );
00031   AutoLibraryLoader::enable();
00032   
00033   // set nice style for histograms
00034   setNiceStyle();
00035 
00036   // define some histograms 
00037   TH1I* noElecs  = new TH1I("noElecs", "N_{Elecs}",      10,  0 , 10 );
00038   TH1F* ptElecs  = new TH1F("ptElecs", "pt_{Elecs}",    100,  0.,300.);
00039   TH1F* enElecs  = new TH1F("enElecs", "energy_{Elecs}",100,  0.,300.);
00040   TH1F* etaElecs = new TH1F("etaElecs","eta_{Elecs}",   100, -3.,  3.);
00041   TH1F* phiElecs = new TH1F("phiElecs","phi_{Elecs}",   100, -5.,  5.);  
00042   
00043   // -------------------------------------------------  
00044   std::cout << "open  file: " << argv[1] << std::endl;
00045   // -------------------------------------------------
00046   TFile* inFile = TFile::Open(argv[1]);
00047   TTree* events_= 0;
00048   if( inFile ) inFile->GetObject("Events", events_); 
00049   if( events_==0 ){
00050     // -------------------------------------------------  
00051     std::cerr << "ERROR:: " 
00052               << "Unable to retrieve TTree Events!" << std::endl
00053               << "        Eighter wrong file name or the the tree doesn't exists" << std::endl;
00054     // -------------------------------------------------  
00055     return -1;
00056   }
00057 
00058   // acess branch of elecs
00059   char elecName[50];
00060   sprintf(elecName, "patElectrons_selectedPatElectrons__%s.obj", argv[2]);
00061   TBranch* elecs_ = events_->GetBranch( elecName ); assert( elecs_!=0 );
00062   
00063   // loop over events and fill histograms
00064   std::vector<pat::Electron> elecs;
00065   int nevt = events_->GetEntries();
00066 
00067   // -------------------------------------------------  
00068   std::cout << "start looping " << nevt << " events..." << std::endl;
00069   // -------------------------------------------------
00070   for(int evt=0; evt<nevt; ++evt){
00071     // set branch address 
00072     elecs_->SetAddress( &elecs );
00073     // get event
00074     elecs_ ->GetEntry( evt );
00075     events_->GetEntry( evt, 0 );
00076 
00077     // -------------------------------------------------  
00078     if(evt>0 && !(evt%10)) std::cout << "  processing event: " << evt << std::endl;
00079     // -------------------------------------------------  
00080 
00081     // fill histograms
00082     noElecs->Fill(elecs.size());
00083     for(unsigned idx=0; idx<elecs.size(); ++idx){
00084       // fill histograms
00085       ptElecs ->Fill(elecs[idx].pt()    );
00086       enElecs ->Fill(elecs[idx].energy());
00087       etaElecs->Fill(elecs[idx].eta()   );
00088       phiElecs->Fill(elecs[idx].phi()   );
00089     }
00090   }
00091   // -------------------------------------------------  
00092   std::cout << "close file" << std::endl;
00093   // -------------------------------------------------
00094   inFile->Close();
00095 
00096   // save histograms to file
00097   TFile outFile( "analyzeElecs.root", "recreate" );
00098   outFile.mkdir("analyzeElec");
00099   outFile.cd("analyzeElec");
00100   noElecs ->Write( );
00101   ptElecs ->Write( );
00102   enElecs ->Write( );
00103   etaElecs->Write( );
00104   phiElecs->Write( );
00105   outFile.Close();
00106 
00107   // free allocated space
00108   delete noElecs;
00109   delete ptElecs;
00110   delete enElecs;
00111   delete etaElecs;
00112   delete phiElecs;
00113 
00114   return 0;
00115 }