From 9f4142a96ed3815242aed2ad5c8085028bc4a6be Mon Sep 17 00:00:00 2001
From: Giovanna Lazzari Miotto <giovanna.lazzari.miotto@cern.ch>
Date: Wed, 28 Feb 2024 16:08:51 +0100
Subject: [PATCH] Add Rename and Flush functions

---
 src/OutputFile.h | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/OutputFile.h b/src/OutputFile.h
index c94ae550..7d0d589a 100644
--- a/src/OutputFile.h
+++ b/src/OutputFile.h
@@ -6,6 +6,7 @@
 #include <atomic>
 #include <cstdint>
 #include <cstdio>
+#include <filesystem>
 #include <fstream>
 #include <iostream>
 #include <string>
@@ -14,6 +15,9 @@
 #include <vector>
 
 #include "cmssw/FRDFileHeader_v2.h"
+#include "log.h"
+
+namespace fs = std::filesystem;
 
 class OutputFile {
  public:
@@ -53,13 +57,32 @@ class OutputFile {
     }
   }
 
+  std::string GetFullPath() const { return fs::path(rundir_) / filepath_ / fileName_; }
+
+  void Rename(const std::string& new_path) const {
+    fs::path from = GetFullPath();
+    fs::path to = new_path;
+
+    try {
+      fs::rename(from, to);
+    } catch (fs::filesystem_error& err) {
+      LOG(FATAL) << "Error renaming file: " << err.what();
+    }
+  }
+
+  void Flush() {
+    if (file_.is_open()) {
+      file_.close();
+    }
+  }
+
   void setFileHeader(FRDFileHeader_v2 fileHeader) { fileHeader_ = fileHeader; }
 
   uint32_t Lumisection() { return lumisection_; }
   uint32_t Index() { return index_; }
 
  private:
-  FILE* filePtr_;
+  std::string filepath_;
   std::string fileName_;
   FRDFileHeader_v2 fileHeader_;
   uint32_t lumisection_;
-- 
GitLab