Rename a histogram inside a file

I have a TH1 h named “old_name” in a TFile f.
Can I rename h to “new_name” inside f?

I try the following code:

TFile *f = TFile::Open("file.root");
TH1 *h = (TH1*) f->Get("old_name");
h -> SetName("new_name");
f -> Save();

After opening file.root again, calling

TH1 *h = (TH1*) f->Get("new_name");

gives a nullptr, while

TH1 *h = (TH1*) f->Get("old_name");

gives a non-zero pointer

so my code does not work.

rootmv, described here can help you.


The name of the histogram I want to rename contains a colon:


By writing

rootmv file:part1_:_part2 file:new_name

the command looks for an object named part1_ inside file. How to escape the colon?

try a wild card:

rootmv file:part1_*_part2 file:new_name
1 Like

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