Access spin pattern from CDEV database

0. Usage and Example Output 


[onl04] ~/spin_pattern/> ./get_fill_pattern.pl --run=10054015

Polarization patterns:
+ = filled and positive
+ = filled and negative
o = unfilled and/or unpolarized

Yellow polarization pattern:
+ o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o
- o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o + o + o - o - o o o o o o o o o

Blue polarization pattern:
+ o - o + o - o - o + o - o + o + o - o + o - o - o + o - o + o + o - o + o - o - o + o - o + o + o - o + o - o - o + o
- o + o + o - o + o - o - o + o - o + o + o - o + o - o - o + o - o + o + o - o + o - o - o + o - o + o o o o o o o o o


1. The script


#!/usr/bin/perl -w

use Getopt::Long;

# run number, begin time, cdev data id, etc...
my $run_number = -1;
my $run_begin_time = -1;
my $cdev_begin_time = -1;
my $cdev_dataid = 9E9;

# hash table to store beam data
my %beam_data = ();

&GetOptions( "run=i" => \$run_number );
&usage if ( $run_number < 0 );

#
# Get the begin of run
#
get_bor();

#
# Get the first cdev record taken after the specified run. Issue
# a warning if its > 10 min later
#
get_cdev( $run_begin_time );

#
# Retrieve and parse XML from the specified data id. XML will be
# parsed into a hash table... of hash tables.
#
get_xml( $cdev_dataid );

# Yellow and blue beam polarization data
@yell = split ' ', $beam_data{yell}{polarization};
@blue = split ' ', $beam_data{blue}{polarization};

#
# Output yellow and blue spin patterns (human-readable)
#
print "\n";
print "Polarization patterns:\n";
print "+ = filled and positive\n";
print "+ = filled and negative\n";
print "o = unfilled and/or unpolarized\n\n";

print "Yellow polarization pattern:\n";
$count =0;
for ( $i = 0; $i < $#yell; $i+=3 )
{
print "+ " if ( $yell[$i]==+1 );
print "- " if ( $yell[$i]==-1 );
print "o " if ( $yell[$i]== 0 );
print "\n" if ( ++$count == 60 );
}
print "\n\n";

print "Blue polarization pattern:\n";
$count =0;
for ( $i = 0; $i < $#blue; $i+=3 )
{
print "+ " if ( $blue[$i]==+1 );
print "- " if ( $blue[$i]==-1 );
print "o " if ( $blue[$i]== 0 );
print "\n" if ( ++$count == 60 );
}
print "\n\n";
sub get_xml
{
my $id = shift;
open QUERY, "mysql -h onldb.starp.bnl.gov --port 3502 Conditions_rhic -s -E -e \"SELECT dataS from kretDbBlobS where dataID=$cdev_dataid\"|";
my $KEY = "";
my $TAG = "";
while ( <QUERY> )
{

if ( /Yell/ )
{
$KEY = "yell";
}
if ( /Blue/ )
{
$KEY = "blue";
}
if ( /\/Yell/ ) {
$KEY = "";
}
if ( /\/Blue/ ) {
$KEY = "";
}

if ( /intendedFill/ ) {
$TAG = "intended";

}
if ( /\/intendedFill/ ) {
$TAG = "";
}

if ( /measuredFill/ ) {
$TAG = "measured";
}
if ( /\/measuredFill/ ) {
$TAG = "";
}

if ( /polarizationFill/ and !$TAG ) {
$TAG = "polarization";
($junk, @patt ) = split ' > ';
$mypatt = join ' ', @patt;
chomp $mypatt;
# print "my patt: $mypatt\n";
chomp $TAG;
chomp $KEY;
$beam_data{$KEY}{$TAG} = $mypatt;
}
if ( /\/polarizationFill/ ) {
$TAG = "";
}

# If the key is there, look for the data which we are interested in
if ( $KEY )
{
# print "[key $KEY $TAG] $_";

# If the tag is there, add to the hash table
# (Special exception for the <polariztionFill>
# and similar tags)
if ( $TAG && !/$TAG/ )
{
# Add more data to the pattern
chomp;
$beam_data{$KEY}{$TAG} .= $_;

}
}

}
}

}
close QUERY;
}

sub get_cdev
{
my $begin = shift;
open QUERY, "mysql -h onldb.starp.bnl.gov --port 3502 Conditions_rhic -s -E -e \"SELECT beginTime,dataID from kretDbBlobS where beginTime>'"
. $run_begin_time . "' \"|";
while ( <QUERY> )
{
if ( /dataID/ )
{
my ( $junk, $id ) = split ': ';
if ( $id < $cdev_dataid ) {
$cdev_dataid = $id;
}
}
}
close QUERY;

open QUERY, "mysql -h onldb.starp.bnl.gov --port 3502 Conditions_rhic -s -E -e \"SELECT beginTime,dataID from kretDbBlobS where dataID=$cdev_dataid\"|";
while ( <QUERY> )
{
if ( /beginTime/ ) {
($junk, $cdev_begin_time ) = split ': ';
}
}
close QUERY;

my $run_unixtime = `date +%s --date=\"$run_begin_time\"`;
my $cdev_unixtime = `date +%s --date=\"$cdev_begin_time\"`;
my $diff = $cdev_unixtime - $run_unixtime;
# print "Difference = $diff\n";
if ( $diff > 600 or $diff < -600 )
{
print " ======================== Warning: ========================= \n\n";
print " cdev entry begin $diff s after (or before) run $run_number.\n";
print " If the time is too long (hours), suspect different fills.\n\n";
print " ======================== Warning: ========================= \n";
}
}

sub get_bor
{
open QUERY, "mysql -h onldb.starp.bnl.gov --port 3501 RunLog -s -E -e \"SELECT beginTime from runDescriptor where runNumber=$run_number\"|";
while ( <QUERY> )
{
# print;
chomp;
if ( /beginTime/ )
{
($junk, $run_begin_time) = split ': ';
}
}
close QUERY;
}

sub usage
{
print "\nSomethings not right...\n\n";
print "usage:\n";
print "\n";
print "Retrieve human-readable polarization pattern:\n";
print "\$ get_fill_pattern.pl --run=<runnumber>\n";
}