# Adding Gaussian PDF in event loop

**URL:** <https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483>\
**Category:** Newbie\
**Created:** [September 7, 2022, 10:02am UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483 "2022-09-07T10:02:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [September 7, 2022, 10:02am UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/1 "2022-09-07T10:02:26Z")

</div>

_ROOT Version:_ 6.14.00  
_Platform:_ Ubuntu 20.04 LTS  
_Compiler:_ gcc

* * *

Dear experts,

I would like to kindly ask for your help.

As a distribution, I want to add a gaussian pdf generated from the given mean(Ea) and sigma. N = sum of na.

From this table,

 ![cernn2](https://root-forum.cern.ch/uploads/default/original/3X/f/4/f4ebc08e1b3e6faa631898bf0956782158291778.png)

I want to calculate the total Probability Distribution given using :  
 ![cern3](https://root-forum.cern.ch/uploads/default/original/3X/b/a/badbe57622b404a597acc72e606b92f9259422ab.png)

And find the E distribution from above and add it here:  
 ![cern4](https://root-forum.cern.ch/uploads/default/original/3X/2/6/2693c6499824bbbf1d400fbaa7443e9d443bdb11.png)  
where, M(A) , Mn, Mp are constant values.

Here is my attempted code:

```auto
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
//#include "TH1D.h"
using namespace std;

//Calculate Probability Distribution Function
double norm_pdf(double x, double mu, double sigma, double na)
{
    
    return na* exp( -((x - mu)*(x-mu))/(2.0*sigma*sigma) );
}

void generateNormPdf(double mu[], double sigma[], double na[], vector<double>& x, vector<double>& density)
{
      
        double N = 22.0; // Total number of neutrons (na)  		
	int nev = x.size(); // Number of points to generate between min_range and max_range
	
        double min_range = 0.0;
	for (int i = 0; i<7; i++){ // 7 is size of sample
	  for (int j = 0; j<nev; j++)
	     { 
	       x[j] = (mu[i]*2.0 - min_range)/(nev-1) * j; 
	       density[j] = (1.0/N)*norm_pdf(x[j], mu[i], sigma[i], na[i]);
             }	    
        } 
     
}

int main() 
{
        int nev = 100; // Number of samples/events
        // PDF               
        vector<double> x(nev);
	vector<double> density(nev);	
	       
	 double mu[] = {62.0,40.0,35.0,18.0,13.15,11.45,5.56}; // Mean
        double sigma[] = {6.25,3.75,3.75,1.25,1.0,0.75,0.75}; // Standard Deviation
        double na[] = {2.0,4.0,2.0,2.6,2.0,4.0,2.0}; // # of nucleons
        generateNormPdf(mu, sigma, na, x, density);              
}

```

The final value I want is M\*(A-1) as distribution (i.e not a constant value).

It would be of great help.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 7, 2022, 8:24pm UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/2 "2022-09-07T20:24:43Z")

</div>

Aren’t you missing the factor `1/sqrt(2pi)/sigma` for the gaussian to be normalized?

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [September 7, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/3 "2022-09-07T21:04:43Z")

</div>

Hi! Thanks for the response.  
I think the normalization here is N, where N is the total number of “na”.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 7, 2022, 9:18pm UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/4 "2022-09-07T21:18:53Z")

</div>

If you want the normalization to be N, then you need to multiply Na with a normalized pdf. Right now, you are multiplying Na with a function (exp(…)) that is not normalized.

You should use:

![image](https://root-forum.cern.ch/uploads/default/original/3X/a/b/abfa6a8fb11b29c102049678c1640c6091f70c01.png)

multiplied by Na.

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [September 7, 2022, 9:25pm UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/5 "2022-09-07T21:25:29Z")

</div>

Okay, Thanks!

Apart from that,

\*\* I want to get the total PDF, my code is incomplete. Any way to extend to get the total probability?

\*\* Is the normalized PDF [i.e P(x) vs x] the same as a 1D histogram of the “x” weighted by the density?  
As I want to add my E (“x” in the code) distribution to M\*(A-1) so that I will get a M\*(A-1) distribution (not a constant value).

Note: “x means E”.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [September 7, 2022, 9:39pm UTC](https://root-forum.cern.ch/t/adding-gaussian-pdf-in-event-loop/51483/6 "2022-09-07T21:39:45Z")

</div>

To get total probabilities:

First, initialize all `density[j]` to zero.

Later, in your loop over ‘i’, change  
`density[j] = ...`  
with  
`density[j] += ...`
