Change TTree name in Tdirectory

Dear root users,

I am trying to change the name of a TTree in a root file located in a TDirectory.

root [0] TFile f("myfile.root", "update");
root [1] TTree *T = (TTree*)f.Get("Directory/Old_name");
root [2] T->SetName("New_name");
root [3] T->Write();

or

root [0] TFile f("myfile.root", "update");
root [1] TTree *T = (TTree*)f.Get("Directory/Old_name");
root [2] T->SetName("Directory/New_name");
root [3] T->Write();

neither are working and I obtain at the end in my root file some strcuture like :

TDirectory / "Old_Name"
"New_Name"

or

TDirectory / "Old_Name"
"Directory/New_name"

either way, some TTree is created with the correct name, but not located in the TDirectory and the TTree with the old name is still here…

Can you help me?
Thank you


ROOT Version: 6/12.06


Hi Marine,

Welcome to the ROOT forum. To best help you, let me ask you a question. Why do you want to change the name? i.e what lead you to want to update an existing file (rather than recreating for example). Do you also want to change the location in the file?

Cheers,
Philippe.

Hi,

I need to change the name of my TTree to perform a merging of root file then.
I thought it was possible to change the name of a TTree already existing updating the root file…

Marine

It is sorta possible to change the name but it is more of a copy of the meta data than a change in name (i.e. you need to delete the old one if you want to). For example:

TFile f("myfile.root", "update");
TTree *T = f.Get<TTree>("Directory/Old_name");
TDirectory d = f.GetDirectory("Directory");
T->SetName(newname);  // Added in update to this post.
d->Delete("Old_name");
T->SetDirectory(&f);
f.Write();

However this might not be the easiest way to handle merge.
For example you may use:

TChain ch("New_name");
ch.Add(file_with_new_name.root);
ch.Add(file_with_old_name.root, "Directory/Old_name");
std::unique_ptr<TFile> of { TFile::Open(output_filename, "RECREATE");
ch.CloneTree("fast");
f->Write();

There is no way to use the SetName() function from the TTree?

I am not sure i understood where the name was changed in your first solution.

In your second solution you are changing the name of the root file but not of the TTree in the root, am I correct?

Thank you again for your help

I am not sure i understood where the name was changed in your first solution.

It was inadvertently missing. I corrected the code.

In your second solution you are changing the name of the root file but not of the TTree in the root, am I correct?

In the second example, I assume that you have many existing file, some with the correct name, some with the incorrect name and that you wanted to merge them into a single file.

The point of the second example is that you might not even need to actually update the old files to be able to use them to do what you need (eg. the TChain as created there could be used as input to RDataFrame).

actually sorry it was not clear, but I need to merge root files with TTree with the same names (with the structure directory/old_name;1 & directory/old_name;2 etc.), and it appears that the TTree are erased in the merging if they have the same name… that is why i wanted to change the name of my TTrees in order or to have a correct merging of my root files

with TTree with the same names (with the structure directory/old_name;1 & directory/old_name;2 etc.)

Are you really sure? In the usual case the key named old_name;1 is a backup of the key old_name;2 and points to a subset of the data (for example ;1 was a snapshot of the meta data after 1000 entries and then ;2 points to all of that data plus any other entries after the snapshot). i.e. if you do this merge in the usual case the resulting case with contains the same data multiple time.

In other words, did you do anything unusual when writing the file so that you will not be duplicating data when merging the 2 keys’ underlying content?

I don’t know about usual cases…
The thing is that my root file is written with the structure I described you, with distinguish data set in the TTree “old_name;1” and “old_name;2”, and even if they are separated in the original root file (as in different TTree) they are erased in the merging process of two root file with this same structure and only the last TTree with the name “old_name” is left in the resulting merged file.

So if I understand correctly, the merging does not work when different TTree are with the same name.

even if they are separated in the original root file (as in different TTree) they are erased in the merging process

The merging tools assume that the cycles (the ;1 and ;2) are used as intended as backup and snapshot and thus the merging tools ignores (as it should) all but the highest cycles numbers. Consequently if you are really in the case you describe, you will to write the merging code explicitly (I.e. retrieve each keys individually, create the target file and tree and copy the data from the 2 inputs to the single output).

The thing is that my root file is written with the structure I described you, with distinguish data set in the TTree “old_name;1” and “old_name;2”

To verify that I am actually understanding what you mean, can you please the code that accomplish this?

Thanks,
Philippe.