Opening remote xrootd file: TChain works, TFile does not work

Please read tips for efficient and successful posting and posting code

ROOT Version: 5.34.36
Platform: CentOS 7
Compiler: gcc 4.9

I am trying to access files on a DPM disk server using the xrootd interface.

I usually access files with paths like root://

Using this path I can access the files using the GFAL tools:

[leonardi@padmeui OnlineMonitor]$ gfal-ls -l root://
-r--------   1 600   38116 343687327 Nov 14 09:59 root://

If I try to open these files from within a C++ program, using TChain works fine while I could not find a way to open them using TFile.

The following (short) code demonstrate the problem:

#include "TFile.h"
#include "TChain.h"
#include "TTree.h"
#include "TString.h"
#include "TError.h"
#include "TSystem.h"
int main(int argc, char* argv[])
  TString treeName = "RawEvents";
  TString fileName = "root://";
  TFile* ff = new TFile(fileName.Data(),"READ");
  delete ff;
  ff = 0;
  TChain* chain = new TChain(treeName.Data());
  printf("Chain has %lld entries\n",chain->GetEntries());
  delete chain;
  chain = 0;

If I run this code, TFile gives an error:

Error in <TFile::TFile>: file /dpm/ does not exist

while TChain works correctly and reports 1000 entries as the content of the file.

What am I doing wrong when I use the TFile command?

Emanuele Leonardi

Hi Emanuele,
using TFile::Open instead of the TFile constructor should work: the former triggers the loading of any required plug-in such as xrootd.


Hi Enrico.

Thanks for the useful input: using TFile::Open I could correctly access the remote files.

If you do not mind, I would like to ask a further question, somewhat related to the previous one.

I noticed that the gSystem->AccessPathName() call to check if a file exists does not work for dpm. I am now trying to use the TFile::Open() call to check if the file exists as, according to the reference manual, it should return 0 if the file is not found.

With that in mind I came up with the following code:

Bool_t InputHandler::FileExists(TString fileName)
  //if (! gSystem->AccessPathName(fileName.Data())) return true; <- Does not work with dpm
  Bool_t exists = true;
  printf("Testing file %s\n",fileName.Data());
  gErrorIgnoreLevel = kBreak; // Will only report kBreak, kSysError, kFatal
  TFile* testTF = TFile::Open(fileName.Data(),"READ");
  if (testTF == 0) {
    printf("File %s does not exist\n",fileName.Data());
    exists = false;
  } else {
    printf("File %s exists\n",fileName.Data());
    delete testTF;
  gErrorIgnoreLevel = kError; // Will only report kError, kBreak, kSysError, kFatal
  return exists;

This code seems to work fine but could you please give a look and check if the way I allocate and deallocate the TFile-related memory is correct?

Thank you very much,


Looks ok! You can also (probably should) just use a unique_ptr and forget about manual memory management:

std::unique_ptr<TFile> file(TFile::Open(...));

Ah and even if tesTF is not a nullptr, you should check testTF->IsZombie(): it returns true if something went wrong in opening the file.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.