Hi all,
I am building a large chain out of many trees, and I get an error occasionally. I get the error for only a few of my runs (less than 1%), but it causes a segfault each time which is a pain. The error is:
Error in TFile::ReadBuffer: error reading all requested bytes from file filename.root, got 234 of 300
I looked up previous cases, but they all had “got 0 out of SomeNumber” and my case appears different because of getting some of the bytes.
My loop is:
TTree *bush=(TTree*)fIN->Get("bush");
if (!bush){ cout <<"--->NO first bush.\n"; return 0;}
cout <<"--->Got the first bush.\n";
TChain *fChain = new TChain("bush");
fChain->SetMakeClass(1);
TFile *f;TString fname;
for(int i=firstRUN;i<lastRUN; i++){
fname = inDIR + fileINbase + Form("%d.root",i);
ifstream ifile(fname);
if(!ifile) {cout <<"---->bush "<< i << " is missing.\n"; continue;}
f = TFile::Open(fname.Data());
if(!f) cout <<"The file could not be opened!";
fChain->Add(fname.Data());
f->Close();
fname.Clear();
}
Can anyone suggest either a way to eliminate this error, or to have better error handling? Or both?
[quote]Error in TFile::ReadBuffer: error reading all requested bytes from file filename.root, got 234 of 300
…
Can anyone suggest either a way to eliminate this error,[/quote]
Most likely those file are corrupted/truncated and can not be used (i.e. could a physical disk error).
In your code testing for the failing case try to useif (!f || f->IsZombie) {
cout <<"The file could not be opened!";
delete f;
} else {
fChain->Add(fname.Data());
}
fname.Clear();
In the code snippet included ‘file’ is never modified:
file = TFile::Open(fname.Data());
if(!file||file->IsZombie()){delete file; continue;}
else{fChain->Add(fname.Data());}
delete file; // (includes calls to Close)
Note that “file->Delete()” does not delete the TFile object, just the in-memory content (user objects) attached the TFile.