Dear experts,
I am running a python script that produces several histograms and saves them in various subdirectories od a ROOT file. I run this script multiple times using HTCondor and I make sure that no file is being used by two jobs.
The problem I face is that some output files get corrupted when I open it in “UPDATE” mode. I see errors such as:
Error R__unzip_header: error in header. Values: 7574
or
Error in <TFile::ReadKeys>: reading illegal key, exiting after 0 keys
Here is my code snippet:
def process_weight(weight, input_file, conditions, output_filename, add_dir, UnfoldVar_reco_list, binning_list, NrBins_list):
list_hist = []
for index in range(0, len(UnfoldVar_reco_list)):
reco_hist = make_hist(input_file, tree, conditions, weight, UnfoldVar_reco_list[index], binning_list[index], NrBins_list[index])
if reco_hist is not None:
reco_hist.SetName("PL_reco_" + UnfoldVar_reco_list[index])
list_hist.append(reco_hist)
if weight in float_weights:
weight_directory = os.path.join(output_dir_nom, f"{weight}")
os.makedirs(weight_directory, exist_ok=True)
else:
weight_directory = os.path.join(output_dir_nom, new_wt_list[og_list.index(f"{weight}")])
os.makedirs(weight_directory, exist_ok=True)
if add_dir == "":
file_path = os.path.join(weight_directory, f'{output_filename}')
else:
final_dir = os.path.join(weight_directory, f"{add_dir}")
os.makedirs(final_dir, exist_ok=True)
file_path = os.path.join(final_dir, f'{output_filename}')
if os.path.exists(file_path):
outFile = ROOT.TFile.Open(file_path,"UPDATE")
else:
outFile = ROOT.TFile.Open(file_path,"RECREATE")
outFile.cd()
# Check if the directory already exists
subdir = getattr(outFile, region, None)
if not subdir:
# If it doesn't exist, create it
subdir = outFile.mkdir(region)
# Change to the directory
subdir.cd()
# Write all histograms from list_hist to the ROOT file
for hist in list_hist:
hist.Write(hist.GetName(), ROOT.TObject.kOverwrite)
outFile.Write()
outFile.Close()
return True
I call this function for around 300 weights and each weight has a different output File. Out of those, randomly some weight output file show those errors. When I rerun that weight, then the file is OK.
I don’t know why this problem occurs only in some of the files and not all, eventhough I use the same code! Is there something I could do to make sure that my output file writing is handled flawlessly?
Thank you in advance!
Regards,
Nilima