10#include <dqm/analysis/modules/DQMHistAnalysisECLSummary.h>
13#include <ecl/geometry/ECLGeometryPar.h>
20#include <boost/format.hpp>
31 B2DEBUG(20,
"DQMHistAnalysisECLSummary: Constructor done.");
33 std::string(
"ECL:channels_info:"));
35 "Mask Cell IDs based on information from ECL PVs",
38 "The higher this parameter, the larger differences in occupancy are allowed for adjacent channels", 0.28);
40 "The higher this parameter, the larger differences in the number of hits with bad chi2 are allowed for adjacent channels",
42 addParam(
"onlyIfUpdated",
m_onlyIfUpdated,
"If true (default), update EPICS PVs only if histograms were updated.",
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 },
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();
72 for (
const auto& ecl_part : {
"All",
"FWDEndcap",
"Barrel",
"BWDEndcap"}) {
73 std::string pv_name = (boost::format(
"%s:%s") % ecl_part % alarm.name).str();
77 std::string mask_pv_name = (boost::format(
"mask:%s") % alarm.name).str();
85 TString title =
"#splitline{ECL errors monitoring}";
86 title +=
"{E - Error, W - Warning, L - Low statistics}";
87 title +=
";ECLCollector ID (same as Crate ID)";
89 ECL::ECL_CRATES, 1, ECL::ECL_CRATES + 1,
99 for (
int i = 1; i <= ECL::ECL_CRATES; i++) {
119 c_occupancy =
new TCanvas(
"ECL/c_cid_Thr5MeV_overlaid_analysis");
120 c_bad_chi2 =
new TCanvas(
"ECL/c_bad_quality_overlaid_analysis");
123 1, ECL::ECL_TOTAL_CHANNELS + 1);
130 1, ECL::ECL_TOTAL_CHANNELS + 1);
137 1, ECL::ECL_TOTAL_CHANNELS + 1);
144 1, ECL::ECL_TOTAL_CHANNELS + 1);
150 B2DEBUG(20,
"DQMHistAnalysisECLSummary: initialized.");
155 B2DEBUG(20,
"DQMHistAnalysisECLSummary: beginRun called.");
174 if (!h_total_events)
return;
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;
204 bool warn_flag =
false;
205 bool error_flag =
false;
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++) {
210 char label_text[2] = {0};
212 int warning_limit =
m_ecl_alarms[alarm_idx].warning_limit;
214 int alarms = alarm_counts[alarm_idx][crate];
220 color = HISTCOLOR_GRAY;
222 }
else if (alarms >= alarm_limit) {
223 color = HISTCOLOR_RED;
227 }
else if (alarms >= warning_limit) {
228 color = HISTCOLOR_ORANGE;
233 color = HISTCOLOR_GREEN;
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);
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);
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);
286 "gStyle->SetPalette(kRainBow);");
289 "gStyle->SetPalette(kBird);");
292 gStyle->SetTitleH(gstyle_title_h);
293 gStyle->SetTitleX(gstyle_title_x);
294 gStyle->SetTitleY(gstyle_title_y);
299 B2DEBUG(20,
"DQMHistAnalysisECLSummary: endRun called");
307 B2DEBUG(20,
"terminate called");
316 const std::string& name)
320 if (alarm_info.name == name)
return {index, alarm_info};
323 B2FATAL(
"Could not get ECL alarm " + name);
335 std::string pv_name = (boost::format(
"crate%02d:%s") % crate_id % alarm.name).str();
337 double upper_warning_limit = 0;
338 double upper_alarm_limit = 0;
341 if (!accessed || upper_alarm_limit <= 0 || upper_warning_limit <= 0) {
342 B2WARNING(
"Failed to get alarm limits");
346 alarm.alarm_limit = upper_alarm_limit;
347 alarm.warning_limit = upper_warning_limit;
353 static std::map<std::string, dbr_sts_long_array> mask_info;
355 B2WARNING(
"Failed to get arrays of masked channels");
361 m_mask[alarm.name].clear();
363 int cell_id = mask_info[alarm.name].value[i];
366 if (cell_id == 0)
break;
367 m_mask[alarm.name].insert(cell_id);
375 std::string mask_pv_name = (boost::format(
"mask:%s") % alarm.name).str();
378 if (mask_chid ==
nullptr)
return false;
382 if (r != ECA_NORMAL)
return false;
388 auto r = ca_pend_io(5.0);
389 if (r != ECA_NORMAL)
return false;
396 std::vector< std::vector<int> > alarm_counts(
397 m_ecl_alarms.size(), std::vector<int>(ECL::ECL_CRATES));
401 TH1* h_fail_crateid =
findHist(
"ECL",
"fail_crateid",
false);
404 for (
int crate_id = 1; crate_id <= ECL::ECL_CRATES; crate_id++) {
405 int errors_count = 0;
406 if (h_fail_crateid) {
407 errors_count = h_fail_crateid->GetBinContent(crate_id);
412 alarm_counts[fit_alarm_index][crate_id - 1] = errors_count;
416 std::map<int, int> error_bitmasks;
420 error_bitmasks[cell_id] |= error_bitmask;
424 error_bitmasks[cell_id] |= error_bitmask;
429 if (!update_mirabelle) {
436 static std::vector<std::string> indices = {
"dead",
"cold",
"hot",
"bad_chi2"};
437 for (
const auto& index_name : indices) {
439 int alarm_bit = 1 << alarm_index;
441 for (
const auto& [cid, error_bitmask] : error_bitmasks) {
442 if ((error_bitmask & alarm_bit) == 0)
continue;
444 bool masked = (
m_mask[index_name].find(cid) !=
m_mask[index_name].end());
447 int crate_id =
m_mapper.getCrateID(cid);
449 alarm_counts[alarm_index][crate_id - 1] += 1;
452 if (update_mirabelle)
continue;
457 if (index_name ==
"bad_chi2") {
460 }
else if (index_name ==
"dead" || index_name ==
"cold" || index_name ==
"hot") {
466 if (update_mirabelle)
continue;
469 if (index_name ==
"hot" || index_name ==
"bad_chi2") {
470 TCanvas* current_canvas;
473 TH1F* overlay_hist_green;
474 if (index_name ==
"hot") {
486 if (main_hist && main_hist->GetEntries() > 0) {
487 for (
const auto& overlay : {overlay_hist, overlay_hist_green}) {
488 for (
int bin_id = 1; bin_id <= ECL::ECL_TOTAL_CHANNELS; bin_id++) {
489 if (overlay->GetBinContent(bin_id) == 0)
continue;
491 if (main_hist->GetBinContent(bin_id) < 1e-6)
continue;
492 overlay->SetBinContent(bin_id, main_hist->GetBinContent(bin_id));
496 current_canvas->Clear();
497 current_canvas->cd();
498 main_hist->Draw(
"hist");
499 overlay_hist->Draw(
"hist;same");
500 overlay_hist_green->Draw(
"hist;same");
501 current_canvas->Modified();
502 current_canvas->Update();
503 current_canvas->Draw();
510 for (
size_t alarm_idx = 0; alarm_idx < alarm_counts.size(); alarm_idx++) {
512 std::map<std::string, int> total;
514 for (
size_t crate = 0; crate < alarm_counts[alarm_idx].size(); crate++) {
515 int crate_id = crate + 1;
516 int value = alarm_counts[alarm_idx][crate];
518 if (!update_mirabelle) {
519 std::string pv_name = (boost::format(
"crate%02d:%s") % crate_id % alarm.name).str();
523 total[
"All"] += value;
524 if (crate_id <= ECL::ECL_BARREL_CRATES) {
525 total[
"Barrel"] += value;
526 }
else if (crate_id <= ECL::ECL_BARREL_CRATES + ECL::ECL_FWD_CRATES) {
527 total[
"FWDEndcap"] += value;
529 total[
"BWDEndcap"] += value;
533 for (
const auto& ecl_part : {
"All",
"FWDEndcap",
"Barrel",
"BWDEndcap"}) {
534 std::string pv_name = (boost::format(
"%s:%s") % ecl_part % alarm.name).str();
535 if (update_mirabelle) {
536 std::string var_name = pv_name;
537 std::replace(var_name.begin(), var_name.end(),
':',
'_');
538 B2DEBUG(100, var_name <<
" = " << total[ecl_part]);
539 m_monObj->setVariable(var_name, total[ecl_part]);
551 static std::vector< std::vector<short> > neighbours(ECL::ECL_TOTAL_CHANNELS);
552 if (neighbours[0].size() == 0) {
553 for (
int cid0 = 0; cid0 < ECL::ECL_TOTAL_CHANNELS; cid0++) {
561 neighbours[cid0].erase(neighbours[cid0].begin());
568 max_deviation,
true);
575 static std::vector< std::vector<short> > neighbours(ECL::ECL_TOTAL_CHANNELS);
576 if (neighbours[0].size() == 0) {
577 for (
int cid_center = 1; cid_center <= ECL::ECL_TOTAL_CHANNELS; cid_center++) {
578 geom->Mapping(cid_center - 1);
579 const int theta_id_center = geom->GetThetaID();
580 int phi_id_center = geom->GetPhiID();
581 const int crystals_in_ring =
m_neighbours_obj.getCrystalsPerRing(theta_id_center);
582 phi_id_center = phi_id_center * 144 / crystals_in_ring;
583 for (
int cid0 = 0; cid0 < 8736; cid0++) {
584 if (cid0 == cid_center - 1)
continue;
586 int theta_id = geom->GetThetaID();
587 int phi_id = geom->GetPhiID();
589 if (std::abs(theta_id - theta_id_center) <= 2 &&
590 std::abs(phi_id - phi_id_center) <= 2) {
591 neighbours[cid_center - 1].push_back(cid0);
600 max_deviation,
false);
604 TH1* hist,
double total_events,
605 const std::vector< std::vector<short> >& neighbours,
606 double max_deviation,
bool occupancy_histogram)
608 std::map<int, int> retval;
610 if (!hist)
return retval;
613 if (hist->Integral() <= 0)
return retval;
619 const auto& [chi2_index, chi2_alarm] =
getAlarmByName(
"bad_chi2");
621 double min_required_events;
623 if (occupancy_histogram) {
624 min_required_events = std::min({
625 dead_alarm.required_statistics,
626 cold_alarm.required_statistics,
627 hot_alarm.required_statistics,
630 min_required_events = chi2_alarm.required_statistics;
633 if (total_events < min_required_events)
return retval;
635 int cold_bit = 1 << cold_index;
636 int hot_bit = 1 << hot_index;
637 int chi2_bit = 1 << chi2_index;
641 if (occupancy_histogram) {
643 bool not_normalized = (
findCanvas(
"ECL/c_cid_Thr5MeV_analysis") ==
nullptr);
644 if (total_events >= dead_alarm.required_statistics) {
645 double min_occupancy;
647 const int dead_bit = 1 << dead_index;
648 if (run_type ==
"physics") {
650 min_occupancy = 1e-4;
653 min_occupancy = 1e-6;
655 if (not_normalized) {
657 min_occupancy *= total_events;
659 for (
int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
660 if (hist->GetBinContent(cid) > min_occupancy)
continue;
661 retval[cid] |= dead_bit;
664 if (total_events >= hot_alarm.required_statistics) {
666 double max_occupancy = 0.7;
667 if (not_normalized) {
669 max_occupancy *= total_events;
671 for (
int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
672 if (hist->GetBinContent(cid) < max_occupancy)
continue;
673 retval[cid] |= hot_bit;
680 for (
int cid = 1; cid <= ECL::ECL_TOTAL_CHANNELS; cid++) {
681 double actual_value = hist->GetBinContent(cid);
683 std::vector<short> neighb = neighbours[cid - 1];
684 std::multiset<double> values_sorted;
685 for (
const auto& neighbour_cid : neighb) {
686 values_sorted.insert(hist->GetBinContent(neighbour_cid));
689 double expected_value;
690 if (occupancy_histogram) {
692 expected_value = *std::next(values_sorted.begin(), 1 * neighb.size() / 4);
695 expected_value = *std::next(values_sorted.begin(), 35 * neighb.size() / 48);
697 double deviation = std::abs((actual_value - expected_value) /
700 if (!occupancy_histogram) {
702 double min_occupancy = 1.41e-5;
703 if (
findCanvas(
"ECL/c_bad_quality_analysis") ==
nullptr) {
705 min_occupancy *= total_events;
708 if (actual_value < min_occupancy)
continue;
711 if (deviation < max_deviation)
continue;
713 if (occupancy_histogram) {
714 if (actual_value < expected_value) retval[cid] |= cold_bit;
715 if (actual_value > 2 * expected_value) retval[cid] |= hot_bit;
717 if (actual_value > expected_value) retval[cid] |= chi2_bit;
726 static std::map<std::string, std::vector<TLine*> > lines;
727 std::string name = hist->GetName();
728 if (lines[name].empty()) {
729 int x_min = hist->GetXaxis()->GetXmin();
730 int x_max = hist->GetXaxis()->GetXmax();
731 int y_min = hist->GetYaxis()->GetXmin();
732 int y_max = hist->GetYaxis()->GetXmax();
733 for (
int x = x_min + 1; x < x_max; x++) {
734 auto l =
new TLine(x, 0, x, 5);
735 l->SetLineStyle(kDashed);
736 lines[name].push_back(l);
738 for (
int y = y_min + 1; y < y_max; y++) {
739 auto l =
new TLine(1, y, ECL::ECL_CRATES + 1, y);
740 l->SetLineStyle(kDashed);
741 lines[name].push_back(l);
744 for (
auto line : lines[name]) {
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.
static void drawGrid(TH2 *hist)
Draw grid with TLine primitives for the specified histogram.
TCanvas * c_channels_summary
TCanvas for ECL alarms regarding suspicious channels.
std::map< int, int > getChannelsWithChi2Problems()
static const int c_max_masked_channels
Size of an array with masked channels.
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 terminate() override final
Terminate.
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.
std::map< int, int > getChannelsWithOccupancyProblems()
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::pair< int, ECLAlarmType > getAlarmByName(const std::string &name)
Returns index of the specific alarm type and detailed information.
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.
@ 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.
static TH1 * findHist(const std::string &dirname, const std::string &histname="", bool onlyIfUpdated=false)
Find histogram.
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 ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.