# Write a subfunction to integral

Hi rooter,
I want to make an integration of my functions after I use them to fill the original data sets. It is a little bit tedious because I have 4 histograms x 8 different data sets. I want to simply write a subfunction so that right after I obtain these parameters from the fitting, I could got some numbers as a result of the integration. However, I totally got lost how to write a subfunction, especially I have little experience on C++

Hi,

I am not sure what you mean by subfunction. Could you clarify?
Are you trying to obtain a 1D function F which is the integral of f after f has been fit on a histogram?

Cheers,
Danilo

Sorry for the unclear expression. I tried to obtain some numbers from the integrating functions. Specifically, I want to integrate something like f= exp(-xx/2.//)+exp(-xx/2.//), and these 4 parameters are from fitting the data(4 histograms per data sets, there are 8 data sets). I wonder whether I could write a function, F= something, that after I fit the data and got parameters I could simply set these parameters in F and it will directly output some numbers for me.

Hi,
should be quite easy, after the fit you have to call

``````f->Integral(a,b);
``````

Cheers
Stefano

Ps

You can use also TMath for define the sum of the gaussian, at least for me the code is more readable.

``````TF1 *f = new TF1("f"," *TMath::Gaus(x,0,) + *TMath::Gaus(x,0,)",0,10);
``````
1 Like

Hi,
Thanks for the suggestion, I tried this method and it could work, just a little bit tedious because I have 4 histograms per data sets, and each histogram is described by four functions. I need each function’s contribution so that I need define a lot of functions and repeat the similar code again and again. I just wonder whether there is a better way to write a new function so that I could simply set different parameters to this function and then obtain the numerical result.

Ok,
If I understood well you have
8 data sets
from each of this 8 data sets you create 4 histograms
and each of this histograms is fitted with 4 functions.

So you have like 128 functions.

Now, do you want is something that for each of the 8 data set give you a number, so 8 number, or something that runs on all the data sets and then gives you a number?

Stefano

Indeed, I need 16 numbers directly from the functions by fitting these histograms, and then 16x8 numbers in total(8 data sets). I could do it one by one, but the process will be extremely tedious.

Hi @HCY,
sorry for the late answer but I was busy.

What you do usually is write a macro like DoSomeStuff.c
and then you wrote in it a main function called with the same name like

``````void DoSomeStuff(){
...
...
}

``````

In this way from the root interactive session you can do

``````root  .x DoSomeStuff()
``````

you can declare another function that can return a double or nothing,

where you fit the histogram with the function and then do the integral and returns the value of the integral let’s say

``````double FitAndIntegrate(TH1D* h, TF1 *f)
{
h->Fit(f,"LR");
f->Integral(a,b);
}
``````

And now you can recall this function in your main function as many times you want.

``````void DoSomeStuff(){
...

printf("Integral f = %lf\n",FitAndIntegrate(h,f));
printf("Integral f1 = %lf\n",FitAndIntegrate(h,f1));
printf("Integral f2 = %lf\n",FitAndIntegrate(h,f2));
printf("Integral f3 = %lf\n",FitAndIntegrate(h,f3));
...

}

``````

And of course run in it over the different histograms, if you want do this using for you should declare
vector of TH1D pointer and TF1 pointer

to launch the function inside for

``````void DoSomeStuff(){

for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
printf("Integral f = %lf\n",FitAndIntegrate(h[i],f[j]));

}

``````

Cheers,
Stefano

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.