# How to fit Gaussian on temporary histogram without Fit panel?

**URL:** <https://root-forum.cern.ch/t/how-to-fit-gaussian-on-temporary-histogram-without-fit-panel/50051>\
**Category:** Newbie\
**Created:** [May 21, 2022, 10:43pm UTC](https://root-forum.cern.ch/t/how-to-fit-gaussian-on-temporary-histogram-without-fit-panel/50051 "2022-05-21T22:43:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [May 21, 2022, 10:43pm UTC](https://root-forum.cern.ch/t/how-to-fit-gaussian-on-temporary-histogram-without-fit-panel/50051/1 "2022-05-21T22:43:52Z")

</div>

Hi,

I am plotting the histogram with the command similar to

> tree → Draw(“variable\>\>(bins, x-axis min, x-axis max”);

This creates a temporary histogram plot. I want to fit a Gaussian on this histogram without the fit panel. Is it possible?

I know one possible solution is to create a histogram and then fit a gaussian. Something like the following code,

```auto
TH1F *h = new TH1F("h", "h", bins, x-axis min, x-axis max); // define histogram
for (int i = 0; i < tree -> GetEntries(); i++){ // read entries
	tree -> GetEntry(i);
	h -> Fill(variable);
}
// fit gaussian
TF1 *fit = new TF1("fit", "gaus", min, max);
h -> Fit("fit", "RQ");

```

But as this approach involves for loop, it is very slow.  
Please give some suggestions.

Thank you.

---

<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:** [May 22, 2022, 4:37am UTC](https://root-forum.cern.ch/t/how-to-fit-gaussian-on-temporary-histogram-without-fit-panel/50051/2 "2022-05-22T04:37:54Z")

</div>

After you create `h` like above (`TH1F *h = new...`), just do

```auto
tree->Draw("variable>>h");

```

instead of the for loop.
