Belle II Software  release-06-02-00
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, string mode, int size)
21 {
22  // Record parameters
23  m_size = size;
24  if (mode != "write" && mode != "WRITE")
25  m_mode = 0;
26  else
27  m_mode = 1;
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 SharedMem((char*)name.c_str(), size);
36 
37  // Open TMemFile if write mode selected
38  if (m_mode == 1) {
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 DqmMemFile::DqmMemFile(int shm_id, int sem_id, string mode, int size)
49 {
50  // Record parameters
51  m_size = size;
52  if (mode != "write" && mode != "WRITE")
53  m_mode = 0;
54  else
55  m_mode = 1;
56  m_name = "dqm_mem_file";
57  m_memfile = NULL;
58 
59  // Allocate memory space for TMemFile
60  m_buf = (char*) new int[size];
61 
62  // Allocate shared memory
63  m_shm = new SharedMem(shm_id, sem_id, size);
64 
65  // Open TMemFile if write mode selected
66  if (m_mode == 1) {
67  m_memfile = new TMemFile(m_name.c_str(), m_buf, size * sizeof(int), "RECREATE");
68  printf("DqmMemFile : TMemFile is opened in WRITE mode.\n");
69  } else
70  printf("DqmMemFile : TMemFile is opend in READ mode.\n");
71 }
72 
73 // Destructor
74 DqmMemFile::~DqmMemFile()
75 {
76  if (m_memfile != NULL)
77  delete m_memfile;
78  delete m_shm;
79  delete m_buf;
80 }
81 
82 // Returns pointer to TMemFile
83 TMemFile* DqmMemFile::GetMemFile()
84 {
85  return m_memfile;
86 }
87 
88 // Copy TMemFile contents to Shared Memory
89 int DqmMemFile::UpdateSharedMem()
90 {
91  if (m_mode == 0) return -1;
92  m_memfile->Write(0, TObject::kOverwrite);
93  m_shm->lock();
94  m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
95  m_shm->unlock();
96  return 0;
97 }
98 
99 int DqmMemFile::ClearSharedMem()
100 {
101  if (m_mode == 0) return -1;
102 
103  if (m_memfile != NULL) delete m_memfile;
104  m_memfile = new TMemFile(m_name.c_str(), m_buf, m_size * sizeof(int), "RECREATE");
105 
106  m_shm->lock();
107  m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
108  m_shm->unlock();
109 
110  return 0;
111 }
112 
113 TMemFile* DqmMemFile::LoadMemFile()
114 {
115  if (m_mode == 1) return NULL;
116 
117  if (m_memfile != NULL) {
118  delete m_memfile;
119  }
120 
121  m_shm->lock();
122  memcpy(m_buf, m_shm->ptr(), m_size * sizeof(int));
123  m_shm->unlock();
124  // m_memfile = new TMemFile ( m_name.c_str(), m_buf, m_size*sizeof(int), "RECREATE" );
125  m_memfile = new TMemFile(m_name.c_str(), m_buf, MEMFILESIZE);
126  return m_memfile;
127 }
128 
129 // Copy Shared Memory to local and stream
130 EvtMessage* DqmMemFile::StreamMemFile()
131 {
132  TMemFile* memfile = LoadMemFile();
133  if (memfile == NULL) return NULL;
134  // memfile->ls();
135  // memfile->Print();
136  memfile->cd();
137  // gDirectory->ls();
138  // TList* keylist = memfile->GetListOfKeys();
139  // keylist->ls();
140  MsgHandler hdl(0);
141  int numobjs = 0;
142  // StreamHistograms ( memfile->GetDirectory(NULL), &hdl, numobjs );
143  StreamHistograms(gDirectory, &hdl, numobjs);
144  // printf ( "DqmMemFile::StreamMemFile : streamed %d histograms in EvtMessage\n", numobjs );
145  EvtMessage* msg = hdl.encode_msg(MSG_EVENT);
146  (msg->header())->reserved[0] = 0;
147  (msg->header())->reserved[1] = numobjs;
148  return msg;
149 }
150 
151 int DqmMemFile::StreamHistograms(TDirectory* curdir, MsgHandler* msg, int& numobjs)
152 {
153  TList* keylist = curdir->GetListOfKeys();
154  // keylist->ls();
155 
156  TIter nextkey(keylist);
157  TKey* key = 0;
158  int nkeys = 0;
159  int nobjs = 0;
160  while ((key = (TKey*)nextkey())) {
161  nkeys++;
162  TObject* obj = curdir->FindObjectAny(key->GetName());
163  if (obj->IsA()->InheritsFrom("TH1")) {
164  TH1* h1 = (TH1*) obj;
165  // printf ( "Key = %s, entry = %f\n", key->GetName(), h1->GetEntries() );
166  msg->add(h1, h1->GetName());
167  nobjs++;
168  numobjs++;
169  } else if (obj->IsA()->InheritsFrom(TDirectory::Class())) {
170  // printf ( "New directory found %s, Go into subdir\n", obj->GetName() );
171  TDirectory* tdir = (TDirectory*) obj;
172  // m_msg->add(tdir, tdir->GetName());
173  TText subdir(0, 0, tdir->GetName());
174  msg->add(&subdir, "SUBDIR:" + string(obj->GetName())) ;
175  nobjs++;
176  numobjs++;
177  tdir->cd();
178  StreamHistograms(tdir, msg, numobjs);
179  TText command(0, 0, "COMMAND:EXIT");
180  msg->add(&command, "SUBDIR:EXIT");
181  nobjs++;
182  numobjs++;
183  curdir->cd();
184  }
185  }
186  return 0;
187 }
188 
189 
190 
191 
192 
193 
194 
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.