1 #include "artdaq/ArtModules/detail/SharedMemoryReader.hh"
2 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
4 #include "art/Framework/Core/FileBlock.h"
6 #include "art/Framework/IO/Sources/SourceHelper.h"
7 #include "art/Framework/Principal/Event.h"
8 #include "art/Framework/Principal/EventPrincipal.h"
9 #include "art/Framework/Principal/Handle.h"
10 #include "art/Framework/Principal/RunPrincipal.h"
11 #include "art/Framework/Principal/SubRunPrincipal.h"
13 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
14 #include "art/Persistency/Provenance/MasterProductRegistry.h"
15 #include "art/Persistency/Provenance/ProductMetaData.h"
16 #include "canvas/Persistency/Provenance/EventID.h"
17 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
18 #include "canvas/Persistency/Provenance/ModuleDescription.h"
19 #include "canvas/Persistency/Provenance/Parentage.h"
20 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
21 #include "canvas/Persistency/Provenance/RunID.h"
22 #include "canvas/Persistency/Provenance/SubRunID.h"
23 #include "canvas/Persistency/Provenance/Timestamp.h"
24 #include "canvas/Utilities/Exception.h"
25 #include "canvas/Utilities/GetPassID.h"
26 #include "art/Version/GetReleaseVersion.h"
27 #include "artdaq-core/Data/Fragment.hh"
28 #include "artdaq-core/Utilities/configureMessageFacility.hh"
29 #include "fhiclcpp/make_ParameterSet.h"
31 #define BOOST_TEST_MODULE shared_memory_reader_t
32 #include <boost/test/auto_unit_test.hpp>
52 typedef std::map<std::string, art::BranchKey>
BKmap_t;
66 art::ProcessConfiguration*
68 std::string
const& processName,
69 fhicl::ParameterSet
const& moduleParams,
70 std::string
const& release = art::getReleaseVersion(),
71 std::string
const& pass = art::getPassID());
80 std::unique_ptr<art::BranchDescription>
82 std::string
const& processName,
83 std::string
const& productInstanceName = std::string());
97 , processConfigurations_()
113 #if ART_HEX_VERSION >= 0x20703
121 art::ProcessConfiguration*
124 std::string
const& processName,
125 fhicl::ParameterSet
const& moduleParams,
126 std::string
const& release,
127 std::string
const& pass)
129 fhicl::ParameterSet processParams;
130 processParams.put(processName, moduleParams);
131 processParams.put<std::string>(
"process_name",
135 std::make_unique<art::ProcessConfiguration>(processName, processParams.id(), release, pass));
136 return emplace_pair.first->second.get();
139 std::unique_ptr<art::BranchDescription>
142 std::string
const& processName,
143 std::string
const& productInstanceName)
145 std::string moduleLabel = processName +
"dummyMod";
146 std::string moduleClass(
"DummyModule");
147 fhicl::ParameterSet modParams;
148 modParams.put<std::string>(
"module_type", moduleClass);
149 modParams.put<std::string>(
"module_label", moduleLabel);
150 art::ProcessConfiguration* process =
152 art::ModuleDescription mod(modParams.id(),
156 art::TypeID dummyType(
typeid(
int));
157 art::BranchDescription* result =
158 new art::BranchDescription(
159 #
if ART_HEX_VERSION >= 0x20703
161 art::TypeLabel(dummyType,
162 productInstanceName),
164 art::TypeLabel(art::InEvent,
166 productInstanceName),
171 branchKeys_.insert(std::make_pair(tag, art::BranchKey(*result)));
172 return std::unique_ptr<art::BranchDescription>(result);
185 static bool once(
true);
188 artdaq::configureMessageFacility(
"shared_memory_reader_t");
190 art::ModuleDescription md(fhicl::ParameterSet().
id(),
193 *
gf().processConfigurations_[
"daq"]);
196 helper().registerProducts(
gf().productRegistry_, md);
218 static art::ProductRegistryHelper s_helper;
228 static std::unique_ptr<art::SourceHelper>
230 if (!s_source_helper)
232 fhicl::ParameterSet sourceParams;
233 std::string moduleType{
"DummySource" };
234 std::string moduleLabel{
"daq" };
235 sourceParams.put<std::string>(
"module_type", moduleType);
236 sourceParams.put<std::string>(
"module_label", moduleLabel);
240 art::ModuleDescription md(sourceParams.id(),
244 s_source_helper = std::make_unique<art::SourceHelper>(md);
246 return *s_source_helper;
255 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t"));
264 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t BROADCAST"));
275 fhicl::ParameterSet pset;
276 pset.put(
"shared_memory_key",
getKey());
278 pset.put(
"max_event_size_bytes", 0x100000);
279 pset.put(
"buffer_count", 10);
284 gf().productRegistry_);
285 static bool reader_initialized =
false;
286 if (!reader_initialized)
289 helper().reconstitutes<artdaq::Fragments, art::InEvent>(
"daq",
"ABCDEF");
290 reader_initialized =
true;
301 fhicl::ParameterSet pset;
302 pset.put(
"shared_memory_key",
getKey());
304 pset.put(
"max_event_size_bytes", 0x100000);
305 pset.put(
"art_analyzer_count", 0);
306 pset.put(
"stale_buffer_timeout_usec", 100000);
307 pset.put(
"expected_fragments_per_event", 1);
308 pset.put(
"buffer_count", 10);
330 std::unique_ptr<art::RunPrincipal>&& run,
331 std::unique_ptr<art::SubRunPrincipal>&& subrun,
332 art::EventID
const& eventid)
334 BOOST_REQUIRE(run || subrun ==
nullptr);
335 std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
337 tmpFrag(artdaq::Fragment::dataFrag(eventid.event(),
341 tmpFrag->setUserType(1);
346 auto iter = tmpFrag->dataBegin();
347 std::ostringstream str;
349 while (iter != tmpFrag->dataEnd())
351 str << std::to_string(*iter) <<
", ";
356 TLOG_DEBUG(
"shared_memory_reader_t") <<
"Fragment to art: "<< str.str() << TLOG_ENDL;
359 artdaq::FragmentPtr tempFrag;
360 auto sts = writer.AddFragment(std::move(tmpFrag), 1000000, tempFrag);
361 BOOST_REQUIRE_EQUAL(sts,
true);
369 art::EventPrincipal* newevent =
nullptr;
370 art::SubRunPrincipal* newsubrun =
nullptr;
371 art::RunPrincipal* newrun =
nullptr;
372 bool rc = reader.
readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
374 if (run.get() && run->run() == eventid.run())
376 BOOST_CHECK(newrun ==
nullptr);
381 BOOST_CHECK(newrun->id() == eventid.runID());
383 if (!newrun && subrun.get() && subrun->subRun() == eventid.subRun())
385 BOOST_CHECK(newsubrun ==
nullptr);
389 BOOST_CHECK(newsubrun);
390 BOOST_CHECK(newsubrun->id() == eventid.subRunID());
392 BOOST_CHECK(newevent);
393 BOOST_CHECK(newevent->id() == eventid);
394 art::Event e(*newevent, art::ModuleDescription());
395 art::Handle<std::vector<artdaq::Fragment>> h;
396 e.getByLabel(
"daq",
"ABCDEF", h);
397 BOOST_CHECK(h.isValid());
398 BOOST_CHECK(h->size() == 1);
400 auto iter2 = h->front().dataBegin();
401 std::ostringstream str2;
403 while(iter2 != h->front().dataEnd())
405 str2 << std::to_string(*iter2) <<
", ";
410 TLOG_DEBUG(
"shared_memory_reader_t") <<
"Fragment from art: " << str2.str() << TLOG_ENDL;
412 BOOST_CHECK(std::equal(fakeData.begin(),
414 h->front().dataBegin()));
423 art::EventID eventid(2112, 1, 3);
425 basic_test(reader(),writer(),
426 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
427 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), eventid.subRun(), now)),
433 art::EventID eventid(2112, 1, 3);
435 basic_test(reader(), writer(),
443 art::EventID eventid(2112, 1, 3);
445 basic_test(reader(), writer(),
446 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
447 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), 0, now)),
453 art::EventID eventid(2112, 1, 3);
455 basic_test(reader(), writer(),
456 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run() - 1, now)),
457 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run() - 1,
468 std::string
const fakeFileName(
"no such file exists");
469 art::FileBlock* pFile =
nullptr;
470 reader().readFile(fakeFileName, pFile);
472 BOOST_CHECK(pFile->fileFormatVersion() == art::FileFormatVersion(1,
"RawEvent2011"));
473 BOOST_CHECK(pFile->tree() ==
nullptr);
475 BOOST_CHECK(!pFile->fastClonable());
479 art::RunID runid(2112);
480 art::SubRunID subrunid(2112, 1);
481 art::EventID eventid(2112, 1, 3);
483 std::unique_ptr<art::RunPrincipal> run(source_helper().makeRunPrincipal(runid.run(), now));
484 std::unique_ptr<art::SubRunPrincipal> subrun(source_helper().makeSubRunPrincipal(runid.run(), subrunid.subRun(), now));
485 std::unique_ptr<art::EventPrincipal> event(source_helper().makeEventPrincipal(runid.run(),
489 writer().endOfData();
490 art::EventPrincipal* newevent =
nullptr;
491 art::SubRunPrincipal* newsubrun =
nullptr;
492 art::RunPrincipal* newrun =
nullptr;
493 bool rc = reader().readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
495 BOOST_CHECK(newrun ==
nullptr);
496 BOOST_CHECK(newsubrun ==
nullptr);
497 BOOST_CHECK(newevent ==
nullptr);
500 BOOST_AUTO_TEST_SUITE_END()
SharedMemoryReader Test Fixture.
std::map< std::string, std::unique_ptr< art::ProcessConfiguration > > processConfigurations_
Configurations.
The SharedMemoryReader is a class which implements the methods needed by art::Source.
void finalize()
Finalizes the ProductRegistry.
size_t GetLockedBufferCount()
Returns the number of buffers currently owned by this manager.
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
ShmRTestFixture()
ShmRTestFixture Constructor.
artdaq::SharedMemoryEventManager & writer()
Get the instance of the SharedMemoryEventManager.
art::MasterProductRegistry productRegistry_
MasterProductRegistry instance.
uint32_t getBroadcastKey()
Gets the key for the broadcast shared memory segment.
art::ProcessConfiguration * fake_single_module_process(std::string const &tag, std::string const &processName, fhicl::ParameterSet const &moduleParams, std::string const &release=art::getReleaseVersion(), std::string const &pass=art::getPassID())
Create the ProcessConfiguration for a single module art process.
void basic_test(artdaq::detail::SharedMemoryReader<> &reader, artdaq::SharedMemoryEventManager &writer, std::unique_ptr< art::RunPrincipal > &&run, std::unique_ptr< art::SubRunPrincipal > &&subrun, art::EventID const &eventid)
Run a basic checkout of the SharedMemoryReader.
std::unique_ptr< art::BranchDescription > fake_single_process_branch(std::string const &tag, std::string const &processName, std::string const &productInstanceName=std::string())
Create a BranchDescription for a process.
MasterProductRegistry Test Fixture.
uint32_t getKey()
Gets the key for the shared memory segment.
void sendMetrics()
Send metrics to the MetricManager, if one has been instantiated in the application.
std::map< std::string, art::BranchKey > BKmap_t
BKmap_t associates a string with a art::BranchKey.
MPRGlobalTestFixture()
MPRGlobalTestFixture Constructor.
artdaq::detail::SharedMemoryReader & reader()
Get an artdaq::detail::SharedMemoryReader object, creating a static instance if necessary.
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.
BKmap_t branchKeys_
Keys in this test fixture.
std::map< Fragment::type_t, std::string > fragment_type_map_
The Fragment type names that this SharedMemoryReader knows about.
MPRGlobalTestFixture & gf()
Get a MPRGlobalTestFixture, creating a static instance if necessary.
art::SourceHelper & source_helper()
Get an art::SourceHelper object, creating a static instance if necessary.
art::ProductRegistryHelper & helper()
Get an art::ProductRegistryHelper, creating a static instance if necessary.
void startRun(run_id_t runID)
Start a Run.