CMS 3D CMS Logo

HcalMLTask.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Long Wang (UMD)
3 // plugin to run ML4DQM ONNX module and plot number of flagged bad channel counts vs LS
4 //
5 
17 
21 
23 
24 #include <cmath>
25 #include <iostream>
26 #include <algorithm>
27 
28 using namespace cms::Ort;
29 using namespace hcaldqm;
30 using namespace hcaldqm::constants;
31 using namespace hcaldqm::filter;
32 
33 class HcalMLTask : public hcaldqm::DQTask {
34 public:
36  ~HcalMLTask() override = default;
37 
38  void dqmBeginRun(edm::Run const&, edm::EventSetup const&) override;
39  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
40  std::shared_ptr<hcaldqm::Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
41  edm::EventSetup const&) const override;
42  void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
43  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
44 
45 private:
46  void _process(edm::Event const&, edm::EventSetup const&) override;
47  void _resetMonitors(hcaldqm::UpdateFreq) override;
48 
49  std::string onnx_model_path_HB, onnx_model_path_HE;
58 
61 
62  std::unique_ptr<OnlineDQMDigiAD> dqmadObj_HB = nullptr;
63  std::unique_ptr<OnlineDQMDigiAD> dqmadObj_HE = nullptr;
64 
65  std::vector<std::vector<float>> digiHcal2DHist_depth_1{
66  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
67  std::vector<std::vector<float>> digiHcal2DHist_depth_2{
68  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
69  std::vector<std::vector<float>> digiHcal2DHist_depth_3{
70  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
71  std::vector<std::vector<float>> digiHcal2DHist_depth_4{
72  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
73  std::vector<std::vector<float>> digiHcal2DHist_depth_5{
74  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
75  std::vector<std::vector<float>> digiHcal2DHist_depth_6{
76  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
77  std::vector<std::vector<float>> digiHcal2DHist_depth_7{
78  std::vector<std::vector<float>>(64, std::vector<float>(72, 0))};
79 };
80 
82  : DQTask(ps), hcalDbServiceToken_(esConsumes<HcalDbService, HcalDbRecord, edm::Transition::BeginRun>()) {
84  "onnx_model_path_HB",
85  "DQM/HcalTasks/data/HB_2022/"
86  "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_5218_v06_02_2023_21h01_stateful.onnx");
88  "onnx_model_path_HE",
89  "DQM/HcalTasks/data/HE_2022/"
90  "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_7763_v06_02_2023_22h55_stateful.onnx");
91  flagDecisionThr = ps.getUntrackedParameter<double>("flagDecisionThr", 20.);
92  tagQIE11 = ps.getUntrackedParameter<edm::InputTag>("tagHBHE", edm::InputTag("hcalDigis"));
93  tagHO = ps.getUntrackedParameter<edm::InputTag>("tagHO", edm::InputTag("hcalDigis"));
94  tagQIE10 = ps.getUntrackedParameter<edm::InputTag>("tagHF", edm::InputTag("hcalDigis"));
95 
96  tokQIE11 = consumes<QIE11DigiCollection>(tagQIE11);
97  tokHO = consumes<HODigiCollection>(tagHO);
98  tokQIE10 = consumes<QIE10DigiCollection>(tagQIE10);
99 
100  auto dqmadObj_HB_ = std::make_unique<OnlineDQMDigiAD>("hb", onnx_model_path_HB, Backend::cpu);
101  auto dqmadObj_HE_ = std::make_unique<OnlineDQMDigiAD>("he", onnx_model_path_HE, Backend::cpu);
102  dqmadObj_HB = std::move(dqmadObj_HB_);
103  dqmadObj_HE = std::move(dqmadObj_HE_);
104 }
105 
106 void HcalMLTask::dqmBeginRun(edm::Run const& r, edm::EventSetup const& es) { DQTask::dqmBeginRun(r, es); }
107 
110 
111  // GET WHAT YOU NEED
113  _emap = dbs->getHcalMapping();
114 
115  // Book monitoring elements
117 
119  "MLBadFlagedChannelsvsLS",
123  0);
124 
127 }
128 
129 void HcalMLTask::_resetMonitors(hcaldqm::UpdateFreq uf) { DQTask::_resetMonitors(uf); }
130 
132  if (_ptype != fOnline)
133  return;
134 
135  auto const chbhe = e.getHandle(tokQIE11);
136 
137  if (not(chbhe.isValid())) {
138  edm::LogWarning("HcalMLTask") << "QIE11 Collection is unavailable, will not fill this event.";
139  return;
140  }
141 
142  auto lumiCache = luminosityBlockCache(e.getLuminosityBlock().index());
143  _currentLS = lumiCache->currentLS;
144 
145  for (QIE11DigiCollection::const_iterator it = chbhe->begin(); it != chbhe->end(); ++it) {
146  const QIE11DataFrame digi = static_cast<const QIE11DataFrame>(*it);
147 
148  HcalDetId const& did = digi.detid();
149  if (did.subdet() != HcalEndcap && did.subdet() != HcalBarrel)
150  continue;
151 
152  Occupancy1LS.get(did)++;
153  }
154 }
155 
156 std::shared_ptr<hcaldqm::Cache> HcalMLTask::globalBeginLuminosityBlock(edm::LuminosityBlock const& lb,
157  edm::EventSetup const& es) const {
158  return DQTask::globalBeginLuminosityBlock(lb, es);
159 }
160 
162  auto lumiCache = luminosityBlockCache(lb.index());
163  _currentLS = lumiCache->currentLS;
164  _xQuality.reset();
165  _xQuality = lumiCache->xQuality;
166 
167  for (auto& HistElement : digiHcal2DHist_depth_1)
168  std::fill(HistElement.begin(), HistElement.end(), 0);
169  for (auto& HistElement : digiHcal2DHist_depth_2)
170  std::fill(HistElement.begin(), HistElement.end(), 0);
171  for (auto& HistElement : digiHcal2DHist_depth_3)
172  std::fill(HistElement.begin(), HistElement.end(), 0);
173  for (auto& HistElement : digiHcal2DHist_depth_4)
174  std::fill(HistElement.begin(), HistElement.end(), 0);
175  for (auto& HistElement : digiHcal2DHist_depth_5)
176  std::fill(HistElement.begin(), HistElement.end(), 0);
177  for (auto& HistElement : digiHcal2DHist_depth_6)
178  std::fill(HistElement.begin(), HistElement.end(), 0);
179  for (auto& HistElement : digiHcal2DHist_depth_7)
180  std::fill(HistElement.begin(), HistElement.end(), 0);
181  float LS_numEvents = (float)_evsPerLS;
182 
183  std::vector<HcalGenericDetId> dids = _emap->allPrecisionId();
184  for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
185  if (!it->isHcalDetId())
186  continue;
187  if (_xQuality.exists(HcalDetId(*it))) {
190  continue;
191  }
192 
193  HcalDetId did = HcalDetId(it->rawId());
194  if (did.subdet() != HcalEndcap && did.subdet() != HcalBarrel)
195  continue;
196 
197  if (did.depth() == 1)
198  digiHcal2DHist_depth_1.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
199  Occupancy1LS.get(did);
200  if (did.depth() == 2)
201  digiHcal2DHist_depth_2.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
202  Occupancy1LS.get(did);
203  if (did.depth() == 3)
204  digiHcal2DHist_depth_3.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
205  Occupancy1LS.get(did);
206  if (did.depth() == 4)
207  digiHcal2DHist_depth_4.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
208  Occupancy1LS.get(did);
209  if (did.depth() == 5)
210  digiHcal2DHist_depth_5.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
211  Occupancy1LS.get(did);
212  if (did.depth() == 6)
213  digiHcal2DHist_depth_6.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
214  Occupancy1LS.get(did);
215  if (did.depth() == 7)
216  digiHcal2DHist_depth_7.at(did.ieta() < 0 ? did.ieta() + 32 : did.ieta() + 31).at(did.iphi() - 1) =
217  Occupancy1LS.get(did);
218  }
219 
220  std::vector<std::vector<float>> ad_HBmodel_output_vectors = dqmadObj_HB->Inference_CMSSW(digiHcal2DHist_depth_1,
227  LS_numEvents,
228  (float)flagDecisionThr);
229 
230  std::vector<std::vector<float>> ad_HEmodel_output_vectors = dqmadObj_HE->Inference_CMSSW(digiHcal2DHist_depth_1,
237  LS_numEvents,
238  (float)flagDecisionThr);
239 
240  std::vector<std::vector<std::vector<float>>> digiHcal3DHist_ANOMALY_FLAG_HB =
241  dqmadObj_HB->ONNXOutputToDQMHistMap(ad_HBmodel_output_vectors, 4, 64, 7);
242  std::vector<std::vector<std::vector<float>>> digiHcal3DHist_ANOMALY_FLAG_HE =
243  dqmadObj_HE->ONNXOutputToDQMHistMap(ad_HEmodel_output_vectors, 7, 64, 7);
244 
245  int NHB_MLbadflags_ = 0, NHE_MLbadflags_ = 0;
246  for (const auto& plane : digiHcal3DHist_ANOMALY_FLAG_HB)
247  for (const auto& row : plane)
248  NHB_MLbadflags_ += std::count(row.begin(), row.end(), 1);
249  for (const auto& plane : digiHcal3DHist_ANOMALY_FLAG_HE)
250  for (const auto& row : plane)
251  NHE_MLbadflags_ += std::count(row.begin(), row.end(), 1);
252 
253  MLFlagvsLS_Subdet.fill(HcalDetId(HcalBarrel, 1, 1, 1), _currentLS, NHB_MLbadflags_);
254  MLFlagvsLS_Subdet.fill(HcalDetId(HcalEndcap, 17, 1, 1), _currentLS, NHE_MLbadflags_);
255 
257  DQTask::globalEndLuminosityBlock(lb, es);
258 }
259 
262  desc.addUntracked<std::string>("name", "HcalMLTask");
263  desc.addUntracked<std::string>(
264  "onnx_model_path_HB",
265  "DQM/HcalTasks/data/HB_2022/"
266  "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_5218_v06_02_2023_21h01_stateful.onnx");
267  desc.addUntracked<std::string>(
268  "onnx_model_path_HE",
269  "DQM/HcalTasks/data/HE_2022/"
270  "CGAE_MultiDim_SPATIAL_vONNX_RCLv22_PIXEL_BT_BN_RIN_IPHI_MED_7763_v06_02_2023_22h55_stateful.onnx");
271  desc.addUntracked<double>("flagDecisionThr", 20.);
272  desc.addUntracked<int>("debug", 0);
273  desc.addUntracked<int>("runkeyVal", 0);
274  desc.addUntracked<std::string>("runkeyName", "pp_run");
275  desc.addUntracked<int>("ptype", 1);
276  desc.addUntracked<bool>("mtype", true);
277  desc.addUntracked<std::string>("subsystem", "Hcal");
278  desc.addUntracked<edm::InputTag>("tagHBHE", edm::InputTag("hcalDigis"));
279  desc.addUntracked<edm::InputTag>("tagHO", edm::InputTag("hcalDigis"));
280  desc.addUntracked<edm::InputTag>("tagHF", edm::InputTag("hcalDigis"));
281  descriptions.addWithDefaultLabel(desc);
282 }
283 
std::string onnx_model_path_HB
Definition: HcalMLTask.cc:49
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
virtual void initialize(std::string const &folder, hashfunctions::HashType, quantity::Quantity *, quantity::Quantity *qy=new quantity::ValueQuantity(quantity::fN), int debug=0)
Definition: Container1D.cc:33
virtual bool exists(HcalDetId const &)
Definition: ContainerXXX.h:213
std::vector< std::vector< float > > digiHcal2DHist_depth_5
Definition: HcalMLTask.cc:73
std::string onnx_model_path_HE
Definition: HcalMLTask.cc:49
hcaldqm::ContainerXXX< double > Occupancy1LS
Definition: HcalMLTask.cc:59
dbs
Find files in dataset.
edm::EDGetTokenT< QIE10DigiCollection > tokQIE10
Definition: HcalMLTask.cc:56
UpdateFreq
Definition: DQTask.h:32
std::vector< std::vector< float > > digiHcal2DHist_depth_3
Definition: HcalMLTask.cc:69
edm::InputTag tagHO
Definition: HcalMLTask.cc:52
virtual void reset()
Definition: ContainerXXX.h:293
edm::InputTag tagQIE10
Definition: HcalMLTask.cc:53
constexpr DetId detid() const
Get the detector id.
example_stream void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
T getUntrackedParameter(std::string const &, T const &) const
std::unique_ptr< OnlineDQMDigiAD > dqmadObj_HB
Definition: HcalMLTask.cc:62
constexpr HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:138
ProcessingType _ptype
Definition: DQModule.h:44
virtual void fill(uint32_t)
Definition: Container1D.cc:73
std::vector< std::vector< float > > digiHcal2DHist_depth_4
Definition: HcalMLTask.cc:71
edm::EDGetTokenT< HODigiCollection > tokHO
Definition: HcalMLTask.cc:55
std::shared_ptr< hcaldqm::Cache > globalBeginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const override
Definition: HcalMLTask.cc:156
ContainerXXX< uint32_t > _xQuality
Definition: DQTask.h:66
constexpr int ieta() const
get the cell ieta
Definition: HcalDetId.h:155
void dqmBeginRun(edm::Run const &, edm::EventSetup const &) override
Definition: HcalMLTask.cc:106
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< QIE11DigiCollection > tokQIE11
Definition: HcalMLTask.cc:54
std::vector< std::vector< float > > digiHcal2DHist_depth_1
Definition: HcalMLTask.cc:65
std::string _name
Definition: DQModule.h:42
HcalElectronicsMap const * _emap
Definition: DQTask.h:81
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
hcaldqm::Container1D MLFlagvsLS_Subdet
Definition: HcalMLTask.cc:60
std::unique_ptr< OnlineDQMDigiAD > dqmadObj_HE
Definition: HcalMLTask.cc:63
std::vector< std::vector< float > > digiHcal2DHist_depth_7
Definition: HcalMLTask.cc:77
double flagDecisionThr
Definition: HcalMLTask.cc:50
edm::ESGetToken< HcalDbService, HcalDbRecord > hcalDbServiceToken_
Definition: HcalMLTask.cc:57
std::vector< std::vector< float > > digiHcal2DHist_depth_2
Definition: HcalMLTask.cc:67
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual void book(HcalElectronicsMap const *)
Definition: ContainerXXX.h:88
virtual void book(DQMStore::IBooker &, HcalElectronicsMap const *, std::string subsystem="Hcal", std::string aux="")
Definition: Container1D.cc:592
boost::transform_iterator< IterHelp, boost::counting_iterator< int > > const_iterator
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: HcalMLTask.cc:108
LuminosityBlockIndex index() const
void _process(edm::Event const &, edm::EventSetup const &) override
Definition: HcalMLTask.cc:131
virtual STDTYPE & get(HcalDetId const &)
Definition: ContainerXXX.h:198
HLT enums.
std::vector< HcalGenericDetId > allPrecisionId() const
std::string _subsystem
Definition: DQModule.h:49
void globalEndLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
Definition: HcalMLTask.cc:161
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: HcalMLTask.cc:260
virtual void initialize(hashfunctions::HashType, int debug=0)
Definition: ContainerXXX.h:82
HcalMLTask(edm::ParameterSet const &)
Definition: HcalMLTask.cc:81
Log< level::Warning, false > LogWarning
void _resetMonitors(hcaldqm::UpdateFreq) override
Definition: HcalMLTask.cc:129
constexpr int iphi() const
get the cell iphi
Definition: HcalDetId.h:157
def move(src, dest)
Definition: eostools.py:511
std::vector< std::vector< float > > digiHcal2DHist_depth_6
Definition: HcalMLTask.cc:75
Definition: Run.h:45
ib
Definition: cuy.py:661
edm::InputTag tagQIE11
Definition: HcalMLTask.cc:51
constexpr int depth() const
get the tower depth
Definition: HcalDetId.h:164