Belle II Software  release-08-01-10
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 
19 using namespace Belle2;
20 using namespace std;
21 
22 int DqmMasterCallback::m_running = 0; // TODO
23 
24 //-----------------------------------------------------------------
25 // Rbuf-Read Thread Interface
26 //-----------------------------------------------------------------
27 void* RunDqmMasterLogger(void*)
28 {
29  return nullptr;
30 }
31 
32 DqmMasterCallback::DqmMasterCallback(ConfigFile& config)
33 {
34  m_histodir = config.get("dqmmaster.histodir");
35  m_instance = config.get("dqmmaster.instance");
36  auto host = config.get("dqmmaster.host");
37  auto port = config.getInt("dqmmaster.port");
38  m_running = 0;
39  LogFile::info("DqmMasterCallback : instance = %s, histodir = %s", m_instance.c_str(), m_histodir.c_str());
40 
41  // Open sockets to hserver
42  m_sock = new EvtSocketSend(host.c_str(), port);
43 }
44 
45 DqmMasterCallback::~DqmMasterCallback()
46 {
47 
48 }
49 
50 void 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 void DqmMasterCallback::start(int expno, int runno)
57 {
58  m_expno = expno;
59  m_runno = runno;
60 
61  MsgHandler hdl(0);
62  int numobjs = 0;
63 
64  TText rc_clear(0, 0, "DQMRC:CLEAR");
65  hdl.add(&rc_clear, "DQMRC:CLEAR");
66  numobjs++;
67  TText subdir(0, 0, "DQMInfo");
68  hdl.add(&subdir, "SUBDIR:DQMInfo") ;
69  numobjs++;
70  TH1F h_expno("expno", to_string(m_expno).c_str(), 1, 0, 1);
71  hdl.add(&h_expno, "expno");
72  numobjs++;
73  TH1F h_runno("runno", to_string(m_runno).c_str(), 1, 0, 1);
74  hdl.add(&h_runno, "runno");
75  numobjs++;
76  TH1F h_rtype("rtype", m_runtype.c_str(), 1, 0, 1);
77  hdl.add(&h_rtype, "rtype");
78  numobjs++;
79  TText command(0, 0, "COMMAND:EXIT");
80  hdl.add(&command, "SUBDIR:EXIT");
81  numobjs++;
82  TText rc_merge(0, 0, "DQMRC:MERGE");
83  hdl.add(&rc_merge, "DQMRC:MERGE");
84  numobjs++;
85 
86  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
87  (msg->header())->reserved[0] = 0;
88  (msg->header())->reserved[1] = numobjs;
89 
90  while (m_sock->send(msg) < 0) {
91  LogFile::error("Connection to histogramm server is missing in START: expno = %d, runno = %d, runtype %s", m_expno, m_runno,
92  m_runtype.c_str());
93  m_sock->sock()->reconnect(10); // each one waits 5s
94  }
95  delete (msg);
96 
97  LogFile::info("START: expno = %d, runno = %d, runtype %s", m_expno, m_runno, m_runtype.c_str());
98  m_running = 1;
99 }
100 
101 void DqmMasterCallback::stop(void)
102 {
103  LogFile::info("STOP: expno = %d, runno = %d, runtype %s", m_expno, m_runno, m_runtype.c_str());
104 
105  if (m_running == 0) return;
106 
107  m_running = 0;
108 
109  char outfile[1024];
110 
111  MsgHandler hdl(0);
112  int numobjs = 0;
113 
114  snprintf(outfile, sizeof(outfile), "DQMRC:SAVE:%s/%sdqm_e%4.4dr%6.6d.root", m_histodir.c_str(), m_instance.c_str(), m_expno,
115  m_runno);
116 
117  TText rc_save(0, 0, outfile);
118  hdl.add(&rc_save, outfile);
119  numobjs++;
120 
121  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
122  (msg->header())->reserved[0] = 0;
123  (msg->header())->reserved[1] = numobjs;
124 
125  while (m_sock->send(msg) < 0) {
126  LogFile::error("Connection closed during STOP, file not saved: expno = %d, runno = %d, runtype %s", m_expno, m_runno,
127  m_runtype.c_str());
128  m_sock->sock()->reconnect(10); // each one waits 5s
129  // we assume that the connection was terminated by a restart of the server
130  // depending on when this happened, we may have new histograms to dump
131  // EVEN if the DQM analysis could not handle it (because after restart there is no
132  // run information.
133  }
134  delete (msg);
135 }
136 
137 void DqmMasterCallback::abort(void)
138 {
139  stop();
140 }
141 
142 
143 
144 
145 
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.