00001
00002 #include "artdaq/DAQdata/Globals.hh"
00003 #include "artdaq/ArtModules/detail/RawEventQueueReader.hh"
00004
00005 #include "art/Framework/IO/Sources/put_product_in_principal.h"
00006 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
00007 #include "canvas/Utilities/Exception.h"
00008 #include "artdaq-core/Data/Fragment.hh"
00009 #include <sys/time.h>
00010 #define TRACE_NAME "RawEventQueueReader"
00011 #include "trace.h"
00012
00013 using std::string;
00014
00015 artdaq::detail::RawEventQueueReader::RawEventQueueReader(fhicl::ParameterSet const& ps,
00016 art::ProductRegistryHelper& help,
00017 art::SourceHelper const& pm) :
00018 pmaker(pm)
00019 , incoming_events(getGlobalQueue())
00020 , waiting_time(ps.get<double>("waiting_time", 86400.0))
00021 , resume_after_timeout(ps.get<bool>("resume_after_timeout", true))
00022 , pretend_module_name(ps.get<std::string>("raw_data_label","daq"))
00023 , unidentified_instance_name("unidentified")
00024 , shutdownMsgReceived(false)
00025 , outputFileCloseNeeded(false)
00026 , bytesRead(0)
00027 , fragment_type_map_(Fragment::MakeSystemTypeMap())
00028 , readNext_calls_(0)
00029 {
00030 help.reconstitutes<Fragments, art::InEvent>(pretend_module_name,
00031 unidentified_instance_name);
00032 for (auto it = fragment_type_map_.begin(); it != fragment_type_map_.end(); ++it)
00033 {
00034 help.reconstitutes<Fragments, art::InEvent>(pretend_module_name, it->second);
00035 }
00036 }
00037
00038 void artdaq::detail::RawEventQueueReader::closeCurrentFile() {}
00039
00040 void artdaq::detail::RawEventQueueReader::readFile(string const&,
00041 art::FileBlock*& fb)
00042 {
00043 fb = new art::FileBlock(art::FileFormatVersion(1, "RawEvent2011"), "nothing");
00044 }
00045
00046 bool artdaq::detail::RawEventQueueReader::readNext(art::RunPrincipal* const & inR,
00047 art::SubRunPrincipal* const & inSR,
00048 art::RunPrincipal*& outR,
00049 art::SubRunPrincipal*& outSR,
00050 art::EventPrincipal*& outE)
00051 {
00052
00053
00054
00055
00056 if (readNext_calls_++ == 0)
00057 {
00058 incoming_events.setReaderIsReady();
00059 TRACE(50, "RawEventQueueReader::readNext after incoming_events.setReaderIsReady()");
00060 }
00061
00062 outR = 0;
00063 outSR = 0;
00064 outE = 0;
00065 RawEvent_ptr popped_event;
00066
00067
00068
00069
00070
00071 bool keep_looping = true;
00072 bool got_event = false;
00073 while (keep_looping)
00074 {
00075 keep_looping = false;
00076 got_event = incoming_events.deqTimedWait(popped_event, waiting_time);
00077 if (!got_event)
00078 {
00079 TLOG_INFO("RawEventQueueReader")
00080 << "InputFailure: Reading timed out in RawEventQueueReader::readNext()" << TLOG_ENDL;
00081 keep_looping = resume_after_timeout;
00082 }
00083 }
00084
00085
00086
00087
00088
00089 if (!got_event || !popped_event)
00090 {
00091 TLOG_DEBUG("RawEventQueueReader") << "Received shutdown message, returning false" << TLOG_ENDL;
00092 shutdownMsgReceived = true;
00093 return false;
00094 }
00095
00096
00097
00098
00099 art::Timestamp currentTime = time(0);
00100
00101
00102 if (inR == 0 || inR->run() != popped_event->runID())
00103 {
00104 outR = pmaker.makeRunPrincipal(popped_event->runID(),
00105 currentTime);
00106 }
00107
00108 if (popped_event->numFragments() == 1)
00109 {
00110 if (popped_event->releaseProduct(Fragment::EndOfRunFragmentType)->size() == 1)
00111 {
00112 art::EventID const evid(art::EventID::flushEvent());
00113 outR = pmaker.makeRunPrincipal(evid.runID(), currentTime);
00114 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
00115 outE = pmaker.makeEventPrincipal(evid, currentTime);
00116 return true;
00117 }
00118 else if (popped_event->releaseProduct(Fragment::EndOfSubrunFragmentType)->size() == 1)
00119 {
00120
00121 if (inR == 0 || inR->run() != popped_event->runID())
00122 {
00123 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
00124 popped_event->subrunID(),
00125 currentTime);
00126 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
00127 art::EventID const evid(art::EventID::flushEvent(outR->id(), outSR->id()));
00128 #else
00129 art::EventID const evid(art::EventID::flushEvent(outSR->id()));
00130 #endif
00131 outE = pmaker.makeEventPrincipal(evid, currentTime);
00132 }
00133 else
00134 {
00135
00136
00137
00138
00139 if (inSR != 0 && !inSR->id().isFlush() && inSR->subRun() == popped_event->subrunID())
00140 {
00141 art::EventID const evid(art::EventID::flushEvent(inR->id()));
00142 outSR = pmaker.makeSubRunPrincipal(evid.subRunID(), currentTime);
00143 outE = pmaker.makeEventPrincipal(evid, currentTime);
00144
00145
00146
00147 }
00148 else
00149 {
00150 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
00151 popped_event->subrunID(),
00152 currentTime);
00153 #ifdef ARTDAQ_ART_EVENTID_HAS_EXPLICIT_RUNID
00154 art::EventID const evid(art::EventID::flushEvent(inR->id(), outSR->id()));
00155 #else
00156 art::EventID const evid(art::EventID::flushEvent(outSR->id()));
00157 #endif
00158 outE = pmaker.makeEventPrincipal(evid, currentTime);
00159
00160
00161 }
00162 outR = 0;
00163 }
00164
00165 return true;
00166 }
00167 }
00168
00169
00170 art::SubRunID subrun_check(popped_event->runID(), popped_event->subrunID());
00171 if (inSR == 0 || subrun_check != inSR->id())
00172 {
00173 outSR = pmaker.makeSubRunPrincipal(popped_event->runID(),
00174 popped_event->subrunID(),
00175 currentTime);
00176 }
00177 outE = pmaker.makeEventPrincipal(popped_event->runID(),
00178 popped_event->subrunID(),
00179 popped_event->sequenceID(),
00180 currentTime);
00181
00182 std::vector<Fragment::type_t> type_list;
00183 popped_event->fragmentTypes(type_list);
00184
00185 std::map<Fragment::type_t, std::string>::const_iterator iter_end =
00186 fragment_type_map_.end();
00187 for (size_t idx = 0; idx < type_list.size(); ++idx)
00188 {
00189 std::map<Fragment::type_t, std::string>::const_iterator iter =
00190 fragment_type_map_.find(type_list[idx]);
00191 auto product = popped_event->releaseProduct(type_list[idx]);
00192 for (auto &frag : *product)
00193 bytesRead += frag.sizeBytes();
00194 if (iter != iter_end)
00195 {
00196 put_product_in_principal(std::move(product),
00197 *outE,
00198 pretend_module_name,
00199 iter->second);
00200 }
00201 else
00202 {
00203 put_product_in_principal(std::move(product),
00204 *outE,
00205 pretend_module_name,
00206 unidentified_instance_name);
00207 TLOG_WARNING("RawEventQueueReader")
00208 << "UnknownFragmentType: The product instance name mapping for fragment type \""
00209 << ((int)type_list[idx]) << "\" is not known. Fragments of this "
00210 << "type will be stored in the event with an instance name of \""
00211 << unidentified_instance_name << "\"." << TLOG_ENDL;
00212 }
00213 }
00214 TRACE( 10, "readNext: bytesRead=%lu metricMan=%p", bytesRead, (void*)metricMan );
00215 if (metricMan)
00216 metricMan->sendMetric( "bytesRead", bytesRead>>20, "MB", 5, false, "", true );
00217
00218 return true;
00219 }