Hi,
the following program is supposed to read all objects from directory ‘dir’ in file ‘file.root’ and save them into directory ‘dir’ in file ‘copy.root’:
#include <TFile.h>
#include <TKey.h>
int main(int argc,char**argv)
{
TFile* ifile = new TFile("file.root","READ");
TDirectory* idir = (TDirectory*)ifile->Get("dir");
TFile* ofile = new TFile("copy.root","RECREATE");
TDirectory* odir = (TDirectory*)ofile->mkdir("dir");
odir->cd();
TIter next(idir->GetListOfKeys());
TKey* key(0);
while ((key=(TKey*)next())) {
TObject* obj = key->ReadObj();
//obj->Write();
}
odir->Write();
delete odir;
ofile->Close();
delete ofile;
ifile->Close();
delete ifile;
return 0;
}
When I do this on linux slc4 with ROOT 5.18, it works as is; on my Mac using ROOT 5.20 on the other hand, the output directory will be empty unless I add the obj->Write()
call. Why? What is the proper way to do it? I thought that if I instantiate an object (e.g. by loading it) and odir is the current directory, the object will be in it, and odir->Write()
will capture it?
Thanks,
Philipp