# Fitting more than one histograms simultaneously?

Hi,

I have two histograms that can be modeled with two different functions that depend on the same parameter. I would like to fit both histograms simultaneously, trying to estimate the (common) parameter. Is there any smart way of doing this?

I could make a third histogram that would contain the two histograms, well separated by an offset, and try to fit that 3rd histogram to the sum of the two functions (separated by the same offset). But this is not very smart, is it…

Thanks.

–Christos

Hi,

Define your own objective function in TVirtualFitter in which
the function is calculated with the appropriate function.

eddy

Ok, but then how do I define the object/histogram to be fit? Would that be just the sum of the histograms (shifted in the x-direction), as I said in my first post?

–Christos

Ok, here a some pseudo code. The array par contains all parameters
to be fitted. Let’s say you want to fit a Gaussian line shape to
each of the two histograms. You know that the width of
the Gaussians should be identical:

h1 = par[0]; p1 = par[1]; w1 = par[2];
h2 = par[3]; p2 = par[4]; w2 = par[2];

//__________________________________________________________
Double_t *par,Int_t flag)
{
const TMyFit *fit = (TMyFit *) gMinuit->GetObjectFit();

const TH1D *h1d = fit->GetH1();
const TH1D *h2d = fit->GetH2();

const TF1 *f1 = fit->GetF1();
const TF1 *f2 = fit->GetF2();

Double_t chiSquare = 0;

gaussPar[0] = par[0];
gaussPar[1] = par[1];
gaussPar[2] = par[2];
for (Int_t i = 1; i < h1d->GetNbinsX(); i++)
{
const Double_t xval = h1d->GetBincenter(i);
const Double_t yval = h1d->GetBinContent(i);
const Double_t yerr = h1d->GetBinError(i);
const Double_t yfun = f1->EvalPar(xval,gaussPar);
chiSquare += (yval-yfun)*(yval-yfun)/yerr/yerr;
}

gaussPar[0] = par[3];
gaussPar[1] = par[4];
gaussPar[2] = par[2];

for (Int_t i = 1; i < h2d->GetNbinsX(); i++)
{
const Double_t xval = h2d->GetBincenter(i);
const Double_t yval = h2d->GetBinContent(i);
const Double_t yerr = h2d->GetBinError(i);
const Double_t yfun = f2->EvalPar(xval,gaussPar);
chiSquare += (yval-yfun)*(yval-yfun)/yerr/yerr;
}
}