#!/usr/bin/perl
# Script to turn Andy's livetime files into something concise
# argument 1 is the first run, argument 2 is the last run
#input the two runs  for which you want to calculate livetime
open(SUMFILE,">livebyrun.dat");
open(SUBFILE,">livebysubrun.dat");
open(DAYFILE,">daynum.dat");
#Set the location of the umlive.log here:
#open (EP,"/disk/upmu/um/log_summ/umlive.log");
open (EP,"/net/sukfs04/work/upmu/rep07sun/log_summ/umlive.log");
$firstrun = 100;
$lastrun  = 9999;

if ($ARGV[0] > 0) {
  $firstrun = $ARGV[0];
  if ($ARGV[1] > 0) {
  $lastrun = $ARGV[1];
  }
}
$reallastrun = $firstrun;
 for $i ($firstrun..$lastrun)
 {
    $ID_live{$i}=0.;
    $OD_live{$i}=0.;
  }

$totalODtime = 0.;
$totalIDtime = 0.;
while (<EP>) 
{
  $_ =~ s/\s*//;    # Get rid of the preceding spaces
  chop($_);         # Get rid of the return after the line
  if ((/Run/) && (/Subrun/)) {
    ($run,$subrun) = (/Run\s+(\d+)\s+Subrun\s+(\d+)/);        # parse the line
#    if (($run <$firstrun) || ($run >= $lastrun))
#      { 
#last;
#}
    $linecount = 1;
    $totalline = 0;
    next;
  }
  if ($linecount==1) { # read the first time way
    $linecount=2;
    ($clmn1_1,
     $clmn1_2,
     $clmn1_3,
     $clmn1_4,
     $clmn1_5,
     $clmn1_6,
     $clmn1_7,
     $clmn1_8) = split(' ',$_,8);
    next;
#cet    print "$_\n";
  }
  if ($linecount==2) { # read the second time way
    $linecount=0;
    ($clmn2_1,
     $clmn2_2,
     $clmn2_3,
     $clmn2_4,
     $clmn2_5,
     $clmn2_6,
     $clmn2_7,
     $clmn2_8) = split(' ',$_,8);

# Now do the calculation.  

 $ID_live{$run} += $clmn2_1-$clmn2_2-$clmn2_3;
 $OD_live{$run} += $clmn2_1-$clmn2_2-$clmn2_3-$clmn2_5-$clmn2_6-$clmn2_7;
  }
next;
}

$OD_total=0.;
$ID_total=0.;
#cet print SUMFILE "$firstrun $lastrun\n";
#for ($runinput=$firstrun;$runinput<=$lastrun;$runinput++) {
for $runinput ($firstrun..$lastrun) {
#cet  print "$runinput $OD_live{$runinput}\n";
$OD_total+=$OD_live{$runinput};
$ID_total+=$ID_live{$runinput};
}
#cet: OD livetime is what I'm interested in.  -EHT
#cet printf "Total OD livetime is %.4f hours\n",$OD_total;
#cet printf "Total ID livetime is %.4f hours\n",$ID_total;
printf $OD_total;
