Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:36

0001 #!/usr/bin/env perl
0002 #     R. Mankel, DESY Hamburg     06-Jul-2007
0003 #     A. Parenti, DESY Hamburg    27-Mar-2008
0004 #     $Revision: 1.3 $ by $Author$
0005 #     $Date: 2009/01/07 18:27:49 $
0006 #
0007 #  Extract INFI directives from input file
0008 #  and write chunk to standard output according to
0009 #  specified splitting scheme.
0010 #  Lines starting with '#' (preceeded by any number of whitespace)
0011 #  will be ignored.
0012 #  A first line starting with 'CastorPool=' will always be output as first line.
0013 #
0014 #  Usage:
0015 #
0016 #  mps_split.pl infile thisChunk numberOfChunks
0017 #
0018 use POSIX;
0019 
0020 $inFile = "undefined";
0021 $thisGen = 1;
0022 $totalGen = 0;
0023 
0024 # parse the arguments
0025 while (@ARGV) {
0026   $arg = shift(@ARGV);
0027   if ($arg =~ /\A-/) {  # check for option 
0028     if ($arg =~ "h") {
0029       $helpwanted = 1;
0030     }
0031     elsif ($arg =~ "d") {
0032       $localdir = 1;
0033     }
0034     elsif ($arg =~ "u") {
0035       $updateDb = 1;
0036     }
0037     $optionstring = "$optionstring$arg";
0038   }
0039   else {                # parameters not related to options
0040     $i = $i + 1;
0041     if ($i eq 1) {
0042       $inFile = $arg;
0043     }
0044     elsif ($i eq 2) {
0045       $thisGen = $arg;
0046     }
0047     elsif ($i eq 3) {
0048       $totalGen = $arg;
0049     }
0050   }
0051 }
0052 
0053 if ($totalGen <= 0) {
0054   print "Insufficient information given\n";
0055   exit 1;
0056 }
0057 
0058 if ($thisGen <= 0) {
0059   print "mps_split: zero thisGen --> exit\n";
0060   exit 1;
0061 }
0062 if ($thisGen > $totalGen) {
0063   print "mps_split: thisGen gt totalGen, $thisGen gt $totalGen --> exit\n";
0064   exit 1;
0065 }
0066 
0067 # open the input file
0068 open INFILE,"$inFile";
0069 undef $/;  # undefining the INPUT-RECORD_SEPARATOR means slurp whole file
0070 $body = <INFILE>;  # read whole file
0071 close INFILE;
0072 $/ = "\n"; # back to normal
0073 
0074 # split the input into lines
0075 @LINESALL = split "\n",$body;
0076 
0077 my @LINES;
0078 foreach my $line (@LINESALL) {
0079     unless ($line =~ /^\s*#/ ) { # exclude lines with comments, i.e.
0080     push (@LINES, $line);    # marked with '#' (& ignore whitespace before)
0081     }
0082 }
0083 
0084 # how many INFI cards
0085 $ninfi = $#LINES + 1;
0086 
0087 my $poolDef = "";
0088 if ($LINES[0] =~ /^CastorPool=/) { # starts with CastorPool
0089   $poolDef = $LINES[0];
0090   $ninfi = $ninfi - 1; # so we have one line less
0091 }
0092 
0093 # Now for the arithmetics
0094 $chunkSize = $ninfi / $totalGen;
0095 
0096 $startId =floor( ($thisGen - 1) * $chunkSize ) + 1;
0097 $endId = floor( $thisGen * $chunkSize);
0098 
0099 if ($thisGen == 1) {
0100   $startId = 1;
0101 }
0102 if  ($thisGen == $totalGen) {
0103   $endId = $ninfi;
0104 }
0105 if ($poolDef ne "") {  #first line was pool definition, so shift again
0106   $startId = $startId + 1;
0107   $endId = $endId + 1;
0108 }
0109 
0110 # print "chunkSize $chunkSize\n ninfi $ninfi startID $startId endId $endId\n";
0111 
0112 if ($endId < $startId) {
0113   print "Invalid interval, startId=$startId, endID=$endId\n";
0114   print "ninfi was $#infi\n";
0115   exit 2;
0116 }
0117 
0118 print "$poolDef\n" if ($poolDef ne ""); #GF
0119 
0120 # prepare the split output
0121 for ($i = $startId; $i <= $endId; ++$i) {
0122   print "@LINES[$i-1]\n";
0123 }