00001
00002
00003
00004
00005 #include "artdaq-demo/Generators/ToySimulator.hh"
00006
00007 #include "canvas/Utilities/Exception.h"
00008
00009 #include "artdaq/Application/GeneratorMacros.hh"
00010 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
00011
00012 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
00013 #include "artdaq-core-demo/Overlays/FragmentType.hh"
00014
00015
00016 #include "cetlib/exception.h"
00017 #include "fhiclcpp/ParameterSet.h"
00018
00019 #include <fstream>
00020 #include <iomanip>
00021 #include <iterator>
00022 #include <iostream>
00023
00024 #include <unistd.h>
00025 #include "trace.h"
00026
00027 demo::ToySimulator::ToySimulator(fhicl::ParameterSet const& ps)
00028 :
00029 CommandableFragmentGenerator(ps)
00030 , hardware_interface_(new ToyHardwareInterface(ps))
00031 , timestamp_(0)
00032 , timestampScale_(ps.get<int>("timestamp_scale_factor", 1))
00033 , metadata_({0,0,0})
00034 , readout_buffer_(nullptr)
00035 , fragment_type_(static_cast<decltype(fragment_type_)>(artdaq::Fragment::InvalidFragmentType))
00036 , distribution_type_(static_cast<ToyHardwareInterface::DistributionType>(ps.get<int>("distribution_type")))
00037 {
00038 hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
00039
00040 metadata_.board_serial_number = hardware_interface_->SerialNumber();
00041 metadata_.num_adc_bits = hardware_interface_->NumADCBits();
00042 TRACE( 50, "ToySimulator ctor metadata_.unused=0x%zu sizeof(metadata_)=%zd"
00043 , metadata_.unused, sizeof(metadata_) );
00044
00045 switch (hardware_interface_->BoardType())
00046 {
00047 case 1002:
00048 fragment_type_ = toFragmentType("TOY1");
00049 break;
00050 case 1003:
00051 fragment_type_ = toFragmentType("TOY2");
00052 break;
00053 default:
00054 throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware";
00055 }
00056 }
00057
00058 demo::ToySimulator::~ToySimulator()
00059 {
00060 hardware_interface_->FreeReadoutBuffer(readout_buffer_);
00061 }
00062
00063 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
00064 {
00065 if (should_stop())
00066 {
00067 return false;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 std::size_t bytes_read = 0;
00082 hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #if 1
00093 std::unique_ptr<artdaq::Fragment> fragptr(
00094 artdaq::Fragment::FragmentBytes(bytes_read,
00095 ev_counter(), fragment_id(),
00096 fragment_type_,
00097 metadata_, timestamp_));
00098 frags.emplace_back(std::move(fragptr));
00099 #else
00100 std::unique_ptr<artdaq::Fragment> fragptr(
00101 artdaq::Fragment::FragmentBytes( 1024-40,
00102 ev_counter(), fragment_id(),
00103 fragment_type_,
00104 metadata_, timestamp_));
00105 frags.emplace_back(std::move(fragptr));
00106 artdaq::detail::RawFragmentHeader *hdr = (artdaq::detail::RawFragmentHeader*)(frags.back()->headerBeginBytes());
00107
00108 hdr->word_count = ceil( (bytes_read+32) / static_cast<double>(sizeof(artdaq::RawDataType)) );
00109 #endif
00110
00111 if (distribution_type_ != ToyHardwareInterface::DistributionType::uninitialized)
00112 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
00113
00114 TRACE( 50, "ToySimulator::getNext_ after memcpy %zu bytes and std::move dataSizeBytes()=%zu metabytes=%zu", bytes_read, frags.back()->sizeBytes(), sizeof(metadata_) );
00115
00116 if (metricMan != nullptr)
00117 {
00118 metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3, artdaq::MetricMode::LastPoint);
00119 }
00120
00121 ev_counter_inc();
00122 timestamp_ += timestampScale_;
00123
00124 return true;
00125 }
00126
00127 void demo::ToySimulator::start()
00128 {
00129 hardware_interface_->StartDatataking();
00130 }
00131
00132 void demo::ToySimulator::stop()
00133 {
00134 hardware_interface_->StopDatataking();
00135 }
00136
00137
00138 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)