PdspHDFFileReader.hh
Go to the documentation of this file.
1 #ifndef dune_ArtModules_PdspHDFFileReader_hh
2 #define dune_ArtModules_PdspHDFFileReader_hh
3 
4 #include "artdaq-core/Data/ContainerFragment.hh"
5 #include "artdaq-core/Data/Fragment.hh"
6 #include "artdaq-core/Data/RawEvent.hh"
7 #include "artdaq-core/Utilities/ExceptionHandler.hh"
8 #include "artdaq-core/Utilities/TimeUtils.hh"
9 
11 
21 #include "fhiclcpp/ParameterSet.h"
22 
24 
25 #include <sys/time.h>
26 #include <map>
27 #include <string>
28 
29 namespace dune
30 {
31 
32  /**
33  * \brief The PdspHDFFileReader is a class which implements the methods needed by art::Source
34  */
36 {
37  /**
38  * \brief Copy Constructor is deleted
39  */
40  PdspHDFFileReader(PdspHDFFileReader const&) = delete;
41 
42  /**
43  * \brief Copy Assignment operator is deleted
44  * \return PdspHDFFileReader copy
45  */
47 
48  art::SourceHelper const& pmaker; ///< An art::SourceHelper instance
49  std::string pretend_module_name; ///< The module name to store data under
50  size_t bytesRead; ///< running total of number of bytes received
51  unsigned readNext_calls_; ///< The number of times readNext has been called
52  //std::unique_ptr<artdaq::hdf5::FragmentDataset> inputFile_; ///< The Dataset plugin which this input source will be reading from
53  std::unique_ptr<dune::HDFFileReader::HDFFileInfo> hdf_file_;
54  std::list<std::string> unprocessedEventList_;
55 
56  /**
57  * \brief PdspHDFFileReader Constructor
58  * \param ps ParameterSet used for configuring PdspHDFFileReader
59  * \param help art::ProductRegistryHelper which is used to inform art about different Fragment types
60  * \param pm art::SourceHelper used to initalize the SourceHelper member
61  *
62  * \verbatim
63  * PdspHDFFileReader accepts the following Parameters:
64  * "raw_data_label" (Default: "daq"): The label to use for all raw data
65  * "shared_memory_key" (Default: 0xBEE7): The key for the shared memory segment
66  * \endverbatim
67  */
70  art::SourceHelper const& pm)
71  : pmaker(pm)
72  , pretend_module_name(ps.get<std::string>("raw_data_label", "daq"))
73  , bytesRead(0)
74  , readNext_calls_(0)
75  {
76  TLOG_INFO("PdspHDFFileReader") << "PdspHDFFileReader initializing with ParameterSet: " << ps.to_string();
77 
78  std::set<std::string> instance_names = {"FELIX", "PHOTON"};
79  for (const auto& set_iter : instance_names)
80  {
82  }
83  hdf_file_ = dune::HDFFileReader::openFile(ps.get<std::string>("fileName"));
84  unprocessedEventList_ = dune::HDFFileReader::getTopLevelGroupNames(hdf_file_);
85 
86  TLOG_INFO("PdspHDFFileReader") << "PdspHDFFileReader opened HDF file with run number " << hdf_file_->runNumber
87  << " and " << unprocessedEventList_.size() << " events";
88  }
89 
90  /**
91  * \brief PdspHDFFileReader destructor
92  */
94  {
96  }
97 
98  /**
99  * \brief Emulate closing a file. No-Op.
100  */
102 
103  /**
104  * \brief Emulate opening a file
105  * \param[out] fb art::FileBlock object
106  */
108  {
109  TLOG_ARB(5, "PdspHDFFileReader") << "readFile enter/start";
110  fb = new art::FileBlock(art::FileFormatVersion(1, "RawEvent2011"), "nothing");
111  }
112 
113  /**
114  * \brief Dequeue a RawEvent and declare its Fragment contents to art, creating
115  * Run, SubRun, and EventPrincipal objects as necessary
116  * \param[in] inR Input art::RunPrincipal
117  * \param[in] inSR Input art::SubRunPrincipal
118  * \param[out] outR Output art::RunPrincipal
119  * \param[out] outSR Output art::SubRunPrincipal
120  * \param[out] outE Output art::EventPrincipal
121  * \return Whether an event was returned
122  */
123  bool readNext(art::RunPrincipal* const& inR, art::SubRunPrincipal* const& inSR, art::RunPrincipal*& outR,
125  {
126  TLOG_DEBUG("PdspHDFFileReader") << "readNext BEGIN";
127  ++readNext_calls_;
128 
129  // Establish default 'results'
130  outR = 0;
131  outSR = 0;
132  outE = 0;
133 
134  if (unprocessedEventList_.empty()) {return false;}
135 
136  std::string nextEventGroupName = unprocessedEventList_.front();
137  unprocessedEventList_.pop_front();
138 
139  hdf_file_ = dune::HDFFileReader::reopenFile(std::move(hdf_file_));
140 
142  dune::HDFFileReader::getFragmentsForEvent(hdf_file_, nextEventGroupName);
143 
144  if (fragmentMap.size() == 0)
145  {
146  TLOG_ERROR("PdspHDFFileReader") << "No data received, either because of a problem or end of file. Returning false (should exit art)";
147  return false;
148  }
149 
150  artdaq::detail::RawEventHeader evtHeader(hdf_file_->runNumber, 1, readNext_calls_, readNext_calls_, 0);
151 
152  for (auto& subDetName_fragmentList_pair : fragmentMap)
153  {
154  for (auto& frag : *subDetName_fragmentList_pair.second)
155  {
156  TLOG_DEBUG("PdspHDFFileReader") << subDetName_fragmentList_pair.first << " Fragment, " << " type "
157  << ((int) frag.type()) << ", timestamp " << frag.timestamp()
158  << ", sequence_id " << frag.sequenceID()
159  << " fragment_id " << frag.fragmentID();
160  evtHeader.event_id = frag.sequenceID();
161  evtHeader.sequence_id = frag.sequenceID();
162  evtHeader.timestamp = frag.timestamp();
163  }
164  }
165 
166  art::Timestamp currentTime = 0;
167  timespec hi_res_time;
168  int retcode = clock_gettime(CLOCK_REALTIME, &hi_res_time);
169  TLOG_ARB(15, "PdspHDFFileReader") << "hi_res_time tv_sec = " << hi_res_time.tv_sec
170  << " tv_nsec = " << hi_res_time.tv_nsec << " (retcode = " << retcode << ")";
171  if (retcode == 0)
172  {
173  currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) | (hi_res_time.tv_nsec & 0xffffffff);
174  }
175  else
176  {
177  TLOG_ERROR("PdspHDFFileReader")
178  << "Unable to fetch a high-resolution time with clock_gettime for art::Event Timestamp. "
179  << "The art::Event Timestamp will be zero for event " << evtHeader.event_id;
180  }
181 
182  // make new run if inR is 0 or if the run has changed
183  if (inR == 0 || inR->run() != evtHeader.run_id)
184  {
185  outR = pmaker.makeRunPrincipal(evtHeader.run_id, currentTime);
186  }
187 
188  // make new subrun if inSR is 0 or if the subrun has changed
189  art::SubRunID subrun_check(evtHeader.run_id, evtHeader.subrun_id);
190  if (inSR == 0 || subrun_check != inSR->subRunID())
191  {
192  outSR = pmaker.makeSubRunPrincipal(evtHeader.run_id, evtHeader.subrun_id, currentTime);
193  }
194 
195  outE = pmaker.makeEventPrincipal(evtHeader.run_id, evtHeader.subrun_id, evtHeader.event_id, currentTime);
196 
197  for (auto& subDetName_fragmentList_pair : fragmentMap)
198  {
199  put_product_in_principal(std::move(subDetName_fragmentList_pair.second), *outE, pretend_module_name,
200  subDetName_fragmentList_pair.first);
201  }
202 
203 
204 #if 0
205  else
206  {
207  TLOG_TRACE("PdspHDFFileReader") << "EventHeader has Run " << evtHeader.run_id << ", SubRun " << evtHeader.subrun_id << ", Event " << evtHeader.event_id << ", SeqID " << evtHeader.sequence_id << ", IsComplete " << evtHeader.is_complete;
208  }
209 
210 
211  for (auto& fragmentTypePair : eventMap)
212  {
213  auto type_code = fragmentTypePair.first;
214  TLOG_TRACE("PdspHDFFileReader") << "Before GetFragmentsByType call, type is " << (int)type_code;
215  TLOG_TRACE("PdspHDFFileReader") << "After GetFragmentsByType call, number of fragments is " << fragmentTypePair.second->size();
216 
217  std::unordered_map<std::string, std::unique_ptr<artdaq::Fragments>> derived_fragments;
218  for (auto& frag : *fragmentTypePair.second)
219  {
220  bytesRead += frag.sizeBytes();
221 
222  // std::pair<bool, std::string> instance_name_result = translator->GetInstanceNameForFragment(frag);
223  std::string label = instance_name_result.second;
224  if (!instance_name_result.first)
225  {
226  TLOG_WARNING("PdspHDFFileReader")
227  << "UnknownFragmentType: The product instance name mapping for fragment type \"" << ((int)type_code)
228  << "\" is not known. Fragments of this "
229  << "type will be stored in the event with an instance name of \"" << label << "\".";
230  }
231  if (!derived_fragments.count(label))
232  {
233  derived_fragments[label] = std::make_unique<Fragments>();
234  }
235  auto fragSize = frag.size();
236  TLOG_TRACE("PdspHDFFileReader") << "Moving Fragment with size " << fragSize << " from type map (type=" << type_code << ") to derived map label=" << label;
237  derived_fragments[label]->emplace_back(std::move(frag));
238  }
239  TLOG_TRACE("PdspHDFFileReader") << "Placing derived fragments in outE";
240  for (auto& type : derived_fragments)
241  {
242  put_product_in_principal(std::move(type.second), *outE, pretend_module_name, type.first);
243  }
244  }
245 #endif
246  TLOG_TRACE("PdspHDFFileReader") << "After putting fragments in event";
247  TLOG_ARB(10, "PdspHDFFileReader") << "readNext: bytesRead=" << bytesRead;;
248 
249  TLOG_TRACE("PdspHDFFileReader") << "Returning from readNext";
250  return true;
251  }
252 };
253 } // namespace dune
254 
255 #endif /* dune_ArtModules_PdspHDFFileReader_hh */
HDFFileInfoPtr reopenFile(HDFFileInfoPtr oldHdfFileInfoPtr)
The PdspHDFFileReader is a class which implements the methods needed by art::Source.
HDFFileInfoPtr openFile(const std::string &fileName)
RunNumber_t run() const
Definition: Principal.cc:1070
std::string string
Definition: nybbler.cc:12
SubRunPrincipal * makeSubRunPrincipal(SubRunAuxiliary const &subRunAux) const
bool readNext(art::RunPrincipal *const &inR, art::SubRunPrincipal *const &inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
Dequeue a RawEvent and declare its Fragment contents to art, creating Run, SubRun, and EventPrincipal objects as necessary.
STL namespace.
std::string pretend_module_name
The module name to store data under.
std::list< std::string > getTopLevelGroupNames(HDFFileInfoPtr &hdfFileInfoPtr)
RunPrincipal * makeRunPrincipal(RunAuxiliary const &runAux) const
Definition: SourceHelper.cc:92
art::SourceHelper const & pmaker
An art::SourceHelper instance.
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
void closeCurrentFile()
Emulate closing a file. No-Op.
PdspHDFFileReader(PdspHDFFileReader const &)=delete
Copy Constructor is deleted.
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
SubRunID subRunID() const
Definition: Principal.cc:1058
std::list< std::string > unprocessedEventList_
size_t bytesRead
running total of number of bytes received
static constexpr double ps
Definition: Units.h:99
FragmentListsByType getFragmentsForEvent(HDFFileInfoPtr &hdfFileInfoPtr, const std::string &topLevelGroupName)
virtual ~PdspHDFFileReader()
PdspHDFFileReader destructor.
std::unique_ptr< dune::HDFFileReader::HDFFileInfo > hdf_file_
std::enable_if_t<!detail::range_sets_supported(P::branch_type)> put_product_in_principal(std::unique_ptr< T > &&product, P &principal, std::string const &module_label, std::string const &instance_name={})
std::map< std::string, std::unique_ptr< artdaq::Fragments > > FragmentListsByType
EventPrincipal * makeEventPrincipal(EventAuxiliary const &eventAux, std::unique_ptr< History > &&history) const
PdspHDFFileReader(fhicl::ParameterSet const &ps, art::ProductRegistryHelper &help, art::SourceHelper const &pm)
PdspHDFFileReader Constructor.
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void readFile(std::string const &, art::FileBlock *&fb)
Emulate opening a file.
unsigned readNext_calls_
The number of times readNext has been called.
void closeFile(HDFFileInfoPtr hdfFileInfoPtr)
std::vector< Fragment > Fragments
Definition: HDF5Utils.h:57
std::string to_string() const
Definition: ParameterSet.h:153
PdspHDFFileReader & operator=(PdspHDFFileReader const &)=delete
Copy Assignment operator is deleted.