Array of Float_t in python for ROOT

I’d like to be able to set bin endpoints in a python script accessing ROOT.

In a regular ROOT macro this would be osmething like,

Float_t bins_et[] = {15, 20, 27, 35, 45, 57, 72, 90, 120, 150, 200, 300, 400, 550}; Int_t num_bins_et = 13; TH1F *photonEt = new TH1F("photonEt", "Photon E_{T}", num_bins_et, bins_et);

For python, doing an import of Float_t does not work:

Traceback (most recent call last): File "plotFromMultipleFiles.py", line 12, in ? from ROOT import TFile, TH1F, TTree, Float_t ImportError: cannot import name Float_t

So where would I even begin?

Hi,

best thing would probably be to use python’s own module array (you could use the C++ equivalents, but I wouldn’t recommend it):

import array bins_et = array.array( 'f', [ 15, 20, 27, 35, 45, 57, 72, 90, 120, 150, 200, 300, 400, 550 ] ) num_bins_et = len(bins_et)
Where for num_bins_et I’m assuming the actual length (which is 14) is what is needed.

Cheers,
Wim

Thanks for the tip, I think the python code should really be something like:

[code]bins_et = array(‘f’, [15.0, 20.0, 27.0, 35.0, 45.0, 57.0, 72.0, 90.0, 120.0, 150.0, 200.0, 300.0, 400.0, 550.0])
bins_eta = array(‘f’, [-2.5, -1.55, -1.45, -0.9, 0.0, 0.9, 1.45, 1.55, 2.5])

etplot = TH1F(“photonEt”, “Photon E_{T} ;E_{T} (GeV)”, len(bins_et)-1 , bins_et )
etaplot = TH1F(“photonEta”, “Photon #eta ;#eta” , len(bins_eta)-1, bins_eta)[/code]

The “-1” is required since ROOT makes a bin between each given number in the array.

I used this in a python script to plot from multiple files with different scales,
http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/UserCode/RootMacros/plotFromMultipleFiles.py?view=markup
Very useful.