#!/usr/bin/perl -w # summarize_epics_wbpm.perl [filename] # # by: Jaideep Singh # # date: 05/16/2006 # # about: reads a raw epics data text file and create a summary file # creates a file: # (0) first line is the prescalar string # (1) afterwards, each line is an epics variable # (2) if the variable is numeric, then the line will be: # # variable name number of values minimum maximum mean standard deviation # (3) the numeric variables come at the beginning of the file and are separated from the alphanumeric variables by a "-----" line # (4) if the variable is alphanumeric, then the line will be: # # variable name number of values first value number of occurences second value number of occurences ... # # Note that some EPICS-like stuff still leaks through... # # input file is of the format: # first line might be prescalar string # all subsequent lines: # timestampseconds from 0000 Jan 1 2003variable namevaricable value # # # EPICS info is injected into the data stream at certain times invertals. # More important stuff is logged more often. # Immediately preceding one log cycle, a timestamp is grabbed # Each variable is recorded in ASCII format on its own line: # # [variable name, upto 30 characters][at least one blank space][upto 20 characters for value] # # for example: # # IPM1H04B.XPOS -0.0408508 # 000000000111111111122222222223333333333444444444455 # 123456789012345678901234567890123456789012345678901 # # Variable names can have ".", ":","+","_" as well as alphanumeric characters # # Modified to cut on beam trips # for bpm variables, beam trips result in the string "0" ---> these are not included in the statistics use strict "subs"; #getting name of file to search + ARGUMENT ERROR HANDLING $filename = $ARGV[0]; die "Uh, what file do you want me to look at dude?: Too few arguments\n" unless ($filename); die "Scared and confused: Too many arguments\n" unless (($#ARGV+1) < 2); #start flagging beam trips open(FILE,$filename) or die "d'oh!: - $filename - not found\n"; #initializing variables $trip_line_count = 0; $trip_line_check_count = 0; $trip_line_found_count = 0; while() { $trip_line_count++; chomp; if (/(ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+)/) { # print "Found prescalars: $1\n"; } elsif ($_ ne "") { @line_parts = split(/\t/); if ($line_parts[3] =~ /([0123456789eE\.\+\-]+)/) { $poop = $1; if ($line_parts[3] eq $1) { $type = "num" } else { $type = "str" } } else {$poop = ""; $type = "str"} if ($type eq "num") { $trip_flag{$line_parts[1]} = "on" if (not $trip_flag{$line_parts[1]}); #beam is on by default if (($line_parts[2] eq "IPM1C08.XPOS") || ($line_parts[2] eq "IPM1C10.XPOS") || ($line_parts[2] eq "IPM1C10.YPOS") || ($line_parts[2] eq "IPM1C12.XPOS") || ($line_parts[2] eq "IPM1C12.YPOS") || ($line_parts[2] eq "IPM1C14.XPOS") || ($line_parts[2] eq "IPM1C14.YPOS") || ($line_parts[2] eq "IPM1C16.XPOS") || ($line_parts[2] eq "IPM1C16.YPOS") || ($line_parts[2] eq "IPM1C18.XPOS") || ($line_parts[2] eq "IPM1C18.YPOS") || ($line_parts[2] eq "IPM1C20.XPOS") || ($line_parts[2] eq "IPM1C20.YPOS") || ($line_parts[2] eq "IPM1H01.XPOS") || ($line_parts[2] eq "IPM1H01.YPOS") || ($line_parts[2] eq "IPM1H04A.XPOS") || ($line_parts[2] eq "IPM1H04A.YPOS") || ($line_parts[2] eq "IPM1H04B.XPOS") || ($line_parts[2] eq "IPM1H04B.YPOS")) # if ($line_parts[2] eq "hac_bcm_average") { $trip_line_check_count++; if ($line_parts[3] eq "0") # if ($line_parts[3] < 0.10) { $trip_flag{$line_parts[1]} = "off"; #no beam $trip_line_found_count++; } } } } }; close(FILE); #really important! $after_trip_cut_time = 30.1; $before_trip_cut_time = 6.1; #really important! #initializations $timecut = 0.0; $beam_on_sum = 0.0; $beam_on_cut_sum = 0.0; $number_of_trips = 0.0; #counting beam on time and number of trips , cutting out after_trip_time foreach $timestamp (sort keys %trip_flag) { if (not $previous_timestamp) { $previous_timestamp = $timestamp } $current_flag = $trip_flag{$timestamp}; if (not $previous_flag) { $previous_flag = $current_flag } if ($current_flag eq "off") { $timecut = $timestamp + $after_trip_cut_time; if ($previous_flag eq "on") { $number_of_trips++} } else { $beam_on_sum += $timestamp-$previous_timestamp } if ($timestamp < $timecut) { $trip_flag{$timestamp} = "off-cut" } # print "$timestamp\t\t$current_flag\t\t$trip_flag{$timestamp}\n"; $last_timestamp = $timestamp; $previous_flag = $current_flag; $previous_timestamp = $timestamp; } #cutting out before_trip_time $timecut = $last_timestamp + 1.0; #print "\n ***** \n"; foreach $timestamp (reverse sort keys %trip_flag) { $current_flag = $trip_flag{$timestamp}; if (($current_flag eq "off") || ($current_flag eq "off-cut")) { $timecut = $timestamp - $before_trip_cut_time } if ($timestamp > $timecut) { $trip_flag{$timestamp} = "off-cut2" } # print "$timestamp\t\t$current_flag\t\t$trip_flag{$timestamp}\n"; $first_timestamp = $timestamp; } #counting beam on time after cuts $previous_timestamp = 0; foreach $timestamp (sort keys %trip_flag) { if (not $previous_timestamp) { $previous_timestamp = $timestamp } $current_flag = $trip_flag{$timestamp}; if ($current_flag eq "on") { $beam_on_cut_sum += $timestamp-$previous_timestamp } $previous_timestamp = $timestamp; } $total_run_time = $last_timestamp-$first_timestamp; $beam_off_time = $total_run_time - $beam_on_sum; #print "trip line count=\t$trip_line_count\n"; #print "trip line check count=\t$trip_line_check_count\n"; #print "trip line found count=\t$trip_line_found_count\n"; #print "approx number of trips=\t$number_of_trips\n"; #print "approx total run time(sec)=\t$total_run_time\n"; #print "approx beam off time (sec)=\t$beam_off_time\n"; #print "approx run time after beam cuts (sec) =\t $beam_on_cut_sum\n"; #end flagging beam trips #die and print "\ndone!\n"; open(FILE,$filename) or die "d'oh!: - $filename - not found\n"; #print "\nSearching raw data file:\t$filename\n"; #initializing variables $line_count = 0; LINENUM: while() { $line_count++; chomp; if (/(ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+)/) { $prescalars = $1; } elsif ($_ ne "") { @line_parts = split(/\t/); if ($line_parts[3] =~ /([0123456789eE\.\+\-]+)/) { $poop = $1; if ($line_parts[3] eq $1) { $type = "num" } else { $type = "str" } } else {$poop = ""; $type = "str"} # $poop is used as just a diagnostic variable # print "\*$line_parts[3]\*\t\*$poop\*\t$type\n"; if ($type eq "num") { #for diagnostics # if ($line_parts[2] eq "hac_bcm_average") # if (($line_parts[2] eq "MSEPRIR") || ($line_parts[2] eq "hac_bcm_average") || ($line_parts[2] eq "HALLA\:p") || ($line_parts[2] eq "halla_MeV") #|| ($line_parts[2] eq "IPM1H04A.XPOS")) # { print "$_\t$trip_flag{$line_parts[1]}\n" } #for diagnostics # # just use count good counter # if (not $data_num{$line_parts[2]}{"count"}) { $data_num{$line_parts[2]}{"count"} = 1.0; $data_num{$line_parts[2]}{"count0"} = 0.0; $data_num{$line_parts[2]}{"count1"} = 0.0; } else { $data_num{$line_parts[2]}{"count"}++ } if ($trip_flag{$line_parts[1]} ne "on") { $data_num{$line_parts[2]}{"count0"}++ } else { $data_num{$line_parts[2]}{"count1"}++; if ($data_num{$line_parts[2]}{"count1"} == 1) { $data_num{$line_parts[2]}{"min"} = $line_parts[3]; $data_num{$line_parts[2]}{"max"} = $line_parts[3]; $data_num{$line_parts[2]}{"sum"} = $line_parts[3]; $data_num{$line_parts[2]}{"sum_sq"} = $line_parts[3]*$line_parts[3]; $data_num{$line_parts[2]}{"mean"} = $line_parts[3]; $data_num{$line_parts[2]}{"std"} = 0.0; } else { $data_num{$line_parts[2]}{"min"} = $line_parts[3] if ($data_num{$line_parts[2]}{"min"} > $line_parts[3]); $data_num{$line_parts[2]}{"max"} = $line_parts[3] if ($data_num{$line_parts[2]}{"max"} < $line_parts[3]); $data_num{$line_parts[2]}{"sum"} = $data_num{$line_parts[2]}{"sum"} + $line_parts[3]; $data_num{$line_parts[2]}{"sum_sq"} = $data_num{$line_parts[2]}{"sum_sq"} + $line_parts[3]*$line_parts[3]; $data_num{$line_parts[2]}{"mean"} = $data_num{$line_parts[2]}{"sum"}/$data_num{$line_parts[2]}{"count1"}; $data_num{$line_parts[2]}{"std"} = sqrt(abs($data_num{$line_parts[2]}{"sum_sq"}/$data_num{$line_parts[2]}{"count1"}-$data_num{$line_parts[2]}{"mean"}*$data_num{$line_parts[2]}{"mean"})); $data_num{$line_parts[2]}{"std"} = 0.0 if ($data_num{$line_parts[2]}{"max"} == $data_num{$line_parts[2]}{"min"}); } } ################## # if (not $data_num{$line_parts[2]}{"count"}) # { # $data_num{$line_parts[2]}{"count"} = 1.0; # if ($trip_flag{$line_parts[1]} ne "on") # { $data_num{$line_parts[2]}{"count0"} = 1.0 } # else # { # $data_num{$line_parts[2]}{"count0"} = 0.0; # $data_num{$line_parts[2]}{"min"} = $line_parts[3]; # $data_num{$line_parts[2]}{"max"} = $line_parts[3]; # $data_num{$line_parts[2]}{"sum"} = $line_parts[3]; # $data_num{$line_parts[2]}{"sum_sq"} = $line_parts[3]*$line_parts[3]; # $data_num{$line_parts[2]}{"mean"} = $line_parts[3]; # $data_num{$line_parts[2]}{"std"} = 0.0; # } # } # else # { # $data_num{$line_parts[2]}{"count"}++; # if ($trip_flag{$line_parts[1]} ne "on") # { $data_num{$line_parts[2]}{"count0"}++ } # else # { # $data_num{$line_parts[2]}{"min"} = $line_parts[3] if ($data_num{$line_parts[2]}{"min"} > $line_parts[3]); # $data_num{$line_parts[2]}{"max"} = $line_parts[3] if ($data_num{$line_parts[2]}{"max"} < $line_parts[3]); # $data_num{$line_parts[2]}{"sum"} = $data_num{$line_parts[2]}{"sum"} + $line_parts[3]; # $data_num{$line_parts[2]}{"sum_sq"} = $data_num{$line_parts[2]}{"sum_sq"} + $line_parts[3]*$line_parts[3]; # $data_num{$line_parts[2]}{"mean"} = $data_num{$line_parts[2]}{"sum"}/($data_num{$line_parts[2]}{"count"} - $data_num{$line_parts[2]}{"count0"}); # $data_num{$line_parts[2]}{"std"} = sqrt(abs($data_num{$line_parts[2]}{"sum_sq"}/($data_num{$line_parts[2]}{"count"}-$data_num{$line_parts[2]}{"count0"})-$data_num{$line_parts[2]}{"mean"}*$data_num{$line_parts[2]}{"mean"})); # $data_num{$line_parts[2]}{"std"} = 0.0 if ($data_num{$line_parts[2]}{"max"} == $data_num{$line_parts[2]}{"min"}); # } # } ################## } else { if (not $data_str{$line_parts[2]}{$line_parts[3]}) {$data_str{$line_parts[2]}{$line_parts[3]} = 1.0} else {$data_str{$line_parts[2]}{$line_parts[3]}++} } } }; close(FILE); #setting up output file @path_name_parts = split(/\//,$filename); @file_name_parts = split(/\./,$path_name_parts[$#path_name_parts]); $file_out = $file_name_parts[0]."_summary_wbpm.txt"; #print "OUTPUT filename : $file_out\n\n"; open(OUTPUT,">$file_out"); print OUTPUT "$prescalars\n" if ($prescalars); foreach $variable_name (sort keys %data_num) { # print "$variable_name\n"; if ($data_num{$variable_name}{"count0"} == $data_num{$variable_name}{"count"}) { $data_num{$variable_name}{"min"} = 0.0; $data_num{$variable_name}{"max"} = 0.0; $data_num{$variable_name}{"mean"} = 0.0; $data_num{$variable_name}{"std"} = 0.0; } #$data_num{$variable_name}{"min"} $count = $data_num{$variable_name}{"count1"}; # $count = $data_num{$variable_name}{"count"}."(".$data_num{$variable_name}{"count1"}." + ".$data_num{$variable_name}{"count0"}.")"; $min = sprintf( "%.9E" , $data_num{$variable_name}{"min"} ); $max = sprintf( "%.9E" , $data_num{$variable_name}{"max"} ); $mean = sprintf( "%.9E" , $data_num{$variable_name}{"mean"} ); $std = sprintf( "%.9E" , $data_num{$variable_name}{"std"} ); print OUTPUT "$variable_name\t$count\t$min\t$max\t$mean\t$std\n" } print OUTPUT "-----\n"; # because %data_str is a hash of hashes, $data_str{$variable_name} is a reference to a hash # therefore $temp is a reference to a hash # and %$temp is the hash # and $$temp{$variable_value} is the value corresponding to the $variable_value key foreach $variable_name (sort keys %data_str) { $temp = $data_str{$variable_name}; $num_values = scalar keys %$temp; print OUTPUT "$variable_name\t$num_values"; foreach $variable_value (sort keys %$temp) { print OUTPUT "\t$variable_value\t$$temp{$variable_value}"} print OUTPUT "\n"; } print OUTPUT "-----\n"; print OUTPUT "trip line count=\t$trip_line_count\n"; print OUTPUT "trip line check count=\t$trip_line_check_count\n"; print OUTPUT "trip line found count=\t$trip_line_found_count\n"; print OUTPUT "approx number of trips=\t$number_of_trips\n"; print OUTPUT "approx total run time(sec)=\t$total_run_time\n"; print OUTPUT "approx beam off time (sec)=\t$beam_off_time\n"; print OUTPUT "approx run time after beam cuts (sec) =\t $beam_on_cut_sum\n"; close(OUTPUT);