Home | News | Documentation | Download

Calling root from bash script

Dear root experts,
I have several root macros which I want to compile and run in root. I have made a bash script for it.
The simple example of the script is
Name:RunMacro_1_to_3.sh
and it contains following lines

root -b -l <<EOF
.L Job_PsiRatio_Eff_PP_Numbers_1.C+g
PsiRatio_Eff_PP_Numbers_1();
.L Job_PsiRatio_Eff_PP_Numbers_2.C+g
PsiRatio_Eff_PP_Numbers_2();
.L Job_PsiRatio_Eff_PP_Numbers_3.C+g
PsiRatio_Eff_PP_Numbers_3();
EOF

To run it, I do in command prompt ./RunMacro_1_to_3.sh

It runs successfully but I want to include one more functionality. I want the script to quit root (.q) after
finishing first macro then call it again compile the second one, run the second one and so on.
My macro is simply doing some calculations and writing the results in a text file.

Please suggest some way to do it.

with regards,
Vineet

I would write your script the following way:

root -b -l -q <<EOF
.L Job_PsiRatio_Eff_PP_Numbers_1.C+g
PsiRatio_Eff_PP_Numbers_1();
EOF
root -b -l -q <<EOF
.L Job_PsiRatio_Eff_PP_Numbers_2.C+g
PsiRatio_Eff_PP_Numbers_2();
EOF
root -b -l -q <<EOF
.L Job_PsiRatio_Eff_PP_Numbers_3.C+g
PsiRatio_Eff_PP_Numbers_3();
EOF

or simpler if your file and macro name are the same:

root -b -l -q Job_PsiRatio_Eff_PP_Numbers_1.C+
root -b -l -q Job_PsiRatio_Eff_PP_Numbers_2.C+
root -b -l -q Job_PsiRatio_Eff_PP_Numbers_3.C+

Dear Couet,
The second method works. Thanks a lot for your help.

with best regards,
Vineet

Dear Couet,
I try to use this method, it works, but in my macro I need to show an histogram and it didn’ see.
How can I show my histogram using a bash script?
Kind regards,
Daniele

Hi Daniele,
The “-b” option means “run in batch mode without graphics” (type root --help to see the options), so removing it will display your graphics. However, when root quits ("-q") any canvas will be closed too, so depending on your macro you may just see the graph quickly pop up and disappear. If you want to keep the canvas you can save it from within the macro, or maybe have your macro wait for some user input before finishing, to give you time to see the histogram.

1 Like

Dear dastudillo,
Many thanks for your support and cooperation.
Kind regards,
Daniele

Dear,
I tried to delete the -b option but I don’t see yet the graphics.
I launched a script containing the following commands:

root -l <<EOF
.L ShowCR_DOY_P.c
Show(“CR distribution Selected Bin - Poisson Fit, 20180801-20181015”, “213cr_507
.dat”);
EOF

What I can do?
Thanks so much in advance for your support,
Regards,
Daniele

Usually, in batch mode I just directly run the macros, something like root -l -q “mymacro.C(myarg)” or similar, without loading, compiling, etc, so I’m not really familiar with the way you are loading and running your code; as couet’s example looks similar, it should work, so your case could be a problem with your code (ShowCR_DOY_P.c).

A first question would be, do you see any graphics at all with that code when not using batch mode? try this: just go into root and run your macro from there and see if you get the graphics; if you don’t, the problem is something else in your code.

Do root -l ShowCR_DOY_P.c -e 'Show("CR distribution Selected Bin - Poisson Fit, 20180801-20181015", "213cr_507.dat");' instead. In heredoc approach, root exits when the Show function returns.

I have deleted that user - he was just using an excuse to post his spam links…