Skip to content

Commit

Permalink
check for correct object loading and links when reading a json file f…
Browse files Browse the repository at this point in the history
…or test suite
  • Loading branch information
brauliorivas committed Jun 17, 2024
1 parent 0c1538b commit c37b2f3
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 4 deletions.
2 changes: 1 addition & 1 deletion js/types/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions js/types/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ export const linkTypes = {
"daughters": DaughterLink,
"trackerHits": Link,
"startVertex": Link,
"particles": Link,
};
2 changes: 1 addition & 1 deletion js/types/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion test/dynamic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
247 changes: 247 additions & 0 deletions test/load.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
52 changes: 51 additions & 1 deletion test/load.test.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
});

0 comments on commit c37b2f3

Please sign in to comment.