Template:ROOT/Exercises/Interpreted ROOT Macros

Exercise

 * 1) Consider the problem solved in the previous exercise. Now write a script that does the same thing and execute it as interpreted macro. Does it run faster?
 * 2) Modify the script in a way that the user can pass the amount of random numbers to be created as a parameter.
 * 3) Overload the macro such that the amount of numbers can still be passed but if no parameter is given, 100 000 000 numbers will be generated.

Part 1
Create the following file and name it

Now open a ROOT session in this directory and say

Between a half and one minute later you shall get:

The script isn't any faster than the code entered directly to the ROOT command line because in either case it is run as interpreted code by CINT.

Part 2
This takes only a minor change of the script file:

Now you can compute the mean of&mdash;for example&mdash;20 numbers:

Note that calling without a parameter will fail:

Part 3
To allow calling with and without parameter, we overload the function with one that takes no parameter and does nothing than calling the other one with the default parameter of 100 millions.

Now both calls work:

Actually, this shouldn't be very exciting. It's just the C++ language. But it's a nice thing to see that it works just as expected inside the ROOT framework.

/Exercises