Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-29 06:08:37

0001 #ifndef RecoParticleFlow_PFClusterProducer_PFEcalEndcapRecHitCreator_h
0002 #define RecoParticleFlow_PFClusterProducer_PFEcalEndcapRecHitCreator_h
0003 
0004 #include "RecoParticleFlow/PFClusterProducer/interface/PFRecHitCreatorBase.h"
0005 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
0006 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0007 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0008 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
0009 
0010 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0011 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0012 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0013 
0014 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0015 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0016 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
0017 #include "Geometry/CaloGeometry/interface/TruncatedPyramid.h"
0018 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0019 
0020 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0021 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
0022 #include "Geometry/EcalAlgo/interface/EcalEndcapGeometry.h"
0023 #include "Geometry/EcalAlgo/interface/EcalBarrelGeometry.h"
0024 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0025 #include "Geometry/CaloTopology/interface/EcalEndcapTopology.h"
0026 #include "Geometry/CaloTopology/interface/EcalBarrelTopology.h"
0027 #include "Geometry/CaloTopology/interface/EcalPreshowerTopology.h"
0028 #include "RecoCaloTools/Navigation/interface/CaloNavigator.h"
0029 
0030 class PFEcalEndcapRecHitCreator : public PFRecHitCreatorBase {
0031 public:
0032   PFEcalEndcapRecHitCreator(const edm::ParameterSet& iConfig, edm::ConsumesCollector& cc)
0033       : PFRecHitCreatorBase(iConfig, cc),
0034         recHitToken_(cc.consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("src"))),
0035         elecMap_(nullptr),
0036         geomToken_(cc.esConsumes()),
0037         mappingToken_(cc.esConsumes<edm::Transition::BeginRun>()) {
0038     auto srF = iConfig.getParameter<edm::InputTag>("srFlags");
0039     if (not srF.label().empty())
0040       srFlagToken_ = cc.consumes<EESrFlagCollection>(srF);
0041   }
0042 
0043   void importRecHits(std::unique_ptr<reco::PFRecHitCollection>& out,
0044                      std::unique_ptr<reco::PFRecHitCollection>& cleaned,
0045                      const edm::Event& iEvent,
0046                      const edm::EventSetup& iSetup) override {
0047     beginEvent(iEvent, iSetup);
0048 
0049     edm::Handle<EcalRecHitCollection> recHitHandle;
0050 
0051     edm::ESHandle<CaloGeometry> geoHandle = iSetup.getHandle(geomToken_);
0052 
0053     bool useSrF = false;
0054     if (not srFlagToken_.isUninitialized()) {
0055       iEvent.getByToken(srFlagToken_, srFlagHandle_);
0056       useSrF = true;
0057     }
0058 
0059     // get the ecal geometry
0060     const CaloSubdetectorGeometry* gTmp = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalEndcap);
0061 
0062     const EcalEndcapGeometry* ecalGeo = dynamic_cast<const EcalEndcapGeometry*>(gTmp);
0063 
0064     iEvent.getByToken(recHitToken_, recHitHandle);
0065     for (const auto& erh : *recHitHandle) {
0066       const DetId& detid = erh.detid();
0067       auto energy = erh.energy();
0068       auto time = erh.time();
0069 
0070       bool hi = (useSrF ? isHighInterest(detid) : true);
0071 
0072       {
0073         auto thisCell = ecalGeo->getGeometry(detid);
0074 
0075         // find rechit geometry
0076         if (!thisCell) {
0077           throw cms::Exception("PFEcalEndcapRecHitCreator") << "detid " << detid.rawId() << "not found in geometry";
0078         }
0079 
0080         out->emplace_back(std::move(thisCell), detid.rawId(), PFLayer::ECAL_ENDCAP, energy);
0081       }
0082       auto& rh = out->back();
0083 
0084       bool rcleaned = false;
0085       bool keep = true;
0086 
0087       //Apply Q tests
0088       for (const auto& qtest : qualityTests_) {
0089         if (!qtest->test(rh, erh, rcleaned, hi)) {
0090           keep = false;
0091         }
0092       }
0093 
0094       if (keep) {
0095         rh.setTime(time);
0096         rh.setDepth(1);
0097       } else {
0098         if (rcleaned)
0099           cleaned->push_back(std::move(out->back()));
0100         out->pop_back();
0101       }
0102     }
0103   }
0104 
0105   void init(const edm::EventSetup& es) override { elecMap_ = &es.getData(mappingToken_); }
0106 
0107 protected:
0108   bool isHighInterest(const EEDetId& detid) {
0109     bool result = false;
0110     auto srf = srFlagHandle_->find(readOutUnitOf(detid));
0111     if (srf == srFlagHandle_->end())
0112       return false;
0113     else
0114       result = ((srf->value() & ~EcalSrFlag::SRF_FORCED_MASK) == EcalSrFlag::SRF_FULL);
0115     return result;
0116   }
0117 
0118   EcalScDetId readOutUnitOf(const EEDetId& detid) const {
0119     const EcalElectronicsId& EcalElecId = elecMap_->getElectronicsId(detid);
0120     int iDCC = EcalElecId.dccId();
0121     int iDccChan = EcalElecId.towerId();
0122     const bool ignoreSingle = true;
0123     const std::vector<EcalScDetId> id = elecMap_->getEcalScDetId(iDCC, iDccChan, ignoreSingle);
0124     return !id.empty() ? id[0] : EcalScDetId();
0125   }
0126 
0127   edm::EDGetTokenT<EcalRecHitCollection> recHitToken_;
0128   edm::EDGetTokenT<EESrFlagCollection> srFlagToken_;
0129 
0130   const EcalTrigTowerConstituentsMap* eTTmap_;
0131 
0132   // Ecal electronics/geometrical mapping
0133   const EcalElectronicsMapping* elecMap_;
0134   // selective readout flags collection
0135   edm::Handle<EESrFlagCollection> srFlagHandle_;
0136 
0137 private:
0138   edm::ESGetToken<CaloGeometry, CaloGeometryRecord> geomToken_;
0139   edm::ESGetToken<EcalElectronicsMapping, EcalMappingRcd> mappingToken_;
0140 };
0141 
0142 #endif