File indexing completed on 2025-01-18 03:42:04
0001 #include "FWCore/Framework/interface/MakerMacros.h"
0002 #include "FWCore/Framework/interface/SourceFactory.h"
0003 #include "FWCore/Framework/interface/ESHandle.h"
0004 #include "FWCore/Framework/interface/ESProducer.h"
0005 #include "FWCore/Framework/interface/ESProducts.h"
0006 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0007 #include "FWCore/ParameterSet/interface/FileInPath.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/Utilities/interface/do_nothing_deleter.h"
0010 #include "CondFormats/DataRecord/interface/HGCalElectronicsMappingRcd.h"
0011 #include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
0012 #include "CondFormats/HGCalObjects/interface/HGCalMappingCellIndexer.h"
0013 #include "CondFormats/HGCalObjects/interface/HGCalMappingParameterHost.h"
0014 #include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
0015 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0016 #include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
0017 #include "Geometry/HGCalMapping/interface/HGCalMappingTools.h"
0018 #include <regex> // regular expression
0019
0020
0021
0022
0023 class HGCalMappingESProducer : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0024 public:
0025 explicit HGCalMappingESProducer(const edm::ParameterSet& iConfig) {
0026
0027 for (auto v : {"modules", "si", "sipm"}) {
0028 edm::FileInPath fip = iConfig.getParameter<edm::FileInPath>(v);
0029 hgcal::mappingtools::HGCalEntityList pmap;
0030 pmap.buildFrom(fip.fullPath());
0031 parsedMaps_[v] = pmap;
0032 }
0033
0034 setWhatProduced(this, &HGCalMappingESProducer::produceCellMapIndexer);
0035 setWhatProduced(this, &HGCalMappingESProducer::produceModuleMapIndexer);
0036
0037 findingRecord<HGCalElectronicsMappingRcd>();
0038
0039 prepareCellMapperIndexer();
0040 prepareModuleMapperIndexer();
0041 }
0042
0043 std::shared_ptr<HGCalMappingModuleIndexer> produceModuleMapIndexer(const HGCalElectronicsMappingRcd&) {
0044 return std::shared_ptr<HGCalMappingModuleIndexer>(&modIndexer_, edm::do_nothing_deleter());
0045 }
0046
0047 std::shared_ptr<HGCalMappingCellIndexer> produceCellMapIndexer(const HGCalElectronicsMappingRcd&) {
0048 return std::shared_ptr<HGCalMappingCellIndexer>(&cellIndexer_, edm::do_nothing_deleter());
0049 }
0050
0051 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0052 edm::ParameterSetDescription desc;
0053 desc.add<edm::FileInPath>("modules")->setComment("module locator file");
0054 desc.add<edm::FileInPath>("si")->setComment("file containing the mapping of the readout cells in Si modules");
0055 desc.add<edm::FileInPath>("sipm")->setComment(
0056 "file containing the mapping of the readout cells in SiPM-on-tile modules");
0057 descriptions.addWithDefaultLabel(desc);
0058 }
0059
0060 private:
0061 void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0062 const edm::IOVSyncValue&,
0063 edm::ValidityInterval& oValidity) override {
0064 oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime());
0065 }
0066
0067 void prepareCellMapperIndexer();
0068 void prepareModuleMapperIndexer();
0069
0070 std::map<std::string, hgcal::mappingtools::HGCalEntityList> parsedMaps_;
0071 HGCalMappingCellIndexer cellIndexer_;
0072 HGCalMappingModuleIndexer modIndexer_;
0073 };
0074
0075
0076 void HGCalMappingESProducer::prepareCellMapperIndexer() {
0077 for (auto v : {"si", "sipm"}) {
0078 auto& pmap = parsedMaps_[v];
0079 const auto& entities = pmap.getEntries();
0080 for (auto row : entities) {
0081 std::string typecode = pmap.getAttr("Typecode", row);
0082 int chip = pmap.getIntAttr("ROC", row);
0083 int half = pmap.getIntAttr("HalfROC", row);
0084 cellIndexer_.processNewCell(typecode, chip, half);
0085 }
0086 }
0087
0088
0089 cellIndexer_.update();
0090 }
0091
0092
0093 void HGCalMappingESProducer::prepareModuleMapperIndexer() {
0094
0095
0096 auto defaultTypeCodeIdx = cellIndexer_.getEnumFromTypecode("MH-F");
0097 auto typecodeidx = defaultTypeCodeIdx;
0098 auto defaultNerx = cellIndexer_.getNErxExpectedFor(defaultTypeCodeIdx);
0099 auto nerx = defaultNerx;
0100 auto defaultTypeNWords = cellIndexer_.getNWordsExpectedFor(defaultTypeCodeIdx);
0101 auto nwords = defaultTypeNWords;
0102
0103 auto& pmap = parsedMaps_["modules"];
0104 auto& entities = pmap.getEntries();
0105 for (auto row : entities) {
0106 std::string typecode = pmap.getAttr("typecode", row);
0107 std::string wtypecode;
0108
0109
0110
0111
0112 const std::regex typecode_regex("(([MX])([LH])-([FTBLR5])).*");
0113 std::smatch typecode_match;
0114 bool matched = std::regex_match(typecode, typecode_match, typecode_regex);
0115 if (matched) {
0116 wtypecode = typecode_match[1].str();
0117 } else {
0118 edm::LogWarning("HGCalMappingIndexESSource")
0119 << "Could not match module type code to expected pattern: " << typecode;
0120 }
0121
0122 try {
0123 typecodeidx = cellIndexer_.getEnumFromTypecode(wtypecode);
0124 nwords = cellIndexer_.getNWordsExpectedFor(wtypecode);
0125 nerx = cellIndexer_.getNErxExpectedFor(wtypecode);
0126 } catch (cms::Exception& e) {
0127 int plane = pmap.getIntAttr("plane", row);
0128 int u = pmap.getIntAttr("u", row);
0129 int v = pmap.getIntAttr("v", row);
0130 edm::LogWarning("HGCalMappingESProducer") << "Exception caught decoding index for typecode=" << typecode
0131 << " @ plane=" << plane << " u=" << u << " v=" << v << "\n"
0132 << e.what() << "\n"
0133 << "===> will assign default (MH-F) which may be inefficient";
0134 typecodeidx = defaultTypeCodeIdx;
0135 nwords = defaultTypeNWords;
0136 nerx = defaultNerx;
0137 }
0138
0139 int fedid = pmap.getIntAttr("fedid", row);
0140 int captureblockidx = pmap.getIntAttr("captureblockidx", row);
0141 int econdidx = pmap.getIntAttr("econdidx", row);
0142 modIndexer_.processNewModule(fedid, captureblockidx, econdidx, typecodeidx, nerx, nwords, typecode);
0143 }
0144
0145 modIndexer_.finalize();
0146 }
0147
0148 DEFINE_FWK_EVENTSETUP_SOURCE(HGCalMappingESProducer);