From c37b2f388faaa4db9b30018b3e698277be4c6845 Mon Sep 17 00:00:00 2001 From: Braulio Rivas Abad Date: Mon, 17 Jun 2024 17:35:59 -0500 Subject: [PATCH] check for correct object loading and links when reading a json file for test suite --- js/types/dynamic.js | 2 +- js/types/links.js | 1 + js/types/load.js | 2 +- test/dynamic.test.js | 2 +- test/load.json | 247 +++++++++++++++++++++++++++++++++++++++++++ test/load.test.js | 52 ++++++++- 6 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 test/load.json diff --git a/js/types/dynamic.js b/js/types/dynamic.js index eb722a61..fa250270 100644 --- a/js/types/dynamic.js +++ b/js/types/dynamic.js @@ -25,7 +25,7 @@ export function loadOneToOneRelations( const linkType = linkTypes[name]; const link = new linkType(object, toObject); - oneToOne[name] = link; + oneToOne[name].push(link); object.oneToOneRelations[name] = link; } } diff --git a/js/types/links.js b/js/types/links.js index c95fc73c..5f2914d1 100644 --- a/js/types/links.js +++ b/js/types/links.js @@ -133,4 +133,5 @@ export const linkTypes = { "daughters": DaughterLink, "trackerHits": Link, "startVertex": Link, + "particles": Link, }; diff --git a/js/types/load.js b/js/types/load.js index ea7340b9..1623a67d 100644 --- a/js/types/load.js +++ b/js/types/load.js @@ -12,7 +12,7 @@ export function loadObjectType(collection, datatype, type) { let oneToOne = {}; if (datatype.oneToOneRelations) datatype.oneToOneRelations.forEach((relation) => { - oneToOne[relation.name] = null; + oneToOne[relation.name] = []; if (colors[relation.name] === undefined) { colors[relation.name] = generateRandomColor(); } diff --git a/test/dynamic.test.js b/test/dynamic.test.js index 0fed4546..10c2d7dd 100644 --- a/test/dynamic.test.js +++ b/test/dynamic.test.js @@ -167,7 +167,7 @@ test("load one to one relations with some definition and data", () => { }, ]; const oneToOne = { - "startVertex": null, + "startVertex": [], }; loadOneToOneRelations(object, data, oneToOneRelations, oneToOne, []); expect(object.oneToOneRelations).not.toBeNull(); diff --git a/test/load.json b/test/load.json new file mode 100644 index 00000000..daa5c4c7 --- /dev/null +++ b/test/load.json @@ -0,0 +1,247 @@ +{ + "Event 0": {}, + "Event 1": { + "Particle": { + "collID": 11, + "collType": "edm4hep::MCParticleCollection", + "collection": [ + { + "PDG": 11, + "charge": -1.0, + "colorFlow": { + "a": 0, + "b": 0 + }, + "daughters": [ + { + "collectionID": 11, + "index": 2 + } + ], + "endpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "generatorStatus": 4, + "mass": 0.0005109999910928309, + "momentum": { + "x": 0.0, + "y": 0.0, + "z": 120.0 + }, + "momentumAtEndpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "parents": [], + "simulatorStatus": 0, + "spin": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "time": 0.0, + "vertex": { + "x": -0.01184066478163004, + "y": -2.074451003863942e-06, + "z": -0.08278788626194 + } + }, + { + "PDG": -11, + "charge": 1.0, + "colorFlow": { + "a": 0, + "b": 0 + }, + "daughters": [], + "endpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "generatorStatus": 4, + "mass": 0.0005109999910928309, + "momentum": { + "x": 0.0, + "y": 0.0, + "z": -120.0 + }, + "momentumAtEndpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "parents": [], + "simulatorStatus": 0, + "spin": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "time": 0.0, + "vertex": { + "x": -0.01184066478163004, + "y": -2.074451003863942e-06, + "z": -0.08278788626194 + } + }, + { + "PDG": 11, + "charge": -1.0, + "colorFlow": { + "a": 0, + "b": 0 + }, + "daughters": [], + "endpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "generatorStatus": 21, + "mass": 0.0, + "momentum": { + "x": 0.0, + "y": 0.0, + "z": 120.0 + }, + "momentumAtEndpoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "parents": [ + { + "collectionID": 11, + "index": 0 + } + ], + "simulatorStatus": 0, + "spin": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "time": 0.0, + "vertex": { + "x": -0.01184066478163004, + "y": -2.074451003863942e-06, + "z": -0.08278788626194 + } + } + ] + }, + "AllMuon": { + "collID": 12, + "collType": "edm4hep::ReconstructedParticleCollection", + "collection": [] + }, + "Jet": { + "collID": 13, + "collType": "edm4hep::ReconstructedParticleCollection", + "collection": [ + { + "charge": 1.0, + "clusters": [], + "covMatrix": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "energy": 12.062528610229492, + "goodnessOfPID": 0.0, + "mass": 2.315242290496826, + "momentum": { + "x": 11.738886833190918, + "y": 1.2114704847335815, + "z": 0.9354811906814575 + }, + "particleIDUsed": { + "collectionID": -2, + "index": -2 + }, + "particleIDs": [ + { + "collectionID": 4, + "index": 45 + } + ], + "particles": [ + { + "collectionID": 14, + "index": 1 + } + ], + "referencePoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "startVertex": { + "collectionID": -2, + "index": -2 + }, + "tracks": [], + "type": 0 + }, + { + "charge": 1.0, + "clusters": [], + "covMatrix": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "energy": 7.181974411010742, + "goodnessOfPID": 0.0, + "mass": 2.346627712249756, + "momentum": { + "x": 1.1801066398620605, + "y": -4.268080234527588, + "z": -5.144407749176025 + }, + "particleIDUsed": { + "collectionID": -2, + "index": -2 + }, + "particleIDs": [ + { + "collectionID": 4, + "index": 46 + } + ], + "particles": [], + "referencePoint": { + "x": 0.0, + "y": 0.0, + "z": 0.0 + }, + "startVertex": { + "collectionID": -2, + "index": -2 + }, + "tracks": [], + "type": 0 + } + ] + } + }, + "Event 2": {} +} \ No newline at end of file diff --git a/test/load.test.js b/test/load.test.js index a795c391..c9e813c4 100644 --- a/test/load.test.js +++ b/test/load.test.js @@ -1,6 +1,7 @@ -import { loadObjectType } from "../js/types/load.js"; +import { loadObjectType, loadObjects } from "../js/types/load.js"; import { datatypes } from "../output/datatypes.js"; import { objectTypes } from "../js/types/objects.js"; +import json from "./load.json" assert { type: "json" }; test("load a collection of particles", () => { const type = "edm4hep::Track"; @@ -171,3 +172,52 @@ test("load a collection of particles", () => { expect(trackerHits.length).toEqual(6); expect(tracks.length).toEqual(0); }); + +test("load a json file with a collection of objects", () => { + const objects = loadObjects(json, 1, [ + "edm4hep::MCParticle", + "edm4hep::ReconstructedParticle", + ]); + + expect(objects["edm4hep::MCParticle"]).toBeDefined(); + expect(objects["edm4hep::ReconstructedParticle"]).toBeDefined(); + + expect(objects["edm4hep::MCParticle"].collection.length).toEqual(3); + expect( + objects["edm4hep::MCParticle"].collection.map((val) => val.id) + ).toEqual([0, 1, 2]); + expect(objects["edm4hep::MCParticle"].oneToMany["daughters"]).toBeDefined(); + expect(objects["edm4hep::MCParticle"].oneToMany["parents"]).toBeDefined(); + expect( + objects["edm4hep::MCParticle"].oneToMany["daughters"][0].from.id + ).toEqual(0); + expect( + objects["edm4hep::MCParticle"].oneToMany["daughters"][0].to.id + ).toEqual(2); + + expect(objects["edm4hep::ReconstructedParticle"].collection.length).toEqual( + 2 + ); + expect( + objects["edm4hep::ReconstructedParticle"].collection.map((val) => val.id) + ).toEqual([0, 1]); + expect( + objects["edm4hep::ReconstructedParticle"].oneToMany["particles"] + ).toBeDefined(); + expect( + objects["edm4hep::ReconstructedParticle"].oneToOne["startVertex"] + ).toBeDefined(); + expect( + objects["edm4hep::ReconstructedParticle"].oneToMany["particles"][0].from.id + ).toEqual(0); + expect( + objects["edm4hep::ReconstructedParticle"].oneToMany["particles"][0].to.id + ).toEqual(1); + + expect( + objects["edm4hep::ReconstructedParticle"].oneToOne["startVertex"][0].to + ).toBeUndefined(); + expect( + objects["edm4hep::ReconstructedParticle"].oneToOne["startVertex"][1].to + ).toBeUndefined(); +});