Invalid pointer to histograms created via TTree::Draw


I stumbled across a ‘trivial’ problem which however is somewhat annoying. When creating a histogram with TTree::Draw via e.g. (with TTree named n)


a histogram is shown and stored as a namend object in the current TDirectory (as far as I understand it). If I repeat this command with ROOT 5.xx.x there is always created a new TH1F* replacing the old one in the TDirectory, i.e.

root [1] n->Draw("m01>>h1(100,2.8,3.4","");
root [2] h1
(class TH1F*)0x3505520
root [3] n->Draw("m01>>h1(100,2.8,3.3","");
root [4] h1
(class TH1F*)0x34f2cc0 // <== different pointer

In ROOT 6, however, the pointer/object is not updated or replaced:

root [1] n->Draw("m01>>h1(100,2.8,3.4","");
root [2] h1
(TH1F *) 0x51f42f0
root [3] n->Draw("m01>>h1(100,2.8,3.3","");
root [4] h1
(TH1F *) 0x51f42f0// <== the same old pointer

This leads in particular to the problem that I get a segfault if trying to do a h1->Draw() after the second TTree::Draw(…).

root [1] n->Draw("m01>>h1(100,2.8,3.4","");
root [2] h1->Draw()
root [3] n->Draw("m01>>h1(100,2.8,3.3","");
root [4] h1->Draw()

 *** Break *** segmentation violation

When executing .ls I get indeed the changed pointer

root [5] .ls
TFile**         D4230_B703p02_all1100/mrg_1117.root
 TFile*         D4230_B703p02_all1100/mrg_1117.root
  OBJ: TTree    n       title : 0 at: 0x4b643c0
  OBJ: TH1F     h1      m01 : 0 at: 0x529c970
root [6] h1
(TH1F *) 0x51f42f0 // <== different address than with .ls above

but this is obviously not propagated to the variable h1 in scope. I think that this it related to the behaviour in ROOT6 not to recreate objects in the root prompt which already exist by name.

Does somebody know whether there is a practical way around this problem?

Best regards and thanks,

Please read tips for efficient and successful posting and posting code

_ROOT Version:6.06/02
Platform: linux
Compiler: Not Provided

As a workaround you always do

h1 = dynamic_cast<TH1*>(gDirectory->Get("h1"));

Thanks for the fast reply!

Yes, I already realized that I can fetch the new histogram by name from gDirectory.

I was wondering whether there is some switch in ROOT6 to ‘emulate’ the old behaviour, i.e. updating the object pointer in scope. Isn’t it considered as a bug (or at least missing feature)?


I have created - I agree it’s a regression from ROOT 5. There are two issues with this:

  • even after many years, not a lot of people have complained about this.
  • it’s non-trivial to fix:

For cling, we really declare a variable of that type. Upon histogram deletion and recreation we’d have to update the variable’s value (and possibly even type) - but it would be TDirectory having to tell cling about that change. Maybe someone has a better idea how to implement that…

Thanks for your report!


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