Belle II Software  release-06-02-00
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 <framework/pcore/EvtMessage.h>
10 #include <framework/pcore/MsgHandler.h>
11 
12 #include <TText.h>
13 #include <TH1.h>
14 #include <TKey.h>
15 
16 #include <unistd.h>
17 
18 #include <sys/wait.h>
19 
20 using namespace Belle2;
21 using namespace std;
22 
23 static DqmMasterCallback* s_dqmmaster = NULL;
24 
25 static int m_running = 0;
26 
27 //-----------------------------------------------------------------
28 // Rbuf-Read Thread Interface
29 //-----------------------------------------------------------------
30 void* RunDqmMasterLogger(void*)
31 {
32 }
33 
34 DqmMasterCallback::DqmMasterCallback(ConfigFile& config)
35 {
36  m_hltdir = config.get("dqmmaster.hltdir");
37  m_erecodir = config.get("dqmmaster.erecodir");
38  m_running = 0;
39  printf("DqmMasterCallback : hltdir = %s, erecodir = %s\n", m_hltdir.c_str(), m_erecodir.c_str());
40 
41  // Open MemFile
42  m_hltdqm = new DqmMemFile("dqmhisto_hlt");
43  m_erecodqm = new DqmMemFile("dqmhisto_reco1");
44 
45  // Open sockets to hservers
46  m_sock_hlt = new EvtSocketSend("localhost", 9991);
47  m_sock_reco = new EvtSocketSend("localhost", 9992);
48 }
49 
50 DqmMasterCallback::~DqmMasterCallback()
51 {
52 
53 }
54 
55 void DqmMasterCallback::load(const DBObject& /* obj */, const std::string& runtype)
56 {
57  m_runtype = runtype;
58 }
59 
60 void DqmMasterCallback::start(int expno, int runno)
61 {
62  m_expno = expno;
63  m_runno = runno;
64 
65  MsgHandler hdl(0);
66  int numobjs = 0;
67 
68  TText rc_clear(0, 0, "DQMRC:CLEAR");
69  hdl.add(&rc_clear, "DQMRC:CLEAR");
70  numobjs++;
71  TText subdir(0, 0, "DQMInfo");
72  hdl.add(&subdir, "SUBDIR:DQMInfo") ;
73  numobjs++;
74  TH1F h_expno("expno", to_string(m_expno).c_str(), 1, 0, 1);
75  hdl.add(&h_expno, "expno");
76  numobjs++;
77  TH1F h_runno("runno", to_string(m_runno).c_str(), 1, 0, 1);
78  hdl.add(&h_runno, "runno");
79  numobjs++;
80  TH1F h_rtype("rtype", m_runtype.c_str(), 1, 0, 1);
81  hdl.add(&h_rtype, "rtype");
82  numobjs++;
83  TText command(0, 0, "COMMAND:EXIT");
84  hdl.add(&command, "SUBDIR:EXIT");
85  numobjs++;
86  TText rc_merge(0, 0, "DQMRC:MERGE");
87  hdl.add(&rc_merge, "DQMRC:MERGE");
88  numobjs++;
89 
90  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
91  (msg->header())->reserved[0] = 0;
92  (msg->header())->reserved[1] = numobjs;
93 
94  m_sock_hlt->send(msg);
95  m_sock_reco->send(msg);
96  delete(msg);
97 
98  printf("expno = %d, runno = %d\n", m_expno, m_runno);
99  m_running = 1;
100 }
101 
102 void DqmMasterCallback::stop()
103 {
104 
105  if (m_running == 0) return;
106 
107  m_running = 0;
108 
109  char outfile[1024];
110  // m_expno = getExpNumber();
111  // m_runno = getRunNumber();
112 
113  // Connect TMemFile
114  TMemFile* hlttmem = m_hltdqm->LoadMemFile();
115  TMemFile* erecotmem = m_erecodqm->LoadMemFile();
116 
117  // Dump HLT DQM
118  int proc1 = fork();
119  if (proc1 == 0) {
120  // TMemFile* tmem = m_hltdqm->LoadMemFile();
121  sprintf(outfile, "%s/hltdqm_e%4.4dr%6.6d.root", m_hltdir.c_str(), m_expno, m_runno);
122  TFile* dqmtfile = new TFile(outfile, "RECREATE");
123  printf("HLT dqm file = %s\n", outfile);
124 
125  // Copy all histograms in TFile
126  TIter next(hlttmem->GetListOfKeys());
127  TKey* key = NULL;
128  while ((key = (TKey*)next())) {
129  TH1* hist = (TH1*)key->ReadObj();
130  printf("HistTitle %s : entries = %f\n", hist->GetName(), hist->GetEntries());
131  TH1* cpyhst = (TH1*)hist->Clone();
132  }
133 
134  // Close TFile
135  dqmtfile->Write();
136  dqmtfile->Close();
137  delete dqmtfile;
138  // delete hlttmem;
139  exit(0);
140  } else if (proc1 < 0) {
141  perror("DQMMASTER : fork HLTDQM writing");
142  }
143 
144  // Dump ERECO DQM
145  int proc2 = fork();
146  if (proc2 == 0) {
147  // TMemFile* tmem = m_erecodqm->LoadMemFile();
148  sprintf(outfile, "%s/erecodqm_e%4.4dr%6.6d.root", m_erecodir.c_str(), m_expno, m_runno);
149  // sprintf(outfile, "hltdqm_e%4.4dr%6.6d.root", m_expno, m_runno);
150  TFile* erdqmtfile = new TFile(outfile, "RECREATE");
151  printf("ERECO dqm file = %s\n", outfile);
152 
153  // Copy all histograms in TFile
154  TIter ernext(erecotmem->GetListOfKeys());
155  TKey* erkey = NULL;
156  while ((erkey = (TKey*)ernext())) {
157  TH1* hist = (TH1*)erkey->ReadObj();
158  printf("HistTitle %s : entries = %f\n", hist->GetName(), hist->GetEntries());
159  TH1* cpyhst = (TH1*)hist->Clone();
160  }
161 
162  // Close TFile
163  erdqmtfile->Write();
164  erdqmtfile->Close();
165  delete erdqmtfile;
166  // delete erecotmem;
167  exit(0);
168  } else if (proc2 < 0) {
169  perror("DQMMASTER : fork ERECODQM writing");
170  }
171 
172  // Wait completion
173  int status1, status2;
174  waitpid(proc1, &status1, 0);
175  waitpid(proc2, &status2, 0);
176 
177  // delete hlttmem;
178  // delete erecotmem;
179 }
180 
181 void DqmMasterCallback::abort()
182 {
183  stop();
184 }
185 
186 
187 
188 
189 
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.