Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:42

0001 //
0002 // Package:    HLTrigger/special
0003 // Class:      HLTRechitsToDigis
0004 //
0005 /**\class HLTRechitsToDigis HLTRechitsToDigis.cc HLTrigger/special/plugins/HLTRechitsToDigis.cc
0006 
0007  Description: [one line class summary]
0008 
0009  Implementation:
0010      [Notes on implementation]
0011 */
0012 //
0013 // Original Author:  Joshua Robert Hardenbrook
0014 //         Created:  Fri, 20 Feb 2015 15:51:36 GMT
0015 //
0016 //
0017 
0018 // system include files
0019 #include <memory>
0020 
0021 // user include files
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/stream/EDProducer.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 
0029 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0030 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0031 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0032 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0033 #include "DataFormats/EcalDetId/interface/EcalDetIdCollections.h"
0034 
0035 #include "CondFormats/EcalObjects/interface/EcalChannelStatus.h"
0036 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
0037 
0038 // for srFlags management
0039 #include "CalibCalorimetry/EcalTPGTools/interface/EcalReadoutTools.h"
0040 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0041 
0042 //
0043 // class declaration
0044 //
0045 
0046 class HLTRechitsToDigis : public edm::stream::EDProducer<> {
0047 public:
0048   explicit HLTRechitsToDigis(const edm::ParameterSet&);
0049   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0050   enum ecalRegion { invalidRegion = 0, barrel, endcap };
0051   static const HLTRechitsToDigis::ecalRegion stringToRegion(const std::string& region);
0052 
0053 private:
0054   void produce(edm::Event&, edm::EventSetup const&) override;
0055 
0056   // ----------member data ---------------------------
0057   // tokens for the digi and rechits for matching
0058   edm::EDGetTokenT<EBDigiCollection> digisEBInToken_;
0059   edm::EDGetTokenT<EEDigiCollection> digisEEInToken_;
0060   edm::EDGetTokenT<EcalRecHitCollection> recHitsToken_;
0061   // tokens for srFlags
0062   edm::EDGetTokenT<EBSrFlagCollection> srFlagsEBInToken_;
0063   edm::EDGetTokenT<EESrFlagCollection> srFlagsEEInToken_;
0064 
0065   EcalReadoutTools::ESGetTokens const ecalReadoutToolsESGetTokens_;
0066 
0067   // input tags
0068   edm::InputTag digisIn_;
0069   edm::InputTag recHits_;
0070   // srFlags
0071   edm::InputTag srFlagsIn_;
0072 
0073   // string for the produced digi collection
0074   std::string digisOut_;
0075   ecalRegion region_;
0076   // string for the produced srFlags collection
0077   std::string srFlagsOut_;
0078 };
0079 //
0080 
0081 //
0082 // constructors and destructor
0083 //
0084 HLTRechitsToDigis::HLTRechitsToDigis(const edm::ParameterSet& iConfig)
0085     : ecalReadoutToolsESGetTokens_{iConfig, consumesCollector()} {
0086   //region to do rechit digi matching
0087   region_ = stringToRegion(iConfig.getParameter<std::string>("region"));
0088 
0089   // digis to match to hit collections
0090   digisIn_ = iConfig.getParameter<edm::InputTag>("digisIn");
0091   digisOut_ = iConfig.getParameter<std::string>("digisOut");
0092 
0093   // hit collections to save digis for
0094   recHits_ = iConfig.getParameter<edm::InputTag>("recHits");
0095 
0096   // srFlags matched to digis to be saved
0097   srFlagsIn_ = iConfig.getParameter<edm::InputTag>("srFlagsIn");
0098   srFlagsOut_ = iConfig.getParameter<std::string>("srFlagsOut");
0099 
0100   // region specific tokens
0101   switch (region_) {
0102     case barrel:
0103       digisEBInToken_ = consumes<EBDigiCollection>(digisIn_);
0104       produces<EBDigiCollection>(digisOut_);
0105       // protection against empty InputTag to allow for backward compatibility
0106       if (not srFlagsIn_.label().empty()) {
0107         srFlagsEBInToken_ = consumes<EBSrFlagCollection>(srFlagsIn_);
0108         produces<EBSrFlagCollection>(srFlagsOut_);
0109       }
0110       break;
0111     case endcap:
0112       digisEEInToken_ = consumes<EEDigiCollection>(digisIn_);
0113       produces<EEDigiCollection>(digisOut_);
0114       // protection against empty InputTag to allow for backward compatibility
0115       if (not srFlagsIn_.label().empty()) {
0116         srFlagsEEInToken_ = consumes<EESrFlagCollection>(srFlagsIn_);
0117         produces<EESrFlagCollection>(srFlagsOut_);
0118       }
0119       break;
0120     case invalidRegion:
0121       break;
0122   }
0123 
0124   recHitsToken_ = consumes<EcalRecHitCollection>(recHits_);
0125 }
0126 
0127 //
0128 // member functions
0129 //
0130 const HLTRechitsToDigis::ecalRegion HLTRechitsToDigis::stringToRegion(const std::string& region) {
0131   if (region == "barrel")
0132     return barrel;
0133   else if (region == "endcap")
0134     return endcap;
0135   else
0136     return invalidRegion;
0137 }
0138 
0139 // ------------ method called to produce the data  ------------
0140 void HLTRechitsToDigis::produce(edm::Event& iEvent, edm::EventSetup const& setup) {
0141   using namespace edm;
0142   // handles for digis
0143   Handle<EBDigiCollection> digisEBHandle;
0144   Handle<EEDigiCollection> digisEEHandle;
0145 
0146   // output collections
0147   std::unique_ptr<EBDigiCollection> outputEBDigiCollection(new EBDigiCollection);
0148   std::unique_ptr<EEDigiCollection> outputEEDigiCollection(new EEDigiCollection);
0149 
0150   // handles for srFlags
0151   Handle<EBSrFlagCollection> srFlagsEBHandle;
0152   Handle<EESrFlagCollection> srFlagsEEHandle;
0153 
0154   // output collections
0155   std::unique_ptr<EBSrFlagCollection> outputEBSrFlagCollection(new EBSrFlagCollection);
0156   std::unique_ptr<EESrFlagCollection> outputEESrFlagCollection(new EESrFlagCollection);
0157   EcalReadoutTools ecalReadOutTool(iEvent, setup, ecalReadoutToolsESGetTokens_);
0158 
0159   // calibrated rechits
0160   Handle<EcalRecHitCollection> recHitsHandle;
0161   iEvent.getByToken(recHitsToken_, recHitsHandle);
0162 
0163   // match the digis based on the region
0164   switch (region_) {
0165     case barrel: {
0166       iEvent.getByToken(digisEBInToken_, digisEBHandle);
0167       const EBDigiCollection* digisEB = digisEBHandle.product();
0168 
0169       const EBSrFlagCollection* srFlagsEB = nullptr;
0170       // protection against uninitialized token (empty InputTag) to allow for backward compatibility
0171       if (not srFlagsEBInToken_.isUninitialized()) {
0172         iEvent.getByToken(srFlagsEBInToken_, srFlagsEBHandle);
0173         srFlagsEB = srFlagsEBHandle.product();
0174       }
0175 
0176       // loop over the collection of rechits and match to digis
0177       // at the same time, create the new sfFlags collection from the original one, keeping only the flags matched to digis
0178       EcalRecHitCollection::const_iterator ituneEB;
0179       for (ituneEB = recHitsHandle->begin(); ituneEB != recHitsHandle->end(); ituneEB++) {
0180         EcalRecHit const& hit = (*ituneEB);
0181         EcalDigiCollection::const_iterator digiLookUp = digisEB->find(hit.id());
0182         // protect against a digi not existing
0183         if (digiLookUp == digisEB->end())
0184           continue;
0185         outputEBDigiCollection->push_back(digiLookUp->id(), digiLookUp->begin());
0186 
0187         EBSrFlagCollection::const_iterator srFlagLookUp;
0188         if (not srFlagsEBInToken_.isUninitialized()) {
0189           // same matching for srFlags
0190           // firstly, get the tower id
0191           const EcalTrigTowerDetId& ttId = ecalReadOutTool.readOutUnitOf(static_cast<EBDetId>(hit.id()));
0192           // avoid inserting the same tower twice in the output collection (all the digis in the same tower will have the same SR flag)
0193           if (outputEBSrFlagCollection->find(ttId) != outputEBSrFlagCollection->end())
0194             continue;
0195           srFlagLookUp = srFlagsEB->find(ttId);
0196           // protect against a srFlag not existing
0197           if (srFlagLookUp == srFlagsEB->end())
0198             continue;
0199           outputEBSrFlagCollection->push_back(*srFlagLookUp);
0200         }
0201       }
0202 
0203       // add the built collection to the event
0204       iEvent.put(std::move(outputEBDigiCollection), digisOut_);
0205       if (not srFlagsEBInToken_.isUninitialized())
0206         iEvent.put(std::move(outputEBSrFlagCollection), srFlagsOut_);
0207       break;
0208     }
0209     case endcap: {
0210       iEvent.getByToken(digisEEInToken_, digisEEHandle);
0211       const EEDigiCollection* digisEE = digisEEHandle.product();
0212 
0213       const EESrFlagCollection* srFlagsEE = nullptr;
0214       // protection against uninitialized token (empty InputTag) to allow for backward compatibility
0215       if (not srFlagsEEInToken_.isUninitialized()) {
0216         iEvent.getByToken(srFlagsEEInToken_, srFlagsEEHandle);
0217         srFlagsEE = srFlagsEEHandle.product();
0218       }
0219 
0220       // loop over the collection of rechits and match to digis
0221       // at the same time, create the new sfFlags collection from the original one, keeping only the flags matched to digis
0222       EcalRecHitCollection::const_iterator ituneEE;
0223       for (ituneEE = recHitsHandle->begin(); ituneEE != recHitsHandle->end(); ituneEE++) {
0224         EcalRecHit const& hit = (*ituneEE);
0225         EcalDigiCollection::const_iterator digiLookUp = digisEE->find(hit.id());
0226         // protect against a digi not existing for the saved rechit
0227         if (digiLookUp == digisEE->end())
0228           continue;
0229         outputEEDigiCollection->push_back(digiLookUp->id(), digiLookUp->begin());
0230 
0231         EESrFlagCollection::const_iterator srFlagLookUp;
0232         if (not srFlagsEEInToken_.isUninitialized()) {
0233           // same matching for srFlags
0234           // firstly, get the tower id
0235           const EcalScDetId& scId = ecalReadOutTool.readOutUnitOf(static_cast<EEDetId>(hit.id()));
0236           // avoid inserting the same tower twice in the output collection (all the digis in the same tower will have the same SR flag)
0237           if (outputEESrFlagCollection->find(scId) != outputEESrFlagCollection->end())
0238             continue;
0239           srFlagLookUp = srFlagsEE->find(scId);
0240           // protect against an srFlag not existing for the saved rechit
0241           if (srFlagLookUp == srFlagsEE->end())
0242             continue;
0243           outputEESrFlagCollection->push_back(*srFlagLookUp);
0244         }
0245       }  // end loop over endcap rechits
0246 
0247       // add the built collection to the event
0248       iEvent.put(std::move(outputEEDigiCollection), digisOut_);
0249       if (not srFlagsEEInToken_.isUninitialized())
0250         iEvent.put(std::move(outputEESrFlagCollection), srFlagsOut_);
0251       break;
0252     }
0253     case invalidRegion: {
0254       break;
0255     }
0256   }  // end switch statement for the region (barrel, endcap, invalid)
0257 }
0258 
0259 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0260 void HLTRechitsToDigis::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0261   //The following says we do not know what parameters are allowed so do no validation
0262   // Please change this to state exactly what you do use, even if it is no parameters
0263   edm::ParameterSetDescription desc;
0264 
0265   desc.add<std::string>("region", "barrel")
0266       ->setComment("Region of rechits to save Digis for. Allowed values: barrel or endcap.");
0267   desc.add<edm::InputTag>("digisIn", edm::InputTag("ecalDigis", "ebDigis"))
0268       ->setComment("The collection of either barrel or endcap digis which correspond to the rechit collection");
0269   desc.add<std::string>("digisOut", "pi0EBDigis")->setComment("Name for the collection of Digis saved by the module");
0270   desc.add<edm::InputTag>("recHits", edm::InputTag("hltAlCaPi0EBUncalibrator", "pi0EcalRecHitsEB"))
0271       ->setComment("Collection of rechits to match Digis to");
0272   desc.add<edm::InputTag>("srFlagsIn", edm::InputTag())
0273       ->setComment("The collection of either barrel or endcap srFlags which correspond to the rechit collection");
0274   desc.add<std::string>("srFlagsOut", "pi0EBSrFlags")
0275       ->setComment("Name for the collection of SrFlags saved by the module");
0276   descriptions.add("hltFindMatchingECALDigisToRechits", desc);
0277 }
0278 
0279 // declare this class as a framework plugin
0280 DEFINE_FWK_MODULE(HLTRechitsToDigis);