Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1389 lines (1198 loc) · 52.7 KB
/
Copy pathapp.js
File metadata and controls
1389 lines (1198 loc) · 52.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
import { createLheHandler } from './lhe_handler.js';
import { createHepmcHandler } from './hepmc_handler.js';
// =============================================================================
// PDG particle database
// =============================================================================
const pidMap = new Map();
function particleColor(pid) {
const abs = Math.abs(pid);
const anti = pid < 0;
const quarkColors = {
1: '#42a5f5', 2: '#ef5350', 3: '#66bb6a',
4: '#ab47bc', 5: '#ffa726', 6: '#ec407a',
};
if (abs >= 1 && abs <= 6) {
const c = quarkColors[abs];
return anti ? shadeColor(c, -30) : c;
}
if (abs === 11) return anti ? '#ff7043' : '#29b6f6';
if (abs === 13) return anti ? '#ce93d8' : '#9ccc65';
if (abs === 15) return anti ? '#78909c' : '#ffca28';
if (abs === 12 || abs === 14 || abs === 16) return '#cfd8dc';
if (pid === 21) return '#f06292';
if (pid === 22) return '#fff176';
if (pid === 23) return '#b0bec5';
if (pid === 24) return '#ef5350';
if (pid === -24) return '#26c6da';
if (pid === 25) return '#ffca28';
if (abs === 2212) return anti ? '#e53935' : '#ef9a9a';
if (abs === 2112) return anti ? '#78909c': '#cfd8dc';
if (abs === 211) return anti ? '#7e57c2' : '#ba68c8';
if (abs === 111) return '#ce93d8';
if (abs === 321 || abs === 310 || abs === 130) return '#26a69a';
if (abs === 221 || abs === 331 || abs === 333) return '#a1887f';
if (abs === 443) return '#5c6bc0';
return '#9e9e9e';
}
function shadeColor(hex, amount) {
const n = parseInt(hex.slice(1), 16);
const r = Math.min(255, Math.max(0, (n >> 16) + amount));
const g = Math.min(255, Math.max(0, ((n >> 8) & 0xff) + amount));
const b = Math.min(255, Math.max(0, (n & 0xff) + amount));
return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, '0')}`;
}
function initPidMap() {
const entries = [
[1, 'd', 'd', -1 / 3], [-1, 'd̄', '\\bar{d}', 1 / 3],
[2, 'u', 'u', 2 / 3], [-2, 'ū', '\\bar{u}', -2 / 3],
[3, 's', 's', -1 / 3], [-3, 's̄', '\\bar{s}', 1 / 3],
[4, 'c', 'c', 2 / 3], [-4, 'c̄', '\\bar{c}', -2 / 3],
[5, 'b', 'b', -1 / 3], [-5, 'b̄', '\\bar{b}', 1 / 3],
[6, 't', 't', 2 / 3], [-6, 't̄', '\\bar{t}', -2 / 3],
[11, 'e⁻', 'e^-', -1], [-11, 'e⁺', 'e^+', 1],
[12, 'νₑ', '\\nu_e', 0], [-12, 'ν̄ₑ', '\\bar{\\nu}_e', 0],
[13, 'μ⁻', '\\mu^-', -1], [-13, 'μ⁺', '\\mu^+', 1],
[14, 'ν_μ', '\\nu_\\mu', 0], [-14, 'ν̄_μ', '\\bar{\\nu}_\\mu', 0],
[15, 'τ⁻', '\\tau^-', -1], [-15, 'τ⁺', '\\tau^+', 1],
[16, 'ν_τ', '\\nu_\\tau', 0], [-16, 'ν̄_τ', '\\bar{\\nu}_\\tau', 0],
[21, 'g', 'g', 0],
[22, 'γ', '\\gamma', 0],
[23, 'Z⁰', 'Z^0', 0], [24, 'W⁺', 'W^+', 1], [-24, 'W⁻', 'W^-', -1],
[25, 'H⁰', 'H^0', 0],
[111, 'π⁰', '\\pi^0', 0], [211, 'π⁺', '\\pi^+', 1], [-211, 'π⁻', '\\pi^-', -1],
[130, 'K⁰L', 'K^0_L', 0], [310, 'K⁰S', 'K^0_S', 0],
[321, 'K⁺', 'K^+', 1], [-321, 'K⁻', 'K^-', -1],
[2112, 'n', 'n', 0], [-2112, 'n̄', '\\bar{n}', 0],
[2212, 'p', 'p', 1], [-2212, 'p̄', '\\bar{p}', -1],
[221, 'η', '\\eta', 0], [331, 'η′', "\\eta'", 0],
[333, 'φ', '\\phi', 0], [443, 'J/ψ', 'J/\\psi', 0],
[9000001, 'd', 'd', -1 / 3], [9000002, 'u', 'u', 2 / 3],
];
for (const [code, name, latex, charge] of entries) {
pidMap.set(code, { name, latex, charge, color: particleColor(code) });
}
}
initPidMap();
export function getParticleInfo(pid) {
const info = pidMap.get(pid);
if (info) return info;
return { name: 'unknown', latex: '\\mathrm{unknown}', charge: null, color: particleColor(pid) };
}
export function renderLatex(el, tex, displayMode = false) {
if (typeof katex !== 'undefined') {
katex.render(tex, el, { throwOnError: false, displayMode });
} else {
el.textContent = tex;
}
}
export function parseFortranFloat(s) {
return parseFloat(String(s).replace(/[dD](?=[+-]?\d)/, 'E'));
}
export function parseNumericLine(line) {
return line.trim().split(/\s+/).map(parseFortranFloat);
}
export function isIncoming(p) {
return p.istup < 0;
}
export const BEAM_NAMES = {
2212: 'p', [-2212]: 'p̄',
11: 'e⁻', [-11]: 'e⁺',
13: 'μ⁻', [-13]: 'μ⁺',
22: 'γ', 9000001: 'd', 9000002: 'u',
};
export function formatBeam(id) {
return BEAM_NAMES[id] ? `${BEAM_NAMES[id]} (${id})` : `PID ${id}`;
}
export function formatPdf(pdf, fallback) {
if (fallback) return fallback;
if (!pdf) return 'N/A';
if (pdf.group === 0 && pdf.set === 0) return 'built-in / sum-of-weights';
return `LHAPDF group ${pdf.group}, set ${pdf.set}`;
}
export function formatCrossSection(xsec, err) {
if (xsec == null || Number.isNaN(xsec)) return 'N/A';
const val = xsec.toExponential(4);
if (err != null && !Number.isNaN(err)) return `${val} ± ${err.toExponential(2)} pb`;
return `${val} pb`;
}
export function kinematicEta(p) {
const pt = Math.hypot(p.px, p.py);
if (pt < 1e-12) return p.pz >= 0 ? Infinity : -Infinity;
return Math.asinh(p.pz / pt);
}
export function kinematicPhi(p) {
return Math.atan2(p.py, p.px);
}
// =============================================================================
// Three.js visualization (shared by LHE and HepMC)
// =============================================================================
let scene, camera, renderer, labelRenderer, controls, viewportEl;
let currentEventGroup = null;
let coordinateAxesGroup = null;
let hitTargets = [];
const _labelWorldPos = new THREE.Vector3();
let currentView = '3d';
let showAxes = false;
let showKinematics = false;
let showParticleCoords = false;
const LABEL_REF_DIST = 10;
const LABEL_BASE_PX = 11;
const LABEL_MIN_PX = 5;
const LABEL_MAX_PX = 36;
function updateLabelScales() {
if (!currentEventGroup) return;
currentEventGroup.traverse(obj => {
if (!obj.isCSS2DObject) return;
obj.getWorldPosition(_labelWorldPos);
const dist = camera.position.distanceTo(_labelWorldPos);
const px = THREE.MathUtils.clamp(LABEL_BASE_PX * (LABEL_REF_DIST / dist), LABEL_MIN_PX, LABEL_MAX_PX);
const katexEl = obj.element.querySelector('.katex');
if (katexEl) katexEl.style.fontSize = `${px}px`;
});
}
function viewportSize() {
const w = viewportEl.clientWidth;
const h = viewportEl.clientHeight;
return { w: w || window.innerWidth, h: h || window.innerHeight };
}
function addAxisLabel(group, position, text, color, fontSize = '14px') {
const div = document.createElement('div');
div.style.color = color;
div.style.fontWeight = '500';
div.style.fontSize = fontSize;
div.style.opacity = '0.95';
div.style.textShadow = '0 0 6px rgba(0,0,0,1)';
div.textContent = text;
const label = new CSS2DObject(div);
label.position.copy(position);
group.add(label);
}
function buildCoordinateAxes() {
if (coordinateAxesGroup) {
scene.remove(coordinateAxesGroup);
coordinateAxesGroup.traverse(obj => {
if (obj.isCSS2DObject && obj.element?.parentNode) obj.element.remove();
});
}
if (!showAxes) {
return;
}
const axisLength = 6;
coordinateAxesGroup = new THREE.Group();
// Reset axes position to origin before shifting
if (currentView === '3d') {
// Z-axis (beam direction, blue)
const zMat = new THREE.LineBasicMaterial({ color: 0x4488ff, transparent: true, opacity: 0.5 });
const zPoints = [new THREE.Vector3(0, 0, -axisLength), new THREE.Vector3(0, 0, axisLength)];
const zLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(zPoints), zMat);
coordinateAxesGroup.add(zLine);
// X-axis (towards LHC ring center, red)
const xMat = new THREE.LineBasicMaterial({ color: 0xff4444, transparent: true, opacity: 0.5 });
const xPoints = [new THREE.Vector3(-axisLength, 0, 0), new THREE.Vector3(axisLength, 0, 0)];
const xLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(xPoints), xMat);
coordinateAxesGroup.add(xLine);
// Y-axis (upwards, green)
const yMat = new THREE.LineBasicMaterial({ color: 0x44ff44, transparent: true, opacity: 0.5 });
const yPoints = [new THREE.Vector3(0, -axisLength, 0), new THREE.Vector3(0, axisLength, 0)];
const yLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(yPoints), yMat);
coordinateAxesGroup.add(yLine);
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(axisLength + 0.6, 0, 0), '+x (towards LHC ring center)', '#ff4444');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, axisLength + 0.6, 0), '+y (up)', '#44ff44');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, 0, axisLength + 0.6), '+z (beam)', '#4488ff');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, 0, -axisLength - 0.6), '-z', '#4488ff');
} else if (currentView === 'transverse') {
// X-axis (red)
const xMat = new THREE.LineBasicMaterial({ color: 0xff4444, transparent: true, opacity: 0.6 });
const xPoints = [new THREE.Vector3(-axisLength, 0, 0), new THREE.Vector3(axisLength, 0, 0)];
const xLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(xPoints), xMat);
coordinateAxesGroup.add(xLine);
// Y-axis (green)
const yMat = new THREE.LineBasicMaterial({ color: 0x44ff44, transparent: true, opacity: 0.6 });
const yPoints = [new THREE.Vector3(0, -axisLength, 0), new THREE.Vector3(0, axisLength, 0)];
const yLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(yPoints), yMat);
coordinateAxesGroup.add(yLine);
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(axisLength + 0.6, 0, 0), '+x', '#ff4444');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, axisLength + 0.6, 0), '+y', '#44ff44');
} else if (currentView === 'longitudinal') {
// Z-axis (blue, beam)
const zMat = new THREE.LineBasicMaterial({ color: 0x4488ff, transparent: true, opacity: 0.6 });
const zPoints = [new THREE.Vector3(0, 0, -axisLength), new THREE.Vector3(0, 0, axisLength)];
const zLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(zPoints), zMat);
coordinateAxesGroup.add(zLine);
// Y-axis (green)
const yMat = new THREE.LineBasicMaterial({ color: 0x44ff44, transparent: true, opacity: 0.6 });
const yPoints = [new THREE.Vector3(0, -axisLength, 0), new THREE.Vector3(0, axisLength, 0)];
const yLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(yPoints), yMat);
coordinateAxesGroup.add(yLine);
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, axisLength + 0.6, 0), '+y (up)', '#44ff44');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, 0, axisLength + 0.6), '+z (beam)', '#4488ff');
addAxisLabel(coordinateAxesGroup, new THREE.Vector3(0, 0, -axisLength - 0.6), '-z', '#4488ff');
}
scene.add(coordinateAxesGroup);
}
function setViewCamera(view) {
currentView = view;
const distance = 12;
if (view === '3d') {
camera.position.set(6, 5, 9);
camera.up.set(0, 1, 0);
camera.lookAt(0, 0, 0);
controls.target.set(0, 0, 0);
} else if (view === 'transverse') {
camera.position.set(0, 0, distance);
camera.up.set(0, 1, 0);
camera.lookAt(0, 0, 0);
controls.target.set(0, 0, 0);
} else if (view === 'longitudinal') {
camera.position.set(-distance, 0, 0); // Look along negative x-axis to get z-y plane
camera.up.set(0, 1, 0);
camera.lookAt(0, 0, 0);
controls.target.set(0, 0, 0);
}
buildCoordinateAxes();
if (activeHandler?.hasVisual()) {
loadEventByNumber(currentEventNumber);
}
}
export function initThree() {
viewportEl = document.getElementById('viewport');
scene = new THREE.Scene();
scene.background = new THREE.Color(0x050b1a);
scene.fog = new THREE.FogExp2(0x050b1a, 0.006);
const { w, h } = viewportSize();
camera = new THREE.PerspectiveCamera(45, w / h, 0.1, 500);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(w, h);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
viewportEl.insertBefore(renderer.domElement, viewportEl.firstChild);
labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(w, h);
labelRenderer.domElement.className = 'particle-label-layer';
viewportEl.appendChild(labelRenderer.domElement);
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.target.set(0, 0, 0);
scene.add(new THREE.AmbientLight(0x606080, 0.7));
const dir = new THREE.DirectionalLight(0xffffff, 0.85);
dir.position.set(5, 8, 4);
scene.add(dir);
const fill = new THREE.DirectionalLight(0x6688cc, 0.35);
fill.position.set(-4, 2, -3);
scene.add(fill);
setViewCamera('3d');
window.addEventListener('resize', () => {
const { w: rw, h: rh } = viewportSize();
camera.aspect = rw / rh;
camera.updateProjectionMatrix();
renderer.setSize(rw, rh);
labelRenderer.setSize(rw, rh);
});
(function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
updateLabelScales();
labelRenderer.render(scene, camera);
})();
}
export function getRenderer() {
return renderer;
}
export function cleanupEventVisual() {
if (!currentEventGroup) return;
currentEventGroup.traverse(obj => {
if (obj.isCSS2DObject && obj.element?.parentNode) obj.element.remove();
if (obj.geometry) obj.geometry.dispose();
if (obj.material) {
if (Array.isArray(obj.material)) obj.material.forEach(m => m.dispose());
else obj.material.dispose();
}
});
scene.remove(currentEventGroup);
currentEventGroup = null;
hitTargets = [];
// Also clean up coordinate axes
if (coordinateAxesGroup) {
coordinateAxesGroup.traverse(obj => {
if (obj.isCSS2DObject && obj.element?.parentNode) obj.element.remove();
if (obj.geometry) obj.geometry.dispose();
if (obj.material) {
if (Array.isArray(obj.material)) obj.material.forEach(m => m.dispose());
else obj.material.dispose();
}
});
scene.remove(coordinateAxesGroup);
coordinateAxesGroup = null;
}
}
function momentumDirection(p) {
const v = new THREE.Vector3(p.px, p.py, p.pz);
if (v.lengthSq() < 1e-12) return new THREE.Vector3(0, 0, 1);
return v.normalize();
}
function arrowLength(p, maxP) {
const pMag = Math.hypot(p.px, p.py, p.pz);
const ratio = maxP > 0 ? pMag / maxP : 0.5;
return 0.5 + ratio * 1.8;
}
function buildChildrenMap(particles) {
const map = new Map();
const link = (mother, child) => {
if (mother <= 0 || mother > particles.length) return;
if (!map.has(mother)) map.set(mother, []);
if (!map.get(mother).includes(child)) map.get(mother).push(child);
};
particles.forEach((p, i) => {
const idx = i + 1;
link(p.moth1, idx);
if (p.moth2 > 0 && p.moth2 !== p.moth1) link(p.moth2, idx);
});
return map;
}
function findRootIndices(particles) {
const incoming = new Set();
particles.forEach((p, i) => { if (isIncoming(p)) incoming.add(i + 1); });
const roots = [];
particles.forEach((p, i) => {
const idx = i + 1;
if (isIncoming(p)) return;
const mothers = [p.moth1, p.moth2].filter(m => m > 0);
if (mothers.length === 0) {
roots.push(idx);
return;
}
if (mothers.every(m => incoming.has(m))) roots.push(idx);
});
return roots;
}
function hexColor(color) {
return typeof color === 'string' ? new THREE.Color(color) : color;
}
function addSphereLabel(group, position, latex) {
const div = document.createElement('div');
div.className = 'particle-label';
renderLatex(div, latex, true);
const label = new CSS2DObject(div);
label.position.copy(position);
group.add(label);
}
function addIncomingBeam(group, start, dir, length, particle) {
const end = start.clone().add(dir.clone().multiplyScalar(length));
const geo = new THREE.BufferGeometry().setFromPoints([start, end]);
const line = new THREE.Line(geo, new THREE.LineDashedMaterial({
color: 0x4ade80,
dashSize: 0.2,
gapSize: 0.14,
transparent: true,
opacity: 0.92,
}));
line.computeLineDistances();
group.add(line);
const hit = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, length, 6),
new THREE.MeshBasicMaterial({ visible: false })
);
hit.position.copy(start.clone().add(dir.clone().multiplyScalar(length * 0.5)));
hit.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir);
hit.userData.particle = particle;
group.add(hit);
hitTargets.push(hit);
}
function deltaPhi(phi1, phi2) {
let dphi = phi1 - phi2;
while (dphi > Math.PI) dphi -= 2 * Math.PI;
while (dphi < -Math.PI) dphi += 2 * Math.PI;
return Math.abs(dphi);
}
function calculatePt(particle) {
return Math.hypot(particle.px, particle.py);
}
function calculatePhi(particle) {
return Math.atan2(particle.py, particle.px);
}
function addParticleTrack(group, start, dir, length, color, particle, info, lineRadius = 0.016, sphereRadius = 0.085, arrowLength = 0.15, arrowRadius = 0.04) {
const col = hexColor(color);
const end = start.clone().add(dir.clone().multiplyScalar(length));
const trackMat = new THREE.MeshStandardMaterial({
color: col,
emissive: col,
emissiveIntensity: 0.3,
metalness: 0.12,
roughness: 0.5,
});
const track = new THREE.Mesh(
new THREE.CylinderGeometry(lineRadius, lineRadius, length, 10),
trackMat
);
track.position.copy(start.clone().add(dir.clone().multiplyScalar(length * 0.5)));
track.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir);
group.add(track);
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(arrowRadius, arrowLength, 12),
trackMat
);
arrow.position.copy(end.clone().sub(dir.clone().multiplyScalar(arrowLength * 0.5)));
arrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir);
group.add(arrow);
const sphereMat = new THREE.MeshStandardMaterial({
color: col,
emissive: col,
emissiveIntensity: 0.45,
metalness: 0.2,
roughness: 0.4,
transparent: true,
opacity: 0.88,
});
const sphere = new THREE.Mesh(new THREE.SphereGeometry(sphereRadius, 18, 18), sphereMat);
sphere.position.copy(end);
sphere.userData.particle = particle;
group.add(sphere);
hitTargets.push(sphere);
// Keep particle name label on the sphere
addSphereLabel(group, end, info.latex || info.name);
if (showParticleCoords) {
// Add compact, stacked (pT, phi, eta) label with units, on the left
const pT = calculatePt(particle);
const phi = calculatePhi(particle);
const eta = kinematicEta(particle);
const kinematicsDiv = document.createElement('div');
kinematicsDiv.style.color = '#94a3b8';
kinematicsDiv.style.fontSize = '9px';
kinematicsDiv.style.fontFamily = 'Consolas, monospace';
kinematicsDiv.style.textShadow = '0 0 3px rgba(0,0,0,1)';
kinematicsDiv.style.background = 'rgba(5,11,26,0.9)';
kinematicsDiv.style.padding = '2px 4px';
kinematicsDiv.style.borderRadius = '3px';
kinematicsDiv.style.lineHeight = '1.2';
kinematicsDiv.style.display = 'flex';
kinematicsDiv.style.flexDirection = 'column';
kinematicsDiv.style.alignItems = 'flex-start';
// Create stacked spans
const ptSpan = document.createElement('span');
ptSpan.textContent = `pT: ${pT.toFixed(2)} GeV`;
const phiSpan = document.createElement('span');
phiSpan.textContent = `φ: ${phi.toFixed(3)}`;
const etaSpan = document.createElement('span');
etaSpan.textContent = `η: ${eta.toFixed(3)}`;
kinematicsDiv.appendChild(ptSpan);
kinematicsDiv.appendChild(phiSpan);
kinematicsDiv.appendChild(etaSpan);
const kinematicsLabel = new CSS2DObject(kinematicsDiv);
// Position labels so they don't cover the particle name
if (currentView === 'transverse') {
kinematicsLabel.position.copy(end.clone().add(new THREE.Vector3(-0.15, 0, 0)));
} else if (currentView === 'longitudinal') {
kinematicsLabel.position.copy(end.clone().add(new THREE.Vector3(0, 0.15, 0))); // above particle in y-z plane
} else { // 3D view
kinematicsLabel.position.copy(end.clone().add(new THREE.Vector3(-0.15, 0, 0)));
}
group.add(kinematicsLabel);
}
}
function calculateTheta(particle) {
const pT = calculatePt(particle);
const pz = particle.pz;
return Math.atan2(pT, pz);
}
function calculateDeltaR(particle1, particle2) {
const dphi = deltaPhi(calculatePhi(particle1), calculatePhi(particle2));
const deta = kinematicEta(particle1) - kinematicEta(particle2);
return Math.sqrt(dphi * dphi + deta * deta);
}
function addDeltaArcs(group, daughterPairs, decayVertices, view, roots, origin) {
if (!showKinematics) return;
for (const { motherIdx, daughters } of daughterPairs) {
const vertex = decayVertices.get(motherIdx);
if (!vertex) continue;
// Get all pairwise combinations of daughters
for (let i = 0; i < daughters.length; i++) {
for (let j = i + 1; j < daughters.length; j++) {
const d1 = daughters[i];
const d2 = daughters[j];
let labelText;
if (view === '3d') {
// Calculate ΔR for 3D view
const deltaR = calculateDeltaR(d1.p, d2.p);
labelText = `ΔR = ${deltaR.toFixed(3)}`;
} else if (view === 'transverse') {
// Calculate Δφ for transverse view
const angle1 = Math.atan2(d1.p.py, d1.p.px);
const angle2 = Math.atan2(d2.p.py, d2.p.px);
const delta = deltaPhi(angle1, angle2);
labelText = `Δφ = ${delta.toFixed(3)}`;
} else { // longitudinal
// Calculate both Δη and Δθ for longitudinal view
const eta1 = kinematicEta(d1.p);
const eta2 = kinematicEta(d2.p);
const deta = Math.abs(eta1 - eta2);
const theta1 = calculateTheta(d1.p);
const theta2 = calculateTheta(d2.p);
let dtheta = Math.abs(theta1 - theta2);
if (dtheta > Math.PI) dtheta = 2 * Math.PI - dtheta;
labelText = `Δη = ${deta.toFixed(3)}\nΔθ = ${dtheta.toFixed(3)}`;
}
// For 3D, let's just add a label at the vertex since drawing an arc in 3D is tricky
if (view === '3d') {
const labelDiv = document.createElement('div');
labelDiv.style.color = '#66d9e8';
labelDiv.style.fontWeight = 'bold';
labelDiv.style.fontSize = '12px';
labelDiv.style.textShadow = '0 0 5px rgba(0,0,0,1)';
labelDiv.style.background = 'rgba(5,11,26,0.8)';
labelDiv.style.padding = '3px 6px';
labelDiv.style.borderRadius = '4px';
labelDiv.style.whiteSpace = 'pre-line'; // for newlines
labelDiv.textContent = labelText;
const label = new CSS2DObject(labelDiv);
label.position.copy(vertex.clone().add(new THREE.Vector3(0, 0.2, 0)));
group.add(label);
} else {
let angle1, angle2;
if (view === 'transverse') {
angle1 = Math.atan2(d1.p.py, d1.p.px);
angle2 = Math.atan2(d2.p.py, d2.p.px);
} else {
// For longitudinal view: angle based on z (beam) and y axes
angle1 = Math.atan2(d1.p.py, d1.p.pz); // theta in y-z plane
angle2 = Math.atan2(d2.p.py, d2.p.pz);
}
// Draw arc centered at decay vertex
const arcRadius = 0.6; // Smaller for decay vertices
const arcPoints = [];
const steps = 40;
let startAngle = Math.min(angle1, angle2);
let endAngle = Math.max(angle1, angle2);
if (endAngle - startAngle > Math.PI) {
[startAngle, endAngle] = [endAngle, startAngle];
startAngle -= 2 * Math.PI;
}
for (let s = 0; s <= steps; s++) {
const t = startAngle + (endAngle - startAngle) * (s / steps);
if (view === 'transverse') {
arcPoints.push(
new THREE.Vector3(
vertex.x + Math.cos(t) * arcRadius,
vertex.y + Math.sin(t) * arcRadius,
vertex.z
)
);
} else {
// Longitudinal: y-z plane
arcPoints.push(
new THREE.Vector3(
vertex.x,
vertex.y + Math.sin(t) * arcRadius,
vertex.z + Math.cos(t) * arcRadius
)
);
}
}
const arcMat = new THREE.LineBasicMaterial({
color: 0x66d9e8,
transparent: true,
opacity: 0.7
});
const arcGeo = new THREE.BufferGeometry().setFromPoints(arcPoints);
const arc = new THREE.Line(arcGeo, arcMat);
group.add(arc);
// Add label
const midAngle = (startAngle + endAngle) / 2;
const labelOffset = arcRadius + 0.2;
let labelPos;
if (view === 'transverse') {
labelPos = new THREE.Vector3(
vertex.x + Math.cos(midAngle) * labelOffset,
vertex.y + Math.sin(midAngle) * labelOffset,
vertex.z
);
} else {
labelPos = new THREE.Vector3(
vertex.x,
vertex.y + Math.sin(midAngle) * labelOffset,
vertex.z + Math.cos(midAngle) * labelOffset
);
}
const dLabelDiv = document.createElement('div');
dLabelDiv.style.color = '#66d9e8';
dLabelDiv.style.fontWeight = 'bold';
dLabelDiv.style.fontSize = '12px';
dLabelDiv.style.textShadow = '0 0 5px rgba(0,0,0,1)';
dLabelDiv.style.background = 'rgba(5,11,26,0.8)';
dLabelDiv.style.padding = '3px 6px';
dLabelDiv.style.borderRadius = '4px';
dLabelDiv.style.whiteSpace = 'pre-line';
dLabelDiv.textContent = labelText;
const dLabel = new CSS2DObject(dLabelDiv);
dLabel.position.copy(labelPos);
group.add(dLabel);
}
}
}
}
}
export function renderEvent(particles) {
cleanupEventVisual();
if (!particles.length) return;
const group = new THREE.Group();
const origin = new THREE.Vector3(0, 0, 0);
const childrenMap = buildChildrenMap(particles);
const maxP = Math.max(...particles.map(p => Math.hypot(p.px, p.py, p.pz)), 1e-6);
const maxPt = Math.max(...particles.map(p => Math.hypot(p.px, p.py)), 1e-6);
const drawn = new Set();
const finalStateParticles = [];
const roots = findRootIndices(particles);
// Track decay vertices and direct daughters
const decayVertices = new Map(); // key: particleIdx, value: position
const daughterPairs = []; // Array of { motherIdx, daughters: [{ p, info, endPos }] }
const allParticlePositions = []; // Collect all particle sphere positions for centering
// Add initial collision particles as a daughter pair with mother at origin
if (roots.length >= 2) {
const initialDaughters = roots.map(idx => ({
p: particles[idx - 1],
info: getParticleInfo(particles[idx - 1].idup)
}));
daughterPairs.push({ motherIdx: 0, daughters: initialDaughters });
decayVertices.set(0, origin.clone());
}
if (currentView === '3d') {
particles.forEach((p, i) => {
if (!isIncoming(p)) return;
const beamZ = p.pz >= 0 ? -3.4 : 3.4;
const start = new THREE.Vector3(0, 0, beamZ);
const dir = origin.clone().sub(start).normalize();
const len = start.distanceTo(origin);
addIncomingBeam(group, start, dir, len, p);
});
function drawBranch(particleIdx, startPos) {
if (drawn.has(particleIdx)) return;
const p = particles[particleIdx - 1];
if (!p || isIncoming(p)) return;
drawn.add(particleIdx);
const dir = momentumDirection(p);
const len = arrowLength(p, maxP);
const info = getParticleInfo(p.idup);
const end = startPos.clone().add(dir.clone().multiplyScalar(len));
addParticleTrack(group, startPos, dir, len, info.color, p, info);
// Collect particle position for centering
allParticlePositions.push(end.clone());
const childList = childrenMap.get(particleIdx) || [];
const hasOutgoingDaughters = childList.some(idx => !isIncoming(particles[idx - 1]));
// Track decay vertex and daughters
if (hasOutgoingDaughters && childList.length >= 2) {
const daughters = [];
decayVertices.set(particleIdx, end.clone());
for (const childIdx of childList) {
if (isIncoming(particles[childIdx - 1])) continue;
daughters.push({
p: particles[childIdx - 1],
info: getParticleInfo(particles[childIdx - 1].idup)
});
}
if (daughters.length >= 2) {
daughterPairs.push({ motherIdx: particleIdx, daughters });
}
}
if (!hasOutgoingDaughters) {
finalStateParticles.push({ p, info, idx: particleIdx });
}
for (const childIdx of childList) {
if (isIncoming(particles[childIdx - 1])) continue;
drawBranch(childIdx, end);
}
}
for (const rootIdx of roots) drawBranch(rootIdx, origin);
particles.forEach((p, i) => {
const idx = i + 1;
if (isIncoming(p) || drawn.has(idx)) return;
drawBranch(idx, origin);
});
// Add delta R labels for 3D view
addDeltaArcs(group, daughterPairs, decayVertices, currentView, roots, origin);
} else {
const finalProjs = [];
function draw2DBranch(particleIdx, startPos) {
if (drawn.has(particleIdx)) return;
const p = particles[particleIdx - 1];
if (!p || isIncoming(p)) return;
drawn.add(particleIdx);
const info = getParticleInfo(p.idup);
let px, py, pz;
let lengthScale;
let endPosition;
const scaleFactor = 0.25; // Even shorter arrows
if (currentView === 'transverse') {
px = p.px;
py = p.py;
pz = 0;
const pt = Math.hypot(px, py);
lengthScale = scaleFactor + (pt / maxPt) * 1.0;
const dir = new THREE.Vector3(px, py, 0).normalize();
endPosition = startPos.clone().add(dir.clone().multiplyScalar(lengthScale));
} else { // longitudinal
px = 0;
py = p.py;
pz = p.pz;
const pl = Math.hypot(py, pz);
lengthScale = scaleFactor + (pl / maxPt) * 1.0;
const dir = new THREE.Vector3(0, py, pz).normalize();
endPosition = startPos.clone().add(dir.clone().multiplyScalar(lengthScale));
}
// Now draw the particle track with thick cylinder, arrow, sphere, and tooltip!
const dirVec = new THREE.Vector3(px, py, pz).normalize();
addParticleTrack(group, startPos, dirVec, lengthScale, info.color, p, info,
0.03, // Thicker line
0.12, // Thicker sphere
0.18, // Longer arrow
0.06 // Thicker arrow
);
// Collect particle position for centering
allParticlePositions.push(endPosition.clone());
const childList = childrenMap.get(particleIdx) || [];
const hasOutgoingDaughters = childList.some(idx => !isIncoming(particles[idx - 1]));
// Track decay vertex and daughters
if (hasOutgoingDaughters && childList.length >= 2) {
const daughters = [];
decayVertices.set(particleIdx, endPosition.clone());
for (const childIdx of childList) {
if (isIncoming(particles[childIdx - 1])) continue;
daughters.push({
p: particles[childIdx - 1],
info: getParticleInfo(particles[childIdx - 1].idup)
});
}
if (daughters.length >= 2) {
daughterPairs.push({ motherIdx: particleIdx, daughters });
}
}
if (!hasOutgoingDaughters) {
finalStateParticles.push({ p, info, idx: particleIdx });
finalProjs.push({ p, info, idx: particleIdx, px, py, endPos: endPosition });
}
for (const childIdx of childList) {
if (isIncoming(particles[childIdx - 1])) continue;
draw2DBranch(childIdx, endPosition);
}
}
for (const rootIdx of roots) draw2DBranch(rootIdx, origin);
particles.forEach((p, i) => {
const idx = i + 1;
if (isIncoming(p) || drawn.has(idx)) return;
draw2DBranch(idx, origin);
});
// Add MET only in transverse view
if (currentView === 'transverse') {
let metX = 0, metY = 0;
finalStateParticles.forEach(({ p }) => {
metX -= p.px;
metY -= p.py;
});
const met = Math.hypot(metX, metY);
if (met > 1e-3) {
const metVec = new THREE.Vector3(metX, metY, 0);
const metDir = metVec.clone().normalize();
const metLen = 0.3 + (met / maxPt) * 1.2;
const metEnd = origin.clone().add(metDir.clone().multiplyScalar(metLen));
// Draw MET track with dashed line
const metGeo = new THREE.CylinderGeometry(0.03, 0.03, metLen, 12);
const metMat = new THREE.MeshStandardMaterial({
color: 0xff6b6b,
emissive: 0xff6b6b,
emissiveIntensity: 0.3,
metalness: 0.12,
roughness: 0.5
});
const metTrack = new THREE.Mesh(metGeo, metMat);
metTrack.position.copy(origin.clone().add(metDir.clone().multiplyScalar(metLen / 2)));
metTrack.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), metDir);
group.add(metTrack);
// Dashed line to indicate MET
const dashedMat = new THREE.LineDashedMaterial({
color: 0xff6b6b,
dashSize: 0.1,
gapSize: 0.1,
transparent: true,
opacity: 0.6
});
const dashedGeo = new THREE.BufferGeometry().setFromPoints([origin, metEnd]);
const dashedLine = new THREE.Line(dashedGeo, dashedMat);
dashedLine.computeLineDistances();
group.add(dashedLine);
// MET arrow
const metArrowGeom = new THREE.ConeGeometry(0.06, 0.18, 12);
const metArrow = new THREE.Mesh(metArrowGeom, metMat);
metArrow.position.copy(metEnd.clone().sub(metDir.clone().multiplyScalar(0.09)));
metArrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), metDir);
group.add(metArrow);
// MET label
const metDiv = document.createElement('div');
metDiv.style.color = '#ff6b6b';
metDiv.style.fontWeight = 'bold';
metDiv.style.fontSize = '13px';
metDiv.style.textShadow = '0 0 5px rgba(0,0,0,1)';
metDiv.style.background = 'rgba(5,11,26,0.85)';
metDiv.style.padding = '4px 8px';
metDiv.style.borderRadius = '6px';
metDiv.textContent = `p_{T}^{miss} = ${met.toFixed(2)} GeV`;
const metLabel = new CSS2DObject(metDiv);
metLabel.position.copy(metEnd.clone().add(new THREE.Vector3(0, 0.2, 0)));
group.add(metLabel);
}
}
// Add Δφ or Δη arcs
addDeltaArcs(group, daughterPairs, decayVertices, currentView, roots, origin);
}
// First build coordinate axes (so it exists before centering)
buildCoordinateAxes();
// Calculate center of all particle positions and shift both event and axes to center the view
if (allParticlePositions.length > 0) {
const center = new THREE.Vector3(0, 0, 0);
allParticlePositions.forEach(pos => center.add(pos));
center.divideScalar(allParticlePositions.length);
// Shift everything by negative of the center
group.position.sub(center);
// Also shift coordinate axes to follow the event (since it's now built)
if (coordinateAxesGroup) {
coordinateAxesGroup.position.sub(center);
}
// Also adjust decay vertices for delta arcs (only for 2D views, since 3D delta is label only)
if (currentView !== '3d') {
decayVertices.forEach((pos, key) => {
pos.sub(center);
});
}