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 Belle2;
23
24//-----------------------------------------------------------------
25// Register the Module
26//-----------------------------------------------------------------
27REG_MODULE(DQMHistAnalysisOutputRelayMsg);
28
29//-----------------------------------------------------------------
30// Implementation
31//-----------------------------------------------------------------
32
35{
36 //Parameter definition
37 addParam("Hostname", m_hostname, "Hostname of THTTP", std::string("localhost"));
38 addParam("Port", m_port, "Port number to THTTP", 9191);
39 addParam("CanvasSendDefault", m_canvasSendDefault, "Send untagged canvases", false);
40 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: Constructor done.");
41}
42
44{
45 if (m_sock != nullptr) delete m_sock;
46 m_sock = new TSocket(m_hostname.c_str(), m_port);
47 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: initialized.");
48}
49
51{
52 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: event called.");
53 TMessage mess(kMESS_OBJECT);
54
55 TSeqCollection* seq = gROOT->GetListOfCanvases();
56 TIter nextkey(seq);
57 TObject* obj = 0;
58
59 time_t now = time(0);
60 char mbstr[100];
61 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
62
63 auto& clist = getCanvasUpdatedList();
64 int sent_canvases = 0;
65
66 B2INFO("[" << mbstr << "] before sending " << seq->GetEntries() << " objects.");
67 bool first_try = true;
68 while ((obj = nextkey())) {
69 if (obj->IsA()->InheritsFrom("TCanvas")) {
70 auto* c = dynamic_cast<TCanvas*>(obj);
71 auto process_canvas = m_canvasSendDefault;
72
73 auto it = clist.find(c->GetName());
74 if (it != clist.end()) {
75 process_canvas = it->second;
76 }
77 if (!process_canvas) continue;
78 sent_canvases++;
79
80 mess.Reset();
81 mess.WriteObject(c); // write object in message buffer
82 if (m_sock->Send(mess) < 0) {
83 if (!first_try) {
84 break;//Only try to reconnect once per event
85 } else {
86 first_try = false;
87 }
88 //The plain TSocket can't reconnect, so delete and create a new one
89 delete m_sock;
90 m_sock = new TSocket(m_hostname.c_str(), m_port);
91 //Try to send the failed message again
92 if (m_sock->Send(mess) < 0) {
93 //If failed a second time stop for this event
94 break;
95 }
96 }
97 }
98 }
99 now = time(0);
100 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
101 B2INFO("[" << mbstr << "] after sending " << sent_canvases << " of " << seq->GetEntries() << " objects.");
102}
103
105{
106 B2DEBUG(20, "DQMHistAnalysisOutputRelayMsg: terminate called");
107 delete m_sock;
108}
109
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.
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.