DABC (Data Acquisition Backbone Core)  2.9.9
LmdOutput.cxx
Go to the documentation of this file.
1 // $Id: LmdOutput.cxx 4479 2020-04-15 14:30:52Z 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 #include "mbs/LmdOutput.h"
17 
18 #include <cstring>
19 #include <cstdlib>
20 #include <cstdio>
21 #include <unistd.h>
22 
23 #if defined(__MACH__) /* Apple OSX section */
24 #include <machine/endian.h>
25 #else
26 #include <endian.h>
27 #endif
28 
29 #include "dabc/Manager.h"
30 
31 #include "mbs/Iterator.h"
32 
33 
35  dabc::FileOutput(url, ".lmd"),
36  fFile(),fUrlOptions()
37 {
38  fUrlOptions = url.GetOptions();
39 
40  if (url.HasOption("rfio"))
41  fFile.SetIO((dabc::FileInterface*) dabc::mgr.CreateAny("rfio::FileInterface"), true);
42  else if (url.HasOption("ltsm"))
43  fFile.SetIO((dabc::FileInterface*) dabc::mgr.CreateAny("ltsm::FileInterface"), true);
44 }
45 
47 {
48  DOUT0(" mbs::LmdOutput::DTOR");
49  CloseFile();
50 }
51 
53 {
54  if (!dabc::FileOutput::Write_Init()) return false;
55 
56  return StartNewFile();
57 }
58 
60 {
61  CloseFile();
62 
63  ProduceNewFileName();
64 
65  if (!fFile.OpenWriting(CurrentFileName().c_str(), fUrlOptions.c_str())) {
66  ShowInfo(-1, dabc::format("%s cannot open file for writing", CurrentFileName().c_str()));
67  return false;
68  }
69 
70  ShowInfo(0, dabc::format("Open %s for writing", CurrentFileName().c_str()));
71 
72  return true;
73 }
74 
76 {
77  DOUT0(" mbs::LmdOutput::CloseFile()");
78  if (fFile.isWriting()) {
79  ShowInfo(0, dabc::format("Close file %s", CurrentFileName().c_str()));
80  fFile.Close();
81  }
82  return true;
83 }
84 
86 {
87  if (!fFile.isWriting() || buf.null()) return dabc::do_Error;
88 
89  if (buf.GetTypeId() == dabc::mbt_EOF) {
90  CloseFile();
91  return dabc::do_Close;
92  }
93 
94  if (buf.GetTypeId() != mbs::mbt_MbsEvents) {
95  ShowInfo(-1, dabc::format("Buffer must contain mbs event(s) 10-1, but has type %u", buf.GetTypeId()));
96  return dabc::do_Error;
97  }
98 
99  if (buf.NumSegments()>1) {
100  ShowInfo(-1, "Segmented buffer not (yet) supported");
101  return dabc::do_Error;
102  }
103 
104  if (CheckBufferForNextFile(buf.GetTotalSize()))
105  if (!StartNewFile()) {
106  EOUT("Cannot start new file for writing");
107  return dabc::do_Error;
108  }
109 
110  unsigned numevents = mbs::ReadIterator::NumEvents(buf);
111 
112  for (unsigned n=0;n<buf.NumSegments();n++)
113  if (!fFile.WriteBuffer(buf.SegmentPtr(n), buf.SegmentSize(n))) {
114  EOUT("lmd write error");
115  return dabc::do_Error;
116  }
117 
118  AccountBuffer(buf.GetTotalSize(), numevents);
119 
120  return dabc::do_Ok;
121 }
void SetIO(FileInterface *_io, bool _ioowner=false)
Definition: BinaryFile.h:114
Reference on memory from memory pool.
Definition: Buffer.h:135
unsigned NumSegments() const
Returns number of segment in buffer.
Definition: Buffer.h:163
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
Definition: Buffer.h:174
unsigned GetTypeId() const
Definition: Buffer.h:152
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
Defines and implements basic POSIX file interface.
Definition: BinaryFile.h:33
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
Definition: DataIO.cxx:211
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Uniform Resource Locator interpreter.
Definition: Url.h:33
bool HasOption(const std::string &optname) const
Definition: Url.h:70
std::string GetOptions() const
Definition: Url.h:63
bool CloseFile()
Definition: LmdOutput.cxx:75
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
Definition: LmdOutput.cxx:52
std::string fUrlOptions
Definition: LmdOutput.h:35
LmdOutput(const dabc::Url &url)
Definition: LmdOutput.cxx:34
virtual ~LmdOutput()
Definition: LmdOutput.cxx:46
virtual unsigned Write_Buffer(dabc::Buffer &buf)
Start writing of buffer to output.
Definition: LmdOutput.cxx:85
bool StartNewFile()
Definition: LmdOutput.cxx:59
mbs::LmdFile fFile
Definition: LmdOutput.h:34
static unsigned NumEvents(const dabc::Buffer &buf)
Definition: Iterator.cxx:187
#define DOUT0(args ...)
Definition: logging.h:156
#define EOUT(args ...)
Definition: logging.h:150
Event manipulation API.
Definition: api.h:23
ManagerRef mgr
Definition: Manager.cxx:42
std::string format(const char *fmt,...)
Definition: string.cxx:49
@ do_Ok
Definition: DataIO.h:142
@ do_Close
Definition: DataIO.h:148
@ do_Error
Definition: DataIO.h:147
@ mbt_EOF
Definition: Buffer.h:45
@ mbt_MbsEvents
Definition: MbsTypeDefs.h:348