Defining TF2 functions and doing fits in for loop

From: <nguler_at_jlab.org>
Date: Wed, 1 Dec 2010 17:30:02 -0500


Hello All,

I am using root 5.26-00-64bit. I have a TH2F object:

    TH2F *histsim = new TH2F(histsimName,histsimName,256,0,256,256,0,256);

Then I define fit functions:

    TF2 *fitsim1_5 =

             new
TF2("fitsim1_5",Gaus2D,jVec[0]-range,jVec[0]+range,iVec[0]-range,iVec[0]+range,5);

    fitsim1_5->SetParameters(pVec[0]/100,jVec[0],1.,iVec[0],1.);

    TF2 *fitsim2_5 =

             new
TF2("fitsim2_5",Gaus2D,jVec[1]-range,jVec[1]+range,iVec[1]-range,iVec[1]+range,5);

    fitsim2_5->SetParameters(pVec[1]/100,jVec[1],1.,iVec[1],1.);

and fit the histogram:

    histsim->Fit("fitsim1_5","RN");
    histsim->Fit("fitsim2_5","RN");

And it works. However, I would like to create the fit functions in a loop and make the fitting in the same loop for several different regions, such as:

    const int isize = pVec.size(); //// isize = 2 since there are two peaks to fit here.

   TF2 *fitarr[isize];
    for (int is=0; is<isize; is++)
    {

        char afitn[20];
        int range = 10;
        sprintf(afitn, "fitsim3_%d", isize);
        double xmin = double(jVec[is] -range);
        double xmax = double(jVec[is] +range);
        double ymin = double(iVec[is] -range);
        double ymax = double(iVec[is] +range);
        double xctr = double(jVec[is]);
        double yctr = double(iVec[is]);
        double peak = double(pVec[is]/100);

        fitarr[is] = new TF2(afitn,Gaus2D,xmin,xmax,ymin,ymax,5);
        fitarr[is]->SetParameters(peak,xctr,1.,yctr,1.);

        fitarr[is]->SetLineColor(kBlack);
        histsim->Fit(afitn,"RN+");
        fitarr[is]->SetRange(0,0, 256,256);
        fitarr[is]->SetMaximum(histsim->GetMaximum());
        fitarr[is]->Draw("cont1, same");
        gPad->Modified();
        gPad->Update();

    }

When I do this, it only plots the last fit in the loop. The first fit is lost somewhere and not plotted. If I do the loop only for one fit function (by changing isize to 1), it plots the first fit. When isize =2, it only plots the second fit. I don't see any difference between the first method (which works) and the second method (that fails). Unfortunately I am not able to send a working code since the input files are rather big. But the main idea is to do multiple fits on the same histogram (on different ranges of it) by using a for loop. I appreciate your comments if I am doing something wrong in the for loop.
Thanks,

Received on Wed Dec 01 2010 - 23:30:12 CET

This archive was generated by hypermail 2.2.0 : Thu Dec 02 2010 - 05:50:02 CET