2 #include "artdaq/DAQdata/Globals.hh"
3 #include "artdaq/ArtModules/detail/RawEventQueueReader.hh"
4 #include "artdaq-core/Data/ContainerFragment.hh"
6 #include "art/Framework/IO/Sources/put_product_in_principal.h"
7 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
8 #include "canvas/Utilities/Exception.h"
9 #include "artdaq-core/Data/Fragment.hh"
11 #define TRACE_NAME "RawEventQueueReader"
17 art::ProductRegistryHelper& help,
18 art::SourceHelper
const& pm) :
20 , incoming_events(getGlobalQueue())
21 , waiting_time(ps.get<double>(
"waiting_time", 86400.0))
22 , resume_after_timeout(ps.get<bool>(
"resume_after_timeout", true))
23 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
24 , unidentified_instance_name(
"unidentified")
25 , shutdownMsgReceived(false)
26 , outputFileCloseNeeded(false)
28 , fragment_type_map_(Fragment::MakeSystemTypeMap())
45 fb =
new art::FileBlock(art::FileFormatVersion(1,
"RawEvent2011"),
"nothing");
49 art::SubRunPrincipal*
const & inSR,
50 art::RunPrincipal*& outR,
51 art::SubRunPrincipal*& outSR,
52 art::EventPrincipal*& outE)
58 if (readNext_calls_++ == 0)
60 incoming_events.setReaderIsReady();
61 TRACE(50,
"RawEventQueueReader::readNext after incoming_events.setReaderIsReady()");
67 RawEvent_ptr popped_event;
73 bool keep_looping =
true;
74 bool got_event =
false;
78 got_event = incoming_events.deqTimedWait(popped_event, waiting_time);
81 TLOG_INFO(
"RawEventQueueReader")
82 <<
"InputFailure: Reading timed out in RawEventQueueReader::readNext()" << TLOG_ENDL;
83 keep_looping = resume_after_timeout;
91 if (!got_event || !popped_event)
93 TLOG_DEBUG(
"RawEventQueueReader") <<
"Received shutdown message, returning false" << TLOG_ENDL;
94 shutdownMsgReceived =
true;
98 size_t qsize=incoming_events.size();
103 art::Timestamp currentTime = time(0);
106 if (inR == 0 || inR->run() != popped_event->runID())
108 outR = pmaker.makeRunPrincipal(popped_event->runID(),
112 if (popped_event->numFragments() == 1)
114 if (popped_event->releaseProduct(Fragment::EndOfRunFragmentType)->size() == 1)
116 art::EventID
const evid(art::EventID::flushEvent());
117 outR = pmaker.makeRunPrincipal(evid.runID(), currentTime);
118 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
119 outE = pmaker.makeEventPrincipal(evid, currentTime);
122 else if (popped_event->releaseProduct(Fragment::EndOfSubrunFragmentType)->size() == 1)
125 if (inR == 0 || inR->run() != popped_event->runID())
127 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
128 popped_event->subrunID(),
130 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
131 art::EventID
const evid(art::EventID::flushEvent(outR->id(), outSR->id()));
133 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
135 outE = pmaker.makeEventPrincipal(evid, currentTime);
143 if (inSR != 0 && !inSR->id().isFlush() && inSR->subRun() == popped_event->subrunID())
145 art::EventID
const evid(art::EventID::flushEvent(inR->id()));
146 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
147 outE = pmaker.makeEventPrincipal(evid, currentTime);
154 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
155 popped_event->subrunID(),
157 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
158 art::EventID
const evid(art::EventID::flushEvent(inR->id(), outSR->id()));
160 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
162 outE = pmaker.makeEventPrincipal(evid, currentTime);
174 art::SubRunID subrun_check(popped_event->runID(), popped_event->subrunID());
175 if (inSR == 0 || subrun_check != inSR->id())
177 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
178 popped_event->subrunID(),
181 outE = pmaker.makeEventPrincipal(popped_event->runID(),
182 popped_event->subrunID(),
183 popped_event->sequenceID(),
186 std::vector<Fragment::type_t> type_list;
187 popped_event->fragmentTypes(type_list);
189 std::map<Fragment::type_t, std::string>::const_iterator iter_end =
190 fragment_type_map_.end();
191 for (
size_t idx = 0; idx < type_list.size(); ++idx)
193 std::map<Fragment::type_t, std::string>::const_iterator iter =
194 fragment_type_map_.find(type_list[idx]);
195 auto product = popped_event->releaseProduct(type_list[idx]);
196 for (
auto &frag : *product)
197 bytesRead += frag.sizeBytes();
198 if (iter != iter_end)
200 if (type_list[idx] == artdaq::Fragment::ContainerFragmentType)
202 std::unordered_map<std::string, std::unique_ptr<Fragments>> derived_fragments;
203 derived_fragments[iter->second] = std::make_unique<Fragments>();
205 for(
size_t ii = 0; ii < product->size(); ++ii)
207 ContainerFragment cf(product->at(ii));
208 auto contained_type = fragment_type_map_.find(cf.fragment_type());
209 if (contained_type != iter_end)
211 auto label = iter->second + contained_type->second;
212 if (!derived_fragments.count(label))
214 derived_fragments[label] = std::make_unique<Fragments>();
216 derived_fragments[label]->emplace_back(std::move(product->at(ii)));
220 derived_fragments[iter->second]->emplace_back(std::move(product->at(ii)));
224 for (
auto& type : derived_fragments)
226 put_product_in_principal(std::move(type.second),
235 put_product_in_principal(std::move(product),
243 put_product_in_principal(std::move(product),
246 unidentified_instance_name);
247 TLOG_WARNING(
"RawEventQueueReader")
248 <<
"UnknownFragmentType: The product instance name mapping for fragment type \""
249 << ((int)type_list[idx]) <<
"\" is not known. Fragments of this "
250 <<
"type will be stored in the event with an instance name of \""
251 << unidentified_instance_name <<
"\"." << TLOG_ENDL;
254 TRACE( 10,
"readNext: bytesRead=%lu qsize=%zu cap=%zu metricMan=%p", bytesRead, qsize, incoming_events.capacity(), (
void*)metricMan );
256 metricMan->sendMetric(
"bytesRead", bytesRead>>20,
"MB", 5, MetricMode::Accumulate,
"",
true );
257 metricMan->sendMetric(
"queue%Used", static_cast<unsigned long int>(qsize*100/incoming_events.capacity()),
"%", 5, MetricMode::LastPoint,
"",
true );
std::string pretend_module_name
The module name to store data under.
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.
RawEventQueueReader(RawEventQueueReader const &)=delete
Copy Constructor is deleted.
std::string unidentified_instance_name
The name to use for unknown Fragment types.
void readFile(std::string const &, art::FileBlock *&fb)
Emulate opening a file.
void closeCurrentFile()
Emulate closing a file. No-Op.
std::map< Fragment::type_t, std::string > fragment_type_map_
The Fragment type names that this RawEventQueueReader knows about.