# Fitting gaus and expo to same histogram

Hello,

Can someone please help me with a code for how to fit both gausian and exponential function to the same histogram. I read the examples in the tutor but they were quit hard to understand.

I tried in the following way (becouse both functions are predefined), but it didnt work:

hist1->Draw();
TF1 *fit1 = new TF1(“fit1”,“gaus+expo”);
hist1->Fit(“fit1”);

Thanks!

see example below

```void gexpo() { TF1 *f1 = new TF1("f1","expo+gaus(2)",0,10); f1->SetParameters(1,-0.6,1,3,0.5); TH1F *h = new TH1F("h","test",100,0,10); h->FillRandom("f1",10000); h->Fit(f1); } ```

Rene

Thanx a lot for the code.
Could you please answear some questions regarding the code?

void gexpo() {
TF1 *f1 = new TF1(“f1”,“expo+gaus(2)”,0,10);
//why do you have (2) ?

f1->SetParameters(1,-0.6,1,3,0.5);
// what happends if I dont set any parameter values? Is it any reason for these values that you have choosen?

TH1F *h = new TH1F(“h”,“test”,100,0,10);
h->FillRandom(“f1”,10000);
//Any spesial reason for the value 10000? Can I use: h->Draw() instead of FillRandom?

h->Fit(f1);
}

Thank you!

Rene[/quote]

```void gexpo() { TF1 *f1 = new TF1("f1","expo+gaus(2)",0,10); ```

see TF1 doc at root.cern.ch/root/htmldoc//TF1.html
and read in particular “Example B1b” of section “Example of a function of type B+”

[quote]// what happends if I dont set any parameter values? Is it any reason for these values that you have choosen?
[/quote]

Parameters are set to 0 by default

```TH1F *h = new TH1F("h","test",100,0,10); h->FillRandom("f1",10000); ```/[quote]/Any spesial reason for the value 10000? Can I use: h->Draw() instead of FillRandom?[/quote]
of course, you draw your own histogram instead. I was trying to show a stand alone example easy to understand.

Rene

Thank you very much for the great help.

The only thing I am woundering now is that what values I have to choose in order to initialize the parameters?
When I change the values you have chosen I just get a strange graf.

Thank You!

You must provide initial values for your parameters. When fitting
an expression like expo+gaus(2), you have 5 parameters
p1 : a constant (1 should be a good starting point)
p2: the slope of expo
p3: constant for gaus (redundant with p1) you can also give 1
p4: approximate mean value of the gaussian
p5: approximate sigma of the gaussian

Rene