ROOT Version: 6.18.02
Platform: Elementary (Ubuntu 16.04)
Compiler: g++ 5.5.0 20171010
Hi everybody,
I have a small question about modifying ROOT libraries. In particular I would like to change TF1::GetRandom() in order to make it return a number according TF1 shape but starting from a user defined number between 0-1 (thus without using the pseudo-number generator).
Is changing ROOT code something possible? What I have to do without messing things up?
Thank you in advance for any type of help
Hi,
you should overloadTF1::GetRandom in your own class
inheriting from TF1:
class MyTF1 : public TF1 ....
...
Double_t GetRandom()
{
your code
}
MyTF1 should behave exactly as TF1 except GetRandom()
Otto
Hi Otto,
I overloaded in a the header file the class as you suggested in this way:
class MyTF1 : public TF1
{
public:
using TF1::TF1;
Double_t GetRandom(Double_t);
};
and then in a source file .cpp I coded my “new” GetRandom(), substituting the randomness of the generator with a deterministic (now fixed) value 0.5
Then in my main file I create a myTF1 object but when I call several times MyTF1->GetRandom(0.5) i can still see the full distribution and not a single bin as I expected. Maybe MyTF1 is still using the parent TF1::GetRandom() ?
Hi Nicola,
first of all TF1 has:
Double_t GetRandom()
Double_t GetRandom(Double_t xmin, Double_t xmax)
Att a piece of code.MyTF1.h (517 Bytes)
Use it in a root session (I use 6.18.04 on U16.04.06)
.L MyTF1.h+
MyTF1 *myf = new MyTF1("mm","gaus", -2, 2, "");
myf->SetParameters(10,0, 1);
myf->GetRandom();
myf->GetRandom(-1, 0.5);
myf->GetRandom(-1, 0.5);
I get as expected: 0.55550000, -0.36689724, 0.92648268
So it overloads GetRandom() but not GetRandom(double, double)
Cheers
Otto
Hi Otto,
The code perfectly works on my root as well, but I use g++ instead of root compiler.
Attached you can find the header and the source file for MyTF1 MyTF1.h (295 Bytes) MyTF1.cpp (3.2 KB) .
In the source file you can see at line 90:
// return random number
Double_t r = 0.5;
Instead of gRandom->Rndm()
. Then in the main file I define MyTF1 and call GetRandom:
MyTF1 *inh_rate_PDF = new MyTF1("inh_rate_PDF","ROOT::Math::lognormal_pdf(x,[0],[1])",0,33.86);
inh_rate_PDF->SetParameters(2.8147,0.19);
...
double inh_rate;
inh_rate = inh_rate_PDF->GetRandom();
...
Then I fill an histogram with inh_rate. I expect a single bin since I removed the randomness, but I can still see my distribution, as if the function was not overloaded at all.
Lastly, when I compile my main (macroSobol.cpp) I link also MyTF1 class using the following script:
#! /bin/bash
g++ -o accident macroSobol.cpp `root-config --cflags --libs` source/Source.cpp source/Utility.cpp source/Transport.cpp source/OuterSource.cpp source/Dispersion.cpp source/Dose.cpp source/MyTF1.cpp
./accident
Where’s the problem in the steps I’m following?
Hi Nicola,
in your MyTF1 class you do not provide a constructor.
At least to my knowledge it is needed.
Att the mod file
MyTF1.h (451 Bytes)
With this I get.
root [0] .L MyTF1.cpp+
root [3] auto inh_rate_PDF= new MyTF1("inh_rate_PDF","ROOT::Math::lognormal_pdf(x,[0],[1])",0,33.86);
root [4] inh_rate_PDF->SetParameters(2.8147,0.19);
root [5] inh_rate_PDF->GetRandom();
root [6] inh_rate_PDF->GetRandom()
(double) 16.687753
root [7] inh_rate_PDF->GetRandom()
(double) 16.687753
root [8] inh_rate_PDF->GetRandom()
(double) 16.687753
root [9] inh_rate_PDF->GetRandom()
(double) 16.687753
root [10] inh_rate_PDF->GetRandom(10,30)
(double) 29.983647
root [11] inh_rate_PDF->GetRandom(10,30)
(double) 13.868303
So GetRandom() returns always 16.687753 (dont know if the number is right)
and GetRandom(from,to) is really random as expected.
Otto
It worked! Thank you so much!