File indexing completed on 2023-03-17 11:14:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "FWCore/Framework/interface/stream/EDProducer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015
0016 #include "DataFormats/Math/interface/deltaR.h"
0017 #include "DataFormats/Math/interface/deltaPhi.h"
0018
0019 #include "DataFormats/Common/interface/Association.h"
0020 #include "DataFormats/Common/interface/ValueMap.h"
0021 #include "DataFormats/Common/interface/Ptr.h"
0022 #include "DataFormats/Common/interface/View.h"
0023
0024 #include "MuonAnalysis/MuonAssociators/interface/L1MuonMatcherAlgo.h"
0025 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0026
0027 #include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
0028
0029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0030
0031 namespace pat {
0032
0033 class HLTL1MuonMatcher : public edm::stream::EDProducer<> {
0034 public:
0035 explicit HLTL1MuonMatcher(const edm::ParameterSet &iConfig);
0036 ~HLTL1MuonMatcher() override {}
0037
0038 void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0039
0040
0041 bool operator()(const pat::TriggerObjectStandAlone &l1) const {
0042 if (resolveAmbiguities_ && (std::find(lockedItems_.begin(), lockedItems_.end(), &l1) != lockedItems_.end()))
0043 return false;
0044 return selector_(l1);
0045 }
0046
0047 private:
0048 typedef pat::TriggerObjectStandAlone PATPrimitive;
0049 typedef pat::TriggerObjectStandAloneCollection PATPrimitiveCollection;
0050 typedef pat::TriggerObjectStandAloneMatch PATTriggerAssociation;
0051
0052 L1MuonMatcherAlgo matcher_;
0053
0054
0055 edm::EDGetTokenT<edm::View<reco::Candidate> > recoToken_;
0056 edm::EDGetTokenT<PATPrimitiveCollection> l1Token_;
0057
0058
0059 StringCutObjectSelector<PATPrimitive> selector_;
0060 bool resolveAmbiguities_;
0061
0062
0063 std::string labelProp_;
0064
0065
0066 bool writeExtraInfo_;
0067
0068
0069 template <typename Hand, typename T>
0070 void storeExtraInfo(edm::Event &iEvent,
0071 const Hand &handle,
0072 const std::vector<T> &values,
0073 const std::string &label) const;
0074
0075
0076 std::vector<const pat::TriggerObjectStandAlone *> lockedItems_;
0077 };
0078
0079 }
0080
0081 pat::HLTL1MuonMatcher::HLTL1MuonMatcher(const edm::ParameterSet &iConfig)
0082 : matcher_(iConfig, consumesCollector()),
0083 recoToken_(consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("src"))),
0084 l1Token_(consumes<PATPrimitiveCollection>(iConfig.getParameter<edm::InputTag>("matched"))),
0085 selector_{iConfig.getParameter<std::string>("matchedCuts")},
0086 resolveAmbiguities_(iConfig.getParameter<bool>("resolveAmbiguities")),
0087 labelProp_(iConfig.getParameter<std::string>("setPropLabel")),
0088 writeExtraInfo_(iConfig.existsAs<bool>("writeExtraInfo") ? iConfig.getParameter<bool>("writeExtraInfo") : false) {
0089 produces<PATPrimitiveCollection>("propagatedReco");
0090 produces<PATTriggerAssociation>("propagatedReco");
0091 produces<PATTriggerAssociation>();
0092 if (writeExtraInfo_) {
0093 produces<edm::ValueMap<float> >("deltaR");
0094 produces<edm::ValueMap<float> >("deltaPhi");
0095 }
0096 }
0097
0098 void pat::HLTL1MuonMatcher::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0099 using namespace edm;
0100 using namespace std;
0101
0102 matcher_.init(iSetup);
0103
0104 Handle<View<reco::Candidate> > reco;
0105 Handle<PATPrimitiveCollection> l1s;
0106
0107 iEvent.getByToken(recoToken_, reco);
0108 iEvent.getByToken(l1Token_, l1s);
0109
0110 unique_ptr<PATPrimitiveCollection> propOut(new PATPrimitiveCollection());
0111 vector<int> propMatches(reco->size(), -1);
0112 vector<int> fullMatches(reco->size(), -1);
0113 vector<float> deltaRs(reco->size(), 999), deltaPhis(reco->size(), 999);
0114 lockedItems_.clear();
0115 for (int i = 0, n = reco->size(); i < n; ++i) {
0116 TrajectoryStateOnSurface propagated;
0117 const reco::Candidate &mu = (*reco)[i];
0118 int match = matcher_.matchGeneric(mu, *l1s, *this, deltaRs[i], deltaPhis[i], propagated);
0119 if (propagated.isValid()) {
0120 GlobalPoint pos = propagated.globalPosition();
0121 propMatches[i] = propOut->size();
0122 propOut->push_back(PATPrimitive(math::PtEtaPhiMLorentzVector(mu.pt(), pos.eta(), pos.phi(), mu.mass())));
0123 propOut->back().addFilterLabel(labelProp_);
0124 propOut->back().setCharge(mu.charge());
0125 }
0126 fullMatches[i] = match;
0127 if (match != -1) {
0128 lockedItems_.push_back(&(*l1s)[match]);
0129 }
0130 }
0131 lockedItems_.clear();
0132
0133 OrphanHandle<PATPrimitiveCollection> propDone = iEvent.put(std::move(propOut), "propagatedReco");
0134
0135 unique_ptr<PATTriggerAssociation> propAss(new PATTriggerAssociation(propDone));
0136 PATTriggerAssociation::Filler propFiller(*propAss);
0137 propFiller.insert(reco, propMatches.begin(), propMatches.end());
0138 propFiller.fill();
0139 iEvent.put(std::move(propAss), "propagatedReco");
0140
0141 unique_ptr<PATTriggerAssociation> fullAss(new PATTriggerAssociation(l1s));
0142 PATTriggerAssociation::Filler fullFiller(*fullAss);
0143 fullFiller.insert(reco, fullMatches.begin(), fullMatches.end());
0144 fullFiller.fill();
0145 iEvent.put(std::move(fullAss));
0146
0147 if (writeExtraInfo_) {
0148 storeExtraInfo(iEvent, reco, deltaRs, "deltaR");
0149 storeExtraInfo(iEvent, reco, deltaPhis, "deltaPhi");
0150 }
0151 }
0152
0153 template <typename Hand, typename T>
0154 void pat::HLTL1MuonMatcher::storeExtraInfo(edm::Event &iEvent,
0155 const Hand &handle,
0156 const std::vector<T> &values,
0157 const std::string &label) const {
0158 using namespace edm;
0159 using namespace std;
0160 unique_ptr<ValueMap<T> > valMap(new ValueMap<T>());
0161 typename edm::ValueMap<T>::Filler filler(*valMap);
0162 filler.insert(handle, values.begin(), values.end());
0163 filler.fill();
0164 iEvent.put(std::move(valMap), label);
0165 }
0166
0167 #include "FWCore/Framework/interface/MakerMacros.h"
0168 using namespace pat;
0169 DEFINE_FWK_MODULE(HLTL1MuonMatcher);