ROOT Version: 6.18/04
Platform: Ubuntu 18.04
Compiler: gcc 7.5.0 (Ubuntu 7.5.0-3)
Dear co-rooters,
I’m trying to open a root file and add a TBranch
to it. Since I want to keep the original file, I thought of copying it and open the clone with the UPDATE
option.
I tried copying the file by piping in bash and using cp
command. If the file lives in another directory (i.e. I execute the macro by add_branch("../raw_data/run1.root")
I get the following error
cp: cannot create regular file 'new_../raw_data/run1.root': No such file or directory SysError in <TFile::TFile>: file new_../raw_data/run1.root can not be opened (No such file or directory)
I understand why I get this error and I know that if I add a suffix in the new file name instead of a prefix it will work. A snippet of my code is
void add_branch(TString filename_in){
// (a) Open the file
TFile *f_root = TFile::Open(filename_in, "READ");
// copy the file with cp command
TString new_filename_in = TString::Format("new_%s", filename_in.Data());
gSystem->Exec( TString::Format("cp %s new_%s", filename_in.Data(), filename_in.Data()) );
f_root->Close();
f_root = TFile::Open(new_filename_in, "UPDATE");
}
I also tried using the TFile::Cp()
function but if the file lives in another directory things get complicated again because I get an error for the similar reasons.
[TFile::Cp] Total 1292.43 MB |====================| 100.00 % [1104.8 MB/s] SysError in <TFile::TFile>: file new_files/../raw_data/run1.root can not be opened (Not a directory)
This is a snippet of the code I’m using.
void add_branch(TString filename_in){
// (a) Open the file
TFile *f_root = TFile::Open(filename_in, "READ");
//create a directory because Cp() wants it
gSystem->Exec("mkdir new_files");
// copy the file in a directory using Cp() function
f_root->Cp("new_files");
f_root->Close();
f_root = TFile::Open(TString::Format("new_files/%s", filename_in.Data()), "UPDATE");
}
What I need is to get the directory of the root file I’m opening but when I’m calling gDirectory->pwd()
I also get the filename i.e.
../raw_data/run1.root:/
Is there a way to get only the directory of the root file or does anyone have any idea on to copy the file?