10 #include <framework/core/ModuleParam.templateDetails.h>
11 #include <dqm/analysis/modules/DQMHistAnalysisInputPVSrv.h>
13 #include <TDirectory.h>
30 static void printChidInfo(chid ichid,
const char* message)
33 B2DEBUG(20,
"pv: " << ca_name(ichid) <<
" type(" << ca_field_type(ichid) <<
") nelements(" << ca_element_count(
34 ichid) <<
") host(" << ca_host_name(ichid)
35 <<
") read(" << ca_read_access(ichid) <<
") write(" << ca_write_access(ichid) <<
") state(" << ca_state(ichid) <<
")");
38 static void exceptionCallback(
struct exception_handler_args args)
40 chid ichid = args.chid;
41 long stat = args.stat;
43 const char* noname =
"unknown";
45 channel = (ichid ? ca_name(ichid) : noname);
48 if (ichid) printChidInfo(ichid,
"exceptionCallback");
49 printf(
"exceptionCallback stat %s channel %s\n", ca_message(stat), channel);
52 static void connectionCallback(
struct connection_handler_args args)
54 chid ichid = args.chid;
56 printChidInfo(ichid,
"connectionCallback");
59 static void accessRightsCallback(
struct access_rights_handler_args args)
61 chid ichid = args.chid;
63 printChidInfo(ichid,
"accessRightsCallback");
65 static void eventCallback(
struct event_handler_args eha)
67 chid ichid = eha.chid;
68 MYNODE* n = (MYNODE*)eha.usr;
70 if (eha.status != ECA_NORMAL) {
71 printChidInfo(ichid,
"eventCallback");
82 DQMHistAnalysisInputPVSrvModule::DQMHistAnalysisInputPVSrvModule()
87 addParam(
"HistoList",
m_histlist,
"pvname, histname, histtitle, (bins,min,max[,bins,min,max])");
90 B2DEBUG(20,
"DQMHistAnalysisInputPVSrv: Constructor done.");
94 DQMHistAnalysisInputPVSrvModule::~DQMHistAnalysisInputPVSrvModule()
97 if (ca_current_context()) ca_context_destroy();
107 if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback),
"ca_context_create");
108 SEVCHK(ca_add_exception_event(exceptionCallback, NULL),
"ca_add_exception_event");
110 if (it.size() != 4 && it.size() != 5) {
111 B2WARNING(
"Histolist with wrong nr of parameters " << it.size());
114 auto n = (MYNODE*) callocMustSucceed(1,
sizeof(MYNODE),
"caMonitor");
115 pmynode.push_back(n);
119 TDirectory* oldDir = gDirectory;
120 TDirectory* d = oldDir;
121 TString myl = it.at(1).c_str();
124 while (myl.Tokenize(tok, from,
"/")) {
128 if (myl.Tokenize(dummy, f,
"/")) {
130 e = d->GetDirectory(tok);
132 B2DEBUG(20,
"Cd Dir " << tok);
135 B2DEBUG(20,
"Create Dir " << tok);
144 B2DEBUG(20,
"Create Histo " << tok);
148 strncpy(n->name, it.at(0).c_str(), MAX_PV_NAME_LEN - 1);
149 istringstream is(it.at(3));
153 if (it.size() == 4) {
154 n->histo = (TH1*)
new TH1F(tok, it.at(2).c_str(), x, xmin, xmax);
161 istringstream iss(it.at(4));
165 n->histo = (TH1*)
new TH2F(tok, it.at(2).c_str(), x, xmin, xmax, y, ymin, ymax);
177 for (
auto n : pmynode) {
178 SEVCHK(ca_create_channel(n->name, connectionCallback, n, 20, &n->mychid),
"ca_create_channel");
179 SEVCHK(ca_replace_access_rights_event(n->mychid, accessRightsCallback),
"ca_replace_access_rights_event");
181 SEVCHK(ca_create_subscription(DBR_STRING, 1, n->mychid, DBE_VALUE, eventCallback, n, &n->myevid),
"ca_create_subscription");
186 B2DEBUG(20,
"DQMHistAnalysisInputPVSrv: initialized.");
192 B2DEBUG(20,
"DQMHistAnalysisInputPVSrv: beginRun called.");
206 SEVCHK(ca_pend_event(0.0001),
"ca_pend_event");
208 for (
auto n : pmynode) {
212 auto bufferorg =
new char[dbr_size_n(ca_field_type(n->mychid), ca_element_count(n->mychid))];
213 void* buffer = (
void*) bufferorg;
216 if (ca_field_type(n->mychid) != DBF_LONG && ca_field_type(n->mychid) != DBF_FLOAT)
continue;
217 status = ca_array_get(ca_field_type(n->mychid), ca_element_count(n->mychid), n->mychid, buffer);
218 SEVCHK(status,
"ca_array_get()");
219 status = ca_pend_io(15.0);
220 if (status != ECA_NORMAL) {
221 B2WARNING(
"EPICS ca_array_get " << ca_name(n->mychid) <<
" didn't return a value.");
230 bins = ca_element_count(n->mychid) < n->binmax ? ca_element_count(n->mychid) : n->binmax;
231 TH1* histo = n->histo;
232 for (
unsigned int j = ca_element_count(n->mychid); j < n->binmax; j++) histo->SetBinContent(j + 1, 0);
233 switch (ca_field_type(n->mychid)) {
235 dbr_char_t* b = (dbr_char_t*)buffer;
236 for (
unsigned int j = 0; j < bins; j++) {
237 histo->SetBinContent(j + 1, b[j]);
242 dbr_short_t* b = (dbr_short_t*)buffer;
243 for (
unsigned int j = 0; j < bins; j++) {
244 histo->SetBinContent(j + 1, b[j]);
248 dbr_long_t* b = (dbr_long_t*)buffer;
249 for (
unsigned int j = 0; j < bins; j++) {
250 histo->SetBinContent(j + 1, b[j]);
254 dbr_float_t* b = (dbr_float_t*)buffer;
255 for (
unsigned int j = 0; j < bins; j++) {
256 histo->SetBinContent(j + 1, b[j]);
260 dbr_double_t* b = (dbr_double_t*)buffer;
261 for (
unsigned int j = 0; j < bins; j++) {
262 histo->SetBinContent(j + 1, b[j]);
276 }
while (!t.CheckTimer(gSystem->Now()));
282 B2DEBUG(20,
"DQMHistAnalysisInputPVSrv: endRun called");
288 B2DEBUG(20,
"DQMHistAnalysisInputPVSrv: terminate called");