Belle II Software  release-06-02-00
DQMHistAnalysisECLShapers.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/DQMHistAnalysisECLShapers.h>
11 
12 //ROOT
13 #include <TProfile.h>
14 
15 using namespace Belle2;
16 
17 REG_MODULE(DQMHistAnalysisECLShapers)
18 
21 {
22  B2DEBUG(20, "DQMHistAnalysisECLShapers: Constructor done.");
23 
24  addParam("useEpics", m_useEpics, "Whether to update EPICS PVs.", false);
25 }
26 
27 
29 {
30 #ifdef _BELLE2_EPICS
31  if (m_useEpics) {
32  if (ca_current_context()) ca_context_destroy();
33  }
34 #endif
35 }
36 
38 {
39  B2DEBUG(20, "DQMHistAnalysisECLShapers: initialized.");
40 
41 #ifdef _BELLE2_EPICS
42  if (m_useEpics) {
43 
44  if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback), "ca_context_create");
45  for (int i = 0; i < c_collector_count; i++) {
46  std::string pv_name = "ECL:logic_check:crate" + std::to_string(i + 1);
47  SEVCHK(ca_create_channel(pv_name.c_str(), NULL, NULL, 10, &chid_logic[i]), "ca_create_channel failure");
48  }
49  SEVCHK(ca_create_channel("ECL:pedwidth:test:max_fw", NULL, NULL, 10,
50  &chid_pedwidth[0]), "ca_create_channel failure");
51  SEVCHK(ca_create_channel("ECL:pedwidth:test:max_br", NULL, NULL, 10,
52  &chid_pedwidth[1]), "ca_create_channel failure");
53  SEVCHK(ca_create_channel("ECL:pedwidth:test:max_bw", NULL, NULL, 10,
54  &chid_pedwidth[2]), "ca_create_channel failure");
55  SEVCHK(ca_create_channel("ECL:pedwidth:test:max_al", NULL, NULL, 10,
56  &chid_pedwidth[3]), "ca_create_channel failure");
57  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
58  }
59 #endif
60 }
61 
63 {
64  B2DEBUG(20, "DQMHistAnalysisECLShapers: beginRun called.");
65 }
66 
68 {
69  TH1* h_fail_crateid = findHist("ECL/fail_crateid");
70  TProfile* h_pedrms_cellid = (TProfile*)findHist("ECL/pedrms_cellid");
71 
72 #ifdef _BELLE2_EPICS
73  if (m_useEpics) {
74  if (h_fail_crateid != NULL) {
75  for (int i = 0; i < c_collector_count; i++) {
76  int errors_count = h_fail_crateid->GetBinContent(i + 1);
77  if (chid_logic[i]) SEVCHK(ca_put(DBR_LONG, chid_logic[i], (void*)&errors_count), "ca_set failure");
78  }
79  }
80  if (h_pedrms_cellid != NULL) {
81  // Using multiset to automatically sort the added values.
82  std::multiset<double> barrel_pedwidth;
83  std::multiset<double> bwd_pedwidth;
84  std::multiset<double> fwd_pedwidth;
85 
86  for (int i = 0; i < 8736; i++) {
87  const int cellid = i + 1;
88  if (h_pedrms_cellid->GetBinEntries(cellid) < 100) continue;
89  double pedrms = h_pedrms_cellid->GetBinContent(cellid);
90  if (cellid < 1153) {
91  fwd_pedwidth.insert(pedrms);
92  } else if (cellid < 7777) {
93  barrel_pedwidth.insert(pedrms);
94  } else {
95  bwd_pedwidth.insert(pedrms);
96  }
97  }
98 
99  double pedwidth_max[4] = {
100  robust_max(barrel_pedwidth),
101  robust_max(bwd_pedwidth),
102  robust_max(fwd_pedwidth),
103  0
104  };
105  pedwidth_max[3] = *std::max(&pedwidth_max[0], &pedwidth_max[2]);
106  for (int i = 0; i < 4; i++) {
107  if (pedwidth_max[i] <= 0) continue;
108  if (!chid_pedwidth[i]) continue;
109  SEVCHK(ca_put(DBR_DOUBLE, chid_pedwidth[i], (void*)&pedwidth_max[i]), "ca_set failure");
110  }
111  }
112  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
113  }
114 #endif
115 }
116 
118 {
119  B2DEBUG(20, "DQMHistAnalysisECLShapers: endRun called");
120 }
121 
122 
124 {
125  B2DEBUG(20, "terminate called");
126 
127 #ifdef _BELLE2_EPICS
128  if (m_useEpics) {
129  for (int i = 0; i < c_collector_count; i++) {
130  if (chid_logic[i]) SEVCHK(ca_clear_channel(chid_logic[i]), "ca_clear_channel failure");
131  }
132  for (int i = 0; i < 4; i++) {
133  if (chid_pedwidth[i]) SEVCHK(ca_clear_channel(chid_pedwidth[i]), "ca_clear_channel failure");
134  }
135  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
136  }
137 #endif
138 }
139 
140 double DQMHistAnalysisECLShapersModule::robust_max(std::multiset<double> values)
141 {
142  int len = values.size();
143  if (len < 10) {
144  return 0;
145  }
146  // Move end iterator back by 10% to remove
147  // noisy values.
148  auto end_iter = std::prev(values.end(), len * 0.1);
149 
150  return *end_iter;
151 }
152 
This module is for analysis of ECL DQM histograms.
virtual void initialize() override
Initialize the module.
virtual void event() override
Event processor.
virtual void endRun() override
Call when a run ends.
virtual void terminate() override
Terminate.
double robust_max(std::multiset< double > values)
Remove upper 10% of the values, return the maximum in the remaining 90%.
virtual void beginRun() override
Call when a run begins.
static const int c_collector_count
Number of ECLCollector modules (normally 52)
The base class for the histogram analysis module.
static TH1 * findHist(const std::string &histname)
Find histogram.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.