Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-04 04:04:31

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/EventSetupRecordIntervalFinder.h"
0006 #include "FWCore/Framework/interface/ESProducts.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/HGCalMappingParameterHostCollection.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 
0019 /**
0020    @short plugin parses the module/cell locator files to produce the indexer records
0021  */
0022 class HGCalMappingESProducer : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0023 public:
0024   explicit HGCalMappingESProducer(const edm::ParameterSet& iConfig) {
0025     //parse the files and hold the list of entities in memory
0026     for (auto v : {"modules", "si", "sipm"}) {
0027       edm::FileInPath fip = iConfig.getParameter<edm::FileInPath>(v);
0028       hgcal::mappingtools::HGCalEntityList pmap;
0029       pmap.buildFrom(fip.fullPath());
0030       parsedMaps_[v] = pmap;
0031     }
0032 
0033     setWhatProduced(this, &HGCalMappingESProducer::produceCellMapIndexer);
0034     setWhatProduced(this, &HGCalMappingESProducer::produceModuleMapIndexer);
0035 
0036     findingRecord<HGCalElectronicsMappingRcd>();
0037 
0038     prepareCellMapperIndexer();
0039     prepareModuleMapperIndexer();
0040   }
0041 
0042   std::shared_ptr<HGCalMappingModuleIndexer> produceModuleMapIndexer(const HGCalElectronicsMappingRcd&) {
0043     return std::shared_ptr<HGCalMappingModuleIndexer>(&modIndexer_, edm::do_nothing_deleter());
0044   }
0045 
0046   std::shared_ptr<HGCalMappingCellIndexer> produceCellMapIndexer(const HGCalElectronicsMappingRcd&) {
0047     return std::shared_ptr<HGCalMappingCellIndexer>(&cellIndexer_, edm::do_nothing_deleter());
0048   }
0049 
0050   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0051     edm::ParameterSetDescription desc;
0052     desc.add<edm::FileInPath>("modules")->setComment("module locator file");
0053     desc.add<edm::FileInPath>("si")->setComment("file containing the mapping of the readout cells in Si modules");
0054     desc.add<edm::FileInPath>("sipm")->setComment(
0055         "file containing the mapping of the readout cells in SiPM-on-tile modules");
0056     descriptions.addWithDefaultLabel(desc);
0057   }
0058 
0059 private:
0060   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0061                       const edm::IOVSyncValue&,
0062                       edm::ValidityInterval& oValidity) override {
0063     oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime());
0064   }
0065 
0066   void prepareCellMapperIndexer();
0067   void prepareModuleMapperIndexer();
0068 
0069   std::map<std::string, hgcal::mappingtools::HGCalEntityList> parsedMaps_;
0070   HGCalMappingCellIndexer cellIndexer_;
0071   HGCalMappingModuleIndexer modIndexer_;
0072 };
0073 
0074 //
0075 void HGCalMappingESProducer::prepareCellMapperIndexer() {
0076   for (auto v : {"si", "sipm"}) {
0077     auto& pmap = parsedMaps_[v];
0078     const auto& entities = pmap.getEntries();
0079     for (auto row : entities) {
0080       std::string typecode = pmap.getAttr("Typecode", row);
0081       int chip = pmap.getIntAttr("ROC", row);
0082       int half = pmap.getIntAttr("HalfROC", row);
0083       cellIndexer_.processNewCell(typecode, chip, half);
0084     }
0085   }
0086 
0087   // all {hex,tile}board types are loaded finalize the mapping indexer
0088   cellIndexer_.update();
0089 }
0090 
0091 //
0092 void HGCalMappingESProducer::prepareModuleMapperIndexer() {
0093   //default values to assign in case module type has not yet been mapped
0094   //a high density module (max possible) will be assigned so that the mapping doesn't block
0095   auto defaultTypeCodeIdx = cellIndexer_.getEnumFromTypecode("MH-F");
0096   auto typecodeidx = defaultTypeCodeIdx;
0097   auto defaultNerx = cellIndexer_.getNErxExpectedFor(defaultTypeCodeIdx);
0098   auto nerx = defaultNerx;
0099   auto defaultTypeNWords = cellIndexer_.getNWordsExpectedFor(defaultTypeCodeIdx);
0100   auto nwords = defaultTypeNWords;
0101 
0102   auto& pmap = parsedMaps_["modules"];
0103   auto& entities = pmap.getEntries();
0104   for (auto row : entities) {
0105     std::string typecode = pmap.getAttr("typecode", row);
0106 
0107     if (typecode.find('M') == 0 && typecode.size() > 4)
0108       typecode = typecode.substr(0, 4);
0109 
0110     try {
0111       typecodeidx = cellIndexer_.getEnumFromTypecode(typecode);
0112       nwords = cellIndexer_.getNWordsExpectedFor(typecode);
0113       nerx = cellIndexer_.getNErxExpectedFor(typecode);
0114     } catch (cms::Exception& e) {
0115       int plane = pmap.getIntAttr("plane", row);
0116       int u = pmap.getIntAttr("u", row);
0117       int v = pmap.getIntAttr("v", row);
0118       edm::LogWarning("HGCalMappingESProducer") << "Exception caught decoding index for typecode=" << typecode
0119                                                 << " @ plane=" << plane << " u=" << u << " v=" << v << "\n"
0120                                                 << e.what() << "\n"
0121                                                 << "===> will assign default (MH-F) which may be inefficient";
0122       typecodeidx = defaultTypeCodeIdx;
0123       nwords = defaultTypeNWords;
0124       nerx = defaultNerx;
0125     }
0126 
0127     int fedid = pmap.getIntAttr("fedid", row);
0128     int captureblockidx = pmap.getIntAttr("captureblockidx", row);
0129     int econdidx = pmap.getIntAttr("econdidx", row);
0130     modIndexer_.processNewModule(fedid, captureblockidx, econdidx, typecodeidx, nerx, nwords);
0131   }
0132 
0133   modIndexer_.finalize();
0134 }
0135 
0136 DEFINE_FWK_EVENTSETUP_SOURCE(HGCalMappingESProducer);