Belle II Software development
HistoRelay2.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#include "daq/dqm/HistoRelay2.h"
10#include <framework/pcore/EvtMessage.h>
11#include <TFile.h>
12#include <TH1.h>
13#include <TText.h>
14#include <TKey.h>
15
16using namespace Belle2;
17using namespace std;
18
19HistoRelay2::HistoRelay2(string& filename, string& dest, int port) : m_filename(filename), m_dest(dest)
20{
21 m_port = port;
22 m_sock = new EvtSocketSend(m_dest, m_port);
23}
24
25HistoRelay2::~HistoRelay2()
26{
27 delete m_sock;
28}
29
30int HistoRelay2::collect()
31{
32 // printf ( "HistoRelay2 : collect!!\n" );
33 EvtMessage* msg = StreamFile(m_filename);
34
35 if (!msg) return 1; // error indicator
36
37 auto ret = m_sock->send(msg);
38
39 delete (msg);
40
41 if (ret < 0) {
42 // Socket error, e.g server died
43 delete m_sock;
44 // try reconnect
45 printf("HistoRelay2: socket seems dead -> reconnect\n");
46 m_sock = new EvtSocketSend(m_dest, m_port);
47 }
48
49 return 0;
50}
51
52// Copy Shared Memory to local and stream
53EvtMessage* HistoRelay2::StreamFile(string& filename)
54{
55 TFile* file = new TFile((std::string("/dev/shm/") + filename).c_str(), "read");
56 if (file == NULL || file->IsZombie()) return NULL;
57 file->cd();
58 MsgHandler hdl(0);
59 int numobjs = 0;
60 StreamHistograms(gDirectory, &hdl, numobjs);
61 file->Close();
62 delete file;
63 // printf ( "DqmMemFile::StreamMemFile : streamed %d histograms in EvtMessage\n", numobjs );
64 EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
65 (msg->header())->reserved[0] = 0;
66 (msg->header())->reserved[1] = numobjs;
67 return msg;
68}
69
70int HistoRelay2::StreamHistograms(TDirectory* curdir, MsgHandler* msg, int& numobjs)
71{
72 TList* keylist = curdir->GetListOfKeys();
73
74 TIter nextkey(keylist);
75 TKey* key = 0;
76 while ((key = static_cast<TKey*>(nextkey()))) {
77 TObject* obj = curdir->FindObjectAny(key->GetName());
78 if (obj->IsA()->InheritsFrom("TH1")) {
79 TH1* h1 = static_cast<TH1*>(obj);
80 // printf ( "Key = %s, entry = %f\n", key->GetName(), h1->GetEntries() );
81 msg->add(h1, h1->GetName());
82 numobjs++;
83 } else if (obj->IsA()->InheritsFrom(TDirectory::Class())) {
84 // printf ( "New directory found %s, Go into subdir\n", obj->GetName() );
85 TDirectory* tdir = static_cast<TDirectory*>(obj);
86 // m_msg->add(tdir, tdir->GetName());
87 TText subdir(0, 0, tdir->GetName());
88 msg->add(&subdir, "SUBDIR:" + string(obj->GetName())) ;
89 numobjs++;
90 tdir->cd();
91 StreamHistograms(tdir, msg, numobjs);
92 TText command(0, 0, "COMMAND:EXIT");
93 msg->add(&command, "SUBDIR:EXIT");
94 numobjs++;
95 curdir->cd();
96 }
97 }
98 return 0;
99}
Class to manage streamed object.
Definition EvtMessage.h:59
EvtHeader * header()
Get pointer to EvtHeader.
A class to encode/decode an EvtMessage.
Definition MsgHandler.h:103
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition MsgHandler.cc:46
Abstract base class for different kinds of events.
STL namespace.