Defining template function, problems

Hi Guys

I have this problem, I am tying to define a function that is based on the values of previous array, one for x, and one for y = f(x). I’m using pyroot. I have defined my function trhough a c++ string

cpp_code = ''' 
// Template Mixing definition

double StepFunction(double x, double bins[], double Counts[], int arraysize){
    int index =0 ;
    for(int i = 0; i < arraysize; i++){
        if(x >= bins[i]){
            index = i;
        }
    }
    
    return Counts[index];
}

'''

ROOT.gInterpreter.ProcessLine(cpp_code)

This function takes x, the arrays and an int value. I am able to call this function with ROOT.StepFunction.
What I want to do is to define a TF1 function based on this StepFunction, using two arrays that I have. I have tried something like

WallAnnihilation = ROOT.TF1("WallAnnihilation", "[0]*StepFunction(x, arr1, arr2, 30)",0,4)

But at this point I receive an error because arr1 and arr2 are not “seen” by the program.

Thank you for your help

But

ROOT Version: 6.26/10
Platform: Ubuntu
Compiler: Not Provided

Dear @Adrianodelvincio ,

In your definition of the TF1 object, you are using objects that should be known by the ROOT C++ interpreter. You correctly defined the StepFunction so that the interpreter knows about it, but indeed the other variables (arr1, arr2) are probably some Python objects that are not directly accessible by the interpreter.

One way to make this work is defining arr1 and arr2 in some call to gInterpreter.Declare (so you can group the definition of the step function with the definition of the arrays).

Cheers,
Vincenzo

2 Likes

Yes you are right. I have almost a working code. My problem now is the following

cpp_definition = ''' 
//definition of the function and arrays
double StepFunction(double x, double bins[], double Counts[], int arraysize);

double bins[30] = {0.133, 0.267, 0.4, 0.533, 0.667, 0.8, 0.933, 
    1.067, 1.2, 1.333, 1.467, 1.6, 1.733, 1.867, 2.0, 2.133, 2.267, 
    2.4, 2.533, 2.667, 2.8, 2.933, 3.067, 3.2, 3.333, 3.467, 3.6, 
    3.733, 3.867, 4.0};
    
double MixingCount[30] = {0.007, 0.027, 0.047, 0.064, 0.082, 0.101, 0.126,
                        0.136, 0.185, 0.216, 0.256, 0.322, 0.348,
                        0.409, 0.397, 0.458, 0.479, 0.484, 0.504, 0.432, 0.382,
                        0.376, 0.353, 0.297, 0.267, 0.24, 0.206, 0.114, 0.088, 0.097};
                    
double Cosmic[30] = {0.017, 0.025, 0.021, 0.059, 0.038, 0.097, 
                    0.08, 0.109, 0.189, 0.155, 0.168, 0.185, 0.185, 0.256, 0.181, 
                    0.281, 0.269, 0.328, 0.302, 0.344, 0.378, 0.428, 0.433, 0.37, 
                    0.361, 0.475, 0.588, 0.395, 0.403, 0.382};

'''

This is how I define functions and arrays

When I call

WallAnnihilation = ROOT.TF1("WallAnnihilation", "[0]*StepFunction(x, bins, MixingCount,30)",0,4)

I receive the following error, quite misterious to me:

nput_line_93:2:100: warning: braces around scalar initializer [-Wbraced-scalar-init]
Double_t TFormula____id4815093105798894742(Double_t *x,Double_t *p){ return p[0]*StepFunction(x[0],{bins},{MixingCount},30) ; }

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