Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from LDMX-Software/iss1037-unpack-cleanup
Browse files Browse the repository at this point in the history
Unpacking Clean-Up
  • Loading branch information
tomeichlersmith authored Nov 30, 2021
2 parents 9d7b10c + 9df1a1b commit 0e504ca
Showing 1 changed file with 55 additions and 87 deletions.
142 changes: 55 additions & 87 deletions src/Hcal/HcalRawDecoder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,15 @@
#include "Hcal/HcalDetectorMap.h"
#include "Packing/Utility/BufferReader.h"
#include "Packing/Utility/CRC.h"
#include "Packing/Utility/Hex.h"
#include "Packing/Utility/Mask.h"
#include "Recon/Event/HgcrocDigiCollection.h"

namespace hcal {

namespace utility {

/**
* Read out 32-bit words from a 8-bit buffer.
*/
class Reader {
const std::vector<uint8_t> &buffer_;
std::size_t i_word_;
uint32_t next() {
uint32_t w = buffer_.at(i_word_) |
(buffer_.at(i_word_ + 1) << 8) |
(buffer_.at(i_word_ + 2) << 16) |
(buffer_.at(i_word_ + 3) << 24);
i_word_ += 4;
return w;
}

public:
Reader(const std::vector<uint8_t>& b) : buffer_{b}, i_word_{0} {}
operator bool() {
return (i_word_ < buffer_.size());
}
Reader& operator>>(uint32_t& w) {
if (*this)
w = next();
return *this;
}
}; // Reader

} // namespace utility

namespace debug {

struct hex {
uint32_t word_;
hex(uint32_t w) : word_{w} {}
};

} // namespace debug

inline std::ostream& operator<<(std::ostream& os, const debug::hex& h) {
os << "0x" << std::setfill('0') << std::setw(8) << std::hex << h.word_
<< std::dec;
return os;
}
// we are using 32-bit words here
using hex = packing::utility::hex<uint32_t>;
using BufferReader = packing::utility::BufferReader<uint32_t>;

void HcalRawDecoder::configure(framework::config::Parameters& ps) {
input_name_ = ps.getParameter<std::string>("input_name");
Expand All @@ -76,7 +35,7 @@ void HcalRawDecoder::produce(framework::Event& event) {
/// words for reading and decoding
static uint32_t head1, head2, w;

utility::Reader reader_{event.getCollection<uint8_t>(input_name_, input_pass_)};
BufferReader reader{event.getCollection<uint8_t>(input_name_, input_pass_)};

/** Re-sort the data from grouped by bunch to by channel
*
Expand All @@ -88,38 +47,40 @@ void HcalRawDecoder::produce(framework::Event& event) {
std::map<ldmx::HcalElectronicsID,
std::vector<ldmx::HgcrocDigiCollection::Sample>>
eid_to_samples;
while (reader_ >> head1 >> head2) {
while (reader >> head1 >> head2) {
/// are we reading a buffer from multi-sample per event?
if (head1 == 0x11111111 and head2 == 0xbeef2021) {
/* whole event header word looks like
*
* VERSION (4) | FPGA ID (8) | NSAMPLES (4) | LEN (16)
*/
uint32_t whole_event_header;
reader_ >> whole_event_header;
uint32_t version = (whole_event_header >> 28) & packing::utility::mask<4>;
uint32_t fpga = (whole_event_header >> 20) & packing::utility::mask<8>;
uint32_t nsamples = (whole_event_header >> 16) & packing::utility::mask<4>;
reader >> whole_event_header;
uint32_t version = (whole_event_header >> 28) & packing::utility::mask<4>;
uint32_t fpga = (whole_event_header >> 20) & packing::utility::mask<8>;
uint32_t nsamples =
(whole_event_header >> 16) & packing::utility::mask<4>;
uint32_t eventlen = whole_event_header & packing::utility::mask<16>;

// sample counters
std::vector<uint32_t> length_per_sample(nsamples, 0);
for (uint32_t i_sample{0}; i_sample < nsamples; i_sample++) {
if (i_sample%2 == 0) {
reader_ >> w;
if (i_sample % 2 == 0) {
reader >> w;
}
uint32_t shift_in_word = 16*(i_sample%2);
length_per_sample[i_sample] = (w >> shift_in_word) & packing::utility::mask<12>;
uint32_t shift_in_word = 16 * (i_sample % 2);
length_per_sample[i_sample] =
(w >> shift_in_word) & packing::utility::mask<12>;
}

// read first sample headers
reader_ >> head1 >> head2;
reader >> head1 >> head2;
} else if (head1 == 0xd07e2021 and head2 == 0x12345678) {
// these are the special footer words at the end,
// done with event
break;
}

/** Decode Bunch Header
* We have a few words of header material before the actual data.
* This header material is assumed to be encoded as in Table 3
Expand All @@ -137,9 +98,9 @@ void HcalRawDecoder::produce(framework::Event& event) {
*/
packing::utility::CRC fpga_crc;
fpga_crc << head1;
//std::cout << debug::hex(head1) << " : ";
std::cout << hex(head1) << " : ";
uint32_t version = (head1 >> 28) & packing::utility::mask<4>;
//std::cout << "version " << version << std::flush;
// std::cout << "version " << version << std::flush;
uint32_t one{1};
if (version != one)
EXCEPTION_RAISE("VersMis",
Expand All @@ -149,29 +110,32 @@ void HcalRawDecoder::produce(framework::Event& event) {
uint32_t nlinks = (head1 >> 14) & packing::utility::mask<6>;
uint32_t len = head1 & packing::utility::mask<12>;

//std::cout << ", fpga: " << fpga << ", nlinks: " << nlinks << ", len: " << len << std::endl;
// std::cout << ", fpga: " << fpga << ", nlinks: " << nlinks << ", len: " <<
// len << std::endl;
fpga_crc << head2;
//std::cout << debug::hex(head2) << " : ";
// std::cout << hex(head2) << " : ";

uint32_t bx_id = (head2 >> 20) & packing::utility::mask<12>;
uint32_t rreq = (head2 >> 10) & packing::utility::mask<10>;
uint32_t orbit = head2 & packing::utility::mask<10>;

//std::cout << "bx_id: " << bx_id << ", rreq: " << rreq << ", orbit: " << orbit << std::endl;
// std::cout << "bx_id: " << bx_id << ", rreq: " << rreq << ", orbit: " <<
// orbit << std::endl;

std::vector<uint32_t> length_per_link(nlinks, 0);
for (uint32_t i_link{0}; i_link < nlinks; i_link++) {
if (i_link % 4 == 0) {
reader_ >> w;
reader >> w;
fpga_crc << w;
//std::cout << debug::hex(w) << " : Four Link Pack " << std::endl;
// std::cout << hex(w) << " : Four Link Pack " << std::endl;
}
uint32_t shift_in_word = 8 * (i_link % 4);
bool rid_ok = (w >> (shift_in_word + 7)) & packing::utility::mask<1> == 1;
bool cdc_ok = (w >> (shift_in_word + 6)) & packing::utility::mask<1> == 1;
length_per_link[i_link] =
(w >> shift_in_word) & packing::utility::mask<6>;
//std::cout << " Link " << i_link << " readout " << length_per_link.at(i_link) << " channels" << std::endl;
// std::cout << " Link " << i_link << " readout " <<
// length_per_link.at(i_link) << " channels" << std::endl;
}

/** Decode Each Link in Sequence
Expand All @@ -185,25 +149,26 @@ void HcalRawDecoder::produce(framework::Event& event) {

for (uint32_t i_link{0}; i_link < nlinks; i_link++) {
// move on from last word counting links or previous link
//std::cout << "RO Link " << i_link << std::endl;
// std::cout << "RO Link " << i_link << std::endl;
packing::utility::CRC link_crc;
reader_ >> w;
reader >> w;
fpga_crc << w;
link_crc << w;
uint32_t roc_id = (w >> 16) & packing::utility::mask<16>;
bool crc_ok = (w >> 15) & packing::utility::mask<1> == 1;
//std::cout << debug::hex(w) << " : roc_id " << roc_id << ", crc_ok (v2 always false) " << std::boolalpha << crc_ok << std::endl;
// std::cout << hex(w) << " : roc_id " << roc_id << ", crc_ok (v2
// always false) " << std::boolalpha << crc_ok << std::endl;

// get readout map from the last 8 bits of this word
// and the entire next word
std::bitset<40> ro_map = w & packing::utility::mask<8>;
ro_map <<= 32;
reader_ >> w;
reader >> w;
fpga_crc << w;
link_crc << w;
ro_map |= w;

//std::cout << "Start looping through channels..." << std::endl;
// std::cout << "Start looping through channels..." << std::endl;
// loop through channels on this link,
// since some channels may have been suppressed because of low
// amplitude the channel ID is not the same as the index it
Expand All @@ -216,9 +181,9 @@ void HcalRawDecoder::produce(framework::Event& event) {
} while (channel_id < 40 and not ro_map.test(channel_id));

// next word is this channel
reader_ >> w;
reader >> w;
fpga_crc << w;
//std::cout << debug::hex(w) << " " << channel_id;
// std::cout << hex(w) << " " << channel_id;

if (channel_id == 0) {
/** Special "Header" Word from ROC
Expand All @@ -229,7 +194,7 @@ void HcalRawDecoder::produce(framework::Event& event) {
* version 2:
* 10101010 | BXID (12) | WADD (9) | 1010
*/
//std::cout << " : ROC Header";
// std::cout << " : ROC Header";
link_crc << w;
uint32_t bx_id = (w >> 16) & packing::utility::mask<12>;
uint32_t short_event = (w >> 10) & packing::utility::mask<6>;
Expand All @@ -240,11 +205,12 @@ void HcalRawDecoder::produce(framework::Event& event) {
* 10 | 0000000000 | Common Mode ADC 0 (10) | Common Mode ADC 1 (10)
*/
link_crc << w;
//std::cout << " : Common Mode";
// std::cout << " : Common Mode";
} else if (channel_id == 39) {
// CRC checksum from ROC
uint32_t crc = w;
//std::cout << " : CRC checksum : " << debug::hex(link_crc.get()) << " =? " << debug::hex(crc);
// std::cout << " : CRC checksum : " << hex(link_crc.get()) <<
// " =? " << hex(crc);
/*
if (link_crc.get() != crc) {
EXCEPTION_RAISE("BadCRC",
Expand All @@ -266,9 +232,9 @@ void HcalRawDecoder::produce(framework::Event& event) {
* using the link and channel indices.
*/

//std::cout << " : DAQ Channel ";
// std::cout << " : DAQ Channel ";

//std::cout << fpga << " " << roc_id-256 << " " << channel_id << " ";
// std::cout << fpga << " " << roc_id-256 << " " << channel_id << " ";
/**
* The subfields for the electronics ID infrastructure need to start
* from 0 and count up. This means we need to subtract some of the
Expand All @@ -279,21 +245,22 @@ void HcalRawDecoder::produce(framework::Event& event) {
* roc_id-256 is the ssame as i_link = is this a coincidence?
* or should we change the second input to be the link index
*/
ldmx::HcalElectronicsID eid(fpga-1, roc_id-256, channel_id);
//std::cout << eid.index();
ldmx::HcalElectronicsID eid(fpga - 1, roc_id - 256, channel_id);
// std::cout << eid.index();

// copy data into EID->sample map
eid_to_samples[eid].emplace_back(w);
} // type of channel
//std::cout << std::endl;
// std::cout << std::endl;
} // loop over channels (j in Table 4)
//std::cout << "done looping through channels" << std::endl;
// std::cout << "done looping through channels" << std::endl;
} // loop over links

// another CRC checksum from FPGA
reader_ >> w;
reader >> w;
uint32_t crc = w;
//std::cout << "FPGA Checksum : " << debug::hex(fpga_crc.get()) << " =? " << debug::hex(crc) << std::endl;
// std::cout << "FPGA Checksum : " << hex(fpga_crc.get()) << " =? "
// << hex(crc) << std::endl;
/* TODO
* fix calculation of FPGA checksum
* I can't figure out why it isn't matching, but there
Expand Down Expand Up @@ -335,9 +302,9 @@ void HcalRawDecoder::produce(framework::Event& event) {
* no zero supp during test beam on the front-end,
* so channels that aren't connected to anything are still
* being readout.
std::cout << "EID(" << eid.fiber() << "," << eid.elink() << "," << eid.channel() << ") ";
for (auto& s : digi) std::cout << debug::hex(s.raw()) << " ";
std::cout << std::endl;
std::cout << "EID(" << eid.fiber() << "," << eid.elink() << "," <<
eid.channel() << ") "; for (auto& s : digi) std::cout <<
hex(s.raw()) << " "; std::cout << std::endl;
*/
}
}
Expand All @@ -354,7 +321,8 @@ void HcalRawDecoder::produce(framework::Event& event) {
}
}

//std::cout << "adding " << digis.getNumDigis() << " digis each with " << digis.getNumSamplesPerDigi() << " samples to event bus" << std::endl;
// std::cout << "adding " << digis.getNumDigis() << " digis each with " <<
// digis.getNumSamplesPerDigi() << " samples to event bus" << std::endl;
event.add(output_name_, digis);
return;
} // produce
Expand Down

0 comments on commit 0e504ca

Please sign in to comment.