Accessing Covariance Matrix from TFractionFitter

Hi all,

My question might be a simple one but I’m stumped. I’m using TFractionFitter to fit a histogram, hoping to get out a covariance matrix from the fit. I can see from the documentation that there is a function called GetFitter() that “Give direct access to the underlying fitter class.” From the “underlying fitter class” I should be able to access a FitResult and from FitResult I can use GetCovarianceMatrix().

My problem is that I can’t get the code to properly perform these steps and I can’t find an example online illustrating how to use TFractionFitter::GetFitter(). Could someone post a couple lines of code showing how to use TFractionFitter::GetFitter()?

Thanks

Hi,

You get directly the TFitResult as a returned object from TFractionFitter::Fit. Here is an example

auto r = tFractionFitter.Fit(); 
auto matrix = r->GetCovarianceMatrix();
matrix.Print();

Lorenzo

Perhaps I’m having an error with the fact that I used TFractionFitter* but it doesn’t seem to work. The compiler is identifying “r” as type “int”. When I run the following code, I get an error stating that r is not of type ‘pointer’ and when I change it to .GetCovarianceMatrix() I get an error stating that it is an int.

TFractionFitter* fit = new TFractionFitter(dataHist, MCTheory); // initialise auto r = fit->Fit(); auto matrix = r->GetCovarianceMatrix(); matrix.Print();

You are probably using an old version of ROOT (version 5). This works only in version 6.
For the older version you might try using

TVirtualFitter::GetFitter()->GetCovarianceMatrix();

Lorenzo

Thank you, that seemed to work!

Quick follow-up, how would one access the correlation coefficients matrix from TFractionFitter? I see a function called Correlation() in ROOT::Fit::FitResult and I suspect this is what I’m trying to call.

Hi,

If you are using ROOT 6, you can call also TFitResult::GetCorrelationMatrix()

TFractionFitter* fit = new TFractionFitter(dataHist, MCTheory);       // initialise
  auto r = fit->Fit();
  auto matrix = r->GetCorrelationMatrix();
  matrix.Print();

But there is no GetCorrelationMatrix from TVirtualFitter. But you can get the correlation matrix from the covariance

Lorenzo

I tried the following code:

cout << "Testing correlation matrix: " << TFitResult::GetCorrelationMatrix()[0][0] << endl;

and got the following error:
error: incomplete type ‘TFitResult’ used in nested name specifier

I’m using root version 6.08.

There is no static method TFitResult::GetCorrelationMatrix. You need to get the TFitResult as return object from the Fit method as shown in the code snipped in my previous post.

Lorenzo

Thanks for all the help Lorenzo. This seems to be working.

if you don’t mind, I have yet another follow-up question. How would one set initial parameters in the frac0 and frac1 fitting? It always seems to start at 0.5 and 0.5 and I’m getting a bunch of seg faults on some of my data sets. I suspect it is because the actual fractions should end up as ~0.95 and ~0.05 so it is causing seg faults. Weirdly enough, it doesn’t cause it on all my data sets but just a select few that don’t seem to be special at all.

Nevermind, I figured it out. The following code snippet starts the parameter search at the fractions set by “value”.

[code] TFractionFitter* fit = new TFractionFitter(dataHist, MCTheory, “V”); // initialise
TVirtualFitter* vfit = fit->GetFitter();
int fitMin = 10;
int fitMax = 85;
fit -> SetRangeX(fitMin, fitMax); // Set range in bin numbers

// Setting initial search parameters.
int ipar = 0;
char name[3] = “a”;
double value = 0.999;
double valueerr = 0.1;
double valuelow = 0;
double valuehigh = 1.5;
vfit->SetParameter(ipar, name, value, valueerr, valuelow, valuehigh);
[/code]