From 9f17824cce56a5615194c78822aee645d59c0db3 Mon Sep 17 00:00:00 2001
From: Thomas Owen James <tom.james@cern.ch>
Date: Mon, 22 Aug 2022 15:28:09 +0200
Subject: [PATCH] fixed orbit numbers for bx's in gap and added verbosity
 option

---
 etc/scdaq/scdaq.conf        |  3 +++
 scripts/fileMover.py        | 15 ++++++++++++++-
 src/config.h                |  4 ++++
 src/controls.h              |  2 ++
 src/processor.cc            | 33 +++++++++++++++++++++++++++------
 src/scdaq.cc                |  3 +++
 test/config/scdaq-calo.conf |  3 +++
 test/config/scdaq-gmt.conf  |  3 +++
 8 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/etc/scdaq/scdaq.conf b/etc/scdaq/scdaq.conf
index 71647b67..e3b23a40 100644
--- a/etc/scdaq/scdaq.conf
+++ b/etc/scdaq/scdaq.conf
@@ -95,3 +95,6 @@ quality_cut:12
 
 # Pipeline settings
 threads:8
+
+#verbosity level, currently supports 0 and 1
+verbosity:0
diff --git a/scripts/fileMover.py b/scripts/fileMover.py
index 384f7c25..340c9ec6 100755
--- a/scripts/fileMover.py
+++ b/scripts/fileMover.py
@@ -16,7 +16,20 @@ if __name__ == "__main__":
             for f in onlyfiles:
                 full_filename = join('/fff/ramdisk/scdaq', f)
                 outfile = f+'.bz2'
-                dest_name = join('/fff/output/scdaq',outfile)
+
+                if f[7:10] == 'GMT':
+                    run_number = f[10:16]
+                elif f[7:11] == 'CALO':
+                    run_number = f[11:17]
+                else:
+                    run_number = '000000'
+
+                dest_path = join('/store/lustre/l1scout/scdaq/',run_number)
+
+                if not (os.path.exists(dest_path)):
+                    os.mkdir(dest_path)
+
+                dest_name = join(dest_path,outfile) 
                 command = "lbzip2 "+full_filename+" -c > "+dest_name
                 print "compressing "+full_filename
                 retval = os.system(command)
diff --git a/src/config.h b/src/config.h
index 150fb54d..b529892e 100644
--- a/src/config.h
+++ b/src/config.h
@@ -106,6 +106,10 @@ class config {
     return (true ? vmap.at("enable_elastic_processor") == "yes" : false);
   }
   bool getDoZS() const { return (true ? vmap.at("doZS") == "yes" : false); }
+  bool getVerbosity() const {
+    std::string v = vmap.at("verbosity");
+    return boost::lexical_cast<uint32_t>(v.c_str());
+  }
 
  private:
   std::map<std::string, std::string> vmap;
diff --git a/src/controls.h b/src/controls.h
index 05d31884..6581c625 100644
--- a/src/controls.h
+++ b/src/controls.h
@@ -9,10 +9,12 @@ struct ctrl {
   std::atomic<bool> running;
   /* Always write data to a file regardless of the run status */
   bool output_force_write;
+  bool verbosity;
   uint64_t max_file_size;
   int packets_per_report;
   int n_orbits_per_dma_packet;
   std::atomic<uint32_t> orbit_trailer_error_count;
   std::atomic<uint64_t> packet_count;
+  std::atomic<uint64_t> excessOrbitsPerPacketCount;
 };
 #endif
diff --git a/src/processor.cc b/src/processor.cc
index 21da7eb2..a408db3d 100644
--- a/src/processor.cc
+++ b/src/processor.cc
@@ -68,7 +68,10 @@ std::vector<unsigned int> StreamProcessor::CountBX(Slice &input, char *rd_ptr, b
       orbit_trailer *ot = (orbit_trailer *)(rd_ptr);
       for (unsigned int k = 0; k < (14 * 8); k++) {  // 14*8 = 14 frames, 8 links of orbit trailer
                                                      // containing BX hitmap
-        bit_check(&bx_vect, ot->bx_map[k], k * 32);
+        // bit_check(&bx_vect, ot->bx_map[k], (k*32 + 1));// +1 added to account
+        // for BX counting starting at 1, commented out until firmware fix for
+        // BX number being one more in the trailer than the CMS convention
+        bit_check(&bx_vect, ot->bx_map[k], (k * 32));
       }
       return bx_vect;
     }
@@ -107,12 +110,16 @@ uint32_t StreamProcessor::FillOrbitCalo(std::vector<unsigned int> &bx_vect, char
       break;
     }  // orbit trailer has been reached, end of orbit data
     uint32_t bx = uint32_t{bx_vect[relbx]};
+    uint32_t orbit = uint32_t{orbit_header.first};
+    if (bx > 3554) {
+      orbit--;
+    }  // fix for the fact that bx 3555 - 3564 are from the previous orbit
     uint32_t header = uint32_t{orbit_header.second};  // header can be added to later
     memcpy(wr_ptr, (char *)&header, 4);
     wr_ptr += 4;
     memcpy(wr_ptr, (char *)&bx, 4);
     wr_ptr += 4;
-    memcpy(wr_ptr, (char *)&orbit_header.first, 4);
+    memcpy(wr_ptr, (char *)&orbit, 4);
     wr_ptr += 4;
     for (uint32_t i = 0; i < 8; i++) {
       memcpy(wr_ptr, (char *)&i, 4);
@@ -158,13 +165,19 @@ uint32_t StreamProcessor::FillOrbitMuon(std::vector<unsigned int> &bx_vect, char
     bool AblocksOn[8];
     bool BblocksOn[8];
     uint32_t bx = uint32_t{bx_vect[relbx]};
+    if (bx > 3554) {
+      orbit--;
+    }  // fix for the fact that bx 3555 - 3564 are from the previous orbit
     for (unsigned int i = 0; i < 8; i++) {
       uint32_t bxA = (bl->bx[i] >> shifts::bx) & masks::bx;
-      if ((bxA != bx) && (i == 0)) {
+      if ((bxA != bx) && (i == 0) && (bx < 3555) &&
+          control.verbosity) {  // only prints warning when BX < 3555 i.e from
+                                // the same orbit
         LOG(WARNING) << "BX mismatch, uGMT data word BX = " << std::hex << bxA
                      << ", BX extracted from trailer = " << bx << ", orbitN is " << std::dec
                      << orbit;
       }
+
       uint32_t pt = uint32_t{(bl->mu1f[i] >> shifts::pt) & masks::pt};
 
       AblocksOn[i] = ((pt > 0) || (doZS == 0));
@@ -305,9 +318,17 @@ void StreamProcessor::process(Slice &input, Slice &out) {
       }
 
       if (orbit_per_packet_count > nOrbitsPerDMAPacket) {
-        LOG(WARNING) << "expected DMA trailer word deadbeef, found " << std::hex
-                     << *dma_trailer_word << ". Orbits per packet count " << orbit_per_packet_count
-                     << ", > expected, (" << nOrbitsPerDMAPacket << ") skipping packet.";
+        if (control.verbosity) {
+          LOG(WARNING) << "expected DMA trailer word deadbeef, found " << std::hex
+                       << *dma_trailer_word << ". Orbits per packet count "
+                       << orbit_per_packet_count << ", > expected, (" << nOrbitsPerDMAPacket
+                       << ") skipping packet.";
+        }
+        control.excessOrbitsPerPacketCount++;
+        if (control.excessOrbitsPerPacketCount % 10000 == 0) {
+          LOG(WARNING) << "count of packets with excess # orbits "
+                       << control.excessOrbitsPerPacketCount;
+        }
         return;
       }
     }
diff --git a/src/scdaq.cc b/src/scdaq.cc
index e9866e95..6149484b 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -141,6 +141,9 @@ int main(int argc, char *argv[]) {
     control.packets_per_report = conf.getPacketsPerReport();
     control.output_force_write = conf.getOutputForceWrite();
     control.n_orbits_per_dma_packet = conf.getNOrbitsPerDMAPacket();
+    control.verbosity = conf.getVerbosity();
+    control.excessOrbitsPerPacketCount = 0;
+
     // Firmware needs at least 1MB buffer for DMA
     if (conf.getDmaPacketBufferSize() < 1024 * 1024) {
       LOG(ERROR) << "dma_packet_buffer_size must be at least 1048576 bytes (1MB), but "
diff --git a/test/config/scdaq-calo.conf b/test/config/scdaq-calo.conf
index 8d8d08ab..f00c1e63 100644
--- a/test/config/scdaq-calo.conf
+++ b/test/config/scdaq-calo.conf
@@ -94,3 +94,6 @@ quality_cut:12
 
 # Pipeline settings
 threads:8
+
+# verbosity level, currently supports 0 and 1
+verbosity:1
diff --git a/test/config/scdaq-gmt.conf b/test/config/scdaq-gmt.conf
index cec8f07d..83344d93 100644
--- a/test/config/scdaq-gmt.conf
+++ b/test/config/scdaq-gmt.conf
@@ -95,3 +95,6 @@ quality_cut:12
 
 # Pipeline settings
 threads:8
+
+# verbosity level, currently supports 0 and 1
+verbosity:0
-- 
GitLab