Adding TF1s in pyROOT

In pyROOT how do you add two TF1s together?

I have a user defined function:

def TopHatFn(x, par):
    if x[0] < par[0] or x[0] > par[1]:
        return 0
    elif x[0] >= par[0] and x[0] <= par[1]:
        return 1

I make two TF1s, both in the same way, but with different names:

tophata = TF1('tophata',TopHatFn, xmin, xmax, 2);
tophata.SetParameter(0, 3);
tophata.SetParameter(1, 7);

Then I add them:

 tophat = TF1('tophat','tophata + tophatb', xmin, xmax)

gives the error message

Error in <TFormula::Compile>:  Bad numerical expression : "tophata"
Error in <TF1::TF1>: function: tophat/tophata + tophatb has 0 parameters instead of 1

What’s the correct way to do this?

Hi,

adding the TF1s by name when they are defined with a functor is not possible.
The correct way would be to act at python function level directly and then define a TF1 based on the “sum function”.

Cheers,
D

Hi,

Ok. I originally made the following function to make N top hats:

def AcceptanceIntervals(x, par):
    for tpl, tph in zip( par[::2], par[1::2] ):
        if x[0] <= tph and x[0] >= tpl:
            return 1
    else:
        return 0

But this function didn’t work. Running it gave the error:

  File "makeTopHats.py", line 23, in AcceptanceIntervals
    for tpl, tph in zip( par[::2], par[1::2] ):
SystemError: error return without exception set

So the zip and :: operations dont seem to work in these functions. I tried the following code and it worked

def AcceptanceIntervals(x, par):
    for i in range(0, len(par), 2):
        if x[0] > par[i] and x[0] < par[i+1]:
            return 1
	elif x[0] == par[i] or x[0] == par[i+1]:
            return 1/2
    else:
        return 0

The problem is now solved.