Belle II Software development
DQMHistAnalysisOutputRelayMsg.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 : DQMHistAnalysisOutputRelayMsg.cc
10// Description : DQM Output, send Canvases to jsroot server.
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisOutputRelayMsg.h>
15#include <TROOT.h>
16#include <TClass.h>
17#include <TObject.h>
18#include <TCanvas.h>
19#include <TMessage.h>
20#include <ctime>
21
22using namespace std;
23using namespace Belle2;
24
25//-----------------------------------------------------------------
26// Register the Module
27//-----------------------------------------------------------------
28REG_MODULE(DQMHistAnalysisOutputRelayMsg);
29
30//-----------------------------------------------------------------
31// Implementation
32//-----------------------------------------------------------------
33
36{
37 //Parameter definition
38 addParam("Hostname", m_hostname, "Hostname of THTTP", std::string("localhost"));
39 addParam("Port", m_port, "Port number to THTTP", 9191);
40 addParam("CanvasSendDefault", m_canvasSendDefault, "Send untagged canvases", false);
41 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: Constructor done.");
42}
43
45{
46 if (m_sock != nullptr) delete m_sock;
47 m_sock = new TSocket(m_hostname.c_str(), m_port);
48 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: initialized.");
49}
50
51
53{
54 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: beginRun called.");
55}
56
57
59{
60 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: event called.");
61 TMessage mess(kMESS_OBJECT);
62
63 TSeqCollection* seq = gROOT->GetListOfCanvases();
64 TIter nextkey(seq);
65 TObject* obj = 0;
66
67 time_t now = time(0);
68 char mbstr[100];
69 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
70
71 auto& clist = getCanvasUpdatedList();
72 int sent_canvases = 0;
73
74 B2INFO("[" << mbstr << "] before sending " << seq->GetEntries() << " objects.");
75 bool first_try = true;
76 while ((obj = (TObject*)nextkey())) {
77 if (obj->IsA()->InheritsFrom("TCanvas")) {
78 TCanvas* c = (TCanvas*) obj;
79 auto process_canvas = m_canvasSendDefault;
80
81 auto it = clist.find(c->GetName());
82 if (it != clist.end()) {
83 process_canvas = it->second;
84 }
85 if (!process_canvas) continue;
86 sent_canvases++;
87
88 mess.Reset();
89 mess.WriteObject(c); // write object in message buffer
90 if (m_sock->Send(mess) < 0) {
91 if (!first_try) {
92 break;//Only try to reconnect once per event
93 } else {
94 first_try = false;
95 }
96 //The plain TSocket can't reconnect, so delete and create a new one
97 delete m_sock;
98 m_sock = new TSocket(m_hostname.c_str(), m_port);
99 //Try to send the failed message again
100 if (m_sock->Send(mess) < 0) {
101 //If failed a second time stop for this event
102 break;
103 }
104 }
105 }
106 }
107 now = time(0);
108 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
109 B2INFO("[" << mbstr << "] after sending " << sent_canvases << " of " << seq->GetEntries() << " objects.");
110}
111
113{
114 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: endRun called");
115}
116
117
119{
120 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: terminate called");
121 delete m_sock;
122}
123
static const CanvasUpdatedList & getCanvasUpdatedList()
Get the list of the canvas update status.
DQMHistAnalysisModule()
Constructor / Destructor.
TSocket * m_sock
The socket to the canvas server.
void terminate() override final
This method is called at the end of the event processing.
void event() override final
This method is called for each event.
bool m_canvasSendDefault
Send untagged canvas by default.
void endRun() override final
This method is called if the current run ends.
void beginRun() override final
Called when entering a new run.
std::string m_hostname
The hostname of the canvas server.
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.