Creating histogram without key in directory


I would like to create a vector of histograms. I would like to have the histograms not having any keys in gDirectory. Because it would be unneccesary mess if I would have to create different names for hundreds of histograms, and taking care of that there can be multiple vectors of histograms which as well should have then different names. And If I would create all histograms with the same name, I would get a bunch of warning messages, that there might be leakage.

I tried to create a histogram with the default constructor and then set the properties of the x-axis, but when I do so, I cannot draw the histogram then.


First the code where the histogram gets drawn without problems, but a key is put into the directory:

root [2] hist = new TH1F("temp","temp",5,0.,5. );
root [3] .ls
TROOT*          Rint    The ROOT of EVERYTHING
 OBJ: TH1F      temp    temp : 0 at: 0xaac6af8
root [4] hist->Fill( 3 )
root [5] hist->Draw()

Here is the code where I tried to get rid of the key:

root [6] h2 = new TH1F()
root [7] h2->GetXaxis()->Set(5,0.,5.)
root [8] h2->Fill( 3. )
root [9] h2->Draw()
<TCanvas>: created default TCanvas with name c1

in this case a histogram is drawn, even with the right axis-demension, but its empty.

I dont quite understand why the histogram is drawn empty at the second example. How could I solve this problem?

But maybe there is even another more appropiate method to prevent root from setting the key in the directory.

I tried to simply hist->SetDirectory(0), but this didn’t change anything about the key itself.


Use TH1::AddDirectory(kFALSE);

see chapter about Object Ownership in the Users Guide.