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

Last change on this file since 17718 was 17718, checked in by tbretz, 11 years ago
Increased memory limit to allow for roi=1024 -- There is unfortunately no easy way to reduce the memory consumption of php arrays... they are in the order of 100 bytes per entry\! Therefore a maximum event size is 100 MB \!
File size: 7.0 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 'L': $size = 4; break;
114 case 'S': $size = 2; break;
115 case 's': $size = 2; break;
116 }
117
118 if ($size==0)
119 return;
120
121 $data = fread($handle, $size*$count);
122 $data = unpack($format.$count, $data);
123
124 return $count==1 ? $data[1] : $data;
125}
126
127// This is to allow for events of ROI=1024
128ini_set("memory_limit", "384M");
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
146$file = popen("/home/fact/FACT++/getevent ".$filename." ".$event." 2> /dev/null", "r");
147if (!$file)
148 return header('HTTP/1.0 400 Could not open file.');
149
150$evt = array();
151
152$evt['numEvents'] = get($file, "L");
153$evt['numRoi'] = get($file, "L");
154$evt['numPix'] = get($file, "L");
155$evt['eventNumber'] = get($file, "L");
156$evt['triggerNumber'] = get($file, "L");
157$evt['triggerType'] = get($file, "S");
158$evt['unixTime'] = get($file, "L", 2);
159
160if (isset($_POST['source1']))
161{
162 // Read the data and copy it from an associative array to an Array
163 // (this is just nicer and seems more logical)
164 $data = array();
165 for ($i=0; $i<$evt['numPix']; $i++)
166 {
167 //$data[$i] = get($file, "s", $evt['numRoi']);
168 $var = get($file, "s", $evt['numRoi']);
169
170 $data[$i] = array();
171 for ($j=0; $j<$evt['numRoi']; $j++)
172 $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
173 }
174}
175
176//if (feof($file))
177// return;
178
179pclose($file);
180
181if ($evt['numEvents']==0)
182 return header('HTTP/1.0 400 Could not read event.');
183
184// =============================== Copy waveform =============================
185
186$rc['event'] = $evt;
187$rc['event']['index'] = $event;
188$rc['event']['pixel'] = $pixel;
189//$rc['waveform'] = $data[$pixel];
190
191// Get execution times
192$now = microtime(true);
193
194if (isset($_POST['source1']))
195{
196 // =============================== Run Javascript ============================
197
198/*
199$JS = <<< EOT
200require("path/to/module1");
201EOT;
202
203$v8 = new V8Js();
204$v8->setModuleLoader(function($module) {
205 switch ($module) {
206 case 'path/to/module1':
207 return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
208
209 case 'path/to/module2':
210 return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
211
212 default:
213 return 'print(' . json_encode($module . PHP_EOL) . ');';
214 }
215});
216*/
217
218 // V8Js global methods: exit, print, sleep, var_dump, require
219 //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
220 $v8 = new V8Js("$"/*, array("data"=>"data")*/);
221
222 //$v8 = new V8Js("$", array("data"=>"data"));
223
224 //V8Js::registerExtension("exit", "1");
225 //$v8 = new V8Js("$", array(), array("exit"));
226
227 //$v8->func = function($a) { echo "Closure with param $a\n"; };
228 //$v8->greeting = "From PHP with love!";
229
230 // This is much faster than the variables option in the constructor
231 $v8->data = $data;
232 $v8->event = $evt;
233 $v8->clone = function($data) { return $data; };
234
235 $rc['startJs'] = microtime(true);
236
237 // Buffer output from javascript
238 ob_start();
239
240 try
241 {
242 $JS = "function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
243 $rc['waveform'] = $v8->executeString($JS, 'proc');
244
245 if (isset($_POST['source2']))
246 {
247 $JS = "(function main(){\n".$_POST['source2']."\n})();";
248 $rc['ret'] = $v8->executeString($JS, 'main');
249 }
250
251 // This is supposed to work, but it seems it does not...
252 //$v8->proc = $rc['ret']->proc;
253 //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
254 }
255 catch (V8JsException $e)
256 {
257 $rc['err'] = array();
258 $rc['err']['message'] = $e->getMessage();
259 $rc['err']['file'] = $e->getJsFileName();
260 $rc['err']['sourceLine'] = $e->getJsSourceLine();
261 $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
262 $rc['err']['trace'] = $e->getJsTrace();
263 }
264
265 // Copy output buffer and clean it
266 $rc['debug'] = ob_get_contents();
267 ob_end_clean();
268
269 // Get execution times
270 $now = microtime(true);
271
272 $rc['timeJs'] = ($now - $rc['startJs']);
273}
274
275$rc['timePhp'] = ($now - $rc['startPhp']);
276
277// Output result as JSON object
278print(json_encode($rc));
279
280?>
Note: See TracBrowser for help on using the repository browser.