Problem reading data & using TLinearFitter

I slightly adapted the tutorial for TLinearFitter, so that it would read data from a file. Unfortunately, this seems to lead to problems. The macro below boils down the problem. If one runs the without arguments, it generates random data, writes data to a file (data.txt), and then fits it fine. If you then run it with as test(kFALSE), it will read the data from the file and fit it - and fail. :frowning: There is a commented section that dumps the data points to the screen to verify they are the same in both cases.

Cheers,
Chris

#include <TRandom.h>
#include <TLinearFitter.h>
#include
#include
#include
using namespace std;

void test( Bool_t random=kTRUE ) {

const Int_t n=100;

Double_t x[n102];
Double_t y[n10];
Double_t e[n
10];

if ( random ) {
TRandom rand;
ofstream outfile(“data.txt”);
for (Int_t i=0; i<n; i++){
x[0 + i2] = rand.Uniform(-10, 10);
x[1 + i
2] = rand.Uniform(-10, 10);
e[i] = 0.01;
y[i] = 4x[0+i2] + x[1+i2] + rand.Gaus()e[i];
outfile << setw(20) << x[0+i
2]
<< setw(20) << x[1+i
2]
<< setw(20) << y[i] << endl;
}
}
else {
ifstream infile(“data.txt”);
for (Int_t i=0; i<n; i++){
infile >> x[0+i2]
>> x[1+i
2]
>> y[i];
infile.ignore(8192,’\n’);
}
}

// for (Int_t i=0; i<n; i++){
// cout << setw(20) << x[0+i2]
// << setw(20) << x[1+i
2]
// << setw(20) << y[i] << endl;
// }

TLinearFitter lf(2);
lf.SetFormula(“1 ++ x0 ++ x1 ++ x0x0 ++ x0x1 ++ x1*x1”);
lf.AssignData(n, 2, x, y, e);

lf.Eval();
for (Int_t i=0; i<lf.GetNumberFreeParameters(); i++)
printf(“par[%d]=%f±%f\n”, i, lf.GetParameter(i),
lf.GetParError(i));
Double_t chisquare=lf.GetChisquare();
printf(“chisquare=%f\n”, chisquare);
}

Hi, Chris,

You don’t write your array e into the file, so when you run your test(kFALSE) function, it’s not initialised and that’s why the linear fitter crashes.

Cheers,
Anna

Hi Anna,

Thanks for catching my foolish mistake. I fixed that by changing

lf.AssignData(n, 2, x, y, e);

to

lf.AssignData(n, 2, x, y);

since I’m not interested in weights for this test case. Now, it works fine - but if I change

lf.Eval();

to

lf.EvalRobust();

I get a crash! Can you reproduce this?

Thanks,
Chris