#!/usr/bin/perl #..make_snippet # C. Hearty, May 2004 #..Generates tcl snippets for specified tcl files by BbkDatasetTcl. Originally # written for CharmUser. Modify subroutines snippet_text and script_text # at end for other jobs. #..For usage, type ./make_snippet -h use Getopt::Long; #-------------------------------------------------------------------------- #..Help listing my $help_listing = "\n"."make_snippet produces a tcl snippet file for each tcl file listed, ". "and a script to submit the resulting files to the ". "batch system.\n\n". "Usage:\n". "./make_snippet -MC file1 file2 ... or\n". "./make_snippet -data file1 file2 ...\n". "Where the file names can contain wildcard characters.\n\n". "A file run_xxx.tcl is produced for each specified tcl file xxx.tcl. ". "Normally, xxx.tcl will have been created by BbkDatasetTcl. ". "A single perl script per command, sub_xxx, is also created to submit ". "the resulting run_xxx files to the batch system. (Type source sub_xxx).\n\n". "make_snippet should be run from workdir, and by default, will place the ". "resulting run_xxx.tcl files in workdir, and set the location of the ". "log files and ntuple files to workdir. These locations can be modified ". "by options -tcldir -logdir and -ntpdir. For example:\n\n". "./make_snippet -MC -tcldir snippets -logdir log -ntpdir ntuples tcl/SP*.tcl\n\n"; #-------------------------------------------------------------------------- #..Check working directory (-d '../workdir') || die "workdir not found.\n The script must be run from a subdirectory of a test release area.\n"; #-------------------------------------------------------------------------- #..Check for valid options my %opts = (); GetOptions( \%opts, "data", "mc", "tcldir|td=s", "ntpdir|nd=s", "logdir|ld=s", "help|h"); $Getopt::Long::error && die "\n", $help_listing; my $MCTruth; if ($opts{"help"}) { die $help_listing; } elsif ($opts{"mc"}) { $MCTruth = 'set MCTruth "true"'; } elsif ($opts{"data"}) { $MCTruth = 'set MCTruth "false"'; } else { die "Either the '-data' or '-MC' option must be specified.\n\n", $help_listing; } my $tclDir = ($opts{"tcldir"}) ? $opts{"tcldir"} : '.'; (-d $tclDir) || die "Non-existing output tcl directory: $tclDir\n\n", $help_listing; my $ntpDir = ($opts{"ntpdir"}) ? $opts{"ntpdir"} : '.'; (-d $ntpDir) || die "Non-existing output ntuple directory: $ntpDir\n\n", $help_listing; my $logDir = ($opts{"logdir"}) ? $opts{"logdir"} : '.'; (-d $logDir) || die "Non-existing log directory: $logDir\n\n", $help_listing; #-------------------------------------------------------------------------- #..Create the script name to submit all the jobs. my $script_name = $ARGV[0]; $script_name = (split '/', $script_name)[-1]; # Strip path $script_name =~ s/\.tcl$//; $script_name = "sub_$script_name"; open SCRIPT, ">../workdir/$script_name" or die "Cannot create $script_name: $!"; print SCRIPT "#!/bin/sh -f\n"; #-------------------------------------------------------------------------- #..Make the tcl snippets and write a line per file to the batch submit # script. Skip any input files that don't end in .tcl foreach my $input_file (@ARGV) { unless ($input_file =~ /\.tcl$/) { print "file $input_file does not have .tcl extension; skipping\n"; next; } my $inFileName = (split '/', $input_file)[-1]; # Strip path my $output_file = 'run_'.$inFileName; open SNIPPET, ">${tclDir}/$output_file" or die "Cannot create $output_file: $!"; print SNIPPET &snippet_text($MCTruth, $input_file, $inFileName); close SNIPPET; print "Created file $output_file\n"; print SCRIPT &script_text($output_file); } #..Fix up the permissions on the script, and we are done close SCRIPT; chmod 0777, "$script_name"; print "\n"."script to batch submit these jobs is $script_name. ". "To run it:\n"."source $script_name\n"; #-------------------------------------------------------------------------- #..Subroutine to create snippet text. Edit this to personalize snippets. sub snippet_text { my($Config, $input_file, $inFileName) = @_; my $root_name = $inFileName; $root_name =~ s/.tcl$/.root/; return "#..See Analysis.tcl for description of FwkCfgVars.\n". "sourceFoundFile $input_file\n". "${Config}\n". "set FilterOnTag \"false\"\n". "set BetaMiniTuple \"root\"\n". "set histFileName ${ntpDir}/$root_name\n". "set NEvents 0\n". "sourceFoundFile Analysis.tcl\n"; } #-------------------------------------------------------------------------- #..Subroutine to generate the batch submit command for each file. sub script_text { my $snippet_name = shift @_; my $job_name = $snippet_name; $job_name =~ s/.tcl$/.log/; $job_name =~ s/^run_//; return "bsub -q kanga -o ${logDir}/$job_name \.\./bin/\$BFARCH/BtaTupleApp ${tclDir}/$snippet_name\n"; }