Wiki software last updated 28-Jul-2014 - Comments to Ruben Squartini <ruben@auger.org.ar>

Use of PHP for plotting

Php is used to access the DataBase and classes defined by the Jpgraph package are used for simple plotting.

In AugerMonitoring/WebSite/GENERAL/Graphics/Jpgraph/draw.php a simple draw function is defined, which just makes a proper img html-tag, but otherwise follows an http-address.

<?php
function DrawImage($aLink,$aAlt=NULL,$aclass="border",$rand=1,$options="")
{
  if ( ! $aAlt ) $aAlt = $aLink;
  if ($rand) {
    if (strstr($aLink,"?")) $aLink.="&".rand();
    else $aLink.="?".rand();
  }
echo '<img class="'.$aclass.'" src="'.$aLink.'" alt="'.$aAlt.'"'.$options.'>';
}
?>

This funcition is called in a general layout page. We have now two examples, one for 1D histograms, and one for the eye-view status-plot.

1D histograms like in BG_LOOP

Since plotting the 1D histograms is almost the same for all variables, there is only one page, AugerMonitoring/WebSite/FD/BG_LOOP/var_1d.php:

<?php 
  include("sidebar.php");
  echo '<h2 id="ARef">'.$Var.' Variance --- 1D</h2>';
  require_once("Graphics/Jpgraph/draw.php");
  $FDSite     =isset($_GET["FDSite"])     ? $_GET["FDSite"]     : 'Los Leones';
  $Sc         =isset($_GET['Sc'])         ? $_GET['Sc']         : 1.0;
  $Var        =isset($_GET['Var'])        ? $_GET['Var']        : 'Variance';
  DrawImage(FD_JPGRAPH_PATH."telescopes_histos.php?Sc=$Sc&Var=$Var&FDSite=$FDSite", "$Var of pixels", "noborder");
?>

The variable is passed by from the link given in the sidebar. The AugerMonitoring/WebSite/FD/Graphics/Jpgraph/telescopes_histos.php then does the plotting. First, the variables are initianlised:

<?php
  include("jpgraph.php");
  include("jpgraph_line.php");
  include("jpgraph_utils.inc");
  include("Graphics/Jpgraph/histo_1d.php");
// VARIABLES -----------
$FDSite     =isset($_GET["FDSite"])     ? $_GET["FDSite"]     : 'Los Leones';
$Sc         =isset($_GET['Sc'])         ? $_GET['Sc']         : 1.0;
$Var        =isset($_GET['Var'])        ? $_GET['Var']        : 'Variance';
$title      =isset($_GET['title'])      ? $_GET['title']      : 'Telescope ';
$hColor     =isset($_GET['hColor'])     ? $_GET['hColor']     : "black";
$FillColor  =isset($_GET['FillColor'])  ? $_GET['FillColor']  : $hColor;
$xAxisTitle =isset($_GET['xAxisTitle']) ? $_GET['xAxisTitle'] : 'PixelNr';
$yAxisTitle =isset($_GET['yAxisTitle']) ? $_GET['yAxisTitle'] : $Var;
$xmin       =isset($_GET['xmin'])       ? $_GET['xmin']       : 0;
$xmax       =isset($_GET['xmax'])       ? $_GET['xmax']       : 440;
$ymin       =isset($_GET['ymin'])       ? $_GET['ymin']       : 0;
$ymax       =isset($_GET['ymax'])       ? $_GET['ymax']       : 0;
$xsize      =isset($_GET['xsize'])      ? $_GET['xsize']      : 260*$Sc;
$ysize      =isset($_GET['ysize'])      ? $_GET['ysize']      : 200*$Sc;
$dx         =isset($_GET['dx'])         ? $_GET['dx']         : 10;
$dy         =isset($_GET['dy'])         ? $_GET['dy']         : 10;

Then the db is connected

$db = new TFDDb($FDSite);

the Histo_1D class instances are created, histo_1d is defined in AugerMonitoring/WebSite/GENERAL/Graphics/Jpgraph/histo_1d.php

$histo = array();
// create histo-instances
for ($i=1; $i<7; $i++) {
  $histo[$i] = new Histo_1D("TEST",$xmax-$xmin,$xmin,$xmax);
  $histo[$i]->SethColor($hColor);
  $histo[$i]->SetFillColor($FillColor);
}

Then the first query to extract the latest event:

$query = "SELECT MAX(GPS) as maxGPS FROM BGPixelTab WHERE TelescopeId=2";
$result =$db->Query($query);
$row=mysql_fetch_object($result);
$GPS_max=$row->maxGPS;
mysql_free_result($result);

And then the query for the actual data, which is then filled into the histograms:

$query = "SELECT TelescopeId, PixelId,$Var FROM BGPixelTab WHERE GPS=$GPS_max  AND PixelId<=440";
$result = $db->Query($query);
// loop over aquired data and fill histos
while( $row=mysql_fetch_object($result) ) {
  $histo[$row->TelescopeId]->fill($row->PixelId,$row->$Var/1000.);
}
mysql_free_result($result);

Create each of the 6 telescope histograms with apropriate style:

// Create the graphs
$graphs = array();
for ($i=1; $i<7; $i++) {
  $graphs[$i] = new Graph($xsize, $ysize);
  $graphs[$i]->img->SetMargin(50, 10, 20, 32);
  $graphs[$i]->SetScale("linlin", $ymin, $ymax, $xmin, $xmax);
  $graphs[$i]->ygrid->Show(true, true);
  $graphs[$i]->xgrid->Show(true, false);
  $graphs[$i]->yaxis->scale->ticks->SupressFirst();
  $graphs[$i]->Add($histo[$i]);
  $graphs[$i]->title->Set($title.$i);
  $graphs[$i]->xaxis->SetTitle($xAxisTitle);
  $graphs[$i]->yaxis->SetTitle($yAxisTitle);
  $graphs[$i]->yaxis->SetTitleMargin(35);
  $graphs[$i]->title->SetFont(FF_FONT1, FS_BOLD, 14);
  $graphs[$i]->yaxis->title->SetFont(FF_FONT1,FS_BOLD, 10);
  $graphs[$i]->xaxis->title->SetFont(FF_FONT1,FS_BOLD, 10);
  $graphs[$i]->yaxis->SetColor("black");
 }

The graphs are collected to a jpgraph object called multi-graph, which is finally displayed:

//define the multi graph
$mgraph = new MGraph();
//define offsets for multi-graph
$xpos = array();
$ypos = array();
for ($i=1; $i<4; $i++) {
  $xpos[$i] = ($xsize+$dx)*($i-1);
  $xpos[$i+3] = ($xsize+$dx)*($i-1);
  $ypos[$i] = 0;
  $ypos[$i+3] = $ysize+$dy;
}
//make mgraph and display
for ($i=1; $i<7; $i++) {
  $mgraph->Add($graphs[$i], $xpos[$i], $ypos[$i]);
}
$mgraph->Stroke();
?> 

2D camara-view of pixels' status

The 2D camara view of the pixels status is called in the page AugerMonitoring/WebSite/FD/Calibration/caliba/index.php:

 <?php include("sidebar.php"); ?>
 <h2 id="ARef">Calib A summary</h2>
 <?php
  require_once("Graphics/Jpgraph/draw.php");
  $FDSite     =isset($_GET["FDSite"])     ? $_GET["FDSite"]     : 'Los Leones';
  $Sc         =isset($_GET['Sc'])         ? $_GET['Sc']         : 1.0;
  $Var        =isset($_GET['Var'])        ? $_GET['Var']        : 'Status';
  echo '<table BORDER="1px" CELLPADDING="0px" CELLSPACING="0px"><tr><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=1", "$Var of pixels bay 1", "noborder"); 
  echo '    </td><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=2", "$Var of pixels bay 2", "noborder") ;
  echo '    </td><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=3", "$Var of pixels bay 3", "noborder") ;
  echo '    </td></tr><tr><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=4", "$Var of pixels bay 4", "noborder") ;
  echo '    </td><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=5", "$Var of pixels bay 5", "noborder") ;
  echo '    </td><td>';
  DrawImage(FD_JPGRAPH_PATH."statusplot.php?Sc=$Sc&Var=$Var&FDSite=$FDSite&Bay=6", "$Var of pixels bay 6", "noborder") ;
  echo '    </td></tr>
    </table>
   ';
  ?>  

If you look now at the statusplot.php, you see how the parameters are passed to the actual plotting-script. It assumes default values if not given.

<?php
include ("jpgraph.php");
include ("jpgraph_line.php");
include ("jpgraph_scatter.php");
// VARIABLES -----------
$FDSite    =isset($_GET["FDSite"])    ? $_GET["FDSite"]   : 'Los Leones';
$Bay       =isset($_GET['Bay'])       ? $_GET['Bay']      : 1.0;
$Var       =isset($_GET['Var'])       ? $_GET['Var']      : 'Status';
$psize     =isset($_GET['psize'])     ? $_GET['psize']    : 10;
$title="Pixel status for telescope $Bay";
$xAxisTitle="Column #";
$yAxisTitle="Row #";
$xmin=0;
$xmax=21;
$ymin=0;
$ymax=23;

Then the mysql-query is prepared. First extract the lastest entry identified by the maximum GPS-time:

// open the database
$db = new TFDDb($FDSite);
//take the latest GPS time from database
$query = "SELECT MAX(GPSSec) FROM CalibATab WHERE TelescopeID=$Bay";
$result = $db->Query($query);
$row=mysql_fetch_array($result);
$maxgps=$row[0];
mysql_free_result($result);
// ---------------------
// status coding, only stati entered here are shown in this order:: 
$statCode = array(0 => "green", 
   		  4 => "red",
	  );

Then create the query for the given $maxgps:

// Create Query
$query="select CalibATab.PixelId, PixelPosLookupTab.ColId, PixelPosLookupTab.RowId, $Var FROM CalibATab,PixelPosLookupTab where PixelPosLookupTab.PixelId=CalibATab.PixelId AND CalibATab.GPSSec=$maxgps AND CalibATab.TelescopeId=$Bay";
$result = $db->Query($query);
// fill pixel array prow and pcol
while($row=mysql_fetch_object($result)){
 $prow[$row->Status][]=$row->RowId;
 $pcol[$row->Status][]=$row->ColId;
}
// free result
mysql_free_result($result);

Then create the jpgraph object 'ScatterPlot':

// Create the scatter plots
$scplot=array();
$i=0;
foreach($statCode as $tStatus => $tColor) {
  $scplot[$i]=new ScatterPlot($prow[$tStatus], $pcol[$tStatus]);
  $scplot[$i]->mark->SetFillColor($tColor);
  $scplot[$i]->mark->SetSize(6);
  $scplot[$i]->SetWeight(1);
  $scplot[$i]->mark->SetWidth($psize);	
  $graph->Add($scplot[$i++]);
}

Add some general plotting-styles:

$graph->yaxis->scale->ticks->SupressFirst();
$graph->title->Set($title);
$graph->xaxis->title->Set($xAxisTitle);
$graph->yaxis->title->Set($yAxisTitle);
$graph->xgrid->Show(false,false);
$graph->ygrid->Show(false,false);
$graph->SetTickDensity(TICKD_DENSE);
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->SetColor("black");
$graph->SetMarginColor("black");
$graph->xaxis->SetColor("white"); 
$graph->xaxis->title->SetColor("white"); 
$graph->yaxis->SetColor("white"); 
$graph->yaxis->title->SetColor("white"); 
$graph->title->SetColor("white"); 
// Display the graph
$graph->Stroke();
?> 

That's it.

For debugging purpose it's also nice to see only the plot, so just type the adress of the plot, that is given to the draw-function directly into the http: of your browser.


Log In