Skip to content

Commit 0e311b4

Browse files
start outline of live decoding
looking at templating over DaqRunConsumer derived classes, getting complicated quickly unfortunately
1 parent 00003ce commit 0e311b4

File tree

4 files changed

+69
-39
lines changed

4 files changed

+69
-39
lines changed

app/tool/algorithm/level_pedestals.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
* Calib and Common Mode channels are ignored.
1919
* TOT/TOA and the sample Tp/Tc flags are ignored.
2020
*/
21+
template<class EventPacket>
2122
static std::array<int, 72> get_adc_medians(
22-
const std::vector<pflib::packing::SingleROCEventPacket>& data) {
23+
const std::vector<EventPacket>& data) {
2324
std::array<int, 72> medians;
2425
/// reserve a vector of the appropriate size to avoid repeating allocation
2526
/// time for all 72 channels

app/tool/daq.cxx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,34 @@ static void daq(const std::string& cmd, Target* pft) {
350350
pftool::readline_bool("Should we decode the packet into CSV?", true);
351351

352352
if (decoding) {
353-
DecodeAndWriteToCSV writer{all_channels_to_csv(fname + ".csv")};
354-
daq_run(pft, cmd, writer, nevents, pftool::state.daq_rate);
353+
std::unique_ptr<DaqRunConsumer> consumer;
354+
switch(pftool::state.daq_format_mode) {
355+
case Target::DaqFormat::ECOND_SW_HEADERS:
356+
consumer = std::make_unique<DecodeAndWriteToCSV<pflib::packing::SoftWrappedECONDEventPacket>>(
357+
fname+".csv",
358+
[](std::ofstream& f) {
359+
f << std::boolalpha;
360+
f << pflib::packing::SoftWrappedECONDEventPacket::to_csv_header << '\n';
361+
},
362+
[](std::ofstream& f, const pflib::packing::SoftWrappedECONDEventPacket& ep) {
363+
ep.to_csv(f);
364+
});
365+
break;
366+
case Target::DaqFormat::SIMPLEROC:
367+
consumer = std::make_unique<DecodeAndWriteToCSV<pflib::packing::SingleROCEventPacket>>(
368+
fname+".csv",
369+
[](std::ofstream& f) {
370+
f << std::boolalpha;
371+
f << pflib::packing::SingleROCEventPacket::to_csv_header << '\n';
372+
},
373+
[](std::ofstream& f, const pflib::packing::SingleROCEventPacket& ep) {
374+
ep.to_csv(f);
375+
});
376+
break;
377+
default:
378+
PFEXCEPTION_RAISE("BadConf", "Unable to do live decoding for the currently configured format.");
379+
}
380+
daq_run(pft, cmd, *consumer, nevents, pftool::state.daq_rate);
355381
} else {
356382
WriteToBinaryFile writer{fname + ".raw"};
357383
daq_run(pft, cmd, writer, nevents, pftool::state.daq_rate);

app/tool/daq_run.cxx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ void WriteToBinaryFile::consume(std::vector<uint32_t>& event) {
7777
fwrite(&(event[0]), sizeof(uint32_t), event.size(), fp_);
7878
}
7979

80-
void DecodeAndWrite::consume(std::vector<uint32_t>& event) {
80+
template<class EventPacket>
81+
void DecodeAndWrite<EventPacket>::consume(std::vector<uint32_t>& event) {
8182
// we have to manually check the size so that we can do the reinterpret_cast
8283
if (event.size() == 0) {
8384
pflib_log(warn) << "event with zero words passed in, skipping";
@@ -91,58 +92,62 @@ void DecodeAndWrite::consume(std::vector<uint32_t>& event) {
9192
write_event(ep_);
9293
}
9394

94-
DecodeAndWriteToCSV::DecodeAndWriteToCSV(
95+
template<class EventPacket>
96+
DecodeAndWriteToCSV<EventPacket>::DecodeAndWriteToCSV<EventPacket>(
9597
const std::string& file_name,
9698
std::function<void(std::ofstream&)> write_header,
97-
std::function<void(std::ofstream& f,
98-
const pflib::packing::SingleROCEventPacket&)>
99+
std::function<void(std::ofstream& f, const EventPacket&)>
99100
write_event)
100-
: DecodeAndWrite(), file_{file_name}, write_event_{write_event} {
101+
: DecodeAndWrite<EventPacket>(), file_{file_name}, write_event_{write_event} {
101102
if (not file_) {
102103
PFEXCEPTION_RAISE("FileOpen",
103104
"unable to open " + file_name + " for writing");
104105
}
105106
write_header(file_);
106107
}
107108

108-
void DecodeAndWriteToCSV::write_event(
109-
const pflib::packing::SingleROCEventPacket& ep) {
109+
template<class EventPacket>
110+
void DecodeAndWriteToCSV<EventPacket>::write_event(const EventPacket& ep) {
110111
write_event_(file_, ep);
111112
}
112113

113-
DecodeAndWriteToCSV all_channels_to_csv(const std::string& file_name) {
114-
return DecodeAndWriteToCSV(
114+
template<class EventPacket>
115+
DecodeAndWriteToCSV<EventPacket> all_channels_to_csv(const std::string& file_name) {
116+
return DecodeAndWriteToCSV<EventPacket>(
115117
file_name,
116118
[](std::ofstream& f) {
117119
f << std::boolalpha;
118-
f << pflib::packing::SingleROCEventPacket::to_csv_header << '\n';
120+
f << EventPacket::to_csv_header << '\n';
119121
},
120-
[](std::ofstream& f, const pflib::packing::SingleROCEventPacket& ep) {
122+
[](std::ofstream& f, const EventPacket& ep) {
121123
ep.to_csv(f);
122124
});
123125
}
124126

125-
DecodeAndBuffer::DecodeAndBuffer(int nevents) : DecodeAndWrite() {
127+
template<class EventPacket>
128+
DecodeAndBuffer<EventPacket>::DecodeAndBuffer<EventPacket>(int nevents) : DecodeAndWrite<EventPacket>() {
126129
set_buffer_size(nevents);
127130
}
128131

129-
void DecodeAndBuffer::write_event(
130-
const pflib::packing::SingleROCEventPacket& ep) {
132+
template<class EventPacket>
133+
void DecodeAndBuffer<EventPacket>::write_event(const EventPacket& ep) {
131134
if (ep_buffer_.size() > ep_buffer_.capacity()) {
132135
pflib_log(warn) << "Trying to push more elements to buffer than allocated "
133136
"capacity. Skipping!";
134137
return;
135138
}
136139
ep_buffer_.push_back(ep);
137140
}
141+
142+
template<class EventPacket>
143+
void DecodeAndBuffer<EventPacket>::start_run() { ep_buffer_.clear(); }
138144

139-
void DecodeAndBuffer::start_run() { ep_buffer_.clear(); }
140-
141-
const std::vector<pflib::packing::SingleROCEventPacket>&
142-
DecodeAndBuffer::get_buffer() const {
145+
template<class EventPacket>
146+
const std::vector<EventPacket>& DecodeAndBuffer<EventPacket>::get_buffer() const {
143147
return ep_buffer_;
144148
}
145149

146-
void DecodeAndBuffer::set_buffer_size(int nevents) {
150+
template<class EventPacket>
151+
void DecodeAndBuffer<EventPacket>::set_buffer_size(int nevents) {
147152
ep_buffer_.reserve(nevents);
148153
}

app/tool/daq_run.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class WriteToBinaryFile : public DAQRunConsumer {
5555
* other code just needs to write functions that define how the
5656
* decoded data should be written out.
5757
*/
58+
template<class EventPacket>
5859
class DecodeAndWrite : public DAQRunConsumer {
5960
public:
6061
virtual ~DecodeAndWrite() = default;
@@ -65,43 +66,40 @@ class DecodeAndWrite : public DAQRunConsumer {
6566
virtual void consume(std::vector<uint32_t>& event) final;
6667

6768
/// pure virtual function for writing out decoded event
68-
virtual void write_event(const pflib::packing::SingleROCEventPacket& ep) = 0;
69+
virtual void write_event(const EventPacket& ep) = 0;
6970

7071
protected:
7172
/// logging for warning messages on empty events
7273
mutable ::pflib::logging::logger the_log_;
7374

7475
private:
7576
/// event packet for decoding
76-
pflib::packing::SingleROCEventPacket ep_;
77+
EventPacket ep_;
7778
};
7879

7980
/**
8081
* specializatin of DecodeAndWrite that holds a std::ofstream
8182
* for the user with functions for writing the header and events
8283
*/
83-
class DecodeAndWriteToCSV : public DecodeAndWrite {
84+
template<class EventPacket>
85+
class DecodeAndWriteToCSV : public DecodeAndWrite<EventPacket> {
8486
/// output file writing to
8587
std::ofstream file_;
8688
/// function that writes row(s) to csv given an event
87-
std::function<void(std::ofstream&,
88-
const pflib::packing::SingleROCEventPacket&)>
89-
write_event_;
89+
std::function<void(std::ofstream&, const EventPacket&)> write_event_;
9090

9191
public:
9292
DecodeAndWriteToCSV(
9393
const std::string& file_name,
9494
std::function<void(std::ofstream&)> write_header,
95-
std::function<void(std::ofstream&,
96-
const pflib::packing::SingleROCEventPacket&)>
97-
write_event);
95+
std::function<void(std::ofstream&, EventPacket&)> write_event);
9896
virtual ~DecodeAndWriteToCSV() = default;
9997
/// call write_event with our file handle
100-
virtual void write_event(
101-
const pflib::packing::SingleROCEventPacket& ep) final;
98+
virtual void write_event(const EventPacket& ep) final;
10299
};
103100

104-
DecodeAndWriteToCSV all_channels_to_csv(const std::string& file_name);
101+
template<class EventPacket>
102+
DecodeAndWriteToCSV<EventPacket> all_channels_to_csv(const std::string& file_name);
105103

106104
/**
107105
* Consume an event packet, decode it, and save to buffer.
@@ -119,21 +117,21 @@ DecodeAndWriteToCSV all_channels_to_csv(const std::string& file_name);
119117
* const auto& events{buffer.get_buffer()};
120118
* ```
121119
*/
122-
class DecodeAndBuffer : public DecodeAndWrite {
120+
template<typename EventPacket>
121+
class DecodeAndBuffer : public DecodeAndWrite<EventPacket> {
123122
public:
124123
DecodeAndBuffer(int nevents);
125124
virtual ~DecodeAndBuffer() = default;
126125
/// get buffer
127-
const std::vector<pflib::packing::SingleROCEventPacket>& get_buffer() const;
126+
const std::vector<EventPacket>& get_buffer() const;
128127
/// Set the buffer size
129128
void set_buffer_size(int nevents);
130129
/// Save to buffer
131-
virtual void write_event(
132-
const pflib::packing::SingleROCEventPacket& ep) override;
130+
virtual void write_event(const EventPacket& ep) override;
133131
/// Check that the buffer was read and flushed since last run
134132
virtual void start_run() override;
135133

136134
private:
137135
/// Buffer for event packets
138-
std::vector<pflib::packing::SingleROCEventPacket> ep_buffer_;
136+
std::vector<EventPacket> ep_buffer_;
139137
};

0 commit comments

Comments
 (0)