CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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  *****************************************************************************/
00021 // Includes
00023 #include "FWCore/Framework/interface/EDProducer.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "FWCore/Utilities/interface/InputTag.h"
00027 
00028 #include "DataFormats/Common/interface/View.h"
00029 #include "DataFormats/Math/interface/deltaR.h"
00030 
00031 #include "DataFormats/MuonReco/interface/Muon.h"
00032 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00033 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00034 #include "DataFormats/JetReco/interface/Jet.h"
00035 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00036 #include "DataFormats/TrackReco/interface/Track.h"
00037 
00038 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00039 
00040 
00041 #include <memory>
00042 #include <vector>
00043 #include <sstream>
00044 
00045 
00047 // class definition
00049 template<typename T1, typename T2>
00050 class ObjectViewMatcher : public edm::EDProducer
00051 {
00052 public:
00053   // construction/destruction
00054   ObjectViewMatcher(const edm::ParameterSet& iConfig);
00055   virtual ~ObjectViewMatcher();
00056   
00057   // member functions
00058   void produce(edm::Event& iEvent,const edm::EventSetup& iSetup) override;
00059   void endJob();
00060 
00061 private:  
00062   // member data
00063   edm::InputTag              srcCands_;
00064   std::vector<edm::InputTag> srcObjects_;
00065   double                     deltaRMax_;
00066   
00067   std::string  moduleLabel_;
00068 
00069   StringCutObjectSelector<T1,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00070   StringCutObjectSelector<T2,true> objMatchCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00071 
00072 
00073   unsigned int nObjectsTot_;
00074   unsigned int nObjectsMatch_;
00075 };
00076 
00077 
00078 
00080 // construction/destruction
00082 
00083 //______________________________________________________________________________
00084 template<typename T1, typename T2>
00085 ObjectViewMatcher<T1, T2>::ObjectViewMatcher(const edm::ParameterSet& iConfig)
00086   : srcCands_    (iConfig.getParameter<edm::InputTag>         ("srcObject"))
00087   , srcObjects_ (iConfig.getParameter<std::vector<edm::InputTag> >("srcObjectsToMatch"))
00088   , deltaRMax_  (iConfig.getParameter<double>                ("deltaRMax"))
00089   , moduleLabel_(iConfig.getParameter<std::string>                ("@module_label"))
00090   , objCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
00091   ,objMatchCut_(iConfig.existsAs<std::string>("srcObjectsToMatchSelection") ? iConfig.getParameter<std::string>("srcObjectsToMatchSelection") : "", true)
00092   , nObjectsTot_(0)
00093   , nObjectsMatch_(0)
00094 {
00095   produces<std::vector<T1> >();
00096 }
00097 
00098 
00099 //______________________________________________________________________________
00100 template<typename T1, typename T2>
00101 ObjectViewMatcher<T1, T2>::~ObjectViewMatcher(){}
00102 
00103 
00104 
00106 // implementation of member functions
00108 
00109 //______________________________________________________________________________
00110 template<typename T1, typename T2>
00111 void ObjectViewMatcher<T1, T2>::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
00112 {
00113   std::auto_ptr<std::vector<T1> > cleanObjects(new std::vector<T1 >);
00114 
00115   edm::Handle<edm::View<T1> > candidates;
00116   iEvent.getByLabel(srcCands_,candidates);
00117   
00118   bool* isMatch = new bool[candidates->size()];
00119   for (unsigned int iObject=0;iObject<candidates->size();iObject++) isMatch[iObject] = false;
00120   
00121   for (unsigned int iSrc=0;iSrc<srcObjects_.size();iSrc++) {
00122     edm::Handle<edm::View<T2> > objects;
00123     iEvent.getByLabel(srcObjects_[iSrc],objects);
00124    
00125     if(objects->size()==0) continue;
00126  
00127     for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
00128       const T1& candidate = candidates->at(iObject);
00129       if (!objCut_(candidate)) continue;
00130 
00131 
00132       for (unsigned int iObj=0;iObj<objects->size();iObj++) {
00133         const T2& obj = objects->at(iObj);
00134         if (!objMatchCut_(obj)) continue;
00135         double deltaR = reco::deltaR(candidate,obj);
00136         if (deltaR<deltaRMax_)  isMatch[iObject] = true;
00137       }
00138     } 
00139   }
00140   
00141 
00142   
00143   unsigned int counter=0;
00144   typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
00145   for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
00146     if(isMatch[counter]) cleanObjects->push_back( *tIt );  
00147   }
00148 
00149   nObjectsTot_  +=candidates->size();
00150   nObjectsMatch_+=cleanObjects->size();
00151 
00152   delete [] isMatch;  
00153   iEvent.put(cleanObjects);
00154 }
00155 
00156 
00157 //______________________________________________________________________________
00158 template<typename T1, typename T2>
00159 void ObjectViewMatcher<T1, T2>::endJob()
00160 {
00161   std::stringstream ss;
00162   ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsMatched="<<nObjectsMatch_
00163     <<" fObjectsMatch="<<100.*(nObjectsMatch_/(double)nObjectsTot_)<<"%\n";
00164   std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00165            <<"\n"<<moduleLabel_<<"(ObjectViewMatcher) SUMMARY:\n"<<ss.str()
00166            <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00167            << std::endl;
00168 }
00169 
00170 
00172 // plugin definition
00174 
00175 typedef ObjectViewMatcher<reco::Photon, reco::Track>      TrackMatchedPhotonProducer;
00176 typedef ObjectViewMatcher<reco::Jet, reco::Track>         TrackMatchedJetProducer;
00177 
00178 #include "FWCore/Framework/interface/MakerMacros.h"
00179 DEFINE_FWK_MODULE(TrackMatchedPhotonProducer);
00180 DEFINE_FWK_MODULE(TrackMatchedJetProducer);