Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-08 05:47:14

0001 // -*- C++ -*-
0002 //
0003 // Package:    ReducedRecHitCollectionProducer
0004 // Class:      ReducedRecHitCollectionProducer
0005 //
0006 /**\class ReducedRecHitCollectionProducer ReducedRecHitCollectionProducer.cc Calibration/EcalAlCaRecoProducers/src/ReducedRecHitCollectionProducer.cc
0007 
0008 Original author: Paolo Meridiani PH/CMG
0009  
0010 Implementation:
0011  <Notes on implementation>
0012 */
0013 
0014 #include <iostream>
0015 #include <memory>
0016 
0017 #include "DataFormats/DetId/interface/DetIdCollection.h"
0018 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0019 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0020 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0021 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
0022 #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
0023 #include "FWCore/Framework/interface/ESHandle.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/EventSetup.h"
0026 #include "FWCore/Framework/interface/MakerMacros.h"
0027 #include "FWCore/Framework/interface/stream/EDProducer.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/InputTag.h"
0031 #include "FWCore/Utilities/interface/transform.h"
0032 #include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
0033 #include "Geometry/CaloTopology/interface/CaloTopology.h"
0034 #include "Geometry/Records/interface/CaloTopologyRecord.h"
0035 
0036 class ReducedRecHitCollectionProducer : public edm::stream::EDProducer<> {
0037 public:
0038   //! ctor
0039   explicit ReducedRecHitCollectionProducer(const edm::ParameterSet&);
0040   ~ReducedRecHitCollectionProducer() override;
0041   //! producer
0042   void produce(edm::Event&, const edm::EventSetup&) override;
0043 
0044 private:
0045   // ----------member data ---------------------------
0046   edm::EDGetTokenT<EcalRecHitCollection> recHitsToken_;
0047   std::vector<edm::EDGetTokenT<DetIdCollection>> interestingDetIdCollections_;
0048   std::string reducedHitsCollection_;
0049 };
0050 
0051 #include "FWCore/Framework/interface/MakerMacros.h"
0052 DEFINE_FWK_MODULE(ReducedRecHitCollectionProducer);
0053 
0054 ReducedRecHitCollectionProducer::ReducedRecHitCollectionProducer(const edm::ParameterSet& iConfig) {
0055   recHitsToken_ = consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("recHitsLabel"));
0056 
0057   interestingDetIdCollections_ =
0058       edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag>>("interestingDetIdCollections"),
0059                             [this](edm::InputTag const& tag) { return consumes<DetIdCollection>(tag); });
0060 
0061   reducedHitsCollection_ = iConfig.getParameter<std::string>("reducedHitsCollection");
0062 
0063   //register your products
0064   produces<EcalRecHitCollection>(reducedHitsCollection_);
0065 }
0066 
0067 ReducedRecHitCollectionProducer::~ReducedRecHitCollectionProducer() {}
0068 
0069 // ------------ method called to produce the data  ------------
0070 void ReducedRecHitCollectionProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0071   using namespace edm;
0072   using namespace std;
0073 
0074   if (interestingDetIdCollections_.empty()) {
0075     edm::LogError("ReducedRecHitCollectionProducer") << "VInputTag collections empty";
0076     return;
0077   }
0078 
0079   Handle<DetIdCollection> detIds;
0080   iEvent.getByToken(interestingDetIdCollections_[0], detIds);
0081   std::vector<DetId> xtalsToStore((*detIds).size());
0082   std::copy((*detIds).begin(), (*detIds).end(), xtalsToStore.begin());
0083 
0084   //Merging DetIds from different collections
0085   for (unsigned int t = 1; t < interestingDetIdCollections_.size(); ++t) {
0086     Handle<DetIdCollection> detId;
0087     iEvent.getByToken(interestingDetIdCollections_[t], detId);
0088     if (!detId.isValid()) {
0089       Labels labels;
0090       labelsForToken(interestingDetIdCollections_[t], labels);
0091       edm::LogError("MissingInput") << "no reason to skip detid from : (" << labels.module << ", "
0092                                     << labels.productInstance << ", " << labels.process << ")" << std::endl;
0093       continue;
0094     }
0095 
0096     for (unsigned int ii = 0; ii < (*detId).size(); ii++) {
0097       if (std::find(xtalsToStore.begin(), xtalsToStore.end(), (*detId)[ii]) == xtalsToStore.end())
0098         xtalsToStore.push_back((*detId)[ii]);
0099     }
0100   }
0101 
0102   Handle<EcalRecHitCollection> recHitsHandle;
0103   iEvent.getByToken(recHitsToken_, recHitsHandle);
0104   if (!recHitsHandle.isValid()) {
0105     edm::LogError("ReducedRecHitCollectionProducer") << "RecHit collection not found";
0106     return;
0107   }
0108 
0109   //Create empty output collections
0110   auto miniRecHitCollection = std::make_unique<EcalRecHitCollection>();
0111 
0112   for (unsigned int iCry = 0; iCry < xtalsToStore.size(); iCry++) {
0113     EcalRecHitCollection::const_iterator iRecHit = recHitsHandle->find(xtalsToStore[iCry]);
0114     if ((iRecHit != recHitsHandle->end()) &&
0115         (miniRecHitCollection->find(xtalsToStore[iCry]) == miniRecHitCollection->end()))
0116       miniRecHitCollection->push_back(*iRecHit);
0117   }
0118 
0119   std::sort(xtalsToStore.begin(), xtalsToStore.end());
0120   std::unique(xtalsToStore.begin(), xtalsToStore.end());
0121 
0122   //   std::cout << "New Collection " << reducedHitsCollection_ << " size is " << miniRecHitCollection->size() << " original is " << recHitsHandle->size() << std::endl;
0123   iEvent.put(std::move(miniRecHitCollection), reducedHitsCollection_);
0124 }