source: trunk/FACT++/www/viewer/index.php@ 17821

Last change on this file since 17821 was 17821, checked in by tbretz, 11 years ago
Make it easier to debug printing the executed command.
File size: 11.0 KB
Line 
1<?php
2
3if (!extension_loaded('v8js'))
4 die("V8Js missing");
5
6$path = array(
7 "cal" => "/daq/caltest/",
8 "raw" => "/daq/raw/",
9 "mc" => "/daq/mctest/",
10 );
11
12if (isset($_POST['editor1']) || isset($_POST['editor2']))
13{
14 $isOne = isset($_POST['editor1']);
15
16 $source = $isOne ? $_POST['editor1'] : $_POST['editor2'];
17 if (!isset($_POST['files']))
18 $name = $isOne ? "proc.js" : "main.js";
19 else
20 $name = $_POST['files'][0];
21
22 header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
23 header('Cache-Control: public'); // needed for i.e.
24 header('Content-Type: text/plain');
25 header('Content-Transfer-Encoding: Text');
26 header('Content-Disposition: attachment; filename="'.$name.'"');
27 print(str_replace("\r", "", $source));
28 return;
29}
30
31// This is a pretty weird hack because it does first convert
32// all data to ascii (json) to print it...
33if (isset($_POST['data']))
34{
35 header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
36 header('Cache-Control: public'); // needed for i.e.
37 header('Content-Type: text/plain');
38 header('Content-Transfer-Encoding: Text');
39 header('Content-Disposition: attachment; filename="data.txt"');
40
41 $json = json_decode($_POST['data']);
42
43 $n = count($json[0]);
44 $cnt = count($json);
45
46 for ($i=0; $i<$n; $i++)
47 {
48 print($i." ".$json[0][$i]);
49
50 if ($cnt>1)
51 print(" ".$json[1][$i]);
52 if ($cnt>2)
53 print(" ".$json[2][$i]);
54 if ($cnt>3)
55 print(" ".$json[3][$i]);
56 print("\n");
57 }
58 return;
59}
60
61if (!isset($_POST['file']) || !isset($_POST['event']) || !isset($_POST['pixel']))
62{
63 /*
64 function getList($path)
65 {
66 $hasdir = false;
67
68 $list = array();
69 foreach (new DirectoryIterator($path) as $file)
70 {
71 if ($file->isDot())
72 continue;
73
74 $name = $file->getFilename();
75
76
77 if ($file->isDir())
78 {
79 $list[$name] = getList($path."/".$name);
80 $hasdir = true;
81 }
82
83 if ($file->isFile() && $file->isReadable())
84 {
85 if (substr($name, 12)!=".fits.fz")
86 continue;
87
88 array_push($list, substr($name, 9, 3));
89 }
90 }
91
92 if (!$hasdir)
93 sort($list);
94 return $list;
95 }
96 */
97
98 function getList(&$list, $path, $ext, $id, $sub = "")
99 {
100 $hasdir = false;
101
102 $dir = new DirectoryIterator($path."/".$sub);
103 foreach ($dir as $file)
104 {
105 if ($file->isDot())
106 continue;
107
108 $name = $file->getFilename();
109
110 if ($file->isDir())
111 getList($list, $path, $ext, $id, $sub."/".$name);
112
113 if (!$file->isFile() || !$file->isReadable())
114 continue;
115
116 if (substr($name, -strlen($ext))!=$ext)
117 continue;
118
119 $rc = substr($name, 0, 4)."/".substr($name, 4, 2)."/".substr($name, 6, 2)."-".substr($name, 9, 3);
120
121 if (!isset($list[$rc]))
122 $list[$rc] = 0;
123
124 $list[$rc] |= $id;
125 }
126 }
127
128 try
129 {
130 $list = array();
131 getList($list, $path['raw'], ".fits.fz", 1);
132 getList($list, $path['raw'], ".drs.fits", 2);
133 getList($list, $path['cal'], ".fits.fz", 4);
134 getList($list, $path['mc'], ".fits", 8);
135 ksort($list);
136 }
137 catch (Exception $e)
138 {
139 return header('HTTP/1.0 400 '.$e->getMessage());
140 }
141
142 print(json_encode($list));
143 return;
144}
145
146$event = intval($_POST['event']);
147$pixel = intval($_POST['pixel']);
148
149function get($handle, $format, $count = 1)
150{
151 $size = 0;
152 switch ($format)
153 {
154 case 'd': $size = 8; break;
155 case 'L': $size = 4; break;
156 case 'S': $size = 2; break;
157 case 's': $size = 2; break;
158 }
159
160 if ($size==0)
161 return;
162
163 $binary = fread($handle, $size*$count);
164 $data = unpack($format.$count, $binary);
165
166 return $count==1 ? $data[1] : $data;
167}
168
169//ini_set("memory_limit", "64M");
170
171//define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
172// E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
173
174$rc = array();
175$rc['startPhp'] = microtime(true);
176
177// ============================ Read data from file ==========================
178$file = $_POST['file'];
179
180$y = substr($file, 0, 4);
181$m = substr($file, 5, 2);
182$d = substr($file, 8, 2);
183$r = substr($file, 11, 3);
184
185$rootpath = isset($_POST['calibrated']) && !isset($_POST['drsfile']) ? $path['cal'] : $path['raw'];
186$extension = isset($_POST['drsfile']) ? ".drs.fits" : ".fits.fz";
187$filename = $rootpath.$y."/".$m."/".$d."/".$y.$m.$d."_".$r.$extension;
188
189if (isset($_POST['montecarlo']))
190{
191 $rootpath = $path['mc'];
192 $extension = ".fits";
193 $filename = $rootpath.$y.$m.$d."_".$r.$extension;
194}
195
196if (!file_exists($filename))
197 return header("HTTP/1.0 400 File '".$file."' not found.");
198
199$command = "/home/fact/FACT++/getevent ".$filename." ".$event." 2> /dev/null";
200$file = popen($command, "r");
201if (!$file)
202 return header('HTTP/1.0 400 Could not open pipe.');
203
204$evt = array();
205$fil = array();
206
207$fil['runType'] = trim(fread($file, 80));
208if (feof($file))
209 return header('HTTP/1.0 400 Data not available.');
210
211$fil['runStart'] = get($file, "d");
212$fil['runEnd'] = get($file, "d");
213$fil['drsFile'] = get($file, "s");
214$fil['numEvents'] = get($file, "L");
215$fil['scale'] = get($file, "s");
216$evt['numRoi'] = get($file, "L");
217$evt['numPix'] = get($file, "L");
218$evt['eventNumber'] = get($file, "L");
219//$evt['triggerNumber'] = get($file, "L");
220$evt['triggerType'] = get($file, "S");
221$evt['unixTime'] = get($file, "L", 2);
222
223if (isset($_POST['source1']))
224{
225 // Read the data and copy it from an associative array to an Array
226 // (this is just nicer and seems more logical)
227 $binary = array();
228 for ($i=0; $i<$evt['numPix']; $i++)
229 $binary[$i] = fread($file, 2*$evt['numRoi']);
230 /*
231 $data = array();
232 for ($i=0; $i<$evt['numPix']; $i++)
233 {
234 $var = get($file, "s", $evt['numRoi']);
235 $data[$i] = array();
236 for ($j=0; $j<$evt['numRoi']; $j++)
237 $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
238 }*/
239}
240
241if (feof($file))
242 return header('HTTP/1.0 400 Data from file incomplete.');
243
244pclose($file);
245
246if ($fil['numEvents']==0)
247 return header('HTTP/1.0 400 Could not read event.');
248
249// =========================== Decode trigger type ===========================
250
251$typ = $evt['triggerType'];
252$evt['trigger'] = array();
253if ($typ!=0 && ($typ & 0x8703)==0)
254 array_push($evt['trigger'], "PHYS");
255if (($typ&0x0100)!=0)
256 array_push($evt['trigger'], "LPext");
257if (($typ&0x0200)!=0)
258 array_push($evt['trigger'], "LPint");
259if (($typ&0x0400)!=0)
260 array_push($evt['trigger'], "PED");
261if (($typ&0x8000)!=0)
262 array_push($evt['trigger'], "TIM");
263if (($typ&0x0001)!=0)
264 array_push($evt['trigger'], "EXT1");
265if (($typ&0x0002)!=0)
266 array_push($evt['trigger'], "EXT2");
267
268// =============================== Some data =================================
269
270//require_once("neighbors.php");
271
272//$rc['neighbors'] = $neighbors;
273$rc['event'] = $evt;
274$rc['file'] = $fil;
275$rc['event']['index'] = $event;
276$rc['event']['pixel'] = $pixel;
277//$rc['waveform'] = $data[$pixel];
278
279// Get execution times
280$now = microtime(true);
281
282if (isset($_POST['source1']))
283{
284 // =============================== Run Javascript ============================
285
286/*
287$JS = <<< EOT
288require("path/to/module1");
289EOT;
290
291$v8 = new V8Js();
292$v8->setModuleLoader(function($module) {
293 switch ($module) {
294 case 'path/to/module1':
295 return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
296
297 case 'path/to/module2':
298 return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
299
300 default:
301 return 'print(' . json_encode($module . PHP_EOL) . ');';
302 }
303});
304*/
305
306 // V8Js global methods: exit, print, sleep, var_dump, require
307 //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
308 $v8 = new V8Js("$"/*, array("data"=>"data")*/);
309
310 //$v8 = new V8Js("$", array("data"=>"data"));
311
312 //V8Js::registerExtension("exit", "1");
313 //$v8 = new V8Js("$", array(), array("exit"));
314
315 //$v8->func = function($a) { echo "Closure with param $a\n"; };
316 //$v8->greeting = "From PHP with love!";
317
318 // This is much faster than the variables option in the constructor
319
320 $roi = $evt['numRoi'];
321
322 //$v8->data = $data;
323 //$v8->test = array();;
324 //$v8->data = array();
325 $v8->nroi = $evt['numRoi'];
326 $v8->npix = $evt['numPix'];
327 $v8->trigger = $evt['trigger'];
328 $v8->event = $evt;
329 $v8->file = $fil;
330// $v8->neighbors = $neighbors;
331 $v8->clone = function($data) { return $data; };
332 $v8->scale = $fil['scale']==0 ? 2000./4096. : 1./$fil['scale'];
333 $v8->unpack = function($i)
334 {
335 global $binary, $roi, $v8;
336 $u = unpack("s".$roi, $binary[$i]);
337 $arr = array();
338 for ($i=0; $i<$roi; $i++)
339 $arr[$i] = $u[$i+1]*$v8->scale;
340 return $arr;
341 };
342
343 // 10, 445, 91.75 MiB
344 // 376, 720, 91.75 MiB
345
346 $internal = file_get_contents("internal.js");
347
348 // Buffer output from javascript
349 ob_start();
350
351 $rc['timeJS'] = array();
352 $rc['startJs'] = microtime(true);
353
354 try
355 {
356 $v8->executeString($internal, 'internal.js');
357
358 // We unpack the data pixel by pixel and copy the array directly to the
359 // Javasscript array. This significantly decreases memory usage because
360 // we need only one of the super memory hungry php arrays of size ROI
361 // instead of 1440.
362 $JS = "$.data = new Array($.event.numPix); for (var i=0; i<$.event.numPix; i++) $.data[i] = $.unpack(i);";
363 $v8->executeString($JS, 'internal');
364
365 $rc['timeJs'][0] = (microtime(true) - $rc['startJs']);
366
367 $JS = "'use strict'; function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
368 $rc['waveform'] = $v8->executeString($JS, 'proc');
369
370 $rc['timeJs'][1] = (microtime(true) - $rc['startJs']);
371
372 if (isset($_POST['source2']))
373 {
374 $JS = "'use strict'; (function main(){\n".$_POST['source2']."\n})();";
375 $rc['ret'] = $v8->executeString($JS, 'main');
376 }
377
378 // This is supposed to work, but it seems it does not...
379 //$v8->proc = $rc['ret']->proc;
380 //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
381 }
382 catch (V8JsException $e)
383 {
384 $rc['err'] = array();
385 $rc['err']['message'] = $e->getMessage();
386 $rc['err']['file'] = $e->getJsFileName();
387 $rc['err']['sourceLine'] = $e->getJsSourceLine();
388 $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
389 $rc['err']['trace'] = $e->getJsTrace();
390 }
391
392 $rc['timeJs'][2] = (microtime(true) - $rc['startJs']);
393
394 // Copy output buffer and clean it
395 $rc['debug'] = ob_get_contents();
396 ob_end_clean();
397}
398
399$rc['memory'] = memory_get_peak_usage(true)/1024/1024;
400$rc['timePhp'] = (microtime(true) - $rc['startPhp']);
401
402//unset($rc['neighbors']);
403
404// Output result as JSON object
405print(json_encode($rc));
406?>
Note: See TracBrowser for help on using the repository browser.