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 #include "fhiclcpp/ParameterSet.h"
00016
00017 #include <fstream>
00018 #include <iomanip>
00019 #include <iterator>
00020 #include <iostream>
00021
00022 #include <unistd.h>
00023 #define TRACE_NAME "ToySimulator"
00024 #include "tracemf.h"
00025 #include "cetlib_except/exception.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( TLVL_INFO, "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 TLOG_ARB(50, "ToySimulator") << "ToySimulator::getNext_ after memcpy " << std::to_string(bytes_read)
00115 << " bytes and std::move dataSizeBytes()=" << std::to_string(frags.back()->sizeBytes()) << " metabytes=" << std::to_string(sizeof(metadata_)) << TLOG_ENDL;
00116
00117 if (metricMan != nullptr)
00118 {
00119 metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3, artdaq::MetricMode::LastPoint);
00120 }
00121
00122 ev_counter_inc();
00123 timestamp_ += timestampScale_;
00124
00125 return true;
00126 }
00127
00128 void demo::ToySimulator::start()
00129 {
00130 hardware_interface_->StartDatataking();
00131 }
00132
00133 void demo::ToySimulator::stop()
00134 {
00135 hardware_interface_->StopDatataking();
00136 }
00137
00138
00139 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)