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

Last change on this file since 17928 was 17856, checked in by tbretz, 11 years ago
Added the possibility to retrieve different file names in the additional controls and the possibility to get the geometry.
File size: 11.2 KB
Line 
1<?php
2
3if (!extension_loaded('v8js'))
4 die("V8Js missing");
5
6$path = array(
7 "cal" => "/daq/www/cal/",
8 "raw" => "/daq/raw/",
9 "mc" => "/daq/www/mc/",
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="'.$_POST['name'].'"');
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.fz", 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.fz";
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$fil['isMC'] = isset($_POST['montecarlo']);
207$fil['isDRS'] = isset($_POST['drsfile']);
208$fil['isCalibrated'] = isset($_POST['calibrated']);
209
210$fil['runType'] = trim(fread($file, 80));
211if (feof($file))
212 return header('HTTP/1.0 400 Data not available.');
213
214$fil['runStart'] = get($file, "d");
215$fil['runEnd'] = get($file, "d");
216$fil['drsFile'] = get($file, "s");
217$fil['numEvents'] = get($file, "L");
218$fil['scale'] = get($file, "s");
219$evt['numRoi'] = get($file, "L");
220$evt['numPix'] = get($file, "L");
221$evt['eventNumber'] = get($file, "L");
222//$evt['triggerNumber'] = get($file, "L");
223$evt['triggerType'] = get($file, "S");
224$evt['unixTime'] = get($file, "L", 2);
225
226if (isset($_POST['source1']))
227{
228 // Read the data and copy it from an associative array to an Array
229 // (this is just nicer and seems more logical)
230 $binary = array();
231 for ($i=0; $i<$evt['numPix']; $i++)
232 $binary[$i] = fread($file, 2*$evt['numRoi']);
233 /*
234 $data = array();
235 for ($i=0; $i<$evt['numPix']; $i++)
236 {
237 $var = get($file, "s", $evt['numRoi']);
238 $data[$i] = array();
239 for ($j=0; $j<$evt['numRoi']; $j++)
240 $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
241 }*/
242}
243
244if (feof($file))
245 return header('HTTP/1.0 400 Data from file incomplete.');
246
247pclose($file);
248
249if ($fil['numEvents']==0)
250 return header('HTTP/1.0 400 Could not read event.');
251
252// =========================== Decode trigger type ===========================
253
254$typ = $evt['triggerType'];
255$evt['trigger'] = array();
256if ($typ!=0 && ($typ & 0x8703)==0)
257 array_push($evt['trigger'], "PHYS");
258if (($typ&0x0100)!=0)
259 array_push($evt['trigger'], "LPext");
260if (($typ&0x0200)!=0)
261 array_push($evt['trigger'], "LPint");
262if (($typ&0x0400)!=0)
263 array_push($evt['trigger'], "PED");
264if (($typ&0x8000)!=0)
265 array_push($evt['trigger'], "TIM");
266if (($typ&0x0001)!=0)
267 array_push($evt['trigger'], "EXT1");
268if (($typ&0x0002)!=0)
269 array_push($evt['trigger'], "EXT2");
270
271// =============================== Some data =================================
272
273//require_once("neighbors.php");
274
275//$rc['neighbors'] = $neighbors;
276$rc['event'] = $evt;
277$rc['file'] = $fil;
278$rc['event']['index'] = $event;
279$rc['event']['pixel'] = $pixel;
280//$rc['waveform'] = $data[$pixel];
281
282// Get execution times
283$now = microtime(true);
284
285if (isset($_POST['source1']))
286{
287 // =============================== Run Javascript ============================
288
289/*
290$JS = <<< EOT
291require("path/to/module1");
292EOT;
293
294$v8 = new V8Js();
295$v8->setModuleLoader(function($module) {
296 switch ($module) {
297 case 'path/to/module1':
298 return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
299
300 case 'path/to/module2':
301 return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
302
303 default:
304 return 'print(' . json_encode($module . PHP_EOL) . ');';
305 }
306});
307*/
308
309 // V8Js global methods: exit, print, sleep, var_dump, require
310 //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
311 $v8 = new V8Js("$"/*, array("data"=>"data")*/);
312
313 //$v8 = new V8Js("$", array("data"=>"data"));
314
315 //V8Js::registerExtension("exit", "1");
316 //$v8 = new V8Js("$", array(), array("exit"));
317
318 //$v8->func = function($a) { echo "Closure with param $a\n"; };
319 //$v8->greeting = "From PHP with love!";
320
321 // This is much faster than the variables option in the constructor
322
323 $roi = $evt['numRoi'];
324
325 //$v8->data = $data;
326 //$v8->test = array();;
327 //$v8->data = array();
328 $v8->nroi = $evt['numRoi'];
329 $v8->npix = $evt['numPix'];
330 $v8->trigger = $evt['trigger'];
331 $v8->event = $evt;
332 $v8->file = $fil;
333// $v8->neighbors = $neighbors;
334 $v8->clone = function($data) { return $data; };
335 $v8->scale = $fil['scale']==0 ? 2000./4096. : 1./$fil['scale'];
336 $v8->unpack = function($i)
337 {
338 global $binary, $roi, $v8;
339 $u = unpack("s".$roi, $binary[$i]);
340 $arr = array();
341 for ($i=0; $i<$roi; $i++)
342 $arr[$i] = $u[$i+1]*$v8->scale;
343 return $arr;
344 };
345
346 // 10, 445, 91.75 MiB
347 // 376, 720, 91.75 MiB
348
349 $internal = file_get_contents("internal.js");
350
351 // Buffer output from javascript
352 ob_start();
353
354 $rc['timeJS'] = array();
355 $rc['startJs'] = microtime(true);
356
357 try
358 {
359 $v8->executeString($internal, 'internal.js');
360
361 // We unpack the data pixel by pixel and copy the array directly to the
362 // Javasscript array. This significantly decreases memory usage because
363 // we need only one of the super memory hungry php arrays of size ROI
364 // instead of 1440.
365 $JS = "$.data = new Array($.event.numPix); for (var i=0; i<$.event.numPix; i++) $.data[i] = $.unpack(i);";
366 $v8->executeString($JS, 'internal');
367
368 $rc['timeJs'][0] = (microtime(true) - $rc['startJs']);
369
370 $JS = "'use strict'; function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
371 $rc['waveform'] = $v8->executeString($JS, 'proc');
372
373 $rc['timeJs'][1] = (microtime(true) - $rc['startJs']);
374
375 if (isset($_POST['source2']))
376 {
377 $JS = "'use strict'; (function main(){\n".$_POST['source2']."\n})();";
378 $rc['ret'] = $v8->executeString($JS, 'main');
379 }
380
381 // This is supposed to work, but it seems it does not...
382 //$v8->proc = $rc['ret']->proc;
383 //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
384 }
385 catch (V8JsException $e)
386 {
387 $rc['err'] = array();
388 $rc['err']['message'] = $e->getMessage();
389 $rc['err']['file'] = $e->getJsFileName();
390 $rc['err']['sourceLine'] = $e->getJsSourceLine();
391 $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
392 $rc['err']['trace'] = $e->getJsTrace();
393 }
394
395 $rc['timeJs'][2] = (microtime(true) - $rc['startJs']);
396
397 // Copy output buffer and clean it
398 $rc['debug'] = ob_get_contents();
399 ob_end_clean();
400}
401
402$rc['memory'] = memory_get_peak_usage(true)/1024/1024;
403$rc['timePhp'] = (microtime(true) - $rc['startPhp']);
404
405//unset($rc['neighbors']);
406
407// Output result as JSON object
408print(json_encode($rc));
409?>
Note: See TracBrowser for help on using the repository browser.