Import variables from linux script into a root macro

I am processing a file in a linux script. Inside that script, I am creating some variables and I want to import them to root. The file to be processed has the format

Kmax Event File - Text Format 1 4 1000 65 4121 9426 12312 56 4118 8882 12307 1273 4188 8217 12309 1291 4204 8233 12308 1329 4170 8225 12303 1341 4135 8207 12306 63 4108 8904 12300 60 4106 8897 12307 731 4108 8192 12306 ... ÿÿÿÿÿÿÿÿ

The script(named evnt2dat) is the following

[code]#!/bin/bash

if test $1 ; then
if [ -f $1.evnt ] ; then
rm -f $1.dat
sed -n ‘2p’ $1.evnt | (read v1 v2 v3
for filename in $1*.evnt ; do
echo -e “Processing file $filename"
sed ‘$d’ < $filename > $1_tmp
sed -i ‘/Kmax/d’ $1_tmp
sed -i ‘/^’”$v1"’ ‘"$v2"’ /d’ $1_tmp
cat $1_tmp >> $1.dat
done
v3=wc -l $1.dat | awk '{print $1}'
echo -e “$v1 $v2 $v3” > .$1.dat
rm -f $1_tmp)
else
echo -e "\a!!!"
echo -e " Event file $1.evnt doesn’t exist !!!"
echo -e "!!!"
fi
else
echo -e "\a!!!"
echo -e "!!! Give name for event files !!!"
echo -e "!!!"
fi
awk -v channels=4096 ‘NR<=2{next}{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{detectors=NF;}1’ $1.dat >$1_Processed.dat
rm -f $1.dat
exit 0[/code]

As you can see I am creating the variables channels and detectors. I tried to use them-in a raw manner- inside root but with no luck at all. My macro is the following

[code]#include "Riostream.h"
void AutoNTuple(char * file_c) {

TString file(file_c);
gSystem->Exec(TString::Format("./evnt2dat %s",file.Data()));
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“AutoNTuple.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(TString::Format("%s%s_Processed.dat",dir.Data(),file.Data()));

printf(“There are %d detectors”,detectors);
printf(“There are %d channels”,channels);
}[/code]

This code doesn’t work;root cannot understand the variables detectors and channels. Is there a wa y to import them into root from the linux script?

Hi,

in general, such a text manipulation is a C++ exercise and is loosely coupled to ROOT.
What exectly do you want to manipulate in ROOT in the file?

Kmax Event File - Text Format
1 4 1000 
65 4121 9426 12312 
56 4118 8882 12307 
1273 4188 8217 12309 
1291 4204 8233 12308 

In general, for this kind of operations I would suggest to use pyrthon and PyROOT.

Cheers,
Danilo

I am not interested in text manipulation. I have manipulated my data outside root, using awk.
My question is on how to import a variable I have defined and assigned a value in it, in root.

Check this line of my code(it’s the 4th counting from bottom

There I am defining two variables

[ul]channels[/ul]
[ul]detectors[/ul]

I want those two variables to be imported in root.
Is this possible?

Hi,

if you have already preprocessed your data and you have it parsed and at disposal in your c++ application, the next natural step to take advantage of the ROOT data analysis and persistency facilities would be to use a TTree or, maybe more suited for this case, a TNtuple.
A decent point to start seeing this concepts in action is this tutorial:
root.cern.ch/root/html/tutorials … sic.C.html

Cheers,
Danilo

If you look at my first post, I have a code that does exactly this.
I have already have a code that makes the ntuple.

My question is different to that, though.
In my Ntuple code, I am using the same variables that I have defined in my linux script to process the data.
So it would be rather convenient to define the variables only once, preferably in the linux script.

The root code I am providing is just a test code to check whether the variables are parsed/imported into root. Check this thread to see my macro code.

[url]Create(and fill) histograms and ntuple using a for loop

Sorry I don’t get what we are trying to achieve here. Probably I don’t get the meaning of “automated way”.
To recap:
o You have your nicely formatted input data as text file.
o You have the code which uses ROOT to fill the histograms to do your analysis.
What is exactly missing?

To do the processing I need to define some variables, which I do in the script.
To create the ntuple, I want to use the exact same variables(that will have the exact same value) in my root code, so I have to redefine(and reassign) the exact same variables.

That’s why I need to import the variable to root.
Is it clear enough now?

So you mean importing in variables as names in a txt file in C++?

I mean a variable I have in a linux script( that is executed with root’s gSystem() ) to be imported in the root code.

For instance this code runs the script

I want to be able to directly use(without having to redefine the variables)

printf("There are %d detectors",detectors); printf("There are %d channels",channels);

where “channels” and “detectors” are variables that have been defined in the linux script.

If you want to catch the output of your shell script than you have to use the GetFromPipe method instead of the “Exec” one.

Thank you very much for your answer!
I tried

instead of

but again it didn’t work. I got the following error message

[quote]thanos@thanos-laptop:~/Desktop$ root 'AutoNTuple.C(“c+Au”)'
root [0]
Processing AutoNTuple.C(“c+Au”)…
Error: Symbol detectors is not defined in current scope AutoNTuple.C:12:
*** Interpreter error recovered ***
[/quote]

In your shell, what is the output of your script?
The GetFromPipe method returns a TString which contains the output of your script. Then you have to parse this TString to extract your variables (“detector” and so on).

My output is a .dat file that root uses to make the ntuple.
I also define the variables “detectors” and “channels” as well as assigning them values.
I used a print command in awk(in my script) to check if it’s working properly and my variables indeed have a value assigned.

But those variables(as well as their value) cannot pass to root, giving me the error message I wrote above.

I meant what is displayed in your terminal when you type

I just get my processed file.
If instead of

END{detectors=NF}

I write

END{printf "There are %d detectors\n ",detectors=NF;}

I get my processed file with the message