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