Skip to content

Commit

Permalink
Merge pull request #9 from kjvbrt/error_msg
Browse files Browse the repository at this point in the history
Showing error message if MC particle collection is not found
  • Loading branch information
kjvbrt authored Apr 9, 2024
2 parents 6bf61f8 + 2615fa4 commit 64dc27d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 56 deletions.
12 changes: 7 additions & 5 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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];
}
Expand Down
120 changes: 69 additions & 51 deletions js/tools.js
Original file line number Diff line number Diff line change
@@ -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 = "<p>INFO: " + msg + "</p>";
}

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

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) {
Expand Down

0 comments on commit 64dc27d

Please sign in to comment.