Creating Histogram Using ROOT from Python

_Hello, I am trying to create a histogram using Root from Python. I created the histogram in python by writing:
’ ’ ’
h1 = ROOT.TH1F(“h1”,” Some Title",500,0,5)

h1.Fill(theta)

’ ’ ’

I then created a TFile objects so that ROOT can access the file in which I created my histogram files.
I wrote
’ ’ ’
ThetaFile = ROOT.TFile(“Math.root”,“NEW”)

’ ’ ’

Then in the ROOT prompt, I wrote

root [0] TFile *hfile = new TFile(“Math.root”);

root[1] TH1F histog = (TH1F) hfile->Get(“h1”);

root[2] histog->Draw()

And I don’t get a histogram. Instead I get this warning:

Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
ROOT_prompt_3:1:1: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
histo->Draw()
^~~~~

When I read about creating histograms, it writes that a histogram plot window should appear, but I am not seeing that. What am I doing wrong? Do I need to install something to see the histogram?__
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Supposing the histogram was correctly filled and saved to the file (you can check by creating a TBrowser and opening -double clicking- the root file and then the histogram inside the file), try this:

TH1F *histog = (TH1F*) hfile->Get(“h1”);

I tried TBrowser b and I clicked on my .root file that supposedly had the histogram object. I just got this in root: 0x7ffff671ce90.
I did try your suggestion and I got the same error.
What I am trying to do exactly is this:

I am trying to create a histogram using Python and ROOT. I wrote my code in python. At the beginning of my code, I opened a file that contains several hundred lines of data and read the data in it and did some calculation on that file and called the result theta. Theta was calculated for each data line in the file. Now, I want to create a histogram with my pre-defined function theta. I create my histogram object using root and by writing

histo = ROOT.TH1F("histo", "My histogram", 10, 0, 100)

histo.SetDirectory(0) 

Then, I fill my histogram with my pre-defined function and draw the histogram

histo.Fill(theta)
histo.Draw()

However, this only gives me a blank histogram and only one entry in my histogram. I do not understand why since the above code is inside my for loop that calculated my theta for each line. Does anyone know how I can fix this? If I create a histogram through a user defined function (by using TF1), I just get the error that the object doesn’t have the attribute Fill.

Thanks

If you are trying to do Draw (and it fails) inside the same macro that fills the histo, then it probably is not being filled at all, or it’s being filled outside its (x-axis) range and all data is going to the under or overflow. In your first post you declared histo with limits 0 to 5, and now it’s 0 to 100, so what you are doing is not clear; it would be more helpful if you post all the relevant parts of your code. In any case, you can start by checking that you are actually reading the input data file (I suppose a text file); try printing out some values while it’s being read and check.

Ok, thank you. I will keep your suggestions in mind.

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