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

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