From afe12683eea14bc30c1b7c0b2f71a3235d5cb0f3 Mon Sep 17 00:00:00 2001
From: Dinyar Rabady <dinyar@cern.ch>
Date: Wed, 18 Oct 2023 15:19:24 +0200
Subject: [PATCH] Don't write uninitialised slice to output

---
 src/OutputByOrbit.cc     | 19 ++++++++++++-------
 src/OutputFileHandler.cc |  8 ++++----
 src/processor.cc         |  1 -
 src/slice.h              | 14 ++++++++++----
 4 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/src/OutputByOrbit.cc b/src/OutputByOrbit.cc
index 895fcf66..8a85ae0f 100644
--- a/src/OutputByOrbit.cc
+++ b/src/OutputByOrbit.cc
@@ -25,18 +25,22 @@ OutputByOrbitStream::OutputByOrbitStream(ctrl &ctrl, config &conf)
 
 void OutputByOrbitStream::OutputFixedOrbits(Slice &out) {
   uint32_t orbitN = out.get_firstOrbitN();
-  LOG(TRACE) << "First orbit number in file received is " << orbitN << ".";
+  // LOG(TRACE) << "First orbit number in file received is " << orbitN << ".";
   uint32_t index = uint32_t(orbitN / conf_.getNOrbitsPerFile());
   size_t n = 0;
-  if (index == 0) {
-    LOG(DEBUG) << "index is 0";
-  }
-  if (out.get_counts() == 0) {
-    LOG(TRACE) << "got an empty slice ";
-  }
+  // if (index == 0) {
+  //   LOG(DEBUG) << "index is 0";
+  // }
+  // if (out.get_counts() == 0) {
+  //   LOG(TRACE) << "got an empty slice ";
+  // }
   if ((out.get_counts() != 0) || conf_.getCMSSWHeaders()) {
     if (control_.running.load(std::memory_order_acquire) ||
         control_.output_force_write) {  // i.e should be writing data
+      if (!out.isInitialized()) {
+        // LOG(TRACE) << "Slice is uninitialised! Skipping.";
+        return;
+      }
       n = fwrite(out.begin(), 1, out.size(),
                  output_file_handler_.getFile(control_.run_number, index).getFilePtr());
       output_file_handler_.upFileSize(n);
@@ -47,6 +51,7 @@ void OutputByOrbitStream::OutputFixedOrbits(Slice &out) {
       LOG(TRACE) << "the run was stopped. queueing the last file for close and "
                     "rename ";
       output_file_handler_.enqueue_current_file_for_close_and_move_maybe();
+      // TODO: Need to write EoR file here, probably.
     }
   }
 }
diff --git a/src/OutputFileHandler.cc b/src/OutputFileHandler.cc
index d6950846..05e5bd68 100644
--- a/src/OutputFileHandler.cc
+++ b/src/OutputFileHandler.cc
@@ -117,7 +117,8 @@ void OutputFileHandler::open_new_file() {
   std::string full_filename = working_files_base_path_ + "/" + filename;
   LOG(TRACE) << "opening file with index " << current_index_ << ", in lumisection " << ls;
   OutputFile outputFile(fopen(full_filename.c_str(), "wbx"), filename, createFileHeader(ls), ls,
-                        current_index_ % (max_index_per_ls_ + 1), run_dir_);
+                        current_index_ % (max_index_per_ls_ + 1),
+                        run_dir_);  // TODO: run_dir_ probably needs also the run number
   if (!outputFile.exists()) {
     std::string err =
         tools::strerror("ERROR when creating file '" + outputFile.getFileName() + "'");
@@ -149,7 +150,7 @@ std::string OutputFileHandler::format_filename(uint32_t run_number, uint32_t ind
 
 void OutputFileHandler::close_and_rename::operator()() const {
   OutputFile outputFile;
-  while (file_handler_running_ && (files_to_close_.size() > 0)) {
+  while (file_handler_running_ || (files_to_close_.size() > 0)) {
     LOG(TRACE) << "try pop now. queue size now " << files_to_close_.size();
     try {
       files_to_close_.pop(outputFile);
@@ -162,8 +163,7 @@ void OutputFileHandler::close_and_rename::operator()() const {
     if (fclose(outputFile.getFilePtr()) != 0) {
       LOG(ERROR) << tools::strerror("File close failed");
     }
-    std::string from =
-        outputfilehandler_->working_files_base_path_ + "/" + outputFile.getFileName();
+    std::string from = outputFile.getRunDir() + "/" + working_dir_ + "/" + outputFile.getFileName();
     std::string to = outputFile.getRunDir() + "/" + outputFile.getFileName();
     if (rename(from.c_str(), to.c_str()) != 0) {
       LOG(ERROR) << tools::strerror("File rename of " + outputFile.getFileName() + " failed");
diff --git a/src/processor.cc b/src/processor.cc
index a95ca3b2..76daa92a 100644
--- a/src/processor.cc
+++ b/src/processor.cc
@@ -445,7 +445,6 @@ void StreamProcessor::process(Slice &input, Slice &out) {
     assert(rd_ptr <= rd_end_ptr);
     counts += orbitCount;
     if (firstOrbit) {
-      LOG(TRACE) << "Setting first orbit to " << meta.orbit;
       out.set_firstOrbitN(meta.orbit);
       firstOrbit = false;
     }
diff --git a/src/slice.h b/src/slice.h
index d64a07a9..aa080585 100644
--- a/src/slice.h
+++ b/src/slice.h
@@ -15,7 +15,8 @@ class Slice {
   uint32_t counts;
   bool output;
   static tbb::concurrent_bounded_queue<Slice *> free_slices;
-  uint32_t firstOrbitN;
+  uint32_t firstOrbitN_;
+  bool initialized_{false};
 
  public:
   //! Allocate a Slice object that can hold up to max_size bytes
@@ -41,7 +42,8 @@ class Slice {
     logical_end = begin();
     counts = 0;
     output = false;
-    firstOrbitN = UINT_MAX;
+    firstOrbitN_ = UINT_MAX;
+    initialized_ = false;
   }
 
   //! Free a Slice object
@@ -59,7 +61,11 @@ class Slice {
   void set_output(bool o) { output = o; }
   void set_counts(uint32_t c) { counts = c; }
   uint32_t get_counts() const { return counts; }
-  uint32_t get_firstOrbitN() const { return firstOrbitN; }
-  void set_firstOrbitN(uint32_t firstOrbitN_) { firstOrbitN = firstOrbitN_; }
+  uint32_t get_firstOrbitN() const { return firstOrbitN_; }
+  void set_firstOrbitN(uint32_t firstOrbitN) {
+    firstOrbitN_ = firstOrbitN;
+    initialized_ = true;
+  }
+  bool isInitialized() { return initialized_; }
 };
 #endif
-- 
GitLab