# How to add 2 PDFs?

Hi,
I want to add 2 PDFs. One is gaussian and the other is Landau function. How to do it ?

-Diksha

If you are using `RooFit`, you can just use `RooAddPdf`:

``````auto mass = new RooRealVar("mass", ...);
auto mu = new RooRealVar("mu", ...);
auto sigma = new RooRealVar("sigma", ...);
auto gauss = new RooGaussian("signal", "Peak", mass, mu, sigma);
...
auto landau = new RooLandau(...);

auto n_gauss = new RooRealVar("n_gauss", "N_{gauss}", ...);
auto n_landau = new RooRealVar("n_landau", "N_{landau}", ...);
auto combined = new RooAddPdf("combined_model", "Gauss + Landau", RooArgList(gauss, landau), RooArgList(n_gauss, n_landau));
``````

`combined` is then an extended PDF which is the sum of the two PDFs. If you don’t want an extended PDF, just give it only one variable in the second `RooArgList`, which will then be the fraction of the first PDF of the total PDF.

I am actually new to root. I have defined my gaussian function and landau function separately using TF1 command.
Now I want to add these two. Can you please tell me in this direction.

If you have defined them something like this, it is quite easy as well:

``````auto gaus = new TF1("g", "gaus", -10, 10);
gaus->SetParameters(1, 0, 1);
auto landau = new TF1("l", "landau", -10, 10);
landau->SetParameters(1, 4, 0.5);

auto f = new TF1("s", "g + l", -10, 10);
f->Draw("l");
``````

Or, just define the combined function right away:

``````auto f = new TF1("f", "gaus(0) + landau(3)", -10, 10);
f->SetParameters(1, 0, 1, 1, 4, 0.5);
f->Draw("l");
``````

Alright I have to fit this landau+gaus curve to my values present in a root file. So, I did my fitting in this way :

{

TH1F *h= new TH1F(“h”,“trkmm gaus+landau fit”,30,0,2.5); /*creating empty
histogram */

T1-> Project (“h”,“abs(trkmm)”); /*projecting the values of trkmm present in root file on the empty histogram, h */

auto gaus = new TF1(“g”, “gaus”, -10, 10);

gaus-> SetParameters(1, 0, 1);

auto landau = new TF1 (“l”,“landau”,-10,10);

landau-> SetParameters(1, 0, 1);

auto f = new TF1 (“s”,“g+l”,-10,10);

h->Fit(“s”);
h->Draw(“e1”);

}

Now I also want to now the area under the curve. How to do that?
And this AddPdf function is only present in RooFit ?

Now I also want to *know

Yes, `AddPdf` only works in RooFit. However, using RooFit might be a good idea anyway, at least as soon as you are doing somewhat complicated fits.

To get the integral of a `TF1`, you can simply do:

``````f->Integral(0., 100.);
``````

where the two numbers are the boundaries of the integral.

Alright. Thanks a lot ! This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.