5 #include "artdaq-demo/Generators/ToySimulator.hh"
7 #include "canvas/Utilities/Exception.h"
9 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
10 #include "artdaq/Generators/GeneratorMacros.hh"
12 #include "artdaq-core-demo/Overlays/FragmentType.hh"
13 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
15 #include "fhiclcpp/ParameterSet.h"
23 #define TRACE_NAME "ToySimulator"
24 #include "cetlib_except/exception.h"
28 : CommandableFragmentGenerator(ps)
31 , timestampScale_(ps.get<int>(
"timestamp_scale_factor", 1))
32 , rollover_subrun_interval_(ps.get<int>(
"rollover_subrun_interval", 0))
33 , metadata_({0, 0, 0})
34 , readout_buffer_(
nullptr)
35 , fragment_type_(
static_cast<decltype(fragment_type_)
>(artdaq::Fragment::InvalidFragmentType))
36 , distribution_type_(static_cast<ToyHardwareInterface::DistributionType>(ps.get<
int>(
"distribution_type")))
37 , generated_fragments_per_event_(ps.get<
int>(
"generated_fragments_per_event", 1))
38 , exception_on_config_(ps.get<
bool>(
"exception_on_config",
false))
39 , dies_on_config_(ps.get<
bool>(
"dies_on_config",
false))
40 , lazy_mode_(ps.get<
bool>(
"lazy_mode",
false))
43 if (lazy_mode_ && request_mode() == artdaq::RequestMode::Ignored)
45 throw cet::exception(
"ToySimulator") <<
"The request mode has been set to \"Ignored\"; this is inconsistent with this ToySimulator's lazy mode set to \"true\"";
48 hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
50 if (exception_on_config_)
52 throw cet::exception(
"ToySimulator") <<
"This is an engineered exception designed for testing purposes, set "
53 "by the exception_on_config FHiCL variable";
55 else if (dies_on_config_)
57 TLOG(TLVL_ERROR) <<
"This is an engineered process death, set by the dies_on_config FHiCL variable";
61 metadata_.board_serial_number = hardware_interface_->SerialNumber() & 0xFFFF;
62 metadata_.num_adc_bits = hardware_interface_->NumADCBits();
63 TLOG(TLVL_INFO) <<
"Constructor: metadata_.unused = 0x" << std::hex << metadata_.unused
64 <<
" sizeof(metadata_) = " << std::dec <<
sizeof(metadata_);
66 switch (hardware_interface_->BoardType())
69 fragment_type_ = toFragmentType(
"TOY1");
72 fragment_type_ = toFragmentType(
"TOY2");
75 throw cet::exception(
"ToySimulator") <<
"Unable to determine board type supplied by hardware";
81 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
83 if (should_stop()) {
return false; }
112 auto request = GetNextRequest();
113 if (request.first == 0)
119 timestamp_ = request.second;
120 TLOG(51) <<
"Received a request for the fragment with timestamp " << timestamp_
121 <<
" and sequenceId " << request.first <<
". Proceeding to fill the fragment buffer, etc.";
123 auto requests = GetRequests();
124 auto request_iterator = requests.begin();
125 std::pair<artdaq::Fragment::sequence_id_t, artdaq::Fragment::timestamp_t> new_request(0, 0);
126 TLOG(52) <<
"Looping through " << requests.size() <<
" requests to see if there is a new one.";
127 while (request_iterator != requests.end())
129 if (lazily_handled_requests_.find(request_iterator->first) == lazily_handled_requests_.end())
131 lazily_handled_requests_.insert(request_iterator->first);
132 new_request = *request_iterator;
137 if (new_request.first == 0)
142 timestamp_ = new_request.second;
143 TLOG(51) <<
"Found a new request for the fragment with timestamp " << timestamp_
144 <<
" and sequenceId " << new_request.first <<
". Proceeding to fill the fragment buffer, etc.";
148 std::size_t bytes_read = 0;
149 hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
159 for (
auto&
id : fragmentIDs())
166 std::unique_ptr<artdaq::Fragment> fragptr(
167 artdaq::Fragment::FragmentBytes(bytes_read, ev_counter(),
id, fragment_type_, metadata_, timestamp_));
168 frags.emplace_back(std::move(fragptr));
171 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
175 memcpy(frags.back()->dataBeginBytes(), readout_buffer_,
sizeof(ToyFragment::Header));
178 TLOG(50) <<
"getNext_ after memcpy " << bytes_read
179 <<
" bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes()
180 <<
" metabytes=" <<
sizeof(metadata_);
183 if (metricMan !=
nullptr)
184 { metricMan->sendMetric(
"Fragments Sent", ev_counter(),
"Events", 3, artdaq::MetricMode::LastPoint); }
186 if (rollover_subrun_interval_ > 0 && ev_counter() % rollover_subrun_interval_ == 0 && fragment_id() == 0)
188 bool fragmentIdZero =
false;
189 for (
auto&
id : fragmentIDs())
191 if (
id == 0) fragmentIdZero =
true;
195 artdaq::FragmentPtr endOfSubrunFrag(
new artdaq::Fragment(static_cast<size_t>(
196 ceil(
sizeof(my_rank) / static_cast<double>(
sizeof(artdaq::Fragment::value_type))))));
197 endOfSubrunFrag->setSystemType(artdaq::Fragment::EndOfSubrunFragmentType);
199 endOfSubrunFrag->setSequenceID(ev_counter() + 1);
200 endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
202 *endOfSubrunFrag->dataBegin() = my_rank;
203 frags.emplace_back(std::move(endOfSubrunFrag));
208 timestamp_ += timestampScale_;
213 void demo::ToySimulator::start()
215 hardware_interface_->StartDatataking();
217 lazily_handled_requests_.clear();
220 void demo::ToySimulator::stop() { hardware_interface_->StopDatataking(); }
A use-after-free expliot distribution.
ToySimulator(fhicl::ParameterSet const &ps)
ToySimulator Constructor.
JCF, Mar-17-2016: ToyHardwareInterface is meant to mimic a vendor-provided hardware API...
virtual ~ToySimulator()
Shutdown the ToySimulator.
ToySimulator is a simple type of fragment generator intended to be studied by new users of artdaq as ...