From 3b336e93b3b9de3d61635a8603196f14009dcfd9 Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 09:16:29 +0100
Subject: [PATCH 1/9] Updated tests to show new functionality of single readout
 per header

---
 src/modules/loki/test/DataParserTest.cpp   | 35 ++++++++--------
 src/modules/loki/test/DataParserTestData.h | 48 ++++++----------------
 2 files changed, 31 insertions(+), 52 deletions(-)

diff --git a/src/modules/loki/test/DataParserTest.cpp b/src/modules/loki/test/DataParserTest.cpp
index 2ede35afd..a5e45d858 100644
--- a/src/modules/loki/test/DataParserTest.cpp
+++ b/src/modules/loki/test/DataParserTest.cpp
@@ -15,6 +15,7 @@ protected:
   void TearDown() override {}
 };
 
+
 TEST_F(DataParserTest, Constructor) {
   ASSERT_EQ(Parser.Stats.Readouts, 0);
   ASSERT_EQ(Parser.Stats.DataHeaders, 0);
@@ -62,7 +63,7 @@ TEST_F(DataParserTest, GoodRingBadFEN) {
 }
 
 TEST_F(DataParserTest, DataSizeMismatch) {
-  auto Res = Parser.parse((char *)&OkThreeLokiReadouts[0], 10);
+  auto Res = Parser.parse((char *)&OkLokiReadout[0], 10);
   ASSERT_EQ(Res, 0);
   ASSERT_EQ(Parser.Stats.Readouts, 0);
   ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 1);
@@ -70,11 +71,11 @@ TEST_F(DataParserTest, DataSizeMismatch) {
   ASSERT_EQ(Parser.Result.size(), 0);
 }
 
-TEST_F(DataParserTest, ParseThree) {
+TEST_F(DataParserTest, ParseOne) {
   auto Res =
-      Parser.parse((char *)&OkThreeLokiReadouts[0], OkThreeLokiReadouts.size());
-  ASSERT_EQ(Res, 3);
-  ASSERT_EQ(Parser.Stats.Readouts, 3);
+      Parser.parse((char *)&OkLokiReadout[0], OkLokiReadout.size());
+  ASSERT_EQ(Res, 1);
+  ASSERT_EQ(Parser.Stats.Readouts, 1);
   ASSERT_EQ(Parser.Stats.DataHeaders, 1);
   ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 0);
   ASSERT_EQ(Parser.Stats.ErrorBytes, 0);
@@ -82,10 +83,10 @@ TEST_F(DataParserTest, ParseThree) {
 }
 
 TEST_F(DataParserTest, MultipleDataSection) {
-  auto Res = Parser.parse((char *)&Ok2xThreeLokiReadouts[0],
-                          Ok2xThreeLokiReadouts.size());
-  ASSERT_EQ(Res, 6);
-  ASSERT_EQ(Parser.Stats.Readouts, 6);
+  auto Res = Parser.parse((char *)&Ok2xLokiReadout[0],
+                          Ok2xLokiReadout.size());
+  ASSERT_EQ(Res, 2);
+  ASSERT_EQ(Parser.Stats.Readouts, 2);
   ASSERT_EQ(Parser.Stats.DataHeaders, 2);
   ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 0);
   ASSERT_EQ(Parser.Stats.ErrorBytes, 0);
@@ -93,19 +94,19 @@ TEST_F(DataParserTest, MultipleDataSection) {
 }
 
 TEST_F(DataParserTest, MultipleDataPackets) {
-  auto Res = Parser.parse((char *)&Ok2xThreeLokiReadouts[0],
-                          Ok2xThreeLokiReadouts.size());
-  ASSERT_EQ(Res, 6);
-  ASSERT_EQ(Parser.Stats.Readouts, 6);
+  auto Res = Parser.parse((char *)&Ok2xLokiReadout[0],
+                          Ok2xLokiReadout.size());
+  ASSERT_EQ(Res, 2);
+  ASSERT_EQ(Parser.Stats.Readouts, 2);
   ASSERT_EQ(Parser.Stats.DataHeaders, 2);
   ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 0);
   ASSERT_EQ(Parser.Stats.ErrorBytes, 0);
   ASSERT_EQ(Parser.Result.size(), 2);
 
-  Res = Parser.parse((char *)&Ok2xThreeLokiReadouts[0],
-                     Ok2xThreeLokiReadouts.size());
-  ASSERT_EQ(Res, 6);
-  ASSERT_EQ(Parser.Stats.Readouts, 12);
+  Res = Parser.parse((char *)&Ok2xLokiReadout[0],
+                     Ok2xLokiReadout.size());
+  ASSERT_EQ(Res, 2);
+  ASSERT_EQ(Parser.Stats.Readouts, 4);
   ASSERT_EQ(Parser.Stats.DataHeaders, 4);
   ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 0);
   ASSERT_EQ(Parser.Stats.ErrorBytes, 0);
diff --git a/src/modules/loki/test/DataParserTestData.h b/src/modules/loki/test/DataParserTestData.h
index 319246644..3c5b4946e 100644
--- a/src/modules/loki/test/DataParserTestData.h
+++ b/src/modules/loki/test/DataParserTestData.h
@@ -32,20 +32,21 @@ std::vector<uint8_t> ErrSizeMismatch
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    0x00, 0x00, 0x00, 0x00, // Readout 2
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
+};
 
-    0x00, 0x00, 0x00, 0x00, // Readout 3
+std::vector<uint8_t> OkLokiReadout
+{
+    0x00, 0x00, 0x18, 0x00, // Data Header, ring 0, fen 0
+
+    0x00, 0x00, 0x00, 0x00, // Readout 1
     0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00,
-    0x01, 0x03, 0x02, 0x03,
-    0x03, 0x03, 0x04, 0x03,
+    0x01, 0x01, 0x02, 0x01,
+    0x03, 0x01, 0x04, 0x01,
+
 };
 
-std::vector<uint8_t> OkThreeLokiReadouts
+std::vector<uint8_t> ErrThreeLokiReadouts
 {
     0x00, 0x00, 0x40, 0x00, // Data Header, ring 0, fen 0
 
@@ -68,9 +69,9 @@ std::vector<uint8_t> OkThreeLokiReadouts
     0x03, 0x03, 0x04, 0x03,
 };
 
-std::vector<uint8_t> Ok2xThreeLokiReadouts
+std::vector<uint8_t> Ok2xLokiReadout
 {
-    0x00, 0x00, 0x40, 0x00, // Data Header, ring 0, fen 0
+    0x00, 0x00, 0x18, 0x00, // Data Header, ring 0, fen 0
 
     0x00, 0x00, 0x00, 0x00, // Readout 1, time 0
     0x00, 0x00, 0x00, 0x00,
@@ -78,19 +79,7 @@ std::vector<uint8_t> Ok2xThreeLokiReadouts
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    0x00, 0x00, 0x00, 0x00, // Readout 2
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    0x00, 0x00, 0x00, 0x00, // Readout 3
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x03, 0x02, 0x03,
-    0x03, 0x03, 0x04, 0x03,
-
-    0x01, 0x01, 0x40, 0x00, // Data Header 2, ring 1, fen 1
+    0x01, 0x01, 0x18, 0x00, // Data Header 2, ring 1, fen 1
 
     0x01, 0x00, 0x00, 0x00, // Readout 1, time 1
     0x00, 0x00, 0x00, 0x00,
@@ -98,17 +87,6 @@ std::vector<uint8_t> Ok2xThreeLokiReadouts
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    0x01, 0x00, 0x00, 0x00, // Readout 2
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    0x01, 0x00, 0x00, 0x00, // Readout 3
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x03, 0x02, 0x03,
-    0x03, 0x03, 0x04, 0x03,
 };
 
 // clang-format on
-- 
GitLab


From 0dfc2639f3836357933515af57ec6feff9f7dd37 Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 09:41:57 +0100
Subject: [PATCH 2/9] Updated DataParser.cpp to check the length of the data
 against the header length + readout length, to only accept one readout per
 header, and to not loop over the readouts. Updated DataParserTest.cpp to
 check that the invalid length of data is caught when attempting to pass more
 than one readout at a time.

---
 src/modules/loki/readout/DataParser.cpp  | 36 +++++++++++-------------
 src/modules/loki/test/DataParserTest.cpp | 13 +++++++++
 2 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/src/modules/loki/readout/DataParser.cpp b/src/modules/loki/readout/DataParser.cpp
index d35ac4f33..54e4ce96b 100644
--- a/src/modules/loki/readout/DataParser.cpp
+++ b/src/modules/loki/readout/DataParser.cpp
@@ -64,8 +64,9 @@ int DataParser::parse(const char *Buffer, unsigned int Size) {
            DataHdrPtr->FENId, DataHdrPtr->DataLength);
     Stats.DataHeaders++;
 
-    if (DataHdrPtr->DataLength < sizeof(DataParser::LokiReadout)) {
-      XTRACE(DATA, WAR, "Invalid data length %u", DataHdrPtr->DataLength);
+    if (DataHdrPtr->DataLength != DataHeaderSize + LokiReadoutSize) {
+      XTRACE(DATA, WAR, "Invalid data length %u, expected %u", 
+            DataHdrPtr->DataLength, DataHeaderSize + LokiReadoutSize);
       Stats.ErrorDataHeaders++;
       Stats.ErrorBytes += BytesLeft;
       return ParsedReadouts;
@@ -75,24 +76,19 @@ int DataParser::parse(const char *Buffer, unsigned int Size) {
     CurrentDataSection.RingId = DataHdrPtr->RingId;
     CurrentDataSection.FENId = DataHdrPtr->FENId;
 
-    // Loop through data here
-    auto ReadoutsInDataSection =
-        (DataHdrPtr->DataLength - DataHeaderSize) / LokiReadoutSize;
-    for (unsigned int i = 0; i < ReadoutsInDataSection; i++) {
-      auto Data = (LokiReadout *)((char *)DataHdrPtr + DataHeaderSize +
-                                  i * LokiReadoutSize);
-      XTRACE(DATA, DEB,
-             "%3u: ring %u, fen %u, t(%11u,%11u) SeqNo %6u TubeId %3u , A "
-             "0x%04x B "
-             "0x%04x C 0x%04x D 0x%04x",
-             i, DataHdrPtr->RingId, DataHdrPtr->FENId, Data->TimeHigh,
-             Data->TimeLow, Data->DataSeqNum, Data->TubeId, Data->AmpA,
-             Data->AmpB, Data->AmpC, Data->AmpD);
-
-      CurrentDataSection.Data.push_back(*Data);
-      ParsedReadouts++;
-      Stats.Readouts++;
-    }
+    auto Data = (LokiReadout *)((char *)DataHdrPtr + DataHeaderSize);
+    XTRACE(DATA, DEB,
+           "ring %u, fen %u, t(%11u,%11u) SeqNo %6u TubeId %3u , A "
+           "0x%04x B "
+           "0x%04x C 0x%04x D 0x%04x",
+           DataHdrPtr->RingId, DataHdrPtr->FENId, Data->TimeHigh,
+           Data->TimeLow, Data->DataSeqNum, Data->TubeId, Data->AmpA,
+           Data->AmpB, Data->AmpC, Data->AmpD);
+
+    CurrentDataSection.Data.push_back(*Data);
+    ParsedReadouts++;
+    Stats.Readouts++;
+  
     Result.push_back(CurrentDataSection);
     BytesLeft -= DataHdrPtr->DataLength;
     DataPtr += DataHdrPtr->DataLength;
diff --git a/src/modules/loki/test/DataParserTest.cpp b/src/modules/loki/test/DataParserTest.cpp
index a5e45d858..3a06cde48 100644
--- a/src/modules/loki/test/DataParserTest.cpp
+++ b/src/modules/loki/test/DataParserTest.cpp
@@ -113,6 +113,19 @@ TEST_F(DataParserTest, MultipleDataPackets) {
   ASSERT_EQ(Parser.Result.size(), 2);
 }
 
+
+// TODO, confirm this response when passed too many readouts
+TEST_F(DataParserTest, BadThreeReadouts) {
+  auto Res = Parser.parse((char *)&ErrThreeLokiReadouts[0],
+                          ErrThreeLokiReadouts.size());
+  ASSERT_EQ(Res, 0);
+  ASSERT_EQ(Parser.Stats.Readouts, 0);
+  ASSERT_EQ(Parser.Stats.DataHeaders, 1);
+  ASSERT_EQ(Parser.Stats.ErrorDataHeaders, 1);
+  ASSERT_EQ(Parser.Stats.ErrorBytes, 64);
+  ASSERT_EQ(Parser.Result.size(), 0);
+} 
+
 int main(int argc, char **argv) {
   testing::InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();
-- 
GitLab


From ae90fbcacfb8a661cedfdb2b6c82d6dac86f818e Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 09:53:27 +0100
Subject: [PATCH 3/9] Updated ParsedData struct to include single LokiReadout
 instead of vector, and updated LokiInstrument to handle the single readout
 instead of looping over a vector of readouts

---
 src/modules/loki/LokiInstrument.cpp     | 86 ++++++++++++-------------
 src/modules/loki/readout/DataParser.cpp |  2 +-
 src/modules/loki/readout/DataParser.h   |  2 +-
 3 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/src/modules/loki/LokiInstrument.cpp b/src/modules/loki/LokiInstrument.cpp
index c848b6474..67ca0d3cb 100644
--- a/src/modules/loki/LokiInstrument.cpp
+++ b/src/modules/loki/LokiInstrument.cpp
@@ -157,51 +157,51 @@ void LokiInstrument::processReadouts() {
       continue;
     }
 
-    for (auto &Data : Section.Data) {
-
-      if (DumpFile) {
-        dumpReadoutToFile(Section, Data);
-      }
-
-      // Calculate TOF in ns
-      auto TimeOfFlight = ESSReadoutParser.Packet.Time.getTOF(Data.TimeHigh, Data.TimeLow,
-                                      LokiConfiguration.ReadoutConstDelayNS);
-
-      if (TimeOfFlight == ESSReadoutParser.Packet.Time.InvalidTOF) {
-        TimeOfFlight = ESSReadoutParser.Packet.Time.getPrevTOF(Data.TimeHigh, Data.TimeLow,
-                                       LokiConfiguration.ReadoutConstDelayNS);
-      }
-
-      XTRACE(DATA, DEB, "PulseTime     %" PRIu64 ", TimeStamp %" PRIu64 " ",
-             ESSReadoutParser.Packet.Time.TimeInNS,
-             ESSReadoutParser.Packet.Time.toNS(Data.TimeHigh, Data.TimeLow));
-      XTRACE(DATA, DEB, "PrevPulseTime %" PRIu64 ", TimeStamp %" PRIu64 " ",
-             ESSReadoutParser.Packet.Time.PrevTimeInNS,
-             ESSReadoutParser.Packet.Time.toNS(Data.TimeHigh, Data.TimeLow));
-
-      if (TimeOfFlight == ESSReadoutParser.Packet.Time.InvalidTOF) {
-        XTRACE(DATA, WAR, "No valid TOF from PulseTime or PrevPulseTime");
-        continue;
-      }
-
-      XTRACE(DATA, DEB,
-             "  Data: time (%10u, %10u) tof %llu, SeqNo %u, Tube %u, A %d, B "
-             "%d, C %d, D %d",
-             Data.TimeHigh, Data.TimeLow, TimeOfFlight, Data.DataSeqNum,
-             Data.TubeId, Data.AmpA, Data.AmpB, Data.AmpC, Data.AmpD);
-
-      // Calculate pixelid and apply calibration
-      uint32_t PixelId = calcPixel(Panel, Section.FENId, Data);
-
-      if (PixelId == 0) {
-        counters.PixelErrors++;
-      } else {
-        counters.TxBytes += Serializer->addEvent(TimeOfFlight, PixelId);
-        counters.Events++;
-        SerializerII->addEvent(Data.AmpA + Data.AmpB + Data.AmpC + Data.AmpD, 0);
-      }
+    auto Data = Section.Data;
 
+    if (DumpFile) {
+      dumpReadoutToFile(Section, Data);
     }
+
+    // Calculate TOF in ns
+    auto TimeOfFlight = ESSReadoutParser.Packet.Time.getTOF(Data.TimeHigh, Data.TimeLow,
+                                    LokiConfiguration.ReadoutConstDelayNS);
+
+    if (TimeOfFlight == ESSReadoutParser.Packet.Time.InvalidTOF) {
+      TimeOfFlight = ESSReadoutParser.Packet.Time.getPrevTOF(Data.TimeHigh, Data.TimeLow,
+                                     LokiConfiguration.ReadoutConstDelayNS);
+    }
+
+    XTRACE(DATA, DEB, "PulseTime     %" PRIu64 ", TimeStamp %" PRIu64 " ",
+           ESSReadoutParser.Packet.Time.TimeInNS,
+           ESSReadoutParser.Packet.Time.toNS(Data.TimeHigh, Data.TimeLow));
+    XTRACE(DATA, DEB, "PrevPulseTime %" PRIu64 ", TimeStamp %" PRIu64 " ",
+           ESSReadoutParser.Packet.Time.PrevTimeInNS,
+           ESSReadoutParser.Packet.Time.toNS(Data.TimeHigh, Data.TimeLow));
+
+    if (TimeOfFlight == ESSReadoutParser.Packet.Time.InvalidTOF) {
+      XTRACE(DATA, WAR, "No valid TOF from PulseTime or PrevPulseTime");
+      continue;
+    }
+
+    XTRACE(DATA, DEB,
+           "  Data: time (%10u, %10u) tof %llu, SeqNo %u, Tube %u, A %d, B "
+           "%d, C %d, D %d",
+           Data.TimeHigh, Data.TimeLow, TimeOfFlight, Data.DataSeqNum,
+           Data.TubeId, Data.AmpA, Data.AmpB, Data.AmpC, Data.AmpD);
+
+    // Calculate pixelid and apply calibration
+    uint32_t PixelId = calcPixel(Panel, Section.FENId, Data);
+
+    if (PixelId == 0) {
+      counters.PixelErrors++;
+    } else {
+      counters.TxBytes += Serializer->addEvent(TimeOfFlight, PixelId);
+      counters.Events++;
+      SerializerII->addEvent(Data.AmpA + Data.AmpB + Data.AmpC + Data.AmpD, 0);
+    }
+
+    
   } // for()
   counters.ReadoutsClampLow = LokiCalibration.Stats.ClampLow;
   counters.ReadoutsClampHigh = LokiCalibration.Stats.ClampHigh;
diff --git a/src/modules/loki/readout/DataParser.cpp b/src/modules/loki/readout/DataParser.cpp
index 54e4ce96b..6f06d2092 100644
--- a/src/modules/loki/readout/DataParser.cpp
+++ b/src/modules/loki/readout/DataParser.cpp
@@ -85,7 +85,7 @@ int DataParser::parse(const char *Buffer, unsigned int Size) {
            Data->TimeLow, Data->DataSeqNum, Data->TubeId, Data->AmpA,
            Data->AmpB, Data->AmpC, Data->AmpD);
 
-    CurrentDataSection.Data.push_back(*Data);
+    CurrentDataSection.Data = *Data;
     ParsedReadouts++;
     Stats.Readouts++;
   
diff --git a/src/modules/loki/readout/DataParser.h b/src/modules/loki/readout/DataParser.h
index 8de844e2a..43d10ad94 100644
--- a/src/modules/loki/readout/DataParser.h
+++ b/src/modules/loki/readout/DataParser.h
@@ -47,7 +47,7 @@ public:
   struct ParsedData {
     uint8_t RingId;
     uint8_t FENId;
-    std::vector<LokiReadout> Data;
+    LokiReadout Data;
   };
 
   // To be iterated over in processing thread
-- 
GitLab


From 87557e97e5ba9d6183be00278ccbcff8872fdc1b Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 10:57:17 +0100
Subject: [PATCH 4/9] Updated CompinedParserTest and ReadoutGenerator to remove
 multiple readouts per header, removing the "Elements" variable

---
 src/modules/loki/test/CombinedParserTest.cpp | 48 +++++---------------
 src/modules/loki/test/ReadoutGenerator.cpp   | 26 +++++------
 src/modules/loki/test/ReadoutGenerator.h     |  2 +-
 3 files changed, 25 insertions(+), 51 deletions(-)

diff --git a/src/modules/loki/test/CombinedParserTest.cpp b/src/modules/loki/test/CombinedParserTest.cpp
index 8fe903f44..e738159d3 100644
--- a/src/modules/loki/test/CombinedParserTest.cpp
+++ b/src/modules/loki/test/CombinedParserTest.cpp
@@ -16,14 +16,14 @@ std::vector<uint8_t> UdpPayload
 {
                 0x00, 0x00, // pad, v0
     0x45, 0x53, 0x53, 0x30, // 'E', 'S', 'S', type 0x30
-    0x9e, 0x00, 0x01, 0x00, // len 158/0x9e OQ 1
+    0x4e, 0x00, 0x01, 0x00, // len 78/0x4e OQ 1
     0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00,
     0x01, 0x00, 0x00, 0x00, // Seq number 1
 
-    0x00, 0x00, 0x40, 0x00, // Data Header, ring 0, fen 0
+    0x00, 0x00, 0x18, 0x00, // Data Header, ring 0, fen 0
 
     0x00, 0x00, 0x00, 0x00, // Readout 1, time 0
     0x00, 0x00, 0x00, 0x00,
@@ -31,19 +31,7 @@ std::vector<uint8_t> UdpPayload
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    0x00, 0x00, 0x00, 0x00, // Readout 2
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    0x00, 0x00, 0x00, 0x00, // Readout 3
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x03, 0x02, 0x03,
-    0x03, 0x03, 0x04, 0x03,
-
-    0x01, 0x01, 0x40, 0x00, // Data Header 2, ring 1, fen 1
+    0x01, 0x01, 0x18, 0x00, // Data Header 2, ring 1, fen 1
 
     0x01, 0x00, 0x00, 0x00, // Readout 1, time 1
     0x00, 0x00, 0x00, 0x00,
@@ -51,17 +39,6 @@ std::vector<uint8_t> UdpPayload
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    0x01, 0x00, 0x00, 0x00, // Readout 2
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    0x01, 0x00, 0x00, 0x00, // Readout 3
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x03, 0x02, 0x03,
-    0x03, 0x03, 0x04, 0x03,
 };
 // clang-format on
 
@@ -88,10 +65,9 @@ TEST_F(CombinedParserTest, DataGenSizeTooBig) {
   uint8_t Buffer[BufferSize];
 
   uint16_t Sections{1000};
-  uint16_t Elements{1000};
 
   ReadoutGenerator gen;
-  auto Length = gen.lokiReadoutDataGen(false, Sections, Elements, 1, Buffer, BufferSize, FirstSeqNum);
+  auto Length = gen.lokiReadoutDataGen(false, Sections, 1, Buffer, BufferSize, FirstSeqNum);
   ASSERT_EQ(Length, 0);
 }
 
@@ -102,15 +78,14 @@ TEST_F(CombinedParserTest, DataGen) {
   uint8_t Buffer[BufferSize];
 
   for (unsigned int Sections = 1; Sections < 372; Sections++) {
-    uint16_t Elements = ((BufferSize - sizeof(ESSReadout::Parser::PacketHeaderV0) - Sections*4)/20/Sections);
     ReadoutGenerator gen;
-    auto Length = gen.lokiReadoutDataGen(false, Sections, Elements, 1, Buffer, BufferSize, FirstSeqNum);
-    ASSERT_EQ(Length, sizeof(ESSReadout::Parser::PacketHeaderV0) + Sections *(4 + Elements * 20));
+    auto Length = gen.lokiReadoutDataGen(false, Sections, 1, Buffer, BufferSize, FirstSeqNum);
+    ASSERT_EQ(Length, sizeof(ESSReadout::Parser::PacketHeaderV0) + Sections *(4 + 20));
 
     auto Res = CommonReadout.validate((char *)&Buffer[0], Length, DataType);
     ASSERT_EQ(Res, ESSReadout::Parser::OK);
     Res = LokiParser.parse(CommonReadout.Packet.DataPtr, CommonReadout.Packet.DataLength);
-    ASSERT_EQ(Res, Sections*Elements);
+    ASSERT_EQ(Res, Sections);
   }
 }
 
@@ -119,15 +94,14 @@ TEST_F(CombinedParserTest, ParseUDPPacket) {
   auto Res = CommonReadout.validate((char *)&UdpPayload[0], UdpPayload.size(), DataType);
   ASSERT_EQ(Res, ESSReadout::Parser::OK);
   Res = LokiParser.parse(CommonReadout.Packet.DataPtr, CommonReadout.Packet.DataLength);
-  ASSERT_EQ(Res, 6);
+  ASSERT_EQ(Res, 2);
 
   // Just for visual inspection for now
   for (auto & Section : LokiParser.Result) {
     printf("Ring %u, FEN %u\n", Section.RingId, Section.FENId);
-    for (auto & Data : Section.Data) {
-      printf("time (%u, %u), SeqNum %u, Tube %u, A %u, B %u, C %u, D %u\n",
-        Data.TimeHigh, Data.TimeLow, Data.DataSeqNum, Data.TubeId, Data.AmpA, Data.AmpB, Data.AmpC, Data.AmpD);
-    }
+    printf("time (%u, %u), SeqNum %u, Tube %u, A %u, B %u, C %u, D %u\n",
+            Section.Data.TimeHigh, Section.Data.TimeLow, Section.Data.DataSeqNum, Section.Data.TubeId,
+            Section.Data.AmpA, Section.Data.AmpB, Section.Data.AmpC, Section.Data.AmpD);
   }
 }
 
diff --git a/src/modules/loki/test/ReadoutGenerator.cpp b/src/modules/loki/test/ReadoutGenerator.cpp
index eac8765d6..117f78bb9 100644
--- a/src/modules/loki/test/ReadoutGenerator.cpp
+++ b/src/modules/loki/test/ReadoutGenerator.cpp
@@ -17,10 +17,11 @@
 using namespace Loki;
 
 uint16_t ReadoutGenerator::lokiReadoutDataGen(
-    bool Randomise, uint16_t DataSections, uint16_t DataElements, uint8_t Rings,
+    bool Randomise, uint16_t DataSections, uint8_t Rings,
     uint8_t *Buffer, uint16_t MaxSize, uint32_t SeqNum) {
 
-  uint16_t DataSize = HeaderSize + DataSections * (4 + DataElements * 20);
+  // readout header size = 4, data size = 20
+  uint16_t DataSize = HeaderSize + DataSections * (4 + 20);
   if (DataSize > MaxSize) {
     printf("Too much data for buffer. DataSize: %u, MaxSize: %u\n", DataSize,
            MaxSize);
@@ -46,21 +47,20 @@ uint16_t ReadoutGenerator::lokiReadoutDataGen(
     auto DataHeader = (ESSReadout::Parser::DataHeader *)DP;
     DataHeader->RingId = RingCount % Rings;
     DataHeader->FENId = 0x00;
-    DataHeader->DataLength = DataHeaderSize + DataElements * LokiDataSize;
+    DataHeader->DataLength = DataHeaderSize + LokiDataSize;
     assert(DataHeader->DataLength == 4 + 20 * DataElements);
     RingCount++;
     DP += DataHeaderSize;
 
-    for (auto Element = 0; Element < DataElements; Element++) {
-      auto DataBlock = (DataParser::LokiReadout *)DP;
-      DataBlock->TimeLow = 100;
-      DataBlock->TubeId = Element % 8;
-      DataBlock->AmpA = DataElements - Element;
-      DataBlock->AmpB = Element + 1;
-      DataBlock->AmpC = Element + 1;
-      DataBlock->AmpD = Element + 1;
-      DP += LokiDataSize;
-    }
+    
+    auto DataBlock = (DataParser::LokiReadout *)DP;
+    DataBlock->TimeLow = 100;
+    DataBlock->TubeId = 1;
+    DataBlock->AmpA = 1;
+    DataBlock->AmpB = 1;
+    DataBlock->AmpC = 1;
+    DataBlock->AmpD = 1;
+    DP += LokiDataSize;
   }
 
   // if doing fuzzing, fuzz up to one field in header & up to 20 fields in data
diff --git a/src/modules/loki/test/ReadoutGenerator.h b/src/modules/loki/test/ReadoutGenerator.h
index 4b5413190..617530fa1 100644
--- a/src/modules/loki/test/ReadoutGenerator.h
+++ b/src/modules/loki/test/ReadoutGenerator.h
@@ -18,7 +18,7 @@ public:
 
   /// \brief Fill out specified buffer with LoKI readouts
   uint16_t lokiReadoutDataGen(bool Randomise, uint16_t DataSections,
-                              uint16_t DataElements, uint8_t Rings,
+                               uint8_t Rings,
                               uint8_t *Buffer, uint16_t MaxSize,
                               uint32_t SeqNum);
 
-- 
GitLab


From 7a844ace0bd0b9a347295f24ff554e022aa3fcb2 Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 14:04:51 +0100
Subject: [PATCH 5/9] Updated LokiBaseTest to reflect single readout per header
 functionality

---
 src/modules/loki/generators/udpgen_loki.cpp |  3 +-
 src/modules/loki/test/LokiBaseTest.cpp      | 57 +++++++--------------
 2 files changed, 20 insertions(+), 40 deletions(-)

diff --git a/src/modules/loki/generators/udpgen_loki.cpp b/src/modules/loki/generators/udpgen_loki.cpp
index 08b67c852..85cd668c6 100644
--- a/src/modules/loki/generators/udpgen_loki.cpp
+++ b/src/modules/loki/generators/udpgen_loki.cpp
@@ -48,7 +48,6 @@ int main(int argc, char *argv[]) {
   const int BufferSize{8972};
   uint8_t Buffer[BufferSize];
   const uint16_t DataSections{100};
-  const uint16_t DataElements{4};
 
   Socket::Endpoint local("0.0.0.0", 0);
   Socket::Endpoint remote(Settings.IpAddress.c_str(), Settings.UDPPort);
@@ -64,7 +63,7 @@ int main(int argc, char *argv[]) {
   do {
     uint32_t SeqNum = TotalPackets;
     uint16_t DataSize =
-        gen.lokiReadoutDataGen(Settings.Randomise, DataSections, DataElements,
+        gen.lokiReadoutDataGen(Settings.Randomise, DataSections,
                                Settings.NRings, Buffer, BufferSize, SeqNum);
 
     DataSource.send(Buffer, DataSize);
diff --git a/src/modules/loki/test/LokiBaseTest.cpp b/src/modules/loki/test/LokiBaseTest.cpp
index 6d915b76d..ef1e2cb29 100644
--- a/src/modules/loki/test/LokiBaseTest.cpp
+++ b/src/modules/loki/test/LokiBaseTest.cpp
@@ -73,7 +73,7 @@ std::vector<uint8_t> TestPacket2{
     // ESS header
                 0x00, 0x00, // pad, v0
     0x45, 0x53, 0x53, 0x30, //  'E' 'S' 'S' 0x00
-    0xb6, 0x00, 0x00, 0x00, // 0x00b6 = 182 bytes
+    0x7e, 0x00, 0x00, 0x00, // 0x00b6 = 126 bytes
     0x11, 0x00, 0x00, 0x00, // Pulse time High (17s)
     0x00, 0x01, 0x00, 0x00, // Pulse time Low (256 clocks)
     0x11, 0x00, 0x00, 0x00, // Prev PT
@@ -82,65 +82,46 @@ std::vector<uint8_t> TestPacket2{
 
 
     // Data Header 1
-    0x00, 0x01, 0x40, 0x00, // ring 0, fen 1, data size 64 bytes
-
-    // Readout 1
+    0x00, 0x01, 0x18, 0x00, // ring 0, fen 1, data size 64 bytes
+    // Readout
     0x11, 0x00, 0x00, 0x00, // time high (17s)
     0x01, 0x01, 0x00, 0x00, // time low (257 clocks)
     0x00, 0x00, 0x00, 0x00, // fpga 0, tube 0
     0x01, 0x01, 0x02, 0x01, // amp a, amp b
     0x03, 0x01, 0x04, 0x01, // amp c, amp d
 
-    // Readout 2
-    0x11, 0x00, 0x00, 0x00, // time high (17s)
-    0x02, 0x01, 0x00, 0x00, // time low (258 clocks)
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    // Readout 3
-    0x11, 0x00, 0x00, 0x00, // time high (17s)
-    0x03, 0x01, 0x00, 0x00, // time low (259 clocks)
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-
 
     // Data Header 2
     // Ring 5 is invalid -> RingErrors++
-    0x05, 0x01, 0x40, 0x00, // ring 5, fen 1, data size 64 bytes
-
-    // Readout 1
+    0x05, 0x01, 0x18, 0x00, // ring 5, fen 1, data size 64 bytes
+    // Readout
     0x11, 0x00, 0x00, 0x00, //time high 17s
     0x01, 0x02, 0x00, 0x00, // time low (257 clocks)
     0x00, 0x00, 0x00, 0x00,
     0x01, 0x01, 0x02, 0x01,
     0x03, 0x01, 0x04, 0x01,
 
-    // Readout 2
-    0x11, 0x00, 0x00, 0x00,
-    0x02, 0x02, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x02, 0x02, 0x02,
-    0x03, 0x02, 0x04, 0x02,
-
-    // Readout 3
-    0x11, 0x00, 0x00, 0x00,
-    0x03, 0x02, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00,
-    0x01, 0x00, 0x10, 0x00,
-    0x00, 0x01, 0x01, 0x00,
 
     // Data Header 3
     // FEN 5 is invalid -> FENErrors++
     0x01, 0x05, 0x18, 0x00, // ring 1, fen 5, size 24 bytes
-
-    // Readout 1
+    // Readout
     0x11, 0x00, 0x00, 0x00,
     0x02, 0x02, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00,
     0x01, 0x02, 0x02, 0x02,
     0x03, 0x02, 0x04, 0x02,
+
+
+    // Data Header 4 
+    0x00, 0x01, 0x18, 0x00, // ring 0, fen 1, data size 64 bytes
+    // Readout
+    0x11, 0x00, 0x00, 0x00, // time high (17s)
+    0x03, 0x01, 0x00, 0x00, // time low (259 clocks)
+    0x00, 0x00, 0x00, 0x00, // amplitudes are all 0, PixelErrors ++
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+
 };
 // clang-format on
 
@@ -173,8 +154,8 @@ TEST_F(LokiBaseTest, DataReceiveGood) {
   Readout.stopThreads();
   EXPECT_EQ(Readout.Counters.RxPackets, 1);
   EXPECT_EQ(Readout.Counters.RxBytes, TestPacket2.size());
-  EXPECT_EQ(Readout.Counters.Readouts, 7);
-  EXPECT_EQ(Readout.Counters.DataHeaders, 3);
+  EXPECT_EQ(Readout.Counters.Readouts, 4);
+  EXPECT_EQ(Readout.Counters.DataHeaders, 4);
   EXPECT_EQ(Readout.Counters.PixelErrors, 1);
   EXPECT_EQ(Readout.Counters.RingErrors, 1);
   EXPECT_EQ(Readout.Counters.FENErrors, 1);
-- 
GitLab


From c93c9beedbd2ece91a4234cb07f954b0439b32d7 Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Tue, 14 Dec 2021 14:10:48 +0100
Subject: [PATCH 6/9] Removed DataElements variable that is no longer used, is
 always 1

---
 src/modules/loki/test/ReadoutGenerator.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/modules/loki/test/ReadoutGenerator.cpp b/src/modules/loki/test/ReadoutGenerator.cpp
index 117f78bb9..71e27737c 100644
--- a/src/modules/loki/test/ReadoutGenerator.cpp
+++ b/src/modules/loki/test/ReadoutGenerator.cpp
@@ -48,7 +48,7 @@ uint16_t ReadoutGenerator::lokiReadoutDataGen(
     DataHeader->RingId = RingCount % Rings;
     DataHeader->FENId = 0x00;
     DataHeader->DataLength = DataHeaderSize + LokiDataSize;
-    assert(DataHeader->DataLength == 4 + 20 * DataElements);
+    assert(DataHeader->DataLength == 4 + 20);
     RingCount++;
     DP += DataHeaderSize;
 
-- 
GitLab


From f8813b6393b53b56b655ee92f3db4ea5d93c2acf Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Thu, 16 Dec 2021 10:12:51 +0100
Subject: [PATCH 7/9] Avoid copying using a reference

---
 src/modules/loki/LokiInstrument.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/modules/loki/LokiInstrument.cpp b/src/modules/loki/LokiInstrument.cpp
index 67ca0d3cb..cb7bd0085 100644
--- a/src/modules/loki/LokiInstrument.cpp
+++ b/src/modules/loki/LokiInstrument.cpp
@@ -157,7 +157,7 @@ void LokiInstrument::processReadouts() {
       continue;
     }
 
-    auto Data = Section.Data;
+    auto &Data = Section.Data;
 
     if (DumpFile) {
       dumpReadoutToFile(Section, Data);
-- 
GitLab


From 4031016fe0a024c2092c5378bf4444e3d14963aa Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Thu, 16 Dec 2021 11:13:18 +0100
Subject: [PATCH 8/9] adjusted section numbers to make up for the lack of
 elements to make up udp packet size

---
 src/modules/loki/generators/udpgen_loki.cpp  | 2 +-
 src/modules/loki/test/CombinedParserTest.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/modules/loki/generators/udpgen_loki.cpp b/src/modules/loki/generators/udpgen_loki.cpp
index 85cd668c6..4c9be7616 100644
--- a/src/modules/loki/generators/udpgen_loki.cpp
+++ b/src/modules/loki/generators/udpgen_loki.cpp
@@ -47,7 +47,7 @@ int main(int argc, char *argv[]) {
 
   const int BufferSize{8972};
   uint8_t Buffer[BufferSize];
-  const uint16_t DataSections{100};
+  const uint16_t DataSections{400};
 
   Socket::Endpoint local("0.0.0.0", 0);
   Socket::Endpoint remote(Settings.IpAddress.c_str(), Settings.UDPPort);
diff --git a/src/modules/loki/test/CombinedParserTest.cpp b/src/modules/loki/test/CombinedParserTest.cpp
index e738159d3..961c6c959 100644
--- a/src/modules/loki/test/CombinedParserTest.cpp
+++ b/src/modules/loki/test/CombinedParserTest.cpp
@@ -64,7 +64,7 @@ TEST_F(CombinedParserTest, DataGenSizeTooBig) {
   const uint16_t BufferSize{8972};
   uint8_t Buffer[BufferSize];
 
-  uint16_t Sections{1000};
+  uint16_t Sections{100000};
 
   ReadoutGenerator gen;
   auto Length = gen.lokiReadoutDataGen(false, Sections, 1, Buffer, BufferSize, FirstSeqNum);
-- 
GitLab


From e279fd944d103abc14a0af0f390d4f05b0d3c1d3 Mon Sep 17 00:00:00 2001
From: JennyLouise <jenny.walker@live.co.uk>
Date: Thu, 16 Dec 2021 11:31:19 +0100
Subject: [PATCH 9/9] Reverting increasing the section number in test, was too
 large to store in uint_16

---
 src/modules/loki/test/CombinedParserTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/modules/loki/test/CombinedParserTest.cpp b/src/modules/loki/test/CombinedParserTest.cpp
index 961c6c959..e738159d3 100644
--- a/src/modules/loki/test/CombinedParserTest.cpp
+++ b/src/modules/loki/test/CombinedParserTest.cpp
@@ -64,7 +64,7 @@ TEST_F(CombinedParserTest, DataGenSizeTooBig) {
   const uint16_t BufferSize{8972};
   uint8_t Buffer[BufferSize];
 
-  uint16_t Sections{100000};
+  uint16_t Sections{1000};
 
   ReadoutGenerator gen;
   auto Length = gen.lokiReadoutDataGen(false, Sections, 1, Buffer, BufferSize, FirstSeqNum);
-- 
GitLab