Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:37

0001 #include <memory>
0002 
0003 #include "FWCore/Framework/interface/Frameworkfwd.h"
0004 #include "FWCore/Framework/interface/global/EDProducer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Utilities/interface/ESInputTag.h"
0007 #include "FWCore/Utilities/interface/Transition.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0011 
0012 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0013 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0014 
0015 #include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"
0016 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
0017 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0018 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0019 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
0020 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
0021 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0022 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0023 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0024 
0025 class HcalDetIdTableProducer : public edm::global::EDProducer<edm::BeginRunProducer> {
0026 private:
0027   edm::ESGetToken<HcalDbService, HcalDbRecord> tokenHcalDbService_;
0028   edm::EDPutTokenT<std::vector<HcalDetId>> hbDetIdListToken_;
0029   edm::EDPutTokenT<std::vector<HcalDetId>> heDetIdListToken_;
0030   edm::EDPutTokenT<std::vector<HcalDetId>> hfDetIdListToken_;
0031   edm::EDPutTokenT<std::vector<HcalDetId>> hoDetIdListToken_;
0032 
0033 public:
0034   explicit HcalDetIdTableProducer(const edm::ParameterSet& iConfig)
0035       : tokenHcalDbService_(esConsumes<HcalDbService, HcalDbRecord, edm::Transition::BeginRun>()) {
0036     hbDetIdListToken_ = produces<std::vector<HcalDetId>, edm::Transition::BeginRun>("HBDetIdList");
0037     heDetIdListToken_ = produces<std::vector<HcalDetId>, edm::Transition::BeginRun>("HEDetIdList");
0038     hfDetIdListToken_ = produces<std::vector<HcalDetId>, edm::Transition::BeginRun>("HFDetIdList");
0039     hoDetIdListToken_ = produces<std::vector<HcalDetId>, edm::Transition::BeginRun>("HODetIdList");
0040 
0041     produces<nanoaod::FlatTable, edm::Transition::BeginRun>("HBDetIdList");
0042     produces<nanoaod::FlatTable, edm::Transition::BeginRun>("HEDetIdList");
0043     produces<nanoaod::FlatTable, edm::Transition::BeginRun>("HFDetIdList");
0044     produces<nanoaod::FlatTable, edm::Transition::BeginRun>("HODetIdList");
0045   };
0046 
0047   void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0048   void globalBeginRunProduce(edm::Run& iRun, edm::EventSetup const& iSetup) const override;
0049 };
0050 
0051 void HcalDetIdTableProducer::produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const {}
0052 
0053 void HcalDetIdTableProducer::globalBeginRunProduce(edm::Run& iRun, edm::EventSetup const& iSetup) const {
0054   // Setup products
0055   const std::vector<HcalSubdetector> subdets = {HcalBarrel, HcalEndcap, HcalForward, HcalOuter};
0056   std::map<HcalSubdetector, std::unique_ptr<std::vector<HcalDetId>>> didLists;
0057   didLists[HcalBarrel] = std::make_unique<std::vector<HcalDetId>>();
0058   didLists[HcalEndcap] = std::make_unique<std::vector<HcalDetId>>();
0059   didLists[HcalForward] = std::make_unique<std::vector<HcalDetId>>();
0060   didLists[HcalOuter] = std::make_unique<std::vector<HcalDetId>>();
0061 
0062   // Load channels from emap
0063   edm::ESHandle<HcalDbService> dbService = iSetup.getHandle(tokenHcalDbService_);
0064   HcalElectronicsMap const* emap = dbService->getHcalMapping();
0065 
0066   std::vector<HcalGenericDetId> alldids = emap->allPrecisionId();
0067   for (auto it_did = alldids.begin(); it_did != alldids.end(); ++it_did) {
0068     if (!it_did->isHcalDetId()) {
0069       continue;
0070     }
0071     HcalDetId did = HcalDetId(it_did->rawId());
0072     if (!(did.subdet() == HcalBarrel || did.subdet() == HcalEndcap || did.subdet() == HcalForward ||
0073           did.subdet() == HcalOuter)) {
0074       continue;
0075     }
0076 
0077     // TODO: Add filtering, for example on FED whitelist
0078 
0079     didLists[did.subdet()]->push_back(did);
0080   }
0081 
0082   // Sort HcalDetIds
0083   for (auto& it_subdet : subdets) {
0084     std::sort(didLists[it_subdet]->begin(), didLists[it_subdet]->end());
0085   }
0086 
0087   // Make NanoAOD tables
0088   std::map<HcalSubdetector, std::string> subdetNames = {
0089       {HcalBarrel, "HB"}, {HcalEndcap, "HE"}, {HcalForward, "HF"}, {HcalOuter, "HO"}};
0090 
0091   for (auto& it_subdet : subdets) {
0092     auto didTable =
0093         std::make_unique<nanoaod::FlatTable>(didLists[it_subdet]->size(), subdetNames[it_subdet], false, false);
0094 
0095     std::vector<int> vdids;
0096     for (auto& it_did : *(didLists[it_subdet])) {
0097       vdids.push_back(it_did.rawId());
0098     }
0099     didTable->addColumn<int>("did", vdids, "HcalDetId");
0100 
0101     iRun.put(std::move(didTable), subdetNames[it_subdet] + "DetIdList");
0102     iRun.put(std::move(didLists[it_subdet]), subdetNames[it_subdet] + "DetIdList");
0103   }
0104 }
0105 
0106 #include "FWCore/Framework/interface/MakerMacros.h"
0107 //define this as a plug-in
0108 DEFINE_FWK_MODULE(HcalDetIdTableProducer);