Fitting a histogram with a collection of histograms?

Hi - I am using ROOT5 34/36 version.
I have a histogram I would like to fit with a linear combination of four other histograms, like h = ah1+bh2+ch3+dh4, with a,b,c,d fitting parameters

Anybody has an simple/elegant example of how to do it?

I have googled the “fitting ROOT histograms with histograms”. Essentially,
I would like a fitting function to be represented as an linear combination of
4 histograms. I found the expression:

TF1 * f_en = new TF1(“f_en”,[&](double *x, double *p){ return p[0]histo->GetBinContent(int(4x[0])); }, 60, 260, 1);

but when I try to use something like that, the program crashes with an error

root [0]
Processing analyze_b.C…
Error: Can’t call TF1::TF1(“f_en”,&{return p[0]histo->GetBinContent(int(4x[0]));},60,260,1) in current scope analyze_b.C:89:
Possible candidates are…

So how do I write TF1 that depends on the predefined histogram values?
Of course, I can think of a few very in-elegant solutions, but …;:sunglasses:

Thanks, Emil

I’m not sure there is an easy solution, but maybe @moneta or @dpiparo can give some hints…

Hi Emil,

what are you exactly trying to achieve here? Are you sure, even if we manage to build a “histpdf” (and we can), that the fit will converge to something meaningful?


Hi D -

it is sim0ple - in Geant4 simulation I have an energy spectrum.
I have flagged the 4 different process that have different energy spectra
(electron contained in the detector, electron entered and then reflected form a detector, bremsstrahlung process in the detector, etc.
I can flag it in simulation, but I cannot know what process was effective
in real measurement. Rather then trying to find the analytic function
that somewhat describes the spectrum, I want to use the simulated energy
spectrum in GEANT4 and see that the fit of the measured spectrum gives
the same fractions of each process as in Mothe Carlo. The Monte Carlo spectrum should be fitted perfectly (within the statistics, if I use different MC runs
for total histogram and component individual-process histograms.

Cheers, Emil

If you have multiple histograms that describe how your categories should look like and want to find out their relative fraction in some data histogram, you can use TFractionFitter.

It takes into account the limited statistics of your MC histograms as well.

Thanks you Graipher - that is exactly what I needed.
I have check it out for my problem and it works beautifully.
Cheers, Emil

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.