Hi everyone,
I have thousands of .root data files that I would like to combine using hadd. The problem is that some of them cannot be opened and I would like to come up with a script that loops through the files and remove the null ones and then do hadd, but my knowledge of C and C++ is limited. Anyone has any suggestions?
Thank you 
Hi @azinbeigi ,
and welcome to the ROOT forum! You can do this from C++ or Python equivalently, you want this:
good_files = []
for filename in filenames:
f = ROOT.TFile.Open(filename)
if (f is not None and not f.IsZombie())
good_files.append(filename)
(similarly for C++, instead of None
you will have a nullptr
).
See also our manual and the primer.
Cheers,
Enrico
Hi Enrico,
Thank you for your response. So, I rand this simple python code:
import os
import ROOT
good_files=[]
path_of_the_directory= ‘/home/shirin/TPS/Xoft/ResultsFromCluster/fixed_complete/’
print(“Files and directories in a specified path:”)
for filename in os.listdir(path_of_the_directory):
f = os.path.join(path_of_the_directory,filename)
f = ROOT.TFile.Open(filename)
if (f is not f.IsZombie()):
good_files.append(filename)
I get this error when I run that:
Files and directories in a specified path:
Error in TFile::ReadBuffer: error reading all requested bytes from file 635635501.xRay.root, got 290 of 300
Error in TFile::Init: 635635501.xRay.root failed to read the file type data.
Traceback (most recent call last):
File “getridfiles.py”, line 9, in
if (f is not None and not f.IsZombie()):
ReferenceError: attempt to access a null-pointer
this line should be if f is not None and not f.IsZombie():
Without that, you will try to use files even if they could not be opened correctly, as it is for 635635501.xRay.root.
Cheers,
Enrico
Thank you, but even with that line, I get the same error:
Error in TFile::ReadBuffer: error reading all requested bytes from file 635635501.xRay.root, got 290 of 300
Error in TFile::Init: 635635501.xRay.root failed to read the file type data.
Traceback (most recent call last):
File “getridfiles.py”, line 9, in
if (f is not None and not f.IsZombie()):
ReferenceError: attempt to access a null-pointer
Uhm weird, 635635501.xRay.root is a malformed file and it looks like we don’t manage to detect that when opening it. Can you share 635635501.xRay.root with us?