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 , readout_buffer_(nullptr)
00034 , fragment_type_(static_cast<decltype(fragment_type_)>(artdaq::Fragment::InvalidFragmentType))
00035 , distribution_type_(static_cast<ToyHardwareInterface::DistributionType>(ps.get<int>("distribution_type")))
00036 {
00037 hardware_interface_->AllocateReadoutBuffer(&readout_buffer_);
00038
00039 metadata_.board_serial_number = hardware_interface_->SerialNumber();
00040 metadata_.num_adc_bits = hardware_interface_->NumADCBits();
00041
00042 switch (hardware_interface_->BoardType())
00043 {
00044 case 1002:
00045 fragment_type_ = toFragmentType("TOY1");
00046 break;
00047 case 1003:
00048 fragment_type_ = toFragmentType("TOY2");
00049 break;
00050 default:
00051 throw cet::exception("ToySimulator") << "Unable to determine board type supplied by hardware";
00052 }
00053 }
00054
00055 demo::ToySimulator::~ToySimulator()
00056 {
00057 hardware_interface_->FreeReadoutBuffer(readout_buffer_);
00058 }
00059
00060 bool demo::ToySimulator::getNext_(artdaq::FragmentPtrs& frags)
00061 {
00062 if (should_stop())
00063 {
00064 return false;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 std::size_t bytes_read = 0;
00079 hardware_interface_->FillBuffer(readout_buffer_, &bytes_read);
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #if 1
00090 std::unique_ptr<artdaq::Fragment> fragptr(
00091 artdaq::Fragment::FragmentBytes(bytes_read,
00092 ev_counter(), fragment_id(),
00093 fragment_type_,
00094 metadata_, timestamp_));
00095 frags.emplace_back(std::move(fragptr));
00096 #else
00097 std::unique_ptr<artdaq::Fragment> fragptr(
00098 artdaq::Fragment::FragmentBytes( 1024-40,
00099 ev_counter(), fragment_id(),
00100 fragment_type_,
00101 metadata_, timestamp_));
00102 frags.emplace_back(std::move(fragptr));
00103 artdaq::detail::RawFragmentHeader *hdr = (artdaq::detail::RawFragmentHeader*)(frags.back()->headerBeginBytes());
00104
00105 hdr->word_count = ceil( (bytes_read+32) / static_cast<double>(sizeof(artdaq::RawDataType)) );
00106 #endif
00107
00108 if (distribution_type_ != ToyHardwareInterface::DistributionType::uninitialized)
00109 memcpy(frags.back()->dataBeginBytes(), readout_buffer_, bytes_read);
00110
00111 TRACE( 50, "ToySimulator::getNext_ after memcpy %zu bytes and std::move dataSizeBytes()=%zu metabytes=%zu", bytes_read, frags.back()->sizeBytes(), sizeof(metadata_) );
00112
00113 if (metricMan != nullptr)
00114 {
00115 metricMan->sendMetric("Fragments Sent", ev_counter(), "Events", 3);
00116 }
00117
00118 ev_counter_inc();
00119 timestamp_ += timestampScale_;
00120
00121 return true;
00122 }
00123
00124 void demo::ToySimulator::start()
00125 {
00126 hardware_interface_->StartDatataking();
00127 }
00128
00129 void demo::ToySimulator::stop()
00130 {
00131 hardware_interface_->StopDatataking();
00132 }
00133
00134
00135 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(demo::ToySimulator)