CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/PhysicsTools/TagAndProbe/plugins/ObjectViewMatcher.cc

Go to the documentation of this file.
00001 
00002 /*****************************************************************************
00003  * Project: CMS detector at the CERN
00004  *
00005  * Package: PhysicsTools/TagAndProbe
00006  *
00007  *
00008  * Authors:
00009  *
00010  *   Kalanand Mishra, Fermilab - kalanand@fnal.gov
00011  *
00012  * Description:
00013  *   - Matches a given object with other objects using deltaR-matching.
00014  *   - For example: can match a photon with track within a given deltaR.
00015  *   - Saves collection of the reference vectors of matched objects.
00016  * History:
00017  *   
00018  *
00019  * Copyright (C) 2010 FNAL 
00020  *****************************************************************************/
00022 // Includes
00024 #include "FWCore/Framework/interface/EDProducer.h"
00025 #include "FWCore/Framework/interface/Event.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027 #include "FWCore/Utilities/interface/InputTag.h"
00028 
00029 #include "DataFormats/Common/interface/View.h"
00030 #include "DataFormats/Math/interface/deltaR.h"
00031 
00032 #include "DataFormats/MuonReco/interface/Muon.h"
00033 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00034 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00035 #include "DataFormats/JetReco/interface/Jet.h"
00036 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00037 #include "DataFormats/TrackReco/interface/Track.h"
00038 
00039 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00040 
00041 
00042 #include <memory>
00043 #include <vector>
00044 #include <sstream>
00045 
00046 
00048 // class definition
00050 template<typename T1, typename T2>
00051 class ObjectViewMatcher : public edm::EDProducer
00052 {
00053 public:
00054   // construction/destruction
00055   ObjectViewMatcher(const edm::ParameterSet& iConfig);
00056   virtual ~ObjectViewMatcher();
00057   
00058   // member functions
00059   void produce(edm::Event& iEvent,const edm::EventSetup& iSetup);
00060   void endJob();
00061 
00062 private:  
00063   // member data
00064   edm::InputTag              srcCands_;
00065   std::vector<edm::InputTag> srcObjects_;
00066   double                     deltaRMax_;
00067   
00068   std::string  moduleLabel_;
00069 
00070   StringCutObjectSelector<T1,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00071   StringCutObjectSelector<T2,true> objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00072 
00073 
00074   unsigned int nObjectsTot_;
00075   unsigned int nObjectsMatch_;
00076 };
00077 
00078 
00079 
00081 // construction/destruction
00083 
00084 //______________________________________________________________________________
00085 template<typename T1, typename T2>
00086 ObjectViewMatcher<T1, T2>::ObjectViewMatcher(const edm::ParameterSet& iConfig)
00087   : srcCands_    (iConfig.getParameter<edm::InputTag>         ("srcObject"))
00088   , srcObjects_ (iConfig.getParameter<std::vector<edm::InputTag> >("srcObjectsToMatch"))
00089   , deltaRMax_  (iConfig.getParameter<double>                ("deltaRMax"))
00090   , moduleLabel_(iConfig.getParameter<std::string>                ("@module_label"))
00091   , objCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
00092   ,objMatchCut_(iConfig.existsAs<std::string>("srcObjectsToMatchSelection") ? iConfig.getParameter<std::string>("srcObjectsToMatchSelection") : "", true)
00093   , nObjectsTot_(0)
00094   , nObjectsMatch_(0)
00095 {
00096   produces<std::vector<T1> >();
00097 }
00098 
00099 
00100 //______________________________________________________________________________
00101 template<typename T1, typename T2>
00102 ObjectViewMatcher<T1, T2>::~ObjectViewMatcher(){}
00103 
00104 
00105 
00107 // implementation of member functions
00109 
00110 //______________________________________________________________________________
00111 template<typename T1, typename T2>
00112 void ObjectViewMatcher<T1, T2>::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
00113 {
00114   std::auto_ptr<std::vector<T1> > cleanObjects(new std::vector<T1 >);
00115 
00116   edm::Handle<edm::View<T1> > candidates;
00117   iEvent.getByLabel(srcCands_,candidates);
00118   
00119   bool* isMatch = new bool[candidates->size()];
00120   for (unsigned int iObject=0;iObject<candidates->size();iObject++) isMatch[iObject] = false;
00121   
00122   for (unsigned int iSrc=0;iSrc<srcObjects_.size();iSrc++) {
00123     edm::Handle<edm::View<T2> > objects;
00124     iEvent.getByLabel(srcObjects_[iSrc],objects);
00125    
00126     if(objects->size()==0) continue;
00127  
00128     for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
00129       const T1& candidate = candidates->at(iObject);
00130       if (!objCut_(candidate)) continue;
00131 
00132 
00133       for (unsigned int iObj=0;iObj<objects->size();iObj++) {
00134         const T2& obj = objects->at(iObj);
00135         if (!objMatchCut_(obj)) continue;
00136         double deltaR = reco::deltaR(candidate,obj);
00137         if (deltaR<deltaRMax_)  isMatch[iObject] = true;
00138       }
00139     } 
00140   }
00141   
00142 
00143   
00144   unsigned int counter=0;
00145   typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
00146   for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
00147     if(isMatch[counter]) cleanObjects->push_back( *tIt );  
00148   }
00149 
00150   nObjectsTot_  +=candidates->size();
00151   nObjectsMatch_+=cleanObjects->size();
00152 
00153   delete [] isMatch;  
00154   iEvent.put(cleanObjects);
00155 }
00156 
00157 
00158 //______________________________________________________________________________
00159 template<typename T1, typename T2>
00160 void ObjectViewMatcher<T1, T2>::endJob()
00161 {
00162   std::stringstream ss;
00163   ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsMatched="<<nObjectsMatch_
00164     <<" fObjectsMatch="<<100.*(nObjectsMatch_/(double)nObjectsTot_)<<"%\n";
00165   std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00166            <<"\n"<<moduleLabel_<<"(ObjectViewMatcher) SUMMARY:\n"<<ss.str()
00167            <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00168            << std::endl;
00169 }
00170 
00171 
00173 // plugin definition
00175 
00176 typedef ObjectViewMatcher<reco::Photon, reco::Track>      TrackMatchedPhotonProducer;
00177 typedef ObjectViewMatcher<reco::Jet, reco::Track>         TrackMatchedJetProducer;
00178 
00179 #include "FWCore/Framework/interface/MakerMacros.h"
00180 DEFINE_FWK_MODULE(TrackMatchedPhotonProducer);
00181 DEFINE_FWK_MODULE(TrackMatchedJetProducer);