What’s wrong with the following code?

```
f = TFile.Open("myfile.root","r")
h = TH1F("h1","h1",10,0,100)
f.Draw("Path/With/Multiple/Dirs/histo >> h")
print h.GetEntries() // output = 0.0
```

What’s wrong with the following code?

```
f = TFile.Open("myfile.root","r")
h = TH1F("h1","h1",10,0,100)
f.Draw("Path/With/Multiple/Dirs/histo >> h")
print h.GetEntries() // output = 0.0
```

Hi,

what are you exactly trying to do?

Cheers,

D

Hi,

Eventually I want to loop over files to recover histograms, and plot them on the same canvas. In particular, I need to setup a bunch of drawing options and binning. Is there an easier way?

Cheers,

D.

I guess the question is really: is the problem with `Draw("histo >> h")`

or with accessing `histo`

via its TDirectory path?

Neither, I’m not exactly sure what `TFile.Draw`

does, but if you just want to get a histogram from a file, you can do:

```
f = TFile.Open("myfile.root","r")
h = f.Get("Path/With/Multiple/Dirs/histo")
print h.GetEntries()
```

1 Like

Hi Graipher,

Thanks for that! How can I then set the number of bins, xmin and xmax?

You should have already done that when creating the histogram in the first place.

Alternatively, you can `Rebin`

a histogram, and constrain its xlimits (but you can only make the binning coarser and the limits smaller of course).

However, with `Rebin`

you only have a choice between specifying how many neighboring bins should be joined (e.g. `h.Rebin(2)`

will halve the number of bins) or explicitly specifying the new bin edges:

```
import numpy as np
xbins = np.linspace(0, 100, num=11)
h_new = h.Rebin(len(xbins) - 1, "hnew", xbins)
```

NOTE: The bin edges specified in xbins should correspond to bin edges in the original histogram. If a bin edge in the new histogram is in the middle of a bin in the original histogram, all entries in the split bin in the original histogram will be transfered to the lower of the two possible bins in the new histogram. This is probably not what you want.

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