CMS 3D CMS Logo

PatMcMatchFWLiteAnalyzer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <vector>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 
8 #include <TH2F.h> // Use the correct histograms
9 #include <TROOT.h>
10 #include <TFile.h>
11 #include <TSystem.h>
12 
14 #include "DataFormats/PatCandidates/interface/Muon.h" // Include the examined data formats
15 #include "DataFormats/PatCandidates/interface/Jet.h" // Include the examined data formats
17 
18 
19 int main(int argc, char* argv[])
20 {
21  // ----------------------------------------------------------------------
22  // First Part:
23  //
24  // * enable FWLite
25  // * book the histograms of interest
26  // * open the input file
27  // ----------------------------------------------------------------------
28 
29  // load framework libraries
30  gSystem->Load( "libFWCoreFWLite" );
32 
33  // book a set of histograms
34  TH2F* muonPt_ = new TH2F( "muonPt", "Muon Pt", 60, 0., 300., 60, 0., 300. ); // 2-D histo for muon Pt
35  muonPt_->SetXTitle( "gen." );
36  muonPt_->SetYTitle( "reco." );
37  TH2F* jetPt_ = new TH2F( "jetPt", "Jet Pt", 100, 0., 500., 100, 0., 500. ); // 2-D histo for jet Pt
38  jetPt_->SetXTitle( "gen." );
39  jetPt_->SetYTitle( "reco." );
40 
41  // open input file (can be located on castor)
42  TFile* inFile = TFile::Open( "file:edmPatMcMatch.root" ); // Adapt the input file name
43 
44  // ----------------------------------------------------------------------
45  // Second Part:
46  //
47  // * loop the events in the input file
48  // * receive the collections of interest via fwlite::Handle
49  // * fill the histograms
50  // * after the loop close the input file
51  // ----------------------------------------------------------------------
52 
53  // loop the events
54  unsigned int iEvent=0;
56  for(event.toBegin(); !event.atEnd(); ++event, ++iEvent){
57  // break loop after end of file is reached
58  // or after 1000 events have been processed
59  if( iEvent==1000 ) break;
60 
61  // simple event counter
62  if(iEvent>0 && iEvent%25==0){ // Reduce print-out
63  std::cout << " processing event: " << iEvent << std::endl;
64  }
65 
66  // fwlite::Handle to the muon collection
67  fwlite::Handle<std::vector<pat::Muon> > muons; // Access the muon collection
68  muons.getByLabel(event, "cleanLayer1Muons");
69  // fwlite::Handle to the muon collection
70  fwlite::Handle<std::vector<pat::Jet> > jets; // Access the jet collection
71  jets.getByLabel(event, "cleanLayer1Jets");
72 
73  // loop muon collection and fill histograms
74  for(unsigned i=0; i<muons->size(); ++i){
75  const reco::GenParticle * genMuon = (*muons)[i].genParticle(); // Get the matched generator muon
76  if ( genMuon ) { // Check for valid reference
77  muonPt_->Fill( genMuon->pt(), (*muons)[i].pt() ); // Fill 2-D histo
78  }
79  }
80  // loop jet collection and fill histograms
81  for(unsigned i=0; i<jets->size(); ++i){
82  const reco::GenJet * genJet = (*jets)[i].genJet(); // Get the matched generator jet
83  if ( genJet ) { // Check for valid reference
84  jetPt_->Fill( genJet->pt(), (*jets)[i].pt() ); // Fill 2-D histo
85  }
86  }
87  }
88  // close input file
89  inFile->Close();
90 
91  // ----------------------------------------------------------------------
92  // Third Part:
93  //
94  // * open the output file
95  // * write the histograms to the output file
96  // * close the output file
97  // ----------------------------------------------------------------------
98 
99  //open output file
100  TFile outFile( "rootPatMcMatch.root", "recreate" ); // Adapt the output file name
101  outFile.mkdir("analyzeMcMatchPat"); // Adapt output file according to modifications
102  outFile.cd("analyzeMcMatchPat");
103  muonPt_->Write( );
104  jetPt_->Write( );
105  outFile.Close();
106 
107  // ----------------------------------------------------------------------
108  // Fourth Part:
109  //
110  // * never forgett to free the memory of the histograms
111  // ----------------------------------------------------------------------
112 
113  // free allocated space
114  delete muonPt_; // Delete the muon histo
115  delete jetPt_; // Delete the jet histo
116 
117  // that's it!
118  return 0;
119 }
int main(int argc, char *argv[])
double pt() const final
transverse momentum
muons
the two sets of parameters below are mutually exclusive, depending if RECO or ALCARECO is used the us...
Definition: DiMuonV_cfg.py:214
int iEvent
Definition: GenABIO.cc:224
static void enable()
enable automatic library loading
Jets made from MC generator particles.
Definition: GenJet.h:23
Definition: event.py:1