Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new types of collections #25

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions js/types/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import jsonData from "../../input/p8_ee_ZH_ecm240_edm4hep.edm4hep.json" assert { type: "json" }; // node 20
import {
Cluster,
ParticleID,
ReconstructedParticle,
Vertex,
Track,
} from "./reconstruction.js";

const loaders = {
ReconstructedParticle: ReconstructedParticle.load,
ParticleID: ParticleID.load,
Vertex: Vertex.load,
Track: Track.load,
Cluster: Cluster.load,
};

const loadersConfig = [
"ReconstructedParticle",
"ParticleID",
"Vertex",
"Track",
"Cluster",
];

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 config) {
if (loaders.hasOwnProperty(particle)) {
newLoader.types[particle] = loaders[particle];
}
}

return newLoader;
}

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

const particles = {};

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

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 {
} catch (error) {
console.error(error);
}

return particles;
}

loadParticles(jsonData, 4, loadersConfig);
175 changes: 175 additions & 0 deletions js/types/reconstruction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
export class Cluster {
constructor() {
// Physics properties
this.type = 0;
this.energy = 0; // GeV
this.energyError = 0; // GeV
this.position = []; // mm
this.positionError = [];
this.iTheta = 0;
this.phi = 0;
this.directionError = {}; // mm^2
this.shapeParameters = [];
this.subdetectorEnergies = [];
this.clusters = [];
this.hits = [];
}

static load(collection) {
const particles = [];

for (const [index, particle] of collection.entries()) {
const cluster = new Cluster();
cluster.index = index;

cluster.type = particle.type;
cluster.energy = particle.energy;
cluster.energyError = particle.energyError;
cluster.position = particle.position;
cluster.positionError = particle.positionError;
cluster.iTheta = particle.iTheta;
cluster.phi = particle.phi;
cluster.directionError = particle.directionError;
cluster.shapeParameters = particle.shapeParameters;
cluster.subdetectorEnergies = particle.subdetectorEnergies;
cluster.clusters = particle.clusters;
cluster.hits = particle.hits;

particles.push(cluster);
}

return particles;
}
}

export class ParticleID {
constructor() {
// Physics properties
this.type = 0;
this.pdg = 0;
this.algorithmType = 0;
this.likelihood = 0;
this.parameters = [];
this.particle = [];
}

static load(collection) {
const particles = [];

for (const [index, particle] of collection.entries()) {
const particleID = new ParticleID();
particleID.index = index;

particleID.type = particle.type;
particleID.pdg = particle.pdg;
particleID.algorithmType = particle.algorithmType;
particleID.likelihood = particle.likelihood;
particleID.parameters = particle.parameters;

particles.push(particleID);
}

return particles;
}
}

export class ReconstructedParticle {
constructor() {
// Physics properties
this.pdg = 0;
this.energy = 0; // GeV
this.momentum = {}; // GeV
this.referencePoint = {}; // mm
this.charge = 0;
this.mass = 0; // GeV
this.goodnessOfPID = 0;
this.covMatrix = [];
this.startVertex = {};
this.clusters = [];
this.tracks = [];
this.particles = [];
}

static load(collection) {
const particles = [];

for (const [index, particle] of collection.entries()) {
const reconstructedParticle = new ReconstructedParticle();
reconstructedParticle.index = index;

reconstructedParticle.energy = particle.energy;
reconstructedParticle.momentum = particle.momentum;
reconstructedParticle.referencePoint = particle.referencePoint;
reconstructedParticle.charge = particle.charge;
reconstructedParticle.mass = particle.mass;
reconstructedParticle.goodnessOfPID = particle.goodnessOfPID;
reconstructedParticle.covMatrix = particle.covMatrix;
reconstructedParticle.startVertex = particle.startVertex;
reconstructedParticle.clusters = particle.clusters;
reconstructedParticle.tracks = particle.tracks;
reconstructedParticle.particles = particle.particles;

particles.push(reconstructedParticle);
}

return particles;
}
}

export class Vertex {
constructor() {
// Physics properties
this.primary = 0;
this.chi2 = 0;
this.probability = 0;
this.position = 0; // mm
this.covMatrix = [];
this.algorithmType = 0;
this.parameters = 0;
this.associatedParticles = [];
}

static load() {}
}

export class Track {
constructor() {
// Physics properties
this.type = 0;
this.chi2 = 0;
this.ndf = 0;
this.dEdx = 0;
this.dEdxError = 0;
this.radiusOfInnermostHit = 0;
this.subdetectorHitNumbers = [];
this.trackStates = [];
this.dxQuantities = [];
this.trackerHits = [];
this.tracks = [];
}

static load(collection) {
const particles = [];

for (const [index, particle] of collection.entries()) {
const track = new Track();
track.index = index;

track.type = particle.type;
track.chi2 = particle.chi2;
track.ndf = particle.ndf;
track.dEdx = particle.dEdx;
track.dEdxError = particle.dEdxError;
track.radiusOfInnermostHit = particle.radiusOfInnermostHit;
track.subdetectorHitNumbers = particle.subdetectorHitNumbers;
track.trackStates = particle.trackStates;
track.dxQuantities = particle.dxQuantities;
track.trackerHits = particle.trackerHits;
track.tracks = particle.tracks;

particles.push(track);
}

return particles;
}
}
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": [],
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as an FYI: this seems to be a subset collection, i.e. the actual data live in another collection and this just has "pointers" into that collection. Looking at this bit of JSON here, I think edm4hep2json does not yet handle this case properly (unless you have cut quite a bit of the original JSON).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the actual data live in another collection and this just has "pointers" into that collection.

Ohh thanks. I didn't know that.

Looking at this bit of JSON here, I think edm4hep2json does not yet handle this case properly (unless you have cut quite a bit of the original JSON).

Yeah, I think it is about edm4hep2json. I took the first collection without cutting. I will change it.

"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
);
});
});
Loading