Example to show how to display histograms?

Hi all,

Is there an example that will show me how to display the histograms in the attached picture?

Thanks in advance

Tim

root_screenshot

Hi Tim,

You can double-click on the histogram and it will appear on the right side of the browser.

Cheers,
Jakob

Hi Jakob, thanks for your reply.

I suppose I wasn’t sufficiently clear with my request. I want to draw the histograms by code and not through the Root browser so I can do some funky fitting stuff in that code. But I’ve found an example that may help (tree1.C). I’ll play with that first.

Regards

Tim

your_histogram->Draw();

Apologies for late reply. I’m a part-timer and don’t get as much time to dedicate to this as I’d like.

Having thought about this for a while I think my confusion stems from the difference between a tree, branch and leaf.

If we look at the picture I posted, there are examples that describe how to read and display histograms on the level immediately below the root file but my histograms are 2 levels below that. May I consider those histograms (in this case) as leaves of the branch PerEntryHistograms which is a branch off Event which is, in turn a branch off the root file?

If I’m correct in my above assumption I suppose I would address the branch Event and, off that, address the branch PerEntryHistograms and, finally, open and draw my histograms?

Regards

Tim

your_file->cd("subdir");
your_histogram->Draw();

Hi Olivier,

sorry not at my machine with root installed currently. But do the “branches” appear like directories to root?

Regards

Tim

What your picture shows is a TFile with several levels of directories. At the bottom level you have histograms. You just need to cd to that level and do a Draw of your histogram.

Thanks Olivier. I found this great tut which explains it well: SLAC link

So I’m on my way…I hope :blush:

Regards

Tim

1 Like

Hi all

Oliviers suggestion worked fine but only from the Root command line. If I try do the same in a macro it raises an error when I try to Draw the histogram. Not on my Linux machine currently but will paste the error when I get home.

Regards

Tim

In a macro do:

your_file->cd("subdir");
TH1F *h = (TH1F*)your_file->Get("your_histogram");
h->Draw();

Sorry for being an idiot but I’m getting a warning when I use your suggestion:

Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_28:1:
/home/tim/Data/Thesis/bdsim/WP/read_plot_from_file.C:20:2: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
h->Draw();

I tried passing a non-null argument in Draw (“SAME”).

Regards

Tim

read_plot_from_file.C (504 Bytes)

Try:

gDirectory->ls();
TH1F *h1; gDirectory->GetObject("Primary_Energy", h1);

Nah, sorry. Same error.

What do you get from gDirectory->ls();

Truncated, terser, version of previous f->ls();

TDirectoryFile* PerEntryHistograms PerEntryHistograms
KEY: TH1D Primary_Energy;1 Primary_Energy
KEY: TH1D EnergyLoss;1 EnergyLoss

So, if you have a “TH1D” in your file, why are you surprised that it cannot retrieve a “TH1F”?

'cause I’m as thick as :blush: (in my defence I’m a noob).

Cheers mate. That’s got it.

Regards

Tim