artdaq_demo  v3_06_00
ToySimulator_generator.cc
1 // For an explanation of this class, look at its header,
2 // ToySimulator.hh, as well as
3 // https://cdcvs.fnal.gov/redmine/projects/artdaq-demo/wiki/Fragments_and_FragmentGenerators_w_Toy_Fragments_as_Examples
4 
5 #include "artdaq-demo/Generators/ToySimulator.hh"
6 
7 #include "canvas/Utilities/Exception.h"
8 
9 #include "artdaq/Generators/GeneratorMacros.hh"
10 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
11 
12 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
13 #include "artdaq-core-demo/Overlays/FragmentType.hh"
14 
15 #include "fhiclcpp/ParameterSet.h"
16 
17 #include <fstream>
18 #include <iomanip>
19 #include <iterator>
20 #include <iostream>
21 
22 #include <unistd.h>
23 #define TRACE_NAME "ToySimulator"
24 #include "tracemf.h" // TRACE, TLOG*
25 #include "cetlib_except/exception.h"
26 
27 demo::ToySimulator::ToySimulator(fhicl::ParameterSet const& ps)
28  :
29  CommandableFragmentGenerator(ps)
30  , hardware_interface_(new ToyHardwareInterface(ps))
31  , timestamp_(0)
32  , timestampScale_(ps.get<int>("timestamp_scale_factor", 1))
33  , rollover_subrun_interval_(ps.get<int>("rollover_subrun_interval", 0))
34  , metadata_({ 0,0,0 })
35  , readout_buffer_(nullptr)
36  , fragment_type_(static_cast<decltype(fragment_type_)>(artdaq::Fragment::InvalidFragmentType))
37  , distribution_type_(static_cast<ToyHardwareInterface::DistributionType>(ps.get<int>("distribution_type")))
38  , generated_fragments_per_event_(ps.get<int>("generated_fragments_per_event", 1))
39  , exception_on_config_(ps.get<bool>("exception_on_config", false))
40  , dies_on_config_(ps.get<bool>("dies_on_config", false))
41 
42 {
43  hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
44 
45  if (exception_on_config_) {
46  throw cet::exception("ToySimulator") << "This is an engineered exception designed for testing purposes, set by the exception_on_config FHiCL variable";
47  } else if (dies_on_config_) {
48  TLOG(TLVL_ERROR) << "This is an engineered process death, set by the dies_on_config FHiCL variable";
49  std::exit(1);
50  }
51 
52  metadata_.board_serial_number = hardware_interface_->SerialNumber() & 0xFFFF;
53  metadata_.num_adc_bits = hardware_interface_->NumADCBits();
54  TLOG(TLVL_INFO) << "Constructor: metadata_.unused = 0x" << std::hex << metadata_.unused << " sizeof(metadata_) = " << std::dec << sizeof(metadata_);
55 
56  switch (hardware_interface_->BoardType())
57  {
58  case 1002:
59  fragment_type_ = toFragmentType("TOY1");
60  break;
61  case 1003:
62  fragment_type_ = toFragmentType("TOY2");
63  break;
64  default:
65  throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware";
66  }
67 }
68 
70 {
71  hardware_interface_->FreeReadoutBuffer(readout_buffer_);
72 }
73 
74 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
75 {
76  if (should_stop())
77  {
78  return false;
79  }
80 
81  // ToyHardwareInterface (an instance to which "hardware_interface_"
82  // is a unique_ptr object) is just one example of the sort of
83  // interface a hardware library might offer. For example, other
84  // interfaces might require you to allocate and free the memory used
85  // to store hardware data in your generator using standard C++ tools
86  // (rather than via the "AllocateReadoutBuffer" and
87  // "FreeReadoutBuffer" functions provided here), or could have a
88  // function which directly returns a pointer to the data buffer
89  // rather than sticking the data in the location pointed to by your
90  // pointer (which is what happens here with readout_buffer_)
91 
92  std::size_t bytes_read = 0;
93  hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
94 
95  // We'll use the static factory function
96 
97  // artdaq::Fragment::FragmentBytes(std::size_t payload_size_in_bytes, sequence_id_t sequence_id,
98  // fragment_id_t fragment_id, type_t type, const T & metadata)
99 
100  // which will then return a unique_ptr to an artdaq::Fragment
101  // object.
102 
103  for (auto& id : fragmentIDs()) {
104 
105  // The offset logic below is designed to both ensure
106  // backwards compatibility and to (help) avoid collisions
107  // with fragment_ids from other boardreaders if more than
108  // one fragment is generated per event
109 
110  std::unique_ptr<artdaq::Fragment> fragptr(
111  artdaq::Fragment::FragmentBytes(bytes_read,
112  ev_counter(),
113  id,
114  fragment_type_,
115  metadata_, timestamp_));
116  frags.emplace_back(std::move(fragptr));
117 
119  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
120  else
121  {
122  // Must preserve the Header!
123  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, sizeof(ToyFragment::Header));
124  }
125 
126  TLOG(50) << "getNext_ after memcpy " << bytes_read
127  << " bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes() << " metabytes=" << sizeof(metadata_);
128  }
129 
130  if (metricMan != nullptr)
131  {
132  metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3, artdaq::MetricMode::LastPoint);
133  }
134 
135  if (rollover_subrun_interval_ > 0 && ev_counter() % rollover_subrun_interval_ == 0 && fragment_id() == 0)
136  {
137  bool fragmentIdZero = false;
138  for (auto& id : fragmentIDs()) { if (id == 0) fragmentIdZero = true; }
139  if (fragmentIdZero) {
140  artdaq::FragmentPtr endOfSubrunFrag(new artdaq::Fragment(static_cast<size_t>(ceil(sizeof(my_rank) / static_cast<double>(sizeof(artdaq::Fragment::value_type))))));
141  endOfSubrunFrag->setSystemType(artdaq::Fragment::EndOfSubrunFragmentType);
142 
143  endOfSubrunFrag->setSequenceID(ev_counter() + 1);
144  endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
145 
146  *endOfSubrunFrag->dataBegin() = my_rank;
147  frags.emplace_back(std::move(endOfSubrunFrag));
148  }
149  }
150 
151  ev_counter_inc();
152  timestamp_ += timestampScale_;
153 
154  return true;
155 }
156 
157 void demo::ToySimulator::start()
158 {
159  hardware_interface_->StartDatataking();
160  timestamp_ = 0;
161 }
162 
163 void demo::ToySimulator::stop()
164 {
165  hardware_interface_->StopDatataking();
166 }
167 
168 // The following macro is defined in artdaq's GeneratorMacros.hh header
169 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)
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 ...
Definition: ToySimulator.hh:37