GSI Object Oriented Online Offline (Go4) GO4-6.4.5
Loading...
Searching...
No Matches
go4canvas.mjs
Go to the documentation of this file.
1import { ObjectPainter, getHPainter, addDrawFunc, create, gStyle, draw, floatToString, getElementMainPainter } from 'jsroot';
2import { addMoveHandler } from 'jsrootsys/modules/gui/utils.mjs';
3import { GO4 } from './core.mjs';
4import { ConditionEditor } from './condition.mjs';
5
6GO4.web_canvas = true;
7
8function findPainter(painter, obj, name, typ) {
9 const pp = painter.getPadPainter();
10 return pp?.findPainterFor(obj, name, typ);
11}
12
13function cleanupPainterFor(painter, obj) {
14 if (!obj || !painter)
15 return;
16 const objp = findPainter(painter, obj);
17 if (objp) {
18 objp.removeFromPadPrimitives();
19 objp.cleanup();
20 }
21}
22
23class MarkerPainter extends ObjectPainter{
24 constructor(dom, marker) {
25 super(dom, marker);
26 this.pave = null; // drawing of stat
27 }
28
29 moveDrag(dx,dy) {
30 this.grx += dx;
31 this.gry += dy;
32 this.getG().select('path').attr('d', this.markeratt.create(this.grx, this.gry));
33 }
34
35 moveEnd() {
36 let marker = this.getObject();
37 marker.fX = this.svgToAxis('x', this.grx);
38 marker.fY = this.svgToAxis('y', this.gry);
39 let exec = `SetXY(${marker.fX},${marker.fY})`;
40 this.submitCanvExec(exec);
41 this.drawLabel();
42 }
43
44 drawMarker() {
45 let g = this.createG(), // can draw in complete pad
46 marker = this.getObject();
47
48 this.createAttMarker({ attr: marker });
49
50 this.grx = this.axisToSvg('x', marker.fX);
51 this.gry = this.axisToSvg('y', marker.fY);
52
53 let path = this.markeratt.create(this.grx, this.gry);
54
55 if (path)
56 g.append("svg:path")
57 .attr("d", path)
58 .call(this.markeratt.func);
59
60 addMoveHandler(this);
61 }
62
63 fillLabels(marker) {
64 let lbls = [],
65 rect = this.getFramePainter().getFrameRect(),
66 main = this.getMainPainter(), hint = null;
67
68 if (main && typeof main.processTooltipEvent == 'function')
69 hint = main.processTooltipEvent({ enabled: false, x: this.grx - rect.x, y: this.gry - rect.y });
70
71 lbls.push(marker.fxName + ((hint && hint.name) ? (" : " + hint.name) : ""));
72
73 if (marker.fbXDraw)
74 lbls.push("X = " + floatToString(marker.fX, "6.4g"));
75
76 if (marker.fbYDraw)
77 lbls.push("Y = " + floatToString(marker.fY, "6.4g"));
78
79 if (hint && hint.user_info) {
80 if (marker.fbXbinDraw) {
81 let bin = "<undef>";
82 if (hint.user_info.binx !== undefined) bin = hint.user_info.binx; else
83 if (hint.user_info.bin !== undefined) bin = hint.user_info.bin;
84 lbls.push("Xbin = " + bin);
85 }
86
87 if (marker.fbYbinDraw) {
88 lbls.push("Ybin = " + ((hint.user_info.biny !== undefined) ? hint.user_info.biny : "<undef>"));
89 }
90
91 if (marker.fbContDraw)
92 lbls.push("Cont = " + hint.user_info.cont);
93 }
94
95 return lbls;
96 }
97
98 drawLabel() {
99
100 let marker = this.getObject();
101
102 if (!marker.fbHasLabel)
103 return Promise.resolve(this);
104
105 let pave_painter = findPainter(this, this.pave);
106
107 if (!pave_painter) {
108 this.pave = create("TPaveStats");
109 this.pave.fName = "stats_" + marker.fName;
110
111 let pp = this.getPadPainter(),
112 pad_width = pp.getPadWidth(),
113 pad_height = pp.getPadHeight();
114
115 let px = this.grx / pad_width + 0.02,
116 py = this.gry / pad_height - 0.02;
117 Object.assign(this.pave, { fX1NDC: px, fY1NDC: py - 0.15, fX2NDC: px + 0.2, fY2NDC: py, fBorderSize: 1, fFillColor: 0, fFillStyle: 1001 });
118
119 const st = gStyle;
120 Object.assign(this.pave, { fFillColor: st.fStatColor, fFillStyle: st.fStatStyle, fTextAngle: 0, fTextSize: st.fStatFontSize,
121 fTextAlign: 12, fTextColor: st.fStatTextColor, fTextFont: st.fStatFont });
122 } else {
123 this.pave.Clear();
124 }
125
126 let lbls = this.fillLabels(marker);
127 for (let k = 0; k < lbls.length; ++k)
128 this.pave.AddText(lbls[k]);
129
130 let pr = pave_painter ? pave_painter.redraw() :
131 draw(this.getDrawDom(), this.pave, "").then(p => { if (p) p.$secondary = true; });
132 return pr.then(() => this);
133 }
134
135 redrawObject(obj) {
136 if (!this.updateObject(obj)) return false;
137 this.redraw(); // no need to redraw complete pad
138 return true;
139 }
140
141 cleanup(arg) {
142 cleanupPainterFor(this, this.pave);
143 delete this.pave;
144
145 super.cleanup(arg);
146 }
147
148 redraw() {
149 this.drawMarker();
150 return this.drawLabel();
151 }
152
153// fillContextMenu(menu) {
154// let marker = this.getObject();
155// menu.add("header:"+ marker._typename + "::" + marker.fxName);
156// function select(name,exec) {
157// let marker = this.getObject();
158// marker[name] = !marker[name];
159// this.submitCanvExec(exec + (marker[name] ? '(true)' : '(false)'));
160// this.redraw();
161// }
162// menu.addchk(marker.fbHasLabel, 'Label', select.bind(this, 'fbHasLabel', 'SetLabelDraw'));
163// menu.addchk(marker.fbHasConnector, 'Connector', select.bind(this, 'fbHasConnector', 'SetLineDraw'));
164// menu.addchk(marker.fbXDraw, 'Draw X', select.bind(this, 'fbXDraw', 'SetXDraw'));
165// menu.addchk(marker.fbYDraw, 'Draw Y', select.bind(this, 'fbYDraw', 'SetYDraw'));
166// menu.addchk(marker.fbXbinDraw, 'Draw X bin', select.bind(this, 'fbXbinDraw', 'SetXbinDraw'));
167// menu.addchk(marker.fbYbinDraw, 'Draw Y bin', select.bind(this, 'fbYbinDraw', 'SetYbinDraw'));
168// menu.addchk(marker.fbContDraw, 'Draw content', select.bind(this, 'fbContDraw', 'SetContDraw'));
169// return true;
170// }
171
172 processTooltipEvent(pnt) {
173 if (!pnt) return null;
174
175 let marker = this.getObject(),
176 rect = this.getFramePainter().getFrameRect(),
177 fx = rect.x,
178 fy = rect.y,
179 marker_sz = this.markeratt.getFullSize();
180
181 let hint = { name: marker.fxName,
182 title: marker.fxName,
183 painter: this,
184 menu: true,
185 x: this.grx - fx,
186 y: this.gry - fy,
187 color1: this.markeratt.color };
188
189 let dist = Math.sqrt(Math.pow(pnt.x - hint.x, 2) + Math.pow(pnt.y - hint.y, 2));
190
191 hint.menu_dist = dist;
192
193 if (dist < 2.5 * marker_sz) hint.exact = true;
194
195 if (hint.exact)
196 hint.lines = this.fillLabels(marker);
197
198 // res.menu = res.exact; // activate menu only when exactly locate bin
199 // res.menu_dist = 3; // distance always fixed
200
201 return hint;
202 }
203
204 ShowTooltip(hint) {}
205
206 static draw(dom, obj, /*, option */) {
207 const painter = new MarkerPainter(dom, obj);
208 painter.drawMarker();
209 painter.addToPadPrimitives();
210 painter.drawLabel();
211 return painter.drawLabel();
212 }
213
214} // class MarkerPainter
215
216
217// =========================================================================
218
219class ConditionPainter extends ObjectPainter {
220 constructor(dom, cond) {
221 super(dom, cond);
222 this.pave = null; // drawing of stat
223 }
224
225 Test(x,y) {
226 // JAM: need to put this here, since condition object will lose internal definition after cloning it again!
227 let cond = this.getObject();
228 if (!cond.fbEnabled)
229 return cond.fbResult;
230
231 if (cond.fxCut)
232 return cond.fxCut.IsInside(x,y) ? cond.fbTrue : cond.fbFalse;
233
234 if ((x < cond.fLow1) || (x > cond.fUp1)) return cond.fbFalse;
235
236 if ((cond.fiDim==2) && ((y < cond.fLow2) || (y > cond.fUp2))) return cond.fbFalse;
237
238 return cond.fbTrue;
239 }
240
241
242 isPolyCond() {
243 return this.matchObjectType("TGo4PolyCond") || this.matchObjectType("TGo4ShapedCond");
244 }
245
246 isEllipseCond() {
247 return this.matchObjectType("TGo4ShapedCond");
248 }
249
250 afterCutDraw(p) {
251 if (!p || !this.snapid || p._oldexec) return;
252 p.snapid = this.snapid + "#member_fxCut";
253 p._condpainter = this;
254
255 // catch TCutG exec and mark condition as modified
256 p._oldexec = p.submitCanvExec;
257 p.submitCanvExec = function(exec, arg) {
258 this._oldexec(exec, arg);
259 p._condpainter.submitCanvExec("SetChanged()");
260 }
261 }
262
263 drawCondition(interactive) {
264
265 let cond = this.getObject();
266
267 if (!cond || !cond.fbVisible) return;
268
269 if (this.isPolyCond()) {
270 if (cond.fxCut) {
271 // look here if cut is already drawn
272 let cutpaint = findPainter(this, null, cond.fName, 'TCutG');
273
274 if (cutpaint) {
275 if (cutpaint.updateObject(cond.fxCut)) cutpaint.redraw();
276 this.afterCutDraw(cutpaint);
277 } else {
278 cond.fxCut.fFillStyle = 3006;
279 cond.fxCut.fFillColor = 2;
280 draw(this.getDrawDom(), cond.fxCut, "LF").then(p => this.afterCutDraw(p));
281 }
282
283 }
284 return;
285 }
286
287 let g = this.createG(true); // drawing performed inside frame
288
289 if ((cond.fFillStyle==1001) && (cond.fFillColor==19)) {
290 cond.fFillStyle = 3006;
291 cond.fFillColor = 2;
292 }
293
294 this.createAttFill({attr: cond});
295 this.createAttLine({attr: cond});
296
297 this.grx1 = this.axisToSvg("x", cond.fLow1);
298 this.grx2 = this.axisToSvg("x", cond.fUp1);
299
300 if (cond.fiDim == 2) {
301 this.gry1 = this.axisToSvg("y", cond.fUp2);
302 this.gry2 = this.axisToSvg("y", cond.fLow2);
303 this.candy = true;
304 } else {
305 this.gry1 = 0;
306 this.gry2 = this.getFramePainter().getFrameHeight();
307 this.candy = false;
308 }
309
310 g.append("svg:rect")
311 .attr("x", this.grx1)
312 .attr("y", this.gry1)
313 .attr("width", this.grx2 - this.grx1)
314 .attr("height", this.gry2 - this.gry1)
315 .call(this.lineatt.func)
316 .call(this.fillatt.func);
317
318 addMoveHandler(this);
319 }
320
321 moveStart(x,y) {
322 this.swapx = this.swapy = false;
323 this.dx1 = Math.abs(x-this.grx1) < 5;
324 this.dx2 = Math.abs(x-this.grx2) < 5;
325 this.dy1 = Math.abs(y-this.gry1) < 5;
326 this.dy2 = Math.abs(y-this.gry2) < 5;
327 if (!this.dx1 && !this.dx2 && !this.dy1 && !this.dy2)
328 this.dx1 = this.dx2 = this.dy1 = this.dy2 = true;
329 if (!this.candy) this.dy1 = this.dy2 = false;
330 }
331
332 moveDrag(dx,dy) {
333 if (this.dx1) this.grx1 += dx;
334 if (this.dx2) this.grx2 += dx;
335 if (this.grx1 > this.grx2) {
336 this.swapx = true;
337 let tempx = this.grx1; this.grx1 = this.grx2; this.grx2 = tempx;
338 tempx = this.dx1; this.dx1 = this.dx2; this.dx2 = tempx;
339 }
340 if (this.dy1) this.gry1 += dy;
341 if (this.dy2) this.gry2 += dy;
342 if (this.gry1 > this.gry2) {
343 this.swapy = true;
344 let tempy = this.gry1; this.gry1 = this.gry2; this.gry2 = tempy;
345 tempy = this.dy1; this.dy1 = this.dy2; this.dy2 = tempy;
346 }
347 this.getG().select('rect').attr("x",this.grx1).attr("y", this.gry1)
348 .attr("width", this.grx2 - this.grx1).attr("height", this.gry2 - this.gry1);
349 }
350
351 moveEnd() {
352 let cond = this.getObject(), exec = "";
353 if (this.dx1 || this.swapx) { cond.fLow1 = this.svgToAxis("x", this.grx1); exec += "SetXLow(" + cond.fLow1 + ");;"; }
354 if (this.dx2 || this.swapx) { cond.fUp1 = this.svgToAxis("x", this.grx2); exec += "SetXUp(" + cond.fUp1 + ");;"; }
355 if (this.dy2 || this.swapy) { cond.fLow2 = this.svgToAxis("y", this.gry2); exec += "SetYLow(" + cond.fLow2 + ");;"; }
356 if (this.dy1 || this.swapy) { cond.fUp2 = this.svgToAxis("y", this.gry1); exec += "SetYUp(" + cond.fUp2 + ");;"; }
357 if (exec) {
358 this.submitCanvExec(exec + "SetChanged()");
359 this.drawLabel();
360 }
361 }
362
363 drawLabel() {
364 let cond = this.getObject(), painter = this, stat = {};
365
366 if (!cond.fbLabelDraw || !cond.fbVisible)
367 return Promise.resolve(this);
368
369 let pave_painter = findPainter(this, this.pave);
370
371 if (!pave_painter) {
372 this.pave = create('TPaveStats');
373 this.pave.fName = 'stats_' + cond.fName;
374 Object.assign(this.pave, { fX1NDC: 0.1, fY1NDC: 0.4, fX2NDC: 0.4, fY2NDC: 0.65, fBorderSize: 1, fFillColor: 0, fFillStyle: 1001 });
375
376 const st = gStyle;
377 Object.assign(this.pave, { fFillColor: st.fStatColor, fFillStyle: st.fStatStyle, fTextAngle: 0, fTextSize: st.fStatFontSize,
378 fTextAlign: 12, fTextColor: st.fStatTextColor, fTextFont: st.fStatFont});
379 } else {
380 this.pave.Clear();
381 }
382
383 this.pave.AddText(cond.fName);
384
385 this.pave.AddText("Counts = " + cond.fiCounts);
386
387 if (cond.fbLimitsDraw)
388 if (this.isPolyCond()) {
389 let res = { xmin: 0, xmax: 0, ymin: 0, ymax: 0 };
390 if (cond.fxCut.fNpoints > 0) {
391 res.xmin = res.xmax = cond.fxCut.fX[0];
392 res.ymin = res.ymax = cond.fxCut.fY[0];
393 for (let i=1; i<cond.fxCut.fNpoints; i++) {
394 res.xmin = Math.min(res.xmin, cond.fxCut.fX[i]);
395 res.xmax = Math.max(res.xmax, cond.fxCut.fX[i]);
396 res.ymin = Math.min(res.ymin, cond.fxCut.fY[i]);
397 res.ymax = Math.max(res.ymax, cond.fxCut.fY[i]);
398 }
399 }
400 this.pave.AddText("Xmin = " + res.xmin);
401 this.pave.AddText("Xmax = " + res.xmax);
402 this.pave.AddText("Ymin = " + res.ymin);
403 this.pave.AddText("Ymax = " + res.ymax);
404 } else {
405 this.pave.AddText("Xmin = " + cond.fLow1);
406 this.pave.AddText("Xmax = " + cond.fUp1);
407 if (cond.fiDim==2) {
408 this.pave.AddText("Ymin = " + cond.fLow2);
409 this.pave.AddText("Ymax = " + cond.fUp2);
410 }
411 }
412
413 stat = this.getMainPainter().countStat((x,y) => painter.Test(x,y));
414
415 if (cond.fbIntDraw) this.pave.AddText("Integral = " + floatToString(stat.integral, "14.7g"));
416
417 if (cond.fbXMeanDraw) this.pave.AddText("Mean x = " + floatToString(stat.meanx, "6.4g"));
418
419 if (cond.fbXRMSDraw) this.pave.AddText("RMS x = " + floatToString(stat.rmsx, "6.4g"));
420
421 if (cond.fiDim==2) {
422 if (cond.fbYMeanDraw) this.pave.AddText("Mean y = " + floatToString(stat.meany, "6.4g"));
423 if (cond.fbYRMSDraw) this.pave.AddText("RMS y = " + floatToString(stat.rmsy, "6.4g"));
424 }
425
426 if (cond.fbXMaxDraw) this.pave.AddText("X max = " + floatToString(stat.xmax, "6.4g"));
427
428 if (cond.fiDim==2) {
429 if (cond.fbYMaxDraw) this.pave.AddText("Y max = " + floatToString(stat.ymax, "6.4g"));
430 }
431
432 if (cond.fbCMaxDraw) this.pave.AddText("C max = " + floatToString(stat.wmax, "14.7g"));
433
434 let pr = pave_painter ? pave_painter.redraw() : draw(this.getDrawDom(), this.pave, "nofillstats");
435 return pr.then(() => this); // ensure that condition painter is returned
436 }
437
438// fillContextMenu(menu) {
439// let cond = this.getObject();
440// menu.add("header:"+ cond._typename + "::" + cond.fName);
441// function select(name,exec) {
442// let cond = this.getObject();
443// cond[name] = !cond[name];
444// this.submitCanvExec(exec + (cond[name] ? '(true)' : '(false)'));
445// this.redraw();
446// }
447// menu.addchk(cond.fbLabelDraw, 'Label', select.bind(this, 'fbLabelDraw', 'SetLabelDraw'));
448// menu.addchk(cond.fbLimitsDraw, 'Limits', select.bind(this, 'fbLimitsDraw', 'SetLimitsDraw'));
449// menu.addchk(cond.fbIntDraw, 'Integral', select.bind(this, 'fbIntDraw', 'SetIntDraw'));
450// menu.addchk(cond.fbXMeanDraw, 'X mean', select.bind(this, 'fbXMeanDraw', 'SetXMeanDraw'));
451// menu.addchk(cond.fbXRMSDraw, 'X rms', select.bind(this, 'fbXRMSDraw', 'SetXRMSDraw'));
452// menu.addchk(cond.fbXMaxDraw, 'X max', select.bind(this, 'fbXMaxDraw', 'SetXMaxDraw'));
453// menu.addchk(cond.fbYMeanDraw, 'Y mean', select.bind(this, 'fbYMeanDraw', 'SetYMeanDraw'));
454// menu.addchk(cond.fbYRMSDraw, 'Y rms', select.bind(this, 'fbYRMSDraw', 'SetYRMSDraw'));
455// menu.addchk(cond.fbYMaxDraw, 'Y max', select.bind(this, 'fbYMaxDraw', 'SetYMaxDraw'));
456// return true;
457// }
458
459 processTooltipEvent(pnt) {
460 if (!pnt) return null;
461
462 let cond = this.getObject(),
463 hint = { name: cond.fName, title: cond.fTitle, painter: this, menu: true, x: pnt.x, y: pnt.y };
464
465 if (!this.isPolyCond()) {
466
467 hint.color1 = this.fillatt.color;
468 hint.color2 = this.lineatt.color;
469
470 hint.menu_dist = Math.sqrt(Math.pow(pnt.x - (this.grx1 + this.grx2)/2, 2) + Math.pow(pnt.y - (this.gry1 + this.gry2)/2, 2));
471 hint.exact = (this.grx1 <= pnt.x) && (pnt.x <= this.grx2) && (this.gry1 <= pnt.y) && (pnt.y <= this.gry2);
472 if (Math.abs(this.grx1 - pnt.x) < 5) hint.sidex = -1;
473 if (Math.abs(this.grx2 - pnt.x) < 5) hint.sidex = 1;
474 if (cond.fiDim == 2) {
475 if (Math.abs(this.gry1 - pnt.y) < 5) hint.sidey = 1;
476 if (Math.abs(this.gry2 - pnt.y) < 5) hint.sidey = -1;
477 }
478 }
479
480 if (hint.exact)
481 hint.lines = ["condition", cond.fName ];
482
483 return hint;
484 }
485
486 redrawObject(obj) {
487 if (!this.updateObject(obj)) return false;
488 this.redraw(); // no need to redraw complete pad
489 return true;
490 }
491
492 cleanup(arg) {
493 cleanupPainterFor(this, this.pave);
494 delete this.pave;
495 super.cleanup(arg);
496 }
497
498 redraw() {
499 this.drawCondition();
500 return this.drawLabel();
501 }
502
503 static async draw(dom, cond, option) {
504 if (!option)
505 option = '';
506
507 let condpainter = new ConditionPainter(dom, cond),
508 elem = condpainter.selectDom(),
509 main = getElementMainPainter(dom);
510
511 if (GO4.web_canvas || (option.indexOf('same') >= 0) || main) {
512 // if no hist painter, do nothing, just return dummy
513 if (!main)
514 return condpainter;
515 condpainter.addToPadPrimitives();
516 condpainter.drawCondition();
517 return condpainter.drawLabel();
518 }
519
520 // from here normal code for plain THttpServer
521 if ((option == 'editor') || !cond.fxHistoName) {
522 const rect = elem.node().getBoundingClientRect();
523 if ((rect.height < 10) && (rect.width > 10))
524 elem.style('height', Math.round(rect.width*0.4) + 'px');
525 const editor = new ConditionEditor(dom, cond);
526 return editor.drawEditor();
527 }
528
529 const hpainter = getHPainter();
530 if (!hpainter) return;
531
532 let histofullpath = null;
533
534 hpainter.forEachItem(h => {
535 if ((h._name == cond.fxHistoName) && h._kind && (h._kind.indexOf('ROOT.TH') == 0))
536 histofullpath = hpainter.itemFullName(h);
537 });
538
539 if (histofullpath === null) {
540 histofullpath = "../../Histograms/" + cond.fxHistoName;
541
542 let hitem = hpainter.findItem({ name: histofullpath, force: true });
543
544 hitem._kind = 'ROOT.TH1I';
545 }
546
547 return hpainter.display(histofullpath, '', dom).then(hist_painter => {
548 if (!hist_painter) {
549 console.log('fail to draw histogram ' + histofullpath);
550 return null;
551 }
552
553 condpainter.addToPadPrimitives();
554 condpainter.drawCondition();
555 return condpainter.drawLabel();
556
557 });
558 }
559
560} // class ConditionPainter
561
562function drawCondArray(dom, obj, option) {
563 const arr = obj.condarr.arr,
564 num = obj.fiNumCond,
565 main = getElementMainPainter(dom);
566 let first = null;
567
568 async function drawNext(i) {
569 if (i >= num)
570 return first;
571
572 const cond = arr[i];
573 if ((i === 0) && !cond.fxHistoName && !main) {
574 cond.fxHistoName = obj.fxHistoName;
575 if (!cond.fxHistoName)
576 return null;
577 }
578
579 return ConditionPainter.draw(dom, cond, ((i > 0) || main) ? 'same' : '').then(p => {
580 if (!p)
581 return null;
582 if (!first)
583 first = p;
584 return drawNext(i + 1);
585 });
586 }
587
588 return drawNext(0);
589}
590
591addDrawFunc({ name: 'TGo4Marker', func: MarkerPainter.draw });
592addDrawFunc({ name: 'TGo4WinCond', func: ConditionPainter.draw });
593addDrawFunc({ name: 'TGo4PolyCond', func: ConditionPainter.draw });
594addDrawFunc({ name: 'TGo4ShapedCond', func: ConditionPainter.draw });
595addDrawFunc({ name: 'TGo4CondArray', func: drawCondArray });
596
597export { MarkerPainter, ConditionPainter, drawCondArray };