Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-20 03:14:17

0001 #include "TauAnalysis/MCEmbeddingTools/plugins/CaloCleaner.h"
0002 
0003 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
0004 #include "DataFormats/HcalRecHit/interface/HBHERecHit.h"
0005 #include "DataFormats/HcalRecHit/interface/HFRecHit.h"
0006 #include "DataFormats/HcalRecHit/interface/HORecHit.h"
0007 #include "DataFormats/HcalRecHit/interface/ZDCRecHit.h"
0008 
0009 typedef CaloCleaner<EcalRecHit> EcalRecHitColCleaner;
0010 typedef CaloCleaner<HBHERecHit> HBHERecHitColCleaner;
0011 typedef CaloCleaner<HFRecHit> HFRecHitColCleaner;
0012 typedef CaloCleaner<HORecHit> HORecHitColCleaner;
0013 typedef CaloCleaner<ZDCRecHit> ZDCRecHitColCleaner;
0014 
0015 template <typename T>
0016 CaloCleaner<T>::CaloCleaner(const edm::ParameterSet &iConfig)
0017     : mu_input_(consumes<edm::View<pat::Muon>>(iConfig.getParameter<edm::InputTag>("MuonCollection"))),
0018       propagatorToken_(esConsumes(edm::ESInputTag("", "SteppingHelixPropagatorAny"))) {
0019   std::vector<edm::InputTag> inCollections = iConfig.getParameter<std::vector<edm::InputTag>>("oldCollection");
0020   for (const auto &inCollection : inCollections) {
0021     inputs_[inCollection.instance()] = consumes<RecHitCollection>(inCollection);
0022     produces<RecHitCollection>(inCollection.instance());
0023   }
0024 
0025   is_preshower_ = iConfig.getUntrackedParameter<bool>("is_preshower", false);
0026   edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
0027   edm::ConsumesCollector iC = consumesCollector();
0028   parameters_.loadParameters(parameters, iC);
0029 }
0030 
0031 template <typename T>
0032 CaloCleaner<T>::~CaloCleaner() {
0033   // nothing to be done yet...
0034 }
0035 
0036 template <typename T>
0037 void CaloCleaner<T>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0038   auto const &propagator = iSetup.getData(propagatorToken_);
0039   trackAssociator_.setPropagator(&propagator);
0040 
0041   edm::Handle<edm::View<pat::Muon>> muonHandle;
0042   iEvent.getByToken(mu_input_, muonHandle);
0043   edm::View<pat::Muon> muons = *muonHandle;
0044 
0045   std::map<uint32_t, float> correction_map;
0046 
0047   // Fill the correction map
0048   for (edm::View<pat::Muon>::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
0049     const reco::Track *track = nullptr;
0050     if (iMuon->track().isNonnull())
0051       track = iMuon->track().get();
0052     else if (iMuon->standAloneMuon().isNonnull())
0053       track = iMuon->standAloneMuon().get();
0054     else
0055       throw cms::Exception("FatalError")
0056           << "Failed to fill muon id information for a muon with undefined references to tracks";
0057     TrackDetMatchInfo info =
0058         trackAssociator_.associate(iEvent, iSetup, *track, parameters_, TrackDetectorAssociator::Any);
0059     fill_correction_map(&info, &correction_map);
0060   }
0061 
0062   // Copy the old collection and correct if necessary
0063   for (const auto &input_ : inputs_) {
0064     std::unique_ptr<RecHitCollection> recHitCollection_output(new RecHitCollection());
0065     edm::Handle<RecHitCollection> recHitCollection;
0066     iEvent.getByToken(input_.second, recHitCollection);
0067     for (typename RecHitCollection::const_iterator recHit = recHitCollection->begin();
0068          recHit != recHitCollection->end();
0069          ++recHit) {
0070       if (correction_map[recHit->detid().rawId()] > 0) {
0071         float new_energy = recHit->energy() - correction_map[recHit->detid().rawId()];
0072         if (new_energy <= 0)
0073           continue;  // Do not save empty Hits
0074         T newRecHit(*recHit);
0075         newRecHit.setEnergy(new_energy);
0076         recHitCollection_output->push_back(newRecHit);
0077       } else {
0078         recHitCollection_output->push_back(*recHit);
0079       }
0080     }
0081     // Save the new collection
0082     recHitCollection_output->sort();
0083     iEvent.put(std::move(recHitCollection_output), input_.first);
0084   }
0085 }
0086 
0087 //-------------------------------------------------------------------------------
0088 // define 'buildRecHit' functions used for different types of recHits
0089 //-------------------------------------------------------------------------------
0090 
0091 template <typename T>
0092 void CaloCleaner<T>::fill_correction_map(TrackDetMatchInfo *, std::map<uint32_t, float> *) {
0093   assert(0);
0094 }
0095 
0096 template <>
0097 void CaloCleaner<EcalRecHit>::fill_correction_map(TrackDetMatchInfo *info, std::map<uint32_t, float> *cor_map) {
0098   if (is_preshower_) {
0099     for (std::vector<DetId>::const_iterator detId = info->crossedPreshowerIds.begin();
0100          detId != info->crossedPreshowerIds.end();
0101          ++detId) {
0102       (*cor_map)[detId->rawId()] = 9999999;  // just remove all energy (Below 0 is not possible)
0103     }
0104   } else {
0105     for (std::vector<const EcalRecHit *>::const_iterator hit = info->crossedEcalRecHits.begin();
0106          hit != info->crossedEcalRecHits.end();
0107          hit++) {
0108       (*cor_map)[(*hit)->detid().rawId()] = (*hit)->energy();
0109     }
0110   }
0111 }
0112 
0113 template <>
0114 void CaloCleaner<HBHERecHit>::fill_correction_map(TrackDetMatchInfo *info, std::map<uint32_t, float> *cor_map) {
0115   for (std::vector<const HBHERecHit *>::const_iterator hit = info->crossedHcalRecHits.begin();
0116        hit != info->crossedHcalRecHits.end();
0117        hit++) {
0118     (*cor_map)[(*hit)->detid().rawId()] = (*hit)->energy();
0119   }
0120 }
0121 
0122 template <>
0123 void CaloCleaner<HORecHit>::fill_correction_map(TrackDetMatchInfo *info, std::map<uint32_t, float> *cor_map) {
0124   for (std::vector<const HORecHit *>::const_iterator hit = info->crossedHORecHits.begin();
0125        hit != info->crossedHORecHits.end();
0126        hit++) {
0127     (*cor_map)[(*hit)->detid().rawId()] = (*hit)->energy();
0128   }
0129 }
0130 
0131 template <>
0132 void CaloCleaner<HFRecHit>::fill_correction_map(TrackDetMatchInfo *info, std::map<uint32_t, float> *cor_map) {
0133   return;  // No corrections for HF
0134 }
0135 
0136 template <>
0137 void CaloCleaner<ZDCRecHit>::fill_correction_map(TrackDetMatchInfo *info, std::map<uint32_t, float> *cor_map) {
0138   return;  // No corrections for ZDC
0139 }
0140 
0141 DEFINE_FWK_MODULE(EcalRecHitColCleaner);
0142 DEFINE_FWK_MODULE(HBHERecHitColCleaner);
0143 DEFINE_FWK_MODULE(HORecHitColCleaner);
0144 // no  need for cleaning outside of tracker, so just a copy of the old collection
0145 DEFINE_FWK_MODULE(HFRecHitColCleaner);
0146 DEFINE_FWK_MODULE(ZDCRecHitColCleaner);