# Fit histogram to another historgam

**URL:** <https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [February 2, 2023, 10:54am UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407 "2023-02-02T10:54:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuisMiguel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b2d939/32.png) [@LuisMiguel](https://root-forum.cern.ch/u/LuisMiguel)\
**Post date:** [February 2, 2023, 10:54am UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407/1 "2023-02-02T10:54:38Z")

</div>

Hello everyone

I have some experimental data and simulated data of some beta decay. I want to find out a method to scale down the experimental data and stretch or shrink it so it will fit the simulated data, and getting the parameters that root used for this fit, something like:

A_h(a_x+b),

where “A” would be the scaling factor, as in the Scale(); function and x the bins of the histogram, where “a” and “b” are some parameters. I have the idea of how this could be done with minimization in a mathematical sense, but I don’t know how to implement it in root.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 2, 2023, 10:56am UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407/2 "2023-02-02T10:56:17Z")

</div>

> [@Can we shift histogram for several channels?](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2):
>
> I’m not sure if this is fully kosher, but suit yourself : #include "TAxis.h" #include "TH1.h" #include "TArrayD.h" Double\_t ScaleX(Double\_t x) { Double\_t v; v = 10 \* x + 100; // "linear scaling" function example return v; } Double\_t ScaleY(Double\_t y) { Double\_t v; v = 20 \* y + 200; // "linear scaling" function example return v; } Double\_t ScaleZ(Double\_t z) { Double\_t v; v = 30 \* z + 300; // "linear scaling" function example return v; } void ScaleAxis(TAxis \*a, Double\_t (…

---

<div class="post-metadata">

**Author:** ![LuisMiguel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b2d939/32.png) [@LuisMiguel](https://root-forum.cern.ch/u/LuisMiguel)\
**Post date:** [February 2, 2023, 12:02pm UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407/3 "2023-02-02T12:02:03Z")

</div>

Thank you!

I read it yesterday while searching the topic (now I know how to do the stretching so it’s great), but my questions was rather how to find the _best_ parameters for the scaling and stretching apart from trial and error, like a minimization of the distance between the two histograms like

\min \int |h\_1-h\_2|^2

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 2, 2023, 12:04pm UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407/4 "2023-02-02T12:04:21Z")

</div>

I guess, in general (when stretching/shrinking and scaling are needed), you would need to write your own “chi2” or “likelihood” function.  
See: [`${ROOTSYS}/tutorials/fit/`](https://root.cern/doc/master/group __tutorial__ fit.html)

You could also try to use (but the involved histograms need to have well “aligned” bins, I guess):

- [TFractionFitter](https://root.cern/doc/master/classTFractionFitter.html)

- an idea given in:

> [@Newbie Fit/RooFit Question](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334/3):
>
> Hi @katrina09, indeed, RooFit is well suited for this! You can find the necessary code snippets for your task in the [RooFit tutorials](https://root.cern.ch/doc/master/group __tutorial__ roofit.html). In particular, take a look at: [ROOT: tutorials/roofit/rf706\_histpdf.C File Reference](https://root.cern.ch/doc/master/rf706__histpdf_8C.html), which tells you how to build a pdf based of template histograms [ROOT: tutorials/roofit/rf201\_composite.C File Reference](https://root.cern.ch/doc/master/rf201__composite_8C.html), which tells you how to create the normalized sum of two pdfs Note that in ht e tutorial overview, there are also links to the Python version of both tu…

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [February 2, 2023, 12:47pm UTC](https://root-forum.cern.ch/t/fit-histogram-to-another-historgam/53407/5 "2023-02-02T12:47:03Z")

</div>

It sounds like a job made for RooFIT.  
You should do something like this:

```auto
using namespace RooFit;
RooRealVar XAxis("Axis","Axis",0,2000);
RooPlot *Frame = XAxis.frame();

RooRealVar Amount("Amount","Amount",0,0,10000);
RooRealVar Shift("Shift","Shift",0,-10,10);
RooFormulaVar ShiftedX("ShiftedX","ShiftedX",RooArgSet(XAxis,Shift));

RooDataHist *DataHist = new RooDataHist("Data","Data",XAxis,Some_Histogram_Simulated_Data);
RooHistPdf FitCurve("Curve","Curve",ShiftedX,XAxis,*DataHist,0);

RooDataHist RealData("RD","RD",XAxis,Import(*Some_Histogram_Real_Data));
RealData.plotOn(Frame);
FitCurve->fitTo(RealData);
FitCurve->plotOn(Frame);
Frame->Draw();

```

It is important to have the simulated data and real data with same binning (otherwise it looks horrible). If you fit several histograms, it is also better to only allow a shift on the x Axis that is discrete like the bins
