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