File indexing completed on 2023-03-17 11:25:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef TauAnalysis_MCEmbeddingTools_CaloCleaner_H
0014 #define TauAnalysis_MCEmbeddingTools_CaloCleaner_H
0015
0016 #include "FWCore/Framework/interface/stream/EDProducer.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "DataFormats/Common/interface/Handle.h"
0020 #include "DataFormats/PatCandidates/interface/Muon.h"
0021 #include "DataFormats/MuonReco/interface/MuonEnergy.h"
0022 #include "FWCore/Framework/interface/MakerMacros.h"
0023
0024 #include "TrackingTools/TrackAssociator/interface/TrackAssociatorParameters.h"
0025 #include "TrackingTools/TrackAssociator/interface/TrackDetectorAssociator.h"
0026 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
0027
0028 #include "DataFormats/Common/interface/SortedCollection.h"
0029 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
0030
0031 #include <string>
0032 #include <iostream>
0033 #include <map>
0034
0035 template <typename T>
0036 class CaloCleaner : public edm::stream::EDProducer<> {
0037 public:
0038 explicit CaloCleaner(const edm::ParameterSet&);
0039 ~CaloCleaner() override;
0040
0041 private:
0042 void produce(edm::Event&, const edm::EventSetup&) override;
0043
0044 typedef edm::SortedCollection<T> RecHitCollection;
0045
0046 const edm::EDGetTokenT<edm::View<pat::Muon> > mu_input_;
0047
0048 std::map<std::string, edm::EDGetTokenT<RecHitCollection> > inputs_;
0049 edm::ESGetToken<Propagator, TrackingComponentsRecord> propagatorToken_;
0050
0051 TrackDetectorAssociator trackAssociator_;
0052 TrackAssociatorParameters parameters_;
0053
0054 bool is_preshower_;
0055 void fill_correction_map(TrackDetMatchInfo*, std::map<uint32_t, float>*);
0056 };
0057
0058 template <typename T>
0059 CaloCleaner<T>::CaloCleaner(const edm::ParameterSet& iConfig)
0060 : mu_input_(consumes<edm::View<pat::Muon> >(iConfig.getParameter<edm::InputTag>("MuonCollection"))),
0061 propagatorToken_(esConsumes(edm::ESInputTag("", "SteppingHelixPropagatorAny"))) {
0062 std::vector<edm::InputTag> inCollections = iConfig.getParameter<std::vector<edm::InputTag> >("oldCollection");
0063 for (const auto& inCollection : inCollections) {
0064 inputs_[inCollection.instance()] = consumes<RecHitCollection>(inCollection);
0065 produces<RecHitCollection>(inCollection.instance());
0066 }
0067
0068 is_preshower_ = iConfig.getUntrackedParameter<bool>("is_preshower", false);
0069 edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
0070 edm::ConsumesCollector iC = consumesCollector();
0071 parameters_.loadParameters(parameters, iC);
0072
0073 }
0074
0075 template <typename T>
0076 CaloCleaner<T>::~CaloCleaner() {
0077
0078 }
0079
0080 template <typename T>
0081 void CaloCleaner<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0082 auto const& propagator = iSetup.getData(propagatorToken_);
0083 trackAssociator_.setPropagator(&propagator);
0084
0085 edm::Handle<edm::View<pat::Muon> > muonHandle;
0086 iEvent.getByToken(mu_input_, muonHandle);
0087 edm::View<pat::Muon> muons = *muonHandle;
0088
0089 std::map<uint32_t, float> correction_map;
0090
0091
0092 for (edm::View<pat::Muon>::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
0093
0094
0095 const reco::Track* track = nullptr;
0096 if (iMuon->track().isNonnull())
0097 track = iMuon->track().get();
0098 else if (iMuon->standAloneMuon().isNonnull())
0099 track = iMuon->standAloneMuon().get();
0100 else
0101 throw cms::Exception("FatalError")
0102 << "Failed to fill muon id information for a muon with undefined references to tracks";
0103 TrackDetMatchInfo info =
0104 trackAssociator_.associate(iEvent, iSetup, *track, parameters_, TrackDetectorAssociator::Any);
0105 fill_correction_map(&info, &correction_map);
0106 }
0107
0108
0109 for (auto input_ : inputs_) {
0110 std::unique_ptr<RecHitCollection> recHitCollection_output(new RecHitCollection());
0111 edm::Handle<RecHitCollection> recHitCollection;
0112
0113 iEvent.getByToken(input_.second, recHitCollection);
0114 for (typename RecHitCollection::const_iterator recHit = recHitCollection->begin();
0115 recHit != recHitCollection->end();
0116 ++recHit) {
0117 if (correction_map[recHit->detid().rawId()] > 0) {
0118 float new_energy = recHit->energy() - correction_map[recHit->detid().rawId()];
0119 if (new_energy <= 0)
0120 continue;
0121 T newRecHit(*recHit);
0122 newRecHit.setEnergy(new_energy);
0123 recHitCollection_output->push_back(newRecHit);
0124 } else {
0125 recHitCollection_output->push_back(*recHit);
0126 }
0127
0128
0129
0130
0131
0132
0133
0134
0135 }
0136
0137 recHitCollection_output->sort();
0138 iEvent.put(std::move(recHitCollection_output), input_.first);
0139 }
0140 }
0141 #endif