2 #include "artdaq/DAQdata/Globals.hh"
3 #include "artdaq/ArtModules/detail/RawEventQueueReader.hh"
5 #include "art/Framework/IO/Sources/put_product_in_principal.h"
6 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
7 #include "canvas/Utilities/Exception.h"
8 #include "artdaq-core/Data/Fragment.hh"
10 #define TRACE_NAME "RawEventQueueReader"
16 art::ProductRegistryHelper& help,
17 art::SourceHelper
const& pm) :
19 , incoming_events(getGlobalQueue())
20 , waiting_time(ps.get<double>(
"waiting_time", 86400.0))
21 , resume_after_timeout(ps.get<bool>(
"resume_after_timeout", true))
22 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
23 , unidentified_instance_name(
"unidentified")
24 , shutdownMsgReceived(false)
25 , outputFileCloseNeeded(false)
27 , fragment_type_map_(Fragment::MakeSystemTypeMap())
43 fb =
new art::FileBlock(art::FileFormatVersion(1,
"RawEvent2011"),
"nothing");
47 art::SubRunPrincipal*
const & inSR,
48 art::RunPrincipal*& outR,
49 art::SubRunPrincipal*& outSR,
50 art::EventPrincipal*& outE)
56 if (readNext_calls_++ == 0)
58 incoming_events.setReaderIsReady();
59 TRACE(50,
"RawEventQueueReader::readNext after incoming_events.setReaderIsReady()");
65 RawEvent_ptr popped_event;
71 bool keep_looping =
true;
72 bool got_event =
false;
76 got_event = incoming_events.deqTimedWait(popped_event, waiting_time);
79 TLOG_INFO(
"RawEventQueueReader")
80 <<
"InputFailure: Reading timed out in RawEventQueueReader::readNext()" << TLOG_ENDL;
81 keep_looping = resume_after_timeout;
89 if (!got_event || !popped_event)
91 TLOG_DEBUG(
"RawEventQueueReader") <<
"Received shutdown message, returning false" << TLOG_ENDL;
92 shutdownMsgReceived =
true;
96 size_t qsize=incoming_events.size();
101 art::Timestamp currentTime = time(0);
104 if (inR == 0 || inR->run() != popped_event->runID())
106 outR = pmaker.makeRunPrincipal(popped_event->runID(),
110 if (popped_event->numFragments() == 1)
112 if (popped_event->releaseProduct(Fragment::EndOfRunFragmentType)->size() == 1)
114 art::EventID
const evid(art::EventID::flushEvent());
115 outR = pmaker.makeRunPrincipal(evid.runID(), currentTime);
116 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
117 outE = pmaker.makeEventPrincipal(evid, currentTime);
120 else if (popped_event->releaseProduct(Fragment::EndOfSubrunFragmentType)->size() == 1)
123 if (inR == 0 || inR->run() != popped_event->runID())
125 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
126 popped_event->subrunID(),
128 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
129 art::EventID
const evid(art::EventID::flushEvent(outR->id(), outSR->id()));
131 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
133 outE = pmaker.makeEventPrincipal(evid, currentTime);
141 if (inSR != 0 && !inSR->id().isFlush() && inSR->subRun() == popped_event->subrunID())
143 art::EventID
const evid(art::EventID::flushEvent(inR->id()));
144 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
145 outE = pmaker.makeEventPrincipal(evid, currentTime);
152 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
153 popped_event->subrunID(),
155 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
156 art::EventID
const evid(art::EventID::flushEvent(inR->id(), outSR->id()));
158 art::EventID
const evid(art::EventID::flushEvent(outSR->id()));
160 outE = pmaker.makeEventPrincipal(evid, currentTime);
172 art::SubRunID subrun_check(popped_event->runID(), popped_event->subrunID());
173 if (inSR == 0 || subrun_check != inSR->id())
175 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
176 popped_event->subrunID(),
179 outE = pmaker.makeEventPrincipal(popped_event->runID(),
180 popped_event->subrunID(),
181 popped_event->sequenceID(),
184 std::vector<Fragment::type_t> type_list;
185 popped_event->fragmentTypes(type_list);
187 std::map<Fragment::type_t, std::string>::const_iterator iter_end =
188 fragment_type_map_.end();
189 for (
size_t idx = 0; idx < type_list.size(); ++idx)
191 std::map<Fragment::type_t, std::string>::const_iterator iter =
192 fragment_type_map_.find(type_list[idx]);
193 auto product = popped_event->releaseProduct(type_list[idx]);
194 for (
auto &frag : *product)
195 bytesRead += frag.sizeBytes();
196 if (iter != iter_end)
198 put_product_in_principal(std::move(product),
205 put_product_in_principal(std::move(product),
208 unidentified_instance_name);
209 TLOG_WARNING(
"RawEventQueueReader")
210 <<
"UnknownFragmentType: The product instance name mapping for fragment type \""
211 << ((int)type_list[idx]) <<
"\" is not known. Fragments of this "
212 <<
"type will be stored in the event with an instance name of \""
213 << unidentified_instance_name <<
"\"." << TLOG_ENDL;
216 TRACE( 10,
"readNext: bytesRead=%lu qsize=%zu cap=%zu metricMan=%p", bytesRead, qsize, incoming_events.capacity(), (
void*)metricMan );
218 metricMan->sendMetric(
"bytesRead", bytesRead>>20,
"MB", 5,
false,
"",
true );
219 metricMan->sendMetric(
"queue%Used", static_cast<unsigned long int>(qsize*100/incoming_events.capacity()),
"%", 5,
false,
"",
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.