Easy way to get a histogram from a ttree

The documentation says that to save a histogram one must:

Example:
tree.Draw(“sqrt(x)>>hsqrt”,“y>0”)
will draw sqrt(x) and save the histogram as “hsqrt” in the current
directory. To retrieve it do:
TH1F hsqrt = (TH1F)gDirectory->Get(“hsqrt”);

but from the commandline a simple ttree->Draw(“branch”) draws the histogram I want.

My question is: is they’re any simple GetHistogram member function that will return a pointer to the histogram that would have otherwise been created by the Draw() command. (That is, is there a way without using the >> operator.) I know there is a TTree::GetHistogram function, but this method seems to be made for creating a histogram out of some general function.

My guess is that there is something I’m not understanding from the C++ that wont allow someone to do something like:

TH1F *myhisto = mytree->GetHistogram(“mybranch”);
myhisto->Draw();

Thanks for all your help.

James

1 Like

You have the following choices

CASE 1

You create your own histo before TTree::Draw and let TTree::Draw fill it

TH1F *myh = new TH1F("myh","....); tree.Draw("sqrt(x)>>myh","y<0>>myh","y<0",..) TH1 *myh = gDirectory->Get("myh");
CASE 3

Use the default histogram named “htemp” created by TTree::Draw

tree.Draw("sqrt(x)","y<0",..) The histogram exists only in the current pad and will be destroyed
by the next TTree::Draw command. It is not allocated to the current dir

TH1 *myh = (TH1*)gPad->GetPrimitive("htemp");

[quote]My guess is that there is something I’m not understanding from the C++ that wont allow someone to do something like:

TH1F *myhisto = mytree->GetHistogram(“mybranch”);
myhisto->Draw();
[/quote]
No, this would be quite bad, because next week somebody will request

mytree->GetHistogram("mybranch","some selection"); and the week after a request for

[code] mytree->GetHistogram(“mybranch”,“some selection”,“graphics option”,Nentries,firstentry);

[/code]Rene

1 Like

If I use case 1, ROOT will draw the histogram, of course. Can I tell ROOT to not draw the histogram just fill it?

Use option "goff"
tree.Draw(“sqrt(x)>>myh”,“y<0>>myh”,“goff”);

see TTree::Draw options at root.cern.ch/root/htmldoc/TTree.html#TTree:Draw

Rene

Thank you-- missed that one in the options. Sorry to trouble you.