Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-16 06:15:46

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