DABC (Data Acquisition Backbone Core)  2.9.9
LmdFile.h
Go to the documentation of this file.
1 // $Id: LmdFile.h 4721 2021-03-13 13:52:01Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef MBS_LmdFile
17 #define MBS_LmdFile
18 
19 #ifndef DABC_BinaryFile
20 #include "dabc/BinaryFile.h"
21 #endif
22 
23 #ifndef MBS_LmdTypeDefs
24 #include "mbs/LmdTypeDefs.h"
25 #endif
26 
27 
28 namespace mbs {
29 
35  class LmdFile : public dabc::BasicFile {
36  protected:
38 
39  public:
40 
42 
44  {
45  Close();
46  }
47 
48  bool OpenReading(const char* fname, const char* opt = 0)
49  {
50  if (isOpened()) return false;
51 
52  if (fname==0 || *fname==0) {
53  fprintf(stderr, "file name not specified\n");
54  return false;
55  }
56 
57  CheckIO();
58 
59  fd = io->fopen(fname, "r", opt);
60  if (fd==0) {
61  fprintf(stderr, "File open failed %s for reading\n", fname);
62  return false;
63  }
64 
65  if (io->fread(&fFileHdr, sizeof(fFileHdr), 1, fd) != 1) {
66  fprintf(stderr, "Failure reading file %s header\n", fname);
67  Close();
68  return false;
69  }
70 
71  if (fFileHdr.iEndian != 1) {
72  fprintf(stderr, "Wrong endian %u in file %s, expected 1\n", (unsigned) fFileHdr.iEndian, fname);
73  Close();
74  return false;
75  }
76 
77  if (fFileHdr.isTypePair(0x65, 0x1) && (fFileHdr.FullSize() == 0xfffffff0) && (fFileHdr.iOffsetSize == 8)) {
78  // this is LMD file, created by DABC
79  fReadingMode = true;
80  // fMbsFormat = false;
81  return true;
82  }
83 
84  fprintf(stderr, "%s is original LMD file, which is not supported by DABC\n", fname);
85  Close();
86  return false;
87 /*
88  if ((fFileHdr.FullSize() > 0x8000) || (fFileHdr.FullSize() < sizeof(fFileHdr))) {
89  fprintf(stderr, "File header %u too large in file %s\n", (unsigned) fFileHdr.FullSize(), fname);
90  Close();
91  return false;
92  }
93 
94  if (fFileHdr.FullSize() > sizeof(fFileHdr)) {
95  // skip dummy bytes from file header
96  io->fseek(fd, fFileHdr.FullSize() - sizeof(fFileHdr), true);
97  }
98 
99  fReadingMode = true;
100  // fMbsFormat = true;
101  return true;
102 */
103  }
104 
105  bool OpenWriting(const char* fname, const char* opt = nullptr)
106  {
107  if (isOpened()) return false;
108 
109  if (fname==0 || *fname==0) {
110  fprintf(stderr, "file name not specified\n");
111  return false;
112  }
113 
114  CheckIO();
115 
116  fd = io->fopen(fname, "w", opt);
117  if (fd==0) {
118  fprintf(stderr, "File open failed %s for writing\n", fname);
119  return false;
120  }
121 
122  fFileHdr.SetFullSize(0xfffffff0);
123  fFileHdr.SetTypePair(0x65, 0x1);
125  fFileHdr.iElements = 0xffffffff;
126  fFileHdr.iOffsetSize = 8; // Offset size, 4 or 8 [bytes]
127  fFileHdr.iTimeSpecSec = 0; // compatible with s_bufhe (2*32bit)
128  fFileHdr.iTimeSpecNanoSec = 0; // compatible with s_bufhe (2*32bit)
129 
130  fFileHdr.iEndian = 1; // compatible with s_bufhe free[0]
131  fFileHdr.iWrittenEndian = 0;// one of LMD__ENDIAN_x
132  fFileHdr.iUsedWords = 0; // total words without header to read for type=100, free[2]
133  fFileHdr.iFree3 = 0; // free[3]
134 
135  if (io->fwrite(&fFileHdr, sizeof(fFileHdr),1,fd)!=1) {
136  fprintf(stderr, "Failure writing file %s header\n", fname);
137  Close();
138  return false;
139  }
140 
141  fReadingMode = false;
142  // fMbsFormat = false;
143  return true;
144  }
145 
146  bool Close()
147  {
148  return CloseBasicFile();
149  }
150 
151  const FileHeader& hdr() const { return fFileHdr; }
152 
155  bool WriteBuffer(const void* ptr, uint64_t sz)
156  {
157  if (!isWriting() || (ptr==0) || (sz==0)) return false;
158 
159  if (io->fwrite(ptr, sz, 1, fd) != 1) {
160  fprintf(stderr, "fail to write buffer of size %u to lmd file\n", (unsigned) sz);
161  Close();
162  return false;
163  }
164 
165  return true;
166  }
167 
168 
170  bool ReadBuffer(void* ptr, uint64_t* sz, bool onlyevent = false)
171  {
172  if (isWriting() || (ptr==0) || (sz==0) || (*sz < sizeof(mbs::Header))) return false;
173 
174  // if (fMbsFormat) return ReadMbsBuffer(ptr, sz, onlyevent);
175 
176  uint64_t maxsz = *sz; *sz = 0;
177 
178  // printf("start buffer reading maxsz = %u\n", (unsigned) maxsz);
179 
180  // any data in LMD should be written with 4-byte wrapping
181  size_t readsz = io->fread(ptr, 1, maxsz, fd);
182 
183  // printf("readsz = %u\n", (unsigned) readsz);
184 
185  if (readsz==0) return false;
186 
187  size_t checkedsz = 0;
188 
189  mbs::Header* hdr = (mbs::Header*) ptr;
190 
191  while (checkedsz < readsz) {
192  // special case when event was read not completely
193  // or we want to provide only event
194  if ((checkedsz + hdr->FullSize() > readsz) || (onlyevent && (checkedsz>0))) {
195  // return pointer to the begin of event
196  io->fseek(fd, -(readsz - checkedsz), true);
197  break;
198  }
199  checkedsz += hdr->FullSize();
200  hdr = (mbs::Header*) ((char*) hdr + hdr->FullSize());
201  }
202 
203  *sz = checkedsz;
204 
205  return checkedsz>0;
206  }
207 
208  };
209 }
210 
211 #endif
Base class for file writing/reading in DABC.
Definition: BinaryFile.h:84
FileInterface::Handle fd
if true, io object owned by file
Definition: BinaryFile.h:88
void CheckIO()
Definition: BinaryFile.h:99
bool isOpened() const
Definition: BinaryFile.h:129
FileInterface * io
Definition: BinaryFile.h:86
bool CloseBasicFile()
reading/writing mode
Definition: BinaryFile.h:91
bool isWriting() const
Definition: BinaryFile.h:133
bool fReadingMode
file descriptor
Definition: BinaryFile.h:89
virtual bool fseek(Handle f, long int offset, bool relative=true)
Definition: BinaryFile.h:57
virtual size_t fwrite(const void *ptr, size_t sz, size_t nmemb, Handle f)
Definition: BinaryFile.h:45
virtual size_t fread(void *ptr, size_t sz, size_t nmemb, Handle f)
Definition: BinaryFile.h:48
virtual Handle fopen(const char *fname, const char *mode, const char *=0)
Definition: BinaryFile.h:41
Reading/writing LMD files (new API)
Definition: LmdFile.h:35
const FileHeader & hdr() const
Definition: LmdFile.h:151
LmdFile()
file header
Definition: LmdFile.h:41
bool Close()
Definition: LmdFile.h:146
bool OpenReading(const char *fname, const char *opt=0)
Definition: LmdFile.h:48
bool WriteBuffer(const void *ptr, uint64_t sz)
Write buffer or part of buffer User must ensure that content of buffer is corresponds to the lmd head...
Definition: LmdFile.h:155
bool OpenWriting(const char *fname, const char *opt=nullptr)
Definition: LmdFile.h:105
bool ReadBuffer(void *ptr, uint64_t *sz, bool onlyevent=false)
Reads buffer with several MBS events.
Definition: LmdFile.h:170
FileHeader fFileHdr
Definition: LmdFile.h:37
Event manipulation API.
Definition: api.h:23
Support for MBS - standard GSI DAQ.
Definition: api.h:36
Structure of the LMD file header.
Definition: LmdTypeDefs.h:73
uint32_t iFree3
free[3]
Definition: LmdTypeDefs.h:83
uint64_t iTableOffset
optional offset to element offset table in file
Definition: LmdTypeDefs.h:74
uint32_t iWrittenEndian
one of LMD__ENDIAN_x
Definition: LmdTypeDefs.h:81
uint32_t iTimeSpecNanoSec
compatible with s_bufhe (2*32bit)
Definition: LmdTypeDefs.h:78
uint32_t iUsedWords
total words without header to read for type=100, free[2]
Definition: LmdTypeDefs.h:82
uint32_t iOffsetSize
Offset size, 4 or 8 [bytes].
Definition: LmdTypeDefs.h:76
uint32_t iEndian
compatible with s_bufhe free[0]
Definition: LmdTypeDefs.h:80
uint32_t iElements
compatible with s_bufhe
Definition: LmdTypeDefs.h:75
uint32_t iTimeSpecSec
compatible with s_bufhe (2*32bit)
Definition: LmdTypeDefs.h:77
Structure of any entry in LMD file.
Definition: LmdTypeDefs.h:39
bool isTypePair(unsigned typ, unsigned subtyp) const
Definition: LmdTypeDefs.h:66
void SetFullSize(uint32_t sz)
Definition: LmdTypeDefs.h:56
uint32_t FullSize() const
Definition: LmdTypeDefs.h:55
void SetTypePair(unsigned typ, unsigned subtyp)
Definition: LmdTypeDefs.h:63