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)
115 auto request = GetNextRequest();
116 if (request.first == 0)
122 timestamp_ = request.second;
123 TLOG(51) <<
"Received a request for the fragment with timestamp " << timestamp_
124 <<
" and sequenceId " << request.first <<
". Proceeding to fill the fragment buffer, etc.";
126 auto requests = GetRequests();
127 auto request_iterator = requests.begin();
128 std::pair<artdaq::Fragment::sequence_id_t, artdaq::Fragment::timestamp_t> new_request(0, 0);
129 TLOG(52) <<
"Looping through " << requests.size() <<
" requests to see if there is a new one.";
130 while (request_iterator != requests.end())
132 if (lazily_handled_requests_.find(request_iterator->first) == lazily_handled_requests_.end())
134 lazily_handled_requests_.insert(request_iterator->first);
135 new_request = *request_iterator;
140 if (new_request.first == 0)
145 timestamp_ = new_request.second;
146 TLOG(51) <<
"Found a new request for the fragment with timestamp " << timestamp_
147 <<
" and sequenceId " << new_request.first <<
". Proceeding to fill the fragment buffer, etc.";
151 std::size_t bytes_read = 0;
152 hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
162 for (
auto&
id : fragmentIDs())
169 std::unique_ptr<artdaq::Fragment> fragptr(
170 artdaq::Fragment::FragmentBytes(bytes_read, ev_counter(),
id, fragment_type_, metadata_, timestamp_));
171 frags.emplace_back(std::move(fragptr));
174 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
178 memcpy(frags.back()->dataBeginBytes(), readout_buffer_,
sizeof(ToyFragment::Header));
181 TLOG(50) <<
"getNext_ after memcpy " << bytes_read
182 <<
" bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes()
183 <<
" metabytes=" <<
sizeof(metadata_);
186 if (metricMan !=
nullptr)
188 metricMan->sendMetric(
"Fragments Sent", ev_counter(),
"Events", 3, artdaq::MetricMode::LastPoint);
191 if (rollover_subrun_interval_ > 0 && ev_counter() % rollover_subrun_interval_ == 0 && fragment_id() == 0)
193 bool fragmentIdZero =
false;
194 for (
auto&
id : fragmentIDs())
196 if (
id == 0) fragmentIdZero =
true;
200 artdaq::FragmentPtr endOfSubrunFrag(
new artdaq::Fragment(static_cast<size_t>(
201 ceil(
sizeof(my_rank) / static_cast<double>(
sizeof(artdaq::Fragment::value_type))))));
202 endOfSubrunFrag->setSystemType(artdaq::Fragment::EndOfSubrunFragmentType);
204 endOfSubrunFrag->setSequenceID(ev_counter() + 1);
205 endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
207 *endOfSubrunFrag->dataBegin() = my_rank;
208 frags.emplace_back(std::move(endOfSubrunFrag));
213 timestamp_ += timestampScale_;
218 void demo::ToySimulator::start()
220 hardware_interface_->StartDatataking();
222 lazily_handled_requests_.clear();
225 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 ...