User Defined TF1 Fit Problem in Class

Hello,
I am a newbie on roottalk, so I appologise if these questions are inappropriate. I have been trying to do fits of user defined functions, but they don’t seem to be behaving as I’d wished. It could just be down to bad programming. There are two fits, an initial one and a final one which uses a convoluting loop, and it is this final fit which is not behaving. It seems to continue to run the function after it has tried to fit it, and seems to change the _binwidth variable in the process, although I don’t know how. I have admittedly taken a shortcut in using a static pointer to the class to access member variables in the fits.
What have I done wrong?

Cheers,
Toby Davies

I am using Red Hat Linux, Fermi Linux Release 7.3.1 (Bottom), 2.4.20-30.7smp, i686, and it does the same thing on root versions 4.00/08 and 3.05/07
Imp.tar (8.13 KB)

Hi,
So probably the problem is the function pointer (i’m just guessing) so why not try without
member function pointer and write the function in global space?
What I’m talking is about the line:

 _FDFunc = new TF1("_FDFunc",TDImpFit::FD,-0.007,0.007,2);

and the ::FD function.

Hi,
there are two fits in the code, _FDFunc and _ImpFunc; the FD fucntion works fine, and does everything that I expect it to do so I assumed that that was the correct syntax. It is the _ImpFunc that does not work.
So as a separate question, does the function have to be in the global space? I was hoping for the class to have the ability be instantiated several times upon a range of histograms (I’m not sure if this is possible in the present state of the class because the functions in different classes would have the same root name). Still, even if this is possible, surely it can recognise the function within the class…?

Cheers,
Toby

edit —sorry -being stupid- the definition of the function could be global (it’s static anyway…) and still used by the TF1 classes… I think…

Hi,
I think somehow the problem is in the second function implemntation if you set

Double_t TDImpFit::ImpFunc(Double_t *x, Double_t *par)
{
  return 10000;
}

the fit is working

Hi,
I agree too, that was the original purpose of the return 3000 line which was commented out. I originally read the _binnumber variable directly from the histogram pointer but this always caused a segmentation fault; so there is something wrong with accessing the variables, I believe. Although I don’t know why that may be. Again, I believe something dodgey is going on because _binnumber changes value during the fit, and I was under the impression that as it was protected, it could not be changed by anything which was external to the class. My bet is that this is probably a result of the calling of the static pointer to the class in order for the static fit to access the data, but I believe it still should work…
Cheers,
Toby

Hi,

Can you send the instruction (and maybe additional files) to exactly reproduce your problem?

Cheers,
Philippe.

Hi,
the files in the tar file are enough for me to run the program with the command
root ImpFitRun.C
Cheers,
Toby

Hi Toby,

Your major problem is:[quote]TH1F ImpHist((TH1F)f->Get(“d0_D0_0;1”));
f->Close();
cout <<“HistIntegral Check:”<Integral()<<endl;
[/quote]because histogram (tree and event list) belong by default to the file so when you close the file they are deleted. Any time you (in your current code) access ImpHist after the f->Close() you access random memory (with random results!).

There was also a few member that you did not initliaze and you were using _c1 before setting it!. Anyway using valgrind (valgrind.kde.org) I was able to find most of those problems. I attached a new version of your code (that seems to work).

Cheers,
Philippe.
Imp2.tar.gz (4.15 KB)

Great!
Thanks, it works without any problems (apart from seemingly a bad fit.) I suppose the messing around in the undefined memory messed up all the other variables too…

Cheers,
Toby Davies