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 TLOG(TLVL_INFO) << "Constructor: metadata_.unused = 0x" << std::hex << metadata_.unused << " sizeof(metadata_) = " << std::dec << sizeof(metadata_);
00043
00044 switch (hardware_interface_->BoardType())
00045 {
00046 case 1002:
00047 fragment_type_ = toFragmentType("TOY1");
00048 break;
00049 case 1003:
00050 fragment_type_ = toFragmentType("TOY2");
00051 break;
00052 default:
00053 throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware";
00054 }
00055 }
00056
00057 demo::ToySimulator::~ToySimulator()
00058 {
00059 hardware_interface_->FreeReadoutBuffer(readout_buffer_);
00060 }
00061
00062 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
00063 {
00064 if (should_stop())
00065 {
00066 return false;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 std::size_t bytes_read = 0;
00081 hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 #if 1
00092 std::unique_ptr<artdaq::Fragment> fragptr(
00093 artdaq::Fragment::FragmentBytes(bytes_read,
00094 ev_counter(), fragment_id(),
00095 fragment_type_,
00096 metadata_, timestamp_));
00097 frags.emplace_back(std::move(fragptr));
00098 #else
00099 std::unique_ptr<artdaq::Fragment> fragptr(
00100 artdaq::Fragment::FragmentBytes( 1024 - 40,
00101 ev_counter(), fragment_id(),
00102 fragment_type_,
00103 metadata_, timestamp_));
00104 frags.emplace_back(std::move(fragptr));
00105 artdaq::detail::RawFragmentHeader *hdr = (artdaq::detail::RawFragmentHeader*)(frags.back()->headerBeginBytes());
00106
00107 hdr->word_count = ceil((bytes_read + 32) / static_cast<double>(sizeof(artdaq::RawDataType)));
00108 #endif
00109
00110 if (distribution_type_ != ToyHardwareInterface::DistributionType::uninitialized)
00111 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
00112
00113 TLOG(50) << "getNext_ after memcpy " << std::to_string(bytes_read)
00114 << " bytes and std::move dataSizeBytes()=" << std::to_string(frags.back()->sizeBytes()) << " metabytes=" << std::to_string(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 timestamp_ = 0;
00131 }
00132
00133 void demo::ToySimulator::stop()
00134 {
00135 hardware_interface_->StopDatataking();
00136 }
00137
00138
00139 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)