# fit2.C Tutorial Macro Help

Hello,
I am trying to understand the fit2.C tutorial macro included in the ROOT program. I am using CMSSW_5_3_16 ROOT version 5.13. I am attaching my commented version of the code.

I have conferred with several other people but we do not understand the lines from 51 to 56. It seems to me that it is redefining the amplitude parameters for the Gaussian functions. However, I do not know why this is necessary when the amplitudes can already be defined. Is there a point to redefining them afterwards? Where does the number 4 come from, and why does it not seem to matter if I change it to be higher or lower? Changing it to 1 or removing these lines entirely makes the code return “Warning in : Abnormal termination of minimization.” My ultimate goal is to use what I learn to make a macro that will create a Gaussian fit for the energy deposit in a detector, but I am wondering if I am going about it the wrong way. Please let me know if you have any insight.

Sincerely,
Owen Baron
fit2.C (2.7 KB)

The lines in question set “initial values” for parameters for the function to be fitted. They should be set as close as possible to “real values”, otherwise the fit procedure may fail to find the minimum (and you will get different failure messages, including “abnormal termination”).
The factor 4 is related to the “h2” bin “area”.

Thank you very much - can you clarify on what you mean by “bin area”? How would I find the right value to use if I were using an energy-deposit histogram from a simulation, for instance?

What matters in this case is how many entries you will get in your histogram’s bins (the bigger the “area” of bins, the more entries will be in each particular bin, while the total number will remain “nentries”), so you can define:
“bin area” = “size of a bin along X” * “size of a bin along Y”

Then, for example, for:
TH2F *h2 = new TH2F(“h2”, “from f2”, 80, -10, 10, 80, -10, 10);
you can try (4 times more bins in total, so 4 times smaller “bin area”, so 4 times smaller “factor”):
Float_t ratio = 1.*nentries/100000.;

As already said, the whole point is to set as good as possible “initial values” for parameters for the function to be fitted (so it depends on how many entries your histogram has and how your fit function looks like).

Thank you again - I have another question about this code, though. I believe that line 40 TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar); is setting the range for the function to fit, and then feeding in the parameters. Is this accurate? Looking over the code, I noticed that I can’t tell where the array par[] is defined, but my guess is that line 40 is providing the values through npar - somehow it knows to feed in the next five values of parameters in the array for p2 and p3, respectively.

I wanted to try setting the function to fit using just a certain range by Fit(“R”) but I am confused as to what is happening if I change the values in line 40. If I put in large numbers than 10, the covered area gets smaller, and smaller numbers make the area larger - why is that the case? My goal is to make a fit that goes from abs(1) to abs(10) for an application that does not take into account a peak in the middle.

Sincerely,
Owen Baron

The “npar” is an integer value which defines how many parameters the “f2” will have and then the TF2 constructor creates an “internal array” of appropriate size. The call “f2->SetParameters(f2params);” initializes this “internal array” with values from “f2params”.
If you want to play with the “R” fit option, you can change the “f2” function range directly before the fit call using something like (xmin, ymin, xmax, ymax are just Double_t values):
f2->SetRange(xmin, ymin, xmax, ymax);
h2->Fit(“f2”, “R”);

Thank you again, that makes lots of sense. One more question - say I want to fit the range of x = { (-10,-1), (1,10)} leaving out the area from -1 to 1. Is there a way to do that - I suppose using AddRange? Or would it not work?

Sincerely,
Owen Baron

Thank you - I couldn’t get multifit to work earlier, but I will look at the other macro and see if I can use that.

Sincerely,
Owen Baron

I have another question after looking over the fit2.C macro some more. In lines 17-20 it creates the gaussian formula from the parameters in f2params[npar]. What I don’t understand is how it knows to use parameters 5-9 and 10-14 for the next two functions. My guess is that it has something to do with the input of (x,p1) and it knows that (x,p2) uses 5-9, etc. Is this what is being defined by lines 26-31, where (for example) it defines *p2 = [5] and *p3 = [10]? The flow of the macro is not very intuitive to me so it is hard to tell what is feeding what.

Sincerely,
Owen Baron