# Running root macros with multiple (2+) arguments from the shell/Terminal directly from a script

**URL:** <https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433>\
**Category:** Newbie\
**Tags:** root\
**Created:** [December 7, 2023, 11:38am UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433 "2023-12-07T11:38:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 7, 2023, 11:38am UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/1 "2023-12-07T11:38:02Z")

</div>

I am trying to run a macro that has several arguments from a terminal through a script. The point of it all is that I want to run the macro for several input files which are in different directories while passing the values or some components of the macro arguments via the shell script arguments. Please see the detailed script below.

```auto
#!/bin/bash

# Function to check if input is a valid integer
is_number() {
    re='^[0-9]+$'
    if ! [[$1 =~ $re]] ; then
        echo "Error: Not a number" >&2; return 1
    else
        return 0
    fi
}

# Prompt user for input range and other parameters
echo "Please enter the required parameters for the macro execution."

# Get range from the user
read -p "Enter start 'day': " start_range
read -p "Enter end 'day': " end_range

# Validate range input
if ! is_number "$start_range" || ! is_number "$end_range" ; then
    echo "Invalid input. Please enter numeric values."
    exit 1
fi

# Get version of S1_ROFsByday
read -p "Enter the version of S1_ROFsByday list files: " s1_version

# Get the other parameters
read -p "Enter the number of events to be analysed (nEvents): " nEvents
read -p "Enter the tree name (treeName): " treeName
read -p "Enter the primary vertex code (PV): " PVID
read -p "Enter the event selection code (ES): " ESID

# Loop over the range
for i in $(seq $start_range $end_range); do
    # Construct the input and output file names
    input_file="/analysis/LambdaRecon_git_repos/dstlistFiles/dataDSTs/feb22_gen02/S1_ROFsByday_lists_${s1_version}/S1_0${I}_ROFs.list"
    output_filePath="/LambdaRecon_git_repos/Data_gen02DST_outs/stagelevel_analysis/stage2/dEdx1_PV${PVID}_ES${ESID}"
    output_file="S2_CutSel_S10${s1_version}_PV_${PVID}_ES_${ESID}_0${I}_out"
    output_file_param="${output_filePath}/${output_file}.root"

    # Construct the macro command
    macro_command="S2_LL_CutBased.C+\(\"${input_file}\", \"${output_file_param}\", ${nEvents}, \"${treeName}\", ${PVID}, ${ESID}\)"

    echo $PWD

    echo ${macro_command}

    echo "root -l -q ${macro_command} 2> \"${output_filePath}/${output_file}.strace\""

    # Run the macro with ROOT and redirect stack trace
# root -l -q '${macro_command}' 2> \"${output_filePath}/${output_file}.strace\"
    root -l -q ${macro_command}
done

```

Essentially, I want to run the analysis macro over data from several days so I pass start\_range = 34 and end\_range = 42 which will analyse all the data (i.e. root files) in the folders for these specific days.

But the root does not execute the `root -l -q ${macro_command}` and throws an error. I believe, this is likely due to a problem with how the string for the `root` command is being constructed in the script.  
If you have any suggestions or corrections will be deeply appreciated. This is kind of an urgent issue to be fixed.

Or if you have an entirely different idea to execute the program, that will also help.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [December 7, 2023, 12:12pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/2 "2023-12-07T12:12:15Z")

</div>

> [@elemental](#):
>
> and throws an error.

What is the error?

Philippe.

PS. You could also write the whole code in C++ 🙂

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 7, 2023, 1:13pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/3 "2023-12-07T13:13:58Z")

</div>

```auto
Warning in <TApplication::GetOptions>: macro 'S2_LL_CutBased.C not found
Warning in <TApplication::GetOptions>: macro "analysis/LambdaRecon_git_repos/Data_gen02DST_outs/stagelevel_analysis/stage2/dEdx1_PV5_ES2/S2_CutSel_S103_PV_5_ES_2_042_out.root", not found
Warning in <TApplication::GetOptions>: macro "t_cands", not found
Warning in <TApplication::GetOptions>: macro 5, not found
Warning in <TApplication::GetOptions>: macro 2)' not found

```

for the values of the variables:

```auto

${i} = 42
${s1_version} = 3
${PVID} = 5
${ESID} = 2
${treeName} = "t_cands"

```

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 7, 2023, 1:32pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/4 "2023-12-07T13:32:20Z")

</div>

> [@pcanal](#):
>
> PS. You could also write the whole code in C++ 🙂

Unfortunately. the macro is too long with few thousand lines. It runs without any error when I execute `root -l -q 'S2_LL_CutBased.C+(arg1, arg2, arg3, arg4, arg5)'` individually with specific variables.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [December 7, 2023, 8:59pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/5 "2023-12-07T20:59:38Z")

</div>

It must be an issue with quotes in bash. It thinks the single quote is part of the filename. Try maybe like this, it works for me:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/3/9/3939b8b5f131a976359273a0a55c0b86730e9352.png)

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 8, 2023, 5:02pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/6 "2023-12-08T17:02:41Z")

</div>

> [@pcanal](#):
>
> PS. You could also write the whole code in C++ 🙂

Do you mind showing how to do this with C++ ?

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 8, 2023, 5:26pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/7 "2023-12-08T17:26:45Z")

</div>

Hi ferhue,

I tried your suggestion with the following command:

` root -l S2_LL_CutBased.C+(\"${input_file}\", \"${output_file_param}\", ${nEvents}, \"${treeName}\",${PVID}, ${ESID}) -b -q`

I get the following error, Not sure what I am doing wrong here.

```auto
bash: /lustre/hades/user/gappager/gandharva/analysis/LambdaRecon_git_repos/lambda_recon_withkinfit/scripts/run_macros_S2.sh: line 56: syntax error near unexpected token `('
bash: /lustre/hades/user/gappager/gandharva/analysis/LambdaRecon_git_repos/lambda_recon_withkinfit/scripts/run_macros_S2.sh: line 56: ` root -l S2_LL_CutBased.C+(\"${input_file}\", \"${output_file_param}\", ${nEvents}, \"${treeName}\", ${PVID}, ${ESID}) -b -q'

```

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [December 8, 2023, 5:28pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/8 "2023-12-08T17:28:37Z")

</div>

Because anything goes after `root` are considered arguments of the command, you should put you `${macro_command}` in quotes (to make it a single argument). By doing this, you can remove the escape for the parentheses  
you can try:

```auto
    macro_command="S2_LL_CutBased.C+( \"${input_file}\", \"${output_file_param}\", ${nEvents}, \"${treeName}\", ${PVID}, ${ESID} )"
    root -l -q "${macro_command}"

```

* * *

Here is an example

my C code: (`hello.C`)

```auto
#include <cstdio>
#include <TFile.h>

void hello ( const char* name )
{
	printf( "hello %s\n", name );
}

```

my run script (`run.sh`)

```auto
#!/bin/bash

name=$1
command="hello.C+( \"$name\" )"
root -l -b -q "$command"

```

running the script:

```auto
$ ./run.sh "tony stark"

Processing hello.C+("tony stark")...
hello tony stark

```

if you don’t have the quotes around `$command`, then bash will “see” the following arguments:

1. hello.C+("tony
2. stark"  
which are not valid argument for root.

---

<div class="post-metadata">

**Author:** ![elemental](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elemental/32/34841_2.png) [@elemental](https://root-forum.cern.ch/u/elemental)\
**Post date:** [December 10, 2023, 10:26pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/9 "2023-12-10T22:26:53Z")

</div>

Thanks @LongHoa for a detailed explanation. I tried you suggestion and the script now works as I intend it to. Cheers.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [December 11, 2023, 7:50am UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/10 "2023-12-11T07:50:51Z")

</div>

The solution I suggested before is simpler and also works:

```auto
#!/bin/bash

root -l -b -q hello.C+( \"$1\" )

```

But you need to call

`source run.sh`

instead of

`./run.sh`

to avoid the error message you mentioned in the previous post.

“source” and “dot” use different shell programs, with different quote behavior. (At least on Ubuntu 22).

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [December 11, 2023, 4:35pm UTC](https://root-forum.cern.ch/t/running-root-macros-with-multiple-2-arguments-from-the-shell-terminal-directly-from-a-script/57433/11 "2023-12-11T16:35:40Z")

</div>

> [@elemental](#):
>
> Thanks @LongHoa for a detailed explanation. I tried you suggestion and the script now works as I intend it to. Cheers.

Glad that I can help.
