The type of a GetCovarianceMatrix() and GetMatrixArray() and how to get them in a multiple fitted functions case?

In my code, this part was working before in this form as stated below. After I change the code a bit in the 2nd version, I couldn’t figure out about the correct type for my objects. Thus, it doesn’t work properly.
Relevant part of my code:////////////////////////////////////////////////////
double* gaussParameters = new double[3] ;
gaussParameters[0] = p0;
gaussParameters[1] = p1;
gaussParameters[2] = p2;
auto r1 = h1->Fit(g1,“RS”);
// h1 is my histogram, so the type of r1 object should be the same as h1 in Theory.
auto covMatrix = r1->GetCovarianceMatrix();
// r1 is defined above, so the type of covMatrix object should be the same as h1 in Theory.

double GrossArea1 = g1->Integral( xVmin, xVmax) ; // g1 is a Gaussian function here.
double Error_GA1 = g1->IntegralError(xVmin, xVmax , gaussParameters, covMatrix.GetMatrixArray());
/////////////////////////////////////////////////////////////////////

On the other hand, the ROOT website type specifications are on this link:
image

In my new code:////////////////////////////////////////////////
I fitted total function with 2 Gaussians and 1 Pol2 on a histogram. Then, I wanted to get these 2 lines below without fitting gaussians explicitly like this as in the 1st code: auto r1 = h1->Fit(g1,“RS”);

void *r1= g1->GetParameters(); ???How should this part be?
void covMatrix1 = r1->GetCovarianceMatrix(); ???How should this part be?

//This integral initially does not cover the 100% of the area, only the one in the selected range

double GrossArea1 = g1->Integral( xVmin, xVmax) / HistoBinning ;// ConfidenceLimitCorrection; //virtual Double_t Integral (Double_t a, Double_t b, Double_t epsrel=1.e-12)

double Error_GA1 = g1->IntegralError(xVmin, xVmax , g1Parameters, covMatrix1.GetMatrixArray()) / HistoBinning;


ROOT: TVirtualFitter Class Reference (cern.ch)

ROOT: TMatrixT< Element > Class Template Reference (cern.ch)

Double_t *r1 = g1->GetParameters();

See TF1::GetParameters()

TMatrixDSym covMatrix1 = r1->GetCovarianceMatrix();

See: TFitResult:: GetCovarianceMatrix()

Error: non class,struct,union object r1 used with . or → *** Interpreter error recovered ***

///////////////////////////////////////
double *r1= g1->GetParameters();
TMatrixDSym covMatrix1 = r1.GetCovarianceMatrix();

double *r2=g2->GetParameters();
TMatrixDSym covMatrix2 = r2.GetCovarianceMatrix();

double *r3= bg->GetParameters();
TMatrixDSym covMatrix3 = r3.GetCovarianceMatrix();

double GrossArea1 = g1->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA1 = g1->IntegralError(xVmin, xVmax , g1Parameters, covMatrix1.GetMatrixArray()) / HistoBinning;
cout <<"1st peak’s " << "Integral: "<< GrossArea1 << " ± "<< Error_GA1 << endl;

double GrossArea2= g2->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA2 = g2->IntegralError(xVmin, xVmax , g2Parameters, covMatrix2.GetMatrixArray()) / HistoBinning;
cout <<"2nd peak’s " << "Integral: "<< GrossArea2 << " ± "<< Error_GA2 << endl;

double GrossArea3 = bg->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA3 = bg->IntegralError(xVmin, xVmax , bgParameters, covMatrix3.GetMatrixArray()) / HistoBinning;
cout <<"Background’s " << "Integral: "<< GrossArea3 << " ± "<< Error_GA3 << endl;

If bg is a pol2 background function. Does the covariance matrix exist for it like the way in the Gaussian function?

I fixed that part by changing the code like that:
Reference: Fitting histograms - ROOT

TFitResultPtr r1 = hist->Fit(g1,“S”, “same”, p1 - 3p2 , p1 + 3p2);
// Access the covariance matrix.
TMatrixDSym covMatrix1 = r1->GetCovarianceMatrix();
//////////////////////////////////////////////
TFitResultPtr r2 = hist->Fit(g2,“S”, “same”, p4- 3p5, p4 + 3p5);
TMatrixDSym covMatrix2 = r2->GetCovarianceMatrix();
///////////////////////////////////////////////////////
TFitResultPtr r3 = hist->Fit(bg,“S”, “same”, xl, xr);
TMatrixDSym covMatrix3 = r3->GetCovarianceMatrix();

However, I was trying to avoid the fit gaussians and pol2 separately like this. I was fitting the total function, and then retrieve the rest from this total function by setting and recalling the parameters.
When I fit things separately, it does not give me as nice results as the other one. Maybe, I should have fixed the parameters. I keep playing with the code.
FindFittingValues.cpp (7.6 KB)

Co60.txt (3.5 KB)

Please, comment in and out either line 108 or 174. Line 174 seems couldn’t get or set the parameters correctly.


when line 108 was commented out, and 174 was commented in.

when line 174 was commented out, and 108 was commented in.

In addition, if I don’t do SetRange(…) to a histogram, it does not fit properly at all. It gives nothing.meaningful.

New Code:FindFittingValues.cpp (7.2 KB)
Co60.txt (3.5 KB)

When I comment the lines related to pol2 background, the drawings and pol2 fitting do not work.
Please, comment in and out these lines: 132, 136, 141, optional: 170, 171, 172

Pol2 seems have parameters issue , or what?

Your last post is not dealing with the original title/question.
Therefore, it will not be found by other users when searching their issue.

Now back to your several hundred line script.

First of all there are several programming/syntax issues. You include non-existent header
files (you do not notice that because you use the interpreter), refer to not defined variables. I could not run your script.

You are asking why in plot 1 the background does not seem to follow the data. Actually in
both plots the Gaussians are also not correct.

The issue is simple, at the start you make the correct fit by defining a function that is
the sum of the background and two Gaussians. You plot the individual functions using
the fit parameters.
Then later on, you fit each function individually, this goes wrong in particular for
the polynomial background.

Attached the corrected script FindFittingValues.C (5.0 KB) and plot

In your text you ask the following:

If bg is a pol2 background function. Does the covariance matrix exist for it like the way in the Gaussian function?

I am not sure I understand, but the only correct covariance matrix of your fit is the one where you fit all the functions simultaneously.

Thanks, I can update the title. Sorry, questions led other questions.

I notice that, but I have ignored since it was working so far. I know I should have run the code with .cpp++ edition to compile and the run. My bad.

I stated the lines to look at, and I thought it might be better to see the whole picture. Sorry for the time consumption I caused.

I see now, it is the matrix constructed for the fit with all functions included. Thus, I guess the last part of the code was wrong because I defined 3 different separate covmatrix and r objects. It should be only one constructed from the fit by using total function.

WRONG PART is also this one, right??

TFitResultPtr r1 = hist->Fit(g1,“LS”, “same”, p1-2p2, p1+2p2);
TFitResultPtr r2 = hist->Fit(g2,“LS”, “same”, p4-2p5, p4+2p5);
TFitResultPtr r3 = hist->Fit(bg,“LS”, “same”, p1-4p2, p4+4p5);

// Access the covariance matrix.

TMatrixDSym covMatrix1 = r1->GetCovarianceMatrix();
TMatrixDSym covMatrix2 = r2->GetCovarianceMatrix();
TMatrixDSym covMatrix3 = r3->GetCovarianceMatrix();

double GrossArea1 = g1->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA1 = g1->IntegralError(xVmin, xVmax , g1Parameters, covMatrix1.GetMatrixArray()) / HistoBinning;
cout <<"1st peak’s " << "Integral: "<< GrossArea1 << " ± "<< Error_GA1 << endl;

double GrossArea2= g2->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA2 = g2->IntegralError(xVmin, xVmax , g2Parameters, covMatrix2.GetMatrixArray()) / HistoBinning;
cout <<"2nd peak’s " << "Integral: "<< GrossArea2 << " ± "<< Error_GA2 << endl;

double GrossArea3 = bg->Integral( xVmin, xVmax) / HistoBinning ;
double Error_GA3 = bg->IntegralError(xVmin, xVmax , bgParameters, covMatrix3.GetMatrixArray()) / HistoBinning;
cout <<"Background’s " << "Integral: "<< GrossArea3 << " ± "<< Error_GA3 << endl;

STILL LEARNING…

TFitResultPtr r_total = h1->Fit(total, "LRS");
TMatrixD c_total = r_total->GetCovarianceMatrix();
TMatrixD c_g1 = c_total.GetSub(0, 2, 0, 2); // gausn(0)
TMatrixD c_g2 = c_total.GetSub(3, 5, 3, 5); // gausn(3)
TMatrixD c_bg = c_total.GetSub(6, 8, 6, 8); // pol2(6) or "broken line" (6)
Double_t *p_total = total->GetParameters();
Double_t *p_g1 = p_total + 0; // gausn(0)
Double_t *p_g2 = p_total + 3; // gausn(3)
Double_t *p_bg = p_total + 6; // pol2(6) or "broken line" (6)

Then, e.g., for the “bg” function:

cout << bg->Integral(xVmin, xVmax, p_bg) << endl;
cout << bg->IntegralError(xVmin, xVmax, p_bg, c_bg.GetMatrixArray()) << endl;

BTW. Looking at the “total” fit plot, it seems to me that you would get a better result with a “broken line” background (see this old post), instead of a “pol2” background.

I have been checking how to construct the covariance matrix and so on in detail. Also, I need to check how ROOT takes care of it in simple case and in more complicated cases. The number of function I add into my total function, which I use to fit on my data, will affect the size of the rows and columns in the Co-variance matrix. I think you guys solve it with this GetSub(…) routine.

Everything I plan to write by myself was written already in the ROOT, but it takes ages for me to figure out how it was written by somebody else in an advance way.

Actually, the real story was that I have forgotten how to get an integral’s error from a function. Now, it came up, and the function the ROOT created for this purpose asks me to put covariance matrix as an input variable. That gave me a hard time :slight_smile:

I think you use this method:


I couldn’t guess, I’ll say, what lwb and upb were meant? row and column of what??? What do they refer?

See the description of parameters of the “template TMatrixTBase< Element > & TMatrixT< Element >::GetSub” method.

For the full documentation, see:

ROOT Manual → Functional parts → Mathematical libraries → Linear algebra packages → Matrix package

ROOT Reference Documentation → Functional Parts → Math → Matrix Linear Algebra

I see, thanks.

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