artdaq_demo  v3_04_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/Application/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)) , dies_on_config_(ps.get<bool>("dies_on_config", false))
40 
41 {
42  hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
43 
44  if (exception_on_config_) {
45  throw cet::exception("ToySimulator") << "This is an engineered exception designed for testing purposes, set by the exception_on_config FHiCL variable";
46  } else if (dies_on_config_) {
47  TLOG(TLVL_ERROR) << "This is an engineered process death, set by the dies_on_config FHiCL variable";
48  std::exit(1);
49  }
50 
51  metadata_.board_serial_number = hardware_interface_->SerialNumber() & 0xFFFF;
52  metadata_.num_adc_bits = hardware_interface_->NumADCBits();
53  TLOG(TLVL_INFO) << "Constructor: metadata_.unused = 0x" << std::hex << metadata_.unused << " sizeof(metadata_) = " << std::dec << sizeof(metadata_);
54 
55  switch (hardware_interface_->BoardType())
56  {
57  case 1002:
58  fragment_type_ = toFragmentType("TOY1");
59  break;
60  case 1003:
61  fragment_type_ = toFragmentType("TOY2");
62  break;
63  default:
64  throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware";
65  }
66 }
67 
69 {
70  hardware_interface_->FreeReadoutBuffer(readout_buffer_);
71 }
72 
73 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
74 {
75  if (should_stop())
76  {
77  return false;
78  }
79 
80  // ToyHardwareInterface (an instance to which "hardware_interface_"
81  // is a unique_ptr object) is just one example of the sort of
82  // interface a hardware library might offer. For example, other
83  // interfaces might require you to allocate and free the memory used
84  // to store hardware data in your generator using standard C++ tools
85  // (rather than via the "AllocateReadoutBuffer" and
86  // "FreeReadoutBuffer" functions provided here), or could have a
87  // function which directly returns a pointer to the data buffer
88  // rather than sticking the data in the location pointed to by your
89  // pointer (which is what happens here with readout_buffer_)
90 
91  std::size_t bytes_read = 0;
92  hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
93 
94  // We'll use the static factory function
95 
96  // artdaq::Fragment::FragmentBytes(std::size_t payload_size_in_bytes, sequence_id_t sequence_id,
97  // fragment_id_t fragment_id, type_t type, const T & metadata)
98 
99  // which will then return a unique_ptr to an artdaq::Fragment
100  // object.
101 
102 #if 1
103  for (auto i_f = 0; i_f < generated_fragments_per_event_; ++i_f) {
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  auto offset = i_f == 0 ? 0 : i_f + 10000;
111  std::unique_ptr<artdaq::Fragment> fragptr(
112  artdaq::Fragment::FragmentBytes(bytes_read,
113  ev_counter(),
114  fragment_id() + offset,
115  fragment_type_,
116  metadata_, timestamp_));
117  frags.emplace_back(std::move(fragptr));
118  }
119 #else
120  std::unique_ptr<artdaq::Fragment> fragptr(
121  artdaq::Fragment::FragmentBytes(/*bytes_read*/ 1024 - 40,
122  ev_counter(), fragment_id(),
123  fragment_type_,
124  metadata_, timestamp_));
125  frags.emplace_back(std::move(fragptr));
126  artdaq::detail::RawFragmentHeader *hdr = (artdaq::detail::RawFragmentHeader*)(frags.back()->headerBeginBytes());
127  // Need a way to fake frag->sizeBytes() (which calls frag->size() which calls fragmentHeader()->word_count
128  hdr->word_count = ceil((bytes_read + 32) / static_cast<double>(sizeof(artdaq::RawDataType)));
129 #endif
130 
131  if ( !frags.empty() ) {
132 
134  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
135  else
136  {
137  // Must preserve the Header!
138  memcpy(frags.back()->dataBeginBytes(), readout_buffer_, sizeof(ToyFragment::Header));
139  }
140 
141  TLOG(50) << "getNext_ after memcpy " << bytes_read
142  << " bytes and std::move dataSizeBytes()=" << frags.back()->sizeBytes() << " metabytes=" << sizeof(metadata_);
143  }
144 
145  if (metricMan != nullptr)
146  {
147  metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3, artdaq::MetricMode::LastPoint);
148  }
149 
150  if (rollover_subrun_interval_ > 0 && ev_counter() % rollover_subrun_interval_ == 0 && fragment_id() == 0)
151  {
152  artdaq::FragmentPtr endOfSubrunFrag(new artdaq::Fragment(static_cast<size_t>(ceil(sizeof(my_rank) / static_cast<double>(sizeof(artdaq::Fragment::value_type))))));
153  endOfSubrunFrag->setSystemType(artdaq::Fragment::EndOfSubrunFragmentType);
154 
155  endOfSubrunFrag->setSequenceID(ev_counter() + 1);
156  endOfSubrunFrag->setTimestamp(1 + (ev_counter() / rollover_subrun_interval_));
157 
158  *endOfSubrunFrag->dataBegin() = my_rank;
159  frags.emplace_back(std::move(endOfSubrunFrag));
160  }
161 
162  ev_counter_inc();
163  timestamp_ += timestampScale_;
164 
165  return true;
166 }
167 
168 void demo::ToySimulator::start()
169 {
170  hardware_interface_->StartDatataking();
171  timestamp_ = 0;
172 }
173 
174 void demo::ToySimulator::stop()
175 {
176  hardware_interface_->StopDatataking();
177 }
178 
179 // The following macro is defined in artdaq's GeneratorMacros.hh header
180 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