Belle II Software  release-08-01-10
hsendfile.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/dataflow/EvtSocket.h>
9 #include <framework/pcore/EvtMessage.h>
10 #include <framework/pcore/MsgHandler.h>
11 
12 #include "TObject.h"
13 #include "TText.h"
14 #include "TH1.h"
15 #include "TKey.h"
16 #include "TFile.h"
17 #include "TIterator.h"
18 #include "TDirectory.h"
19 #include "TList.h"
20 
21 #include <stdio.h>
22 #include <string>
23 
24 using namespace Belle2;
25 using namespace std;
26 
27 void StreamHistogramsInDir(TDirectory* curdir, MsgHandler* hdl, int& numobjs)
28 {
29  TList* keylist = curdir->GetListOfKeys();
30 
31  TIter nextkey(keylist);
32  TKey* key = 0;
33  int nkeys = 0;
34  int nobjs = 0;
35  while ((key = (TKey*)nextkey())) {
36  nkeys++;
37  TObject* obj = key->ReadObj();
38  if (obj->IsA()->InheritsFrom("TH1")) {
39  TH1* h1 = (TH1*) obj;
40  hdl->add(h1, h1->GetName());
41  nobjs++;
42  numobjs++;
43  } else if (obj->IsA()->InheritsFrom(TDirectory::Class())) {
44  TDirectory* tdir = (TDirectory*) obj;
45  printf("subdir: %s\n", tdir->GetName());
46  TText subdir(0, 0, tdir->GetName());
47  hdl->add(&subdir, "SUBDIR:" + string(obj->GetName())) ;
48  nobjs++;
49  numobjs++;
50  tdir->cd();
51  StreamHistogramsInDir(tdir, hdl, numobjs);
52  TText command(0, 0, "COMMAND:EXIT");
53  hdl->add(&command, "SUBDIR:EXIT");
54  nobjs++;
55  numobjs++;
56  curdir->cd();
57  }
58  }
59 }
60 
61 int main(int argc, char** argv)
62 {
63  if (argc < 4) {
64  printf("Usage : hsendfile file host port\n");
65  exit(-1);
66  }
67  string file = string(argv[1]);
68  string host = string(argv[2]);
69  int port = atoi(argv[3]);
70 
71  EvtSocketSend* sock = new EvtSocketSend(host.c_str(), port);
72 
73  MsgHandler hdl(0);
74  int numobjs = 0;
75 
76  TFile* f = new TFile(file.c_str());
77 
78  StreamHistogramsInDir(f, &hdl, numobjs);
79 
80  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
81  (msg->header())->reserved[0] = 0;
82  (msg->header())->reserved[1] = numobjs;
83 
84  sock->send(msg);
85  delete(msg);
86  delete(sock);
87 
88  f->Close();
89  delete f;
90 
91  return 0;
92 }
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
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition: MsgHandler.cc:46
virtual EvtMessage * encode_msg(ERecordType rectype)
Stream object list into an EvtMessage.
Definition: MsgHandler.cc:67
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91