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

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