Skip to content

Commit

Permalink
test suite for loadParticles and buildLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed May 31, 2024
1 parent 186024e commit fbfb0ea
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
33 changes: 24 additions & 9 deletions js/types/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,47 @@ const loadersConfig = [
"Cluster",
];

function selectedParticles(loaders, particles) {
let newLoader = {};
export function buildLoader(config) {
let newLoader = {
types: {},
getLoader: (name) => {
if (newLoader.types.hasOwnProperty(name)) {
return newLoader.types[name];
}
return false;
},
getAllLoaders: () => {
return newLoader.types;
},
};

for (const particle of particles) {
for (const particle of config) {
if (loaders.hasOwnProperty(particle)) {
newLoader[particle] = loaders[particle];
newLoader.types[particle] = loaders[particle];
}
}

return newLoader;
}

export function loadParticles(event, loadersConfig) {
export function loadParticles(jsonData, event, loadersConfig) {
const eventData = Object.values(jsonData["Event " + event]);

const particles = [];
const particles = {};

const loader = selectedParticles(loaders, loadersConfig);
if (typeof loadersConfig === "string") loadersConfig = [loadersConfig];
const loader = buildLoader(loadersConfig);

for (const [type, loadFunction] of Object.entries(loader)) {
Object.keys(loader.getAllLoaders()).forEach((key) => (particles[key] = []));

for (const [type, loadFunction] of Object.entries(loader.getAllLoaders())) {
const particlesType = eventData.filter(
(element) => element.collType === `edm4hep::${type}Collection`
);

particlesType.forEach((collection) => {
const loadedParticles = loadFunction(collection.collection);
particles[type] = loadedParticles;
});
}
try {
Expand All @@ -59,4 +74,4 @@ export function loadParticles(event, loadersConfig) {
return particles;
}

loadParticles(4, loadersConfig);
loadParticles(jsonData, 4, loadersConfig);
77 changes: 77 additions & 0 deletions test/load.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { buildLoader, loadParticles } from "../js/types/load";

const data = {
"Event 0": {
"AllMuon": {
"collID": 12,
"collType": "edm4hep::ReconstructedParticleCollection",
"collection": [],
},
"EFlowPhoton": {
"collID": 7,
"collType": "edm4hep::ClusterCollection",
"collection": [
{
"clusters": [],
"directionError": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
},
"energy": 6.2020978927612305,
"energyError": 0.0,
"hits": [],
"iTheta": 0.0,
"particleIDs": [],
"phi": 0.0,
"position": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
},
"positionError": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"shapeParameters": [],
"subdetectorEnergies": [],
"type": 0,
},
],
},
},
};

describe("build loader", () => {
it("should create a loader with a set of types", () => {
const loadersConfig = ["ReconstructedParticle", "ParticleID"];
const loader = buildLoader(loadersConfig);
const loaderFunctions = loadersConfig.map((type) => loader.getLoader(type));

expect(loaderFunctions.every((f) => typeof f === "function")).toBe(true);
});

it("should fail when requesting a loader that doesn't exist", () => {
const loadersConfig = ["ReconstructedParticle", "ParticleID"];
const loader = buildLoader(loadersConfig);

expect(loader.getLoader("Vertex")).toBe(false);
});
});

describe("load different types of particles", () => {
it("should only load particles of one type", () => {
const loadersConfig = "ReconstructedParticle";

const particles = loadParticles(data, "0", loadersConfig);

expect(particles.hasOwnProperty(loadersConfig)).toBe(true);
});

it("should load particles of multiple types", () => {
const loadersConfig = ["ReconstructedParticle", "Cluster"];

const particles = loadParticles(data, "0", loadersConfig);

expect(loadersConfig.every((type) => particles.hasOwnProperty(type))).toBe(
true
);
});
});

0 comments on commit fbfb0ea

Please sign in to comment.