File indexing completed on 2024-04-06 12:07:33
0001
0002
0003
0004
0005
0006 #include "DQM/HcalCommon/interface/DQTask.h"
0007 #include "DQM/HcalCommon/interface/Utilities.h"
0008 #include "DQM/HcalCommon/interface/HashFilter.h"
0009 #include "DQM/HcalCommon/interface/Container1D.h"
0010 #include "DQM/HcalCommon/interface/Container2D.h"
0011 #include "DQM/HcalCommon/interface/ContainerProf1D.h"
0012 #include "DQM/HcalCommon/interface/ContainerProf2D.h"
0013 #include "DQM/HcalCommon/interface/ContainerSingle1D.h"
0014 #include "DQM/HcalCommon/interface/ContainerSingle2D.h"
0015 #include "DQM/HcalCommon/interface/ContainerSingleProf2D.h"
0016 #include "DQM/HcalCommon/interface/ElectronicsMap.h"
0017
0018 #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
0019 #include "FWCore/ParameterSet/interface/FileInPath.h"
0020 #include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
0021
0022 #include "DQM/HcalTasks/plugins/OnlineDQMDigiAD_cmssw.h"
0023
0024 #include <cmath>
0025 #include <iostream>
0026 #include <algorithm>
0027
0028 using namespace cms::Ort;
0029 using namespace hcaldqm;
0030 using namespace hcaldqm::constants;
0031 using namespace hcaldqm::filter;
0032
0033 class HcalMLTask : public hcaldqm::DQTask {
0034 public:
0035 HcalMLTask(edm::ParameterSet const&);
0036 ~HcalMLTask() override = default;
0037
0038 void dqmBeginRun(edm::Run const&, edm::EventSetup const&) override;
0039 void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0040 std::shared_ptr<hcaldqm::Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0041 edm::EventSetup const&) const override;
0042 void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0043 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0044
0045 private:
0046 void _process(edm::Event const&, edm::EventSetup const&) override;
0047 void _resetMonitors(hcaldqm::UpdateFreq) override;
0048
0049 std::string onnx_model_path_HB, onnx_model_path_HE;
0050 double flagDecisionThr;
0051 edm::InputTag tagQIE11;
0052 edm::InputTag tagHO;
0053 edm::InputTag tagQIE10;
0054 edm::EDGetTokenT<QIE11DigiCollection> tokQIE11;
0055 edm::EDGetTokenT<HODigiCollection> tokHO;
0056 edm::EDGetTokenT<QIE10DigiCollection> tokQIE10;
0057 edm::ESGetToken<HcalDbService, HcalDbRecord> hcalDbServiceToken_;
0058
0059 hcaldqm::ContainerXXX<double> Occupancy1LS;
0060 hcaldqm::Container1D MLFlagvsLS_Subdet;
0061
0062 std::unique_ptr<OnlineDQMDigiAD> dqmadObj_HB = nullptr;
0063 std::unique_ptr<OnlineDQMDigiAD> dqmadObj_HE = nullptr;
0064
0065 std::vector<std::vector<float>> digiHcal2DHist_depth_1{
0066 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0067 std::vector<std::vector<float>> digiHcal2DHist_depth_2{
0068 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0069 std::vector<std::vector<float>> digiHcal2DHist_depth_3{
0070 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0071 std::vector<std::vector<float>> digiHcal2DHist_depth_4{
0072 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0073 std::vector<std::vector<float>> digiHcal2DHist_depth_5{
0074 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0075 std::vector<std::vector<float>> digiHcal2DHist_depth_6{
0076 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0077 std::vector<std::vector<float>> digiHcal2DHist_depth_7{
0078 std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
0079 };
0080
0081 HcalMLTask::HcalMLTask(edm::ParameterSet const& ps)
0082 : DQTask(ps), hcalDbServiceToken_(esConsumes<HcalDbService, HcalDbRecord, edm::Transition::BeginRun>()) {
0083 onnx_model_path_HB = ps.getUntrackedParameter<std::string>(
0084 "onnx_model_path_HB",
0085 "DQM/HcalTasks/data/HB_2022/"
0086 "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_5218_v06_02_2023_21h01_stateful.onnx");
0087 onnx_model_path_HE = ps.getUntrackedParameter<std::string>(
0088 "onnx_model_path_HE",
0089 "DQM/HcalTasks/data/HE_2022/"
0090 "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_7763_v06_02_2023_22h55_stateful.onnx");
0091 flagDecisionThr = ps.getUntrackedParameter<double>("flagDecisionThr", 20.);
0092 tagQIE11 = ps.getUntrackedParameter<edm::InputTag>("tagHBHE", edm::InputTag("hcalDigis"));
0093 tagHO = ps.getUntrackedParameter<edm::InputTag>("tagHO", edm::InputTag("hcalDigis"));
0094 tagQIE10 = ps.getUntrackedParameter<edm::InputTag>("tagHF", edm::InputTag("hcalDigis"));
0095
0096 tokQIE11 = consumes<QIE11DigiCollection>(tagQIE11);
0097 tokHO = consumes<HODigiCollection>(tagHO);
0098 tokQIE10 = consumes<QIE10DigiCollection>(tagQIE10);
0099
0100 auto dqmadObj_HB_ = std::make_unique<OnlineDQMDigiAD>("hb", onnx_model_path_HB, Backend::cpu);
0101 auto dqmadObj_HE_ = std::make_unique<OnlineDQMDigiAD>("he", onnx_model_path_HE, Backend::cpu);
0102 dqmadObj_HB = std::move(dqmadObj_HB_);
0103 dqmadObj_HE = std::move(dqmadObj_HE_);
0104 }
0105
0106 void HcalMLTask::dqmBeginRun(edm::Run const& r, edm::EventSetup const& es) { DQTask::dqmBeginRun(r, es); }
0107
0108 void HcalMLTask::bookHistograms(DQMStore::IBooker& ib, edm::Run const& r, edm::EventSetup const& es) {
0109 DQTask::bookHistograms(ib, r, es);
0110
0111
0112 edm::ESHandle<HcalDbService> dbs = es.getHandle(hcalDbServiceToken_);
0113 _emap = dbs->getHcalMapping();
0114
0115
0116 Occupancy1LS.initialize(hcaldqm::hashfunctions::fDChannel);
0117
0118 MLFlagvsLS_Subdet.initialize(_name,
0119 "MLBadFlagedChannelsvsLS",
0120 hcaldqm::hashfunctions::fSubdet,
0121 new hcaldqm::quantity::LumiSection(_maxLS),
0122 new hcaldqm::quantity::ValueQuantity(hcaldqm::quantity::fN),
0123 0);
0124
0125 Occupancy1LS.book(_emap);
0126 MLFlagvsLS_Subdet.book(ib, _emap, _subsystem);
0127 }
0128
0129 void HcalMLTask::_resetMonitors(hcaldqm::UpdateFreq uf) { DQTask::_resetMonitors(uf); }
0130
0131 void HcalMLTask::_process(edm::Event const& e, edm::EventSetup const&) {
0132 if (_ptype != fOnline)
0133 return;
0134
0135 auto const chbhe = e.getHandle(tokQIE11);
0136
0137 if (not(chbhe.isValid())) {
0138 edm::LogWarning("HcalMLTask") << "QIE11 Collection is unavailable, will not fill this event.";
0139 return;
0140 }
0141
0142 auto lumiCache = luminosityBlockCache(e.getLuminosityBlock().index());
0143 _currentLS = lumiCache->currentLS;
0144
0145 for (QIE11DigiCollection::const_iterator it = chbhe->begin(); it != chbhe->end(); ++it) {
0146 const QIE11DataFrame digi = static_cast<const QIE11DataFrame>(*it);
0147
0148 HcalDetId const& did = digi.detid();
0149 if (did.subdet() != HcalEndcap && did.subdet() != HcalBarrel)
0150 continue;
0151
0152 Occupancy1LS.get(did)++;
0153 }
0154 }
0155
0156 std::shared_ptr<hcaldqm::Cache> HcalMLTask::globalBeginLuminosityBlock(edm::LuminosityBlock const& lb,
0157 edm::EventSetup const& es) const {
0158 return DQTask::globalBeginLuminosityBlock(lb, es);
0159 }
0160
0161 void HcalMLTask::globalEndLuminosityBlock(edm::LuminosityBlock const& lb, edm::EventSetup const& es) {
0162 auto lumiCache = luminosityBlockCache(lb.index());
0163 _currentLS = lumiCache->currentLS;
0164 _xQuality.reset();
0165 _xQuality = lumiCache->xQuality;
0166
0167 for (auto& HistElement : digiHcal2DHist_depth_1)
0168 std::fill(HistElement.begin(), HistElement.end(), 0);
0169 for (auto& HistElement : digiHcal2DHist_depth_2)
0170 std::fill(HistElement.begin(), HistElement.end(), 0);
0171 for (auto& HistElement : digiHcal2DHist_depth_3)
0172 std::fill(HistElement.begin(), HistElement.end(), 0);
0173 for (auto& HistElement : digiHcal2DHist_depth_4)
0174 std::fill(HistElement.begin(), HistElement.end(), 0);
0175 for (auto& HistElement : digiHcal2DHist_depth_5)
0176 std::fill(HistElement.begin(), HistElement.end(), 0);
0177 for (auto& HistElement : digiHcal2DHist_depth_6)
0178 std::fill(HistElement.begin(), HistElement.end(), 0);
0179 for (auto& HistElement : digiHcal2DHist_depth_7)
0180 std::fill(HistElement.begin(), HistElement.end(), 0);
0181 float LS_numEvents = (float)_evsPerLS;
0182
0183 std::vector<HcalGenericDetId> dids = _emap->allPrecisionId();
0184 for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
0185 if (!it->isHcalDetId())
0186 continue;
0187 if (_xQuality.exists(HcalDetId(*it))) {
0188 HcalChannelStatus cs(it->rawId(), _xQuality.get(HcalDetId(*it)));
0189 if (cs.isBitSet(HcalChannelStatus::HcalCellMask) || cs.isBitSet(HcalChannelStatus::HcalCellDead))
0190 continue;
0191 }
0192
0193 HcalDetId did = HcalDetId(it->rawId());
0194 if (did.subdet() != HcalEndcap && did.subdet() != HcalBarrel)
0195 continue;
0196
0197 if (did.depth() == 1)
0198 digiHcal2DHist_depth_1.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0199 Occupancy1LS.get(did);
0200 if (did.depth() == 2)
0201 digiHcal2DHist_depth_2.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0202 Occupancy1LS.get(did);
0203 if (did.depth() == 3)
0204 digiHcal2DHist_depth_3.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0205 Occupancy1LS.get(did);
0206 if (did.depth() == 4)
0207 digiHcal2DHist_depth_4.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0208 Occupancy1LS.get(did);
0209 if (did.depth() == 5)
0210 digiHcal2DHist_depth_5.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0211 Occupancy1LS.get(did);
0212 if (did.depth() == 6)
0213 digiHcal2DHist_depth_6.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0214 Occupancy1LS.get(did);
0215 if (did.depth() == 7)
0216 digiHcal2DHist_depth_7.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
0217 Occupancy1LS.get(did);
0218 }
0219
0220 std::vector<std::vector<float>> ad_HBmodel_output_vectors = dqmadObj_HB->Inference_CMSSW(digiHcal2DHist_depth_1,
0221 digiHcal2DHist_depth_2,
0222 digiHcal2DHist_depth_3,
0223 digiHcal2DHist_depth_4,
0224 digiHcal2DHist_depth_5,
0225 digiHcal2DHist_depth_6,
0226 digiHcal2DHist_depth_7,
0227 LS_numEvents,
0228 (float)flagDecisionThr);
0229
0230 std::vector<std::vector<float>> ad_HEmodel_output_vectors = dqmadObj_HE->Inference_CMSSW(digiHcal2DHist_depth_1,
0231 digiHcal2DHist_depth_2,
0232 digiHcal2DHist_depth_3,
0233 digiHcal2DHist_depth_4,
0234 digiHcal2DHist_depth_5,
0235 digiHcal2DHist_depth_6,
0236 digiHcal2DHist_depth_7,
0237 LS_numEvents,
0238 (float)flagDecisionThr);
0239
0240 std::vector<std::vector<std::vector<float>>> digiHcal3DHist_ANOMALY_FLAG_HB =
0241 dqmadObj_HB->ONNXOutputToDQMHistMap(ad_HBmodel_output_vectors, 4, 64, 7);
0242 std::vector<std::vector<std::vector<float>>> digiHcal3DHist_ANOMALY_FLAG_HE =
0243 dqmadObj_HE->ONNXOutputToDQMHistMap(ad_HEmodel_output_vectors, 7, 64, 7);
0244
0245 int NHB_MLbadflags_ = 0, NHE_MLbadflags_ = 0;
0246 for (const auto& plane : digiHcal3DHist_ANOMALY_FLAG_HB)
0247 for (const auto& row : plane)
0248 NHB_MLbadflags_ += std::count(row.begin(), row.end(), 1);
0249 for (const auto& plane : digiHcal3DHist_ANOMALY_FLAG_HE)
0250 for (const auto& row : plane)
0251 NHE_MLbadflags_ += std::count(row.begin(), row.end(), 1);
0252
0253 MLFlagvsLS_Subdet.fill(HcalDetId(HcalBarrel, 1, 1, 1), _currentLS, NHB_MLbadflags_);
0254 MLFlagvsLS_Subdet.fill(HcalDetId(HcalEndcap, 17, 1, 1), _currentLS, NHE_MLbadflags_);
0255
0256 Occupancy1LS.reset();
0257 DQTask::globalEndLuminosityBlock(lb, es);
0258 }
0259
0260 void HcalMLTask::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0261 edm::ParameterSetDescription desc;
0262 desc.addUntracked<std::string>("name", "HcalMLTask");
0263 desc.addUntracked<std::string>(
0264 "onnx_model_path_HB",
0265 "DQM/HcalTasks/data/HB_2022/"
0266 "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_5218_v06_02_2023_21h01_stateful.onnx");
0267 desc.addUntracked<std::string>(
0268 "onnx_model_path_HE",
0269 "DQM/HcalTasks/data/HE_2022/"
0270 "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_7763_v06_02_2023_22h55_stateful.onnx");
0271 desc.addUntracked<double>("flagDecisionThr", 20.);
0272 desc.addUntracked<int>("debug", 0);
0273 desc.addUntracked<int>("runkeyVal", 0);
0274 desc.addUntracked<std::string>("runkeyName", "pp_run");
0275 desc.addUntracked<int>("ptype", 1);
0276 desc.addUntracked<bool>("mtype", true);
0277 desc.addUntracked<std::string>("subsystem", "Hcal");
0278 desc.addUntracked<edm::InputTag>("tagHBHE", edm::InputTag("hcalDigis"));
0279 desc.addUntracked<edm::InputTag>("tagHO", edm::InputTag("hcalDigis"));
0280 desc.addUntracked<edm::InputTag>("tagHF", edm::InputTag("hcalDigis"));
0281 descriptions.addWithDefaultLabel(desc);
0282 }
0283
0284 DEFINE_FWK_MODULE(HcalMLTask);