|
| 1 | +#include "FWCore/Framework/interface/global/EDProducer.h" |
| 2 | +#include "FWCore/Framework/interface/Event.h" |
| 3 | +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" |
| 4 | +#include "DataFormats/TrackReco/interface/DeDxData.h" |
| 5 | +#include "DataFormats/TrackReco/interface/DeDxHitInfo.h" |
| 6 | +#include "DataFormats/TrackReco/interface/TrackFwd.h" |
| 7 | +#include "DataFormats/PatCandidates/interface/PackedCandidate.h" |
| 8 | + |
| 9 | +// |
| 10 | +// class declaration |
| 11 | +// |
| 12 | + |
| 13 | +class DeDxEstimatorRekeyer : public edm::global::EDProducer<> { |
| 14 | +public: |
| 15 | + explicit DeDxEstimatorRekeyer(const edm::ParameterSet&); |
| 16 | + ~DeDxEstimatorRekeyer() override{}; |
| 17 | + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); |
| 18 | + |
| 19 | +private: |
| 20 | + void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override; |
| 21 | + |
| 22 | + template <typename T> |
| 23 | + std::map<std::string, edm::EDGetTokenT<T>> getTokens(const std::vector<edm::InputTag>& tags) { |
| 24 | + std::map<std::string, edm::EDGetTokenT<T>> tokens; |
| 25 | + for (const auto& tag : tags) |
| 26 | + tokens.emplace(tag.label(), consumes<T>(tag)); |
| 27 | + return tokens; |
| 28 | + }; |
| 29 | + |
| 30 | + // ----------member data --------------------------- |
| 31 | + const edm::EDGetTokenT<reco::TrackCollection> tracksToken_; |
| 32 | + const edm::EDGetTokenT<reco::DeDxHitInfoAss> dedxHitAssToken_; |
| 33 | + const std::map<std::string, edm::EDGetTokenT<edm::ValueMap<reco::DeDxData>>> dedxEstimatorsTokens_; |
| 34 | + const std::map<std::string, edm::EDGetTokenT<pat::PackedCandidateCollection>> packedCandidatesTokens_; |
| 35 | + const std::map<std::string, edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>>> trk2pcTokens_; |
| 36 | +}; |
| 37 | + |
| 38 | +void DeDxEstimatorRekeyer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { |
| 39 | + edm::ParameterSetDescription desc; |
| 40 | + desc.add<edm::InputTag>("tracks", {"generalTracks"}); |
| 41 | + desc.add<edm::InputTag>("dedxHits", {"dedxHitInfo"}); |
| 42 | + desc.add<std::vector<edm::InputTag>>( |
| 43 | + "packedCandidates", |
| 44 | + {edm::InputTag("packedPFCandidates"), edm::InputTag("lostTracks"), edm::InputTag("lostTracks:eleTracks")}); |
| 45 | + desc.add<std::vector<edm::InputTag>>("dedxEstimators", |
| 46 | + {edm::InputTag("dedxHarmonic2"), edm::InputTag("dedxPixelHarmonic2")}); |
| 47 | + descriptions.addWithDefaultLabel(desc); |
| 48 | +} |
| 49 | + |
| 50 | +DeDxEstimatorRekeyer::DeDxEstimatorRekeyer(const edm::ParameterSet& iConfig) |
| 51 | + : tracksToken_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"))), |
| 52 | + dedxHitAssToken_(consumes<reco::DeDxHitInfoAss>(iConfig.getParameter<edm::InputTag>("dedxHits"))), |
| 53 | + dedxEstimatorsTokens_( |
| 54 | + getTokens<edm::ValueMap<reco::DeDxData>>(iConfig.getParameter<std::vector<edm::InputTag>>("dedxEstimators"))), |
| 55 | + packedCandidatesTokens_(getTokens<pat::PackedCandidateCollection>( |
| 56 | + iConfig.getParameter<std::vector<edm::InputTag>>("packedCandidates"))), |
| 57 | + trk2pcTokens_(getTokens<edm::Association<pat::PackedCandidateCollection>>( |
| 58 | + iConfig.getParameter<std::vector<edm::InputTag>>("packedCandidates"))) { |
| 59 | + for (const auto& d : dedxEstimatorsTokens_) |
| 60 | + produces<edm::ValueMap<reco::DeDxData>>(d.first); |
| 61 | + produces<reco::DeDxHitInfoCollection>(); |
| 62 | + produces<reco::DeDxHitInfoAss>(); |
| 63 | +} |
| 64 | + |
| 65 | +void DeDxEstimatorRekeyer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const { |
| 66 | + // Get input collections |
| 67 | + const auto& tracks = iEvent.getHandle(tracksToken_); |
| 68 | + std::vector<reco::TrackRef> trackRefs; |
| 69 | + trackRefs.reserve(tracks->size()); |
| 70 | + for (auto track = tracks->begin(); track != tracks->end(); track++) |
| 71 | + trackRefs.emplace_back(tracks, track - tracks->begin()); |
| 72 | + |
| 73 | + typedef std::map<pat::PackedCandidateRef, reco::TrackRef> PCTrkMap; |
| 74 | + std::vector<std::pair<edm::Handle<pat::PackedCandidateCollection>, PCTrkMap>> pcTrkMap; |
| 75 | + pcTrkMap.reserve(packedCandidatesTokens_.size()); |
| 76 | + for (const auto& p : packedCandidatesTokens_) { |
| 77 | + PCTrkMap map; |
| 78 | + const auto& trk2pc = iEvent.get(trk2pcTokens_.at(p.first)); |
| 79 | + for (const auto& track : trackRefs) { |
| 80 | + const auto& pc = trk2pc[track]; |
| 81 | + if (pc.isNonnull()) |
| 82 | + map.emplace(pc, track); |
| 83 | + } |
| 84 | + pcTrkMap.emplace_back(iEvent.getHandle(p.second), map); |
| 85 | + } |
| 86 | + |
| 87 | + // Rekey dEdx estimators |
| 88 | + for (const auto& d : dedxEstimatorsTokens_) { |
| 89 | + const auto& dedxEstimators = iEvent.get(d.second); |
| 90 | + auto trackDeDxValueMap = std::make_unique<edm::ValueMap<reco::DeDxData>>(); |
| 91 | + edm::ValueMap<reco::DeDxData>::Filler filler(*trackDeDxValueMap); |
| 92 | + // Loop over packed candidates |
| 93 | + for (const auto& h : pcTrkMap) { |
| 94 | + std::vector<reco::DeDxData> dedxEstimate(h.first->size()); |
| 95 | + for (const auto& p : h.second) |
| 96 | + dedxEstimate[p.first.key()] = dedxEstimators[p.second]; |
| 97 | + filler.insert(h.first, dedxEstimate.begin(), dedxEstimate.end()); |
| 98 | + } |
| 99 | + // Fill the value map and put it into the event |
| 100 | + filler.fill(); |
| 101 | + iEvent.put(std::move(trackDeDxValueMap), d.first); |
| 102 | + } |
| 103 | + |
| 104 | + // Rekey dEdx hit info |
| 105 | + const auto& dedxHitAss = iEvent.get(dedxHitAssToken_); |
| 106 | + const auto& dedxHitInfoHandle = iEvent.getRefBeforePut<reco::DeDxHitInfoCollection>(); |
| 107 | + auto dedxHitInfoAssociation = std::make_unique<reco::DeDxHitInfoAss>(dedxHitInfoHandle); |
| 108 | + reco::DeDxHitInfoAss::Filler filler(*dedxHitInfoAssociation); |
| 109 | + auto resultdedxHitColl = std::make_unique<reco::DeDxHitInfoCollection>(); |
| 110 | + resultdedxHitColl->reserve(pcTrkMap.size() > 0 ? pcTrkMap.size() * pcTrkMap[0].second.size() : 0); |
| 111 | + // Loop over packed candidates |
| 112 | + for (const auto& h : pcTrkMap) { |
| 113 | + std::vector<int> indices(h.first->size(), -1); |
| 114 | + for (const auto& p : h.second) { |
| 115 | + indices[p.first.key()] = resultdedxHitColl->size(); |
| 116 | + resultdedxHitColl->emplace_back(*dedxHitAss[p.second]); |
| 117 | + } |
| 118 | + filler.insert(h.first, indices.begin(), indices.end()); |
| 119 | + } |
| 120 | + const auto& dedxHitCollHandle = iEvent.put(std::move(resultdedxHitColl)); |
| 121 | + // Fill the association map and put it into the event |
| 122 | + filler.fill(); |
| 123 | + iEvent.put(std::move(dedxHitInfoAssociation)); |
| 124 | +} |
| 125 | + |
| 126 | +//define this as a plug-in |
| 127 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 128 | +DEFINE_FWK_MODULE(DeDxEstimatorRekeyer); |
0 commit comments