Belle II Software  release-08-01-10
DqmMemFile.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/DqmMemFile.h"
10 #include <framework/pcore/MsgHandler.h>
11 
12 #include <TH1.h>
13 #include <TKey.h>
14 #include <TText.h>
15 
16 using namespace Belle2;
17 using namespace std;
18 
19 // Constructor
20 DqmMemFile::DqmMemFile(string name, const string& mode, int size)
21 {
22  // Record parameters
23  m_size = size;
24  if (mode != "write" && mode != "WRITE")
25  m_writeMode = false;
26  else
27  m_writeMode = true;
28  m_name = name;
29  m_memfile = NULL;
30 
31  // Allocate memory space for TMemFile
32  m_buf = (char*) new int[size];
33 
34  // Allocate shared memory
35  m_shm = new DqmSharedMem((char*)name.c_str(), size);
36 
37  // Open TMemFile if write mode selected
38  if (m_writeMode) {
39  m_memfile = new TMemFile(name.c_str(), m_buf, size * sizeof(int), "RECREATE");
40  m_shm->lock();
41  m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
42  m_shm->unlock();
43  printf("DqmMemFile : TMemFile is opened in WRITE mode.\n");
44  } else {
45  printf("DqmMemFile : TMemFile is opend in READ mode.\n");
46  }
47 }
48 
49 DqmMemFile::DqmMemFile(int shm_id, int sem_id, const string& mode, int size)
50 {
51  // Record parameters
52  m_size = size;
53  if (mode != "write" && mode != "WRITE")
54  m_writeMode = false;
55  else
56  m_writeMode = true;
57  m_name = "dqm_mem_file";
58  m_memfile = NULL;
59 
60  // Allocate memory space for TMemFile
61  m_buf = (char*) new int[size];
62 
63  // Allocate shared memory
64  m_shm = new DqmSharedMem(shm_id, sem_id, size);// size is not used in here
65 
66  // Open TMemFile if write mode selected
67  if (m_writeMode) {
68  m_memfile = new TMemFile(m_name.c_str(), m_buf, size * sizeof(int), "RECREATE");
69  printf("DqmMemFile : TMemFile is opened in WRITE mode.\n");
70  } else {
71  printf("DqmMemFile : TMemFile is opend in READ mode.\n");
72  }
73 }
74 
75 // Destructor
76 DqmMemFile::~DqmMemFile()
77 {
78  if (m_memfile != NULL)
79  delete m_memfile;
80  delete m_shm;
81  delete m_buf;
82 }
83 
84 // Returns pointer to TMemFile
85 TMemFile* DqmMemFile::GetMemFile()
86 {
87  return m_memfile;
88 }
89 
90 // Copy TMemFile contents to Shared Memory
91 int DqmMemFile::UpdateSharedMem()
92 {
93  if (!m_writeMode) return -1;
94  m_memfile->Write(0, TObject::kOverwrite);
95  m_shm->lock();
96  m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
97  m_shm->unlock();
98  return 0;
99 }
100 
101 int DqmMemFile::ClearSharedMem()
102 {
103  if (!m_writeMode) return -1;
104 
105  if (m_memfile != NULL) delete m_memfile;
106  m_memfile = new TMemFile(m_name.c_str(), m_buf, m_size * sizeof(int), "RECREATE");
107 
108  m_shm->lock();
109  m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
110  m_shm->unlock();
111 
112  return 0;
113 }
114 
115 TMemFile* DqmMemFile::LoadMemFile()
116 {
117  if (m_writeMode) return NULL;
118 
119  if (m_memfile != NULL) {
120  delete m_memfile;
121  }
122 
123  m_shm->lock();
124  memcpy(m_buf, m_shm->ptr(), m_size * sizeof(int));
125  m_shm->unlock();
126  // m_memfile = new TMemFile ( m_name.c_str(), m_buf, m_size*sizeof(int), "RECREATE" );
127  m_memfile = new TMemFile(m_name.c_str(), m_buf, MEMFILESIZE);
128  return m_memfile;
129 }
130 
131 // Copy Shared Memory to local and stream
132 EvtMessage* DqmMemFile::StreamMemFile()
133 {
134  TMemFile* memfile = LoadMemFile();
135  if (memfile == NULL) return NULL;
136  // memfile->ls();
137  // memfile->Print();
138  memfile->cd();
139  // gDirectory->ls();
140  // TList* keylist = memfile->GetListOfKeys();
141  // keylist->ls();
142  MsgHandler hdl(0);
143  int numobjs = 0;
144  // StreamHistograms ( memfile->GetDirectory(NULL), &hdl, numobjs );
145  StreamHistograms(gDirectory, &hdl, numobjs);
146  // printf ( "DqmMemFile::StreamMemFile : streamed %d histograms in EvtMessage\n", numobjs );
147  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
148  (msg->header())->reserved[0] = 0;
149  (msg->header())->reserved[1] = numobjs;
150  return msg;
151 }
152 
153 int DqmMemFile::StreamHistograms(TDirectory* curdir, MsgHandler* msg, int& numobjs)
154 {
155  TList* keylist = curdir->GetListOfKeys();
156  // keylist->ls();
157 
158  TIter nextkey(keylist);
159  TKey* key = 0;
160  int nkeys = 0;
161  int nobjs = 0;
162  while ((key = (TKey*)nextkey())) {
163  nkeys++;
164  TObject* obj = curdir->FindObjectAny(key->GetName());
165  if (obj->IsA()->InheritsFrom("TH1")) {
166  TH1* h1 = (TH1*) obj;
167  // printf ( "Key = %s, entry = %f\n", key->GetName(), h1->GetEntries() );
168  msg->add(h1, h1->GetName());
169  nobjs++;
170  numobjs++;
171  } else if (obj->IsA()->InheritsFrom(TDirectory::Class())) {
172  // printf ( "New directory found %s, Go into subdir\n", obj->GetName() );
173  TDirectory* tdir = (TDirectory*) obj;
174  // m_msg->add(tdir, tdir->GetName());
175  TText subdir(0, 0, tdir->GetName());
176  msg->add(&subdir, "SUBDIR:" + string(obj->GetName())) ;
177  nobjs++;
178  numobjs++;
179  tdir->cd();
180  StreamHistograms(tdir, msg, numobjs);
181  TText command(0, 0, "COMMAND:EXIT");
182  msg->add(&command, "SUBDIR:EXIT");
183  nobjs++;
184  numobjs++;
185  curdir->cd();
186  }
187  }
188  return 0;
189 }
190 
191 bool DqmMemFile::SaveToFile(std::string outfile)
192 {
193  // we do not work on shared memory, thus can directly write w/o locking
194 // m_memfile->Write(0, TObject::kOverwrite);
195 // m_memfile->WriteToFile(outfile);
196 
197  printf("dump to dqm file = %s\n", outfile.c_str());
198 
199  TFile* dqmtfile = new TFile(outfile.c_str(), "RECREATE");
200 
201  // Copy all histograms in TFile
202  TIter next(m_memfile->GetListOfKeys());
203  TKey* key = NULL;
204  while ((key = (TKey*)next())) {
205  TH1* hist = (TH1*)key->ReadObj();
206  // printf("HistTitle %s : entries = %f\n", hist->GetName(), hist->GetEntries());
207  hist->Write();
208  }
209 
210  // Close TFile
211  dqmtfile->Write();
212  dqmtfile->Close();
213 
214  delete dqmtfile;
215 
216  return true;
217 }
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
Abstract base class for different kinds of events.