From 32bb4c259b7e592c003d52b606242a3ed6efb57c 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     | 13 ++++++++++---
 src/processor.h      |  4 ++--
 src/scdaq.cc         |  6 ++++--
 6 files changed, 25 insertions(+), 7 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 57bf8c67..8311584d 100644
--- a/src/config.h
+++ b/src/config.h
@@ -99,6 +99,11 @@ public:
     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 49cdaf7f..8dbdae25 100644
--- a/src/controls.h
+++ b/src/controls.h
@@ -12,5 +12,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 c72340d5..3fa8079b 100644
--- a/src/processor.cc
+++ b/src/processor.cc
@@ -7,13 +7,14 @@
 #include <vector>
 #include <algorithm>
 
-StreamProcessor::StreamProcessor(size_t max_size_, bool doZS_, ProcessorType processorType_, uint32_t nOrbitsPerDMAPacket_, ctrl& control_) :
+StreamProcessor::StreamProcessor(size_t max_size_, bool doZS_, ProcessorType processorType_, 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);
@@ -43,8 +44,7 @@ bool StreamProcessor::CheckFrameMultBlock(size_t inputSize){
 	int bsize = sizeof(blockMuon);
 	if((inputSize-nOrbitsPerDMAPacket*constants::orbit_trailer_size - 32*nOrbitsPerDMAPacket -32)%bsize!=0){
 		LOG(WARNING)
-			<< "Frame size not a multiple of block size. Will be skipped. Frame size = "
-			<< inputSize << ", block size = " << bsize;
+			<< "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;
@@ -194,6 +194,13 @@ 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 bcb698e7..b1a52866 100644
--- a/src/processor.h
+++ b/src/processor.h
@@ -14,11 +14,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);
+  StreamProcessor(size_t max_size_, bool doZS_, ProcessorType processorType_, uint32_t nOrbitsPerDMAPacket_, uint32_t prescaleFactor, ctrl& control);
   void* operator()( void* item )/*override*/;
   ~StreamProcessor();
 
@@ -35,6 +34,7 @@ private:
   bool doZS;
   ProcessorType processorType;
   uint32_t nOrbitsPerDMAPacket;
+  uint32_t prescaleFactor;
   ctrl& control;
 };
 
diff --git a/src/scdaq.cc b/src/scdaq.cc
index 917609ab..c18719af 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -38,7 +38,8 @@ int run_pipeline( int nbThreads, ctrl& control, config& conf )
   config::InputType input = conf.getInput();
   size_t packetBufferSize = conf.getDmaPacketBufferSize();
   size_t nbPacketBuffers = conf.getNumberOfDmaPacketBuffers();
-  // Create empty input reader, will assign later when we know what is the data source
+  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;
 
   // Create the pipeline
@@ -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);
+  StreamProcessor stream_processor(packetBufferSize, conf.getDoZS(), conf.getProcessorType(), conf.getNOrbitsPerDMAPacket(), prescaleFactor, control);
   if ( conf.getEnableStreamProcessor() ) {
     pipeline.add_filter( stream_processor );
   }
@@ -140,6 +141,7 @@ if(argc < 2){
     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