DABC (Data Acquisition Backbone Core)  2.9.9
HldFile.cxx
Go to the documentation of this file.
1 // $Id: HldFile.cxx 4425 2019-10-18 12:44:38Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) q *
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 #include "hadaq/HldFile.h"
17 #include "dabc/logging.h"
18 
20  dabc::BasicFile(),
21  fRunNumber(0),
22  fEOF(true)
23 {
24 }
25 
27 {
28  Close();
29 }
30 
31 bool hadaq::HldFile::OpenWrite(const char* fname, uint32_t runid, const char* opt)
32 {
33  if (isOpened()) return false;
34 
35  if (fname==0 || *fname==0) {
36  fprintf(stderr, "file name not specified\n");
37  return false;
38  }
39 
40  CheckIO();
41 
42  fd = io->fopen(fname, "w", opt);
43  if (fd==0) {
44  fprintf(stderr, "File open failed %s for writing\n", fname);
45  return false;
46  }
47 
48  fReadingMode = false;
49 
50  // put here a dummy event into file:
51  hadaq::RawEvent evnt;
52  evnt.Init(0, runid, EvtId_runStart);
53  if(!WriteBuffer(&evnt, sizeof(evnt))) {
54  CloseBasicFile();
55  return false;
56  }
57 
58  fRunNumber = runid;
59 
60  return true;
61 }
62 
63 bool hadaq::HldFile::OpenRead(const char* fname, const char* opt)
64 {
65  if (isOpened()) return false;
66 
67  if (fname==0 || *fname==0) {
68  fprintf(stderr, "file name not specified\n");
69  return false;
70  }
71 
72  CheckIO();
73 
74  fd = io->fopen(fname, "r", opt);
75  if (fd==0) {
76  fprintf(stderr, "File open failed %s for reading\n", fname);
77  return false;
78  }
79  fReadingMode = true;
80 
81 // DOUT0("Open HLD file %s for reading", fname);
82 
83  hadaq::RawEvent evnt;
84  uint32_t size = sizeof(hadaq::RawEvent);
85 
86  // printf("starts reading into buf %u isreading %u \n", (unsigned) size, (unsigned)isReading());
87 
88  if (!ReadBuffer(&evnt, &size, true)) {
89  fprintf(stderr,"Cannot read starting event from file\n");
90  CloseBasicFile();
91  return false;
92  }
93 
94  if ((size!=sizeof(hadaq::RawEvent)) || (evnt.GetId() != EvtId_runStart)) {
95  fprintf(stderr,"Did not found start event at the file beginning\n");
96  CloseBasicFile();
97  return false;
98  }
99 
100 // DOUT0("Find start event at the file begin");
101 
102  fRunNumber = evnt.GetRunNr();
103  fEOF = false;
104 
105  return true;
106 }
107 
109 {
110  DOUT3("hadaq::HldFile::Close()... ");
111  if (isWriting()) {
112  // need to add empty terminating event:
113  hadaq::RawEvent evnt;
114  evnt.Init(0, fRunNumber, EvtId_runStop);
115  WriteBuffer(&evnt, sizeof(evnt));
116  }
117 
118  CloseBasicFile();
119 
120  fRunNumber = 0;
121  fEOF = true;
122 }
123 
124 
125 
126 bool hadaq::HldFile::WriteBuffer(void* buf, uint32_t bufsize)
127 {
128  if (!isWriting() || (buf==0) || (bufsize==0)) return false;
129 
130  if (io->fwrite(buf, bufsize, 1, fd)!=1) {
131  fprintf(stderr, "fail to write buffer payload of size %u\n", (unsigned) bufsize);
132  CloseBasicFile();
133  return false;
134  }
135 
136  return true;
137 }
138 
139 bool hadaq::HldFile::ReadBuffer(void* ptr, uint32_t* sz, bool onlyevent)
140 {
141  if (!isReading() || (ptr==0) || (sz==0) || (*sz < sizeof(hadaq::HadTu))) return false;
142 
143  uint64_t maxsz = *sz; *sz = 0;
144 
145  size_t readsz = io->fread(ptr, 1, (onlyevent ? sizeof(hadaq::HadTu) : maxsz), fd);
146 
147  //printf("Read HLD portion of data %u max %u\n", (unsigned) readsz, (unsigned) maxsz);
148 
149  if (readsz < sizeof(hadaq::HadTu)) {
150  if (!io->feof(fd)) fprintf(stderr, "Fail to read next portion while no EOF detected\n");
151  fEOF = true;
152  return false;
153  }
154 
155  hadaq::HadTu* hdr = (hadaq::HadTu*) ptr;
156 
157 
158  if (onlyevent) {
159 
160  if (hdr->GetPaddedSize() > maxsz) {
161  fprintf(stderr, "Buffer %u too small to read next event %u from hld file\n", (unsigned) maxsz, (unsigned)hdr->GetPaddedSize());
162  return false;
163  }
164 
165  // printf("Expect next event of size %u\n", (unsigned) hdr->GetPaddedSize());
166 
167  readsz = io->fread((char*) ptr + sizeof(hadaq::HadTu), 1, hdr->GetPaddedSize() - sizeof(hadaq::HadTu), fd);
168 
169  // printf("Read size %u expects %u \n", (unsigned) readsz, (unsigned) (hdr->GetPaddedSize() - sizeof(hadaq::HadTu)));
170 
171  // not possible to read event completely
172  if ( readsz != (hdr->GetPaddedSize() - sizeof(hadaq::HadTu))) {
173  fprintf(stderr, "Reading problem\n");
174  fEOF = true;
175  return false;
176  }
177 
178  if ((hdr->GetPaddedSize() == sizeof(hadaq::RawEvent)) && (((hadaq::RawEvent*)hdr)->GetId() == EvtId_runStop)) {
179  // we are not deliver such stop event to the top
180 
181  // printf("Find stop event at the file end\n");
182  fEOF = true;
183  return false;
184  }
185 
186  *sz = hdr->GetPaddedSize();
187  return true;
188  }
189 
190 
191  size_t checkedsz = 0;
192 
193  while (checkedsz < readsz) {
194  // special case when event was read not completely
195  // or we want to provide only event
196 
197  size_t restsize = readsz - checkedsz;
198  if (restsize >= sizeof(hadaq::HadTu)) restsize = hdr->GetPaddedSize();
199 
200 // printf("tu padded size = %u\n", (unsigned) restsize);
201 
202  if ((restsize == sizeof(hadaq::RawEvent)) && (((hadaq::RawEvent*)hdr)->GetId() == EvtId_runStop)) {
203  // we are not deliver such stop event to the top
204 
205  // printf("Find stop event at the file end\n");
206  fEOF = true;
207  break;
208  }
209 
210  bool not_enough_place_for_next_event = (checkedsz + restsize) > readsz;
211 
212  if (not_enough_place_for_next_event || (onlyevent && (checkedsz>0))) {
213 
214  if (not_enough_place_for_next_event && (readsz<maxsz)) fEOF = true;
215 
216  // return file pointer to the begin of event
217  io->fseek(fd, -(readsz - checkedsz), true);
218 
219  break;
220  }
221  checkedsz += hdr->GetPaddedSize();
222  hdr = (hadaq::HadTu*) ((char*) hdr + hdr->GetPaddedSize());
223  }
224 
225  // detect end of file by such method
226  if ((readsz<maxsz) && (checkedsz == readsz) && !fEOF) fEOF = true;
227 
228  *sz = checkedsz;
229 
230 // DOUT0("Return size %u", (unsigned) checkedsz);
231 
232  return checkedsz>0;
233 }
bool ReadBuffer(void *ptr, uint32_t *bufsize, bool onlyevent=false)
Read one or several elements to provided user buffer When called, bufsize should has available buffer...
Definition: HldFile.cxx:139
bool WriteBuffer(void *buf, uint32_t bufsize)
Write user buffer to file without reformatting User must be aware about correct formatting of data.
Definition: HldFile.cxx:126
void Close()
Close file.
Definition: HldFile.cxx:108
bool OpenRead(const char *fname, const char *opt=0)
Opened file for reading.
Definition: HldFile.cxx:63
HldFile()
flag indicate that end-of-file was reached
Definition: HldFile.cxx:19
bool OpenWrite(const char *fname, uint32_t rid=0, const char *opt=0)
Open file with specified name for writing.
Definition: HldFile.cxx:31
#define DOUT3(args ...)
Definition: logging.h:176
Event manipulation API.
Definition: api.h:23
@ EvtId_runStop
Definition: defines.h:122
@ EvtId_runStart
Definition: defines.h:121
uint32_t GetId() const
Definition: defines.h:208
HADES transport unit header.
Definition: defines.h:138
uint32_t GetPaddedSize() const
Definition: defines.h:184
Hadaq event structure.
Definition: defines.h:443
void Init(uint32_t evnt, uint32_t run=0, uint32_t id=EvtId_DABC)
Definition: defines.h:472
int32_t GetRunNr() const
Definition: defines.h:463