getDAQData.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <html>
  2. <head>
  3. <title>getDAQData</title>
  4. </head>
  5. <body>
  6. <?php
  7. function getXMLValue($name, $fileip)
  8. {
  9. $posA = strpos($fileip, '<' . $name . '>');
  10. $posB = strpos($fileip, '</' . $name . '>');
  11. $len = strlen($name) + 2;
  12. return substr($fileip, $posA + $len, $posB - $posA - $len);
  13. }
  14. function getADCData($data, $start, $min, $max)
  15. {
  16. $counts = 0;
  17. for ($i = 0; $i < 1000; $i++)
  18. {
  19. $help = substr($data, 0, strpos($data, ","));
  20. if ($i >= $min-$start && $i <= $max-$start) $counts = $counts + $help;
  21. $data = substr($data, strpos($data, ",") + 1);
  22. }
  23. return $counts;
  24. }
  25. // get region of interest
  26. $adeiChannel = array(
  27. array(
  28. "fpd__katrin_rep__0__118",
  29. "fpd__katrin_rep__0__80"
  30. ),
  31. array(
  32. "hv__katrin-hv_rep__0__87",
  33. "hv__katrin-hv_rep__0__95"
  34. )
  35. );
  36. $username = 'katrin';
  37. $password = 'neutrino';
  38. $context = stream_context_create(array(
  39. 'http' => array(
  40. 'header' => "Authorization: Basic " . base64_encode("$username:$password"), "Connection: close\r\n"
  41. )
  42. ));
  43. for ($id = 0; $id < sizeof($adeiChannel); $id++)
  44. {
  45. $adeiChannels = "";
  46. for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
  47. {
  48. $adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
  49. }
  50. $adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);
  51. $fileip = file_get_contents("https://adei-katrin.kaas.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
  52. //$fileip = file_get_contents("http://ipekatrinbackupserv1.ipe.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
  53. //echo $fileip . "<br>";
  54. for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
  55. {
  56. $pos = strrpos($fileip, ",");
  57. $adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
  58. $fileip = substr($fileip, 0, $pos);
  59. //echo $id . "," . $id2 . "," . $adeiValue[$id][$id2] . "<br>";
  60. }
  61. }
  62. $roi = ($adeiValue[0][0] + 1000. * $adeiValue[0][1] + $adeiValue[1][0] + $adeiValue[1][1]) / 1000.; // in keV
  63. $roiMin = sprintf('%.2F', $roi - 3);
  64. $roiMax = sprintf('%.2F', $roi + 2);
  65. $roiMinAdc = sprintf('%.0F', 10. * $roiMin);
  66. $roiMaxAdc = sprintf('%.0F', 10. * $roiMax);
  67. // connect to data base
  68. $db_link = mysqli_connect('localhost', 'katrinstatus', 'pjX3nVWhnQNt7nP8', 'katrinstatus');
  69. // read data base
  70. $sql = "SELECT pulser, flt, fltCh FROM pulsers";
  71. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  72. $id = 0;
  73. while ($row = mysqli_fetch_array($db_erg, MYSQL_BOTH))
  74. {
  75. $pulserId[$id] = $row['pulser'];
  76. $flt[$id] = $row['flt'];
  77. $fltCh[$id] = $row['fltCh'];
  78. $id++;
  79. }
  80. mysqli_free_result($db_erg);
  81. // setup
  82. $daqMachine = array(438, 433, 374); // FPD, MUON, MOS
  83. // get pulser data
  84. $fileip = file_get_contents("http://katrin.kit.edu/OrcaWeb/query.php?tag=dataList&query=select%20name,counts,datastr,start%20from%20Histogram1Ds%20where%20machine_id=" . $daqMachine[0] . "%20ORDER%20BY%20name", false);
  85. for ($id = 0; $id < sizeof($pulserId); $id++)
  86. {
  87. $help = substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $flt[$id] . ',Channel '. $fltCh[$id]));
  88. if (strpos($help, "encoding") > 0) { $pulserCounts[$id] = 0; continue; }
  89. $pulserCounts[$id] = getXMLValue("counts", $help);
  90. //if (strpos($pulserCounts[$id], "encoding") !== FALSE) $pulserCounts[$id] = 0;
  91. }
  92. // write to data base
  93. for ($id = 0; $id < sizeof($pulserId); $id++)
  94. {
  95. $sql = "UPDATE pulsers SET c0 = " . $pulserCounts[$id] . " WHERE pulser = '" . $pulserId[$id] . "'";
  96. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  97. }
  98. // get FPD data
  99. $events = 0;
  100. $pixels = 148+4;
  101. for ($fltId = 2; $fltId < 10; $fltId++)
  102. {
  103. for ($chId = 0; $chId < 19; $chId++)
  104. {
  105. if ($chId < 10) $help = substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $fltId . ',Channel '. $chId));
  106. else $help = substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $fltId . ',Channel '. $chId));
  107. $start = getXMLValue("start", $help);
  108. if ($start < 1) $pixels = $pixels - 1;
  109. $events = $events + getADCData(getXMLValue("datastr", $help), $start, $roiMinAdc-5, $roiMaxAdc+5);
  110. }
  111. }
  112. $fileip = file_get_contents("http://katrin.kit.edu/OrcaWeb/query.php?tag=runInfo&query=select%20*%20from%20machines,runs%20where%20runs.machine_id=" . $daqMachine[0] . "%20and%20machines.machine_id=" . $daqMachine[0], false);
  113. $time = getXMLValue("elapsedTime", $fileip);
  114. $run = getXMLValue("run", $fileip);
  115. $rate = sprintf('%.3F', $events / $time);
  116. $rateError = sprintf('%.3F', sqrt($events) / $time);
  117. $sql = "UPDATE detector SET rate = " . $rate . " , rateError = " . $rateError . " , roiMin = " . $roiMin . " , roiMax = " . $roiMax . " , pixels = " . $pixels . " , run = " . $run;
  118. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  119. echo "It works!";
  120. /*
  121. // read data base
  122. for ($id = 0; $id < sizeof($adei_table); $id++)
  123. {
  124. $sql = "SELECT * FROM " . $adei_table[$id];
  125. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  126. $id2 = 0;
  127. while ($row = mysqli_fetch_array($db_erg, MYSQL_ASSOC))
  128. {
  129. $pulser[$id2][$id] = $row[$adei_column[$id]];
  130. $id2++;
  131. }
  132. mysqli_free_result($db_erg);
  133. }
  134. */
  135. /*
  136. $username = 'katrin';
  137. $password = 'neutrino';
  138. $context = stream_context_create(array(
  139. 'http' => array(
  140. 'header' => "Authorization: Basic " . base64_encode("$username:$password")
  141. )
  142. ));
  143. for ($id = 0; $id < sizeof($adeiChannel); $id++)
  144. {
  145. $adeiChannels = "";
  146. for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
  147. {
  148. $adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
  149. }
  150. $adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);
  151. $fileip = file_get_contents("http://katrin.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
  152. //echo $fileip . "<br>";
  153. for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
  154. {
  155. $pos = strrpos($fileip, ",");
  156. $adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
  157. $fileip = substr($fileip, 0, $pos);
  158. //echo $id . "," . $id2 . "," . $adeiValue[$id][$id2] . "<br>";
  159. }
  160. }
  161. */
  162. /*
  163. // ÄNDERN
  164. $sql = "UPDATE sds SET actual = 0, ideal = 0 WHERE system = 'lfcs' AND id = 2";
  165. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  166. */
  167. /*
  168. // LESEN
  169. $sql = "SELECT * FROM dcct_current WHERE id = 'lfcs 2'";
  170. $db_erg = mysqli_query($db_link, $sql) or die("Anfrage fehlgeschlagen: " . mysqli_error());
  171. while ($zeile = mysqli_fetch_array($db_erg, MYSQL_ASSOC))
  172. {
  173. echo $zeile['ideal'];
  174. }
  175. mysqli_free_result($db_erg);
  176. */
  177. /*
  178. // status info DAQ machines
  179. for ($id = 0; $id < sizeof($daqMachine); $id++)
  180. {
  181. $fileip = file_get_contents("http://katrin.kit.edu/OrcaWeb/query.php?tag=runInfo&query=select%20*%20from%20machines,runs%20where%20runs.machine_id=" . $daqMachine[$id] . "%20and%20machines.machine_id=" . $daqMachine[$id], false);
  182. $daqRun[$id] = getXMLValue("run", $fileip);
  183. $daqSubrun[$id] = getXMLValue("subrun", $fileip);
  184. $daqElapsed[$id] = getXMLValue("elapsedTime", $fileip);
  185. $daqTogo[$id] = getXMLValue("timeToGo", $fileip);
  186. $daqState[$id] = getXMLValue("state", $fileip);
  187. $daqRepeat[$id] = getXMLValue("repeatRun", $fileip);
  188. $daqOffline[$id] = getXMLValue("offline", $fileip);
  189. $daqTimed[$id] = getXMLValue("timedRun", $fileip);
  190. }
  191. */
  192. /*
  193. // get pulser data
  194. $fileip = file_get_contents("http://katrin.kit.edu/OrcaWeb/query.php?tag=dataList&query=select%20dataset_id,name,counts%20from%20Histogram1Ds%20where%20machine_id=" . $daqMachine[0] . "%20ORDER%20BY%20name", false);
  195. $counts[0] = getXMLValue("counts", substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $pulser[0][1] . ',Channel '. $pulser[0][2])));
  196. $counts[1] = getXMLValue("counts", substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $pulser[1][1] . ',Channel '. $pulser[1][2])));
  197. $counts[2] = getXMLValue("counts", substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $pulser[2][1] . ',Channel '. $pulser[2][2])));
  198. $counts[3] = getXMLValue("counts", substr($fileip, strpos($fileip, 'FLT,Energy,Crate 0,Station ' . $pulser[3][1] . ',Channel '. $pulser[3][2])));
  199. echo $pulser[0][0] . " " . $counts[0];
  200. echo "<br>";
  201. echo $pulser[1][0] . " " . $counts[1];
  202. echo "<br>";
  203. echo $pulser[2][0] . " " . $counts[2];
  204. echo "<br>";
  205. echo $pulser[3][0] . " " . $counts[3];
  206. */
  207. ?>
  208. </body>
  209. </html>