2222#include < TFile.h>
2323#include < TTree.h>
2424#include < filesystem>
25+ #include < mutex>
2526#include < fmt/format.h>
2627
2728namespace o2
@@ -77,9 +78,10 @@ class TRDKrClsWriterTask : public o2::framework::Task
7778
7879 void closeOutputFile ()
7980 {
80- if (!mOutputFileCreated ) {
81+ if (!mOutputFileCreated || mOutputFileClosed ) {
8182 return ;
8283 }
84+ std::lock_guard<std::mutex> guard (mMutex );
8385 writeToFile ();
8486 mTreeOut .reset ();
8587 mFileOut ->Close ();
@@ -103,6 +105,7 @@ class TRDKrClsWriterTask : public o2::framework::Task
103105 LOG (error) << " Failed to store meta data file " << metaFileName << " , reason: " << e.what ();
104106 }
105107 }
108+ mOutputFileClosed = true ;
106109 }
107110
108111 void run (o2::framework::ProcessingContext& pc) final
@@ -111,14 +114,14 @@ class TRDKrClsWriterTask : public o2::framework::Task
111114 auto tInfo = pc.services ().get <o2::framework::TimingInfo>();
112115 createOutputFile (tInfo.runNumber , pc);
113116 }
117+ if (mRunStopRequested ) {
118+ return ;
119+ }
114120 if (pc.transitionState () == TransitionHandlingState::Requested) {
115121 LOG (info) << " Run stop requested, closing output file" ;
116122 mRunStopRequested = true ;
117123 closeOutputFile ();
118124 }
119- if (mRunStopRequested ) {
120- return ;
121- }
122125 auto cluster = pc.inputs ().get <gsl::span<KrCluster>>(" krcluster" );
123126 auto triggerRecords = pc.inputs ().get <gsl::span<KrClusterTriggerRecord>>(" krtrigrec" );
124127 for (const auto & cls : cluster) {
@@ -148,8 +151,10 @@ class TRDKrClsWriterTask : public o2::framework::Task
148151 bool mRunStopRequested {false };
149152 bool mStoreMetaFile {false };
150153 bool mOutputFileCreated {false };
154+ bool mOutputFileClosed {false };
151155 int mAutoSave {0 };
152156 uint64_t mTFCounter {0 };
157+ std::mutex mMutex ;
153158 std::string mOutputDir {" none" };
154159 std::string mMetaFileDir {" /dev/null" };
155160 std::string mHostName {};
0 commit comments