Skip to content
Merged
134 changes: 71 additions & 63 deletions plugins/dogma/dldprint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@

#include "dogma/api.h"
#include "dogma/tdc5.h"
#include "dogma/ur-unpacker.h"
#include "dabc/Url.h"
#include "dabc/api.h"

#include "../hadaq/tdc_print_code.cxx"


#include <unordered_map>

int usage(const char *errstr = nullptr)
{
Expand Down Expand Up @@ -95,82 +96,87 @@ bool is_tdc(unsigned id)
std::map<uint32_t, TuStat> tu_stats;
uint32_t ref_addr = 0;

ur_config cfg_2051;

void print_tu(dogma::DogmaTu *tu, const char *prefix = "")
std::unordered_map<int, ur_config> cfgs;

void _init_ur_config()
{
unsigned epoch0 = 0, coarse0 = 0;
cfg_2051.coarsetime_len = 18;
cfg_2051.finetime_len = 11;
cfg_2051.tdc_type = 3;
cfg_2051.freq = 150;
cfg_2051.has_edge_type = true;

cfgs[2051] = cfg_2051;
}


void print_tu(dogma::DogmaTu *tu, const char *prefix = "")
{
if (!onlytdc || (onlytdc == tu->GetAddr())) {
epoch0 = tu->GetTrigTime() & 0xfffffff;
coarse0 = tu->GetLocalTrigTime() & 0x7ff;
printf("%sTu addr:%06x", prefix, (unsigned)tu->GetAddr());
if (tu->IsMagic())
printf(" magic:%02x", (unsigned) tu->GetMagicType());
else if(tu->IsMagicTdc5())
printf(" tdc5:%04x", (unsigned) tu->GetMagic() & 0xffff);
else
printf(" unkn:%08x", (unsigned) tu->GetMagic());

printf(" trigtype:%02x trignum:%06x epoch0:%u tc0:%03x err:%02x frame:%02x paylod:%04x size:%u\n",
auto has_config = cfgs.find(tu->GetDeviceId()) != cfgs.end();

printf("%sTu addr:%08x devid:%s%04x%s trigtype:%02x trignum:%06x rawsz:%u\n",
prefix, (unsigned)tu->GetAddr(),
getCol(has_config ? col_GREEN : col_RED), (unsigned)tu->GetDeviceId(), getCol(col_RESET),
(unsigned)tu->GetTrigType(), (unsigned)tu->GetTrigNumber(),
(unsigned)tu->GetTrigTime() & 0xfffffff, (unsigned)tu->GetLocalTrigTime() & 0x7ff,
(unsigned)tu->GetErrorBits(), (unsigned)tu->GetFrameBits(), (unsigned)tu->GetPayloadLen(), (unsigned) tu->GetSize());
(unsigned)tu->GetRawPacketSize());
}

unsigned len = tu->GetPayloadLen();

if (printraw) {
unsigned len = tu->GetPayloadLen();
printf("%s", prefix);
for (unsigned i = 0; i < len; ++i) {
printf(" %08x", (unsigned) tu->GetPayload(i));
if ((i == len - 1) || (i % 8 == 7))
printf(" %02x", (unsigned) tu->GetPayload(i));
if ((i == len - 1) || (i % 32 == 31))
printf("\n%s", i < len-1 ? prefix : "");
}
} else if (is_tdc(tu->GetAddr()) || (onlytdc && (onlytdc == tu->GetAddr()))) {
if (tu->IsMagicDefault()) {
// this is convential TDC
std::vector<uint32_t> data(len, 0);
for (unsigned i = 0; i < len; ++i)
data[i] = tu->GetPayload(i);
unsigned errmask = 0;
PrintTdcDataPlain(0, data, strlen(prefix) + 3, errmask, false, epoch0, coarse0);
} else if (tu->IsMagicTdc5()) {
// this is new TDC5
tdc5_header h;
tdc5_parse_it it;
tdc5_time tm;
const char *buf = (const char *) tu;
int pktlen = (int) tu->GetTdc5PaketLength();
tdc5_parse_header(&h, &it, buf, pktlen);

double last_rising_tm = 0.;
int last_rising_ch = -1;
printf("%s Trigger time: %12.9fs\n", prefix, h.trig_time * coarse_tmlen5 * 1e-9); // time in seconds

// keep for debug purposes
if (h.trig_time != tu->GetTdc5TrigTime())
printf("%s DECODING TRIGGER TIME FAILURE 0x%016lx 0x%016lx\n", prefix, (long unsigned) h.trig_time, (long unsigned) tu->GetTdc5TrigTime());
while (tdc5_parse_next(&tm, &it, buf, pktlen) == 1) {
unsigned fine = tm.fine;
if (fine < fine_min5)
fine = fine_min5;
else if (fine > fine_max5)
fine = fine_max5;
double fulltm = -coarse_tmlen5 * (tm.coarse + (0. + fine - fine_min5) / (0. + fine_max5 - fine_min5));
printf("%s ch:%02u falling:%1d coarse:%04u fine:%03u fulltm:%7.3f",
prefix, (unsigned) tm.channel, tm.is_falling,
(unsigned) tm.coarse, (unsigned) tm.fine, fulltm);
if (tm.is_falling && (last_rising_ch == tm.channel))
printf(" ToT:%5.3f", fulltm - last_rising_tm);

printf("\n");
if (!tm.is_falling) {
last_rising_tm = fulltm;
last_rising_ch = tm.channel;
} else {
last_rising_tm = 0;
last_rising_ch = -1;
}
// this is new TDC5

ur_context it;
ur_header h;
ur_time tm;
unsigned devid = tu->GetDeviceId();
auto entry = cfgs.find(devid);
if (entry != cfgs.end())
ur_set_config(&it, &entry->second);
else
ur_set_config(&it, &cfg_2051);

const char *buf = (const char *) tu->RawHeader();
int pktlen = tu->GetRawPacketSize();
ur_parse_header(&h, &it, buf, pktlen);

double last_rising_tm = 0.;
int last_rising_ch = -1;
printf("%s Trigger time: %12.9fs\n", prefix, h.trig_time * coarse_tmlen5 * 1e-9); // time in seconds

// keep for debug purposes
// if (h.trig_time != tu->GetTdc5TrigTime())
// printf("%s DECODING TRIGGER TIME FAILURE 0x%016lx 0x%016lx\n", prefix, (long unsigned) h.trig_time, (long unsigned) tu->GetTdc5TrigTime());
while (ur_parse_next(&tm, &it, buf, pktlen) == 1) {
unsigned fine = tm.fine;
if (fine < fine_min5)
fine = fine_min5;
else if (fine > fine_max5)
fine = fine_max5;
double fulltm = -coarse_tmlen5 * (tm.coarse + (0. + fine - fine_min5) / (0. + fine_max5 - fine_min5));
printf("%s ch:%02u falling:%1d coarse:%04u fine:%03u fulltm:%7.3f",
prefix, (unsigned) tm.channel, tm.is_falling,
(unsigned) tm.coarse, (unsigned) tm.fine, fulltm);
if (tm.is_falling && (last_rising_ch == tm.channel))
printf(" ToT:%5.3f", fulltm - last_rising_tm);

printf("\n");
if (!tm.is_falling) {
last_rising_tm = fulltm;
last_rising_ch = tm.channel;
} else {
last_rising_tm = 0;
last_rising_ch = -1;
}
}
}
Expand Down Expand Up @@ -266,6 +272,8 @@ int main(int argc, char* argv[])
if ((argc < 2) || !strcmp(argv[1], "-help") || !strcmp(argv[1], "?"))
return usage();

_init_ur_config();

long number = 10, skip = 0, nagain = 0;
double tmout = -1., maxage = -1.;
unsigned tdcmask = 0;
Expand Down
11 changes: 6 additions & 5 deletions plugins/dogma/dogma/UdpTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace dogma {

uint64_t fTotalRecvPacket{0};
uint64_t fTotalDiscardPacket{0};
uint64_t fTotalDiscardMagic{0};
uint64_t fTotalDiscardSPort{0};
uint64_t fTotalArtificialSkip{0};
uint64_t fTotalRecvBytes{0};
uint64_t fTotalDiscardBytes{0};
Expand All @@ -58,7 +58,7 @@ namespace dogma {
{
fTotalRecvPacket = 0;
fTotalDiscardPacket = 0;
fTotalDiscardMagic = 0;
fTotalDiscardSPort = 0;
fTotalArtificialSkip = 0;
fTotalRecvBytes = 0;
fTotalDiscardBytes = 0;
Expand All @@ -72,9 +72,9 @@ namespace dogma {
return dabc::number_to_str(fTotalDiscardPacket);
}

std::string GetDiscardMagicString()
std::string GetDiscardSPortString()
{
std::string res = dabc::number_to_str(fTotalDiscardMagic);
std::string res = dabc::number_to_str(fTotalDiscardSPort);

if (fTotalArtificialSkip > 0)
res += std::string("#") + dabc::number_to_str(fTotalArtificialSkip);
Expand All @@ -98,6 +98,7 @@ namespace dogma {
std::string fHostName; ///< host name used to create UDP socket
int fRecvBufLen{100000}; ///< recv buf len
std::string fMcastAddr; ///< mcast address
int fSourcePort{0}; ///< allowed source port
unsigned fMTU{0}; ///< maximal size of packet expected from DOG
void* fMtuBuffer{nullptr}; ///< buffer used to skip packets when no normal buffer is available
int fSkipCnt{0}; ///< counter used to control buffers skipping
Expand All @@ -122,7 +123,7 @@ namespace dogma {
bool CloseBuffer();

public:
UdpAddon(int fd, const std::string &host, int nport, int rcvbuflen, const std::string &mcast, int mtu, bool debug, bool print, int maxloop, double reduce);
UdpAddon(int fd, const std::string &host, int nport, int sport, int rcvbuflen, const std::string &mcast, int mtu, bool debug, bool print, int maxloop, double reduce);
~UdpAddon() override;

bool HasBuffer() const { return !fTgtPtr.null(); }
Expand Down
Loading