RE: [ROOT] multiple TMinuit fits

From: Philippe Canal (pcanal@fnal.gov)
Date: Thu Dec 20 2001 - 19:54:41 MET


Hi Ben,

If you create and delete TMinuit object, you need to make sure to
maintain properly the gMinuit pointer.  Because other object might
rely on gMinuit to be set, you need to have:

  TMinuit *old = gMinuit;
  for (int i=0; i < 2; i++) {
     TMinuit *minuit = new TMinuit(2); 
     .... 
     delete minuit;
  }
  gMinuit = old;

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Ben Kilminster
Sent: Thursday, December 20, 2001 12:05 PM
To: roottalk@pcroot.cern.ch
Subject: [ROOT] multiple TMinuit fits


Hi,

I have a function which has a loop containing minuit fits to a user
defined function. After the loop, TH1F::Fit() is called. When I execute
the function twice in a row, it segmentation faults at the TH1F::Fit()
line.   

The problem may be related to the fact that I delete the instance of 
TMinuit after each use (I do this to guarantee that in each iteration
minuit does not retain any information) - but this doesn't explain why the
function works the first time.

The problem is recreated in the folowing code when the following is run:

.L testHFit.C
testH()   // runs okay
testH()   // segmentation faults at h->Fit("gaus")


//-------------------  FILE:  testHFit.C ------------------

#include "TMinuit.h"
#include "TH1.h"

double value, MyX, MyY;

Double_t func(double x,double y,Double_t *par) {
  value  = par[1]*(par[0]*x + (1.-par[0])*y);
  return(value);
}

void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t
iflag) {
  f = func(MyX,MyY,par);
}

void testH () {

  for (int i=0; i < 2; i++) {
     TMinuit *minuit = new TMinuit(2); 
     minuit->SetFCN(fcn);
     Double_t arglist[2];
     Int_t ierflg = 0;
     arglist[0] = 1;
     minuit->mnexcm("SET ERR", arglist ,1,ierflg);
     Double_t vstart[2] = {0.5, 10};
     Double_t step[2] = {0.01, 1.0};
     minuit->mnparm(0, "Frac", vstart[0], step[0], 0,0,ierflg);
     minuit->mnparm(1, "Normaliz", vstart[1], step[1], 0,0,ierflg);
     arglist[0] = 6000;
     arglist[1] = 1.;
     MyX = 0.3;
     MyY = 0.7; 
     minuit->mnexcm("MIGRAD", arglist ,2,ierflg);
     minuit->Clear();
     delete minuit;
  }

TH1F *h = new TH1F("h","test",10,0,10);
h->Fill(1);h->Fill(2);h->Fill(3);h->Fill(4);h->Fill(3);

// seg faults here the second time that testH() is run.
h->Fit("gaus");

}

//------------------- END FILE:  testHFit.C ------------------



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:13 MET