EventPrincipal_t.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 #define BOOST_TEST_MODULE (eventprincipal_t)
3 #include "boost/test/unit_test.hpp"
4 
28 #include "fhiclcpp/ParameterSet.h"
29 
30 #include <map>
31 #include <memory>
32 #include <exception>
33 #include <string>
34 #include <typeinfo>
35 
36 using namespace std;
37 using namespace std::string_literals;
38 using namespace art;
39 
40 namespace art {
41  std::ostream&
42  boost_test_print_type(std::ostream& os,
44  {
45  return os << pd.get();
46  }
47 }
48 
50 public: // MEMBER FUNCTIONS -- Special Member Functions
52 
53  ProductTables producedProducts_{ProductTables::invalid()};
54  std::map<std::string, art::ProductID> productIDs_{};
55  std::map<std::string, art::ProcessConfiguration*> processConfigurations_{};
56 
57 private:
58  art::BranchDescription fake_single_process_branch(
59  std::string const& tag,
60  std::string const& processName,
61  std::string const& productInstanceName = {});
62 
63  ProcessConfiguration* fake_single_module_process(
64  std::string const& tag,
65  std::string const& processName,
66  fhicl::ParameterSet const& moduleParams,
68 };
69 
70 auto const invalid_module_context = ModuleContext::invalid();
71 
73 {
74  // We can only insert products registered in the product tables
75  ProductDescriptions descriptions;
76  descriptions.push_back(fake_single_process_branch("hlt", "HLT"));
77  descriptions.push_back(fake_single_process_branch("prod", "PROD"));
78  descriptions.push_back(fake_single_process_branch("test", "TEST"));
79  descriptions.push_back(fake_single_process_branch("user", "USER"));
80  descriptions.push_back(fake_single_process_branch("rick", "USER2", "rick"));
81  producedProducts_ = ProductTables{descriptions};
82 }
83 
86  string const& tag,
87  string const& processName,
88  fhicl::ParameterSet const& moduleParams,
89  string const& release)
90 {
91  fhicl::ParameterSet processParams;
92  processParams.put(processName, moduleParams);
93  processParams.put("process_name", processName);
94  auto result =
95  new ProcessConfiguration(processName, processParams.id(), release);
96  processConfigurations_[tag] = result;
97  return result;
98 }
99 
102  std::string const& tag,
103  std::string const& processName,
104  std::string const& productInstanceName)
105 {
106  string const moduleLabel{processName + "dummyMod"};
107  string const moduleClass{"DummyModule"};
108  TypeID const dummyType{typeid(arttest::DummyProduct)};
109  fhicl::ParameterSet modParams;
110  modParams.put("module_type", moduleClass);
111  modParams.put("module_label", moduleLabel);
112 
114  art::InEvent,
115  art::TypeLabel{dummyType,
116  productInstanceName,
118  false},
119  moduleLabel,
120  modParams.id(),
121  *fake_single_module_process(tag, processName, modParams)};
122  productIDs_.emplace(tag, result.productID());
123  return result;
124 }
125 
128  ProductTablesFixture& ptf();
129  std::unique_ptr<art::EventPrincipal> pEvent_{nullptr};
130 };
131 
133 {
134  (void)ptf(); // Bootstrap ProductTables creation first time out.
135  EventID const eventID{101, 87, 20};
136 
137  // Making a functional EventPrincipal is not trivial, so we do it
138  // all here. Put products we'll look for into the EventPrincipal.
139  std::unique_ptr<art::EDProduct> product =
140  std::make_unique<art::Wrapper<arttest::DummyProduct>>();
141 
142  std::string const tag{"rick"};
143  auto i = ptf().productIDs_.find(tag);
144  BOOST_TEST_REQUIRE(static_cast<bool>(i != ptf().productIDs_.end()));
145 
146  auto pd = ptf().producedProducts_.get(InEvent).description(i->second);
147  BOOST_TEST_REQUIRE(pd != nullptr);
148 
149  auto entryDescriptionPtr = std::make_shared<art::Parentage>();
150  auto productProvenancePtr = std::make_unique<art::ProductProvenance const>(
151  pd->productID(),
153  entryDescriptionPtr->parents());
154 
155  auto* process = ptf().processConfigurations_[tag];
156  BOOST_TEST_REQUIRE(process);
157 
158  constexpr art::Timestamp now{1234567UL};
159 
160  art::RunAuxiliary const runAux{eventID.run(), now, now};
161  auto rp = std::make_unique<art::RunPrincipal>(runAux, *process, nullptr);
162 
163  art::SubRunAuxiliary const subRunAux{rp->run(), eventID.subRun(), now, now};
164  auto srp =
165  std::make_unique<art::SubRunPrincipal>(subRunAux, *process, nullptr);
166  srp->setRunPrincipal(rp.get());
167 
168  art::EventAuxiliary const eventAux{eventID, now, true};
169  pEvent_ = std::make_unique<art::EventPrincipal>(eventAux, *process, nullptr);
170  pEvent_->setSubRunPrincipal(srp.get());
171  pEvent_->createGroupsForProducedProducts(ptf().producedProducts_);
172  pEvent_->enableLookupOfProducedProducts(ptf().producedProducts_);
173  pEvent_->put(
174  *pd, move(productProvenancePtr), move(product), make_unique<RangeSet>());
175  BOOST_TEST_REQUIRE(pEvent_->size() == 5u);
176 
177  auto pdPtr = pEvent_->getProductDescription(i->second);
178  BOOST_TEST_REQUIRE(*pd == *pdPtr);
179 }
180 
183 {
184  static ProductTablesFixture ptf_s;
185  return ptf_s;
186 }
187 
188 BOOST_FIXTURE_TEST_SUITE(eventprincipal_t, EventPrincipalTestFixture)
189 
190 BOOST_AUTO_TEST_CASE(failgetbyIdTest)
191 {
192  auto const invalid = art::ProductID::invalid();
193  auto const& h = pEvent_->getByProductID(invalid);
194  BOOST_TEST(h.failed());
195 }
196 
197 BOOST_AUTO_TEST_CASE(failgetbySelectorTest)
198 {
199  // We don't put ProductIDs into the EventPrincipal, so that's a type
200  // sure not to match any product.
201  auto const& wrapped = art::WrappedTypeID::make<art::ProductID>();
202 
203  art::ProcessNameSelector const pnsel{"PROD"};
204  auto const& h = pEvent_->getBySelector(
205  invalid_module_context, wrapped, pnsel, ProcessTag{"PROD"s, "USER2"s});
206  BOOST_TEST(h.failed());
207 }
208 
209 BOOST_AUTO_TEST_CASE(failgetbyLabelTest)
210 {
211  // We don't put ProductIDs into the EventPrincipal, so that's a type
212  // sure not to match any product.
213  auto const& wrapped = art::WrappedTypeID::make<art::ProductID>();
214 
215  std::string const label{"this does not exist"};
216 
217  auto const& h = pEvent_->getByLabel(
218  invalid_module_context, wrapped, label, ""s, ProcessTag{""s, "USER2"s});
219  BOOST_TEST(h.failed());
220 }
221 
222 BOOST_AUTO_TEST_CASE(failgetManyTest)
223 {
224  // We don't put ProductIDs into the EventPrincipal, so that's a type
225  // sure not to match any product.
226  auto const& wrapped = art::WrappedTypeID::make<art::ProductID>();
227 
228  art::ProcessNameSelector const sel{"PROD"};
229  auto const& query_results = pEvent_->getMany(
230  invalid_module_context, wrapped, sel, ProcessTag{"PROD"s, "USER2"s});
231  BOOST_TEST(query_results.empty());
232 }
233 
234 BOOST_AUTO_TEST_CASE(failgetManybyTypeTest)
235 {
236  // We don't put ProductIDs into the EventPrincipal, so that's a type
237  // sure not to match any product.
238  auto const& wrapped = art::WrappedTypeID::make<art::ProductID>();
239 
240  // getManyByType is achieved by providing a selector that matches
241  // everything.
242  auto const& query_results = pEvent_->getMany(invalid_module_context,
243  wrapped,
245  ProcessTag{""s, "USER2"s});
246  BOOST_TEST(query_results.empty());
247 }
248 
249 BOOST_AUTO_TEST_SUITE_END()
static QCString result
std::string string
Definition: nybbler.cc:12
art::BranchDescription fake_single_process_branch(std::string const &tag, std::string const &processName, std::string const &productInstanceName={})
STL namespace.
constexpr pointer get() const noexcept
Definition: exempt_ptr.h:148
std::vector< BranchDescription > ProductDescriptions
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
ProcessConfiguration * fake_single_module_process(std::string const &tag, std::string const &processName, fhicl::ParameterSet const &moduleParams, std::string const &release=art::getReleaseVersion())
def process(f, kind)
Definition: search.py:254
def move(depos, offset)
Definition: depos.py:107
std::string const & getReleaseVersion()
RunNumber_t run() const noexcept
ParameterSetID id() const
RunNumber_t run() const noexcept
string release
Definition: conf.py:24
std::ostream & boost_test_print_type(std::ostream &os, cet::exempt_ptr< BranchDescription const > const pd)
static constexpr ProductID invalid() noexcept
Definition: ProductID.h:26
static QCString * s
Definition: config.cpp:1042
constexpr ProductStatus present() noexcept
Definition: ProductStatus.h:10
void put(std::string const &key)
ProductTablesFixture & ptf()
BOOST_AUTO_TEST_CASE(failgetbyIdTest)
auto const invalid_module_context