Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:25:59

0001 /** \class CaloCleaner
0002  *
0003  * Clean collections of calorimeter recHits
0004  * (detectors supported at the moment: EB/EE, HB/HE and HO)
0005  * 
0006  * \author Tomasz Maciej Frueboes;
0007  *         Christian Veelken, LLR
0008  *
0009  * 
0010  *
0011  *  Clean Up from STefan Wayand, KIT
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   //trackAssociator_.useDefaultPropagator();
0073 }
0074 
0075 template <typename T>
0076 CaloCleaner<T>::~CaloCleaner() {
0077   // nothing to be done yet...
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   // Fill the correction map
0092   for (edm::View<pat::Muon>::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
0093     // get the basic informaiton like fill reco mouon does
0094     //     RecoMuon/MuonIdentification/plugins/MuonIdProducer.cc
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   // Copy the old collection and correct if necessary
0109   for (auto input_ : inputs_) {
0110     std::unique_ptr<RecHitCollection> recHitCollection_output(new RecHitCollection());
0111     edm::Handle<RecHitCollection> recHitCollection;
0112     // iEvent.getByToken(input_.second[0], recHitCollection);
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;  // Do not save empty Hits
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       /* For the inveted collection   
0128      if (correction_map[recHit->detid().rawId()] > 0){
0129     float new_energy =   correction_map[recHit->detid().rawId()];
0130     if (new_energy < 0) new_energy =0;
0131     T newRecHit(*recHit);
0132     newRecHit.setEnergy(new_energy); 
0133     recHitCollection_output->push_back(newRecHit);
0134       }*/
0135     }
0136     // Save the new collection
0137     recHitCollection_output->sort();
0138     iEvent.put(std::move(recHitCollection_output), input_.first);
0139   }
0140 }
0141 #endif