From d5f0a1628794546b83e7938979195b35fdfbd26c Mon Sep 17 00:00:00 2001
From: Thomas Owen James <tjames@cmd-scouting-ctrlhub.dyndns.cern.ch>
Date: Wed, 3 Aug 2022 16:18:44 +0200
Subject: [PATCH] added configuration option that prescales the DMA packets

---
 etc/scdaq/scdaq.conf |  3 +++
 src/config.h         |  5 +++++
 src/controls.h       |  1 +
 src/processor.cc     | 20 ++++++++++++++++----
 src/processor.h      |  5 ++---
 src/scdaq.cc         |  4 +++-
 6 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/etc/scdaq/scdaq.conf b/etc/scdaq/scdaq.conf
index 687c9356..c194bebb 100644
--- a/etc/scdaq/scdaq.conf
+++ b/etc/scdaq/scdaq.conf
@@ -28,6 +28,9 @@ packets_per_report:200000
 # number of orbits per DMA packet, in decimal
 NOrbitsPerDMAPacket:20
 
+# number of orbits per DMA packet, in decimal
+prescale_factor:20
+
 ## Extra settings for "filedma" input
 
 #input_file:/dev/shm/testdata.bin
diff --git a/src/config.h b/src/config.h
index 05972a9c..150fb54d 100644
--- a/src/config.h
+++ b/src/config.h
@@ -86,6 +86,11 @@ class config {
     return boost::lexical_cast<uint32_t>(v.c_str());
   }
 
+  uint32_t getPrescaleFactor() const {
+    std::string v = vmap.at("prescale_factor");
+    return boost::lexical_cast<uint32_t>(v.c_str());
+  }
+
   uint32_t getNumThreads() const {
     std::string v = vmap.at("threads");
     return boost::lexical_cast<uint32_t>(v.c_str());
diff --git a/src/controls.h b/src/controls.h
index eb41c395..05d31884 100644
--- a/src/controls.h
+++ b/src/controls.h
@@ -13,5 +13,6 @@ struct ctrl {
   int packets_per_report;
   int n_orbits_per_dma_packet;
   std::atomic<uint32_t> orbit_trailer_error_count;
+  std::atomic<uint64_t> packet_count;
 };
 #endif
diff --git a/src/processor.cc b/src/processor.cc
index b085f3e4..2bf81ac5 100644
--- a/src/processor.cc
+++ b/src/processor.cc
@@ -10,13 +10,15 @@
 #include "slice.h"
 
 StreamProcessor::StreamProcessor(size_t max_size_, bool doZS_, ProcessorType processorType_,
-                                 uint32_t nOrbitsPerDMAPacket_, ctrl &control_)
+                                 uint32_t nOrbitsPerDMAPacket_, uint32_t prescaleFactor_,
+                                 ctrl &control_)
     : tbb::filter(parallel),
       max_size(max_size_),
       nbPackets(0),
       doZS(doZS_),
       processorType(processorType_),
       nOrbitsPerDMAPacket(nOrbitsPerDMAPacket_),
+      prescaleFactor(prescaleFactor_),
       control(control_) {
   LOG(TRACE) << "Created transform filter at " << static_cast<void *>(this);
   myfile.open("example.txt");
@@ -44,9 +46,10 @@ bool StreamProcessor::CheckFrameMultBlock(size_t inputSize) {
        32) %
           bsize !=
       0) {
-    LOG(WARNING) << "Frame size not a multiple of block size. Will be skipped. "
-                    "Frame size = "
-                 << inputSize << ", block size = " << bsize;
+    LOG(WARNING) << "Frame size not a multiple of block size after orbit headers "
+                    "(32B*nOrbitsPerPacket), orbit trailers (544B*nOrbitsPerPacket), "
+                    "and packet trailer (32B) have been subtracted. \n Frame size = "
+                 << inputSize << ", block size = " << bsize << ", packet will be skipped";
     return false;
   }
   return true;
@@ -232,6 +235,15 @@ uint32_t StreamProcessor::FillOrbitMuon(std::vector<unsigned int> &bx_vect, char
 
 void StreamProcessor::process(Slice &input, Slice &out) {
   nbPackets++;
+  control.packet_count++;
+
+  // Implement prescale - only for CALO
+  if (processorType == ProcessorType::CALO) {
+    if (control.packet_count % prescaleFactor != 0) {
+      return;
+    }
+  }
+
   char *rd_ptr = input.begin();
   char *wr_ptr = out.begin();
   uint32_t counts = 0;
diff --git a/src/processor.h b/src/processor.h
index 201d2e17..5120f5ba 100644
--- a/src/processor.h
+++ b/src/processor.h
@@ -15,12 +15,10 @@
 class Slice;
 
 class StreamProcessor : public tbb::filter {
-  static std::atomic<uint32_t> orbit_trailer_error_count;
-
  public:
   enum class ProcessorType { PASS_THROUGH, GMT, CALO };
   StreamProcessor(size_t max_size_, bool doZS_, ProcessorType processorType_,
-                  uint32_t nOrbitsPerDMAPacket_, ctrl &control);
+                  uint32_t nOrbitsPerDMAPacket_, uint32_t prescaleFactor, ctrl &control);
   void *operator()(void *item) /*override*/;
   ~StreamProcessor();
 
@@ -37,6 +35,7 @@ class StreamProcessor : public tbb::filter {
   bool doZS;
   ProcessorType processorType;
   uint32_t nOrbitsPerDMAPacket;
+  uint32_t prescaleFactor;
   ctrl &control;
 };
 
diff --git a/src/scdaq.cc b/src/scdaq.cc
index bd930174..7bbbe5d8 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -34,6 +34,7 @@ int run_pipeline(int nbThreads, ctrl &control, config &conf) {
   config::InputType input = conf.getInput();
   size_t packetBufferSize = conf.getDmaPacketBufferSize();
   size_t nbPacketBuffers = conf.getNumberOfDmaPacketBuffers();
+  uint32_t prescaleFactor = conf.getPrescaleFactor();
   // Create empty input reader, will assign later when we know what is the data
   // source
   std::shared_ptr<InputFilter> input_filter;
@@ -65,7 +66,7 @@ int run_pipeline(int nbThreads, ctrl &control, config &conf) {
   // Create reformatter and add it to the pipeline
   // TODO: Created here so we are not subject of scoping, fix later...
   StreamProcessor stream_processor(packetBufferSize, conf.getDoZS(), conf.getProcessorType(),
-                                   conf.getNOrbitsPerDMAPacket(), control);
+                                   conf.getNOrbitsPerDMAPacket(), prescaleFactor, control);
   if (conf.getEnableStreamProcessor()) {
     pipeline.add_filter(stream_processor);
   }
@@ -134,6 +135,7 @@ int main(int argc, char *argv[]) {
     control.running = false;
     control.run_number = 0;
     control.orbit_trailer_error_count = 0;
+    control.packet_count = 0;
     control.max_file_size = conf.getOutputMaxFileSize();  // in Bytes
     control.packets_per_report = conf.getPacketsPerReport();
     control.output_force_write = conf.getOutputForceWrite();
-- 
GitLab