Belle II Software development
DqmMemFile Class Reference

Public Types

enum  { c_memFileSize = 128000000 }
 

Public Member Functions

 DqmMemFile (std::string name="dqmhisto", const std::string &mode="readonly", int size=c_memFileSize)
 
 DqmMemFile (int shm_id, int sem_id, int size=c_memFileSize)
 
TMemFile * GetMemFile ()
 
int UpdateSharedMem ()
 
int ClearSharedMem ()
 
TMemFile * LoadMemFile ()
 
EvtMessageStreamMemFile ()
 
bool SaveToFile (std::string outfile)
 

Private Member Functions

int StreamHistograms (TDirectory *curdir, MsgHandler *msg, int &nobjs)
 

Private Attributes

DqmSharedMemm_shm
 
TMemFile * m_memfile
 
char * m_buf
 
std::string m_name
 
int m_size
 
bool m_writeMode
 

Detailed Description

Definition at line 25 of file DqmMemFile.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

Definition at line 27 of file DqmMemFile.h.

27{c_memFileSize = 128000000};

Constructor & Destructor Documentation

◆ DqmMemFile() [1/2]

DqmMemFile ( std::string  name = "dqmhisto",
const std::string &  mode = "readonly",
int  size = c_memFileSize 
)

Definition at line 20 of file DqmMemFile.cc.

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, m_writeMode);
36
37 // Clear/Open TMemFile if write mode selected
38 // it will check write mode by itself!
39 if (ClearSharedMem() == 0) {
40 printf("DqmMemFile : TMemFile is opened in WRITE mode.\n");
41 } else {
42 printf("DqmMemFile : TMemFile is opend in READ mode.\n");
43 }
44}

◆ DqmMemFile() [2/2]

DqmMemFile ( int  shm_id,
int  sem_id,
int  size = c_memFileSize 
)

Definition at line 47 of file DqmMemFile.cc.

48{
49 // This defaults to read only access
50 // Record parameters
51 m_size = size;
52 m_writeMode = false;
53 m_name = "dqm_mem_file";
54 m_memfile = NULL;
55
56 // Allocate memory space for TMemFile
57 m_buf = (char*) new int[size];
58
59 // Allocate shared memory
60 m_shm = new DqmSharedMem(shm_id, sem_id);
61 printf("DqmMemFile : TMemFile is opend in READ mode.\n");
62}

◆ ~DqmMemFile()

~DqmMemFile ( )

Definition at line 65 of file DqmMemFile.cc.

66{
67 if (m_memfile != NULL)
68 delete m_memfile;
69 delete m_shm;
70 delete m_buf;
71}

Member Function Documentation

◆ ClearSharedMem()

int ClearSharedMem ( )

Definition at line 100 of file DqmMemFile.cc.

101{
102 if (!m_writeMode) return -1;
103 // Open TMemFile only if write mode selected
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}

◆ GetMemFile()

TMemFile * GetMemFile ( )

Definition at line 74 of file DqmMemFile.cc.

75{
76 return m_memfile;
77}

◆ LoadMemFile()

TMemFile * LoadMemFile ( )

Definition at line 115 of file DqmMemFile.cc.

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, c_memFileSize);
128 return m_memfile;
129}

◆ SaveToFile()

bool SaveToFile ( std::string  outfile)

Definition at line 191 of file DqmMemFile.cc.

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}

◆ StreamHistograms()

int StreamHistograms ( TDirectory *  curdir,
MsgHandler msg,
int &  nobjs 
)
private

Definition at line 153 of file DqmMemFile.cc.

154{
155 TList* keylist = curdir->GetListOfKeys();
156 // keylist->ls();
157
158 TIter nextkey(keylist);
159 TKey* key = 0;
160 int nkeys [[maybe_unused]] = 0;
161 int nobjs [[maybe_unused]] = 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}
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition: MsgHandler.cc:46

◆ StreamMemFile()

EvtMessage * StreamMemFile ( )

Definition at line 132 of file DqmMemFile.cc.

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}
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

◆ UpdateSharedMem()

int UpdateSharedMem ( )

Definition at line 80 of file DqmMemFile.cc.

81{
82 if (!m_writeMode) return -1;
83 m_memfile->Write(0, TObject::kOverwrite);
84 m_shm->lock();
85 auto ret = m_memfile->CopyTo((char*)(m_shm->ptr()), m_memfile->GetSize());
86
87 FILE* fh = fopen(("/dev/shm/tmp_" + m_name).c_str(), "wb+");
88 if (fh) {
89 fwrite(m_shm->ptr(), 1, ret, fh);
90 fclose(fh);
91 if (rename(("/dev/shm/tmp_" + m_name).c_str(), ("/dev/shm/" + m_name).c_str())) {
92 perror("Rename dhm file failed ");
93 }
94 }
95
96 m_shm->unlock();
97 return 0;
98}

Member Data Documentation

◆ m_buf

char* m_buf
private

Definition at line 51 of file DqmMemFile.h.

◆ m_memfile

TMemFile* m_memfile
private

Definition at line 50 of file DqmMemFile.h.

◆ m_name

std::string m_name
private

Definition at line 52 of file DqmMemFile.h.

◆ m_shm

DqmSharedMem* m_shm
private

Definition at line 49 of file DqmMemFile.h.

◆ m_size

int m_size
private

Definition at line 53 of file DqmMemFile.h.

◆ m_writeMode

bool m_writeMode
private

Definition at line 54 of file DqmMemFile.h.


The documentation for this class was generated from the following files: