How to fit Gaussian on temporary histogram without Fit panel?


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,

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.

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


instead of the for loop.

1 Like