Belle II Software development
DQMHistOutputToEPICS.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// File : DQMHistOutputToEPICS.cc
10// Description : Write Histogram Content to EPICS Arrays
11//-
12
13
14#include <dqm/analysis/modules/DQMHistOutputToEPICS.h>
15#include <framework/core/ModuleParam.templateDetails.h>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23REG_MODULE(DQMHistOutputToEPICS);
24
25//-----------------------------------------------------------------
26// Implementation
27//-----------------------------------------------------------------
28
31{
32 // This module CAN NOT be run in parallel!
33
34 //Parameter definition
35 addParam("HistoList", m_histlist, "histname, pvname");
36 B2DEBUG(99, "DQMHistOutputToEPICS: Constructor done.");
37}
38
40{
41 for (auto& it : m_histlist) {
42 if (it.size() < 2) {
43 B2WARNING("Histolist with wrong nr of parameters " << it.size());
44 continue;
45 }
46#ifdef _BELLE2_EPICS
47 auto n = new MYNODE;
48 n->histoname = it.at(0);
49 SEVCHK(ca_create_channel(it.at(1).c_str(), NULL, NULL, 10, &n->mychid), "ca_create_channel failure");
50 if (it.size() >= 3) {
51 SEVCHK(ca_create_channel(it.at(2).c_str(), NULL, NULL, 10, &n->mychid_last), "ca_create_channel failure");
52 } else {
53 n->mychid_last = 0;
54 }
55 pmynode.push_back(n);
56#endif
57 }
58
59 B2DEBUG(99, "DQMHistOutputToEPICS: initialized.");
60}
61
63{
64#ifdef _BELLE2_EPICS
65 for (auto* it : pmynode) {
66 if (!it->mychid) continue;
67 int length = int(ca_element_count(it->mychid));
68 if (length > 0) {
69 it->data.resize(length, 0.0);
70 SEVCHK(ca_array_put(DBR_DOUBLE, length, it->mychid, (void*)(it->data.data())), "ca_put failure");
71 if (it->mychid_last) {
72 if (length == int(ca_element_count(it->mychid_last))) {
73 SEVCHK(ca_array_put(DBR_DOUBLE, length, it->mychid_last, (void*)(it->data.data())), "ca_put failure");
74 }
75 }
76 }
77 }
78#endif
79}
80
82{
83 B2DEBUG(99, "DQMHistOutputToEPICS: beginRun called.");
84 cleanPVs();
85 m_dirty = true;
86}
87
89{
90#ifdef _BELLE2_EPICS
91 for (auto& it : pmynode) {
92 if (!it->mychid) continue;
93 int length = it->data.size();
94 TH1* hh1 = findHist(it->histoname);
95 if (hh1 && hh1->GetNcells() > 2 && length > 0 && length == int(ca_element_count(it->mychid))) {
96 // If bin count doesn't match, we loose bins but otherwise ca_array_put will complain
97 // We fill up the array with ZEROs otherwise
98 if (hh1->GetDimension() == 1) {
99 int i = 0;
100 int nx = hh1->GetNbinsX() + 1;
101 for (int x = 1; x < nx && i < length ; x++) {
102 it->data[i++] = hh1->GetBinContent(x);
103 }
104
105 } else if (hh1->GetDimension() == 2) {
106 int i = 0;
107 int nx = hh1->GetNbinsX() + 1;
108 int ny = hh1->GetNbinsY() + 1;
109 for (int y = 1; y < ny && i < length; y++) {
110 for (int x = 1; x < nx && i < length ; x++) {
111 it->data[i++] = hh1->GetBinContent(x, y);
112 }
113 }
114 }
115
116 SEVCHK(ca_array_put(DBR_DOUBLE, length, it->mychid, (void*)it->data.data()), "ca_set failure");
117 }
118 }
119#endif
120}
121
123{
124 if (!m_dirty) return;
125#ifdef _BELLE2_EPICS
126 for (auto* it : pmynode) {
127 if (it->mychid && it->mychid_last) {
128 // copy PVs to last-run-PV if existing
129 int length = it->data.size();
130 if (length > 0 && length == int(ca_element_count(it->mychid_last))) {
131 SEVCHK(ca_array_put(DBR_DOUBLE, length, it->mychid_last, (void*)it->data.data()), "ca_put failure");
132 }
133 }
134 }
135 SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
136#endif
137
138 m_dirty = false;
139}
140
142{
143 B2DEBUG(99, "DQMHistOutputToEPICS: endRun called");
144 copyToLast();
145}
146
148{
149 B2DEBUG(99, "DQMHistOutputToEPICS: terminate called");
150 copyToLast();
151 // the following belongs to terminate
152#ifdef _BELLE2_EPICS
153 for (auto* it : pmynode) {
154 if (it->mychid) SEVCHK(ca_clear_channel(it->mychid), "ca_clear_channel failure");
155 if (it->mychid_last) SEVCHK(ca_clear_channel(it->mychid_last), "ca_clear_channel failure");
156 }
157#endif
158}
159
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
DQMHistAnalysisModule()
Constructor / Destructor.
void terminate(void) override final
This method is called at the end of the event processing.
void initialize(void) override final
Initializer.
void endRun(void) override final
This method is called for each event.
void cleanPVs(void)
set PVs to zero content (at run start)
std::vector< std::vector< std::string > > m_histlist
Parameter list for histograms.
void copyToLast(void)
copy over to "last" PV
bool m_dirty
Flag to mark that a new runs as started and data not copied to last PV.
void beginRun(void) override final
Called when entering a new run.
void event(void) override final
This method is called if the current run ends.
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.
STL namespace.