# Fitting histogram with TF1 multiple times

**URL:** <https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416>\
**Category:** ROOT\
**Created:** [January 17, 2022, 7:15am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416 "2022-01-17T07:15:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![psadangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/13edae/32.png) [@psadangi](https://root-forum.cern.ch/u/psadangi)\
**Post date:** [January 17, 2022, 7:15am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/1 "2022-01-17T07:15:30Z")

</div>

Hi Experts,

I have to fit same histogram with different functions. Different function in the sense, the function is fixed but the parameter values are different. There are 4 parameters to the function and there are around 2000 values for each parameter stored in the tree. How can i fit 2000 times to the same histogram where all the 2000 fit should be visible in the histogram.

I hope what i want to ask is clear. I am attaching the root file where all the parameter values are stored and another root file which consist the histogram. I am attaching the code too where i was trying to do the multifit, but i couldn’t do.

Please help me solving this.

Thanks  
Priyanka

[eff\_band.cpp](https://root-forum.cern.ch/uploads/short-url/bQ19aa5HV57U43nnJ1kxme7Ptbu.cpp) (2.5 KB)  
[eff\_var.root](https://root-forum.cern.ch/uploads/short-url/l61vLmUiKw0i2VIS8GZGhekRC5r.root) (7.5 KB)  
[parametr\_eff\_syst\_2sigma\_ch1.root](https://root-forum.cern.ch/uploads/short-url/84BeTP6htqmOeFfkrQFweojam65.root) (69.4 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 17, 2022, 7:48am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/2 "2022-01-17T07:48:44Z")

</div>

I think @moneta can help you.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 17, 2022, 8:59am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/3 "2022-01-17T08:59:52Z")

</div>

Hi,  
Have you tried to use the option `+` ?  
This will allow you to store all the fitting functions in the histograms.  
If it does not work, please let me know

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![psadangi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/13edae/32.png) [@psadangi](https://root-forum.cern.ch/u/psadangi)\
**Post date:** [January 17, 2022, 10:02am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/4 "2022-01-17T10:02:49Z")

</div>

Hi,

Thanks for your reply. I have tried using R+ before. I could do the fitting with multiple function when all the functions are different. But here specifically my problem is, function parameters need to vary 2000 times. It shows “Error in : function function has illegal number of parameters = 0” when i run the code. (code attached)

Thanks  
Priyanka  
[eff\_band.cpp](https://root-forum.cern.ch/uploads/short-url/rYUzMLW2wBjUzifSsaX1ZJYJd6n.cpp) (2.5 KB)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 17, 2022, 10:52am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/5 "2022-01-17T10:52:47Z")

</div>

See how to do the fits, in particular with “user-defined functions” and more info in the links included:

> [@How to Fit Histograms or Data Points?](https://root-forum.cern.ch/t/how-to-fit-histograms-or-data-points/38870):
>
> Fitting in ROOT is based on the [TMinuit](https://root.cern/doc/master/group__MinuitOld.html) class. See in particular [Basic Concepts of Minuit]([http://root.cern.ch/doc/master/classTMinuit.html#TMinuit:Basic](http://root.cern.ch/doc/master/classTMinuit.html#TMinuit:Basic) Concepts of Minuit). The user has the choice of using directly the TMinuit class, as illustrated in the last section below or use specialized functions provided in the histogram class [TH1](https://root.cern/doc/master/classTH1.html) or the graph classes [TGraph](https://root.cern/doc/master/classTGraph.html) and [TGraphErrors](https://root.cern/doc/master/classTGraphErrors.html). The Minuit package acts on a multiparameter Fortran function FCN. In the ROOT implementation, the function F…

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 31, 2022, 10:53am UTC](https://root-forum.cern.ch/t/fitting-histogram-with-tf1-multiple-times/48416/6 "2022-01-31T10:53:03Z")

</div>

Closed on January 31, 2022, 10:53am UTC

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