Load and run macro with Perl script

Dear ROOT users,

I have to serialyze an analysis with a Perl script. This is my first Perl script, so I’d like to have a little help! :slight_smile:

I have to use these commands:

root -l

.L C14TotalFit.cc

and, for each rootfile into “rootfile” folder, I have to call my function:

C14TotalFit("/home/rootfiles/nomefile.root", “/home/results/nomefile.root”)

This is the script I created, but it doesn’t seem to work; in particular, I get these errors on the terminal:

[…]$ chmod u+x doC14.pl
[…]$ ./doC14.pl
root -l
.L ‘/home/C14Fit/C14TotalFit.cc
C14TotalFit("/home/rootfiles/filename001.root", “/home/results/filename001.root”)‘
sh: -c: line 0: syntax error near unexpected token "/home/rootfiles/filename001.root",' sh: -c: line 0:C14TotalFit("/home/rootfiles/filename001.root", “/home/results/filename001.root”)’’

Here’s the code:

[code]#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use Cwd;

my $infile_path = ‘/home/rootfiles’; # path of rootfiles
my $outfile_path = ‘/home/results’; # path of results
my $here = getcwd; # current working directory

foreach my $arg (@ARGV){
if (($arg =~ /^help$/i) || ($arg =~/^usage$/i)){
# usage();
exit;
}
elsif ($arg =~ /^infile_path=(\w+)/){
$infile_path = $1;
}
elsif ($arg =~ /^outfile_path=(\w+)/){
$outfile_path = $1;
}
else {
die " ERROR: unknown parameter $arg \n";
}
}

chdir $infile_path or die “Cannot chroot to $infile_path: $!\n”;
my @files = glob($infile_path."/*.root");

my $init = “root -l”;
print $init."\n";

$init;

my $load = “.L '${here}/C14TotalFit.cc”;
print $load."\n";

$load;

root -l

.L C14TotalFit.cc

foreach my $file (@files){

C14TotalFit("/home/rootfiles/nomefile.root", “/home/results/nomefile.root”)

my $cmd = "C14TotalFit(\"";
$cmd = $cmd.$infile_path.'/';
$cmd = $cmd.basename($file);
$cmd = $cmd."\", \"";
$cmd = $cmd.$outfile_path.'/';
$cmd = $cmd.basename($file);
$cmd = $cmd."\")'";
print $cmd."\n";
`$cmd`;

}[/code]

Thank you!!

…perhaps I have to compile it, right?
Ok, if I have my compiled program called C14TotalFit.cc, what lines I have to add to my perl script in order to make it working?

Bye!

Succeeded.

What did you do? It could be useful for future users who will have the same question if you explain what you did :smiley:

Sure! Here’s my cmds:

  1. root -l
  2. .L Macro.cc++
  3. chmod u+x doC14.pl (the name of this Perl script)
  4. done. Notice the “.cc” instead the name of the function (don’t use a non-compiled macro!)

And my script doC14.pl

[code]#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use Cwd;

my $infile_path = ‘/home/rootfiles’; # path of rootfiles
my $outfile_path = ‘/home/results’; # path of results
my $here = getcwd;

foreach my $arg (@ARGV){
if (($arg =~ /^help$/i) || ($arg =~/^usage$/i)){
# usage();
exit;
}
elsif ($arg =~ /^infile_path=(\w+)/){
$infile_path = $1;
}
elsif ($arg =~ /^outfile_path=(\w+)/){
$outfile_path = $1;
}
else {
die " ERROR: unknown parameter $arg \n";
}
}

chdir $infile_path or die “Cannot chroot to $infile_path: $!\n”;
my @files = glob($infile_path."/*.root");

foreach my $file (@files){

/home/C14Fit/Macro.cc("/home/rootfiles/nomefile.root", “/home/results/nomefile”)

my $cmd = "root -l -q -b '${here}/Macro.cc(\"";		# quit root at the end and don't show canvas
$cmd = $cmd.$infile_path.'/';
$cmd = $cmd.basename($file);
$cmd = $cmd."\", \"";
$cmd = $cmd.$outfile_path.'/';
$cmd = $cmd.basename($str);
$cmd = $cmd."\")'";
print $cmd."\n";
`$cmd`;

}[/code]