Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#!/usr/bin/env perl

# SELMA - Saclay Ecal Laser Monitoring and Analysis
# contacts: Julie Malcles, Gautier Hamel de Monchenault 
# last modified: Wed Jun 18 16:48:40 CEST 2008

use Term::ANSIColor;
use Cwd;
use File::stat;
use Time::localtime;

$firsteem     = 601;
$firstebm     = 610;
$firstebp     = 628;
$firsteep     = 646;
$lastecal     = 654;

$debugMF=0;

sub getSMname
{

    my $fed    = shift;
    
    my $ecal = "";
    my $ecalmod = "";
    my $nsm  = 0;
    
    if( $fed<$firsteem || $fed>$lastecal )
    {
	print "Wrong SM number in getSMname: $fed \n";
    }
    if( $fed<$firstebm )
    {
	$ecal = "EE-";
	$nsm = $fed-$firsteem+1-3;
	if ($nsm <= 0 ){ $nsm+=9; }
    }
    elsif( $fed<$firstebp )
    {
	$ecal = "EB-";
	$nsm = $fed-$firstebm+1;
    }
    elsif( $fed<$firsteep )
    {
	$ecal = "EB+";
	$nsm = $fed-$firstebp+1;
    }
    else
    {
	$ecal = "EE+";
	$nsm = $fed-$firsteep+1-3;
	if($nsm <= 0) { $nsm+=9; }
    }
    
    $ecalmod = $ecal.$nsm;

    return $ecalmod;

}


sub getFedNumber
{

    my $smname    = shift;
    my $fed = 0;

    if( $smname =~ /EE-(\d*)/ ){
	
	$nsm = $1;
	my $num=$nsm+3;
	if($num>9) {$num-=9;}

	$fed =  $num + $firsteem -1;
	

    }elsif ( $smname =~ /EE\+(\d*)/ ){

	$nsm = $1;
	my $num=$nsm+3;
	if($num>9) {$num-=9;}
	$fed = $num + $firsteep -1;
	
    }elsif ( $smname =~ /EB-(\d*)/ ){

	$nsm = $1;
	$fed = $nsm + $firstebm - 1;

    
    }elsif ( $smname =~ /EB\+(\d*)/ ){

	$nsm = $1;
	$fed =  $nsm+ $firstebp-1;
	
    }else {

	print "Wrong SM name in getFedNumber: $fed\n";
	$fed =0;
    }
   
    return $fed;
    
}

sub doProcessFed{
    
    my $fed=$_[0];
    my $ecalpart=$_[1];
    my $doprocess=0;
    
    
    if( $fed<$firsteem || $fed>$lastecal )
    {
	print "Wrong SM number in doProcessFed: $fed \n";
	return $doprocess;
    }
    
    if( $ecalpart eq "Ecal" ){
	$doprocess=1;
    }
    elsif( $ecalpart eq "EB" ){
	if( getSMname($fed) =~ /EB(.*)/ ){
	    $doprocess=1;
	}
    }
    elsif( $ecalpart eq "EE" ){
	if( getSMname($fed) =~ /EE(.*)/ ){
	    $doprocess=1;
	}
    }	
    elsif( $ecalpart eq "EB-" ){
	if( getSMname($fed) =~ /EB-(\d*)/ ){
	    $doprocess=1;
	}
    }	
    elsif( $ecalpart eq "EB+" ){
	if( getSMname($fed) =~ /EB\+(\d*)/ ){
	    $doprocess=1;
	}
    }	
    elsif( $ecalpart eq "EE-" ){
	if( getSMname($fed) =~ /EE-(\d*)/ ){
	    $doprocess=1;
	}
    }	
    elsif( $ecalpart eq "EE+" ){
	if( getSMname($fed) =~ /EE\+(\d*)/ ){
	    $doprocess=1;
	}
    } 
    elsif( $ecalpart eq "EcalEven" ){
	my $odd=$fed%2;
	if( $odd == 0 ) {
	    $doprocess=1;
	}
    }
    elsif( $ecalpart eq "EcalOdd" ){
	my $odd=$fed%2;
	if( $odd%2  == 1 ) {
	    $doprocess=1;
	}
    }
    elsif( $ecalpart eq "EBEven" ){
	
	if( getSMname($fed) =~ /EB(.*)/ ){
	    if( $fed%2 == 0 ){	
		$doprocess=1;
	    }
	}
    }
    elsif( $ecalpart eq "EBOdd" ){
	
	if( getSMname($fed) =~ /EB(.*)/ ){
	    if( $fed%2 ==1 ) {
		$doprocess=1;
	    }
	}
    }
    elsif( $ecalpart =~ /E(E|B)(\+|-)(\d{1})/ ){
	if( getSMname($fed) eq  $ecalpart ){
	    $doprocess=1;
	}
    } else {
	
	print "Unknown ecalpart: $ecalpart \n";
    }
    
    return $doprocess;
}


sub sendCMSJob{
    
    my $keyword=$_[0];  # header, matacq, laser, testpulse
    my $keyword2=$_[1]; # for ps command
    my $nmaxjobs=$_[2]; 
    my $user=$_[3];
    my $jobdir=$_[4];
    my $scriptdir=$_[5];


    if( $debugMF==1 ){
	print " debug - sendCMSJob 1 : ${cfg} ${log} ${keyword2} ${nmaxjobs} \n";
    }
    my $isItSent = 0;
    my $njobs = 8;
    my $nsub = 8;
    my $ntot = 8;
    my $timewaiting=0;
    my $command="";
    
    if( $keyword2 eq "NoKey" ){
	$command="ps x -u ${user} | grep cmsRun | grep py | grep -v 'ps x -u' |  wc -l"; 
    }else {
	$command="ps x -u ${user} | grep cmsRun | grep py | grep -v 'ps x -u' | grep ${keyword2} |wc -l";
    }
    $njobs =`$command`;
    $njobs =$njobs+0;

    if( $keyword2 eq "NoKey" ){
	$command2="ps x -u ${user} | grep submitJobPy | grep -v 'ps x -u' | wc -l";
    }else{
	$command2="ps x -u ${user} | grep submitJobPy | grep -v 'ps x -u' | grep ${keyword2} | wc -l";
    }
    
    $nsub =`$command2`;
    $nsub =$nsub+0;

    $ntot=$nsub+$njobs;
    
    if( $debugMF==1 ) {
	print "debug - sendCMSJob 2 : njobs= ${njobs}, nsub= ${nsub}, ntot= ${ntot} \n";
    }
    
    my $commandToSend = "${scriptdir}/submitJobPy.csh ${keyword} ${jobdir}";

    while($ntot >=$nmaxjobs){
	
	sleep 5;
	$timewaiting+=5;

	$njobs =`$command`;  
	$njobs =$njobs+0;
	$nsub =`$command2`;  
	$nsub =$nsub+0;
	
	$ntot=$nsub+$njobs;

	if( $debugMF==1 ) {
	    print "debug - sendCMSJob 3 : inside loop njobs= ${njobs}, nsub= ${nsub}, ntot= ${ntot}  \n";
	    
	}
    }

    my $mydate = `date +%s`;
    print " ....... job sent at: ${mydate} after waiting ${timewaiting} seconds \n";
    print " njobs: ${njobs}, nsub: ${nsub}, ntot: ${ntot} \n";
    
    system $commandToSend ;
    
    
    $isItSent=1;
    
    if( $debugMF==1 ) {
	print "debug - sendCMSJob 4 : isItSent: ${isItSent} \n";
	print "debug - sendCMSJob 5 : command: ${commandToSend} \n";
    }
    
    return  $isItSent;
}