Adding files into TDirectory

Hello,

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:

YourGraph->SetName("My_Graph");
gROOT->GetListOfSpecials()->Add(YourGraph);

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

Cheers,
Philippe.

Hello,

Well, this function exists right ?
https://root.cern.ch/doc/master/classTDirectory.html#a779aefffdf99d62ba18cf5cdcaf3d7b3

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)
{
       dir_to_copy->cd();
       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.

Cheers,
Marco

Hi Marco,

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

Cheers,
Philippe.

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 ) ) {
    dir_to_copy->Append(newobj);
}

or

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.