CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/PhysicsTools/HepMCCandAlgos/plugins/MCTruthCompositeMatcherNew.cc

Go to the documentation of this file.
00001 /* \class MCTruthCompositeMatcher
00002  *
00003  * \author Luca Lista, INFN
00004  *
00005  */
00006 
00007 #include "FWCore/Framework/interface/EDProducer.h"
00008 #include "FWCore/Utilities/interface/InputTag.h"
00009 #include "PhysicsTools/CandUtils/interface/CandMatcherNew.h"
00010 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00011 #include "DataFormats/Common/interface/Association.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "DataFormats/Common/interface/Handle.h"
00015 
00016 namespace reco {
00017   namespace modulesNew {
00018 
00019     class MCTruthCompositeMatcher : public edm::EDProducer {
00020     public:
00021       explicit MCTruthCompositeMatcher( const edm::ParameterSet & );
00022       ~MCTruthCompositeMatcher();
00023     private:
00024       edm::InputTag src_;
00025       std::vector<edm::InputTag> matchMaps_;
00026       std::vector<int> pdgId_;
00027       void produce( edm::Event & , const edm::EventSetup & );
00028     };
00029     
00030     MCTruthCompositeMatcher::MCTruthCompositeMatcher( const edm::ParameterSet & cfg ) :
00031       src_(cfg.getParameter<edm::InputTag>("src")),
00032       matchMaps_(cfg.getParameter<std::vector<edm::InputTag> >("matchMaps")),
00033       pdgId_(cfg.getParameter<std::vector<int> >("matchPDGId")) {
00034       produces<reco::GenParticleMatch>();
00035     }
00036     
00037     MCTruthCompositeMatcher::~MCTruthCompositeMatcher() {
00038     }
00039     
00040     void MCTruthCompositeMatcher::produce( edm::Event & evt , const edm::EventSetup & ) {
00041       using namespace edm;
00042       using namespace std;
00043       Handle<CandidateView> cands;
00044       evt.getByLabel(src_, cands);
00045       size_t nMaps = matchMaps_.size();
00046       std::vector<const GenParticleMatch *> maps;
00047       maps.reserve( nMaps );
00048       for( size_t i = 0; i != nMaps; ++ i ) {
00049         Handle<reco::GenParticleMatch> matchMap;
00050         evt.getByLabel(matchMaps_[i], matchMap);
00051         maps.push_back(& * matchMap);
00052       } 
00053       utilsNew::CandMatcher<GenParticleCollection> match(maps); 
00054       auto_ptr<GenParticleMatch> matchMap(new GenParticleMatch(match.ref()));
00055       int size = cands->size();
00056       vector<int>::const_iterator begin = pdgId_.begin(), end = pdgId_.end();
00057       if(size != 0) {
00058         GenParticleMatch::Filler filler(*matchMap);
00059         vector<int> indices(size);
00060         for(int i = 0; i != size; ++ i) {
00061           const Candidate & cand = (* cands)[i];
00062           GenParticleRef mc = match[cand];
00063           if(mc.isNull()) {
00064             indices[i] = -1; 
00065           } else {
00066             bool found = true;
00067             if(begin!=end) found = find(begin, end, std::abs(mc->pdgId())) != end;
00068             indices[i] = found ? int(mc.key()) : -1;
00069           }
00070         }
00071         CandidateBaseRefProd ref(cands->refAt(0));
00072         filler.insert(ref, indices.begin(), indices.end());
00073         filler.fill();
00074       }
00075       evt.put(matchMap);
00076     }
00077 
00078   }
00079 }
00080 
00081 #include "DataFormats/Candidate/interface/Candidate.h"
00082 #include "FWCore/Framework/interface/MakerMacros.h"
00083 
00084 namespace reco {
00085   namespace modulesNew {
00086 
00087     typedef MCTruthCompositeMatcher MCTruthCompositeMatcherNew;
00088 
00089 DEFINE_FWK_MODULE( MCTruthCompositeMatcherNew );
00090 
00091   }
00092 }