artdaq  v3_08_00
BinaryFileOutput_module.cc
1 
2 
3 #define TRACE_NAME "BinaryFileOutput"
4 
5 #include "art/Framework/Core/ModuleMacros.h"
6 #include "art/Framework/Core/OutputModule.h"
7 #include "art/Framework/IO/FileStatsCollector.h"
8 #include "art/Framework/IO/PostCloseFileRenamer.h"
9 #include "art/Framework/Principal/EventPrincipal.h"
10 #include "art/Framework/Principal/Handle.h"
11 #include "art/Framework/Principal/RunPrincipal.h"
12 #include "art/Framework/Principal/SubRunPrincipal.h"
13 #include "art/Persistency/Common/GroupQueryResult.h"
14 #include "canvas/Utilities/DebugMacros.h"
15 #include "canvas/Utilities/Exception.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
18 #include "artdaq-core/Data/Fragment.hh"
19 #include "artdaq/DAQdata/Globals.hh"
20 
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fstream>
24 #include <iomanip>
25 #include <iostream>
26 #include <memory>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace art {
32 class BinaryFileOutput;
33 }
34 
36 using fhicl::ParameterSet;
37 
41 class art::BinaryFileOutput final : public OutputModule
42 {
43 public:
55  explicit BinaryFileOutput(ParameterSet const& ps);
56 
60  virtual ~BinaryFileOutput();
61 
62 private:
63  void beginJob() override;
64 
65  void endJob() override;
66 
67  void write(EventPrincipal&) override;
68 
69  void writeRun(RunPrincipal&) override{};
70  void writeSubRun(SubRunPrincipal&) override{};
71 
72  void initialize_FILE_();
73 
74  void deinitialize_FILE_();
75 
76  bool readParameterSet_(fhicl::ParameterSet const& pset);
77 
78 private:
79  std::string name_ = "BinaryFileOutput";
80  std::string file_name_ = "/tmp/artdaqdemo.binary";
81  bool do_direct_ = false;
82  int fd_ = -1; // Used for direct IO
83  std::unique_ptr<std::ofstream> file_ptr_ = {nullptr};
84  art::FileStatsCollector fstats_;
85 };
86 
88  : OutputModule(ps), fstats_{name_, processName()}
89 {
90  TLOG(TLVL_DEBUG) << "Begin: BinaryFileOutput::BinaryFileOutput(ParameterSet const& ps)\n";
91  readParameterSet_(ps);
92  TLOG(TLVL_DEBUG) << "End: BinaryFileOutput::BinaryFileOutput(ParameterSet const& ps)\n";
93 }
94 
95 art::BinaryFileOutput::~BinaryFileOutput() { TLOG(TLVL_DEBUG) << "Begin/End: BinaryFileOutput::~BinaryFileOutput()\n"; }
96 
97 void art::BinaryFileOutput::beginJob()
98 {
99  TLOG(TLVL_DEBUG) << "Begin: BinaryFileOutput::beginJob()\n";
100  initialize_FILE_();
101  TLOG(TLVL_DEBUG) << "End: BinaryFileOutput::beginJob()\n";
102 }
103 
104 void art::BinaryFileOutput::endJob()
105 {
106  TLOG(TLVL_DEBUG) << "Begin: BinaryFileOutput::endJob()\n";
107  deinitialize_FILE_();
108  TLOG(TLVL_DEBUG) << "End: BinaryFileOutput::endJob()\n";
109 }
110 
111 void art::BinaryFileOutput::initialize_FILE_()
112 {
113  std::string file_name = PostCloseFileRenamer{fstats_}.applySubstitutions(file_name_);
114  if (do_direct_)
115  {
116  fd_ = open(file_name.c_str(), O_WRONLY | O_CREAT | O_DIRECT, 0660);
117  TLOG(TLVL_TRACE) << "initialize_FILE_ fd_=" << fd_;
118  }
119  else
120  {
121  file_ptr_ = std::make_unique<std::ofstream>(file_name, std::ofstream::binary);
122  TLOG(TLVL_TRACE) << "BinaryFileOutput::initialize_FILE_ file_ptr_=" << (void*)file_ptr_.get() << " errno=" << errno;
123  //file_ptr_->rdbuf()->pubsetbuf(0, 0);
124  }
125  fstats_.recordFileOpen();
126 }
127 
128 void art::BinaryFileOutput::deinitialize_FILE_()
129 {
130  if (do_direct_)
131  {
132  close(fd_);
133  fd_ = -1;
134  }
135  else
136  file_ptr_.reset(nullptr);
137  fstats_.recordFileClose();
138 }
139 
140 bool art::BinaryFileOutput::readParameterSet_(fhicl::ParameterSet const& pset)
141 {
142  TLOG(TLVL_DEBUG) << name_ << "BinaryFileOutput::readParameterSet_ method called with "
143  << "ParameterSet = \"" << pset.to_string() << "\".";
144  // determine the data sending parameters
145  try
146  {
147  file_name_ = pset.get<std::string>("fileName");
148  }
149  catch (...)
150  {
151  TLOG(TLVL_ERROR) << name_ << "The fileName parameter was not specified "
152  << "in the BinaryFileOutput initialization PSet: \"" << pset.to_string() << "\".";
153  return false;
154  }
155  do_direct_ = pset.get<bool>("directIO", false);
156  // determine the data sending parameters
157  return true;
158 }
159 
160 #define USE_STATIC_BUFFER 0
161 #if USE_STATIC_BUFFER == 1
162 static unsigned char static_buffer[0xa00000];
163 #endif
164 
165 void art::BinaryFileOutput::write(EventPrincipal& ep)
166 {
167  using RawEvent = artdaq::Fragments;
168  using RawEvents = std::vector<RawEvent>;
169  using RawEventHandle = art::Handle<RawEvent>;
170  using RawEventHandles = std::vector<RawEventHandle>;
171 
172  auto result_handles = std::vector<art::GroupQueryResult>();
173  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
174 #if ART_HEX_VERSION >= 0x30000
175  ModuleContext const mc{moduleDescription()};
176  ProcessTag const processTag{"", mc.moduleDescription().processName()};
177 
178  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
179 #else
180  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
181 #endif
182 
183  for (auto const& result_handle : result_handles)
184  {
185  auto const raw_event_handle = RawEventHandle(result_handle);
186 
187  if (!raw_event_handle.isValid()) continue;
188 
189  for (auto const& fragment : *raw_event_handle)
190  {
191  auto sequence_id = fragment.sequenceID();
192  auto fragid_id = fragment.fragmentID();
193  TLOG(TLVL_TRACE) << "BinaryFileOutput::write seq=" << sequence_id << " frag=" << fragid_id << " "
194  << reinterpret_cast<const void*>(fragment.headerBeginBytes()) << " bytes=0x" << std::hex
195  << fragment.sizeBytes() << " start";
196  if (do_direct_)
197  {
198  ssize_t sts = ::write(fd_, reinterpret_cast<const char*>(fragment.headerBeginBytes()), fragment.sizeBytes());
199  TLOG(5) << "BinaryFileOutput::write seq=" << sequence_id << " frag=" << fragid_id << " done sts=" << sts
200  << " errno=" << errno;
201  }
202  else
203  {
204 #if USE_STATIC_BUFFER == 1
205  file_ptr_->write((char*)static_buffer, fragment.sizeBytes());
206 #else
207  file_ptr_->write(reinterpret_cast<const char*>(fragment.headerBeginBytes()), fragment.sizeBytes());
208 #endif
209  TLOG(5) << "BinaryFileOutput::write seq=" << sequence_id << " frag=" << fragid_id << " done errno=" << errno;
210  }
211  }
212  }
213 #if ART_HEX_VERSION < 0x30000
214  fstats_.recordEvent(ep.id());
215 #else
216  fstats_.recordEvent(ep.eventID());
217 #endif
218  return;
219 }
220 
221 DEFINE_ART_MODULE(art::BinaryFileOutput)
virtual ~BinaryFileOutput()
BinaryFileOutput Destructor.
BinaryFileOutput(ParameterSet const &ps)
BinaryFileOutput Constructor.
The BinaryFileOutput module streams art Events to a binary file, bypassing ROOT.