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

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