Fitting problem

Hi, I attach a simple code here to test user-defined function. I define g1 as a gaussian function and define g2 based on g1. Thus, if I define g2=g1*1.0, I cannot get the right fitting plot, however, if I define g2=g1 I can. Anybody could tell my problem?

Thanks,
Zhiyi.

fitErf(){
TH1F *h = new TH1F(“h”,“gauss”, 100, -3, 3);
Int_t i;
for ( i=0; i<1000; i++)
h->Fill(gRandom->Gaus(0,1));
h->Draw();
TF1 *g1 = new TF1(“g1”,“gaus”,-3,3);
TF1 *g2 = new TF1(“g2”," g1 * 1.0 “,-3,3); // fitting plot is a straight line
//TF1 *g2 = new TF1(“g2”,” g1 ",-3,3); // fitting plot is right
h->Fit( “g2” );
}

You should give initial values to your parameters.

void fitErf(){ TH1F *h = new TH1F("h","gauss", 100, -3, 3); Int_t i; for ( i=0; i<1000; i++) h->Fill(gRandom->Gaus(0,1)); h->Draw(); TF1 *g1 = new TF1("g1","gaus",-3,3); TF1 *g2 = new TF1("g2"," g1 * 1.0 ",-3,3); g2->SetParameters(20,0,1); h->Fit( "g2" ); }

Thanks Brun. But when I use pre-defined function in root to fit, such as gaus, expo, I don’t need to set initial parameters, right?

[quote=“brun”]You should give initial values to your parameters.
[/quote]

Yes, see section “Setting Intial Conditions” at
http://root.cern.ch/root/htmldoc/TH1.html#TH1:Fit
Rene