00001 #include "artdaq/ArtModules/detail/RawEventQueueReader.hh"
00002
00003 #include "art/Framework/Core/FileBlock.h"
00004
00005 #include "art/Framework/IO/Sources/SourceHelper.h"
00006 #include "art/Framework/Principal/Event.h"
00007 #include "art/Framework/Principal/EventPrincipal.h"
00008 #include "art/Framework/Principal/Handle.h"
00009 #include "art/Framework/Principal/RunPrincipal.h"
00010 #include "art/Framework/Principal/SubRunPrincipal.h"
00011
00012 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
00013 #include "art/Persistency/Provenance/MasterProductRegistry.h"
00014 #include "art/Persistency/Provenance/ProductMetaData.h"
00015 #include "canvas/Persistency/Provenance/EventID.h"
00016 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
00017 #include "canvas/Persistency/Provenance/ModuleDescription.h"
00018 #include "canvas/Persistency/Provenance/Parentage.h"
00019 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
00020 #include "canvas/Persistency/Provenance/RunID.h"
00021 #include "canvas/Persistency/Provenance/SubRunID.h"
00022 #include "canvas/Persistency/Provenance/Timestamp.h"
00023 #include "canvas/Utilities/Exception.h"
00024 #include "canvas/Utilities/GetPassID.h"
00025 #include "art/Version/GetReleaseVersion.h"
00026 #include "artdaq-core/Core/GlobalQueue.hh"
00027 #include "artdaq-core/Data/Fragment.hh"
00028 #include "fhiclcpp/make_ParameterSet.h"
00029
00030 #define BOOST_TEST_MODULE ( raw_event_queue_reader_t )
00031 #include <boost/test/auto_unit_test.hpp>
00032
00033 #include <iostream>
00034 #include <memory>
00035 #include <string>
00036
00040 class MPRGlobalTestFixture
00041 {
00042 public:
00046 MPRGlobalTestFixture();
00047
00051 typedef std::map<std::string, art::BranchKey> BKmap_t;
00052
00053 BKmap_t branchKeys_;
00054 std::map<std::string, std::unique_ptr<art::ProcessConfiguration>> processConfigurations_;
00055
00065 art::ProcessConfiguration*
00066 fake_single_module_process(std::string const& tag,
00067 std::string const& processName,
00068 fhicl::ParameterSet const& moduleParams,
00069 std::string const& release = art::getReleaseVersion(),
00070 std::string const& pass = art::getPassID());
00071
00079 std::unique_ptr<art::BranchDescription>
00080 fake_single_process_branch(std::string const& tag,
00081 std::string const& processName,
00082 std::string const& productInstanceName = std::string());
00083
00087 void finalize();
00088
00089 art::MasterProductRegistry productRegistry_;
00090
00091 };
00092
00093 MPRGlobalTestFixture::MPRGlobalTestFixture()
00094 :
00095 branchKeys_()
00096 , processConfigurations_()
00097 , productRegistry_()
00098
00099 {
00100
00101 productRegistry_.addProduct(fake_single_process_branch("hlt", "HLT"));
00102 productRegistry_.addProduct(fake_single_process_branch("prod", "PROD"));
00103 productRegistry_.addProduct(fake_single_process_branch("test", "TEST"));
00104 productRegistry_.addProduct(fake_single_process_branch("user", "USER"));
00105 productRegistry_.addProduct(fake_single_process_branch("rick", "USER2", "rick"));
00106 }
00107
00108 void
00109 MPRGlobalTestFixture::finalize()
00110 {
00111 productRegistry_.setFrozen();
00112 #if ART_HEX_VERSION >= 0x20703
00113 art::BranchIDListRegistry::updateFromProductRegistry(productRegistry_);
00114 #else
00115 art::BranchIDListHelper::updateRegistries(productRegistry_);
00116 #endif
00117 art::ProductMetaData::create_instance(productRegistry_);
00118 }
00119
00120 art::ProcessConfiguration*
00121 MPRGlobalTestFixture::
00122 fake_single_module_process(std::string const& tag,
00123 std::string const& processName,
00124 fhicl::ParameterSet const& moduleParams,
00125 std::string const& release,
00126 std::string const& pass)
00127 {
00128 fhicl::ParameterSet processParams;
00129 processParams.put(processName, moduleParams);
00130 processParams.put<std::string>("process_name",
00131 processName);
00132 auto emplace_pair =
00133 processConfigurations_.emplace(tag,
00134 std::make_unique<art::ProcessConfiguration>(processName, processParams.id(), release, pass));
00135 return emplace_pair.first->second.get();
00136 }
00137
00138 std::unique_ptr<art::BranchDescription>
00139 MPRGlobalTestFixture::
00140 fake_single_process_branch(std::string const& tag,
00141 std::string const& processName,
00142 std::string const& productInstanceName)
00143 {
00144 std::string moduleLabel = processName + "dummyMod";
00145 std::string moduleClass("DummyModule");
00146 fhicl::ParameterSet modParams;
00147 modParams.put<std::string>("module_type", moduleClass);
00148 modParams.put<std::string>("module_label", moduleLabel);
00149 art::ProcessConfiguration* process =
00150 fake_single_module_process(tag, processName, modParams);
00151 art::ModuleDescription mod(modParams.id(),
00152 moduleClass,
00153 moduleLabel,
00154 *process);
00155 art::TypeID dummyType(typeid(int));
00156 art::BranchDescription* result =
00157 new art::BranchDescription(
00158 # if ART_HEX_VERSION >= 0x20703
00159 art::InEvent,
00160 art::TypeLabel(dummyType,
00161 productInstanceName),
00162 # else
00163 art::TypeLabel(art::InEvent,
00164 dummyType,
00165 productInstanceName),
00166 # endif
00167 mod);
00168
00169
00170 branchKeys_.insert(std::make_pair(tag, art::BranchKey(*result)));
00171 return std::unique_ptr<art::BranchDescription>(result);
00172 }
00173
00177 struct REQRTestFixture
00178 {
00182 REQRTestFixture()
00183 {
00184 static bool once(true);
00185 if (once)
00186 {
00187 (void)reader();
00188 art::ModuleDescription md(fhicl::ParameterSet().id(),
00189 "_NAMEERROR_",
00190 "_LABELERROR_",
00191 *gf().processConfigurations_["daq"]);
00192
00193
00194 helper().registerProducts(gf().productRegistry_, md);
00195 gf().finalize();
00196 once = false;
00197 }
00198 }
00199
00204 MPRGlobalTestFixture& gf()
00205 {
00206 static MPRGlobalTestFixture mpr;
00207 return mpr;
00208 }
00209
00214 art::ProductRegistryHelper& helper()
00215 {
00216 static art::ProductRegistryHelper s_helper;
00217 return s_helper;
00218 }
00219
00224 art::SourceHelper& source_helper()
00225 {
00226 static std::unique_ptr<art::SourceHelper>
00227 s_source_helper;
00228 if (!s_source_helper)
00229 {
00230 fhicl::ParameterSet sourceParams;
00231 std::string moduleType{ "DummySource" };
00232 std::string moduleLabel{ "daq" };
00233 sourceParams.put<std::string>("module_type", moduleType);
00234 sourceParams.put<std::string>("module_label", moduleLabel);
00235 auto pc_ptr = gf().fake_single_module_process(moduleLabel,
00236 "TEST",
00237 sourceParams);
00238 art::ModuleDescription md(sourceParams.id(),
00239 moduleType,
00240 moduleLabel,
00241 *pc_ptr);
00242 s_source_helper = std::make_unique<art::SourceHelper>(md);
00243 }
00244 return *s_source_helper;
00245 }
00246
00251 artdaq::detail::RawEventQueueReader& reader()
00252 {
00253 fhicl::ParameterSet pset;
00254 static artdaq::detail::RawEventQueueReader
00255 s_reader(pset,
00256 helper(),
00257 source_helper(),
00258 gf().productRegistry_);
00259 static bool reader_initialized = false;
00260 if (!reader_initialized)
00261 {
00262 s_reader.fragment_type_map_[1] = "ABCDEF";
00263 helper().reconstitutes<artdaq::Fragments, art::InEvent>("daq", "ABCDEF");
00264 reader_initialized = true;
00265 }
00266 return s_reader;
00267 }
00268 };
00269
00270 BOOST_FIXTURE_TEST_SUITE(raw_event_queue_reader_t, REQRTestFixture)
00271
00272 namespace
00273 {
00281 void basic_test(artdaq::detail::RawEventQueueReader& reader,
00282 std::unique_ptr<art::RunPrincipal>&& run,
00283 std::unique_ptr<art::SubRunPrincipal>&& subrun,
00284 art::EventID const& eventid)
00285 {
00286 BOOST_REQUIRE(run || subrun == nullptr);
00287 std::shared_ptr<artdaq::RawEvent> event(new artdaq::RawEvent(eventid.run(), eventid.subRun(), eventid.event()));
00288 std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
00289 artdaq::FragmentPtr
00290 tmpFrag(artdaq::Fragment::dataFrag(eventid.event(),
00291 0,
00292 fakeData.begin(),
00293 fakeData.end()));
00294 tmpFrag->setUserType(1);
00295 event->insertFragment(std::move(tmpFrag));
00296 event->markComplete();
00297 artdaq::getGlobalQueue().enqNowait(event);
00298 art::EventPrincipal* newevent = nullptr;
00299 art::SubRunPrincipal* newsubrun = nullptr;
00300 art::RunPrincipal* newrun = nullptr;
00301 bool rc = reader.readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
00302 BOOST_REQUIRE(rc);
00303 if (run.get() && run->run() == eventid.run())
00304 {
00305 BOOST_CHECK(newrun == nullptr);
00306 }
00307 else
00308 {
00309 BOOST_CHECK(newrun);
00310 BOOST_CHECK(newrun->id() == eventid.runID());
00311 }
00312 if (!newrun && subrun.get() && subrun->subRun() == eventid.subRun())
00313 {
00314 BOOST_CHECK(newsubrun == nullptr);
00315 }
00316 else
00317 {
00318 BOOST_CHECK(newsubrun);
00319 BOOST_CHECK(newsubrun->id() == eventid.subRunID());
00320 }
00321 BOOST_CHECK(newevent);
00322 BOOST_CHECK(newevent->id() == eventid);
00323 art::Event e(*newevent, art::ModuleDescription());
00324 art::Handle<std::vector<artdaq::Fragment>> h;
00325 e.getByLabel("daq", "ABCDEF", h);
00326 BOOST_CHECK(h.isValid());
00327 BOOST_CHECK(h->size() == 1);
00328 BOOST_CHECK(std::equal(fakeData.begin(),
00329 fakeData.end(),
00330 h->front().dataBegin()));
00331 delete(newrun);
00332 delete(newsubrun);
00333 delete(newevent);
00334 }
00335 }
00336
00337 BOOST_AUTO_TEST_CASE(nonempty_event)
00338 {
00339 art::EventID eventid(2112, 1, 3);
00340 art::Timestamp now;
00341 basic_test(reader(),
00342 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
00343 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), eventid.subRun(), now)),
00344 eventid);
00345 }
00346
00347 BOOST_AUTO_TEST_CASE(first_event)
00348 {
00349 art::EventID eventid(2112, 1, 3);
00350 art::Timestamp now;
00351 basic_test(reader(),
00352 nullptr,
00353 nullptr,
00354 eventid);
00355 }
00356
00357 BOOST_AUTO_TEST_CASE(new_subrun)
00358 {
00359 art::EventID eventid(2112, 1, 3);
00360 art::Timestamp now;
00361 basic_test(reader(),
00362 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run(), now)),
00363 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run(), 0, now)),
00364 eventid);
00365 }
00366
00367 BOOST_AUTO_TEST_CASE(new_run)
00368 {
00369 art::EventID eventid(2112, 1, 3);
00370 art::Timestamp now;
00371 basic_test(reader(),
00372 std::unique_ptr<art::RunPrincipal>(source_helper().makeRunPrincipal(eventid.run() - 1, now)),
00373 std::unique_ptr<art::SubRunPrincipal>(source_helper().makeSubRunPrincipal(eventid.run() - 1,
00374 eventid.subRun(),
00375 now)),
00376 eventid);
00377 }
00378
00379 BOOST_AUTO_TEST_CASE(end_of_data)
00380 {
00381
00382
00383
00384 std::string const fakeFileName("no such file exists");
00385 art::FileBlock* pFile = nullptr;
00386 reader().readFile(fakeFileName, pFile);
00387 BOOST_CHECK(pFile);
00388 BOOST_CHECK(pFile->fileFormatVersion() == art::FileFormatVersion(1, "RawEvent2011"));
00389 BOOST_CHECK(pFile->tree() == nullptr);
00390
00391 BOOST_CHECK(!pFile->fastClonable());
00392
00393
00394
00395 art::RunID runid(2112);
00396 art::SubRunID subrunid(2112, 1);
00397 art::EventID eventid(2112, 1, 3);
00398 art::Timestamp now;
00399 std::unique_ptr<art::RunPrincipal> run(source_helper().makeRunPrincipal(runid.run(), now));
00400 std::unique_ptr<art::SubRunPrincipal> subrun(source_helper().makeSubRunPrincipal(runid.run(), subrunid.subRun(), now));
00401 std::unique_ptr<art::EventPrincipal> event(source_helper().makeEventPrincipal(runid.run(),
00402 subrunid.subRun(),
00403 eventid.event(),
00404 now));
00405 artdaq::getGlobalQueue().enqNowait(std::shared_ptr<artdaq::RawEvent>(nullptr));
00406 art::EventPrincipal* newevent = nullptr;
00407 art::SubRunPrincipal* newsubrun = nullptr;
00408 art::RunPrincipal* newrun = nullptr;
00409 bool rc = reader().readNext(run.get(), subrun.get(), newrun, newsubrun, newevent);
00410 BOOST_CHECK(!rc);
00411 BOOST_CHECK(newrun == nullptr);
00412 BOOST_CHECK(newsubrun == nullptr);
00413 BOOST_CHECK(newevent == nullptr);
00414 }
00415
00416 BOOST_AUTO_TEST_SUITE_END()