Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:25:43

0001 /** \class EcalTPSkimmer
0002  *   produce a subset of TP information
0003  *
0004  *  \author Federico Ferri, CEA/Saclay Irfu/SPP
0005  *
0006  **/
0007 
0008 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
0009 #include "CondFormats/EcalObjects/interface/EcalChannelStatus.h"
0010 #include "DataFormats/Common/interface/Handle.h"
0011 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0012 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0013 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
0014 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0015 #include "FWCore/Framework/interface/ESHandle.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/EventSetup.h"
0018 #include "FWCore/Framework/interface/stream/EDProducer.h"
0019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/Utilities/interface/ESGetToken.h"
0022 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0023 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0024 
0025 class EcalTPSkimmer : public edm::stream::EDProducer<> {
0026 public:
0027   explicit EcalTPSkimmer(const edm::ParameterSet& ps);
0028   ~EcalTPSkimmer() override;
0029   void produce(edm::Event& evt, const edm::EventSetup& es) override;
0030 
0031 private:
0032   bool alreadyInserted(EcalTrigTowerDetId ttId);
0033   void insertTP(EcalTrigTowerDetId ttId, edm::Handle<EcalTrigPrimDigiCollection>& in, EcalTrigPrimDigiCollection& out);
0034 
0035   std::string tpCollection_;
0036 
0037   bool skipModule_;
0038   bool doBarrel_;
0039   bool doEndcap_;
0040 
0041   std::vector<uint32_t> chStatusToSelectTP_;
0042   edm::ESHandle<EcalTrigTowerConstituentsMap> ttMap_;
0043   edm::ESGetToken<EcalTrigTowerConstituentsMap, IdealGeometryRecord> ttMapToken_;
0044   edm::ESGetToken<EcalChannelStatus, EcalChannelStatusRcd> chStatusToken_;
0045 
0046   std::set<EcalTrigTowerDetId> insertedTP_;
0047 
0048   edm::EDGetTokenT<EcalTrigPrimDigiCollection> tpInputToken_;
0049 
0050   std::string tpOutputCollection_;
0051 };
0052 
0053 EcalTPSkimmer::EcalTPSkimmer(const edm::ParameterSet& ps) {
0054   skipModule_ = ps.getParameter<bool>("skipModule");
0055 
0056   doBarrel_ = ps.getParameter<bool>("doBarrel");
0057   doEndcap_ = ps.getParameter<bool>("doEndcap");
0058 
0059   chStatusToSelectTP_ = ps.getParameter<std::vector<uint32_t> >("chStatusToSelectTP");
0060 
0061   tpOutputCollection_ = ps.getParameter<std::string>("tpOutputCollection");
0062   tpInputToken_ = consumes<EcalTrigPrimDigiCollection>(ps.getParameter<edm::InputTag>("tpInputCollection"));
0063   ttMapToken_ = esConsumes<EcalTrigTowerConstituentsMap, IdealGeometryRecord>();
0064   if (not skipModule_) {
0065     chStatusToken_ = esConsumes<EcalChannelStatus, EcalChannelStatusRcd>();
0066   }
0067   produces<EcalTrigPrimDigiCollection>(tpOutputCollection_);
0068 }
0069 
0070 EcalTPSkimmer::~EcalTPSkimmer() {}
0071 
0072 void EcalTPSkimmer::produce(edm::Event& evt, const edm::EventSetup& es) {
0073   insertedTP_.clear();
0074 
0075   using namespace edm;
0076 
0077   ttMap_ = es.getHandle(ttMapToken_);
0078 
0079   // collection of rechits to put in the event
0080   auto tpOut = std::make_unique<EcalTrigPrimDigiCollection>();
0081 
0082   if (skipModule_) {
0083     evt.put(std::move(tpOut), tpOutputCollection_);
0084     return;
0085   }
0086 
0087   edm::ESHandle<EcalChannelStatus> chStatus = es.getHandle(chStatusToken_);
0088 
0089   edm::Handle<EcalTrigPrimDigiCollection> tpIn;
0090   evt.getByToken(tpInputToken_, tpIn);
0091 
0092   if (doBarrel_) {
0093     EcalChannelStatusMap::const_iterator chit;
0094     uint16_t code = 0;
0095     for (int i = 0; i < EBDetId::kSizeForDenseIndexing; ++i) {
0096       if (!EBDetId::validDenseIndex(i))
0097         continue;
0098       EBDetId id = EBDetId::detIdFromDenseIndex(i);
0099       chit = chStatus->find(id);
0100       // check if the channel status means TP to be kept
0101       if (chit != chStatus->end()) {
0102         code = (*chit).getStatusCode();
0103         if (std::find(chStatusToSelectTP_.begin(), chStatusToSelectTP_.end(), code) != chStatusToSelectTP_.end()) {
0104           // retrieve the TP DetId
0105           EcalTrigTowerDetId ttDetId(((EBDetId)id).tower());
0106           // insert the TP if not done already
0107           if (!alreadyInserted(ttDetId))
0108             insertTP(ttDetId, tpIn, *tpOut);
0109         }
0110       } else {
0111         edm::LogError("EcalDetIdToBeRecoveredProducer") << "No channel status found for xtal " << id.rawId()
0112                                                         << "! something wrong with EcalChannelStatus in your DB? ";
0113       }
0114     }
0115   }
0116 
0117   if (doEndcap_) {
0118     EcalChannelStatusMap::const_iterator chit;
0119     uint16_t code = 0;
0120     for (int i = 0; i < EEDetId::kSizeForDenseIndexing; ++i) {
0121       if (!EEDetId::validDenseIndex(i))
0122         continue;
0123       EEDetId id = EEDetId::detIdFromDenseIndex(i);
0124       chit = chStatus->find(id);
0125       // check if the channel status means TP to be kept
0126       if (chit != chStatus->end()) {
0127         code = (*chit).getStatusCode();
0128         if (std::find(chStatusToSelectTP_.begin(), chStatusToSelectTP_.end(), code) != chStatusToSelectTP_.end()) {
0129           // retrieve the TP DetId
0130           EcalTrigTowerDetId ttDetId = ttMap_->towerOf(id);
0131           // insert the TP if not done already
0132           if (!alreadyInserted(ttDetId))
0133             insertTP(ttDetId, tpIn, *tpOut);
0134         }
0135       } else {
0136         edm::LogError("EcalDetIdToBeRecoveredProducer") << "No channel status found for xtal " << id.rawId()
0137                                                         << "! something wrong with EcalChannelStatus in your DB? ";
0138       }
0139     }
0140   }
0141 
0142   // put the collection of reconstructed hits in the event
0143   LogInfo("EcalTPSkimmer") << "total # of TP inserted: " << tpOut->size();
0144 
0145   evt.put(std::move(tpOut), tpOutputCollection_);
0146 }
0147 
0148 bool EcalTPSkimmer::alreadyInserted(EcalTrigTowerDetId ttId) { return (insertedTP_.find(ttId) != insertedTP_.end()); }
0149 
0150 void EcalTPSkimmer::insertTP(EcalTrigTowerDetId ttId,
0151                              edm::Handle<EcalTrigPrimDigiCollection>& tpIn,
0152                              EcalTrigPrimDigiCollection& tpOut) {
0153   EcalTrigPrimDigiCollection::const_iterator tpIt = tpIn->find(ttId);
0154   if (tpIt != tpIn->end()) {
0155     tpOut.push_back(*tpIt);
0156     insertedTP_.insert(ttId);
0157   }
0158 }
0159 
0160 #include "FWCore/Framework/interface/MakerMacros.h"
0161 DEFINE_FWK_MODULE(EcalTPSkimmer);