9#include "daq/dqm/DqmMemFile.h"
10#include <framework/pcore/MsgHandler.h>
20DqmMemFile::DqmMemFile(
string name,
const string& mode,
int size)
24 if (mode !=
"write" && mode !=
"WRITE")
32 m_buf = (
char*)
new int[size];
35 m_shm =
new DqmSharedMem((
char*)name.c_str(), size, m_writeMode);
39 if (ClearSharedMem() == 0) {
40 printf(
"DqmMemFile : TMemFile is opened in WRITE mode.\n");
42 printf(
"DqmMemFile : TMemFile is opend in READ mode.\n");
47DqmMemFile::DqmMemFile(
int shm_id,
int sem_id,
int size)
53 m_name =
"dqm_mem_file";
57 m_buf = (
char*)
new int[size];
61 printf(
"DqmMemFile : TMemFile is opend in READ mode.\n");
65DqmMemFile::~DqmMemFile()
67 if (m_memfile != NULL)
74TMemFile* DqmMemFile::GetMemFile()
80int DqmMemFile::UpdateSharedMem()
82 if (!m_writeMode)
return -1;
83 m_memfile->Write(0, TObject::kOverwrite);
85 auto ret = m_memfile->CopyTo((
char*)(m_shm->ptr()), m_memfile->GetSize());
87 FILE* fh = fopen((
"/dev/shm/tmp_" + m_name).c_str(),
"wb+");
89 fwrite(m_shm->ptr(), 1, ret, fh);
91 if (rename((
"/dev/shm/tmp_" + m_name).c_str(), (
"/dev/shm/" + m_name).c_str())) {
92 perror(
"Rename dhm file failed ");
100int DqmMemFile::ClearSharedMem()
102 if (!m_writeMode)
return -1;
105 if (m_memfile != NULL)
delete m_memfile;
106 m_memfile =
new TMemFile(m_name.c_str(), m_buf, m_size *
sizeof(
int),
"RECREATE");
109 m_memfile->CopyTo((
char*)(m_shm->ptr()), m_memfile->GetSize());
115TMemFile* DqmMemFile::LoadMemFile()
117 if (m_writeMode)
return NULL;
119 if (m_memfile != NULL) {
124 memcpy(m_buf, m_shm->ptr(), m_size *
sizeof(
int));
127 m_memfile =
new TMemFile(m_name.c_str(), m_buf, c_memFileSize);
134 TMemFile* memfile = LoadMemFile();
135 if (memfile == NULL)
return NULL;
145 StreamHistograms(gDirectory, &hdl, numobjs);
148 (msg->
header())->reserved[0] = 0;
149 (msg->
header())->reserved[1] = numobjs;
153int DqmMemFile::StreamHistograms(TDirectory* curdir,
MsgHandler* msg,
int& numobjs)
155 TList* keylist = curdir->GetListOfKeys();
158 TIter nextkey(keylist);
160 int nkeys [[maybe_unused]] = 0;
161 int nobjs [[maybe_unused]] = 0;
162 while ((key = (TKey*)nextkey())) {
164 TObject* obj = curdir->FindObjectAny(key->GetName());
165 if (obj->IsA()->InheritsFrom(
"TH1")) {
166 TH1* h1 = (TH1*) obj;
168 msg->
add(h1, h1->GetName());
171 }
else if (obj->IsA()->InheritsFrom(TDirectory::Class())) {
173 TDirectory* tdir = (TDirectory*) obj;
175 TText subdir(0, 0, tdir->GetName());
176 msg->
add(&subdir,
"SUBDIR:" +
string(obj->GetName())) ;
180 StreamHistograms(tdir, msg, numobjs);
181 TText command(0, 0,
"COMMAND:EXIT");
182 msg->
add(&command,
"SUBDIR:EXIT");
191bool DqmMemFile::SaveToFile(std::string outfile)
197 printf(
"dump to dqm file = %s\n", outfile.c_str());
199 TFile* dqmtfile =
new TFile(outfile.c_str(),
"RECREATE");
202 TIter next(m_memfile->GetListOfKeys());
204 while ((key = (TKey*)next())) {
205 TH1* hist = (TH1*)key->ReadObj();
Class to manage streamed object.
EvtHeader * header()
Get pointer to EvtHeader.
A class to encode/decode an EvtMessage.
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Abstract base class for different kinds of events.