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 #if ART_HEX_VERSION < 0x20900
14 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
15 #include "canvas/Utilities/GetPassID.h"
17 #include "art/Persistency/Provenance/MasterProductRegistry.h"
18 #include "art/Persistency/Provenance/ProductMetaData.h"
19 #include "canvas/Persistency/Provenance/EventID.h"
20 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
21 #include "canvas/Persistency/Provenance/ModuleDescription.h"
22 #include "canvas/Persistency/Provenance/Parentage.h"
23 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
24 #include "canvas/Persistency/Provenance/RunID.h"
25 #include "canvas/Persistency/Provenance/SubRunID.h"
26 #include "canvas/Persistency/Provenance/Timestamp.h"
27 #include "canvas/Utilities/Exception.h"
28 #include "art/Version/GetReleaseVersion.h"
29 #include "artdaq-core/Data/Fragment.hh"
30 #include "artdaq-core/Utilities/configureMessageFacility.hh"
31 #include "fhiclcpp/make_ParameterSet.h"
33 #define BOOST_TEST_MODULE shared_memory_reader_t
34 #include <boost/test/auto_unit_test.hpp>
54 typedef std::map<std::string, art::BranchKey>
BKmap_t;
68 art::ProcessConfiguration*
70 std::string
const& processName,
71 fhicl::ParameterSet
const& moduleParams,
72 std::string
const& release = art::getReleaseVersion()
73 #
if ART_HEX_VERSION < 0x20800
74 , std::string
const& pass = art::getPassID()
85 std::unique_ptr<art::BranchDescription>
87 std::string
const& processName,
88 std::string
const& productInstanceName = std::string());
102 , processConfigurations_()
118 #if ART_HEX_VERSION < 0x20900
119 #if ART_HEX_VERSION >= 0x20703
128 art::ProcessConfiguration*
131 std::string
const& processName,
132 fhicl::ParameterSet
const& moduleParams,
133 std::string
const& release
134 #
if ART_HEX_VERSION < 0x20800
135 , std::string
const& pass
139 fhicl::ParameterSet processParams;
140 processParams.put(processName, moduleParams);
141 processParams.put<std::string>(
"process_name",
145 std::make_unique<art::ProcessConfiguration>(processName, processParams.id(), release, pass));
146 return emplace_pair.first->second.get();
149 std::unique_ptr<art::BranchDescription>
152 std::string
const& processName,
153 std::string
const& productInstanceName)
155 std::string moduleLabel = processName +
"dummyMod";
156 std::string moduleClass(
"DummyModule");
157 fhicl::ParameterSet modParams;
158 modParams.put<std::string>(
"module_type", moduleClass);
159 modParams.put<std::string>(
"module_label", moduleLabel);
160 art::ProcessConfiguration* process =
162 art::ModuleDescription mod(modParams.id(),
166 art::TypeID dummyType(
typeid(
int));
167 art::BranchDescription* result =
168 new art::BranchDescription(
169 #
if ART_HEX_VERSION >= 0x20703
171 art::TypeLabel(dummyType,
172 productInstanceName),
174 art::TypeLabel(art::InEvent,
176 productInstanceName),
181 branchKeys_.insert(std::make_pair(tag, art::BranchKey(*result)));
182 return std::unique_ptr<art::BranchDescription>(result);
195 static bool once(
true);
198 artdaq::configureMessageFacility(
"shared_memory_reader_t");
200 art::ModuleDescription md(fhicl::ParameterSet().
id(),
203 *
gf().processConfigurations_[
"daq"]);
206 helper().registerProducts(
gf().productRegistry_, md);
228 static art::ProductRegistryHelper s_helper;
238 static std::unique_ptr<art::SourceHelper>
240 if (!s_source_helper)
242 fhicl::ParameterSet sourceParams;
243 std::string moduleType{
"DummySource" };
244 std::string moduleLabel{
"daq" };
245 sourceParams.put<std::string>(
"module_type", moduleType);
246 sourceParams.put<std::string>(
"module_label", moduleLabel);
250 art::ModuleDescription md(sourceParams.id(),
254 s_source_helper = std::make_unique<art::SourceHelper>(md);
256 return *s_source_helper;
265 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t"));
274 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t BROADCAST"));
285 fhicl::ParameterSet pset;
286 pset.put(
"shared_memory_key",
getKey());
288 pset.put(
"max_event_size_bytes", 0x100000);
289 pset.put(
"buffer_count", 10);
294 gf().productRegistry_);
295 static bool reader_initialized =
false;
296 if (!reader_initialized)
299 helper().reconstitutes<artdaq::Fragments, art::InEvent>(
"daq",
"ABCDEF");
300 reader_initialized =
true;
311 fhicl::ParameterSet pset;
312 pset.put(
"shared_memory_key",
getKey());
314 pset.put(
"max_event_size_bytes", 0x100000);
315 pset.put(
"art_analyzer_count", 0);
316 pset.put(
"stale_buffer_timeout_usec", 100000);
317 pset.put(
"expected_fragments_per_event", 1);
318 pset.put(
"buffer_count", 10);
340 std::unique_ptr<art::RunPrincipal>&& run,
341 std::unique_ptr<art::SubRunPrincipal>&& subrun,
342 art::EventID
const& eventid)
344 BOOST_REQUIRE(run || subrun ==
nullptr);
345 std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
347 tmpFrag(artdaq::Fragment::dataFrag(eventid.event(),
351 tmpFrag->setUserType(1);
356 auto iter = tmpFrag->dataBegin();
357 std::ostringstream str;
359 while (iter != tmpFrag->dataEnd())
361 str << std::to_string(*iter) <<
", ";
366 TLOG_DEBUG(
"shared_memory_reader_t") <<
"Fragment to art: "<< str.str() << TLOG_ENDL;
369 artdaq::FragmentPtr tempFrag;
370 auto sts = writer.AddFragment(std::move(tmpFrag), 1000000, tempFrag);
371 BOOST_REQUIRE_EQUAL(sts,
true);
379 art::EventPrincipal* newevent =
nullptr;
380 art::SubRunPrincipal* newsubrun =
nullptr;
381 art::RunPrincipal* newrun =
nullptr;
382 bool rc = reader.
readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
384 if (run.get() && run->run() == eventid.run())
386 BOOST_CHECK(newrun ==
nullptr);
391 BOOST_CHECK(newrun->id() == eventid.runID());
393 if (!newrun && subrun.get() && subrun->subRun() == eventid.subRun())
395 BOOST_CHECK(newsubrun ==
nullptr);
399 BOOST_CHECK(newsubrun);
400 BOOST_CHECK(newsubrun->id() == eventid.subRunID());
402 BOOST_CHECK(newevent);
403 BOOST_CHECK(newevent->id() == eventid);
404 art::Event e(*newevent, art::ModuleDescription());
405 art::Handle<std::vector<artdaq::Fragment>> h;
406 e.getByLabel(
"daq",
"ABCDEF", h);
407 BOOST_CHECK(h.isValid());
408 BOOST_CHECK(h->size() == 1);
410 auto iter2 = h->front().dataBegin();
411 std::ostringstream str2;
413 while(iter2 != h->front().dataEnd())
415 str2 << std::to_string(*iter2) <<
", ";
420 TLOG_DEBUG(
"shared_memory_reader_t") <<
"Fragment from art: " << str2.str() << TLOG_ENDL;
422 BOOST_CHECK(std::equal(fakeData.begin(),
424 h->front().dataBegin()));
433 art::EventID eventid(2112, 1, 3);
435 basic_test(reader(),writer(),
436 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
437 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), eventid.subRun(), now)),
443 art::EventID eventid(2112, 1, 3);
445 basic_test(reader(), writer(),
453 art::EventID eventid(2112, 1, 3);
455 basic_test(reader(), writer(),
456 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
457 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), 0, now)),
463 art::EventID eventid(2112, 1, 3);
465 basic_test(reader(), writer(),
466 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run() - 1, now)),
467 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run() - 1,
478 std::string
const fakeFileName(
"no such file exists");
479 art::FileBlock* pFile =
nullptr;
480 reader().readFile(fakeFileName, pFile);
482 BOOST_CHECK(pFile->fileFormatVersion() == art::FileFormatVersion(1,
"RawEvent2011"));
483 BOOST_CHECK(pFile->tree() ==
nullptr);
485 BOOST_CHECK(!pFile->fastClonable());
489 art::RunID runid(2112);
490 art::SubRunID subrunid(2112, 1);
491 art::EventID eventid(2112, 1, 3);
493 std::unique_ptr<art::RunPrincipal> run(source_helper().makeRunPrincipal(runid.run(), now));
494 std::unique_ptr<art::SubRunPrincipal> subrun(source_helper().makeSubRunPrincipal(runid.run(), subrunid.subRun(), now));
495 std::unique_ptr<art::EventPrincipal> event(source_helper().makeEventPrincipal(runid.run(),
499 writer().endOfData();
500 art::EventPrincipal* newevent =
nullptr;
501 art::SubRunPrincipal* newsubrun =
nullptr;
502 art::RunPrincipal* newrun =
nullptr;
503 bool rc = reader().readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
505 BOOST_CHECK(newrun ==
nullptr);
506 BOOST_CHECK(newsubrun ==
nullptr);
507 BOOST_CHECK(newevent ==
nullptr);
510 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.