# Fitting Gaussians without so much user input

**URL:** <https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [January 31, 2024, 11:00pm UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018 "2024-01-31T23:00:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liliac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/3bc359/32.png) [@liliac](https://root-forum.cern.ch/u/liliac)\
**Post date:** [January 31, 2024, 11:00pm UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018/1 "2024-01-31T23:00:19Z")

</div>

Hello! I’m new at ROOT and I’m currently working with gamma spectrums. My goal is to write a ROOT script that can find the peaks and fit them with a Gaussian. I’ve managed to find the peaks, but I’m confused when it comes to fitting the peaks.  
It seems to me that TSpectrum() only works if I specify the peak parameters in the script. I’d like to write something that could fit the peaks regardless of the isotope I’m inspecting.  
So, I’m currently using TF1, and it works fine as long as I specify when to start/end in the script itself. This is annoying since I’d like to simulate more than one isotope, and I don’t want to keep changing the script to fit the peaks. Also, having to put it the gaussian parameters feels counterproductive.  
How can I make the process more automatic?  
Thanks!

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [February 1, 2024, 10:17am UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018/2 "2024-02-01T10:17:52Z")

</div>

Hello @liliac,  
welcome on the ROOT forum!  
I think that @Jonas can help with this, but in the meanwhile I suggest you to have a look at the [roofit tutorials](https://root.cern/doc/master/group __tutorial__ roofit.html).

Cheers,  
Monica

---

<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:** [February 2, 2024, 10:42am UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018/3 "2024-02-02T10:42:26Z")

</div>

Hello,

`TSpectrum` should be the think you want to use. It is able to find automatically the position of the peaks that can be used later for Gaussian fitting.  
You can look at the tutorial [peaks.C](https://root.cern.ch/doc/master/peaks_8C.html)

Lorenzo

---

<div class="post-metadata">

**Author:** ![liliac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/3bc359/32.png) [@liliac](https://root-forum.cern.ch/u/liliac)\
**Post date:** [February 2, 2024, 6:09pm UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018/4 "2024-02-02T18:09:53Z")

</div>

Thank you! I ended up using TPolyMarker to find the peaks.

---

<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:** [February 5, 2024, 8:14am UTC](https://root-forum.cern.ch/t/fitting-gaussians-without-so-much-user-input/58018/5 "2024-02-05T08:14:29Z")

</div>

TPolymarker does not “find the peaks”. TSpectrum find them. Then you display what TSpectrum found using TPolymarker.
