Best way to instantiate a TH1D from an array of doubles?

I have a sequence of doubles (a numpy.array, in this case) and I would like to fit a histogram using ROOT.

h = ROOT.TH1D(d) doesn’t seem to work. I have tried using an array.array(‘d’, seq), too, but it just doesn’t seem to want to be automatically cast to a TVectorD.

Additionally, to make a TVectorD, I can’t see a nicer way of doing it other than:

    root_data = ROOT.TVectorD(len(a))
    for index in range(len(a)):
        root_data[index] = a[index]

What am I missing here?

Thanks

Alex

Hi,

the TH1D ctor that takes an C-style array of doubles (as are the array.array and numpy representations) is:TH1D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins);a name, title, and number of bins are needed in addition to the array argument.

Likewise, the TVectorD ctor you’re looking for is of the shape:TVectorT(Int_t n,const Element *elements);so it would be ROOT.TVectorD( len(a), a ).

Cheers,
Wim