CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TopMuonFWLiteAnalyzer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <cstdlib>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 
11 
16 
17 
18 int main(int argc, char* argv[])
19 {
20  if( argc<3 ){
21  // -------------------------------------------------
22  std::cerr << "ERROR:: "
23  << "Wrong number of arguments! Please specify:" << std::endl
24  << " * filepath" << std::endl
25  << " * process name" << std::endl;
26  // -------------------------------------------------
27  return -1;
28  }
29 
30  // load framework libraries
31  gSystem->Load( "libFWCoreFWLite" );
33 
34  // set nice style for histograms
35  setNiceStyle();
36 
37  // define some histograms
38  TH1I* noMuons = new TH1I("noMuons", "N(Muon)", 10, 0 , 10 );
39  TH1I* noLepts = new TH1I("noLepts", "N(Lepton)", 10, 0 , 10 );
40  TH1F* ptMuons = new TH1F("ptMuons", "pt_{Muons}", 100, 0.,300.);
41  TH1F* enMuons = new TH1F("enMuons", "energy_{Muons}",100, 0.,300.);
42  TH1F* etaMuons = new TH1F("etaMuons","eta_{Muons}", 100, -3., 3.);
43  TH1F* phiMuons = new TH1F("phiMuons","phi_{Muons}", 100, -5., 5.);
44 
45  // -------------------------------------------------
46  std::cout << "open file: " << argv[1] << std::endl;
47  // -------------------------------------------------
48  TFile* inFile = TFile::Open(argv[1]);
49  TTree* events_= 0;
50  if( inFile ) inFile->GetObject("Events", events_);
51  if( events_==0 ){
52  // -------------------------------------------------
53  std::cerr << "ERROR:: "
54  << "Unable to retrieve TTree Events!" << std::endl
55  << " Eighter wrong file name or the the tree doesn't exists" << std::endl;
56  // -------------------------------------------------
57  return -1;
58  }
59 
60  // acess branch of muons and elecs
61  char muonName[50];
62  sprintf(muonName, "patMuons_selectedPatMuons__%s.obj", argv[2]);
63  TBranch* muons_ = events_->GetBranch( muonName ); assert( muons_!=0 );
64  char elecName[50];
65  sprintf(elecName, "patElectrons_selectedPatElectrons__%s.obj", argv[2]);
66  TBranch* elecs_ = events_->GetBranch( elecName ); assert( elecs_!=0 );
67 
68  // loop over events and fill histograms
69  std::vector<pat::Muon> muons;
70  std::vector<pat::Electron> elecs;
71  int nevt = events_->GetEntries();
72 
73  // -------------------------------------------------
74  std::cout << "start looping " << nevt << " events..." << std::endl;
75  // -------------------------------------------------
76  for(int evt=0; evt<nevt; ++evt){
77  // set branch address
78  muons_->SetAddress( &muons );
79  elecs_->SetAddress( &elecs );
80  // get event
81  muons_ ->GetEntry( evt );
82  elecs_ ->GetEntry( evt );
83  events_->GetEntry( evt, 0 );
84 
85  // -------------------------------------------------
86  if(evt>0 && !(evt%10)) std::cout << " processing event: " << evt << std::endl;
87  // -------------------------------------------------
88 
89  // fill histograms
90  noMuons->Fill(muons.size());
91  noLepts->Fill(muons.size()+elecs.size());
92  for(unsigned idx=0; idx<muons.size(); ++idx){
93  // fill histograms
94  ptMuons ->Fill(muons[idx].pt() );
95  enMuons ->Fill(muons[idx].energy());
96  etaMuons->Fill(muons[idx].eta() );
97  phiMuons->Fill(muons[idx].phi() );
98  }
99  }
100  // -------------------------------------------------
101  std::cout << "close file" << std::endl;
102  // -------------------------------------------------
103  inFile->Close();
104 
105  // save histograms to file
106  TFile outFile( "analyzeMuons.root", "recreate" );
107  outFile.mkdir("analyzeMuon");
108  outFile.cd("analyzeMuon");
109  noMuons ->Write( );
110  noLepts ->Write( );
111  ptMuons ->Write( );
112  enMuons ->Write( );
113  etaMuons->Write( );
114  phiMuons->Write( );
115  outFile.Close();
116 
117  // free allocated space
118  delete noMuons;
119  delete noLepts;
120  delete ptMuons;
121  delete enMuons;
122  delete etaMuons;
123  delete phiMuons;
124 
125  return 0;
126 }
void setNiceStyle()
Definition: NiceStyle.cc:3
T eta() const
int main(int argc, char **argv)
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
tuple argc
Definition: dir2webdir.py:38
tuple muons
Definition: patZpeak.py:38
static void enable()
enable automatic library loading
tuple cout
Definition: gather_cfg.py:121
Definition: DDAxes.h:10