Histogram name change on write

Hi,
I have been trying to write histograms to file while at the same time changing their names, such that the naming convention I use in the program need not infringe on the file I create and later pass to someone else. To do this I use something along the lines of:

TH1F *hfix = new TH1F(“hfix”,“hfix title”,nbins,xlow,xup);
hfix->Write(“newname”,TObject::kWriteDelete);

Surely enough, when I look in the file using TBrowser, I can see that all the histograms have the new name; but on closer inspection, it seems the names have not changed in the TFile at all, as doing a Get from a file looks for the old name, and an inspection of the histogram shows no trace of the name supplied in the TBrowser, but still cites the old name. Is there any way to load the Histograms using the name I had intended to same them under, or the one which appears in the TBrowser?

Cheers,
Toby Davies

2 Likes

Hello Toby,
The Write method doesn’t change the object name. It changes the object key name. The file browser at some point has no access to the object, it does see the TKey counterparts only. By default, the key name is the object name.
To do what you want, you should change the object name before you write it out. I would like to call your attention to the fact that
root.cern.ch/root/htmldoc/TH1.html TH1 class is derived from TNamed. The later contains the method to change the object name
root.cern.ch/root/htmldoc/TNamed … ed:SetName

May be you want something like this:

TH1F *hfix = new TH1F("hfix","hfix title",nbins,xlow,xup); hfix->SetName("newname"); hfix->Write("newname",TObject::kWriteDelete);

Hi - thanks for the reply!
I’ll do this in future, but now that I have files which have Tkeys which are meaningful, I would like to be able to serch over them. Looking at the code, I think I could use the FindKey() function of the TDirectory from which TFile is inherited, then do a ReadObj() on the key to get the TH1F, presumably doing a cast at this time from TObject to TH1F. Will this procedure work?

I am hoping that it will be able to read both the cases where I have changed only the TKey in the past and the new proceedure of changing the object name, based upon the fact that changing the object name will change the TKey name too. Is this correct?

Cheers,
Toby

[quote=“tdavies”]Hi - thanks for the reply!
I’ll do this in future, but now that I have files which have Tkeys which are meaningful, I would like to be able to serch over them.

Will this procedure work? [/quote]Yes, it should work. Please, see the class root.cern.ch/root/html512/TFileI … escription. That uses the similar approach to navigate the TFile object.