TypeError: bad argument type for built-in operation

Hello,

I’m having the same trouble as in
[url]Branch with array of variable length

My simplified code is :

    gROOT.ProcessLine(
        "struct MyStruct{\
         double b0mass[100];\
         };")
    from ROOT import MyStruct
    s = MyStruct()
    t = TTree('skim', 'Skimmed tree')
    t.Branch('b0mass', AddressOf(s,'b0mass'),'b0mass/D')
    print s.b0mass, type(s.b0mass)
    print s.b0mass[0] 
    #s.b0mass[0] = 12.2
    s.b0mass.__setitem__(0, 12.2)
    print s.b0mass[0] 

Without the comments, it runs OK with the following output:

<Double_t buffer, size 100> <type 'ROOT.PyDoubleBuffer'>                             |
0.0
12.2

But if I remove the comment line:

    s.b0mass[0] = 12.2
TypeError: bad argument type for built-in operation

Any suggestions?

Thanks!
Xin

Hi,

which version of ROOT (and which OS)? It works fine for me (SVN trunk on Linux).

Cheers,
Wim

Hi Wim,

I’m using ROOT 5.27/06b (5 November 2010) on lxplus (Linux lxplus427.cern.ch 2.6.18-274.12.1.el5 #1 SMP Wed Nov 30 08:57:58 CET 2011 x86_64 x86_64 x86_64 GNU/Linux )

If I have to use that version of ROOT, what would be the best way to store array of doubles in PyROOT?

Thanks!

Xin

Hi,

seen from the python-side, either an array from module array or any of the numpy editions would be best. What is the use? In particular, will the array pass onto the C++ side, and if yes, how?

Cheers,
Wim

Hi Wim,

Thanks for your reply! In my case, I just use PyROOT to write a TTree sturcture and no need to pass onto the C++ side. I will look into arrays…

Xin

Xin,

okay; note that an array can be used where C++ expects a double* and it can be used in a TTree as well, so that should all be fine. (See also here, section A2.2.)

Cheers,
Wim