# Changing some lines in ROOT library

**URL:** <https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668>\
**Category:** Newbie\
**Created:** [November 4, 2019, 4:38pm UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668 "2019-11-04T16:38:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [November 4, 2019, 4:38pm UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/1 "2019-11-04T16:38:14Z")

</div>

* * *

_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

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [November 5, 2019, 8:51am UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/2 "2019-11-05T08:51:22Z")

</div>

Hi,  
you should overloadTF1::GetRandom in your own class  
inheriting from TF1:

```auto
class MyTF1 : public TF1 ....
...
Double_t GetRandom()
{
your code
}

```

MyTF1 should behave exactly as TF1 except GetRandom()

Otto

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [November 5, 2019, 6:05pm UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/3 "2019-11-05T18:05:58Z")

</div>

Hi Otto,

I overloaded in a the header file the class as you suggested in this way:

```auto
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() ?

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [November 6, 2019, 9:21am UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/4 "2019-11-06T09:21:40Z")

</div>

Hi Nicola,  
first of all TF1 has:

```auto
Double_t GetRandom()
Double_t GetRandom(Double_t xmin, Double_t xmax)

```

Att a piece of code.[MyTF1.h](https://root-forum.cern.ch/uploads/short-url/ur5M1HpynCz8y8oshbF2lkgIL1T.h) (517 Bytes)  
Use it in a root session (I use 6.18.04 on U16.04.06)

```auto
.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

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [November 6, 2019, 2:13pm UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/5 "2019-11-06T14:13:41Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/rUch4D8222iQLOUnttUNSiSvSte.h) (295 Bytes) [MyTF1.cpp](https://root-forum.cern.ch/uploads/short-url/79h2nBizaDIGXiK5UQyZvUAZlhO.cpp) (3.2 KB) .

In the source file you can see at line 90:

```auto
   // return random number
   Double_t r = 0.5;

```

Instead of `gRandom->Rndm()`. Then in the main file I define MyTF1 and call GetRandom:

```auto
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:

```auto
#! /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?

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [November 7, 2019, 9:06am UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/6 "2019-11-07T09:06:38Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/qCQqkDu64B1dTO9j9a5xlQqEtkn.h) (451 Bytes)  
With this I get.

```auto
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

---

<div class="post-metadata">

**Author:** ![Nicola\_Rizzi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_rizzi/32/18416_2.png) [@Nicola\_Rizzi](https://root-forum.cern.ch/u/Nicola_Rizzi)\
**Post date:** [November 7, 2019, 10:05am UTC](https://root-forum.cern.ch/t/changing-some-lines-in-root-library/36668/7 "2019-11-07T10:05:14Z")

</div>

It worked! Thank you so much!
