TH2F::Fit

Hello!

I have a problem with TH2::Fit function.I am trying to fit a 2D histogram wtih a TF2 function.I call the TF2::Fit(“f2”) function several times in my source.I made an iteration of fitting several 2D histograms and when I run it I almost always got this message:

Warning in TH2F::Fit: Abnormal termination of minimization.

When sometimes the Root manages to fit the histos and gives back the parameters they are pretty good.Then I give those values for the initial parameters for the next fit in order to help the fitting rutin but after fitting the first histo the next fit in the loop always gives that message above and
does not even change the initial values at all - even if I give zeros to them:

FCN=0 FROM MIGRAD STATUS=CONVERGED 231 CALLS 232 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+00 1.41421e+00 0.00000e+00 0.00000e+00
2 p1 -1.00000e-07 1.41421e+00 0.00000e+00 0.00000e+00
3 p2 1.00000e-04 1.41421e+00 0.00000e+00 0.00000e+00
4 p3 6.55000e+03 1.41421e+00 0.00000e+00 0.00000e+00
5 p4 1.00000e+01 1.41421e+00 0.00000e+00 0.00000e+00

So why doesn’t the Root do anything at all to get the least chisquares in this TH2 case if I try to fit several histos in a loop using the same TF2 function and initial values, and why does the Root fit everything well when I fit only a single TH2F histo?

I have ROOT 4.00/06 under windows xp and cygwin.

(I use an array of TH2Fs and I made a class to handle the histos and
the energy ranges that is why h->getHisto(j) is there…)

{

TF2 * f2 = (TF2*)gROOT->FindObject(“f2”);
if(f2)delete f2;
f2 = new TF2(“f2”,"(1/[4])*exp(-(x-([1]yy+[2]y+[3]))(x-([1]yy+[2]*y+[3]))0.5/([4][4]))",0,10000,0,10000);

for(int j = 0;j<n;j++){

  p1 = -1e-07;
  p2 = 1e-04;
  p3 = position[j];
  p4 = 10;
        
  f2->SetParameter(1,p1);
  f2->SetParameter(2,p2);
  f2->SetParameter(3,p3);
  f2->SetParameter(4,p4);

while(1) {
input >> t >> t_abs >> energy;
if (!input.good()) break;
if((h->GetEmin(j) <= energy) && (h->GetEmax(j) >= energy)){
h->fill(j,energy,t);
}
}

 cout << "Fitting #" << j+1 << "..." << endl; 
  
  (h->getHisto(j))->Fit("f2");


}

Could you send the shortest possible running script (and a data file)
reproducing the problem?

Rene

Hello!

Here is the source file and a data file.

Thank you in advance,

            Balint

National Institute for Standards and Technology
Gaithersburg,USA
twodfit.C (5.9 KB)
twodfit.C (5.9 KB)

Sorry,the data file is here!!!

Thanx!

Balint

I have some problems with attaching the data file.
Sorry for that!

Balint

So,one more try again…

Okey,so my file is a plain text file,but the extension was “.tim” and this
posting.php didn’t like it so I renamed to “filename.txt”…

Sorry again!

Balint
calib2_2004_7_20_17_22_27_2.txt (324 KB)

Hello!

One more thing.I changed the default fitting method to LogLikelihood
and now it works sometimes.But it only works at about the 30% of
runnings.I run it in the CINT.

Thanx,
Balint

Hi,

Please post the numbers asked after doing

.L twodfit.C
twodfit(“file.txt”)

eddy

Hello,

All right,I forgot to give you some parameters.So this whole program is about an x-ray detector.But the problem is that the energy-calibration of the detector - for some reason - changes in time,basically the gaussian peaks get slightly shifted to the lower energies in time.If I can fit the energy-time-intensity TH2 histogram with a gaussian distribution with changing position in time I can recorrect my shifted data and put them to the original energy bins.

The first number for that it asks is how many 2d gaussian peaks you want to fit.You can try here a number of three (3):

of peaks: 3

Then you have to give the energy range and the approx. positions of the 3 peaks Give them:

Range #1(min): 940
940 ------> These are just tests.I made it write out the input to check them.
Range #1(max): 1020
1020
Position #1: 980
Range #2(min): 3840
3840
Range #2(max): 3940
3940
Position #2: 3885
Range #3(min): 6450
6450
Range #3(max): 6620
6620
Position #3: 6550
Making histos…

Then you have to confirm to run the fitting loop:

Proceed (y/n)? : y

This is it.I attached a script with which you can take a look into the data
and choose other arbitrary peaks.This draws out the 2d energy-time histo and the 1d energy histo.( think it draws them out on each other.)

Thank you in advance,

             Balint

DRAW.C (1.02 KB)

Hello!

I found serious errors in my code.I didn’t initialize any variables in my class TwoDHisto which means that after some running in the CINT the pointer of TH2F could point to some unwanted location in the memory.
I am not sure wether it influences the problem wiht TH2F::Fit but I am affraid it does!

Thanx in advance,

Balint

Hi,

So,let me copy a total output of my fit of four peaks when running the program:

root [0] .x twodfit_m.C("/home/ebit/balint/data/calib4_2004_7_21_0_50_15_2.tim$

HOW MANY PEAKS?

of peaks: 4

RANGES
n=4
Memomry allocated!
Range #1(min): 3820
3820
Range #1(max): 3939
3939
Position #1: 3887
Range #2(min): 5000
5000
Range #2(max): 5140
5140
Position #2: 5075
Range #3(min): 940
940
Range #3(max): 1010
1010
Position #3: 975
Range #4(min): 6490
6490
Range #4(max): 6610
6610
Position #4: 6545
Making histos…
Proceed (y/n)? : y
Proceeding…
j = 0
Histos filled!
Fitting #1
FCN=3728.26 FROM MIGRAD STATUS=CONVERGED 421 CALLS 422 TOTAL
EDM=2.22479e-07 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 10
0.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+00 1.41421e+00 -0.00000e+00 0.00000e+00
2 p1 1.00000e-07 1.74895e-07 -2.65658e-03** at limit **
3 p2 1.00000e-04 1.86433e-04 1.12682e-02** at limit **
4 p3 3.88364e+03 5.77784e-01 -1.50046e-04 -3.89152e-03
5 p4 1.02610e+01 4.08839e-01 1.53233e-05 -6.54297e-02
6 p5 1.25669e-01 7.08060e-03 -1.61887e-06 -6.96449e-01
Fitting #1ready!
Chisquared = 1.0238
j = 1
Histos filled!
Fitting #2
FCN=0.00028 FROM MIGRAD STATUS=CONVERGED 376 CALLS 377 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per ce
nt
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+00 1.41421e+00 -0.00000e+00 0.00000e+00
2 p1 1.00102e-08 1.53365e-07 3.28224e-07 0.00000e+00
3 p2 1.00007e-05 1.53361e-04 3.19437e-08 0.00000e+00
4 p3 5.07500e+03 1.49408e+01 1.63398e-06 0.00000e+00
5 p4 1.04584e+01 1.15800e+02 9.38670e-04 0.00000e+00
6 p5 2.63075e-11 7.11014e+02 1.65410e-02** at limit **
Fitting #2ready!
Chisquared = -0
j = 2
Histos filled!
Fitting #3
FCN=0.00014 FROM MIGRAD STATUS=CONVERGED 409 CALLS 410 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+00 1.41421e+00 -0.00000e+00 0.00000e+00
2 p1 -8.10003e-08 1.63147e-07 5.60339e-05 0.00000e+00
3 p2 9.84353e-05 1.50245e-04 -6.11122e-06 0.00000e+00
4 p3 9.82227e+02 1.03633e+01 4.25555e-04 0.00000e+00
5 p4 1.74361e+02 1.64404e+02 1.18256e-02 0.00000e+00
6 p5 1.30127e-08 7.11016e+02 -9.08481e-05** at limit **
Fitting #3ready!
Chisquared = -0
j = 3
Histos filled!
Fitting #4
FCN=0.00024 FROM MIGRAD STATUS=CONVERGED 384 CALLS 385 TOTAL
EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+00 1.41421e+00 -0.00000e+00 0.00000e+00
2 p1 -5.39656e-08 1.18804e-07 4.85871e-03 0.00000e+00
3 p2 7.34700e-08 1.49419e-04 7.23179e-04 0.00000e+00
4 p3 6.53568e+03 1.57241e+01 1.37185e-02 0.00000e+00
5 p4 1.96308e+02 1.53941e+02 -1.73316e-01 0.00000e+00
6 p5 1.08654e-09 7.11015e+02 -3.65484e-04** at limit **
Fitting #4ready!
Chisquared = -0

What I see is that the first derivative of the fit is zero except the in the first case.So does it mean that the fit found a local minimum at each case?
Or what does the ‘first derivative’ means in the case of LogLikelihood method?And what does that mean on the case of least squares?
Is this an information on the numerical steps of iterations?

Thanx,
Balint

Hi

I compiled my code under cygwin and now I use in a way that
I wrote a shell script which runs it in a loop so basically it does what I
wanted but the loop is not in ROOT but in shell.

Anyway thanx!

balint