# New to ROOT, hoping it has a solution to my problem. Tried to fit a curve using GSL

**URL:** <https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064>\
**Category:** ROOT\
**Created:** [March 16, 2023, 10:42am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064 "2023-03-16T10:42:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koen\_van\_Wel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/koen_van_wel/32/32821_2.png) [@Koen\_van\_Wel](https://root-forum.cern.ch/u/Koen_van_Wel)\
**Post date:** [March 16, 2023, 10:42am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/1 "2023-03-16T10:42:00Z")

</div>

Hi,  
I keep getting an error and the only relevant hit I got using google was on this forum (for some reason I’m not allowed to link the post)

I’m trying to fit a non-linear curve using GSL (we have 1.15 here) I can’t compute the partial derivative of this curve with respect to it’s input parameters, or at least I don’t know how to. The curve represents the envelope of a frequency that’s send to a magnet in the LHC.

I used scipy.curve\_fit in python and it worked like magic, but now I need to translate it in C++. I tried calculating the Jacobian matrix, but I think I did that wrong. I tried looking into the scipy.curve\_fit implementation, but when I went down that rabbit hole I found myself looking at a Fortran implementation of GSL and now I’m even more lost.

I’m hoping to find a solution to fit a function here. does anyone have any experience in this?

This is the function I’m trying to fit, it basically looks like a trapezoid. it’s 0 before start, has a linear rising edge up until the flat top, is “almost” flat at the flat top and then a linear falling edge until its 0 again.

```auto
std::vector<double> envelope(std::vector<double> timestamps, double start, double startFlt, double endFlt, double end, double startAmpl, double endAmpl) {
    std::vector<double> result(timestamps.size(), 0);
    double startSlope = startAmpl/(startFlt-start);
    double fltSlope = (endAmpl - startAmpl) / (endFlt - startFlt);
    double endSlope = (-endAmpl/(end-endFlt));
    for (size_t i=0; i<result.size(); i++) {
        double timestamp = timestamps[i];
        if (timestamp < start) {
            // do nothing, is already initialized to zero
        } else if (timestamp < startFlt) {
            result[i] = (startSlope * (timestamp - start));
        } else if (timestamp < endFlt) {
            result[i] = (fltSlope * (timestamp - startFlt) + startAmpl);
        } else if (timestamp < end) {
            result[i] = (endSlope * (timestamp - endFlt) + endAmpl);
        } else {
            // do nothing, is already initialized to zero
        }
    }
    return result;
}

```

Oh and by the way. The tutorial of installing ROOT mentions cmake, is there a way by using makefile?

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 16, 2023, 11:51am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/2 "2023-03-16T11:51:51Z")

</div>

Hi @Koen_van_Wel,

Don’t know whether it can help, but I’m inviting @jonas and @moneta -our RooFit experts- to this topic. Maybe the can suggest you something useful.

> [@Koen\_van\_Wel](#):
>
> Oh and by the way. The tutorial of installing ROOT mentions cmake, is there a way by using makefile?

No, ROOT build system is based on CMake. However, if you want to install ROOT, we encourage you to install it via a pre-compiled binary package or through Conda. See [Installing ROOT - ROOT](https://root.cern/install/#conda) for detailed instructions.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![Koen\_van\_Wel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/koen_van_wel/32/32821_2.png) [@Koen\_van\_Wel](https://root-forum.cern.ch/u/Koen_van_Wel)\
**Post date:** [March 16, 2023, 12:15pm UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/3 "2023-03-16T12:15:33Z")

</div>

Hi @jalopezg

Thank you for your reply. I’ll await the other suggestions.

> No, ROOT build system is based on CMake. However, if you want to install ROOT

I think I misspoke. I meant, can I include root in my c++ project that uses makefile? I couldn’t find documentation on that. I 'm working through --root url–/install/build\_from\_source/

In an effort to supply more information:

I’m currently working with c++03 and gsl v1.15 is installed on the FEC. I can use c++11 while building ROOT from source, although I am stuck with gcc v4.8.5 (which has c++11 as an experimental feature) and cmake v2.8.12.2.

I found this forum because the error I was getting is spoken about here: --forum url–/t/gsl-cannot-reach-the-specified-tolerance/12172

I was using `gsl_multifit_fdfsolver`, because there is no implementation of `gsl_multifit_fsolver_type` in GSL v1.15 yet. I suspect I need to use ROOT::Math::GSLMultiFit in a way, but I’m not if it is the right solution.

Cheers,  
Koen

PS. Why cant I link to other urls?

---

<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:** [March 16, 2023, 12:39pm UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/4 "2023-03-16T12:39:35Z")

</div>

Try: `grep -r gsl_multifit_fsolver_type /usr/include/gsl`

With GCC 4.8.5, you can use [the ROOT 6.24 patches branch](https://github.com/root-project/root/tree/v6-24-00-patches) or some older version.

I guess you should be able to “upgrade” your CMake (e.g., you could take the “[cmake.org](https://cmake.org/download/)” binaries or the “`cmake3`” package from the “EPEL” repository).

You can use ordinary makefiles for your own projects. There exist plenty of examples, e.g., grab the ROOT source code, `cd root_source_dir`, and then: `find t[eu]* -name "*akefile*"`

---

<div class="post-metadata">

**Author:** ![Koen\_van\_Wel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/koen_van_wel/32/32821_2.png) [@Koen\_van\_Wel](https://root-forum.cern.ch/u/Koen_van_Wel)\
**Post date:** [March 16, 2023, 1:25pm UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/5 "2023-03-16T13:25:05Z")

</div>

Thanks, I’ll look into it! Do you know what I need from ROOT to do a fit like this?

> Try: `grep -r gsl_multifit_fsolver_type /usr/include/gsl`

Yes the type exists, but there’s no implementation. Here’s the doc on gsl v1.15

> 38.7 Minimization Algorithms without Derivatives  
> There are no algorithms implemented in this section at the moment.

---

<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:** [March 16, 2023, 3:26pm UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/6 "2023-03-16T15:26:07Z")

</div>

I don’t really understand what you expect / miss.

I tried “GSL 2.7.1” and “GSL 1.15” and for both of them, I got almost the same output when I tried:  
`grep -r gsl_multifit_fsolver /usr/include/gsl`

The “GSL 2.7.1” has one more function named “`gsl_multifit_fsolver_driver`”.

For what is available in ROOT, see, e.g.:

- [ROOT Manual → Functional parts → Mathematical libraries → Minimization libraries and classes](https://root.cern/manual/math/#minimization-libraries-and-classes)

- [MathCore/MathMore → Numerical Algorithms → One-dimensional Minimization](https://root.cern/doc/master/group__Min1D.html)

- [MathCore/MathMore → Numerical Algorithms → Multi-dimensional Minimization](https://root.cern/doc/master/group__MultiMin.html)

- [`${ROOTSYS}/tutorials/fit`](https://root.cern/doc/master/group __tutorial__ fit.html)

- [`${ROOTSYS}/tutorials/roofit`](https://root.cern/doc/master/group __tutorial__ roofit.html) and [`${ROOTSYS}/tutorials/roostats`](https://root.cern/doc/master/group __tutorial__ roostats.html)

You can try the “`GSLMultiFit`” with [the `combinedFit2.C` macro](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/5) and/or the [`${ROOTSYS}/tutorials/fit/NumericalMinimization.C`](https://root.cern/doc/master/NumericalMinimization_8C.html) macro.

UPDATE: @moneta Out of these two “tutorials” mentioned above, [the `combinedFit2.C` macro](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/5) breaks with ROOT 6.28 (it’s fine with older releases) and the [`${ROOTSYS}/tutorials/fit/NumericalMinimization.C`](https://root.cern/doc/master/NumericalMinimization_8C.html) macro dies with any version (when the “`GSLMultiFit`” is tried).

---

<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:** [March 17, 2023, 9:45am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/7 "2023-03-17T09:45:03Z")

</div>

Hi,

The `GSLMultiFit` method works only for least-square and likelihood fits. It requires that the function you minimise is a derived class of [`ROOT::Math::BasicFitMethodFunction`](https://root.cern.ch/doc/master/classROOT_1_1Math_1_1BasicFitMethodFunction.html).  
For this is expected that the `NumericalMinimization.C` tutorial does not work in this case, since it applies to minimise a generic function.

Lorenzo

---

<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:** [March 17, 2023, 9:58am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/8 "2023-03-17T09:58:05Z")

</div>

@moneta Well, the [`${ROOTSYS}/tutorials/fit/NumericalMinimization.C`](https://root.cern/doc/master/NumericalMinimization_8C.html) macro explicitly lists available choices for “minimizes” and “specific algorithms” (so maybe one should “clean” this list there).

---

<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:** [March 17, 2023, 11:44am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/9 "2023-03-17T11:44:36Z")

</div>

Thank you @Wile_E_Coyote for reporting this, I will update that list in the tutorial documentation.

Here it is also the updated macro combinedFit2.C working with the 6.28 releases (and older versions)

Lorenzo

[combinedFit2.C](https://root-forum.cern.ch/uploads/short-url/xXpPbdx4zejAB7mYvONp9VpJnYx.C) (6.0 KB)

---

<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:** [March 31, 2023, 11:45am UTC](https://root-forum.cern.ch/t/new-to-root-hoping-it-has-a-solution-to-my-problem-tried-to-fit-a-curve-using-gsl/54064/10 "2023-03-31T11:45:37Z")

</div>

Closed on March 31, 2023, 11:45am UTC

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