Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:25

0001 /** \class TriggerSummaryProducerAOD
0002  *
0003  * See header file for documentation
0004  *
0005  *
0006  *  \author Martin Grunewald
0007  *
0008  */
0009 
0010 #include <ostream>
0011 #include <algorithm>
0012 #include <memory>
0013 #include <typeinfo>
0014 
0015 #include "HLTrigger/HLTcore/interface/TriggerSummaryProducerAOD.h"
0016 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0017 
0018 #include "DataFormats/Common/interface/Handle.h"
0019 #include "DataFormats/Common/interface/OrphanHandle.h"
0020 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0021 #include "DataFormats/Provenance/interface/Provenance.h"
0022 #include "FWCore/Framework/interface/ProcessMatch.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 
0025 #include "DataFormats/RecoCandidate/interface/RecoEcalCandidate.h"
0026 #include "DataFormats/EgammaCandidates/interface/Electron.h"
0027 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
0028 #include "DataFormats/JetReco/interface/CaloJet.h"
0029 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
0030 #include "DataFormats/METReco/interface/MET.h"
0031 #include "DataFormats/METReco/interface/METFwd.h"
0032 #include "DataFormats/METReco/interface/CaloMET.h"
0033 #include "DataFormats/METReco/interface/CaloMETFwd.h"
0034 #include "DataFormats/METReco/interface/PFMET.h"
0035 #include "DataFormats/METReco/interface/PFMETFwd.h"
0036 #include "DataFormats/HcalIsolatedTrack/interface/IsolatedPixelTrackCandidate.h"
0037 
0038 #include "DataFormats/L1Trigger/interface/L1HFRings.h"
0039 #include "DataFormats/L1Trigger/interface/L1EmParticle.h"
0040 #include "DataFormats/L1Trigger/interface/L1JetParticle.h"
0041 #include "DataFormats/L1Trigger/interface/L1MuonParticle.h"
0042 #include "DataFormats/L1Trigger/interface/L1EtMissParticle.h"
0043 
0044 #include "DataFormats/L1Trigger/interface/Muon.h"
0045 #include "DataFormats/L1Trigger/interface/MuonShower.h"
0046 #include "DataFormats/L1Trigger/interface/EGamma.h"
0047 #include "DataFormats/L1Trigger/interface/Jet.h"
0048 #include "DataFormats/L1Trigger/interface/Tau.h"
0049 #include "DataFormats/L1Trigger/interface/EtSum.h"
0050 
0051 #include "DataFormats/L1TMuonPhase2/interface/TrackerMuon.h"
0052 #include "DataFormats/L1TCorrelator/interface/TkElectron.h"
0053 #include "DataFormats/L1TCorrelator/interface/TkEm.h"
0054 #include "DataFormats/L1TParticleFlow/interface/PFJet.h"
0055 #include "DataFormats/L1TParticleFlow/interface/PFTau.h"
0056 #include "DataFormats/L1TParticleFlow/interface/HPSPFTau.h"
0057 #include "DataFormats/L1TParticleFlow/interface/HPSPFTauFwd.h"
0058 #include "DataFormats/L1TParticleFlow/interface/PFTrack.h"
0059 
0060 #include "DataFormats/JetReco/interface/PFJet.h"
0061 #include "DataFormats/TauReco/interface/PFTau.h"
0062 
0063 #include "FWCore/ServiceRegistry/interface/Service.h"
0064 #include "FWCore/Framework/interface/TriggerNamesService.h"
0065 
0066 #include "boost/algorithm/string.hpp"
0067 
0068 namespace {
0069   std::vector<std::regex> convertToRegex(std::vector<std::string> const& iPatterns) {
0070     std::vector<std::regex> result;
0071 
0072     for (auto const& pattern : iPatterns) {
0073       auto regexPattern = pattern;
0074       boost::replace_all(regexPattern, "*", ".*");
0075       boost::replace_all(regexPattern, "?", ".");
0076 
0077       result.emplace_back(regexPattern);
0078     }
0079     return result;
0080   }
0081 }  // namespace
0082 
0083 //
0084 // constructors and destructor
0085 //
0086 TriggerSummaryProducerAOD::TriggerSummaryProducerAOD(const edm::ParameterSet& ps)
0087     : throw_(ps.getParameter<bool>("throw")),
0088       pn_(ps.getParameter<std::string>("processName")),
0089       moduleLabelPatternsToMatch_(
0090           convertToRegex(ps.getParameter<std::vector<std::string>>("moduleLabelPatternsToMatch"))),
0091       moduleLabelPatternsToSkip_(
0092           convertToRegex(ps.getParameter<std::vector<std::string>>("moduleLabelPatternsToSkip"))) {
0093   if (pn_ == "@") {
0094     edm::Service<edm::service::TriggerNamesService> tns;
0095     if (tns.isAvailable()) {
0096       pn_ = tns->getProcessName();
0097     } else {
0098       edm::LogError("TriggerSummaryProducerAOD") << "HLT Error: TriggerNamesService not available!";
0099       pn_ = "*";
0100     }
0101   }
0102   LogDebug("TriggerSummaryProducerAOD") << "Using process name: '" << pn_ << "'";
0103 
0104   produces<trigger::TriggerEvent>();
0105 
0106   auto const* pProcessName = &pn_;
0107   auto const& moduleLabelPatternsToMatch = moduleLabelPatternsToMatch_;
0108   auto const& moduleLabelPatternsToSkip = moduleLabelPatternsToSkip_;
0109   auto productMatch = [pProcessName, &moduleLabelPatternsToSkip, &moduleLabelPatternsToMatch](
0110                           edm::BranchDescription const& iBranch) -> bool {
0111     if (iBranch.processName() == *pProcessName || *pProcessName == "*") {
0112       auto const& label = iBranch.moduleLabel();
0113       for (auto& match : moduleLabelPatternsToMatch) {
0114         if (std::regex_match(label, match)) {
0115           //make sure this is not in the reject list
0116           for (auto& reject : moduleLabelPatternsToSkip) {
0117             if (std::regex_match(label, reject)) {
0118               return false;
0119             }
0120           }
0121           return true;
0122         }
0123       }
0124     }
0125     return false;
0126   };
0127 
0128   getTriggerFilterObjectWithRefs_ = edm::GetterOfProducts<trigger::TriggerFilterObjectWithRefs>(productMatch, this);
0129   getRecoEcalCandidateCollection_ = edm::GetterOfProducts<reco::RecoEcalCandidateCollection>(productMatch, this);
0130   getElectronCollection_ = edm::GetterOfProducts<reco::ElectronCollection>(productMatch, this);
0131   getRecoChargedCandidateCollection_ = edm::GetterOfProducts<reco::RecoChargedCandidateCollection>(productMatch, this);
0132   getCaloJetCollection_ = edm::GetterOfProducts<reco::CaloJetCollection>(productMatch, this);
0133   getCompositeCandidateCollection_ = edm::GetterOfProducts<reco::CompositeCandidateCollection>(productMatch, this);
0134   getMETCollection_ = edm::GetterOfProducts<reco::METCollection>(productMatch, this);
0135   getCaloMETCollection_ = edm::GetterOfProducts<reco::CaloMETCollection>(productMatch, this);
0136   getIsolatedPixelTrackCandidateCollection_ =
0137       edm::GetterOfProducts<reco::IsolatedPixelTrackCandidateCollection>(productMatch, this);
0138   getL1EmParticleCollection_ = edm::GetterOfProducts<l1extra::L1EmParticleCollection>(productMatch, this);
0139   getL1MuonParticleCollection_ = edm::GetterOfProducts<l1extra::L1MuonParticleCollection>(productMatch, this);
0140   getL1JetParticleCollection_ = edm::GetterOfProducts<l1extra::L1JetParticleCollection>(productMatch, this);
0141   getL1EtMissParticleCollection_ = edm::GetterOfProducts<l1extra::L1EtMissParticleCollection>(productMatch, this);
0142   getL1HFRingsCollection_ = edm::GetterOfProducts<l1extra::L1HFRingsCollection>(productMatch, this);
0143   getL1TMuonParticleCollection_ = edm::GetterOfProducts<l1t::MuonBxCollection>(productMatch, this);
0144   getL1TMuonShowerParticleCollection_ = edm::GetterOfProducts<l1t::MuonShowerBxCollection>(productMatch, this);
0145   getL1TEGammaParticleCollection_ = edm::GetterOfProducts<l1t::EGammaBxCollection>(productMatch, this);
0146   getL1TJetParticleCollection_ = edm::GetterOfProducts<l1t::JetBxCollection>(productMatch, this);
0147   getL1TTauParticleCollection_ = edm::GetterOfProducts<l1t::TauBxCollection>(productMatch, this);
0148   getL1TEtSumParticleCollection_ = edm::GetterOfProducts<l1t::EtSumBxCollection>(productMatch, this);
0149 
0150   getL1TTkMuonCollection_ = edm::GetterOfProducts<l1t::TrackerMuonCollection>(productMatch, this);
0151   getL1TTkElectronCollection_ = edm::GetterOfProducts<l1t::TkElectronCollection>(productMatch, this);
0152   getL1TTkEmCollection_ = edm::GetterOfProducts<l1t::TkEmCollection>(productMatch, this);
0153   getL1TPFJetCollection_ = edm::GetterOfProducts<l1t::PFJetCollection>(productMatch, this);
0154   getL1TPFTauCollection_ = edm::GetterOfProducts<l1t::PFTauCollection>(productMatch, this);
0155   getL1THPSPFTauCollection_ = edm::GetterOfProducts<l1t::HPSPFTauCollection>(productMatch, this);
0156   getL1TPFTrackCollection_ = edm::GetterOfProducts<l1t::PFTrackCollection>(productMatch, this);
0157 
0158   getPFJetCollection_ = edm::GetterOfProducts<reco::PFJetCollection>(productMatch, this);
0159   getPFTauCollection_ = edm::GetterOfProducts<reco::PFTauCollection>(productMatch, this);
0160   getPFMETCollection_ = edm::GetterOfProducts<reco::PFMETCollection>(productMatch, this);
0161 
0162   getL1TP2GTCandCollection_ = edm::GetterOfProducts<l1t::P2GTCandidateCollection>(productMatch, this);
0163 
0164   callWhenNewProductsRegistered([this](edm::BranchDescription const& bd) {
0165     getTriggerFilterObjectWithRefs_(bd);
0166     getRecoEcalCandidateCollection_(bd);
0167     getElectronCollection_(bd);
0168     getRecoChargedCandidateCollection_(bd);
0169     getCaloJetCollection_(bd);
0170     getCompositeCandidateCollection_(bd);
0171     getMETCollection_(bd);
0172     getCaloMETCollection_(bd);
0173     getIsolatedPixelTrackCandidateCollection_(bd);
0174     getL1EmParticleCollection_(bd);
0175     getL1MuonParticleCollection_(bd);
0176     getL1JetParticleCollection_(bd);
0177     getL1EtMissParticleCollection_(bd);
0178     getL1HFRingsCollection_(bd);
0179     getL1TMuonParticleCollection_(bd);
0180     getL1TMuonShowerParticleCollection_(bd);
0181     getL1TEGammaParticleCollection_(bd);
0182     getL1TJetParticleCollection_(bd);
0183     getL1TTauParticleCollection_(bd);
0184     getL1TEtSumParticleCollection_(bd);
0185     getL1TTkMuonCollection_(bd);
0186     getL1TTkElectronCollection_(bd);
0187     getL1TTkEmCollection_(bd);
0188     getL1TPFJetCollection_(bd);
0189     getL1TPFTauCollection_(bd);
0190     getL1THPSPFTauCollection_(bd);
0191     getL1TPFTrackCollection_(bd);
0192     getPFJetCollection_(bd);
0193     getPFTauCollection_(bd);
0194     getPFMETCollection_(bd);
0195     getL1TP2GTCandCollection_(bd);
0196   });
0197 }
0198 
0199 TriggerSummaryProducerAOD::~TriggerSummaryProducerAOD() = default;
0200 
0201 //
0202 // member functions
0203 //
0204 
0205 namespace {
0206   inline void tokenizeTag(const std::string& tag, std::string& label, std::string& instance, std::string& process) {
0207     using std::string;
0208 
0209     const char token(':');
0210     const string empty;
0211 
0212     label = tag;
0213     const string::size_type i1(label.find(token));
0214     if (i1 == string::npos) {
0215       instance = empty;
0216       process = empty;
0217     } else {
0218       instance = label.substr(i1 + 1);
0219       label.resize(i1);
0220       const string::size_type i2(instance.find(token));
0221       if (i2 == string::npos) {
0222         process = empty;
0223       } else {
0224         process = instance.substr(i2 + 1);
0225         instance.resize(i2);
0226       }
0227     }
0228   }
0229 }  // namespace
0230 
0231 void TriggerSummaryProducerAOD::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0232   edm::ParameterSetDescription desc;
0233   desc.add<bool>("throw", false)->setComment("Throw exception or LogError");
0234   desc.add<std::string>("processName", "@")
0235       ->setComment(
0236           "Process name to use when getting data. The value of '@' is used to denote the current process name.");
0237   desc.add<std::vector<std::string>>("moduleLabelPatternsToMatch", std::vector<std::string>(1, "hlt*"))
0238       ->setComment("glob patterns for module labels to get data.");
0239   desc.add<std::vector<std::string>>("moduleLabelPatternsToSkip", std::vector<std::string>())
0240       ->setComment("module labels for data products which should not be gotten.");
0241   descriptions.add("triggerSummaryProducerAOD", desc);
0242 }
0243 
0244 // ------------ method called to produce the data  ------------
0245 void TriggerSummaryProducerAOD::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0246   using namespace std;
0247   using namespace edm;
0248   using namespace reco;
0249   using namespace l1extra;
0250   using namespace trigger;
0251   using namespace l1t;
0252 
0253   std::vector<edm::Handle<trigger::TriggerFilterObjectWithRefs>> fobs;
0254   getTriggerFilterObjectWithRefs_.fillHandles(iEvent, fobs);
0255 
0256   const unsigned int nfob(fobs.size());
0257   LogTrace("TriggerSummaryProducerAOD") << "Number of filter  objects found: " << nfob;
0258 
0259   string tagLabel, tagInstance, tagProcess;
0260 
0261   ///
0262   /// check whether collection tags are recorded in filterobjects; if
0263   /// so, these are L3 collections to be packed up, and the
0264   /// corresponding filter is a L3 filter also to be packed up.
0265   /// Record the InputTags of those L3 filters and L3 collections
0266   std::vector<bool> maskFilters;
0267   maskFilters.resize(nfob);
0268   InputTagSet filterTagsEvent(pn_ == "*");
0269   InputTagSet collectionTagsEvent(pn_ == "*");
0270 
0271   unsigned int nf(0);
0272   for (unsigned int ifob = 0; ifob != nfob; ++ifob) {
0273     maskFilters[ifob] = false;
0274     const vector<string>& collectionTags_(fobs[ifob]->getCollectionTagsAsStrings());
0275     const unsigned int ncol(collectionTags_.size());
0276     if (ncol > 0) {
0277       nf++;
0278       maskFilters[ifob] = true;
0279       const string& label(fobs[ifob].provenance()->moduleLabel());
0280       const string& instance(fobs[ifob].provenance()->productInstanceName());
0281       const string& process(fobs[ifob].provenance()->processName());
0282       filterTagsEvent.insert(InputTag(label, instance, process));
0283       for (unsigned int icol = 0; icol != ncol; ++icol) {
0284         // overwrite process name (usually not set)
0285         tokenizeTag(collectionTags_[icol], tagLabel, tagInstance, tagProcess);
0286         collectionTagsEvent.insert(InputTag(tagLabel, tagInstance, pn_));
0287       }
0288     }
0289   }
0290   /// check uniqueness count
0291   if (filterTagsEvent.size() != nf) {
0292     LogError("TriggerSummaryProducerAOD")
0293         << "Mismatch in number of filter tags: " << filterTagsEvent.size() << "!=" << nf;
0294   }
0295 
0296   /// accumulate for endJob printout
0297   collectionTagsGlobal_.insert(collectionTagsEvent.begin(), collectionTagsEvent.end());
0298   filterTagsGlobal_.insert(filterTagsEvent.begin(), filterTagsEvent.end());
0299 
0300   /// debug printout
0301   if (isDebugEnabled()) {
0302     /// event-by-event tags
0303     LogTrace("TriggerSummaryProducerAOD") << "Number of unique collections requested " << collectionTagsEvent.size();
0304     const InputTagSet::const_iterator cb(collectionTagsEvent.begin());
0305     const InputTagSet::const_iterator ce(collectionTagsEvent.end());
0306     for (InputTagSet::const_iterator ci = cb; ci != ce; ++ci) {
0307       LogTrace("TriggerSummaryProducerAOD") << distance(cb, ci) << " " << ci->encode();
0308     }
0309     LogTrace("TriggerSummaryProducerAOD") << "Number of unique filters requested " << filterTagsEvent.size();
0310     const InputTagSet::const_iterator fb(filterTagsEvent.begin());
0311     const InputTagSet::const_iterator fe(filterTagsEvent.end());
0312     for (InputTagSet::const_iterator fi = fb; fi != fe; ++fi) {
0313       LogTrace("TriggerSummaryProducerAOD") << distance(fb, fi) << " " << fi->encode();
0314     }
0315   }
0316 
0317   ///
0318   /// Now the processing:
0319   /// first trigger objects from L3 collections, then L3 filter objects
0320   ///
0321   /// create trigger objects, fill triggerobjectcollection and offset map
0322   trigger::TriggerObjectCollection toc;
0323   //toc_.clear();
0324   std::vector<std::string> tags;
0325   trigger::Keys keys;
0326   std::map<edm::ProductID, unsigned int> offset;
0327 
0328   fillTriggerObjectCollections<RecoEcalCandidateCollection>(
0329       toc, offset, tags, keys, iEvent, getRecoEcalCandidateCollection_, collectionTagsEvent);
0330   fillTriggerObjectCollections<ElectronCollection>(
0331       toc, offset, tags, keys, iEvent, getElectronCollection_, collectionTagsEvent);
0332   fillTriggerObjectCollections<RecoChargedCandidateCollection>(
0333       toc, offset, tags, keys, iEvent, getRecoChargedCandidateCollection_, collectionTagsEvent);
0334   fillTriggerObjectCollections<CaloJetCollection>(
0335       toc, offset, tags, keys, iEvent, getCaloJetCollection_, collectionTagsEvent);
0336   fillTriggerObjectCollections<CompositeCandidateCollection>(
0337       toc, offset, tags, keys, iEvent, getCompositeCandidateCollection_, collectionTagsEvent);
0338   fillTriggerObjectCollections<METCollection>(toc, offset, tags, keys, iEvent, getMETCollection_, collectionTagsEvent);
0339   fillTriggerObjectCollections<CaloMETCollection>(
0340       toc, offset, tags, keys, iEvent, getCaloMETCollection_, collectionTagsEvent);
0341   fillTriggerObjectCollections<IsolatedPixelTrackCandidateCollection>(
0342       toc, offset, tags, keys, iEvent, getIsolatedPixelTrackCandidateCollection_, collectionTagsEvent);
0343   ///
0344   fillTriggerObjectCollections<L1EmParticleCollection>(
0345       toc, offset, tags, keys, iEvent, getL1EmParticleCollection_, collectionTagsEvent);
0346   fillTriggerObjectCollections<L1MuonParticleCollection>(
0347       toc, offset, tags, keys, iEvent, getL1MuonParticleCollection_, collectionTagsEvent);
0348   fillTriggerObjectCollections<L1JetParticleCollection>(
0349       toc, offset, tags, keys, iEvent, getL1JetParticleCollection_, collectionTagsEvent);
0350   fillTriggerObjectCollections<L1EtMissParticleCollection>(
0351       toc, offset, tags, keys, iEvent, getL1EtMissParticleCollection_, collectionTagsEvent);
0352   fillTriggerObjectCollections<L1HFRingsCollection>(
0353       toc, offset, tags, keys, iEvent, getL1HFRingsCollection_, collectionTagsEvent);
0354   fillTriggerObjectCollections<MuonBxCollection>(
0355       toc, offset, tags, keys, iEvent, getL1TMuonParticleCollection_, collectionTagsEvent);
0356   fillTriggerObjectCollections<MuonShowerBxCollection>(
0357       toc, offset, tags, keys, iEvent, getL1TMuonShowerParticleCollection_, collectionTagsEvent);
0358   fillTriggerObjectCollections<EGammaBxCollection>(
0359       toc, offset, tags, keys, iEvent, getL1TEGammaParticleCollection_, collectionTagsEvent);
0360   fillTriggerObjectCollections<JetBxCollection>(
0361       toc, offset, tags, keys, iEvent, getL1TJetParticleCollection_, collectionTagsEvent);
0362   fillTriggerObjectCollections<TauBxCollection>(
0363       toc, offset, tags, keys, iEvent, getL1TTauParticleCollection_, collectionTagsEvent);
0364   fillTriggerObjectCollections<EtSumBxCollection>(
0365       toc, offset, tags, keys, iEvent, getL1TEtSumParticleCollection_, collectionTagsEvent);
0366   ///
0367   fillTriggerObjectCollections<l1t::TrackerMuonCollection>(
0368       toc, offset, tags, keys, iEvent, getL1TTkMuonCollection_, collectionTagsEvent);
0369   fillTriggerObjectCollections<l1t::TkElectronCollection>(
0370       toc, offset, tags, keys, iEvent, getL1TTkElectronCollection_, collectionTagsEvent);
0371   fillTriggerObjectCollections<l1t::TkEmCollection>(
0372       toc, offset, tags, keys, iEvent, getL1TTkEmCollection_, collectionTagsEvent);
0373   fillTriggerObjectCollections<l1t::PFJetCollection>(
0374       toc, offset, tags, keys, iEvent, getL1TPFJetCollection_, collectionTagsEvent);
0375   fillTriggerObjectCollections<l1t::PFTauCollection>(
0376       toc, offset, tags, keys, iEvent, getL1TPFTauCollection_, collectionTagsEvent);
0377   fillTriggerObjectCollections<l1t::HPSPFTauCollection>(
0378       toc, offset, tags, keys, iEvent, getL1THPSPFTauCollection_, collectionTagsEvent);
0379   fillTriggerObjectCollections<l1t::PFTrackCollection>(
0380       toc, offset, tags, keys, iEvent, getL1TPFTrackCollection_, collectionTagsEvent);
0381   ///
0382   fillTriggerObjectCollections<reco::PFJetCollection>(
0383       toc, offset, tags, keys, iEvent, getPFJetCollection_, collectionTagsEvent);
0384   fillTriggerObjectCollections<reco::PFTauCollection>(
0385       toc, offset, tags, keys, iEvent, getPFTauCollection_, collectionTagsEvent);
0386   fillTriggerObjectCollections<reco::PFMETCollection>(
0387       toc, offset, tags, keys, iEvent, getPFMETCollection_, collectionTagsEvent);
0388   ///
0389   fillTriggerObjectCollections<l1t::P2GTCandidateCollection>(
0390       toc, offset, tags, keys, iEvent, getL1TP2GTCandCollection_, collectionTagsEvent);
0391   const unsigned int nk(tags.size());
0392   LogDebug("TriggerSummaryProducerAOD") << "Number of collections found: " << nk;
0393   const unsigned int no(toc.size());
0394   LogDebug("TriggerSummaryProducerAOD") << "Number of physics objects found: " << no;
0395 
0396   ///
0397   /// construct single AOD product, reserving capacity
0398   unique_ptr<TriggerEvent> product(new TriggerEvent(pn_, nk, no, nf));
0399 
0400   /// fill trigger object collection
0401   product->addCollections(tags, keys);
0402   product->addObjects(toc);
0403 
0404   /// fill the L3 filter objects
0405   trigger::Vids ids;
0406   for (unsigned int ifob = 0; ifob != nfob; ++ifob) {
0407     if (maskFilters[ifob]) {
0408       const string& label(fobs[ifob].provenance()->moduleLabel());
0409       const string& instance(fobs[ifob].provenance()->productInstanceName());
0410       const string& process(fobs[ifob].provenance()->processName());
0411       const edm::InputTag filterTag(label, instance, process);
0412       ids.clear();
0413       keys.clear();
0414       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->photonIds(), fobs[ifob]->photonRefs(), offset, keys, ids);
0415       fillFilterObjectMembers(
0416           iEvent, filterTag, fobs[ifob]->electronIds(), fobs[ifob]->electronRefs(), offset, keys, ids);
0417       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->muonIds(), fobs[ifob]->muonRefs(), offset, keys, ids);
0418       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->jetIds(), fobs[ifob]->jetRefs(), offset, keys, ids);
0419       fillFilterObjectMembers(
0420           iEvent, filterTag, fobs[ifob]->compositeIds(), fobs[ifob]->compositeRefs(), offset, keys, ids);
0421       fillFilterObjectMembers(
0422           iEvent, filterTag, fobs[ifob]->basemetIds(), fobs[ifob]->basemetRefs(), offset, keys, ids);
0423       fillFilterObjectMembers(
0424           iEvent, filterTag, fobs[ifob]->calometIds(), fobs[ifob]->calometRefs(), offset, keys, ids);
0425       fillFilterObjectMembers(
0426           iEvent, filterTag, fobs[ifob]->pixtrackIds(), fobs[ifob]->pixtrackRefs(), offset, keys, ids);
0427       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->l1emIds(), fobs[ifob]->l1emRefs(), offset, keys, ids);
0428       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->l1muonIds(), fobs[ifob]->l1muonRefs(), offset, keys, ids);
0429       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->l1jetIds(), fobs[ifob]->l1jetRefs(), offset, keys, ids);
0430       fillFilterObjectMembers(
0431           iEvent, filterTag, fobs[ifob]->l1etmissIds(), fobs[ifob]->l1etmissRefs(), offset, keys, ids);
0432       fillFilterObjectMembers(
0433           iEvent, filterTag, fobs[ifob]->l1hfringsIds(), fobs[ifob]->l1hfringsRefs(), offset, keys, ids);
0434       fillFilterObjectMembers(
0435           iEvent, filterTag, fobs[ifob]->l1tmuonIds(), fobs[ifob]->l1tmuonRefs(), offset, keys, ids);
0436       fillFilterObjectMembers(
0437           iEvent, filterTag, fobs[ifob]->l1tmuonShowerIds(), fobs[ifob]->l1tmuonShowerRefs(), offset, keys, ids);
0438       fillFilterObjectMembers(
0439           iEvent, filterTag, fobs[ifob]->l1tegammaIds(), fobs[ifob]->l1tegammaRefs(), offset, keys, ids);
0440       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->l1tjetIds(), fobs[ifob]->l1tjetRefs(), offset, keys, ids);
0441       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->l1ttauIds(), fobs[ifob]->l1ttauRefs(), offset, keys, ids);
0442       fillFilterObjectMembers(
0443           iEvent, filterTag, fobs[ifob]->l1tetsumIds(), fobs[ifob]->l1tetsumRefs(), offset, keys, ids);
0444       /**/
0445       fillFilterObjectMembers(
0446           iEvent, filterTag, fobs[ifob]->l1ttkmuonIds(), fobs[ifob]->l1ttkmuonRefs(), offset, keys, ids);
0447       fillFilterObjectMembers(
0448           iEvent, filterTag, fobs[ifob]->l1ttkeleIds(), fobs[ifob]->l1ttkeleRefs(), offset, keys, ids);
0449       fillFilterObjectMembers(
0450           iEvent, filterTag, fobs[ifob]->l1ttkemIds(), fobs[ifob]->l1ttkemRefs(), offset, keys, ids);
0451       fillFilterObjectMembers(
0452           iEvent, filterTag, fobs[ifob]->l1tpfjetIds(), fobs[ifob]->l1tpfjetRefs(), offset, keys, ids);
0453       fillFilterObjectMembers(
0454           iEvent, filterTag, fobs[ifob]->l1tpftauIds(), fobs[ifob]->l1tpftauRefs(), offset, keys, ids);
0455       fillFilterObjectMembers(
0456           iEvent, filterTag, fobs[ifob]->l1thpspftauIds(), fobs[ifob]->l1thpspftauRefs(), offset, keys, ids);
0457       fillFilterObjectMembers(
0458           iEvent, filterTag, fobs[ifob]->l1tpftrackIds(), fobs[ifob]->l1tpftrackRefs(), offset, keys, ids);
0459       /**/
0460       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->pfjetIds(), fobs[ifob]->pfjetRefs(), offset, keys, ids);
0461       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->pftauIds(), fobs[ifob]->pftauRefs(), offset, keys, ids);
0462       fillFilterObjectMembers(iEvent, filterTag, fobs[ifob]->pfmetIds(), fobs[ifob]->pfmetRefs(), offset, keys, ids);
0463       fillFilterObjectMembers(
0464           iEvent, filterTag, fobs[ifob]->l1tp2gtcandIds(), fobs[ifob]->l1tp2gtcandRefs(), offset, keys, ids);
0465       product->addFilter(filterTag, ids, keys);
0466     }
0467   }
0468 
0469   OrphanHandle<TriggerEvent> ref = iEvent.put(std::move(product));
0470   LogTrace("TriggerSummaryProducerAOD") << "Number of physics objects packed: " << ref->sizeObjects();
0471   LogTrace("TriggerSummaryProducerAOD") << "Number of filter  objects packed: " << ref->sizeFilters();
0472 }
0473 
0474 template <typename C>
0475 void TriggerSummaryProducerAOD::fillTriggerObjectCollections(trigger::TriggerObjectCollection& toc,
0476                                                              ProductIDtoIndex& offset,
0477                                                              std::vector<std::string>& tags,
0478                                                              trigger::Keys& keys,
0479                                                              const edm::Event& iEvent,
0480                                                              const edm::GetterOfProducts<C>& getter,
0481                                                              const InputTagSet& collectionTagsEvent) const {
0482   /// this routine accesses the original (L3) collections (with C++
0483   /// typename C), extracts 4-momentum and id of each collection
0484   /// member, and packs this up
0485 
0486   using namespace std;
0487   using namespace edm;
0488   using namespace reco;
0489   using namespace l1extra;
0490   using namespace trigger;
0491   using namespace l1t;
0492 
0493   vector<Handle<C>> collections;
0494   getter.fillHandles(iEvent, collections);
0495   const unsigned int nc(collections.size());
0496 
0497   for (unsigned int ic = 0; ic != nc; ++ic) {
0498     const Provenance& provenance(*(collections[ic].provenance()));
0499     const string& label(provenance.moduleLabel());
0500     const string& instance(provenance.productInstanceName());
0501     const string& process(provenance.processName());
0502     const InputTag collectionTag(label, instance, process);
0503 
0504     if (collectionTagsEvent.find(collectionTag) != collectionTagsEvent.end()) {
0505       const ProductID pid(collections[ic].provenance()->productID());
0506       if (offset.find(pid) != offset.end()) {
0507         LogError("TriggerSummaryProducerAOD") << "Duplicate pid: " << pid;
0508       }
0509       offset[pid] = toc.size();
0510       const unsigned int n(collections[ic]->size());
0511       for (unsigned int i = 0; i != n; ++i) {
0512         fillTriggerObject(toc, (*collections[ic])[i]);
0513       }
0514       tags.push_back(collectionTag.encode());
0515       keys.push_back(toc.size());
0516     }
0517 
0518   }  /// end loop over handles
0519 }
0520 
0521 template <typename T>
0522 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc, const T& object) const {
0523   using namespace trigger;
0524   toc.emplace_back(object);
0525 
0526   return;
0527 }
0528 
0529 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc,
0530                                                   const l1extra::L1HFRings& object) const {
0531   using namespace l1extra;
0532   using namespace trigger;
0533 
0534   toc.emplace_back(TriggerL1HfRingEtSums,
0535                    object.hfEtSum(L1HFRings::kRing1PosEta),
0536                    object.hfEtSum(L1HFRings::kRing1NegEta),
0537                    object.hfEtSum(L1HFRings::kRing2PosEta),
0538                    object.hfEtSum(L1HFRings::kRing2NegEta));
0539   toc.emplace_back(TriggerL1HfBitCounts,
0540                    object.hfBitCount(L1HFRings::kRing1PosEta),
0541                    object.hfBitCount(L1HFRings::kRing1NegEta),
0542                    object.hfBitCount(L1HFRings::kRing2PosEta),
0543                    object.hfBitCount(L1HFRings::kRing2NegEta));
0544 
0545   return;
0546 }
0547 
0548 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc,
0549                                                   const l1extra::L1EtMissParticle& object) const {
0550   using namespace l1extra;
0551   using namespace trigger;
0552 
0553   toc.emplace_back(object);
0554   if (object.type() == L1EtMissParticle::kMET) {
0555     toc.emplace_back(TriggerL1ETT, object.etTotal(), 0.0, 0.0, 0.0);
0556   } else if (object.type() == L1EtMissParticle::kMHT) {
0557     toc.emplace_back(TriggerL1HTT, object.etTotal(), 0.0, 0.0, 0.0);
0558   } else {
0559     toc.emplace_back(0, object.etTotal(), 0.0, 0.0, 0.0);
0560   }
0561 
0562   return;
0563 }
0564 
0565 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc,
0566                                                   const reco::PFMET& object) const {
0567   using namespace reco;
0568   using namespace trigger;
0569 
0570   toc.emplace_back(object);
0571   toc.emplace_back(TriggerTET, object.sumEt(), 0.0, 0.0, 0.0);
0572   toc.emplace_back(TriggerMETSig, object.mEtSig(), 0.0, 0.0, 0.0);
0573   toc.emplace_back(TriggerELongit, object.e_longitudinal(), 0.0, 0.0, 0.0);
0574 
0575   return;
0576 }
0577 
0578 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc,
0579                                                   const reco::CaloMET& object) const {
0580   using namespace reco;
0581   using namespace trigger;
0582 
0583   toc.emplace_back(object);
0584   toc.emplace_back(TriggerTET, object.sumEt(), 0.0, 0.0, 0.0);
0585   toc.emplace_back(TriggerMETSig, object.mEtSig(), 0.0, 0.0, 0.0);
0586   toc.emplace_back(TriggerELongit, object.e_longitudinal(), 0.0, 0.0, 0.0);
0587 
0588   return;
0589 }
0590 
0591 void TriggerSummaryProducerAOD::fillTriggerObject(trigger::TriggerObjectCollection& toc,
0592                                                   const reco::MET& object) const {
0593   using namespace reco;
0594   using namespace trigger;
0595 
0596   toc.emplace_back(object);
0597   toc.emplace_back(TriggerTHT, object.sumEt(), 0.0, 0.0, 0.0);
0598   toc.emplace_back(TriggerMHTSig, object.mEtSig(), 0.0, 0.0, 0.0);
0599   toc.emplace_back(TriggerHLongit, object.e_longitudinal(), 0.0, 0.0, 0.0);
0600 
0601   return;
0602 }
0603 
0604 template <typename C>
0605 void TriggerSummaryProducerAOD::fillFilterObjectMembers(const edm::Event& iEvent,
0606                                                         const edm::InputTag& tag,
0607                                                         const trigger::Vids& ids,
0608                                                         const std::vector<edm::Ref<C>>& refs,
0609                                                         const ProductIDtoIndex& offset,
0610                                                         trigger::Keys& keys,
0611                                                         trigger::Vids& oIDs) const {
0612   /// this routine takes a vector of Ref<C>s and determines the
0613   /// corresponding vector of keys (i.e., indices) into the
0614   /// TriggerObjectCollection
0615 
0616   using namespace std;
0617   using namespace edm;
0618   using namespace reco;
0619   using namespace l1extra;
0620   using namespace trigger;
0621 
0622   if (ids.size() != refs.size()) {
0623     LogError("TriggerSummaryProducerAOD") << "Vector length is different: " << ids.size() << " " << refs.size();
0624   }
0625 
0626   const unsigned int n(min(ids.size(), refs.size()));
0627   for (unsigned int i = 0; i != n; ++i) {
0628     const ProductID pid(refs[i].id());
0629     if (!(pid.isValid())) {
0630       std::ostringstream ost;
0631       ost << "Iinvalid pid: " << pid << " FilterTag / Key: " << tag.encode() << " / " << i << "of" << n
0632           << " CollectionTag / Key: "
0633           << " <Unrecoverable>"
0634           << " / " << refs[i].key() << " CollectionType: " << typeid(C).name();
0635       if (throw_) {
0636         throw cms::Exception("TriggerSummaryProducerAOD") << ost.str();
0637       } else {
0638         LogError("TriggerSummaryProducerAOD") << ost.str();
0639       }
0640     } else {
0641       auto itOffset = offset.find(pid);
0642       if (itOffset == offset.end()) {
0643         const auto& prov = iEvent.getStableProvenance(pid);
0644         const string& label(prov.moduleLabel());
0645         const string& instance(prov.productInstanceName());
0646         const string& process(prov.processName());
0647         std::ostringstream ost;
0648         ost << "Uunknown pid: " << pid << " FilterTag / Key: " << tag.encode() << " / " << i << "of" << n
0649             << " CollectionTag / Key: " << InputTag(label, instance, process).encode() << " / " << refs[i].key()
0650             << " CollectionType: " << typeid(C).name();
0651         if (throw_) {
0652           throw cms::Exception("TriggerSummaryProducerAOD") << ost.str();
0653         } else {
0654           LogError("TriggerSummaryProducerAOD") << ost.str();
0655         }
0656       } else {
0657         fillFilterObjectMember(keys, oIDs, itOffset->second, ids[i], refs[i]);
0658       }
0659     }
0660   }
0661   return;
0662 }
0663 
0664 template <typename C>
0665 void TriggerSummaryProducerAOD::fillFilterObjectMember(
0666     trigger::Keys& keys, trigger::Vids& ids, const int& offset, const int& id, const edm::Ref<C>& ref) const {
0667   keys.push_back(offset + ref.key());
0668   ids.push_back(id);
0669 
0670   return;
0671 }
0672 
0673 void TriggerSummaryProducerAOD::fillFilterObjectMember(trigger::Keys& keys,
0674                                                        trigger::Vids& ids,
0675                                                        const int& offset,
0676                                                        const int& id,
0677                                                        const edm::Ref<l1extra::L1HFRingsCollection>& ref) const {
0678   using namespace trigger;
0679 
0680   if (id == TriggerL1HfBitCounts) {
0681     keys.push_back(offset + 2 * ref.key() + 1);
0682   } else {  // if (ids[i]==TriggerL1HfRingEtSums) {
0683     keys.push_back(offset + 2 * ref.key() + 0);
0684   }
0685   ids.push_back(id);
0686 
0687   return;
0688 }
0689 
0690 void TriggerSummaryProducerAOD::fillFilterObjectMember(trigger::Keys& keys,
0691                                                        trigger::Vids& ids,
0692                                                        const int& offset,
0693                                                        const int& id,
0694                                                        const edm::Ref<l1extra::L1EtMissParticleCollection>& ref) const {
0695   using namespace trigger;
0696 
0697   if ((id == TriggerL1ETT) || (id == TriggerL1HTT)) {
0698     keys.push_back(offset + 2 * ref.key() + 1);
0699   } else {
0700     keys.push_back(offset + 2 * ref.key() + 0);
0701   }
0702   ids.push_back(id);
0703 
0704   return;
0705 }
0706 
0707 void TriggerSummaryProducerAOD::fillFilterObjectMember(trigger::Keys& keys,
0708                                                        trigger::Vids& ids,
0709                                                        const int& offset,
0710                                                        const int& id,
0711                                                        const edm::Ref<reco::PFMETCollection>& ref) const {
0712   using namespace trigger;
0713 
0714   if ((id == TriggerTHT) || (id == TriggerTET)) {
0715     keys.push_back(offset + 4 * ref.key() + 1);
0716   } else if ((id == TriggerMETSig) || (id == TriggerMHTSig)) {
0717     keys.push_back(offset + 4 * ref.key() + 2);
0718   } else if ((id == TriggerELongit) || (id == TriggerHLongit)) {
0719     keys.push_back(offset + 4 * ref.key() + 3);
0720   } else {
0721     keys.push_back(offset + 4 * ref.key() + 0);
0722   }
0723   ids.push_back(id);
0724 
0725   return;
0726 }
0727 
0728 void TriggerSummaryProducerAOD::fillFilterObjectMember(trigger::Keys& keys,
0729                                                        trigger::Vids& ids,
0730                                                        const int& offset,
0731                                                        const int& id,
0732                                                        const edm::Ref<reco::CaloMETCollection>& ref) const {
0733   using namespace trigger;
0734 
0735   if ((id == TriggerTHT) || (id == TriggerTET)) {
0736     keys.push_back(offset + 4 * ref.key() + 1);
0737   } else if ((id == TriggerMETSig) || (id == TriggerMHTSig)) {
0738     keys.push_back(offset + 4 * ref.key() + 2);
0739   } else if ((id == TriggerELongit) || (id == TriggerHLongit)) {
0740     keys.push_back(offset + 4 * ref.key() + 3);
0741   } else {
0742     keys.push_back(offset + 4 * ref.key() + 0);
0743   }
0744   ids.push_back(id);
0745 
0746   return;
0747 }
0748 
0749 void TriggerSummaryProducerAOD::fillFilterObjectMember(trigger::Keys& keys,
0750                                                        trigger::Vids& ids,
0751                                                        const int& offset,
0752                                                        const int& id,
0753                                                        const edm::Ref<reco::METCollection>& ref) const {
0754   using namespace trigger;
0755 
0756   if ((id == TriggerTHT) || (id == TriggerTET)) {
0757     keys.push_back(offset + 4 * ref.key() + 1);
0758   } else if ((id == TriggerMETSig) || (id == TriggerMHTSig)) {
0759     keys.push_back(offset + 4 * ref.key() + 2);
0760   } else if ((id == TriggerELongit) || (id == TriggerHLongit)) {
0761     keys.push_back(offset + 4 * ref.key() + 3);
0762   } else {
0763     keys.push_back(offset + 4 * ref.key() + 0);
0764   }
0765   ids.push_back(id);
0766 
0767   return;
0768 }
0769 
0770 void TriggerSummaryProducerAOD::endJob() {
0771   using namespace std;
0772   using namespace edm;
0773   using namespace trigger;
0774 
0775   LogVerbatim("TriggerSummaryProducerAOD") << endl;
0776   LogVerbatim("TriggerSummaryProducerAOD") << "TriggerSummaryProducerAOD::globalEndJob - accumulated tags:" << endl;
0777 
0778   InputTagSet filterTags(false);
0779   InputTagSet collectionTags(false);
0780 
0781   filterTags.insert(filterTagsGlobal_.begin(), filterTagsGlobal_.end());
0782   collectionTags.insert(collectionTagsGlobal_.begin(), collectionTagsGlobal_.end());
0783 
0784   const unsigned int nc(collectionTags.size());
0785   const unsigned int nf(filterTags.size());
0786   LogVerbatim("TriggerSummaryProducerAOD") << " Overall number of Collections/Filters: " << nc << "/" << nf << endl;
0787 
0788   LogVerbatim("TriggerSummaryProducerAOD") << " The collections: " << nc << endl;
0789   const InputTagSet::const_iterator cb(collectionTags.begin());
0790   const InputTagSet::const_iterator ce(collectionTags.end());
0791   for (InputTagSet::const_iterator ci = cb; ci != ce; ++ci) {
0792     LogVerbatim("TriggerSummaryProducerAOD") << "  " << distance(cb, ci) << " " << ci->encode() << endl;
0793   }
0794 
0795   LogVerbatim("TriggerSummaryProducerAOD") << " The filters:" << nf << endl;
0796   const InputTagSet::const_iterator fb(filterTags.begin());
0797   const InputTagSet::const_iterator fe(filterTags.end());
0798   for (InputTagSet::const_iterator fi = fb; fi != fe; ++fi) {
0799     LogVerbatim("TriggerSummaryProducerAOD") << "  " << distance(fb, fi) << " " << fi->encode() << endl;
0800   }
0801 
0802   LogVerbatim("TriggerSummaryProducerAOD") << "TriggerSummaryProducerAOD::endJob." << endl;
0803   LogVerbatim("TriggerSummaryProducerAOD") << endl;
0804 
0805   return;
0806 }