Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:58

0001 #!/usr/bin/env perl
0002 use File::Basename;
0003 
0004 print "Configuring the python executables and run scripts...\n";
0005 $success=1;
0006 
0007 $odir = $ARGV[0];
0008 $datafile1 = $ARGV[1];
0009 $iovrange = $ARGV[2];
0010 $incommoncfg = $ARGV[3];
0011 $inaligncfg = $ARGV[4];
0012 $intrkselcfg = $ARGV[5];
0013 $inuseSurfDef = $ARGV[6];
0014 $inredirectProxy = $ARGV[7];
0015 
0016 $strUseSD = "";
0017 if ($inuseSurfDef == 0){
0018    $strUseSD = "False";
0019 }
0020 else{
0021    $strUseSD = "True";
0022 }
0023 
0024 
0025 open (datafile1) or die "Can't open the file!";
0026 @dataFileInput1 = <datafile1>;
0027 
0028 open (iovrange) or die "Can't open the iovfile!";
0029 @iovInput1 = <iovrange>;
0030 
0031 print "IOV file: $iovrange \n";
0032 
0033 $j = 0;
0034 $k = 0;
0035 
0036 foreach $iovv ( @iovInput1) {
0037    chomp($iovv);
0038    $iovstr .= "$iovv,";
0039 }
0040 chop($iovstr);
0041 print "IOVs: $iovstr\n";
0042 
0043 system( "
0044 mkdir -p $odir/main/ &&
0045 cp $incommoncfg $odir/common_cff_py.txt &&
0046 cp $inaligncfg $odir/align_tpl_py.txt &&
0047 cp python/initial_tpl_py.txt $odir/ &&
0048 cp python/collect_tpl_py.txt $odir/ &&
0049 cp python/upload_tpl_py.txt $odir/ &&
0050 cp scripts/runScript.csh $odir/ &&
0051 cp scripts/runControl.csh $odir/main/ &&
0052 cp scripts/checkError.sh $odir/main/;
0053 ") and die("Couldn't find all the scripts to copy, see ^^^");
0054 $success*=replace( "$odir/common_cff_py.txt", "<iovs>", "$iovstr" );
0055 $success*=replace( "$odir/common_cff_py.txt", "<SURFDEFOPT>", "$strUseSD" );
0056 $success*=replace( "$odir/runScript.csh", "<PROXYREDIRECT>", "$inredirectProxy" );
0057 
0058 
0059 foreach $data1 ( @dataFileInput1 ) {
0060 
0061    chomp $data1;
0062    next if (index($data1, '#') >= 0);
0063 
0064    my @dataspecs = split(',', $data1);
0065    $datafile = $dataspecs[0];
0066    #($dataskim,$path,$suffix) = fileparse($datafile,,qr"\..[^.]*$");
0067    $trkselfile = $dataspecs[1];
0068    chomp $trkselfile;
0069    $flag = $dataspecs[2];
0070    $flaglower = lc($flag);
0071    if ( $trkselfile eq "" ){
0072       $trkselfile = "$flaglower\TrackSelection_cff_py.txt";
0073    }
0074    print "Picking track selection configuration from $trkselfile \n";
0075    $flagopts = "NOOPTS";
0076    if (defined($dataspecs[3]) and $dataspecs[3] ne ""){
0077       print "A flag option is defined.\n";
0078       $flagopts = $dataspecs[3];
0079    }
0080    else{
0081       print "No flag option is defined.\n";
0082    }
0083 
0084    print "Output directory: $odir \n";
0085    print "Datafile: $datafile \n";
0086    print "Flag: $flag \n";
0087    print "Flag options: $flagopts \n";
0088 
0089    # open datafile, get skim name
0090    open (datafile) or die "Can't open the file!";
0091    @dataFileInput = <datafile>;
0092 
0093    system( "
0094    cp $intrkselcfg/$trkselfile $odir/;
0095    " ) and die("Couldn't find the track selection for '$flaglower', see ^^^");
0096 
0097 
0098    # open common_cff.py
0099    $COMMON = "$odir/common_cff_py.txt";
0100    open (COMMON) or die "Can't open the file!";
0101    @commonFileInput = <COMMON>;
0102 
0103    # open selections
0104    $SELECTION = "$odir/$trkselfile";
0105    open (SELECTION) or die "Can't open the file!";
0106    @selectionsInput = <SELECTION>;
0107 
0108    ## setting up parallel jobs
0109    foreach $data ( @dataFileInput ) {
0110       chomp($data);
0111       if ( ( $data ne "" ) and ( index($data, '#') == -1 ) ){
0112          $jsuccess=1;
0113          $j++;
0114          # do stuff
0115          # print "$data";
0116          system( "
0117          mkdir -p $odir/job$j;
0118          cp $odir/align_tpl_py.txt $odir/job$j/align_cfg.py;
0119          cp $odir/runScript.csh $odir/job$j/;
0120          " );
0121          # run script
0122          open OUTFILE,"$odir/job$j/runScript.csh";
0123          insertBlock( "$odir/job$j/align_cfg.py", "<COMMON>", @commonFileInput );
0124          insertBlock( "$odir/job$j/align_cfg.py", "<SELECTION>", @selectionsInput );
0125          # $success*=replaces for align job
0126          $jsuccess*=replace( "$odir/job$j/align_cfg.py", "<FILE>", "$data" );
0127          $jsuccess*=replace( "$odir/job$j/align_cfg.py", "<PATH>", "$odir/job$j" );
0128          #$jsuccess*=replace( "$odir/job$j/align_cfg.py", "<SKIM>", "$dataskim" );
0129          $jsuccess*=replace( "$odir/job$j/align_cfg.py", "<FLAGOPTS>", "$flagopts" );
0130          $jsuccess*=replace( "$odir/job$j/align_cfg.py", "<FLAG>", "$flag" );
0131          # $success*=replaces for runScript
0132          $jsuccess*=replace( "$odir/job$j/runScript.csh", "<ODIR>", "$odir/job$j" );
0133          $jsuccess*=replace( "$odir/job$j/runScript.csh", "<JOBTYPE>", "align_cfg.py" );
0134          close OUTFILE;
0135          system "chmod a+x $odir/job$j/runScript.csh";
0136          if ($jsuccess == 0){
0137             print "Job $j did not setup successfully. Decrementing job number back.\n";
0138             system "rm -rf $odir/job$j";
0139             $j--;
0140          }
0141       }
0142    }
0143 
0144 
0145 }
0146 
0147 foreach $iov ( @iovInput1) {
0148    chomp($iov);
0149    print "Configuring IOV $iov\n";
0150    $k++;
0151    system( "
0152    cp $odir/upload_tpl_py.txt $odir/upload_cfg_$k.py;
0153    cp $odir/initial_tpl_py.txt $odir/main/initial_cfg_$k.py;
0154    cp $odir/collect_tpl_py.txt $odir/main/collect_cfg_$k.py;
0155    cp $odir/runScript.csh $odir/main/runScript_$k.csh;
0156    " );
0157    # run script
0158    ## setting up initial job
0159    $success*=replace( "$odir/main/initial_cfg_$k.py", "<PATH>", "$odir" );
0160    insertBlock( "$odir/main/initial_cfg_$k.py", "<COMMON>", @commonFileInput );
0161    #$success*=replace( "$odir/main/initial_cfg_$k.py", "<FLAG>", "" );
0162    $success*=replace( "$odir/main/initial_cfg_$k.py", "<iovrun>", "$iov" );
0163    ## setting up collector job
0164    $success*=replace( "$odir/main/collect_cfg_$k.py", "<PATH>", "$odir" );
0165    $success*=replace( "$odir/main/collect_cfg_$k.py", "<JOBS>", "$j" );
0166    insertBlock( "$odir/main/collect_cfg_$k.py", "<COMMON>", @commonFileInput );
0167    #$success*=replace( "$odir/main/collect_cfg_$k.py", "<FLAG>", "" );
0168    $success*=replace( "$odir/main/collect_cfg_$k.py", "<iovrun>", "$iov" );
0169    $success*=replace( "$odir/main/runScript_$k.csh", "<ODIR>", "$odir/main" );
0170    $success*=replace( "$odir/main/runScript_$k.csh", "<JOBTYPE>", "collect_cfg_$k.py" );
0171    ## setting up upload job
0172    $success*=replace( "$odir/upload_cfg_$k.py", "<PATH>", "$odir" );
0173    $success*=replace( "$odir/upload_cfg_$k.py", "<iovrun>", "$iov" );
0174    insertBlock( "$odir/upload_cfg_$k.py", "<COMMON>", @commonFileInput );
0175    #close OUTFILE;
0176    system "chmod a+x $odir/main/runScript_$k.csh";
0177 }
0178 
0179 if($success==0){
0180    system("touch $odir/ERROR");
0181 }
0182 
0183 
0184 
0185 # replace sub routines #
0186 ###############################################################################
0187 
0188 sub replace {
0189    $result = 1;
0190 
0191    $infile = @_[0];
0192    $torepl = @_[1];
0193    $repl = @_[2];
0194 
0195    $tmpindc = "tmp";
0196    $tmpfile = "$infile$tmpindc";
0197    if( $repl =~ /^$/ ){
0198       print "Replacing lines $torepl with empty line in $tmpfile is not possible! \n";
0199       $result = 0;
0200    }
0201    elsif( $repl !~ /\S*/ ){
0202       print "Replacing lines $torepl with a line matching whitespace in $tmpfile is not possible! \n";
0203       $result = 0;
0204    }
0205    open(INFILE,"$infile") or die "cannot open $infile";
0206    @log=<INFILE>;
0207    close(INFILE);
0208 
0209    system("rm -f $tmpfile");
0210    open(OUTFILE,">$tmpfile");
0211 
0212    foreach $line (@log) {
0213       $linecopy = $line;
0214       $linecopy =~ s|$torepl|$repl|;
0215       print OUTFILE $linecopy;
0216    }
0217 
0218    close(OUTFILE);
0219    system("mv $tmpfile $infile");
0220 
0221    return $result
0222 }
0223 
0224 sub insertBlock {
0225 
0226    ($infile, $torepl, @repl) = @_;
0227 
0228    open(INFILE,"$infile") or die "cannot open $infile";;
0229    @log=<INFILE>;
0230    close(INFILE);
0231 
0232    $tmpindc = "tmp";
0233    $tmpfile = "$infile$tmpindc";
0234 
0235    system("rm -f $tmpfile");
0236    open(OUTFILE,">$tmpfile");
0237 
0238    foreach $line (@log) {
0239       if ($line =~ /$torepl/) { print OUTFILE @repl; }
0240       else { print OUTFILE $line; }
0241    }
0242 
0243    close(OUTFILE);
0244    system("mv $tmpfile $infile");
0245 
0246 }