RandomNumberSaveTest_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: RandomNumberSaveTest
3 // Module Type: filter
4 // File: RandomNumberSaveTest_module.cc
5 //
6 // Module for testing (with appropriate configuration) both aspects of
7 // random number state storage / retrieval: file and data product.
8 //
9 // Generated at Wed Oct 12 11:44:35 2011 by Chris Green using artmod
10 // from art v1_00_00.
11 ////////////////////////////////////////////////////////////////////////
13 
21 #include "fhiclcpp/types/Atom.h"
22 
23 #include "CLHEP/Random/RandFlat.h"
24 
25 #include <algorithm>
26 #include <atomic>
27 #include <iostream>
28 #include <iterator>
29 #include <ostream>
30 #include <string>
31 #include <vector>
32 
33 using namespace fhicl;
34 
35 namespace arttest {
36  class RandomNumberSaveTest;
37 }
38 
40 public:
41  using prod_t = std::vector<size_t>;
42 
43  struct Config {
44  Atom<long> seed{Name{"seed"}, -1};
45  Atom<size_t> dieOnNthEvent{Name{"dieOnNthEvent"}, 0};
46  Atom<bool> genUnsaved{Name{"genUnsaved"}, true};
47  };
49  explicit RandomNumberSaveTest(Parameters const& p);
50 
51 private:
52  void produce(art::Event& e) override;
53 
55  CLHEP::RandFlat dist_;
56  size_t const dieOnNthEvent_;
57  size_t eventN_{};
58  bool const genUnsaved_;
59 };
60 
61 std::ostream&
63 {
65  v,
66  std::ostream_iterator<arttest::RandomNumberSaveTest::prod_t::value_type>(
67  os, ", "));
68  return os;
69 }
70 
72  : EDProducer{p}
73  , myLabel_{p.get_PSet().get<std::string>("module_label")}
74  , dist_{createEngine(p().seed())}
75  , dieOnNthEvent_{p().dieOnNthEvent()}
76  , genUnsaved_{p().genUnsaved()}
77 {
78  produces<prod_t>();
79 }
80 
81 void
83 {
84  if (++eventN_ == dieOnNthEvent_) {
86  << "Throwing while processing ordinal event " << eventN_
87  << " as requested.\n";
88  }
90  prod_t nums;
91  static size_t constexpr nums_size{5};
92  static size_t constexpr random_range{1000};
93  nums.reserve(nums_size);
94  generate_n(std::back_inserter(nums), nums_size, [this] {
95  return dist_.fireInt(random_range);
96  });
97  std::cerr << "nums: " << nums << "\n";
98  if (e.getByLabel(myLabel_, hp)) {
99  std::cerr << "(*hp): " << *hp << "\n";
100  // Reading.
101  BOOST_REQUIRE((*hp) == nums);
102  if (genUnsaved_) {
103  // Intentionally attempt to throw the sequence off for the next event.
104  dist_.fireInt(random_range);
105  }
106  } else {
107  // Writing.
108  e.put(std::make_unique<prod_t>(nums));
109  }
110 }
111 
base_engine_t & createEngine(seed_t seed)
std::string string
Definition: nybbler.cc:12
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:435
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:68
parameter set interface
BOOST_REQUIRE(inFile)
p
Definition: test.py:228
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
auto copy_all(FwdCont &, FwdIter)
void produce(art::Event &e) override
std::ostream & operator<<(std::ostream &, ParameterSetID const &)
ProductID put(std::unique_ptr< PROD > &&edp, FullSemantic< Level::Run > const semantic)
Definition: DataViewImpl.h:692