From 2615fa40a4988dd5b815b4fddca002e488239945 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Tue, 9 Apr 2024 17:44:22 +0200 Subject: [PATCH] Showing error message if no MC particle collection not found --- js/main.js | 12 +++--- js/tools.js | 120 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 76 insertions(+), 56 deletions(-) diff --git a/js/main.js b/js/main.js index 641e3919..8bd73522 100644 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,4 @@ -import { loadMCParticles } from "./tools.js"; +import { errorMsg, loadMCParticles } from "./tools.js"; const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d"); @@ -278,13 +278,11 @@ document.getElementById("input-file") .addEventListener("change", (event) => { for (const file of event.target.files) { if (!file.name.endsWith("edm4hep.json")) { - document.getElementById("input-message") - .innerHTML = "ERROR: Provided file is not EDM4hep JSON!"; + errorMsg("Provided file is not EDM4hep JSON!"); } if (!file.type.endsWith("/json")) { - document.getElementById("input-message") - .innerHTML = "ERROR: Provided file is not EDM4hep JSON!"; + errorMsg("ERROR: Provided file is not EDM4hep JSON!"); } const reader = new FileReader(); @@ -309,6 +307,10 @@ document.getElementById("visualize-button") const eventNum = document.getElementById("event-number").value; loadMCParticles(jsonData, eventNum, infoBoxes, parentLinks, childrenLinks); + if (infoBoxes.length === 0) { + errorMsg("Provided file does not contain any MC particle tree!"); + return; + } for (const eventNum in jsonData) { delete jsonData[eventNum]; } diff --git a/js/tools.js b/js/tools.js index 4afe095a..00d9cf6f 100644 --- a/js/tools.js +++ b/js/tools.js @@ -1,69 +1,87 @@ import { InfoBox, Link } from "./objects.js"; +export function infoMsg(msg) { + const msgDiv = document.getElementById("input-message"); + msgDiv.classList.add("mb-20"); + msgDiv.style.color = "gray"; + msgDiv.innerHTML = "

INFO: " + msg + "

"; +} + +export function errorMsg(msg) { + const msgDiv = document.getElementById("input-message"); + msgDiv.classList.add("mb-20"); + msgDiv.style.color = "red"; + msgDiv.innerHTML = "

ERROR: " + msg + "

"; +} export function loadMCParticles(jsonData, eventNum, infoBoxes, parentLinks, childrenLinks) { const eventData = jsonData["Event " + eventNum]; - const mcParticles = Object.values(eventData).find(element => element.collType == "edm4hep::MCParticleCollection"); + try { + const mcParticles = Object.values(eventData).find(element => element.collType == "edm4hep::MCParticleCollection"); + for (const [i, particle] of mcParticles.collection.entries()) { + const box = new InfoBox(i); + box.pdg = particle.PDG; + box.genStatus = particle.generatorStatus; + box.simStatus = particle.simulatorStatus; + box.momentum = Math.sqrt(Math.pow(particle.momentum.x, 2) + + Math.pow(particle.momentum.y, 2) + + Math.pow(particle.momentum.z, 2)); + box.momentum = Math.round(box.momentum * 100) / 100; + box.vertex = Math.sqrt(Math.pow(particle.vertex.x, 2) + + Math.pow(particle.vertex.y, 2) + + Math.pow(particle.vertex.z, 2)); + box.vertex = Math.round(box.vertex * 100) / 100; + box.px = Math.round(particle.momentum.x * 100) / 100; + box.py = Math.round(particle.momentum.y * 100) / 100; + box.pz = Math.round(particle.momentum.z * 100) / 100; - for (const [i, particle] of mcParticles.collection.entries()) { - const box = new InfoBox(i); - box.pdg = particle.PDG; - box.genStatus = particle.generatorStatus; - box.simStatus = particle.simulatorStatus; - box.momentum = Math.sqrt(Math.pow(particle.momentum.x, 2) - + Math.pow(particle.momentum.y, 2) - + Math.pow(particle.momentum.z, 2)); - box.momentum = Math.round(box.momentum * 100) / 100; - box.vertex = Math.sqrt(Math.pow(particle.vertex.x, 2) - + Math.pow(particle.vertex.y, 2) - + Math.pow(particle.vertex.z, 2)); - box.vertex = Math.round(box.vertex * 100) / 100; - box.px = Math.round(particle.momentum.x * 100) / 100; - box.py = Math.round(particle.momentum.y * 100) / 100; - box.pz = Math.round(particle.momentum.z * 100) / 100; + box.vx = particle.vertex.x; + box.vy = particle.vertex.y; + box.vz = particle.vertex.z; - box.vx = particle.vertex.x; - box.vy = particle.vertex.y; - box.vz = particle.vertex.z; + box.charge = particle.charge; + box.time = Math.round(particle.time * 100) / 100; + box.mass = Math.round(particle.mass * 100) / 100; - box.charge = particle.charge; - box.time = Math.round(particle.time * 100) / 100; - box.mass = Math.round(particle.mass * 100) / 100; + box.name = getName(particle.PDG); + box.updateTexImg(); - box.name = getName(particle.PDG); - box.updateTexImg(); + if (particle.parents.length === 0 && particle.daughters.length === 0) { + box.row = -1; + console.log("WARNING: Standalone particle!"); + } - if (particle.parents.length === 0 && particle.daughters.length === 0) { - box.row = -1; - console.log("WARNING: Standalone particle!"); - } + if (particle.parents.length === 0) { + box.row = 0; + } - if (particle.parents.length === 0) { - box.row = 0; - } + for (const j in particle.parents) { + const parentId = particle.parents[j].index; + box.parents.push(parentId); + const link = new Link(parseInt(parentLinks.length), parentId, i); + link.color = "#A00"; // Darkish red + link.xShift = 3; + parentLinks.push(link); + box.parentLinks.push(link.id); + } - for (const j in particle.parents) { - const parentId = particle.parents[j].index; - box.parents.push(parentId); - const link = new Link(parseInt(parentLinks.length), parentId, i); - link.color = "#A00"; // Darkish red - link.xShift = 3; - parentLinks.push(link); - box.parentLinks.push(link.id); - } + for (const j in particle.daughters) { + const childrenId = particle.daughters[j].index; + box.children.push(childrenId); + const link = new Link(parseInt(childrenLinks.length), i, childrenId); + link.color = "#0A0"; // Darkish green + link.xShift = -3; + childrenLinks.push(link); + box.childrenLinks.push(link.id); + } - for (const j in particle.daughters) { - const childrenId = particle.daughters[j].index; - box.children.push(childrenId); - const link = new Link(parseInt(childrenLinks.length), i, childrenId); - link.color = "#0A0"; // Darkish green - link.xShift = -3; - childrenLinks.push(link); - box.childrenLinks.push(link.id); + infoBoxes.push(box); + } + } catch (err) { + if (err instanceof TypeError) { + return; } - - infoBoxes.push(box); } const getMaxRow = function(parentIds) {