1 #define TRACE_NAME "shared_memory_reader_t"
3 #include "artdaq/ArtModules/detail/SharedMemoryReader.hh"
4 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
6 #include "art/Framework/Core/FileBlock.h"
8 #include "art/Framework/IO/Sources/SourceHelper.h"
9 #include "art/Framework/Principal/Event.h"
10 #include "art/Framework/Principal/EventPrincipal.h"
11 #include "art/Framework/Principal/Handle.h"
12 #include "art/Framework/Principal/RunPrincipal.h"
13 #include "art/Framework/Principal/SubRunPrincipal.h"
15 #if ART_HEX_VERSION < 0x20900
16 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
17 #include "canvas/Utilities/GetPassID.h"
19 #include "art/Persistency/Provenance/MasterProductRegistry.h"
20 #include "art/Persistency/Provenance/ProductMetaData.h"
21 #include "canvas/Persistency/Provenance/EventID.h"
22 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
23 #include "canvas/Persistency/Provenance/ModuleDescription.h"
24 #include "canvas/Persistency/Provenance/Parentage.h"
25 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
26 #include "canvas/Persistency/Provenance/RunID.h"
27 #include "canvas/Persistency/Provenance/SubRunID.h"
28 #include "canvas/Persistency/Provenance/Timestamp.h"
29 #include "canvas/Utilities/Exception.h"
30 #include "art/Version/GetReleaseVersion.h"
31 #include "artdaq-core/Data/Fragment.hh"
32 #include "artdaq-core/Utilities/configureMessageFacility.hh"
33 #include "fhiclcpp/make_ParameterSet.h"
35 #define BOOST_TEST_MODULE shared_memory_reader_t
36 #include <boost/test/auto_unit_test.hpp>
56 typedef std::map<std::string, art::BranchKey>
BKmap_t;
70 art::ProcessConfiguration*
72 std::string
const& processName,
73 fhicl::ParameterSet
const& moduleParams,
74 std::string
const& release = art::getReleaseVersion()
75 #
if ART_HEX_VERSION < 0x20800
76 , std::string
const& pass = art::getPassID()
87 std::unique_ptr<art::BranchDescription>
89 std::string
const& processName,
90 std::string
const& productInstanceName = std::string());
104 , processConfigurations_()
120 #if ART_HEX_VERSION < 0x20900
121 #if ART_HEX_VERSION >= 0x20703
130 art::ProcessConfiguration*
133 std::string
const& processName,
134 fhicl::ParameterSet
const& moduleParams,
135 std::string
const& release
136 #
if ART_HEX_VERSION < 0x20800
137 , std::string
const& pass
141 fhicl::ParameterSet processParams;
142 processParams.put(processName, moduleParams);
143 processParams.put<std::string>(
"process_name",
147 std::make_unique<art::ProcessConfiguration>(processName, processParams.id(), release, pass));
148 return emplace_pair.first->second.get();
151 std::unique_ptr<art::BranchDescription>
154 std::string
const& processName,
155 std::string
const& productInstanceName)
157 std::string moduleLabel = processName +
"dummyMod";
158 std::string moduleClass(
"DummyModule");
159 fhicl::ParameterSet modParams;
160 modParams.put<std::string>(
"module_type", moduleClass);
161 modParams.put<std::string>(
"module_label", moduleLabel);
162 art::ProcessConfiguration* process =
164 art::ModuleDescription mod(modParams.id(),
168 art::TypeID dummyType(
typeid(
int));
169 art::BranchDescription* result =
170 new art::BranchDescription(
171 #
if ART_HEX_VERSION >= 0x20703
173 art::TypeLabel(dummyType,
174 productInstanceName),
176 art::TypeLabel(art::InEvent,
178 productInstanceName),
183 branchKeys_.insert(std::make_pair(tag, art::BranchKey(*result)));
184 return std::unique_ptr<art::BranchDescription>(result);
197 static bool once(
true);
200 artdaq::configureMessageFacility(
"shared_memory_reader_t");
202 art::ModuleDescription md(fhicl::ParameterSet().
id(),
205 *
gf().processConfigurations_[
"daq"]);
208 helper().registerProducts(
gf().productRegistry_, md);
230 static art::ProductRegistryHelper s_helper;
240 static std::unique_ptr<art::SourceHelper>
242 if (!s_source_helper)
244 fhicl::ParameterSet sourceParams;
245 std::string moduleType{
"DummySource" };
246 std::string moduleLabel{
"daq" };
247 sourceParams.put<std::string>(
"module_type", moduleType);
248 sourceParams.put<std::string>(
"module_label", moduleLabel);
252 art::ModuleDescription md(sourceParams.id(),
256 s_source_helper = std::make_unique<art::SourceHelper>(md);
258 return *s_source_helper;
267 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t"));
276 static uint32_t key =
static_cast<uint32_t
>(std::hash<std::string>()(
"shared_memory_reader_t BROADCAST"));
287 fhicl::ParameterSet pset;
288 pset.put(
"shared_memory_key",
getKey());
290 pset.put(
"max_event_size_bytes", 0x100000);
291 pset.put(
"buffer_count", 10);
296 gf().productRegistry_);
297 static bool reader_initialized =
false;
298 if (!reader_initialized)
301 helper().reconstitutes<artdaq::Fragments, art::InEvent>(
"daq",
"ABCDEF");
302 reader_initialized =
true;
313 fhicl::ParameterSet pset;
314 pset.put(
"shared_memory_key",
getKey());
316 pset.put(
"max_event_size_bytes", 0x100000);
317 pset.put(
"art_analyzer_count", 0);
318 pset.put(
"stale_buffer_timeout_usec", 100000);
319 pset.put(
"expected_fragments_per_event", 1);
320 pset.put(
"buffer_count", 10);
342 std::unique_ptr<art::RunPrincipal>&& run,
343 std::unique_ptr<art::SubRunPrincipal>&& subrun,
344 art::EventID
const& eventid)
346 BOOST_REQUIRE(run || subrun ==
nullptr);
347 std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
349 tmpFrag(artdaq::Fragment::dataFrag(eventid.event(),
353 tmpFrag->setUserType(1);
358 auto iter = tmpFrag->dataBegin();
359 std::ostringstream str;
361 while (iter != tmpFrag->dataEnd())
363 str << std::to_string(*iter) <<
", ";
368 TLOG(TLVL_DEBUG) <<
"Fragment to art: "<< str.str() ;
371 artdaq::FragmentPtr tempFrag;
372 auto sts = writer.AddFragment(std::move(tmpFrag), 1000000, tempFrag);
373 BOOST_REQUIRE_EQUAL(sts,
true);
381 art::EventPrincipal* newevent =
nullptr;
382 art::SubRunPrincipal* newsubrun =
nullptr;
383 art::RunPrincipal* newrun =
nullptr;
384 bool rc = reader.
readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
386 if (run.get() && run->run() == eventid.run())
388 BOOST_CHECK(newrun ==
nullptr);
393 BOOST_CHECK(newrun->id() == eventid.runID());
395 if (!newrun && subrun.get() && subrun->subRun() == eventid.subRun())
397 BOOST_CHECK(newsubrun ==
nullptr);
401 BOOST_CHECK(newsubrun);
402 BOOST_CHECK(newsubrun->id() == eventid.subRunID());
404 BOOST_CHECK(newevent);
405 BOOST_CHECK(newevent->id() == eventid);
406 art::Event e(*newevent, art::ModuleDescription());
407 art::Handle<std::vector<artdaq::Fragment>> h;
408 e.getByLabel(
"daq",
"ABCDEF", h);
409 BOOST_CHECK(h.isValid());
410 BOOST_CHECK(h->size() == 1);
412 auto iter2 = h->front().dataBegin();
413 std::ostringstream str2;
415 while(iter2 != h->front().dataEnd())
417 str2 << std::to_string(*iter2) <<
", ";
422 TLOG(TLVL_DEBUG) <<
"Fragment from art: " << str2.str() ;
424 BOOST_CHECK(std::equal(fakeData.begin(),
426 h->front().dataBegin()));
435 art::EventID eventid(2112, 1, 3);
437 basic_test(reader(),writer(),
438 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
439 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), eventid.subRun(), now)),
445 art::EventID eventid(2112, 1, 3);
447 basic_test(reader(), writer(),
455 art::EventID eventid(2112, 1, 3);
457 basic_test(reader(), writer(),
458 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
459 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), 0, now)),
465 art::EventID eventid(2112, 1, 3);
467 basic_test(reader(), writer(),
468 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run() - 1, now)),
469 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run() - 1,
480 std::string
const fakeFileName(
"no such file exists");
481 art::FileBlock* pFile =
nullptr;
482 reader().readFile(fakeFileName, pFile);
484 BOOST_CHECK(pFile->fileFormatVersion() == art::FileFormatVersion(1,
"RawEvent2011"));
485 BOOST_CHECK(pFile->tree() ==
nullptr);
487 BOOST_CHECK(!pFile->fastClonable());
491 art::RunID runid(2112);
492 art::SubRunID subrunid(2112, 1);
493 art::EventID eventid(2112, 1, 3);
495 std::unique_ptr<art::RunPrincipal> run(source_helper().makeRunPrincipal(runid.run(), now));
496 std::unique_ptr<art::SubRunPrincipal> subrun(source_helper().makeSubRunPrincipal(runid.run(), subrunid.subRun(), now));
497 std::unique_ptr<art::EventPrincipal> event(source_helper().makeEventPrincipal(runid.run(),
501 writer().endOfData();
502 art::EventPrincipal* newevent =
nullptr;
503 art::SubRunPrincipal* newsubrun =
nullptr;
504 art::RunPrincipal* newrun =
nullptr;
505 bool rc = reader().readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
507 BOOST_CHECK(newrun ==
nullptr);
508 BOOST_CHECK(newsubrun ==
nullptr);
509 BOOST_CHECK(newevent ==
nullptr);
512 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.