Trouble cloning histogram fitted with cloned function with latex style parameter names

Hi all,

I’ve having trouble cloning a histogram that has been fitted with a cloned function with latex style parameter names.

To be more specific, I have a template function which I clone and use to fit a histogram. I then want to clone that histogram and do things with it in a separate macro. This works fine unless the original function has parameter names that are defined in a latex style. See below for the error that appears.

Error in <TFormula::Streamer>: number of parameters computed (6) is not same as the stored parameters (5)
         templateFunc : [0]*cos([1]*x) + [2]*sin([3]*x) + [4] Ndim= 1, Npar= 6, Number= 0
 Formula expression:
	[a_a]*cos([b]*x)+[c]*sin([d]*x)+[e]
List of  Variables:
Var   0                    x =    0.000000
List of  Parameters:
Par   0                  a_a =    0.000000
Par   1                a_{a} =    0.000000
Par   2                    b =    0.000000
Par   3                    c =    0.000000
Par   4                    d =    0.000000
Par   5                    e =    0.000000
Expression passed to Cling:
	Double_t TFormula____id5811689462910226460(Double_t *x,Double_t *p){ return p[0]*TMath::Cos(p[2]*x[0])+p[3]*TMath::Sin(p[4]*x[0])+p[5] ; }
Warning in <TFormula::Streamer>: number of parameters list found (6) is not same as the stored one (5) - use re-created list
(int) 1

Depending on what I do afterwards with the cloned histogram, displays can be off or things can go on to segfault.

See here for a simplified macro that reproduces the problem.

#include <TF1.h>
#include <TH1.h>
#include <TCanvas.h>

int cloneHistWithParNames(){

	auto myCanv1 = new TCanvas("myCanv1","myCanv1",200,10,1200,800);


	TF1* templateFunc = new TF1("templateFunc", "[0]*cos([1]*x) + [2]*sin([3]*x) + [4]", 0, 10);
	templateFunc->SetParameters(2,1,2,2,20);
	templateFunc->SetLineColor(2);

	// templateFunc->SetParNames("a", "b", "c", "d", "e"); // <--- works
	templateFunc->SetParNames("a_{a}", "b", "c", "d", "e"); // <--- does not work

	TF1* funcClone = (TF1*) templateFunc->Clone("cloneFunc");

    int nPts = 100000;

    TH1F* myHist = new TH1F("myHist", "myHist", 100, 0, 10);
	myHist->FillRandom("templateFunc", nPts);

	funcClone->SetParameter(4, myHist->Integral("WIDTH")*.1);
	myHist->Fit(funcClone);

    myHist->Draw();

	auto myCanv2 = new TCanvas("myCanv2","myCanv2",200,10,1200,800);

    TH1F* histClone = (TH1F*) myHist->Clone("cloneHist");
	histClone->Draw();


return 1;

}

Am I doing something wrong? Or is this a bug that I should report? Thanks!

Hi,

The problem are the latex characters in the parameter names, which in some case can confuse the TFormula parsing (especially using the “{” brackets).
We can try to improve this, but for the time being a possible workaround is to use the copy-constructor of TF1 instead of Clone. This should avoid re-parsing the formula expression

Best Regards

Lorenzo

Thanks Lorenzo, by copying the function and making sure not to clone the histogram I can get around this issue. (Anything else still seems to fail.)

I should also maybe mention that I don’t see this issue with TGraph. Whether that might help in solving the problem I’m not sure.

Thanks again, Nick

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