Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:30:27

0001 // -*- C++ -*-
0002 //
0003 // Package:    PatAlgos
0004 // Class:      pat::PATTriggerMatchEmbedder
0005 //
0006 /**
0007   \class    pat::PATTriggerMatchEmbedder PATTriggerMatchEmbedder.cc "PhysicsTools/PatAlgos/plugins/PATTriggerMatchEmbedder.cc"
0008   \brief
0009 
0010    .
0011 
0012   \author   Volker Adler
0013   \version  $Id: PATTriggerMatchEmbedder.cc,v 1.6 2010/09/02 17:52:47 vadler Exp $
0014 */
0015 //
0016 //
0017 
0018 #include <vector>
0019 
0020 #include "FWCore/Utilities/interface/transform.h"
0021 
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/global/EDProducer.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028 #include "DataFormats/Common/interface/Handle.h"
0029 #include "DataFormats/Common/interface/RefToBase.h"
0030 
0031 #include "DataFormats/PatCandidates/interface/Electron.h"
0032 #include "DataFormats/PatCandidates/interface/Jet.h"
0033 #include "DataFormats/PatCandidates/interface/MET.h"
0034 #include "DataFormats/PatCandidates/interface/Muon.h"
0035 #include "DataFormats/PatCandidates/interface/Photon.h"
0036 #include "DataFormats/PatCandidates/interface/Tau.h"
0037 
0038 namespace pat {
0039 
0040   template <class PATObjectType>
0041   class PATTriggerMatchEmbedder : public edm::global::EDProducer<> {
0042     const edm::InputTag src_;
0043     const edm::EDGetTokenT<edm::View<PATObjectType>> srcToken_;
0044     const std::vector<edm::InputTag> matches_;
0045     const std::vector<edm::EDGetTokenT<TriggerObjectStandAloneMatch>> matchesTokens_;
0046 
0047   public:
0048     explicit PATTriggerMatchEmbedder(const edm::ParameterSet& iConfig);
0049     ~PATTriggerMatchEmbedder() override{};
0050 
0051   private:
0052     void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0053   };
0054 
0055   typedef PATTriggerMatchEmbedder<Electron> PATTriggerMatchElectronEmbedder;
0056   typedef PATTriggerMatchEmbedder<Jet> PATTriggerMatchJetEmbedder;
0057   typedef PATTriggerMatchEmbedder<MET> PATTriggerMatchMETEmbedder;
0058   typedef PATTriggerMatchEmbedder<Muon> PATTriggerMatchMuonEmbedder;
0059   typedef PATTriggerMatchEmbedder<Photon> PATTriggerMatchPhotonEmbedder;
0060   typedef PATTriggerMatchEmbedder<Tau> PATTriggerMatchTauEmbedder;
0061 
0062 }  // namespace pat
0063 
0064 using namespace pat;
0065 
0066 template <class PATObjectType>
0067 PATTriggerMatchEmbedder<PATObjectType>::PATTriggerMatchEmbedder(const edm::ParameterSet& iConfig)
0068     : src_(iConfig.getParameter<edm::InputTag>("src")),
0069       srcToken_(consumes<edm::View<PATObjectType>>(src_)),
0070       matches_(iConfig.getParameter<std::vector<edm::InputTag>>("matches")),
0071       matchesTokens_(edm::vector_transform(
0072           matches_, [this](edm::InputTag const& tag) { return consumes<TriggerObjectStandAloneMatch>(tag); })) {
0073   produces<std::vector<PATObjectType>>();
0074 }
0075 
0076 template <class PATObjectType>
0077 void PATTriggerMatchEmbedder<PATObjectType>::produce(edm::StreamID,
0078                                                      edm::Event& iEvent,
0079                                                      const edm::EventSetup& iSetup) const {
0080   auto output = std::make_unique<std::vector<PATObjectType>>();
0081 
0082   edm::Handle<edm::View<PATObjectType>> candidates;
0083   iEvent.getByToken(srcToken_, candidates);
0084   if (!candidates.isValid()) {
0085     edm::LogError("missingInputSource") << "Input source with InputTag " << src_.encode() << " not in event.";
0086     return;
0087   }
0088 
0089   for (typename edm::View<PATObjectType>::const_iterator iCand = candidates->begin(); iCand != candidates->end();
0090        ++iCand) {
0091     const unsigned index(iCand - candidates->begin());
0092     PATObjectType cand(candidates->at(index));
0093     std::set<TriggerObjectStandAloneRef> cachedRefs;
0094     for (size_t iMatch = 0; iMatch < matchesTokens_.size(); ++iMatch) {
0095       edm::Handle<TriggerObjectStandAloneMatch> match;
0096       iEvent.getByToken(matchesTokens_.at(iMatch), match);
0097       if (!match.isValid()) {
0098         edm::LogError("missingInputMatch")
0099             << "Input match with InputTag " << matches_.at(iMatch).encode() << " not in event.";
0100         continue;
0101       }
0102       const TriggerObjectStandAloneRef trigRef((*match)[candidates->refAt(index)]);
0103       if (trigRef.isNonnull() && trigRef.isAvailable()) {
0104         if (cachedRefs.insert(trigRef).second) {  // protection from multiple entries of the same trigger objects
0105           cand.addTriggerObjectMatch(*trigRef);
0106         }
0107       }
0108     }
0109     output->push_back(cand);
0110   }
0111 
0112   iEvent.put(std::move(output));
0113 }
0114 
0115 #include "FWCore/Framework/interface/MakerMacros.h"
0116 
0117 DEFINE_FWK_MODULE(PATTriggerMatchElectronEmbedder);
0118 DEFINE_FWK_MODULE(PATTriggerMatchJetEmbedder);
0119 DEFINE_FWK_MODULE(PATTriggerMatchMETEmbedder);
0120 DEFINE_FWK_MODULE(PATTriggerMatchMuonEmbedder);
0121 DEFINE_FWK_MODULE(PATTriggerMatchPhotonEmbedder);
0122 DEFINE_FWK_MODULE(PATTriggerMatchTauEmbedder);