File indexing completed on 2024-04-06 12:23:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0018 #include "DataFormats/Common/interface/RefToPtr.h"
0019 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0020
0021 #include "DataFormats/PatCandidates/interface/Jet.h"
0022 #include "PhysicsTools/PatAlgos/interface/ObjectModifier.h"
0023
0024 namespace pat {
0025
0026 class PATJetSlimmer : public edm::stream::EDProducer<> {
0027 public:
0028 explicit PATJetSlimmer(const edm::ParameterSet& iConfig);
0029 ~PATJetSlimmer() override {}
0030
0031 void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0032
0033 private:
0034 edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> pf2pc_;
0035 edm::EDGetTokenT<edm::ValueMap<reco::CandidatePtr>> pf2pcAny_;
0036 const edm::EDGetTokenT<edm::View<pat::Jet>> jets_;
0037 const StringCutObjectSelector<pat::Jet> dropJetVars_, dropDaughters_, rekeyDaughters_, dropTrackRefs_,
0038 dropSpecific_, dropTagInfos_;
0039 const bool modifyJet_, mayNeedDaughterMap_, mixedDaughters_;
0040 std::unique_ptr<pat::ObjectModifier<pat::Jet>> jetModifier_;
0041 };
0042
0043 }
0044
0045 pat::PATJetSlimmer::PATJetSlimmer(const edm::ParameterSet& iConfig)
0046 : jets_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
0047 dropJetVars_(iConfig.getParameter<std::string>("dropJetVars")),
0048 dropDaughters_(iConfig.getParameter<std::string>("dropDaughters")),
0049 rekeyDaughters_(iConfig.getParameter<std::string>("rekeyDaughters")),
0050 dropTrackRefs_(iConfig.getParameter<std::string>("dropTrackRefs")),
0051 dropSpecific_(iConfig.getParameter<std::string>("dropSpecific")),
0052 dropTagInfos_(iConfig.getParameter<std::string>("dropTagInfos")),
0053 modifyJet_(iConfig.getParameter<bool>("modifyJets")),
0054 mayNeedDaughterMap_(iConfig.getParameter<std::string>("dropDaughters") != "1" &&
0055 iConfig.getParameter<std::string>("rekeyDaughters") != "0"),
0056 mixedDaughters_(iConfig.getParameter<bool>("mixedDaughters")) {
0057 if (mayNeedDaughterMap_) {
0058 if (mixedDaughters_) {
0059 pf2pcAny_ =
0060 consumes<edm::ValueMap<reco::CandidatePtr>>(iConfig.getParameter<edm::InputTag>("packedPFCandidates"));
0061 } else {
0062 pf2pc_ = consumes<edm::Association<pat::PackedCandidateCollection>>(
0063 iConfig.getParameter<edm::InputTag>("packedPFCandidates"));
0064 }
0065 }
0066 if (modifyJet_) {
0067 const edm::ParameterSet& mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
0068 jetModifier_ = std::make_unique<pat::ObjectModifier<pat::Jet>>(mod_config, consumesCollector());
0069 }
0070 produces<std::vector<pat::Jet>>();
0071 }
0072
0073 void pat::PATJetSlimmer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0074 using namespace edm;
0075 using namespace std;
0076
0077 if (modifyJet_)
0078 jetModifier_->setEventContent(iSetup);
0079
0080 Handle<View<pat::Jet>> src;
0081 iEvent.getByToken(jets_, src);
0082 Handle<edm::Association<pat::PackedCandidateCollection>> pf2pc;
0083 Handle<edm::ValueMap<reco::CandidatePtr>> pf2pcAny;
0084 if (mayNeedDaughterMap_) {
0085 if (mixedDaughters_) {
0086 iEvent.getByToken(pf2pcAny_, pf2pcAny);
0087 } else {
0088 iEvent.getByToken(pf2pc_, pf2pc);
0089 }
0090 }
0091
0092 auto out = std::make_unique<std::vector<pat::Jet>>();
0093 out->reserve(src->size());
0094
0095 if (modifyJet_) {
0096 jetModifier_->setEvent(iEvent);
0097 }
0098
0099 for (edm::View<pat::Jet>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0100 out->push_back(*it);
0101 pat::Jet& jet = out->back();
0102
0103 if (modifyJet_) {
0104 jetModifier_->modify(jet);
0105 }
0106
0107 if (dropTagInfos_(*it)) {
0108 jet.tagInfoLabels_.clear();
0109 jet.tagInfos_.clear();
0110 jet.tagInfosFwdPtr_.clear();
0111 }
0112 if (dropJetVars_(*it)) {
0113
0114 jet.setNPasses(0);
0115
0116 }
0117 if (dropTrackRefs_(*it)) {
0118 jet.setAssociatedTracks(reco::TrackRefVector());
0119 }
0120 if (dropDaughters_(*it)) {
0121 jet.clearDaughters();
0122 jet.pfCandidatesFwdPtr_.clear();
0123 jet.caloTowersFwdPtr_.clear();
0124 } else if (rekeyDaughters_(*it)) {
0125
0126 reco::CompositePtrCandidate::daughters old = jet.daughterPtrVector();
0127 jet.clearDaughters();
0128 if (mixedDaughters_) {
0129 std::vector<reco::CandidatePtr> ptrs;
0130 for (const reco::CandidatePtr& oldptr : old) {
0131 ptrs.push_back((*pf2pcAny)[oldptr]);
0132 }
0133 std::sort(ptrs.begin(), ptrs.end());
0134 for (const reco::CandidatePtr& newptr : ptrs) {
0135 jet.addDaughter(newptr);
0136 }
0137 } else {
0138 std::map<unsigned int, reco::CandidatePtr> ptrs;
0139 for (unsigned int i = 0; i < old.size(); i++) {
0140
0141 ptrs[((*pf2pc)[old[i]]).key()] = refToPtr((*pf2pc)[old[i]]);
0142 }
0143 for (std::map<unsigned int, reco::CandidatePtr>::iterator itp = ptrs.begin(); itp != ptrs.end();
0144 itp++)
0145 {
0146 jet.addDaughter(itp->second);
0147 }
0148 }
0149 }
0150 if (dropSpecific_(*it)) {
0151
0152 jet.specificCalo_.clear();
0153 jet.specificPF_.clear();
0154 }
0155
0156
0157
0158
0159 }
0160
0161 iEvent.put(std::move(out));
0162 }
0163
0164 #include "FWCore/Framework/interface/MakerMacros.h"
0165 using namespace pat;
0166 DEFINE_FWK_MODULE(PATJetSlimmer);