Adding files into TDirectory


I noticed some files are automatically added to a TDirectory such as TH1 objects when cloned.
However it seems this is not the same with all objects such as TGraph.

I would like to use gDirectory::Add(TObject*); for those who ain’t copied.
Can you advise me ? :slight_smile:

Thank you!

If it is sufficient for you that they get added to RAM resident objects:


What do you mean by ‘ain’t copied’? Also gDirectory::Add does not exist, are you using gDirectory->Append?



Well, this function exists right ?

Therefore I developed two functions, one which creates a subdirectory and a second one which is supposed to add the object at the place I want, such as :

TDirectory* RecursiveMkdir(TString dirpath)
void CopyTo(vector<TObject*> vObject, TDirectory *dir_to_copy)
       for( ... ) vObject[i]->Clone();

Those objects are cloned, because deleted later on.
However the function CopyTo, doesn’t work as expected because in some cases it doesn’t add objects. TH1 are added, when Cloned, but TGraph not.
As I found the function TDirectory::Add(TObject*, …); I am currently using the special case in case the object inherits from TGraph, but I don’t understand why TH1 are copy and TGraph not.


Hi Marco,

My bad. I did forget about the Add which is an alias to Append.


but I don’t understand why TH1 are copy and TGraph not.

This is for historical reason. Some types (TTree, Histogram, EventList, TGraph2D) auto-add themselves to the current directory, most do not.

You can use:

TObject *newobj = vObject[i]->Clone();
if (! dir_to_copy->GetList()->FindObject( newobj ) ) {


TDirectory::TContext ctxt(nullptr);
for( ... ) dir_to_copy_>Append(vObject[i]->Clone());
1 Like

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