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

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