# Array of functions PyROOT

ROOT version: 6.06.08/x86_64-slc6-gcc49-opt

Hi, I’m wondering how I can create an array of functions in python using root. I want to something like this:

for i in range(tothist.GetXaxis().GetNbins()):
temp1 = ROOT.TF1(“f1”,“function([0],[1],[2],[3]”,-40,40)
temp1.SetParameter(0,a)
temp1.SetParameter(1,b)
temp1.SetParameter(2,c)
temp1.SetParameter(3,tothist.GetBinContent(i))
array[i] = temp1

How can this be done? Using numpy, it wont accept as it is not a number nor a string.

``````array = []
for i in range(tothist.GetXaxis().GetNbins()):
array.append(ROOT.TF1(“f1”,“function([0],[1],[2],[3])”,-40,40))
array[-1].SetParameter(0, a)
...
``````

It helped, thanks

Do you also know an easy way to compute the sum of this array such that I get a TF1 function like

Totalfunction = sum(array)

?

You may try a list comprehension:

``````def total_f(x): return sum([ f(x) for f in array ])
``````

or something similar to that.

That does seem to return a function, however I cant draw it, it says:
‘function’ object has no attribute ‘Draw’

It seems it is no longer a TF1 object
Any suggestions?

Any reason why you need one function per point? Why not make a single function that iterates on the whole vector if you want the sum of all values at the end? I don’t know an easy way to create a TF1 out of a vector of TF1s.

Actually I only need the sum of the functions to get one function, so that might be a better suggestion. I’m new to programming, so I figured the other way would be the easiest to accomplish this. However I’m not quite sure how this needs to be done.

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