Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:00

0001 #include <memory>
0002 #include <string>
0003 #include <vector>
0004 #include <sstream>
0005 #include <fstream>
0006 #include <iostream>
0007 
0008 #include <TH2F.h> // Use the correct histograms
0009 #include <TROOT.h>
0010 #include <TFile.h>
0011 #include <TSystem.h>
0012 
0013 #include "DataFormats/FWLite/interface/Handle.h"
0014 #include "DataFormats/PatCandidates/interface/Muon.h" // Include the examined data formats
0015 #include "DataFormats/PatCandidates/interface/Jet.h"  // Include the examined data formats
0016 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0017 
0018 
0019 int main(int argc, char* argv[]) 
0020 {
0021   // ----------------------------------------------------------------------
0022   // First Part: 
0023   //
0024   //  * enable FWLite 
0025   //  * book the histograms of interest 
0026   //  * open the input file
0027   // ----------------------------------------------------------------------
0028 
0029   // load framework libraries
0030   gSystem->Load( "libFWCoreFWLite" );
0031   FWLiteEnabler::enable();
0032   
0033   // book a set of histograms
0034   TH2F* muonPt_  = new TH2F( "muonPt", "Muon Pt", 60, 0., 300., 60, 0., 300. ); // 2-D histo for muon Pt
0035   muonPt_->SetXTitle( "gen." );
0036   muonPt_->SetYTitle( "reco." );
0037   TH2F* jetPt_   = new TH2F( "jetPt", "Jet Pt", 100, 0., 500., 100, 0., 500. ); // 2-D histo for jet Pt
0038   jetPt_->SetXTitle( "gen." );
0039   jetPt_->SetYTitle( "reco." );
0040   
0041   // open input file (can be located on castor)
0042   TFile* inFile = TFile::Open( "file:edmPatMcMatch.root" ); // Adapt the input file name
0043 
0044   // ----------------------------------------------------------------------
0045   // Second Part: 
0046   //
0047   //  * loop the events in the input file 
0048   //  * receive the collections of interest via fwlite::Handle
0049   //  * fill the histograms
0050   //  * after the loop close the input file
0051   // ----------------------------------------------------------------------
0052 
0053   // loop the events
0054   unsigned int iEvent=0;
0055   fwlite::Event event(inFile);
0056   for(event.toBegin(); !event.atEnd(); ++event, ++iEvent){
0057     // break loop after end of file is reached 
0058     // or after 1000 events have been processed
0059     if( iEvent==1000 ) break;
0060     
0061     // simple event counter
0062     if(iEvent>0 && iEvent%25==0){ // Reduce print-out
0063       std::cout << "  processing event: " << iEvent << std::endl;
0064     }
0065 
0066     // fwlite::Handle to the muon collection
0067     fwlite::Handle<std::vector<pat::Muon> > muons; // Access the muon collection
0068     muons.getByLabel(event, "cleanLayer1Muons");
0069     // fwlite::Handle to the muon collection
0070     fwlite::Handle<std::vector<pat::Jet> > jets; // Access the jet collection
0071     jets.getByLabel(event, "cleanLayer1Jets");
0072     
0073     // loop muon collection and fill histograms
0074     for(unsigned i=0; i<muons->size(); ++i){
0075       const reco::GenParticle * genMuon = (*muons)[i].genParticle(); // Get the matched generator muon
0076       if ( genMuon ) {                                               // Check for valid reference
0077         muonPt_->Fill( genMuon->pt(), (*muons)[i].pt() );            // Fill 2-D histo
0078       }
0079     }
0080     // loop jet collection and fill histograms
0081     for(unsigned i=0; i<jets->size(); ++i){
0082       const reco::GenJet * genJet = (*jets)[i].genJet(); // Get the matched generator jet
0083       if ( genJet ) {                                    // Check for valid reference
0084         jetPt_->Fill( genJet->pt(), (*jets)[i].pt() );   // Fill 2-D histo
0085       }
0086     }
0087   }  
0088   // close input file
0089   inFile->Close();
0090 
0091   // ----------------------------------------------------------------------
0092   // Third Part: 
0093   //
0094   //  * open the output file 
0095   //  * write the histograms to the output file
0096   //  * close the output file
0097   // ----------------------------------------------------------------------
0098   
0099   //open output file
0100   TFile outFile( "rootPatMcMatch.root", "recreate" ); // Adapt the output file name
0101   outFile.mkdir("analyzeMcMatchPat");                 // Adapt output file according to modifications
0102   outFile.cd("analyzeMcMatchPat");
0103   muonPt_->Write( );
0104   jetPt_->Write( );
0105   outFile.Close();
0106   
0107   // ----------------------------------------------------------------------
0108   // Fourth Part: 
0109   //
0110   //  * never forgett to free the memory of the histograms
0111   // ----------------------------------------------------------------------
0112 
0113   // free allocated space
0114   delete muonPt_; // Delete the muon histo
0115   delete jetPt_;  // Delete the jet histo
0116   
0117   // that's it!
0118   return 0;
0119 }