]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/EMCAL/QA/macros/runEMCALQA.pl
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGPP / EMCAL / QA / macros / runEMCALQA.pl
1 #!/usr/bin/perl -w
2 # runEMCALQA.pl v.0.5 A.SHABETAI alexandre.shabetai@cern.ch
3
4 use Cwd 'abs_path';
5 use File::Basename;
6 use File::Path qw(mkpath );
7 use File::Copy;
8 use Getopt::Long;
9
10 use strict;
11 use warnings;
12
13 ##########################################################
14 # What this script is doing and how to use it
15
16 sub usage()
17 {
18  print STDERR << "EOF";
19  This script runs the EMCAL QA macros on selected runs.
20
21  usage: $0 --period <PERIOD> --pass <PASS> [ --gridprefix <GRIDPREFIX> ] [--extraGridPath <EXTRAGRIDPATH> ] [ --runlist <runlist.txt> ] [ --col <pp||PbPb> ] 
22
23  -h --help       : shows this (help) messag
24  
25 The following parameters are mendatory: 
26  --period        : the running period to consider                                                                                                         (ex: LHC1XX                  )
27  --pass          : the production pass to use (use "simu" for simulated data)                                                                             (ex: pass1                   )    
28
29  You may specify the following options if you want to change their default values: 
30  --runlist       : a text file with the list of runs to consider - one run by line   (default: <period>-<pass>-runlist.txt                              ) (ex: MyLHC13arunlist.txt     )
31  --extraGridPath : extra grid path to use                                            (default: none                                                     ) (ex: ESDs/QA68               )
32  --localpath     : path where the existing local QAresults files are stored          (default: <period>/<pass>                                          ) (ex: <a_prefix>              )
33  --savePlots     : save the plots as image files (0: no ; 1:png ; 2: png + pdf)      (default: 1 (png)                                                  ) (ex: 0                       )   
34  --debug         : debug flag (0 or 1)                                               (default; 0                                                        ) (ex: 1                       )
35  --gridQAname    : basename QA filename on the grid (without the .root suffix)       (default: QAresults                                                ) (ex: QAresults               )    
36  --gridTimeOut   : timeOut for grid acces                                            (default: 10 s                                                     ) (ex: 10                      )
37  --gridprefix    : the begining of the grid path to use                              (default: /alice/data/<year>/<period> - or .../sim/... if pass=simu) (ex: /alice/data/201X/LHC1XX )
38  --filter        : filter the input file                                             (default: 0 (no)                                                   ) (ex: 1                       )
39  --treeSelections: filename of file containing additional cuts on the trending tree  (default: none                                                     ) (ex: MySelection.C           )
40  --trigger       : force to use the specified trigger name                           (default: trigger names are automaticly read from the input files  ) (ex: trigEMC                 )
41  --col           : force the type of colisions pp or PbPb                            (default: pp - or PbPb if period ends with "h"                     ) (ex: PbPb                    )      
42                          
43  NB: all switches can be abreviated ex: --period is valid as well as -pe
44
45   example: $0  --period LHC13a --pass pass1 
46 EOF
47
48 exit;
49
50 }
51
52 #############################################################################
53 # the values above should not be changed (please use the command lune options instead)
54
55 my $help;
56 my $period;
57 my $pass;
58 my $gridprefix;
59 my $localpath;
60 my $runlist;
61 my $col;
62 my $extragridPath;
63 my $savePlots = 0;
64 my $debug = 0;
65 my $filter = 0;
66 my $trigger = ""; 
67 my $GridTimeOut = 10;
68 my $gridQAname = "QAresults";
69 my $treeSelections = ""; 
70
71 #############################################################################
72 # get options 
73
74 GetOptions ( 
75  "help!" => \$help,
76  "period=s"  => \$period, # string
77  "pass=s" => \$pass, # string
78  "gridprefix=s" => \$gridprefix, #string
79  "localpath=s" => \$localpath, #string
80  "runlist=s" => \$runlist, #string
81  "col=s" => \$col, #string
82  "extraGridPath=s" => \$extragridPath, #string
83  "savePlots=i" => \$savePlots, #int
84  "debug=i" => \$debug, #int
85  "filter=i" => \$filter, #bool
86  "triggerForced=s" => \$trigger,
87  "gridTimeOut=i" => \$GridTimeOut, #int
88  "gridQAname=s" => \$gridQAname, #string
89  "treeSelections=s" => \$treeSelections) #string
90  or die("Error in command line arguments\n");
91  usage() if ($help);
92  die "Missing --period! (you can get help with $0 -h)" unless $period;
93  die "Missing --pass! (you can get help with $0 -h)" unless $pass;
94
95 if ($pass eq "sim") {$pass= "simu";}
96 if (!(defined($runlist))) {$runlist = "$period-$pass-runlist.txt";}
97 if (!(defined($col)) && index($period, 'h') != -1) {$col = "PbPb";}
98 if (!(defined($col))) {$col = "pp";}
99
100 if (!(defined($gridprefix))) {
101  my $year=sprintf("20%s",substr($period, 3, 2));
102  if ($pass ne "simu") {$gridprefix = "/alice/data/$year/$period";} else {$gridprefix = "/alice/sim/$year/$period";}
103 }
104 if (!(defined($extragridPath))) {
105   if    (($pass ne "simu") && (index($pass, 'c') != -1)) { $extragridPath = $pass;}
106   elsif ($pass ne "simu") { $extragridPath = "ESDs/$pass";}
107   else  {$extragridPath = "";}
108  }
109
110 ##############################################################################
111 # the values above should not be changed
112
113 my $macroDir = abs_path(dirname($0));
114 my $locDir = "$period/$pass";
115 $ENV{QAPATH} = "$locDir/";
116 my $processedrunlist = "$locDir/processed.txt";
117 my $notprocessedrunlist = "$locDir/notprocessed.txt";
118 my $exit_code = 0;
119
120 my $calo = "EMCAL";
121 my @gridsuffixes = qw(.root);
122 if (index($pass, 'c') != -1) {@gridsuffixes = qw(_barrel.root _outer.root);}
123 if (!(defined($localpath))) {$localpath = $locDir;} else {$localpath = $localpath."/";}
124
125 ###############################################################################
126 # sanity checks
127
128 if (! -e $locDir) {mkpath($locDir);}
129 if ((defined(glob("$locDir/*data.txt")))) {unlink(glob("$locDir/*data.txt"));}
130 my $includePath = qx{aliroot -b -l << EOF
131 .include
132 EOF 
133 };
134 if (index($includePath,"$ENV{ALICE_ROOT}/include") == -1) 
135   {print "Please add \$ALICE_ROOT/include to your include path using a rootlogon.C of .rootrc "; exit;}
136
137 my $ALIEN_ROOT = $ENV{'ALIEN_ROOT'};
138 if (!(defined($ALIEN_ROOT))) {die "ALIEN_ROOT not defined!";}
139 my $proxyValidity = qx{$ALIEN_ROOT/api/bin/xrdgsiproxy info 2>&1 | grep "time left" | cut -d " " -f 6 | cut -d: -f1}; chomp($proxyValidity);
140 if (!(defined($proxyValidity)) || $proxyValidity eq "0h") {  die "No valid proxy found (or proxy valid for less that one hour). Nothing done!"};
141 my $isValidToken = qx{$ALIEN_ROOT/api/bin/alien-token-info | grep -c "Token is still valid"};
142 if ($isValidToken==0) { die "No valid token found. Nothing done!";}
143 #############################################################################t
144 # start working
145
146 open(STDOUT, "| tee -i \"$locDir/$period\_$pass\_EMCALQA.log\"");
147 open(STDERR, '>&STDOUT');
148 open(FIN, $runlist) or die "Could not open $runlist: $! please specify --runlist of create $runlist";
149 open(FOUT, ">$processedrunlist") or die "Could not open $processedrunlist: $!";
150 open(FOUT2, ">$notprocessedrunlist") or die "Could not open $notprocessedrunlist: $!";
151
152 print "\n Executing $0";
153  foreach (@ARGV)  { print " $_";} 
154 print " ...\n";
155
156 # read the run list
157 my @runs;
158 while( my $line = <FIN>)  {
159     chomp($line);  
160     my @fields = split(/,/, $line);
161     push @runs, @fields
162 }
163     @runs = sort { $a <=> $b } @runs;
164
165 # create the directory structure and copy the output of the QA train from the grid
166     foreach my $run (@runs) {   
167     $run =~ tr/ //ds;
168     if($pass ne "simu") {$run = sprintf("%09d", $run);}
169     print "\nProcessing run $run...\n";
170     foreach my $suffix (@gridsuffixes) {
171      my $gridFile = "alien://$gridprefix/$run/$extragridPath/$gridQAname$suffix"; 
172      my $shortrun = $run; $shortrun =~ s/^0+//; $shortrun =~ s/ +$//; 
173      my $locFileName = "$shortrun$suffix"; 
174     
175      print "\n Processing file $locFileName...\n";
176     
177      if (! -e "$locDir/$run") {mkdir("$locDir/$run");} #
178
179 if ($localpath eq $locDir) {
180      for (my $trial = 1; $trial <= 2; $trial++)  
181      {  
182  
183       system("root -b -l -q \'$macroDir/CopyQAFile.C+g\(\"$gridFile\",\"$localpath\",kFALSE,\"$locFileName\",$GridTimeOut\)\'");  
184       $exit_code = $?>>8;
185     
186      if (! $exit_code) { print "\nFile $gridQAname$suffix processed succesfully (run $shortrun, try $trial)\n";last} else { print "\nThe processing of file $gridQAname$suffix (run $shortrun) failed! (try $trial)\n";};
187      if ($exit_code eq 252 || $exit_code eq 253) {unlink("$localpath/$locFileName"); }
188     }  
189  }
190 # run the run level QA for the runs that were selected (i.e in the runlist and that do have a valid local QA output filie).
191
192  if (!$exit_code) {
193
194   if (-e "$localpath/$locFileName" )  { 
195
196    system("aliroot -b -q \'$macroDir/CreateEMCALRunQA.C+g\(\"$localpath/$locFileName\",\"$run\",\"$period\",\"$pass\",$savePlots,$filter,\"$trigger\",\"$col\",\"$calo\"\)\'"); 
197    $exit_code = $?>>8;
198    
199    if (! $exit_code) {  print FOUT "$shortrun\n"; print "\nCreateEMCALRunQA() for file $gridQAname$suffix processed succesfully (run $shortrun)\n";} else { print FOUT2 "$shortrun\n" ; print "\nThe processing of CreateEMCALRunQA() for file $gridQAname$suffix failed! (run $shortrun)\n";};
200   
201  if ($suffix ne ".root") { move("$locDir/$run/trending.root", "$locDir/$run/trending$suffix"); move("$locDir/$run/${period}_${pass}_${shortrun}_QAplots.root","$locDir/$run/${period}_${pass}_${shortrun}_QAplots$suffix");}
202
203    } else { print " File not found: $localpath/$locFileName";} }
204   } 
205 }
206  
207 close FIN;
208 close FOUT;
209 close FOUT2;
210 $exit_code = 0;
211
212 #################################################################################
213 # merge the QA trees
214
215 if ((! $exit_code) && defined(glob("$locDir/*/trendin*.root")) && (-e glob("$locDir/*/trendin*.root")))
216 {
217
218  print "\nMerging....\n";       
219  
220  system("hadd -v 1 -f $locDir/trending.root $locDir/*/trendin*.root");
221  $exit_code = $?>>8;
222
223  if (! $debug) { system("rm $locDir/*/trendin*.root"); }
224
225
226 ################################################################################
227 # process and plot the period level QA 
228
229 print "\n";
230
231 if ((! $exit_code) && (-e "$locDir/trending.root")) { system("root -b -l -q \'$macroDir/PlotEMCALQATrendingTree.C(\"$locDir/trending.root\",$savePlots,\"$treeSelections\",\"$trigger\"\)\'"); }
232
233 $exit_code = $?>>8;
234 if (! $exit_code) { print "\nPlotEMCALQATrendingTree() processed succesfully\n";} else { print "\nThe processing of PlotEMCALQATrendingTree() failed!\n";};
235    
236 ################################################################################
237 # cleanup
238
239 move($processedrunlist,"$locDir/runlist.txt")  or die "Cannot move the runlist to the correct local directory : $!";
240 move("$locDir/trendingPlots.root","$locDir/$period"."_$pass"."_trendingPlots.root");
241
242 ################################################################################
243 # final merging
244
245 if ((! $exit_code) && defined(glob("$locDir/*/${period}_${pass}_*_QAplot*.root $locDir/${period}_${pass}_trendingPlots.root $locDir/trending.root")) && (-e glob("$locDir/*/${period}_${pass}_*_QAplot*.root $locDir/${period}_${pass}_trendingPlots.root $locDir/trending.root")))
246 {
247
248  print "\n Final Merging....\n";
249
250  system("hadd -v 1 -f $locDir/${period}_${pass}_EMCALQA.root  $locDir/*/${period}_${pass}_*_QAplot*.root $locDir/${period}_${pass}_trendingPlots.root $locDir/trending.root");
251
252  if (! $debug) {system("rm -f  $locDir/*/${period}_${pass}_*_QAplot*.root $locDir/${period}_${pass}_trendingPlots.root"); }
253
254 }
255
256 ###############################################################################
257 # cleanup some files used for debugging
258
259 if (! $debug && (defined(glob("$locDir/*/*.txt")))) {unlink(glob("$locDir/*/*.txt"));}
260 if (! $debug && (defined(glob("$locDir/*.txt")))) {unlink(glob("$locDir/*.txt"));}
261 if (! $debug && ! $savePlots && (defined(glob("$locDir/*/"))))  {system("rmdir --ignore-fail-on-non-empty  $locDir/*/");}
262
263
264 ##############################################################################
265 #| make final pdf
266
267 if ((! $exit_code) && -e "$locDir/${period}_${pass}_EMCALQA.root") { system("root -b -l -q \'$macroDir/MakeQAPdf.C+g(\"$locDir/${period}_${pass}_EMCALQA.root\"\)\'"); }
268
269
270 ##############################################################################
271 # that's all falks 
272
273 print "\n Done! \n";
274
275