Home | News | Documentation | Download

Copy directory from one file to another


May I ask you some help about the following problem I face:

I have two rootfiles containing many directories which in turns contains sub-directories, histograms and vectors.

I would like to create a new file containing a copy of some directories of the first file and of some other directories of the second file.


If first file is f1 and contains directories d1_1, d1_2, d1_3, d1_4, …
and if second file f2 contains directories d2_1, d2_2, d2_3, …

I would like to create a third file f3 containing d1_1, d1_4 and d2_6.

Is this possible ? I hope my failures to do this are only due to the lack of expertise in C++ and Root.

Thanks in advance for any help you can provide,


Hello Pierre,

Here is one way to do it:

(I assume that f1 and f2 are TFile pointers holding the addresses of you original files)

TFile* f3=new TFile(“f3.root”,“create”)
f3->WriteObject( f1->GetDirectory(“d1_1”), “d3_1”)
f3->WriteObject( f1->GetDirectory(“d1_4”), “d3_2”)
f3->WriteObject( f2->GetDirectory(“d2_6”), “d3_3”)
delete f3

Best regards

see my anser to the Discussion Forum at:


Hi, I think the link is broken (error 404 !).

Unfortunately we moved the forum from phpBB2 to discourse and, in that particular case, we lost the connection with the old René’s post.