Creating a few TF1s With Class function

I’ve been trying to create an array of TF1 objects within a method of a given class, using a function defined within the class itself, but haven’t had much success. (using http://root.cern.ch/root/htmldoc/TF1.html#F5 ).

The analysis class is called AliV0Module, and here is the code I’m attempting to use:

    TF1 *lfitNoise[50];
    TF1 *lSampleNoise[50];
    char lFitNameOne[50];
    for(long i=0; i<fptbinnumb; i++){
      //Define Function to Fit Background 
      sprintf(lFitNameOne,"lfitNoise%i",i);
      cout<<"creating fitnoise, named "<<lFitNameOne<<endl;
      lfitNoise[i] = new TF1(lFitNameOne, this, &AliV0Module::myBgPol1, 
        RoundToThousandth ( lLeftBgLeftLimit[i]   ),
        RoundToThousandth ( lRightBgRightLimit[i] ), 4 , "AliV0Module", "myBgPol1");
      lfitNoise[i] -> FixParameter(2,RoundToThousandth ( lLeftBgRightLimit[i] ) );
      lfitNoise[i] -> FixParameter(3,RoundToThousandth ( lRightBgLeftLimit[i] ) );
      lfitNoise[i] -> SetParameter(0,lLeftPlusRightBgV0[i]*lHistoFullV0[i]->GetBinWidth(5));
      lfitNoise[i] -> SetParameter(1,0);
      sprintf(lFitNameOne,"lSampleNoise%i",i);
      
      //Define Function to Sample Background
      cout<<"creating sample "<<i<<endl;
      lSampleNoise[i] = new TF1(lFitNameOne, this, &AliV0Module::myBgPolToEval1, 
        RoundToThousandth ( lLeftBgLeftLimit[i]   ),
        RoundToThousandth ( lRightBgRightLimit[i] ), 2 ,"AliV0Module", "myBgPolToEval1");
    }    

When I try to, later, access the objects called “lfitNoise1”, “lfitNoise2” (note: lfitNoise0 works fine) and so on, I get the message:

Unknown function: lfitNoise1

Since I have a pointer, I can try to ask for lfitNoise[i]->GetName(), which returns gibberish… If I define the myBgPol1 and myBgPolToEval1 functions without the AliV0Module:: and use them as standalone C++ functions, it works just fine…

What am I doing wrong? Is it that I’m passing the “this” argument to the pointer? Could you please help me? Thanks a lot in advance!

HI,

Looking at the code, it should work fine. Are you compiling the code or using in CINT ? You should maybe try to compile if it is not the case.
Otherwise, please send me a running script so I can reproduce this problem

Lorenzo

Indeed, I’m not using it compiled - I’m running in CINT. I’ll try compiling the class and invoking it, compiled, from within a macro, and see if that improves things… Could also improve execution speed, of course, so it’s good in many ways :slight_smile:

It’s a relatively long piece of code that requires a few other files to run. If I can’t manage, I’ll send you a working example, but simplified, only reproducing the specific problem.

Many thanks for your help!
David

Thanks, I managed to make a compiled version work with the constructor declared as above… CINT didn’t like it but now it works! Incidentally, it’s over 3 times faster when compiled too (2m20s vs 40s execution!), so I’m really happy! :smiley: