I guess what youâ€™re trying to do is fitting a gaussian with an underlying exponential background.
The first thing I notice is that your gaussian part is missing the sigma parameter. The actual gaussian reads [0]*exp(-0.5*((x-[1])/[2])^2). With youre formula the sigma is fixed to 1, which when I look at the plot does not match your data.
Secondly the exponential has to be added to, not multiplied with the gaussian like in your equation.
I would also suggest that you take the first few bins out of the fit range, cause your data are dropping there which cannot be described by the exponental background and thereby can confuse the fitter.
Generally if a fit fails it is always a good idea to plot the function with the inital parameter. It should describe the data already a little bit otherwise the fit is likely to fail cause the optimum is too far away from the inital values.

In your case Iâ€™m pretty sure that fixing your function will fix your fit. The function I think you want ist ([0]*exp(-0.5*((x-[1])/[2])^2) + [3]*exp(-[4]*x))
By the way this is technically the same as gaus(0) + expo(3) using the predefined functions gaus and expo.
You can get more information on declaring eqiations and the predefined ones here: https://root.cern.ch/doc/v612/classTF1.html

Hi,
I had initially kept the sigma but I was multiplying the two functions and was thus getting erroneous results. Thatâ€™s why I decided to mute the parameters one by one to see if anything changes. I gave up after removing the sigma term, and thatâ€™s when I posted here.

I tried adding the two functions and removing the first twenty bins and itâ€™s giving a decent fit.

However, I wanted to ask you a somewhat unrelated question about the histogram that I am trying to fit. Iâ€™m generating the histogram from a tree from a root file. Everytime I run the code, the peaks of the exponential and the gaussian keep changing, which is why I had trouble fitting it in the first place. The changes arenâ€™t too drastic (the exp peaks go up and down only by 10-20 units) but I donâ€™t know why this is happening

This sounds indeed very strange. Could cou provide more information on the code you are using to generate the tree and to extract the histogram? Is there somewhere a random factor going into like in a simulation? Are you using a self written way to fill the histogram or do you use the TTree::Project method?

Generally if you have two data sets that should be identical it is always a good idea to look at the statistical errors. Therefore call ->Sumw2() on your histogram before it is being filled and then draw it using the â€śEâ€ť option. Do this for both histograms you want to compare and then plot them over each other. If qbout 2/3 (1 sigma range) of all point meet within their error bars, the differences are described by statistical fluctuations.

At your maximum count rate of ~ 200 the statistical fluctuations can be estimated by sqrt(200) ~ 14 so fluctuations of 10 - 20 can be purley statistical, but only if there is a random factor going in somewhere, because otherwise the program should be deterministic.

I was given a root file which included a tree.
The code I use to extract and generate the hist is as follows :

void zdecay::Loop()
{
//* Creating a vector of objects of class particles.
// TCanvas c1("c1","Histogram here",600,800);
cout << "Begin" << endl;
int verbose_level = 1;
int need = 0;
if (fChain == 0) return;
// TCanvas c1("c1","c1",800,1000);
TH1F h1("h1","Histogram",150,0,600);
// h1.FillRandom("gaus",250000);
// h1.Draw();
Long64_t nentries = fChain->GetEntriesFast();
cout<<"Total entries "<<nentries<<endl<<endl;
Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
//if (ientry < 0) break;
nb = fChain->GetEntry(jentry);
nbytes += nb;
// cout << "processing event "<<jentry <<endl;
vector<particles> electrons;
// cout<<"event no "<<jentry<<" eta of first electron "<<nEle<<endl;
// Acquisition loop. If the event has at least one electron (or particle) then it makes an electron object
// which is inserted into the vector - electrons
if(nEle>0) {
for (int i=0 ;i<nEle; i++){
particles electron(elePhi -> at(i),eleEta->at(i),elePt->at(i),eleCharge->at(i));
electrons.push_back(electron);
}
std::sort(electrons.begin(),electrons.end(), compare_trans);
// Contructing the four vector for the first two particles with highest Pt - Summing them in V3 for display.
TLorentzVector v1(electrons[0].getPx(),electrons[0].getPy(),electrons[0].getPz(),electrons[0].getE());
TLorentzVector v2(electrons[1].getPx(),electrons[1].getPy(),electrons[1].getPz(),electrons[1].getE());
TLorentzVector v3 = v1+v2;
// if (v3.M()>60.0 & v3.M() <140.0)
h1.Fill(v3.M());

Where I have defined a class particles separately.

Well your code does not explain the fluctuations. I would say it is not really important because they are most probably covered by the statistical errors, but itâ€™s definitly still a strange behavior. Maybe there is a bug in the sorting algorithem for the particles vector, but I can only guess.

BTW. Looking at the plot in your first post here, it seems to me that you should define the range of your â€śfitâ€ť function from about 30 (instead of 1) to 150. Moreover, it seems to me that you should add the â€śgaussianâ€ť and the â€śexponentialâ€ť, not multiply them. Also, do remember to use the â€śRâ€ť fitting option (otherwise the range of your â€śfitâ€ť function will not be taken in to account): h1.Fit("fit", "R");

which was nELe > 0 before. How was this interfering though?

This results in an error.

In file included from input_line_10:1:
D:\debajyoti_project\zdecay.C:78:12: error: member function 'getPt' not viable: 'this' argument has type 'const particles', but function is not marked const
return particle1.getPt() > particle2.getPt();
^~~~~~~~~
D:\debajyoti_project\zdecay.C:36:11: note: 'getPt' declared here
double getPt(){
^
D:\debajyoti_project\zdecay.C:78:32: error: member function 'getPt' not viable: 'this' argument has type 'const particles', but function is not marked const
return particle1.getPt() > particle2.getPt();
^~~~~~~~~
D:\debajyoti_project\zdecay.C:36:11: note: 'getPt' declared here
double getPt(){

which is why I think I opted for omitting const in the first place iirc.

Thank you for this. I was writing ugly code really.

Requiring nEle>1 in the beginning will also solve your problem. My proposal was to leave nEle>0 and then set nonzero â€śv2â€ť when there is the second electron present. This would add cases with one electron only.

The Histogram just wonâ€™t show up during runtime. I always have to save it to a file and then open it with TBrowser in the interactive session. Any fix for this?

I assume it gets deleted when your macro finishes.
If you want to â€śpreserveâ€ť it, you would need to create it (and probably your fit function, too) on the heap (i.e. using â€śnewâ€ť).

I understand that the first three parameters of the crystalball function is amplitude,mean and sigma. I am having trouble figuring out a good starting point for the 4th and 5th parameter.
Help.