Creating ROOT executable with input arguments

Hi there,

I’m pretty new to ROOT, and not sure if there’s a simple answer to this, but I figured this forum would be a good place to ask. If I’m mistaken, apologies in advance.

I have a ROOT macro. I can run the macro with an input argument of a configuration file using the ROOT interpreter, but the execution of my program is pretty slow. What I would like to do is compile my macro into an executable that has the ability to read in an argument so I can pass different configuration files.

Any advice is appreciated - I am using version 6.16.00.

Just write the macro with “main” as your main function (instead of using the same as the file name) and pass/use the arguments the same way as for any c++ program --and don’t forget to #include all the needed headers in your code.
https://www.google.com/search?q=c%2B%2B+command+line+arguments

but the execution of my program is pretty slow.

You can simply using ACLiC to automatically compile your code by adding a trailing + to you script name So replacing something like:

root.exe 'myscript.C(args)`
or
root [] .x myscript.C(args)

with

root.exe myscript.C+(args)
or
root [] .x myscript.C+(args)

Hello,

As people has give you the answer, I just want to add a bit more detail.

Here is an example of valid C macro (file name is draw_hist.C)

#include <cstdio>
#include <cstdlib>
#include <TH1D.h>
#include <TCanvas.h>

// use `int main` if you compile with gcc
void main ( int n_args, const char** args )
{
	int n_entries = 10;
	if ( n_args > 1 )  n_entries = atoi( args[1] );
	TH1D* hist = new TH1D( "hist", "hist", 10, 0, 10 );
	for ( int i=0; i<n_entries; i++ )
		hist -> Fill( 3.4 );
	TCanvas* canvas = new TCanvas( "canvas", "", 400, 300 );
	canvas -> cd( );
	hist -> Draw( "hist" );
	canvas -> SaveAs( "plot_hist.png" );
	canvas -> Close( );
	return 0;
}

Then to compile it, you can do:

$ g++ -o draw_hist draw_hist.C $( root-config --libs --cflags )

and then run the compiled program by

$ ./draw_hist 100

Hope this help,
Hoa.

Thanks for the answer, this is exactly what I needed!

1 Like