DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::trgtools::EmulationUnit< T, U, V > Class Template Reference

#include <EmulationUnit.hpp>

Public Member Functions

uint64_t emulate (const input_t &input, std::vector< output_t > &outputs)
 
std::unique_ptr< daqdataformats::Fragmentemulate_vector (const std::vector< input_t > &inputs)
 
std::vector< output_tget_last_output_buffer ()
 
void set_maker (std::unique_ptr< maker_t > &maker)
 
void set_timing_file (const std::string &file_name)
 
void write_csv_header (const std::string &header)
 

Private Types

using input_t = T
 
using output_t = U
 
using maker_t = V
 

Private Attributes

std::unique_ptr< maker_tm_maker
 
std::vector< output_tm_last_output_buffer
 
std::string m_timing_file_name
 

Detailed Description

template<typename T, typename U, typename V>
class dunedaq::trgtools::EmulationUnit< T, U, V >

Definition at line 27 of file EmulationUnit.hpp.

Member Typedef Documentation

◆ input_t

template<typename T , typename U , typename V >
using dunedaq::trgtools::EmulationUnit< T, U, V >::input_t = T
private

Definition at line 29 of file EmulationUnit.hpp.

◆ maker_t

template<typename T , typename U , typename V >
using dunedaq::trgtools::EmulationUnit< T, U, V >::maker_t = V
private

Definition at line 31 of file EmulationUnit.hpp.

◆ output_t

template<typename T , typename U , typename V >
using dunedaq::trgtools::EmulationUnit< T, U, V >::output_t = U
private

Definition at line 30 of file EmulationUnit.hpp.

Member Function Documentation

◆ emulate()

template<typename T , typename U , typename V >
uint64_t dunedaq::trgtools::EmulationUnit< T, U, V >::emulate ( const input_t & input,
std::vector< output_t > & outputs )

Definition at line 108 of file EmulationUnit.hxx.

108 {
109 auto time_start = std::chrono::steady_clock::now();
110 (*m_maker)(input, outputs); // Feed TX into the TXMaker
111 auto time_end = std::chrono::steady_clock::now();
112
113 uint64_t time_diff = std::chrono::nanoseconds(time_end - time_start).count();
114 return time_diff;
115}

◆ emulate_vector()

template<typename T , typename U , typename V >
std::unique_ptr< daqdataformats::Fragment > dunedaq::trgtools::EmulationUnit< T, U, V >::emulate_vector ( const std::vector< input_t > & inputs)

Definition at line 18 of file EmulationUnit.hxx.

18 {
19 // Create the output.
20 std::vector<U> output_buffer;
21 std::vector<U> temp_buffer;
22
23 // Create the output tp variables
24 std::vector<uint64_t> time_diffs;
25 // TODO: Figure out a way of saving channelid if TPs. TCs don't have it
26 std::vector<uint64_t> tp_adc_integral;
27 std::vector<uint64_t> tp_time_start;
28 std::vector<int> is_last_tp_in_ta;
29 // Only pre-allocate memory if we're actually saving the latencies
30 if (!m_timing_file_name.empty()) {
31 time_diffs.reserve(inputs.size());
32 tp_adc_integral.reserve(inputs.size());
33 tp_time_start.reserve(inputs.size());
34 is_last_tp_in_ta.reserve(inputs.size());
35 }
36
37 for (const T& input : inputs) {
38 size_t output_buffer_size = output_buffer.size();
39 uint64_t time_diff = emulate(input, temp_buffer);
40 if (temp_buffer.size() != 0) {
41 output_buffer.insert(output_buffer.end(), temp_buffer.begin(), temp_buffer.end());
42 temp_buffer.clear();
43 }
44 // 1 if it's the TP that creates a TA, 0 otherwise
45 int last_tp_in_ta = (output_buffer_size == output_buffer.size()) ? 0 : 1;
46
47 // Don't save any times if we're not saving latencies
48 if (m_timing_file_name.empty())
49 continue;
50 time_diffs.push_back(time_diff);
51
52 // Don't save time_start, number of TPs etc. unless it's TA latencies per TP
53 if (!std::is_same<T, dunedaq::trgdataformats::TriggerPrimitive>::value)
54 continue;
55
56 tp_time_start.push_back(input.time_start);
57 tp_adc_integral.push_back(input.adc_integral);
58 is_last_tp_in_ta.push_back(last_tp_in_ta);
59 }
60
61 // Write the timings for each new TP in this fragment.
62 if (!m_timing_file_name.empty()) {
63 std::fstream timings;
64 timings.open(m_timing_file_name, std::ios::out | std::ios::app);
65 for (size_t i = 0; i < time_diffs.size(); i++) {
66 if (std::is_same<T, dunedaq::trgdataformats::TriggerPrimitive>::value) {
67 timings << tp_time_start[i] << "," << tp_adc_integral[i] << ","
68 << time_diffs[i] << "," << is_last_tp_in_ta[i] << "\n";
69 }
70 else {
71 timings << time_diffs[i] << "\n";
72 }
73 }
74 timings.close();
75 }
76
77 // Get the size to save on.
78 size_t payload_size(0);
79 for (const U& output : output_buffer) {
80 payload_size += triggeralgs::get_overlay_nbytes(output);
81 }
82
83 // Don't save empty fragments.
84 // The incomplete TX contents will get pushed onto the next fragment.
85 if (payload_size == 0)
86 return nullptr;
87
88 // Awkward type conversion to avoid compiler complaints on void* arithmetic.
89 char* payload = static_cast<char*>(malloc(payload_size));
90 size_t payload_offset(0);
91 for (const U& output : output_buffer) {
92 triggeralgs::write_overlay(output, static_cast<void*>(payload + payload_offset));
93 payload_offset += triggeralgs::get_overlay_nbytes(output);
94 }
95
96 // Hand it to a fragment,
97 std::unique_ptr<daqdataformats::Fragment> frag
98 = std::make_unique<daqdataformats::Fragment>(static_cast<void*>(payload), payload_size);
99 // And release it.
100 free(static_cast<void*>(payload));
101
102 m_last_output_buffer = output_buffer;
103 return frag;
104}
uint64_t emulate(const input_t &input, std::vector< output_t > &outputs)
std::vector< output_t > m_last_output_buffer
void write_overlay(const Object &object, void *buffer)
size_t get_overlay_nbytes(const Object &object)

◆ get_last_output_buffer()

template<typename T , typename U , typename V >
std::vector< U > dunedaq::trgtools::EmulationUnit< T, U, V >::get_last_output_buffer ( )

Definition at line 119 of file EmulationUnit.hxx.

119 {
121}

◆ set_maker()

template<typename T , typename U , typename V >
void dunedaq::trgtools::EmulationUnit< T, U, V >::set_maker ( std::unique_ptr< maker_t > & maker)
inline

Definition at line 42 of file EmulationUnit.hpp.

42{ m_maker = std::move(maker); }
std::unique_ptr< maker_t > m_maker

◆ set_timing_file()

template<typename T , typename U , typename V >
void dunedaq::trgtools::EmulationUnit< T, U, V >::set_timing_file ( const std::string & file_name)
inline

Definition at line 43 of file EmulationUnit.hpp.

43{ m_timing_file_name = file_name; }

◆ write_csv_header()

template<typename T , typename U , typename V >
void dunedaq::trgtools::EmulationUnit< T, U, V >::write_csv_header ( const std::string & header)
inline

Definition at line 44 of file EmulationUnit.hpp.

44 {
45 std::fstream file_header;
46 file_header.open(m_timing_file_name, std::ios::out | std::ios::app);
47 file_header << header << "\n";
48 file_header.close();
49 }

Member Data Documentation

◆ m_last_output_buffer

template<typename T , typename U , typename V >
std::vector<output_t> dunedaq::trgtools::EmulationUnit< T, U, V >::m_last_output_buffer
private

Definition at line 35 of file EmulationUnit.hpp.

◆ m_maker

template<typename T , typename U , typename V >
std::unique_ptr<maker_t> dunedaq::trgtools::EmulationUnit< T, U, V >::m_maker
private

Definition at line 34 of file EmulationUnit.hpp.

◆ m_timing_file_name

template<typename T , typename U , typename V >
std::string dunedaq::trgtools::EmulationUnit< T, U, V >::m_timing_file_name
private

Definition at line 36 of file EmulationUnit.hpp.


The documentation for this class was generated from the following files: