source: trunk/FACT++/www/viewer/index.js@ 19779

Last change on this file since 19779 was 19415, checked in by tbretz, 6 years ago
FACT camera as default, correctly center cameras, slight modification to under-/overflow color, fixed calculation of image parameters.
File size: 46.3 KB
Line 
1'use strict';
2
3// ==========================================================================
4
5var camera_type = "FACT";
6
7var scale = camera_type=="FACT" ? 83 : 22;
8var npix = camera_type=="FACT" ? 1440 : 64;
9var diameter = camera_type=="FACT" ? 0.1111 : 1.5;
10var center = camera_type=="FACT" ? [0.5,0] : [0,0]
11
12function onRightMouseClick(event)
13{
14 if (event.button!=2)
15 return;
16
17 var strData = event.target.toDataURL("image/png");
18
19 var img = document.createElement("img");
20 img.src = strData;
21
22 $(img).css({ "z-index": "9999", "position": "absolute" });
23 $(img).insertBefore($(event.target));
24
25 setTimeout(function () { $(img).remove(); }, 100);
26}
27
28(function ($)
29 {
30
31 $.plot.plugins.push({
32 init: function(plot, classes)
33 {
34 plot.hooks.bindEvents.push(function(plot, eventHolder) { eventHolder.mousedown(onRightMouseClick); });
35 plot.hooks.shutdown.push(function(plot, eventHolder) { eventHolder.unbind("mousedown", onRightMouseClick); });
36 },
37 name: 'saveAsImage',
38 version: '1.0'
39 });
40
41 })(jQuery);
42
43// ==========================================================================
44
45var editor1;
46var editor2;
47var plot;
48
49function debug(txt)
50{
51 var dbg = document.getElementById("debug");
52 dbg.appendChild(document.createTextNode(txt));
53 dbg.appendChild(document.createElement("br"));
54}
55
56function setupAccordion(accordion, container, inactive)
57{
58 function onAccordion(event, ui)
59 {
60 if (ui.oldHeader.length==0)
61 $(container).slideDown(400);
62 else
63 $(container).slideUp(400);
64 }
65
66 var config = { collapsible: true };
67
68 if (inactive)
69 {
70 config.active = false;
71 $(container).hide();
72 }
73
74 var acc = $(accordion);
75
76 acc.accordion(config);
77 acc.on("accordionbeforeactivate", onAccordion);
78}
79
80function onResize(event, ui)
81{
82 if (!ui.size)
83 return;
84
85 $(event.target.id).css({marginRight:'2px'});
86
87 var editor = event.target.id=="textcontainer1" ? editor1 : editor2;
88
89 editor.setSize("100%", ui.size.height);
90 editor.refresh();
91}
92
93function setSize(id, w, h)
94{
95 w = parseInt(w);
96 h = parseInt(h);
97
98 $("#"+id).width(w);
99 $("#"+id).height(h);
100
101 document.getElementById(id).width=w;
102 document.getElementById(id).height=h;
103}
104
105function onResizeGrid(id)
106{
107 var w = document.getElementById(id+"container").clientWidth/4;
108
109 var offy = 0;
110 var offx = 5;
111
112 var cont = document.getElementById("center"+id).childNodes[0];
113
114 var nn;
115 if (cont)
116 {
117 nn = parseInt(cont.id[cont.id.length-1]);
118 setSize(id+nn, w*2, w*2);
119 }
120
121 if (nn!=1)
122 setSize(id+'1', w-offx, w-offy);
123 if (nn!=2)
124 setSize(id+'2', w-offx, w-offy);
125 if (nn!=3)
126 setSize(id+'3', w-offx, w-offy);
127 if (nn!=4)
128 setSize(id+'4', w-offx, w-offy);
129
130 document.getElementById("center"+id).width=parseInt(w*2);
131
132 setSize('cont'+id+'1', w, w);
133 setSize('cont'+id+'2', w, w);
134 setSize('cont'+id+'3', w, w);
135 setSize('cont'+id+'4', w, w);
136}
137
138function onResizeCameras(event, ui)
139{
140 onResizeGrid('camera');
141
142 drawFullCam("camera1");
143 drawFullCam("camera2");
144 drawFullCam("camera3");
145 drawFullCam("camera4");
146}
147
148function onResizeHistograms(event, ui)
149{
150 onResizeGrid('hist');
151}
152
153function createEditor(textarea)
154{
155 var editor;
156
157 var config =
158 {
159 //value: "function myScript(){return 100;}\n",
160 mode: { name: "text/typescript", globalVars: true },
161 indentUnit: 4,
162 styleActiveLine: true,
163 matchBrackets: true,
164 lineNumbers: true,
165 foldGutter: true,
166 lint: true,
167 highlightSelectionMatches: {showToken: /\w/},
168 gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
169 extraKeys: {
170 //"Ctrl-D": "duplicateLine",
171 //"Alt--": "goToBracket",
172 //"Ctrl-H": "findPrev",
173 "Ctrl-Down": "autocomplete",
174 "Tab": "indentAuto",
175 "Ctrl-Y": "deleteLine",
176 "Ctrl-.": function(cm) {
177 cm.foldCode(cm.getCursor());
178 },
179 "F11": function(cm) {
180 editor.setOption("fullScreen", !editor.getOption("fullScreen"));
181 },
182 "Ctrl-R": function(cm) {
183 editor.execCommand("replace");
184 },
185 "Esc": function(cm) {
186 if (editor.getOption("fullScreen")) editor.setOption("fullScreen", false);
187 },
188 "Enter": function(cm) {
189 editor.execCommand("indentAuto");
190 editor.execCommand("newlineAndIndent");
191 },
192 }
193 };
194
195 editor = CodeMirror.fromTextArea(document.getElementById(textarea), config);
196 editor.setOption("theme", "fact");
197
198 return editor;
199}
200
201function colorizeHTML(textarea)
202{
203 var config =
204 {
205 //value: "function myScript(){return 100;}\n",
206 mode: { name: "text/typescript", globalVars: true },
207 readOnly: true,
208 };
209
210 CodeMirror.fromTextArea(document.getElementById(textarea), config);
211}
212
213function setFileChecks(file)
214{
215 var list = document.getElementById("file").data;
216 if (!list)
217 return;
218
219 if (!file)
220 file = document.getElementById("file").value;
221
222 var drs = document.getElementById("drsfile");
223
224 var hasDrs = list[file]&2;
225 var hasCal = list[file]&4;
226 var isMC = list[file]&8;
227
228 if (!hasDrs || isMC)
229 $('#drsfile').prop('checked', false);
230 if (!hasCal || isMC)
231 $('#calibrated').prop('checked', false);
232
233 $('#calibrated').prop('disabled', (!hasCal && !drs.checked) || isMC);
234 $('#drsfile').prop('disabled', !hasDrs || isMC);
235
236 $('#txtcalibrated').css('color', (!hasCal && !drs.checked) || isMC ? 'darkgrey' : 'black');
237 $('#txtdrsfile').css( 'color', !hasDrs || isMC ? 'darkgrey' : 'black');
238 $('#txtmontecarlo').css('color', !isMC ? 'darkgrey' : 'black');
239
240 $('#montecarlo').prop('checked', isMC);
241}
242
243function disableControls(disabled)
244{
245 $('#submit').prop('disabled', disabled);
246 $('#getcamera').prop('disabled', disabled);
247 $('#getwaveforms').prop('disabled', disabled);
248 $('#event').prop('disabled', disabled);
249 $('#pixel').prop('disabled', disabled);
250 $('#cbpx').prop('disabled', disabled);
251 $('#cbpx-c').prop('disabled', disabled);
252 $('#cbpx-b').prop('disabled', disabled);
253 $('#cbpx-p').prop('disabled', disabled);
254 $('#cbpx-x').prop('disabled', disabled);
255 $('#file').prop('disabled', disabled);
256
257 if (disabled)
258 {
259 $('#calibrated').prop('disabled', true);
260 $('#drsfile').prop('disabled', true);
261 $('#montecarlo').prop('disabled', true);
262 }
263 else
264 {
265 $(document.getElementById("file").value ? '#event' : '#file').focus();
266 setFileChecks();
267 }
268}
269
270function onGetCameras()
271{
272 var arr =
273 [
274 document.getElementById("camera1").dataAbs,
275 document.getElementById("camera2").dataAbs,
276 document.getElementById("camera3").dataAbs,
277 document.getElementById("camera4").dataAbs,
278 ];
279
280 $('#controls > input[name=data]').val(JSON.stringify(arr));
281 $('#controls > input[name=name]').val('cameras.txt');
282 $('#controls').attr('action','index.php');
283 $('#controls').submit();
284}
285
286function onGetWaveforms()
287{
288 var arr = document.getElementById("waveform").data;
289
290 $('#controls > input[name=data]').val(JSON.stringify(arr));
291 $('#controls > input[name=name]').val('waveforms.txt');
292 $('#controls').attr('action','index.php');
293 $('#controls').submit();
294}
295
296function onGetGeometry()
297{
298 var sqrt32 = Math.sqrt(3)/2;
299
300 var arr = [ new Array(npix), new Array(npix) ];
301
302 for (var i=0; i<npix; i++)
303 {
304 arr[0][i] = coord[i][0]*diameter;
305 arr[1][i] = coord[i][1]*diameter*sqrt32;
306 }
307
308 $('#controls > input[name=data]').val(JSON.stringify(arr));
309 $('#controls > input[name=name]').val('geometry.txt');
310 $('#controls').attr('action','index.php');
311 $('#controls').submit();
312}
313
314function onReady()
315{
316 CodeMirror.colorize(null, 'javascript');
317
318 //$('input,select').keypress(function(event) { return event.keyCode != 13; });
319
320 //colorizeHTML("code0");
321 //colorizeHTML("code1");
322
323 $("#accordion").accordion({collapsible:true,active:false,heightStyle:'content'});
324 $("#accordion").find('h3').filter(':contains(Runtime)').hide();
325 if (location.href.search('debug')==-1)
326 $("#accordion").find('h3').filter(':contains(Debug)').hide();
327
328 $("#textcontainer1").resizable({handles:"s",autoHide:true,});
329 $("#textcontainer1").on("resize", onResize);
330
331 $("#textcontainer2").resizable({handles:"s",autoHide:true,});
332 $("#textcontainer2").on("resize", onResize);
333
334 $("#cameracontainer").on("resize", onResizeCameras);
335 onResizeCameras();
336
337 $("#histcontainer").on("resize", onResizeHistograms);
338 onResizeHistograms();
339
340 $("#contcamera1").click(onClickContCamera);
341 $("#contcamera2").click(onClickContCamera);
342 $("#contcamera3").click(onClickContCamera);
343 $("#contcamera4").click(onClickContCamera);
344
345 $("#conthist1").click(onClickContHist);
346 $("#conthist2").click(onClickContHist);
347 $("#conthist3").click(onClickContHist);
348 $("#conthist4").click(onClickContHist);
349
350 $("#camera1").click(onClick);
351 $("#camera2").click(onClick);
352 $("#camera3").click(onClick);
353 $("#camera4").click(onClick);
354
355 $('#camera1').mousedown(onRightMouseClick);
356 $('#camera2').mousedown(onRightMouseClick);
357 $('#camera3').mousedown(onRightMouseClick);
358 $('#camera4').mousedown(onRightMouseClick);
359
360 editor1 = createEditor("editor1");
361 editor2 = createEditor("editor2");
362
363 setupAccordion('#accordion5', '#editorcontainer1', true);
364 setupAccordion('#accordion1', '#editorcontainer2');
365
366 $('#accordion5').on("accordionactivate", function() { $('#editorcontainer1fake').hide(); editor1.refresh(); });
367 $('#accordion1').on("accordionactivate", function() { $('#editorcontainer2fake').hide(); editor2.refresh(); });
368
369 setupAccordion('#accordion2', '#cameracontainer');
370 setupAccordion('#accordion7', '#histcontainer', true);
371 setupAccordion('#accordion3', '#waveformcontainer');
372 setupAccordion('#accordion4', '#helpcontainer', true);
373 setupAccordion('#accordion6', '#ctrlcontainer', true);
374
375 $("#selectfile1").on('change', onFile);
376 $("#selectfile2").on('change', onFile);
377
378 $(document).ajaxStart(function() { disableControls(true) }).ajaxStop(function() { disableControls(false); });
379
380 $.ajax({
381 type: "POST",
382 cache: false,
383 url: "index.php",
384 success: onFilelistReceived,
385 error: function(xhr) { if (xhr.status==0) alert("ERROR[0] - Request failed!"); else alert("ERROR[0] - "+xhr.statusText+" ["+xhr.status+"]"); }
386 });
387}
388
389function onFileSelect(event, ui)
390{
391 setFileChecks(ui.item.value);
392 document.getElementById("event").value = 0;
393 onSubmit(ui.item.value);
394}
395
396function onFilelistReceived(result)
397{
398 if (result.indexOf("<?php")>=0)
399 {
400 alert("Call to 'index.php' returned its contents.\n"+
401 "It seems the web-server does not support php.\n"+
402 "Accessing data files not possible.");
403 return;
404 }
405
406 if (result=="V8Js missing")
407 {
408 alert("The php module V8Js is missing or not properly installed.\n"+
409 "Check here for details: https://github.com/phpv8/v8js");
410 return;
411 }
412 //var dbg = document.getElementById("debug");
413
414 //var pre = document.createElement("pre");
415 //pre.appendChild(document.createTextNode(rc));
416 //dbg.appendChild(pre);
417
418 var rc;
419 try
420 {
421 rc = JSON.parse(result);
422 }
423 catch (e)
424 {
425 alert("ERROR[0] - Decoding answer:\n"+e);
426 debug(result);
427 return;
428 }
429
430 document.getElementById("file").data = rc;
431
432 var list = [ ];
433 for (var file in rc)
434 list.push(file);
435
436 var opts =
437 {
438 source: list,
439 select: onFileSelect,
440 position: { my: "right top", at: "right bottom", collision: "flipfit" },
441 };
442
443 $("#file").autocomplete(opts);
444 //document.getElementById("file").value = "2014/04/17-181";
445
446 //onSubmit("2014/04/17-181");
447}
448
449function setZoom(xfrom, xto, yfrom, yto)
450{
451 var xaxis = plot.getXAxes()[0];
452 var yaxis = plot.getYAxes()[0];
453
454 if (xfrom!==undefined)
455 xaxis.options.min = xfrom;
456 if (xto!==undefined)
457 xaxis.options.max = xto;
458
459 if (yfrom!==undefined)
460 yaxis.options.min = yfrom;
461 if (yto!==undefined)
462 yaxis.options.max = yto;
463
464 plot.setupGrid();
465 plot.draw();
466 plot.clearSelection();
467}
468
469function onPlotHover(event, pos, item)
470{
471 if (!item)
472 {
473 $("#tooltip").hide();//fadeOut(100);
474 return;
475 }
476
477 var x = item.datapoint[0].toFixed(2);
478 var y = item.datapoint[1].toFixed(2);
479
480 var tooltip = $("#tooltip");
481 tooltip.html(parseInt(x) + " / " + y);
482 tooltip.css({top: item.pageY-20, left: item.pageX+5});
483 tooltip.show();//fadeIn(200);
484}
485
486function drawHist(n)
487{
488 var canv = document.getElementById("camera"+n);
489 var hist = document.getElementById("hist"+n);
490
491 var xmin = parseFloat(document.getElementById("histmin"+n).value);
492 var xmax = parseFloat(document.getElementById("histmax"+n).value);
493 var nbins = 100;//parseInt(xmax-xmin);
494 var step = (xmax-xmin)/nbins;
495 if (step<1)
496 {
497 step = 1;
498 nbins = parseInt(xmax-xmin)+1;
499 }
500
501 var bins = new Array(nbins);
502 for (var i=0; i<nbins; i++)
503 bins[i] = [ xmin+i*step, 0 ];
504
505 var data = canv.dataAbs;
506 for (var i=0; i<1440; i++)
507 if (data[i]!==undefined/* && data[i]!==null*/)
508 {
509 var ix = parseInt((data[i]-xmin)/step);
510 if (ix>=0 && ix<nbins)
511 bins[ix][1] ++;
512 }
513
514 var opts =
515 {
516 grid: {
517 hoverable: true,
518 }
519 };
520
521 var hist = $.plot("#hist"+n, [ { data:bins, bars: {show:true} } ], opts);
522 $('#hist'+n).bind("plothover", onPlotHover);
523}
524
525function processCameraData(n, data)
526{
527 var canv = document.getElementById("camera"+n);
528 canv.dataRel = null;
529
530 if (!Array.isArray(data))
531 return;
532
533 canv.dataAbs = new Array(1440);
534 for (var i=0; i<1440; i++)
535 {
536 var val = data[map[i]];
537 if (!isNaN(val) && val!==null)
538 canv.dataAbs[i] = val;
539 }
540
541 canv.min = Math.min.apply(Math, canv.dataAbs.filter(function(e){return e!==undefined;}));
542 canv.max = Math.max.apply(Math, canv.dataAbs.filter(function(e){return e!==undefined;}));
543
544 canv.dataRel = new Array(1440);
545 for (var i=0; i<1440; i++)
546 {
547 var val = canv.dataAbs[i];
548 if (val!==undefined)
549 canv.dataRel[i] = (val-canv.min)/canv.max;
550 }
551
552 if (document.getElementById("cameraminon"+n).checked)
553 document.getElementById("cameramin"+n).value = canv.min;
554 if (document.getElementById("cameramaxon"+n).checked)
555 document.getElementById("cameramax"+n).value = canv.max;
556
557 // ---------------------------
558
559 var hist = document.getElementById("hist"+n);
560
561 hist.min = canv.min;
562 hist.max = canv.max;
563
564 if (document.getElementById("histminon"+n).checked)
565 document.getElementById("histmin"+n).value = canv.min;
566 if (document.getElementById("histmaxon"+n).checked)
567 document.getElementById("histmax"+n).value = canv.max;
568
569 drawHist(n);
570}
571
572function onDataReceived(result)
573{
574 var err = document.getElementById("error");
575 var dbg = document.getElementById("debug");
576 var con = document.getElementById("console");
577
578 //var pre = document.createElement("pre");
579 //pre.appendChild(document.createTextNode(rc));
580 //dbg.appendChild(pre);
581
582 var rc;
583 try
584 {
585 rc = JSON.parse(result);
586 if (!rc)
587 return;
588 }
589 catch (e)
590 {
591 alert("ERROR[1] - Decoding answer:\n"+e);
592 debug(result);
593 return;
594 }
595
596 debug(rc.command);
597
598 var evt = rc.event;
599 var file = rc.file;
600
601 document.getElementById("event").max = file.numEvents-1;
602 var el = document.getElementById("numevents");
603 if (el.firstChild)
604 el.removeChild(el.firstChild);
605 el.appendChild(document.createTextNode(file.numEvents));
606
607 var infotxt = "<pre>";
608 infotxt += "\nStart time: "+new Date(file.runStart*24*3600*1000).toUTCString();
609 infotxt += "\nEnd time: "+new Date(file.runEnd*24*3600*1000).toUTCString();
610 infotxt += "\nRun type: "+file.runType;
611 if (file.drsFile>=0)
612 infotxt += " [drs-step "+file.drsFile+"]";
613
614 $("#runinfo").html(infotxt);
615 $("#eventinfo").html("Trigger: "+evt.trigger.join(' | ')+" [0x"+evt.triggerType.toString(16)+"]");
616
617 if (rc.ret)
618 {
619 while (con.lastChild)
620 con.removeChild(con.lastChild);
621 }
622
623 if (rc.err)
624 {
625 while (err.lastChild)
626 err.removeChild(err.lastChild);
627
628 err.appendChild(document.createTextNode("Javascript runtime exception: "+rc.err.file+":"+rc.err.lineNumber));
629 err.appendChild(document.createTextNode("\n"));
630 err.appendChild(document.createTextNode(rc.err.sourceLine));
631 err.appendChild(document.createTextNode("\n"));
632 err.appendChild(document.createTextNode(rc.err.trace));
633
634 var editor = rc.err.file=="main" ? editor2 : editor1;
635 editor.setCursor(rc.err.lineNumber-1, 1);
636
637 $("#accordion").find('h3').filter(':contains(Runtime)').show();
638 $("#accordion").accordion("option", "active", 0);
639 }
640
641 if (rc.debug!==undefined)
642 {
643 con.appendChild(document.createTextNode(rc.debug));
644
645 debug("PHP execution:");
646 debug("Time Javascripts = "+(rc.timeJs[0]*1000).toFixed(2)+","+(rc.timeJs[1]*1000).toFixed(2)+","+(rc.timeJs[2]*1000).toFixed(2)+ " [ms]");
647 }
648
649 if (rc.ret!==undefined && Array.isArray(rc.ret))
650 {
651 var now = new Date();
652
653 if (rc.ret[0] instanceof Object)
654 processCameraData(1, rc.ret[0]);
655 else
656 processCameraData(1, rc.ret);
657
658 if (rc.ret.length>1)
659 processCameraData(2, rc.ret[1]);
660
661 if (rc.ret.length>2)
662 processCameraData(3, rc.ret[2]);
663
664 if (rc.ret.length>3)
665 processCameraData(4, rc.ret[3]);
666
667 debug("Calc Time = "+(new Date()-now)+" ms");
668 }
669
670 // We have to redraw all of them to display the changed pixel value
671 onCameraMinMax(1);
672 onCameraMinMax(2);
673 onCameraMinMax(3);
674 onCameraMinMax(4);
675
676 debug("Total time = "+(rc.timePhp*1000).toFixed(1)+" ms");
677 debug("Peak memory = "+rc.memory+" MiB");
678
679 if (Array.isArray(rc.waveform))
680 {
681 var waveform = document.getElementById("waveform");
682 waveform.data = [ ];
683
684 var data = [
685 { label: "[0] ", data: new Array(evt.numRoi) },
686 { label: "[1] ", data: new Array(evt.numRoi) },
687 { label: "[2] ", data: new Array(evt.numRoi) },
688 { label: "[3] ", data: new Array(evt.numRoi) },
689 ];
690
691 var min = [];
692 var max = [];
693 if (Array.isArray(rc.waveform) && rc.waveform.length==evt.numRoi)
694 {
695 min.push(Math.min.apply(Math, rc.waveform));
696 max.push(Math.max.apply(Math, rc.waveform));
697
698 var d = data[0].data;
699 for (var i=0; i<evt.numRoi; i++)
700 d[i] = [ i, rc.waveform[i] ];
701
702 waveform.data[0] = rc.waveform;
703 }
704
705 for (var j=0; j<4; j++)
706 {
707 var ref = rc.waveform[j];
708
709 if (Array.isArray(ref) && ref.length==evt.numRoi)
710 {
711 min.push(Math.min.apply(Math, ref));
712 max.push(Math.max.apply(Math, ref));
713
714 var d = data[j].data;
715 for (var i=0; i<evt.numRoi; i++)
716 d[i] = [ i, ref[i] ];
717
718 waveform.data[j] = ref;
719 }
720 }
721
722 waveform.ymin = Math.min.apply(Math, min);
723 waveform.ymax = Math.max.apply(Math, max);
724 waveform.xmin = 0;
725 waveform.xmax = evt.numRoi;
726
727 if (document.getElementById("waveformxminon").checked)
728 document.getElementById("waveformxmin").value = waveform.xmin;
729 if (document.getElementById("waveformxmaxon").checked)
730 document.getElementById("waveformxmax").value = waveform.xmax;
731
732 if (document.getElementById("waveformminon").checked)
733 document.getElementById("waveformmin").value = waveform.ymin;
734 if (document.getElementById("waveformmaxon").checked)
735 document.getElementById("waveformmax").value = waveform.ymax;
736
737 var xmin = document.getElementById("waveformxminon").checked ? waveform.xmin : parseInt(document.getElementById("waveformxmin").value);
738 var xmax = document.getElementById("waveformxmaxon").checked ? waveform.xmax : parseInt(document.getElementById("waveformxmax").value);
739
740 var ymin = document.getElementById("waveformminon").checked ? waveform.ymin : parseInt(document.getElementById("waveformmin").value);
741 var ymax = document.getElementById("waveformmaxon").checked ? waveform.ymax : parseInt(document.getElementById("waveformmax").value);
742
743 var opts =
744 {
745 xaxis: {
746 min: xmin-1,
747 max: xmax+1,
748 },
749 yaxis: {
750 min: ymin-5,
751 max: ymax+5,
752 },
753 series: {
754 lines: {
755 show: true
756 },
757 points: {
758 show: true,
759 symbol: 'cross',
760 }
761 },
762 selection: {
763 mode: "xy"
764 },
765 grid: {
766 hoverable: true,
767 }
768 };
769
770 plot = $.plot("#waveform", data, opts);
771
772 waveform = $('#waveform');
773 waveform.bind("plotselected", function (event, ranges)
774 {
775 setZoom(ranges.xaxis.from, ranges.xaxis.to,
776 ranges.yaxis.from, ranges.yaxis.to);
777 });
778
779 waveform.dblclick(function ()
780 {
781 var waveform = document.getElementById("waveform");
782 setZoom(waveform.xmin-1, waveform.xmax+1, waveform.ymin-5, waveform.ymax+5);
783 });
784 waveform.bind("plothover", onPlotHover);
785 }
786}
787
788function onSubmit(file, pixelOnly)
789{
790 if (!file)
791 file = document.getElementById("file").value;
792
793 var dbg = document.getElementById("debug");
794 while (dbg.lastChild)
795 dbg.removeChild(dbg.lastChild);
796
797 var active = $("#accordion").accordion("option", "active");
798 if (active==0)
799 {
800 $("#accordion").accordion("option", "active", false);
801 $("#accordion").find('h3').filter(':contains(Runtime)').hide();
802 }
803
804 var calibrated = document.getElementById("calibrated");
805 var drsfile = document.getElementById("drsfile");
806 var montecarlo = document.getElementById("montecarlo");
807 var ismc = montecarlo.checked;
808 var calib = !calibrated.disabled && calibrated.checked;
809 var drs = !drsfile.disabled && drsfile.checked;
810 var event = document.getElementById("event").value;
811 var pixel = document.getElementById("pixel").value;
812 var source1 = editor1.getValue();
813 var source2 = editor2.getValue();
814
815 var uri = "file="+file+"&event="+event+"&pixel="+map[pixel];
816 uri += "&source1="+encodeURIComponent(source1);
817 if (!pixelOnly)
818 uri += "&source2="+encodeURIComponent(source2);
819 if (calib)
820 uri += "&calibrated=1";
821 if (drs)
822 uri += "&drsfile=1";
823 if (ismc)
824 uri += "&montecarlo=1";
825
826 $.ajax({
827 type: "POST",
828 cache: false,
829 url: "index.php",
830 data: uri,
831 success: onDataReceived,
832 error: function(xhr) { if (xhr.status==0) alert("ERROR[1] - Request failed!"); else alert("ERROR[1] - "+xhr.statusText+" ["+xhr.status+"]"); }
833 });
834}
835
836function onFile(event, ui)
837{
838 var f = event.target.files[0];
839 if (!f)
840 return;
841
842 if (!f.type.match('text/plain') && !f.type.match('text/javascript') && !f.type.match('application/javascript') && !f.type.match('application/x-javascript'))
843 {
844 alert("ERROR - Unknown file type: "+f.type);
845 return;
846 }
847
848 var id = event.target.id;
849 var editor = id[id.length-1]=='1' ? editor1 : editor2;
850
851 var reader = new FileReader();
852
853 // Closure to capture the file information.
854 reader.onload = (function(theFile) { return function(e) { editor.setValue(e.target.result); }; })(f);
855 // onloadstart
856 // onloadend
857 // onprogress
858
859 // Read in the text file
860 reader.readAsText(f);
861}
862
863function refreshCameras()
864{
865 drawFullCam("camera1");
866 drawFullCam("camera2");
867 drawFullCam("camera3");
868 drawFullCam("camera4");
869}
870
871function onEvent()
872{
873 onSubmit();
874}
875
876function checkPixel()
877{
878 var pix = parseInt(document.getElementById("pixel").value);
879 var c = parseInt(document.getElementById("cbpx-c").value);
880 var b = parseInt(document.getElementById("cbpx-b").value);
881 var p = parseInt(document.getElementById("cbpx-p").value);
882 var x = parseInt(document.getElementById("cbpx-x").value);
883 var cbpx = parseInt(document.getElementById("cbpx").value);;
884
885 if (pix >=0 && pix <1440 &&
886 c >=0 && c < 4 &&
887 b >=0 && b < 10 &&
888 p >=0 && p < 4 &&
889 x >=0 && x < 9 &&
890 cbpx>=0 && cbpx<1440)
891 return;
892
893 document.getElementById("pixel").value = 0;
894 document.getElementById("cbpx-c").value = 1;
895 document.getElementById("cbpx-b").value = 0;
896 document.getElementById("cbpx-p").value = 3;
897 document.getElementById("cbpx-x").value = 6;
898 document.getElementById("cbpx").value = 393;
899}
900
901
902function onPixel()
903{
904 checkPixel();
905
906 var p = parseInt(document.getElementById("pixel").value);
907
908 var cbpx = map[p];
909
910 document.getElementById("cbpx-c").value = parseInt((cbpx/360));
911 document.getElementById("cbpx-b").value = parseInt((cbpx/36)%10);
912 document.getElementById("cbpx-p").value = parseInt((cbpx/9)%4);
913 document.getElementById("cbpx-x").value = parseInt((cbpx%9));
914 document.getElementById("cbpx").value = parseInt(cbpx);
915
916 onSubmit("", true);
917}
918
919function onCBPX()
920{
921 checkPixel();
922
923 var c = parseInt(document.getElementById("cbpx-c").value);
924 var b = parseInt(document.getElementById("cbpx-b").value);
925 var p = parseInt(document.getElementById("cbpx-p").value);
926 var x = parseInt(document.getElementById("cbpx-x").value);
927
928 var cbpx = c*360 + b*36 + p*9 + x;
929
930 document.getElementById("cbpx").value = parseInt(cbpx);
931 document.getElementById("pixel").value = map.indexOf(cbpx);
932
933 onSubmit("", true);
934}
935
936function onHW()
937{
938 checkPixel();
939
940 var cbpx = parseInt(document.getElementById("cbpx").value);;
941
942 document.getElementById("cbpx-c").value = parseInt((cbpx/360));
943 document.getElementById("cbpx-b").value = parseInt((cbpx/36)%10);
944 document.getElementById("cbpx-p").value = parseInt((cbpx/9)%4);
945 document.getElementById("cbpx-x").value = parseInt((cbpx%9));
946
947 document.getElementById("pixel").value = map.indexOf(cbpx);
948
949 onSubmit("", true);
950}
951
952function isInside(x, y, mouse)
953{
954 var dist = Math.sqrt((mouse.x-x)*(mouse.x-x)+(mouse.y-y)*(mouse.y-y));
955 return dist<0.5;
956
957 /*
958 ctx.translate(x, y);
959 ctx.scale(1/2, 1/3);
960
961 ctx.beginPath();
962 ctx.moveTo( 1, 1);
963 ctx.lineTo( 0, 2);
964 ctx.lineTo(-1, 1);
965 ctx.lineTo(-1, -1);
966 ctx.lineTo( 0, -2);
967 ctx.lineTo( 1, -1);
968 ctx.fill();
969
970 ctx.restore();
971 */
972}
973
974var inprogress = { };
975function moveElement(id, n, target, callback)
976{
977 if (inprogress[id]==n || inprogress[id]<0)
978 return;
979
980 inprogress[id] = target ? -n : n;
981
982 var element = $("#"+id+n); //Allow passing in either a JQuery object or selector
983 var newParent = $(target ? target : "#cont"+id+n); //Allow passing in either a JQuery object or selector
984
985 var oldOffset = element.offset();
986
987 var newOffset = newParent.offset();
988
989 var w = newParent.width();
990 var h = newParent.height();
991
992 var temp = element.appendTo('body');
993 temp.css('position', 'absolute')
994 .css('left', oldOffset.left)
995 .css('top', oldOffset.top)
996 .css('zIndex', 999);
997
998 temp.animate( {'top': newOffset.top, 'left':newOffset.left, 'width':w, 'height': h},
999 'slow', function()
1000 {
1001 temp = temp.appendTo(newParent);
1002 temp.css('position', 'relative');
1003 temp.css('width', '');
1004 temp.css('height', '');
1005 temp.css('zIndex', '');
1006 temp.css('left', '0');
1007 temp.css('top', '0');
1008
1009 setSize(id+n, w, h);
1010
1011 if (callback)
1012 callback(id+n);
1013
1014 inprogress[id] = 0;
1015 });
1016}
1017
1018function onClickCont(event, callback)
1019{
1020 var id = event.target.id;
1021 if (!id)
1022 id = event.target.parentNode.id;
1023
1024 var n = parseInt(id[id.length-1]);
1025 var type = id.substr(0, id.length-1);
1026
1027 if (id.substr(0, 4)=="cont")
1028 id = id.substr(4, id.length-4);
1029 if (type.substr(0, 4)=="cont")
1030 type = type.substr(4, type.length-4);
1031
1032 if (id.substr(0, type.length)==type)
1033 {
1034 var cont = document.getElementById("center"+type).childNodes[0];
1035 if (cont)
1036 {
1037 var nn = parseInt(cont.id[cont.id.length-1]);
1038 moveElement(type, nn, null, callback);
1039 }
1040 moveElement(type, n, "#center"+type, callback);
1041
1042 }
1043 else
1044 moveElement(type, n, null, callback);
1045
1046}
1047
1048function onClickContCamera(event)
1049{
1050 onClickCont(event, function(el) { drawFullCam(el); });
1051}
1052
1053function onClickContHist(event)
1054{
1055 onClickCont(event);
1056}
1057
1058function onClick(event)
1059{
1060 var cont = document.getElementById("centercamera").childNodes[0];
1061 if (!cont)
1062 return;
1063
1064 if (cont.id!=event.target.id)
1065 return;
1066
1067 // get click position relative to canvas
1068 var rect = event.target.getBoundingClientRect();
1069
1070 var x = event.clientX - rect.left;
1071 var y = event.clientY - rect.top;
1072
1073 var mouse = { x: x, y: y };
1074
1075 // convert click position to pixel index
1076 var index = getIndex(event.target.id, mouse);
1077 if (index<0)
1078 return;
1079
1080 document.getElementById("pixel").value = index;
1081
1082 onPixel();
1083}
1084
1085function getClickPosition(event)
1086{
1087 var rect = event.target.getBoundingClientRect();
1088
1089 var x = event.clientX - rect.left;
1090 var y = event.clientY - rect.top;
1091
1092 return { x: x, y: y };
1093}
1094
1095function onMinMax(id, n)
1096{
1097 var el = document.getElementById(id+n);
1098
1099 el.zmin = document.getElementById(id+"min"+n).value;
1100 el.zmax = document.getElementById(id+"max"+n).value;
1101}
1102
1103function onCameraMinMax(n)
1104{
1105 onMinMax("camera", n);
1106 drawFullCam("camera"+n);
1107}
1108
1109function onHistMinMax(n)
1110{
1111 onMinMax("hist", n);
1112 drawHist(n);
1113}
1114
1115function onMinMaxOn(id, n)
1116{
1117 var el = document.getElementById(id+n);
1118
1119 var redraw;
1120 if (document.getElementById(id+"minon"+n).checked)
1121 {
1122 document.getElementById(id+"min"+n).setAttribute("disabled", "true");
1123 document.getElementById(id+"min"+n).value = el.min;
1124 redraw = true;
1125 }
1126 else
1127 document.getElementById(id+"min"+n).removeAttribute("disabled");
1128
1129 if (document.getElementById(id+"maxon"+n).checked)
1130 {
1131 document.getElementById(id+"max"+n).setAttribute("disabled", "true");
1132 document.getElementById(id+"max"+n).value = el.max;
1133 redraw = true;
1134 }
1135 else
1136 document.getElementById(id+"max"+n).removeAttribute("disabled");
1137
1138 return redraw;
1139}
1140
1141function onCameraMinMaxOn(n)
1142{
1143 if (onMinMaxOn("camera", n))
1144 onCameraMinMax(n);
1145}
1146
1147function onHistMinMaxOn(n)
1148{
1149 if (onMinMaxOn("hist", n))
1150 onHistMinMax(n);
1151}
1152
1153function onWaveformMinMax()
1154{
1155 var wf = document.getElementById("waveform");
1156
1157 var xmin, xmax, ymin, ymax;
1158
1159 var redraw;
1160 if (!document.getElementById("waveformxminon").checked)
1161 xmin = document.getElementById("waveformxmin").value;
1162 if (!document.getElementById("waveformxmaxon").checked)
1163 xmax = document.getElementById("waveformxmax").value;
1164 if (!document.getElementById("waveformminon").checked)
1165 ymin = document.getElementById("waveformmin").value;
1166 if (!document.getElementById("waveformmaxon").checked)
1167 ymax = document.getElementById("waveformmax").value;
1168
1169 setZoom(xmin, xmax, ymin, ymax);
1170
1171}
1172
1173function onWaveformMinMaxOn()
1174{
1175 var wf = document.getElementById("waveform");
1176
1177 var xmin, xmax, ymin, ymax;
1178
1179 var redraw;
1180 if (document.getElementById("waveformxminon").checked)
1181 {
1182 document.getElementById("waveformxmin").setAttribute("disabled", "true");
1183 document.getElementById("waveformxmin").value = wf.xmin;
1184 xmin = wf.xmin-1;
1185 }
1186 else
1187 document.getElementById("waveformxmin").removeAttribute("disabled");
1188
1189 if (document.getElementById("waveformxmaxon").checked)
1190 {
1191 document.getElementById("waveformxmax").setAttribute("disabled", "true");
1192 document.getElementById("waveformxmax").value = wf.xmax;
1193 xmax = wf.xmax+1;
1194 }
1195 else
1196 document.getElementById("waveformxmax").removeAttribute("disabled");
1197
1198 if (document.getElementById("waveformminon").checked)
1199 {
1200 document.getElementById("waveformmin").setAttribute("disabled", "true");
1201 document.getElementById("waveformmin").value = wf.ymin;
1202 ymin = wf.ymin-5;
1203 }
1204 else
1205 document.getElementById("waveformmin").removeAttribute("disabled");
1206
1207 if (document.getElementById("waveformmaxon").checked)
1208 {
1209 document.getElementById("waveformmax").setAttribute("disabled", "true");
1210 document.getElementById("waveformmax").value = wf.ymax;
1211 ymax = wf.ymax+5;
1212 }
1213 else
1214 document.getElementById("waveformmax").removeAttribute("disabled");
1215
1216 setZoom(xmin, xmax, ymin, ymax);
1217}
1218
1219//document.addEventListener("click", getClickPosition, false);
1220
1221$(document).ready(onReady);
1222
1223// ================================== Pixel mapping =================================================
1224
1225var map = new Array(1440);
1226
1227function initPixelMapFACT()
1228{
1229 var codedMap = "966676:6:A;68656364626Y?\\?;A=A<AGADAN4K4i5g5h5o506W?Z?]?_?>A@A?AJAIAFACAM4J4H4f5d5e5l5m5n516X?[?^?N?P?AA1ABAVAUAKAHAEAO4L4I4G4E4c5a5b5M6j5k5V6Y6\\6_6G?J?O?Q?S?2A4A3AYAXAWAbA_AnAkAhA3404F4D4B4`5^5_5J6K6L6S6T6W6Z6]6E?H?K?M?R?T?V?5A7A6A\\A[AZAeAdAaA^AmAjAgA24o3m3C4A4?4]5[5\\5G6H6I6P6Q6R6U6X6[6^6F?I?L?<?>?U?;@=@8Ah@9AMALA]ABCACfAcA`AoAlAiA4414n3l3<4@4>425Z5X5Y5D6E6F698N6O608E8H8K8H>K>N>?>B>=???A?<@>@@@i@k@j@PAOANAECDCCC<C9CZCWCTC=3:3734313=4;4943515o4W5U5V5A6B6C6687888m7n7C8F8I8F>I>L>=>@>C>E>@?B?D??@A@C@l@n@m@SARAQAHCGCFC?C>C;C8CYCVCSC<393633303n2:4846405n4l4T5R5S5>6?6@6384858j7k7l7o7D8G8J8G>J>M>>>A>D>4>6>C?3?5?B@2@4@o@_@0ALBKBTA0CoBIC8D7D@C=C:C[CXCUC>3;3835323o2m2k27454j3m4k4i4Q5O5P5C7<6=6g71828o8h7i7S9<8?8B8Q>T>W>@=C=F=e<h<5>7>9>4?6?8?3@5@7@`@b@a@OBNBMB3C2C1C;D:D9D_D\\DQCNCKCF3C3@3>2;282Z1W1l2j2h2k3i3g3j4h4f4N5L5M5@7A7B7d7e7f7l8m8n8P9Q9:8=8@8O>R>U>>=A=D=c<f<i<k<8>:><>7?9?;?6@8@:@c@e@d@RBQBPB6C5C4C>D=D<DbDaD^D[DPCMCJCE3B3?3=2:272Y1V1T1i2g2e2h3f3d3g4e4c4K5I5J5=7>7?7a7b7c7i8j8k8M9N9O9R9;8>8A8P>S>V>?=B=E=d<g<j<Z<\\<;>k=m=:?j>l>9@i?k?f@V@g@CBBBSBgBfB7CoCnC?DSDRDcD`D]DRCOCLCG3D3A3?2<292[1X1U1S1Q1f2d2b2e3c3a3d4b4`4H5F5G5:7;7<7^7_7`7f8g8h8J9K9L97:::=:@:C:F:I:I=L=O=7=:===n<1=[<]<_<l=n=0>k>m>o>j?l?n?W@Y@X@FBEBDBjBiBhB2D1D0DVDUDTDCE@EOELEIEXEUERE5222o1l1i1f1c1`1R1P1N1c2a2_2b3`3^3a4_4]4<5:5;5778797[7\\7]7c8d8e8G9H9I94:5:8:;:>:A:D:G:G=J=M=5=8=;=l<o<2=4=^<`<b<o=1>3>n>0?2?m?o?1@Z@\\@[@IBHBGBmBlBkB5D4D3DYDXDWDFEEEBE?ENEKEHEWETEQE4212n1k1h1e1b1_1]1O1M1K1`2^2\\2_3]3[3^4\\4Z4957585475767X7Y7Z7`8a8b8D9E9F91:2:3:6:9:<:?:B:E:H:H=K=N=6=9=<=m<0=3=d;f;a<H<J<2>b=d=1?a>c>0@`?b?]@D@^@:B9BJB^B]BnBfCeC6DJDIDZDnDmDGEDEAEPEMEJEYEVESE623202m1j1g1d1a1^1\\1[0L1J1?1]2[2Y2\\3Z3X3[4Y4W4654555172737U7V7W7]8^8_8A9B9C9e9o90:n9g:j:m:L:O:R:U:X:[:];`;c;T;W;Z;7<9<e;g;i;I<K<M<c=e=g=b>d>f>a?c?e?E@G@F@=B<B;BaB`B_BiChCgCMDLDKD1E0EoD9E7E<F9F6FaE^E[EjEgEdER0O0L0I0F0C0n0l0\\0Z0X0@1>1<1Z2X2V2Y3W3U3X4V4T4E5C5D5n6o607R7S7T7Z8[8\\8>9?9@9b9c9d9l9m9e:h:k:J:M:P:S:V:Y:[;^;a;R;U;X;6<8<:<;<h;j;l;L<N<P<f=h=j=e>g>i>d?f?h?N@Q@H@@B?B>BdBcBbBlCkCjCPDODND4E3E2E;E:E8E6E;F8F5F`E]EZEiEfEcEQ0N0K0H0E0B0m0k0j0Y0W0U0=1;191W2U2S2V3T3R3U4S4Q4B5@5A5k6l6m6O7P7Q7W8X8Y8;9<9=9_9`9a9j9k9\\:_:f:i:l:K:N:Q:T:W:Z:\\;_;b;S;V;Y;C<E<G<<<m;k;1<2<O<Q<S<i=[=P=h>X>Z>g?M@O@R@U@S@J@I@AB1B0BeBTB\\CmCAD@DQDiDhD5EdD<E4F2F0F=F:F7FbE_E\\EkEhEeES0P0M0J0G0D011o0h0g0e0V0T0`0:181Q2T2R2H2S3Q3P3R4P4K3?5=5>5c6i6j6h6M7N7L7U8V8T899:9W9]9^9\\9g9h9i9]:`:c:5;2;o:>;;;8;P;M;J;G;D;A;?<A<D<F<=<><n;o;3<5<R<T<Y=Z=Q=R=Y>[>\\>^>P@T@L@K@5B4B3B2BVBUB^C]CCDBDkDjDfDeD>E=E3F1FnElE@FCFFFIFLFOF;0>0A0205080513101i0f0d0c0b0_0I1H1D1P2O2G2F2D2O3N3M3J3H3a6b6e6f6g6I7J7K7R8S8397989V9Y9Z9[9f9^:a:d:4;1;n:=;:;7;O;L;I;F;C;@;@<B<0<4<U<V<X<`=]=\\=S=U=W=]>_>`>8B7B6BZBXBWB_CaC`CFDEDDDlDgDoEmE>FAFDFGFJFMF90<0?0003060714121a0^0]0G1C1B1N2L2E2C2B2@2L3I3`6d6E7F7G7H7O8Q8192969T9U9X96;3;0;?;<;9;Q;N;K;H;E;B;W<Y<^=_=a=T=V=X=\\B[BYBdCcCbCHDGD?FBFEFHFKFNF:0=0@0104070F1E1A1M2K2J2I2A2D7L8M8N8P809495961b:";
1230 // first: decode the pixel mapping!
1231 var sum = 1036080;
1232 for (var i=0; i<1440; i++)
1233 {
1234 var d0 = codedMap.charCodeAt(i*2) -48;
1235 var d1 = codedMap.charCodeAt(i*2+1)-48;
1236
1237 map[i] = d0 | (d1<<6);
1238 sum -= map[i];
1239 }
1240 if (sum!=0)
1241 alert("Pixel mapping table corrupted ["+sum+"]!");
1242}
1243
1244function initPixelMapFAMOUS()
1245{
1246 var codedMap = "C0D0B0J0I0H0E0\\0=0<07080L0K0h0k0^0]0[0Y0@0>0:0403060M0O0N0g0i0l0a0b0_0Z0X0U0V0A0?0;09010002050P0S0R0Q0f0j0m0n0d0e0c0`0W0T0o01101213141516171F0G0";
1247 // first: decode the pixel mapping!
1248 var sum = 2556;
1249 for (var i=0; i<72; i++)
1250 {
1251 var d0 = codedMap.charCodeAt(i*2) -48;
1252 var d1 = codedMap.charCodeAt(i*2+1)-48;
1253
1254 map[i] = d0 | (d1<<6);
1255 sum -= map[i];
1256 }
1257 if (sum!=0)
1258 alert("Pixel mapping table corrupted ["+sum+"]!");
1259}
1260
1261if (camera_type=="FACT")
1262 initPixelMapFACT();
1263else
1264 initPixelMapFAMOUS();
1265
1266// ================================== Camera Display ================================================
1267
1268var coord = new Array(1440);
1269function initCameraCoordinatesFACT()
1270{
1271 coord[0] = [0, 0];
1272 var cnt = 1;
1273 for (var ring=1; ring<24; ring++)
1274 {
1275 for (var s=0; s<6; s++)
1276 {
1277 for (var i=1; i<=ring; i++)
1278 {
1279 var pos = new Position(s, ring, i);
1280 if (pos.d() - pos.x > 395.75)
1281 continue;
1282
1283 coord[cnt++] = [ pos.x, pos.y];
1284 }
1285 }
1286 }
1287
1288 coord[1438] = [7, -22];
1289 coord[1439] = [7, 22];
1290}
1291
1292function initCameraCoordinatesFAMOUS()
1293{
1294 var pos;
1295
1296 coord[0] = [0,0];
1297 var cnt = 1;
1298 for (var ring=1; ring<5; ring++)
1299 {
1300 for (var s=0; s<6; s++)
1301 {
1302 for (var i=1; i<=ring; i++)
1303 {
1304 pos = new Position(s, ring, i);
1305 coord[cnt++] = [ pos.x, -pos.y];
1306 }
1307 }
1308 }
1309
1310 pos = new Position(3, 6, 2);
1311 coord[cnt++] = [pos.x, -pos.y];
1312 pos = new Position(4, 6, 4);
1313 coord[cnt++] = [pos.x, -pos.y];
1314 pos = new Position(0, 6, 2);
1315 coord[cnt++] = [pos.x, -pos.y];
1316}
1317
1318if (camera_type=="FACT")
1319 initCameraCoordinatesFACT();
1320else
1321 initCameraCoordinatesFAMOUS();
1322
1323
1324function getIndex(id, mouse)
1325{
1326 var canv = document.getElementById(id);
1327
1328 var w = Math.min(canv.width/scale, canv.height/scale);
1329
1330 //ctx.translate(canv.width/2, canv.height/2);
1331 //ctx.scale(w*2, w*2);
1332 //ctx.scale(1, Math.sqrt(3)/2);
1333 //ctx.translate(-0.5, 0);
1334
1335 mouse.x -= canv.width/2;
1336 mouse.y -= canv.height/2;
1337 mouse.x /= w*2;
1338 mouse.y /= w*2;
1339 mouse.y /= Math.sqrt(3)/2;
1340 mouse.x -= -0.5;
1341
1342 for (var i=0; i<npix; i++)
1343 if (isInside(coord[i][0], coord[i][1], mouse))
1344 return i;
1345
1346 return -1;
1347}
1348
1349
1350function hueToRGB(hue)
1351{
1352 hue /= 3;
1353 hue %= 6;
1354
1355 if (hue<1) return parseInt(255*hue, 10);
1356 if (hue<3) return parseInt(255, 10);
1357 if (hue<4) return parseInt(255*(4-hue), 10);
1358
1359 return 0.
1360}
1361
1362function hueToHex(flt)
1363{
1364 var s = hueToRGB(flt).toString(16);
1365 return s.length==2 ? s : "0"+s;
1366}
1367
1368function HLStoRGB(hue)
1369{
1370 if (isNaN(hue))
1371 return "fff";
1372
1373 if (hue<0)
1374 return "e5f"; // 555
1375
1376 if (hue>1)
1377 return "600";//"dde";//"700"; // 666
1378
1379 hue *= 14;
1380
1381 var sr = hueToHex(20-hue);
1382 var sg = hueToHex(14-hue);
1383 var sb = hueToHex(26-hue);
1384
1385 return sr+sg+sb;
1386}
1387
1388function outlineHex(ctx)
1389{
1390 ctx.scale(1/2, 1/3);
1391
1392 ctx.beginPath();
1393 ctx.moveTo( 1, 1);
1394 ctx.lineTo( 0, 2);
1395 ctx.lineTo(-1, 1);
1396 ctx.lineTo(-1, -1);
1397 ctx.lineTo( 0, -2);
1398 ctx.lineTo( 1, -1);
1399 ctx.lineTo( 1, 1);
1400}
1401
1402function fillHex(ctx, i, col, min, max)
1403{
1404 if (col===undefined/* || col===null*/)
1405 return false;
1406
1407 var lvl = max==min ? 0.5 : (col-min)/(max-min);
1408
1409 ctx.fillStyle = "#"+HLStoRGB(lvl);
1410
1411 ctx.save();
1412 ctx.translate(coord[i][0], coord[i][1]);
1413 outlineHex(ctx);
1414 ctx.fill();
1415 ctx.restore();
1416
1417 return true;
1418}
1419
1420function drawHex(ctx, i)
1421{
1422 ctx.save();
1423 ctx.translate(coord[i][0], coord[i][1]);
1424 outlineHex(ctx);
1425 ctx.stroke();
1426 ctx.restore();
1427}
1428
1429
1430function Position(s, ring, i)
1431{
1432 switch (s)
1433 {
1434 case 1: this.x = ring - i*0.5; this.y = + i; break;
1435 case 2: this.x = ring*0.5 - i; this.y = ring ; break;
1436 case 3: this.x = -ring*0.5 - i*0.5; this.y = ring - i; break;
1437 case 4: this.x = -ring + i*0.5; this.y = - i; break;
1438 case 5: this.x = -ring*0.5 + i; this.y = -ring ; break;
1439 case 0: this.x = ring*0.5 + i*0.5; this.y = -ring + i; break;
1440 }
1441 this.d = (function () { return this.x*this.x + this.y*this.y*3/4; });
1442}
1443
1444function drawFullCam(id)
1445{
1446 var canv = document.getElementById(id);
1447 if (!canv)
1448 return;
1449
1450 var ctx = canv.getContext("2d");
1451
1452 ctx.clearRect(0, 0, canv.width, canv.height);
1453
1454 // ======================= Draw Graphics ======================
1455
1456 var data = canv.dataRel;
1457 if (!data)
1458 return;
1459
1460 var pixel = document.getElementById('pixel').value;
1461
1462 var min = (canv.zmin-canv.min)/canv.max;
1463 var max = (canv.zmax-canv.min)/canv.max;
1464
1465 var w = Math.min(canv.width/scale, canv.height/scale);
1466
1467 ctx.save();
1468 ctx.translate(canv.width/2, canv.height/2);
1469 ctx.scale(w*2, w*2);
1470 // ctx.rotate(Math.PI/3);
1471
1472 ctx.scale(1, Math.sqrt(3)/2);
1473 ctx.translate(-0.5, 0);
1474
1475 if (document.getElementById('grid').checked)
1476 {
1477 ctx.lineWidth = 0.02;
1478 ctx.strokeStyle = "#000";
1479 for (var i=0; i<npix; i++)
1480 drawHex(ctx, i);
1481 }
1482
1483 var hasData = false;
1484 if (max>=min)
1485 for (var i=0; i<npix; i++)
1486 hasData |= fillHex(ctx, i, data[i], min, max);
1487
1488 // ======================= Draw Ellipse ======================
1489
1490 if (document.getElementById('image').checked)
1491 {
1492 var h = Hillas(canv.dataAbs, canv.zmin, canv.zmax);
1493 if (h)
1494 {
1495 ctx.save();
1496
1497 ctx.scale(1, 2/Math.sqrt(3));
1498
1499 ctx.beginPath();
1500 ctx.moveTo(center[0], center[1]);
1501 ctx.lineTo(h.mean[0], h.mean[1]);
1502
1503 ctx.strokeStyle = "#CCC";
1504 ctx.lineWidth = 0.1;
1505 ctx.stroke();
1506
1507 ctx.translate(h.mean[0], h.mean[1]);
1508 ctx.rotate(h.phi);
1509
1510 ctx.beginPath();
1511 ctx.moveTo(0, -h.disp);
1512 ctx.lineTo(0, h.disp);
1513
1514 ctx.strokeStyle = "#888";
1515 ctx.lineWidth = 0.15;
1516 ctx.stroke();
1517
1518 ctx.save();
1519 ctx.scale(h.axis[0], h.axis[1]);
1520 ctx.beginPath();
1521 ctx.arc(0, 0, 1, 0, 2*Math.PI);
1522 ctx.restore();
1523
1524 ctx.strokeStyle = "#555";
1525 ctx.lineWidth = 0.15;
1526 ctx.stroke();
1527
1528 ctx.restore();
1529 }
1530 }
1531
1532 // =================== Draw Pixel marker ====================
1533
1534 if (document.getElementById('marker').checked)
1535 {
1536 // Draw marker
1537 ctx.lineWidth = 0.25;
1538 ctx.strokeStyle = "#000";
1539 drawHex(ctx, pixel);
1540 }
1541
1542
1543 ctx.restore();
1544
1545 if (!hasData)
1546 return;
1547
1548 // ======================= Draw Legend ======================
1549
1550 var v0 = parseFloat(canv.zmin);
1551 var v1 = parseFloat(canv.zmax);
1552
1553 var diff = v1-v0;
1554
1555 var cw = canv.width;
1556 //var ch = canv.height;
1557
1558 ctx.font = "8pt Arial";
1559 ctx.textAlign = "right";
1560 ctx.textBaseline = "top";
1561
1562 for (var i=0; i<11; i++)
1563 {
1564 ctx.strokeStyle = "#"+HLStoRGB(i/10);
1565 ctx.strokeText((v0+diff*i/10).toPrecision(3), cw-5, 125-i*12);
1566 }
1567
1568 var pval = parseFloat(canv.dataAbs[pixel]).toFixed(1);
1569 var lmin = parseFloat(canv.min).toFixed(1);
1570 var lmax = parseFloat(canv.max).toFixed(1);
1571
1572 if (isNaN(pval))
1573 pval = "";
1574
1575 var mw = Math.max(ctx.measureText(lmin).width,
1576 ctx.measureText(pval).width,
1577 ctx.measureText(lmax).width);
1578
1579 ctx.textBaseline = "top";
1580 ctx.strokeStyle = "#000";
1581
1582 ctx.strokeText(lmax, 5+mw, 5+24);
1583 ctx.strokeText(pval, 5+mw, 5+12);
1584 ctx.strokeText(lmin, 5+mw, 5);
1585}
1586
1587// ===================================================================
1588
1589function Hillas(data, min, max)
1590{
1591 var mx = 0;
1592 var my = 0;
1593 var sz = 0;
1594
1595 var mx2 = 0;
1596 var my2 = 0;
1597 var mxy = 0;
1598
1599 var cnt = 0;
1600 for (var i=0; i<npix; i++)
1601 {
1602 if (data[i]===undefined || data[i]<min || data[i]>max)
1603 continue;
1604
1605 sz += data[i];
1606 mx += data[i] * coord[i][0];
1607 my += data[i] * coord[i][1];
1608
1609 mx2 += data[i] * coord[i][0]*coord[i][0];
1610 my2 += data[i] * coord[i][1]*coord[i][1];
1611 mxy += data[i] * coord[i][0]*coord[i][1];
1612
1613 cnt++;
1614 }
1615
1616 if (sz==0 || cnt<3)
1617 return;
1618
1619 // Coordinates need to be scaled in y
1620 var f = Math.sqrt(3)/2;
1621
1622 my *= f;
1623 mxy *= f;
1624 my2 *= f*f;
1625
1626 var xx = mx2 - mx*mx/sz;
1627 var yy = my2 - my*my/sz;
1628 var xy = mxy - mx*my/sz;
1629
1630 var d0 = yy - xx;
1631 var d1 = xy*2;
1632 var d2 = Math.sqrt(d0*d0 + d1*d1) + d0;
1633
1634 var phi = 0;
1635 var cos = 0;
1636 var sin = 1;
1637
1638 var axis1 = yy;
1639 var axis2 = xx;
1640
1641 // Correction for scale in x
1642 var ratio = xx/yy;
1643
1644 if (d1!=0 || d2==0)
1645 {
1646 var tand = d2==0 ? 0 : d2 / d1;
1647 var tand2 = tand*tand;
1648
1649 var s2 = tand2+1;
1650 var s = Math.sqrt(s2);
1651
1652 phi = Math.atan(tand)-Math.PI/2;
1653 cos = 1.0 /s;
1654 sin = tand/s;
1655
1656 axis1 = (tand2*yy + d2 + xx);
1657 axis2 = (tand2*xx - d2 + yy);
1658
1659 ratio = axis2/axis1;
1660
1661 axis1 /= s2;
1662 axis2 /= s2;
1663 }
1664
1665 var length = axis1<0 ? 0 : Math.sqrt(axis1/sz);
1666 var width = axis2<0 ? 0 : Math.sqrt(axis2/sz);
1667
1668 return {
1669 "mean": [ mx/sz, my/sz ],
1670 "axis": [ width, length ],
1671 "phi": phi,
1672 "delta": [ cos, sin ],
1673 "sumw": sz,
1674 "count": cnt,
1675 "disp": 1.47/diameter*(1-Math.sqrt(ratio)),
1676 };
1677}
Note: See TracBrowser for help on using the repository browser.