for (int i=1; i<=100; i++)
TF1 f1 = new TF1(āf1ā,"(2.0[0][0][1][1][2][2][3][3])/(xxsin([4][5]/360.0)sin([4][5]/360.0)+2.0*[3]*[3]*x))", 0.0025, 1.0);
i am in the state of confusion because it is not the random number that i am going to average but the function f1ā¦ but maybe it means the same for linear function but for quadratic or higher? (let me check)
You donāt need a TF1 for this; just use a variable (say, f1) that is calculated with your formula using all the constant parametres that you have, and changing āxā in the loop:
Double_t p0=1.0, p1=1.0, p2=1.0/137, ...;
Double_t x, f1; // for each sample
Double sum=0, avr=0;
for (Int_t i=0; i<100; ++i) {
x = gRandom->Gaus(0.938272081,0.000000023);
f1 = 2.0*p0*p0*.../(x*x...);
sum += f1;
}
avr = sum/100;
If you must use TF1, have a look at Eval(). Define the function and params outside the loop (like you did originally), and in the āforā loop youāll only need to generate āxā and use it in f1->Eval(ā¦ to get the value (as āyā).
Note that your last line, f1->Draw(), will draw f1 within the limits you specified at creation, and with a smooth line, independently of the sampling you do; not sure if this is what you want, but be aware of this
It was not clear to me that you wanted the average of the function. I thought you wanted the average of the random number.
Look at the code @dastudillo suggested. It computes the average of a function f1. You do no need a TF1 for that. Where does this āother variableā appears ? can you elaborate @dastudillo example to show what you mean ?
Ok, I think I understand a bit better. Generate param[3] randomly and use it (as well as the other params) to create a function f1 from 0.0025 to 1.0 (x, āfilled inā by ROOT); then, generate another param[3] to create another f1, and repeat 100 times. So this creates 100 functions. In this case the question is: what is the āaverageā we need to obtain? How do you define the āaverageā of all these 100 functions?
This will create 100 functions (f0 to f99):
thanks @dastudillo
here, we now have 100 functions and for each of them we can generate a plot but i only need one curve that is the average of the 100 plots.
Hi,
jbmagallanes wants to plot the average of the 100 functions,
so at every point x one needs the mean(f(x))
Now the question is (given parameters a: variable, b: fixed)
Does the following hold:
For any x:
mean(f(x, a, b)) = f(x, mean(a), b)
If f(x) does not depend linearly on a this is obviously wrong
(try with f(x) = a * x*x)
So the only way I see is construct a TGraph with appropriate
number of points and fill with mean(f(x, a, b))
I am afraid there is a basic misunderstanding here:
f1 or fn is a pointer to a TF1 (function object) and n o t the result
i.e a real number of an evaluated formula.
By the way it should be TF1 *f1=new..
What is āvpiā in parameter 5, is it TMath::Pi() = 3.14ā¦?
Why do you use 5 parameters if only 1 (3) is variable?
yeahā¦ vpi=TMath::Pi().
also, is TF1 *fn=new... and TF1 *fn=new...
the asterisk did not show earlier.
those parameters are āconstantsā except for [3] which have to be a random number in addition to the variable x ranging from 0.0025 to 1.0)
do {
āTF1 *fn = new TF1(āfnā,"(2.0*[0]*[0]*[1]*[1]*[2]*[2]*[3]*[3])/(x*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+2.0*[3]*[3]*x))",0.0025,1.0);
fn->SetParameter(0,1.0);
fn->SetParameter(1,1.0);
fn->SetParameter(2,1.0/137.0);
fn->SetParameter(3,gRandom->Gaus(0.938272081,0.000000023));
fn->SetParameter(4,10.0);
fn->SetParameter(5,TMath::Pi());
fn->SetLineColor(kBlue);
fn->SetLineWidth(2);
"TF1 *f1 = new TF1(āf1ā,"(1/i) (f1 (i-1)+fn)ā,0.0025,1.0);
} while (int i=1; i<=100; i++);
f1->Draw();
Hi
before doing the averaging effort one should check if plotting
the possible result makes sense.
Attached your function plotted with parameter 3 at center of the proton mass
(in whatever units) and with 100 standard deviations (second argument of Gaus) away
Even then the curves overlap, so plotting doesnt show anything interesting
(if your function is correct)
Otto
it is not the whole function.
i purposely cut it because that function is very long.
what i just wanted was the procedural code on getting the an average curve of 100 plots.
i hope you can help me.
thanks