Belle II Software development
DqmMasterCallback.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#include <daq/dqm/DqmMasterCallback.h>
9#include <daq/slc/system/LogFile.h>
10#include <framework/pcore/EvtMessage.h>
11#include <framework/pcore/MsgHandler.h>
12
13#include <TText.h>
14#include <TH1.h>
15#include <TKey.h>
16
17#include <unistd.h>
18
19using namespace Belle2;
20using namespace std;
21
22int DqmMasterCallback::m_running = 0; // TODO
23
24//-----------------------------------------------------------------
25// Rbuf-Read Thread Interface
26//-----------------------------------------------------------------
27void* RunDqmMasterLogger(void*)
28{
29 return nullptr;
30}
31
32DqmMasterCallback::DqmMasterCallback(ConfigFile& config) : m_histodir(config.get("dqmmaster.histodir")),
33 m_tmpdir(config.get("dqmmaster.tmpdir")), m_instance(config.get("dqmmaster.instance"))
34{
35 auto host = config.get("dqmmaster.host");
36 auto port = config.getInt("dqmmaster.port");
37 m_running = 0;
38 LogFile::info("DqmMasterCallback : instance = %s, histodir = %s, tmpdir = %s", m_instance.c_str(), m_histodir.c_str(),
39 m_tmpdir.c_str());
40
41 // Open sockets to hserver
42 m_sock = new EvtSocketSend(host.c_str(), port);
43}
44
45DqmMasterCallback::~DqmMasterCallback()
46{
47
48}
49
50void DqmMasterCallback::load(const DBObject& /* obj */, const std::string& runtype)
51{
52 m_runtype = runtype;
53 LogFile::info("LOAD: runtype %s", m_runtype.c_str());
54
55 {
56 std::string filename = m_tmpdir + "/dqm_" + m_instance + "_runtype";
57 // workaround until we have a better solution
58 auto fh = fopen(filename.c_str(), "wt+");
59 if (fh) {
60 fprintf(fh, "%s", m_runtype.c_str());
61 fclose(fh);
62 }
63 }
64}
65
66void DqmMasterCallback::start(int expno, int runno)
67{
68 m_expno = expno;
69 m_runno = runno;
70
71 // currently, we do not (yet) use exp and run nr, just add it in case it may be needed later
72 {
73 std::string filename = m_tmpdir + "/dqm_" + m_instance + "_expnr";
74 // workaround until we have a better solution
75 auto fh = fopen(filename.c_str(), "wt+");
76 if (fh) {
77 fprintf(fh, "%d", m_expno);
78 fclose(fh);
79 }
80 }
81 {
82 std::string filename = m_tmpdir + "/dqm_" + m_instance + "_runnr";
83 // workaround until we have a better solution
84 auto fh = fopen(filename.c_str(), "wt+");
85 if (fh) {
86 fprintf(fh, "%d", m_runno);
87 fclose(fh);
88 }
89 }
90
91 MsgHandler hdl(0);
92 int numobjs = 0;
93
94 TText rc_clear(0, 0, "DQMRC:CLEAR");
95 hdl.add(&rc_clear, "DQMRC:CLEAR");
96 numobjs++;
97 TText subdir(0, 0, "DQMInfo");
98 hdl.add(&subdir, "SUBDIR:DQMInfo") ;
99 numobjs++;
100 TH1F h_expno("expno", to_string(m_expno).c_str(), 1, 0, 1);
101 hdl.add(&h_expno, "expno");
102 numobjs++;
103 TH1F h_runno("runno", to_string(m_runno).c_str(), 1, 0, 1);
104 hdl.add(&h_runno, "runno");
105 numobjs++;
106 TH1F h_rtype("rtype", m_runtype.c_str(), 1, 0, 1);
107 hdl.add(&h_rtype, "rtype");
108 numobjs++;
109 TText command(0, 0, "COMMAND:EXIT");
110 hdl.add(&command, "SUBDIR:EXIT");
111 numobjs++;
112 TText rc_merge(0, 0, "DQMRC:MERGE");
113 hdl.add(&rc_merge, "DQMRC:MERGE");
114 numobjs++;
115
116 EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
117 (msg->header())->reserved[0] = 0;
118 (msg->header())->reserved[1] = numobjs;
119
120 while (m_sock->send(msg) < 0) {
121 LogFile::error("Connection to histogramm server is missing in START: expno = %d, runno = %d, runtype %s", m_expno, m_runno,
122 m_runtype.c_str());
123 m_sock->sock()->reconnect(10); // each one waits 5s
124 }
125 delete (msg);
126
127 LogFile::info("START: expno = %d, runno = %d, runtype %s", m_expno, m_runno, m_runtype.c_str());
128 m_running = 1;
129}
130
131void DqmMasterCallback::stop(void)
132{
133 LogFile::info("STOP: expno = %d, runno = %d, runtype %s", m_expno, m_runno, m_runtype.c_str());
134
135 if (m_running == 0) return;
136
137 m_running = 0;
138
139 char outfile[1024];
140
141 MsgHandler hdl(0);
142 int numobjs = 0;
143
144 snprintf(outfile, sizeof(outfile), "DQMRC:SAVE:%s/%s_dqm_histo_e%4.4dr%6.6d.root", m_histodir.c_str(), m_instance.c_str(), m_expno,
145 m_runno);
146
147 TText rc_save(0, 0, outfile);
148 hdl.add(&rc_save, outfile);
149 numobjs++;
150
151 EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
152 (msg->header())->reserved[0] = 0;
153 (msg->header())->reserved[1] = numobjs;
154
155 while (m_sock->send(msg) < 0) {
156 LogFile::error("Connection closed during STOP, file not saved: expno = %d, runno = %d, runtype %s", m_expno, m_runno,
157 m_runtype.c_str());
158 m_sock->sock()->reconnect(10); // each one waits 5s
159 // we assume that the connection was terminated by a restart of the server
160 // depending on when this happened, we may have new histograms to dump
161 // EVEN if the DQM analysis could not handle it (because after restart there is no
162 // run information.
163 }
164 delete (msg);
165}
166
167void DqmMasterCallback::abort(void)
168{
169 stop();
170}
171
172
173
174
175
EvtHeader * header()
Get pointer to EvtHeader.
Abstract base class for different kinds of events.
STL namespace.