Belle II Software development
DQMHistAnalysisECLSummary.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9//THIS MODULE
10#include <dqm/analysis/modules/DQMHistAnalysisECLSummary.h>
11
12//ECL
13#include <ecl/geometry/ECLGeometryPar.h>
14
15//ROOT
16#include <TStyle.h>
17#include <TLine.h>
18
19//boost
20#include "boost/format.hpp"
21
22using namespace Belle2;
23
24REG_MODULE(DQMHistAnalysisECLSummary);
25
28 m_neighbours_obj("F", 0.1)
29{
30
31 B2DEBUG(20, "DQMHistAnalysisECLSummary: Constructor done.");
32 addParam("pvPrefix", m_pvPrefix, "Prefix to use for PVs registered by this module",
33 std::string("ECL:channels_info:"));
34 addParam("useChannelMask", m_useChannelMask,
35 "Mask Cell IDs based on information from ECL PVs",
36 true);
37 addParam("maxDeviationForOccupancy", m_maxDeviationForOccupancy,
38 "The higher this parameter, the larger differences in occupancy are allowed for adjacent channels", 0.28);
39 addParam("maxDeviationForChi2", m_maxDeviationForChi2,
40 "The higher this parameter, the larger differences in the number of hits with bad chi2 are allowed for adjacent channels",
41 2.5);
42 addParam("onlyIfUpdated", m_onlyIfUpdated, "If true (default), update EPICS PVs only if histograms were updated.",
43 true);
44}
45
47{
48 m_mapper.initFromFile();
49
50 //=== Set up ECL alarms and corresponding PVs
51
52 m_ecl_alarms = {
53 // name title alarm_limit
54 // | | | warning_limit
55 // | | | | required_statistics
56 // | | | | |
57 {"dead", "#splitline{dead}{channels}", 1, 1, 3e4},
58 {"cold", "#splitline{cold}{channels}", 1, 2, 1e5},
59 {"hot", "#splitline{hot}{channels}", 25, 50, 1e5},
60 {"bad_chi2", "#splitline{bad #chi^{2}}{channels}", 5, 10, 1e6},
61 {"bad_fit", "#splitline{fit incon-}{sistencies}", 5, 10, 0 },
62 };
63
64 // Prepare EPICS PVs
65 for (auto& alarm : m_ecl_alarms) {
66 // By crate
67 for (int crate_id = 1; crate_id <= ECL::ECL_CRATES; crate_id++) {
68 std::string pv_name = (boost::format("crate%02d:%s") % crate_id % alarm.name).str();
69 registerEpicsPV(m_pvPrefix + pv_name, pv_name);
70 }
71 // Totals
72 for (auto& ecl_part : {"All", "FWDEndcap", "Barrel", "BWDEndcap"}) {
73 std::string pv_name = (boost::format("%s:%s") % ecl_part % alarm.name).str();
74 registerEpicsPV(m_pvPrefix + pv_name, pv_name);
75 }
76 // Masked Cell IDs
77 std::string mask_pv_name = (boost::format("mask:%s") % alarm.name).str();
78 registerEpicsPV(m_pvPrefix + mask_pv_name, mask_pv_name);
79 }
80
82
83 //=== Set up the histogram to indicate alarm status
84
85 TString title = "#splitline{ECL errors monitoring}";
86 title += "{E - Error, W - Warning, L - Low statistics}";
87 title += ";ECLCollector ID (same as Crate ID)";
88 h_channels_summary = new TH2F("channels_summary", title,
89 ECL::ECL_CRATES, 1, ECL::ECL_CRATES + 1,
90 m_ecl_alarms.size(), 0, m_ecl_alarms.size());
91
92 // Do not show statistics box.
93 h_channels_summary->SetStats(0);
94 h_channels_summary->SetMinimum(0);
95 h_channels_summary->SetMaximum(1);
96
97 //=== Set X axis labels
98
99 for (int i = 1; i <= ECL::ECL_CRATES; i++) {
100 h_channels_summary->GetXaxis()->SetBinLabel(i, std::to_string(i).c_str());
101 }
102 h_channels_summary->LabelsOption("v", "X"); // Rotate X axis labels 90 degrees
103 h_channels_summary->SetTickLength(0, "XY");
104
105 //=== Customize offsets and margins
106
107 h_channels_summary->GetXaxis()->SetTitleOffset(0.95);
108 h_channels_summary->GetXaxis()->SetTitleSize(0.05);
109 h_channels_summary->GetXaxis()->SetLabelSize(0.04);
110 h_channels_summary->GetYaxis()->SetLabelSize(0.06);
111
112 c_channels_summary = new TCanvas("ECL/c_channels_summary_analysis");
113 c_channels_summary->SetTopMargin(0.10);
114 c_channels_summary->SetLeftMargin(0.20);
115 c_channels_summary->SetRightMargin(0.005);
116 c_channels_summary->SetBottomMargin(0.10);
117
118 //=== Additional canvases/histograms to display which channels have problems
119 c_occupancy = new TCanvas("ECL/c_cid_Thr5MeV_overlaid_analysis");
120 c_bad_chi2 = new TCanvas("ECL/c_bad_quality_overlaid_analysis");
121
122 h_bad_occ_overlay = new TH1F("bad_occ_overlay", "", ECL::ECL_TOTAL_CHANNELS,
123 1, ECL::ECL_TOTAL_CHANNELS + 1);
124 h_bad_occ_overlay->SetLineColor(kRed);
125 h_bad_occ_overlay->SetLineStyle(kDashed);
126 h_bad_occ_overlay->SetFillColor(kRed);
127 h_bad_occ_overlay->SetFillStyle(3007);
128
129 h_bad_chi2_overlay = new TH1F("bad_chi2_overlay", "", ECL::ECL_TOTAL_CHANNELS,
130 1, ECL::ECL_TOTAL_CHANNELS + 1);
131 h_bad_chi2_overlay->SetLineColor(kRed);
132 h_bad_chi2_overlay->SetLineStyle(kDashed);
133 h_bad_chi2_overlay->SetFillColor(kRed);
134 h_bad_chi2_overlay->SetFillStyle(3007);
135
136 h_bad_occ_overlay_green = new TH1F("bad_occ_overlay_green", "", ECL::ECL_TOTAL_CHANNELS,
137 1, ECL::ECL_TOTAL_CHANNELS + 1);
138 h_bad_occ_overlay_green->SetLineColor(kGreen);
139 h_bad_occ_overlay_green->SetLineStyle(kDotted);
140 h_bad_occ_overlay_green->SetFillColor(kGreen);
141 h_bad_occ_overlay_green->SetFillStyle(3013);
142
143 h_bad_chi2_overlay_green = new TH1F("bad_chi2_overlay_green", "", ECL::ECL_TOTAL_CHANNELS,
144 1, ECL::ECL_TOTAL_CHANNELS + 1);
145 h_bad_chi2_overlay_green->SetLineColor(kGreen);
146 h_bad_chi2_overlay_green->SetLineStyle(kDotted);
147 h_bad_chi2_overlay_green->SetFillColor(kGreen);
148 h_bad_chi2_overlay_green->SetFillStyle(3013);
149
150 B2DEBUG(20, "DQMHistAnalysisECLSummary: initialized.");
151}
152
154{
155 B2DEBUG(20, "DQMHistAnalysisECLSummary: beginRun called.");
156
157 //=== Update m_ecl_alarms based on PV limits
158
160
161 //=== Set Y axis labels from m_ecl_alarms
162
163 for (size_t i = 0; i < m_ecl_alarms.size(); i++) {
164 TString label = m_ecl_alarms[i].title;
165 label += " #geq ";
166 label += m_ecl_alarms[i].alarm_limit;
167 h_channels_summary->GetYaxis()->SetBinLabel(i + 1, label);
168 }
169}
170
172{
173 TH1* h_total_events = findHist("ECL/event", m_onlyIfUpdated);
174 if (!h_total_events) return;
175 m_total_events = h_total_events->GetEntries();
176
177 c_channels_summary->Clear();
178 h_channels_summary->Reset();
179
180 // [alarm_type][crate_id - 1] -> number of problematic channels in that crate
181 std::vector< std::vector<int> > alarm_counts = updateAlarmCounts();
182
183 //=== Set warning and error indicators on the histogram
184
185 const double HISTCOLOR_RED = 0.9;
186 const double HISTCOLOR_GREEN = 0.45;
187 const double HISTCOLOR_ORANGE = 0.65;
188 const double HISTCOLOR_GRAY = 0.00;
189
190 // TODO: Currently, I am explicitly removing
191 // TText labels from the histogram. However,
192 // normally I would assume that canvas->Clear()
193 // should have already removed them. So
194 // it might indicate a memory leak or some
195 // problem with ROOT->JSON->ROOT conversion
196 for (auto& text : m_labels) {
197 delete text;
198 }
199 m_labels.clear();
200
201 //== Set correct warning/error colors for each bin based on alarm thresholds
202
203 bool enough = false;
204 bool warn_flag = false;
205 bool error_flag = false;
206
207 for (size_t alarm_idx = 0; alarm_idx < alarm_counts.size(); alarm_idx++) {
208 for (size_t crate = 0; crate < alarm_counts[alarm_idx].size(); crate++) {
209 double color;
210 char label_text[2] = {0};
211 int alarm_limit = m_ecl_alarms[alarm_idx].alarm_limit;
212 int warning_limit = m_ecl_alarms[alarm_idx].warning_limit;
213
214 int alarms = alarm_counts[alarm_idx][crate];
215
216 if (m_total_events < m_ecl_alarms[alarm_idx].required_statistics || alarms < 0) {
217 // `alarms < 0` means that we could not get the relevant histogram.
218 // We assume that the histogram is not yet available due to low
219 // statistics but this is not a guarantee.
220 color = HISTCOLOR_GRAY;
221 label_text[0] = 'L';
222 } else if (alarms >= alarm_limit) {
223 color = HISTCOLOR_RED;
224 label_text[0] = 'E';
225 error_flag = true;
226 enough = true;
227 } else if (alarms >= warning_limit) {
228 color = HISTCOLOR_ORANGE;
229 label_text[0] = 'W';
230 warn_flag = true;
231 enough = true;
232 } else {
233 color = HISTCOLOR_GREEN;
234 enough = true;
235 }
236 if (label_text[0] == 'E' || label_text[0] == 'W') {
237 B2DEBUG(100, "Non-zero (" << alarm_counts[alarm_idx][crate]
238 << ") for alarm_idx, crate = " << alarm_idx << ", " << crate);
239 }
240 h_channels_summary->SetBinContent(crate + 1, alarm_idx + 1, color);
241 if (label_text[0] != 0) {
242 auto text = new TText((crate + 1.5), (alarm_idx + 0.5), label_text);
243 if (label_text[0] != 'L') text->SetTextColor(kWhite);
244 text->SetTextSize(0.03);
245 text->SetTextAlign(22); // centered
246 m_labels.push_back(text);
247 }
248 }
249 }
250
251 //=== Draw histogram, labels and grid
252
253 // Customize title
254 auto gstyle_title_h = gStyle->GetTitleH();
255 auto gstyle_title_x = gStyle->GetTitleX();
256 auto gstyle_title_y = gStyle->GetTitleY();
257 gStyle->SetTitleH(0.04);
258 gStyle->SetTitleX(0.60);
259 gStyle->SetTitleY(1.00);
260
261 //=== Set background color based on combined status.
262 c_channels_summary->cd();
263 colorizeCanvas(c_channels_summary, makeStatus(enough, warn_flag, error_flag));
265
266 //=== Prepare special style objects to use correct color palette
267 // and use it only for this histogram
268 // Based on https://root-forum.cern.ch/t/different-color-palettes-for-different-plots-with-texec/5250/3
269 // and https://root.cern/doc/master/multipalette_8C.html
270
271 //=== Draw with special style
272 // https://root.cern.ch/js/latest/examples.htm#th2_colpal77
273 h_channels_summary->Draw("");
274 h_channels_summary->Draw("col;pal55;same");
275 for (auto& text : m_labels) {
276 text->Draw();
277 }
279
280 //
281 c_channels_summary->Modified();
282 c_channels_summary->Update();
283 c_channels_summary->Draw();
284
285 m_ecl_style = new TExec("ecl_style",
286 "gStyle->SetPalette(kRainBow);");
287 c_channels_summary->GetListOfPrimitives()->AddFirst(m_ecl_style);
288 m_default_style = new TExec("default_style",
289 "gStyle->SetPalette(kBird);"); // " Changing back to the default color palette
290 c_channels_summary->GetListOfPrimitives()->AddLast(m_default_style);
291
292 gStyle->SetTitleH(gstyle_title_h);
293 gStyle->SetTitleX(gstyle_title_x);
294 gStyle->SetTitleY(gstyle_title_y);
295}
296
298{
299 B2DEBUG(20, "DQMHistAnalysisECLSummary: endRun called");
300
301 updateAlarmCounts(true);
302}
303
304
306{
307 B2DEBUG(20, "terminate called");
308 delete c_channels_summary;
309 delete c_occupancy;
310 delete c_bad_chi2;
311 delete h_bad_occ_overlay;
312 delete h_bad_chi2_overlay;
313}
314
315std::pair<int, DQMHistAnalysisECLSummaryModule::ECLAlarmType> DQMHistAnalysisECLSummaryModule::getAlarmByName(std::string name)
316{
317 int index = 0;
318 for (auto& alarm_info : m_ecl_alarms) {
319 if (alarm_info.name == name) return {index, alarm_info};
320 index++;
321 }
322 B2FATAL("Could not get ECL alarm " + name);
323 return {-1, m_ecl_alarms[0]};
324}
325
327{
328 //===== Get alarm limits
329
330 for (auto& alarm : m_ecl_alarms) {
331 // In the current version, use only first crate PV to get alarm limits
332 int crate_id = 1;
333
334 std::string pv_name = (boost::format("crate%02d:%s") % crate_id % alarm.name).str();
335 double unused = 0;
336 double upper_warning_limit = 0;
337 double upper_alarm_limit = 0;
338 bool accessed = requestLimitsFromEpicsPVs(pv_name, unused, unused, upper_warning_limit, upper_alarm_limit);
339
340 if (!accessed || upper_alarm_limit <= 0 || upper_warning_limit <= 0) {
341 B2WARNING("Failed to get alarm limits");
342 return;
343 }
344
345 alarm.alarm_limit = upper_alarm_limit;
346 alarm.warning_limit = upper_warning_limit;
347 }
348
349 //===== Get masked channels
350
351 // alarm_type -> array_of_masked_channels
352 static std::map<std::string, dbr_sts_long_array> mask_info;
353 if (!getMaskedChannels(mask_info)) {
354 B2WARNING("Failed to get arrays of masked channels");
355 return;
356 }
357
358 for (auto& alarm : m_ecl_alarms) {
359 // Update the list of masked channels
360 m_mask[alarm.name].clear();
361 for (int i = 0; i < c_max_masked_channels; i++) {
362 int cell_id = mask_info[alarm.name].value[i];
363 // The array of masked channels goes like this: [15, 22, 716, 0, 0, 0, 0, 0]
364 // So if 0 (invalid channel ID) is encountered, stop reading further.
365 if (cell_id == 0) break;
366 m_mask[alarm.name].insert(cell_id);
367 }
368 }
369}
370
371bool DQMHistAnalysisECLSummaryModule::getMaskedChannels(std::map<std::string, dbr_sts_long_array>& mask_info)
372{
373 for (auto& alarm : m_ecl_alarms) {
374 std::string mask_pv_name = (boost::format("mask:%s") % alarm.name).str();
375 chid mask_chid = getEpicsPVChID(mask_pv_name);
376
377 if (mask_chid == nullptr) return false;
378 // ca_array_get data is NOT VALID here.
379 // It will only be valid after ca_pend_io returns ECA_NORMAL
380 auto r = ca_array_get(DBR_STS_LONG, c_max_masked_channels, mask_chid, &mask_info[alarm.name]);
381 if (r != ECA_NORMAL) return false;
382 }
383
384 // ca_pend_io has to be called here,
385 // see https://epics.anl.gov/base/R3-14/12-docs/CAref.html#ca_get
386
387 auto r = ca_pend_io(5.0);
388 if (r != ECA_NORMAL) return false;
389
390 return true;
391}
392
393std::vector< std::vector<int> > DQMHistAnalysisECLSummaryModule::updateAlarmCounts(bool update_mirabelle)
394{
395 std::vector< std::vector<int> > alarm_counts(
396 m_ecl_alarms.size(), std::vector<int>(ECL::ECL_CRATES));
397
398 //=== Get number of fit inconsistencies
399
400 TH1* h_fail_crateid = findHist("ECL/fail_crateid", false);
401
402 const int fit_alarm_index = getAlarmByName("bad_fit").first;
403 for (int crate_id = 1; crate_id <= ECL::ECL_CRATES; crate_id++) {
404 int errors_count = 0;
405 if (h_fail_crateid) {
406 errors_count = h_fail_crateid->GetBinContent(crate_id);
407 } else {
408 errors_count = -1;
409 }
410
411 alarm_counts[fit_alarm_index][crate_id - 1] = errors_count;
412 }
413
414 // [Cell ID] -> error_bitmask
415 std::map<int, int> error_bitmasks;
416
417 //=== Get number of dead/cold/hot channels
418 // cppcheck-suppress unassignedVariable
419 for (auto& [cell_id, error_bitmask] : getChannelsWithOccupancyProblems()) {
420 error_bitmasks[cell_id] |= error_bitmask;
421 }
422 //=== Get number of channels with bad_chi2
423 // cppcheck-suppress unassignedVariable
424 for (auto& [cell_id, error_bitmask] : getChannelsWithChi2Problems()) {
425 error_bitmasks[cell_id] |= error_bitmask;
426 }
427
428 //=== Combine the information
429
430 if (!update_mirabelle) {
431 h_bad_chi2_overlay->Reset();
432 h_bad_occ_overlay->Reset();
435 }
436
437 static std::vector<std::string> indices = {"dead", "cold", "hot", "bad_chi2"};
438 for (auto& index_name : indices) {
439 int alarm_index = getAlarmByName(index_name).first;
440 int alarm_bit = 1 << alarm_index;
441
442 for (auto& [cid, error_bitmask] : error_bitmasks) {
443 if ((error_bitmask & alarm_bit) == 0) continue;
444
445 bool masked = (m_mask[index_name].find(cid) != m_mask[index_name].end());
446
447 if (!masked) {
448 int crate_id = m_mapper.getCrateID(cid);
449 // If channel is not masked, increase the alarm counter.
450 alarm_counts[alarm_index][crate_id - 1] += 1;
451 }
452
453 if (update_mirabelle) continue;
454
455 // Unless it is the end of run, add problematic channels
456 // to the detailed 1D histogram.
457
458 if (index_name == "bad_chi2") {
459 if (!masked) h_bad_chi2_overlay->SetBinContent(cid, 1);
460 else h_bad_chi2_overlay_green->SetBinContent(cid, 1);
461 } else if (index_name == "dead" || index_name == "cold" || index_name == "hot") {
462 if (!masked) h_bad_occ_overlay->SetBinContent(cid, 1);
463 else h_bad_occ_overlay_green->SetBinContent(cid, 1);
464 }
465 }
466
467 if (update_mirabelle) continue;
468
469 //=== Prepare the histogram to display the list of all bad channels
470 if (index_name == "hot" || index_name == "bad_chi2") {
471 TCanvas* current_canvas;
472 TH1* main_hist;
473 TH1F* overlay_hist;
474 TH1F* overlay_hist_green;
475 if (index_name == "hot") {
476 main_hist = findHist("ECL/cid_Thr5MeV", m_onlyIfUpdated);
477 overlay_hist = h_bad_occ_overlay;
478 overlay_hist_green = h_bad_occ_overlay_green;
479 current_canvas = c_occupancy;
480 } else {
481 main_hist = findHist("ECL/bad_quality", m_onlyIfUpdated);
482 overlay_hist = h_bad_chi2_overlay;
483 overlay_hist_green = h_bad_chi2_overlay_green;
484 current_canvas = c_bad_chi2;
485 }
486
487 if (main_hist && main_hist->GetEntries() > 0) {
488 for (auto& overlay : {overlay_hist, overlay_hist_green}) {
489 for (int bin_id = 1; bin_id <= ECL::ECL_TOTAL_CHANNELS; bin_id++) {
490 if (overlay->GetBinContent(bin_id) == 0) continue;
491 // Do not adjust bin height for dead channels
492 if (main_hist->GetBinContent(bin_id) < 1e-6) continue;
493 overlay->SetBinContent(bin_id, main_hist->GetBinContent(bin_id));
494 }
495 }
496
497 current_canvas->Clear();
498 current_canvas->cd();
499 main_hist->Draw("hist");
500 overlay_hist->Draw("hist;same");
501 overlay_hist_green->Draw("hist;same");
502 current_canvas->Modified();
503 current_canvas->Update();
504 current_canvas->Draw();
505 }
506 }
507 }
508
509 //== Update EPICS PVs or MiraBelle monObjs
510
511 for (size_t alarm_idx = 0; alarm_idx < alarm_counts.size(); alarm_idx++) {
512 auto& alarm = m_ecl_alarms[alarm_idx];
513 std::map<std::string, int> total;
514 // Convert values per crate to totals
515 for (size_t crate = 0; crate < alarm_counts[alarm_idx].size(); crate++) {
516 int crate_id = crate + 1;
517 int value = alarm_counts[alarm_idx][crate];
518
519 if (!update_mirabelle) {
520 std::string pv_name = (boost::format("crate%02d:%s") % crate_id % alarm.name).str();
521 setEpicsPV(pv_name, value);
522 }
523
524 total["All"] += value;
525 if (crate_id <= ECL::ECL_BARREL_CRATES) {
526 total["Barrel"] += value;
527 } else if (crate_id <= ECL::ECL_BARREL_CRATES + ECL::ECL_FWD_CRATES) {
528 total["FWDEndcap"] += value;
529 } else {
530 total["BWDEndcap"] += value;
531 }
532 }
533 // Export totals
534 for (auto& ecl_part : {"All", "FWDEndcap", "Barrel", "BWDEndcap"}) {
535 std::string pv_name = (boost::format("%s:%s") % ecl_part % alarm.name).str();
536 if (update_mirabelle) {
537 std::string var_name = pv_name;
538 std::replace(var_name.begin(), var_name.end(), ':', '_');
539 B2DEBUG(100, var_name << " = " << total[ecl_part]);
540 m_monObj->setVariable(var_name, total[ecl_part]);
541 } else {
542 setEpicsPV(pv_name, total[ecl_part]);
543 }
544 }
545 }
546
547 return alarm_counts;
548}
549
551{
552 static std::vector< std::vector<short> > neighbours(ECL::ECL_TOTAL_CHANNELS);
553 if (neighbours[0].size() == 0) {
554 for (int cid0 = 0; cid0 < ECL::ECL_TOTAL_CHANNELS; cid0++) {
555 // [0]First is the crystal itself.
556 // [1]Second is PHI neighbour, phi+1.
557 // [2]Third is PHI neighbour, phi-1.
558 // [3]Next one (sometimes two) are THETA neighbours in theta-1.
559 // [4]Next one (sometimes two) are THETA neighbours in theta+1.
560 neighbours[cid0] = m_neighbours_obj.getNeighbours(cid0 + 1);
561 // Remove first element (the crystal itself)
562 neighbours[cid0].erase(neighbours[cid0].begin());
563 }
564 }
565
566 TH1* h_occupancy = findHist("ECL/cid_Thr5MeV", m_onlyIfUpdated);
567 const double max_deviation = m_maxDeviationForOccupancy;
568 return getSuspiciousChannels(h_occupancy, m_total_events, neighbours,
569 max_deviation, true);
570}
571
573{
575
576 static std::vector< std::vector<short> > neighbours(ECL::ECL_TOTAL_CHANNELS);
577 if (neighbours[0].size() == 0) {
578 for (int cid_center = 1; cid_center <= ECL::ECL_TOTAL_CHANNELS; cid_center++) {
579 geom->Mapping(cid_center - 1);
580 const int theta_id_center = geom->GetThetaID();
581 int phi_id_center = geom->GetPhiID();
582 const int crystals_in_ring = m_neighbours_obj.getCrystalsPerRing(theta_id_center);
583 phi_id_center = phi_id_center * 144 / crystals_in_ring;
584 for (int cid0 = 0; cid0 < 8736; cid0++) {
585 if (cid0 == cid_center - 1) continue;
586 geom->Mapping(cid0);
587 int theta_id = geom->GetThetaID();
588 int phi_id = geom->GetPhiID();
589 phi_id = phi_id * 144 / m_neighbours_obj.getCrystalsPerRing(theta_id);
590 if (std::abs(theta_id - theta_id_center) <= 2 &&
591 std::abs(phi_id - phi_id_center) <= 2) {
592 neighbours[cid_center - 1].push_back(cid0);
593 }
594 }
595 }
596 }
597
598 TH1* h_bad_chi2 = findHist("ECL/bad_quality", m_onlyIfUpdated);
599 const double max_deviation = m_maxDeviationForChi2;
600 return getSuspiciousChannels(h_bad_chi2, m_total_events, neighbours,
601 max_deviation, false);
602}
603
605 TH1* hist, double total_events,
606 const std::vector< std::vector<short> >& neighbours,
607 double max_deviation, bool occupancy_histogram)
608{
609 std::map<int, int> retval;
610
611 if (!hist) return retval;
612 //== A bit hacky solution to skip incorrectly
613 // filled histograms
614 if (hist->Integral() <= 0) return retval;
615
616 //=== Extract alarm details
617 // cppcheck-suppress unassignedVariable
618 const auto& [dead_index, dead_alarm] = getAlarmByName("dead");
619 // cppcheck-suppress unassignedVariable
620 const auto& [cold_index, cold_alarm] = getAlarmByName("cold");
621 // cppcheck-suppress unassignedVariable
622 const auto& [hot_index, hot_alarm ] = getAlarmByName("hot");
623 // cppcheck-suppress unassignedVariable
624 const auto& [chi2_index, chi2_alarm] = getAlarmByName("bad_chi2");
625
626 double min_required_events;
627
628 if (occupancy_histogram) {
629 min_required_events = std::min({
630 dead_alarm.required_statistics,
631 cold_alarm.required_statistics,
632 hot_alarm.required_statistics,
633 });
634 } else {
635 min_required_events = chi2_alarm.required_statistics;
636 }
637
638 if (total_events < min_required_events) return retval;
639
640 int dead_bit = 1 << dead_index;
641 int cold_bit = 1 << cold_index;
642 int hot_bit = 1 << hot_index;
643 int chi2_bit = 1 << chi2_index;
644
645 // == Search for dead channels
646
647 if (occupancy_histogram) {
648 // This indicates that DQMHistAnalysisECL module is not included in the path
649 bool not_normalized = (findCanvas("ECL/c_cid_Thr5MeV_analysis") == nullptr);
650 if (total_events >= dead_alarm.required_statistics) {
651 double min_occupancy;
652 const std::string run_type = getRunType();
653 if (run_type == "physics") {
654 // For physics runs, occupancy should be higher than 0.01%
655 min_occupancy = 1e-4;
656 } else {
657 // For every other run type, occupancy should be higher than 0.0001%
658 min_occupancy = 1e-6;
659 }
660 if (not_normalized) {
661 // The histogram is not normalized, multiply the threshold by evt count
662 min_occupancy *= total_events;
663 }
664 for (int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
665 if (hist->GetBinContent(cid) > min_occupancy) continue;
666 retval[cid] |= dead_bit;
667 }
668 }
669 if (total_events >= hot_alarm.required_statistics) {
670 // Number of hits with E > threshold should be less than 70%
671 double max_occupancy = 0.7;
672 if (not_normalized) {
673 // The histogram is not normalized, multiply the threshold by evt count
674 max_occupancy *= total_events;
675 }
676 for (int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
677 if (hist->GetBinContent(cid) < max_occupancy) continue;
678 retval[cid] |= hot_bit;
679 }
680 }
681 }
682
683 // == Search for cold and hot channels (or channels with bad chi2)
684
685 for (int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
686 double actual_value = hist->GetBinContent(cid);
687
688 std::vector<short> neighb = neighbours[cid - 1];
689 std::multiset<double> values_sorted;
690 for (auto& neighbour_cid : neighb) {
691 values_sorted.insert(hist->GetBinContent(neighbour_cid));
692 }
693
694 double expected_value;
695 if (occupancy_histogram) {
696 // Use "lower 25%" value:
697 expected_value = *std::next(values_sorted.begin(), 1 * neighb.size() / 4);
698 } else {
699 // Use "upper ~75%" value (48 is the expected number of neighbours)
700 expected_value = *std::next(values_sorted.begin(), 35 * neighb.size() / 48);
701 }
702 double deviation = std::abs((actual_value - expected_value) /
703 expected_value);
704
705 if (!occupancy_histogram) {
706 // Min occupancy for high-energy (> 1GeV) hits
707 double min_occupancy = 1.41e-5;
708 if (findCanvas("ECL/c_bad_quality_analysis") == nullptr) {
709 // The histogram is not normalized, multiply the threshold by evt count
710 min_occupancy *= total_events;
711 }
712
713 if (actual_value < min_occupancy) continue;
714 }
715
716 if (deviation < max_deviation) continue;
717
718 if (occupancy_histogram) {
719 if (actual_value < expected_value) retval[cid] |= cold_bit;
720 if (actual_value > 2 * expected_value) retval[cid] |= hot_bit;
721 } else {
722 if (actual_value > expected_value) retval[cid] |= chi2_bit;
723 }
724 }
725
726 return retval;
727}
728
730{
731 static std::map<std::string, std::vector<TLine*> > lines;
732 std::string name = hist->GetName();
733 if (lines[name].empty()) {
734 int x_min = hist->GetXaxis()->GetXmin();
735 int x_max = hist->GetXaxis()->GetXmax();
736 int y_min = hist->GetYaxis()->GetXmin();
737 int y_max = hist->GetYaxis()->GetXmax();
738 for (int x = x_min + 1; x < x_max; x++) {
739 auto l = new TLine(x, 0, x, 5);
740 l->SetLineStyle(kDashed);
741 lines[name].push_back(l);
742 }
743 for (int y = y_min + 1; y < y_max; y++) {
744 auto l = new TLine(1, y, ECL::ECL_CRATES + 1, y);
745 l->SetLineStyle(kDashed);
746 lines[name].push_back(l);
747 }
748 }
749 for (auto line : lines[name]) {
750 line->Draw();
751 }
752}
753
std::map< int, int > getSuspiciousChannels(TH1 *hist, double total_events, const std::vector< std::vector< short > > &neighbours, double max_deviation, bool occupancy_histogram)
Get outlier channels, ones with values (occupancy, errors, etc) that are much higher than the values ...
double m_total_events
Number of events with ECL data in the current run.
double m_maxDeviationForChi2
The higher this parameter, the larger differences in the number of hits with bad chi2 are allowed for...
TH1F * h_bad_occ_overlay
Overlay to indicate bad channels on occupancy histogram.
void initialize() override final
Initialize the module.
void updateAlarmConfig()
Set alarm limits in DQM based on EPICS PV limits.
std::vector< std::vector< int > > updateAlarmCounts(bool update_mirabelle=false)
Calculate and return number of problems per crate for all alarm categories.
std::vector< TText * > m_labels
Labels on the ECL alarms histogram.
ECL::ECLChannelMapper m_mapper
Object to map ECL crystal ID to ECL crate ID.
void drawGrid(TH2 *hist)
Draw grid with TLine primitives for the specified histogram.
TCanvas * c_channels_summary
TCanvas for ECL alarms regarding suspicious channels.
static const int c_max_masked_channels
Size of an array with masked channels.
std::pair< int, ECLAlarmType > getAlarmByName(std::string name)
Returns index of the specific alarm type and detailed information.
TH2F * h_channels_summary
Summarized information about ECL channels.
double m_maxDeviationForOccupancy
The higher this parameter, the larger differences in occupancy are allowed for adjacent channels.
ECL::ECLNeighbours m_neighbours_obj
Object to get ECL crystal neighbours.
TCanvas * c_occupancy
ECL occupancy histogram with highlighted suspicious channels.
TExec * m_ecl_style
Special object to specify style parameters.
std::string m_pvPrefix
Prefix to use for PVs registered by this module.
MonitoringObject * m_monObj
MiraBelle monitoring object.
void event() override final
Event processor.
TCanvas * c_bad_chi2
ECL bad chi2 histogram with highlighted suspicious channels.
TH1F * h_bad_chi2_overlay
Overlay to indicate bad channels on chi2 histogram.
void endRun() override final
Call when a run ends.
bool m_onlyIfUpdated
If true (default), update EPICS PVs only if there were changes in the histograms.
TH1F * h_bad_chi2_overlay_green
Overlay to indicate masked bad channels on chi2 histogram.
void beginRun() override final
Call when a run begins.
DQMHistAnalysisECLSummaryModule()
< derived from DQMHistAnalysisModule class.
TExec * m_default_style
Special object to revert changes done by ecl_style.
TH1F * h_bad_occ_overlay_green
Overlay to indicate masked bad channels on occupancy histogram.
std::map< std::string, std::set< int > > m_mask
List of masked Cell IDs for each alarm type.
bool getMaskedChannels(std::map< std::string, dbr_sts_long_array > &mask_info)
Get the array of masked channels for each type of alarm case monitored by this module.
bool m_useChannelMask
If true, mask Cell IDs based on DQM:ECL:channels_info:{alarm_type} PV info.
std::vector< ECLAlarmType > m_ecl_alarms
Different alarms monitored in h_channels_summary.
static TCanvas * findCanvas(TString cname)
Find canvas by name.
chid getEpicsPVChID(const std::string &keyname)
Get EPICS PV Channel Id.
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
static void colorizeCanvas(TCanvas *canvas, EStatus status)
Helper function for Canvas colorization.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
@ c_ColorTooFew
Not enough entries/event to judge.
static const std::string & getRunType(void)
Get the list of the reference histograms.
DQMHistAnalysisModule()
Constructor / Destructor.
static EStatus makeStatus(bool enough, bool warn_flag, bool error_flag)
Helper function to judge the status for coloring and EPICS.
bool requestLimitsFromEpicsPVs(chid id, double &lowerAlarm, double &lowerWarn, double &upperWarn, double &upperAlarm)
Get Alarm Limits from EPICS PV.
void setEpicsPV(const std::string &keyname, double value)
Write value to a EPICS PV.
The Class for ECL Geometry Parameters.
static ECLGeometryPar * Instance()
Static method to get a reference to the ECLGeometryPar instance.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.