PyROOT : "Could not convert argument to buffer or nullptr"

Dear experts,

While playing with PyROOT to make a function that manually rebins histograms in a specific way, I discovered a behavior that I cannot explain.
I am sorry if the problem is obvious as I am not very experimented with ROOT in general.

Here is the relevant code:

import ROOT as RT
import numpy as np
import random

canvas = RT.TCanvas (" canvas ")

def merge_bins_flux(histogram,name,title):
    bins = np.concatenate([np.arange(0,2200,100),np.arange(2200,4000,200),np.arange(4000,5000,500),np.arange(6000,10000,1000)])
    new_hist = RT.TH1D(name,title,37,bins)
    print(new_hist.GetNbinsX())
    for i in range(1000):
        new_hist.AddBinContent(random.randint(0,37))
    return new_hist
h = merge_bins_flux("h","name","title")
h.Draw()
canvas.Print("deletable.pdf")

This code simply tries to create an histogram with specific binning and then it is filled with 1000 random numbers. (Please ignore the names, the function is not fulfilling it’s destined task at the moment)
Upon executing the code I get the following error:

Traceback (most recent call last):
  File "deletable.py", line 18, in <module>
    h = merge_bins_flux("h","name","title")
  File "deletable.py", line 12, in merge_bins_flux
    new_hist = RT.TH1D(name,title,37,bins)
TypeError: none of the 6 overloaded methods succeeded. Full details:
  TH1D::TH1D(const TH1D& h1d) =>
    TypeError: takes at most 1 arguments (4 given)
  TH1D::TH1D(const TVectorT<double>& v) =>
    TypeError: takes at most 1 arguments (4 given)
  TH1D::TH1D() =>
    TypeError: takes at most 0 arguments (4 given)
  TH1D::TH1D(const char* name, const char* title, int nbinsx, double xlow, double xup) =>
    TypeError: takes at least 5 arguments (4 given)
  TH1D::TH1D(const char* name, const char* title, int nbinsx, const float* xbins) =>
    TypeError: could not convert argument 4 (could not convert argument to buffer or nullptr)
  TH1D::TH1D(const char* name, const char* title, int nbinsx, const double* xbins) =>
    TypeError: could not convert argument 4 (could not convert argument to buffer or nullptr)

I was looking to use the following signature: TH1D::TH1D(const char* name, const char* title, int nbinsx, const double* xbins).
As I understand it, there are troubles in converting the numpy array I give as an argument into an array that ROOT can use.

However, I discovered this by making a mistake, there is a way to make the code above not throw any error and actually output the histogram to the pdf correctly.
By changing the second “np.arange” to “np.linspace” in my concatenation of arrays, the code executes without a problem eventhough the number of bins I indicate is not correct (37 when it should now be 228).

So my question would be: How come one case works while the other throws an error? Aren’t both arrays simple numpy arrays filled with floats?
Am I doing something wrong when writing this code? What should I do instead?

Thank you very much for your time and any help you can provide,
Théo

Hello,

The underlying data type of bins is int64, while the constructor of TH1D expects either a float array or a double array. If you pass a numpy array of floats or doubles, it should work.

1 Like

Hi,

Thank you very much for your answer! Changing np.arange to np.linspace made the content of the array float values and this made the whole array of float type upon concatenation which is why it worked in that case.

Changing the array to float type solved the problem! Thank you!

Cheers,
Théo

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