diff --git a/js/types/load.js b/js/types/load.js index 2e27311f..0122e42c 100644 --- a/js/types/load.js +++ b/js/types/load.js @@ -6,13 +6,14 @@ import { Vertex, Track, } from "./reconstruction.js"; +import { compatible } from "./version.js"; -const loaders = { - ReconstructedParticle: ReconstructedParticle.load, - ParticleID: ParticleID.load, - Vertex: Vertex.load, - Track: Track.load, - Cluster: Cluster.load, +const types = { + "Cluster": Cluster, + "ParticleID": ParticleID, + "ReconstructedParticle": ReconstructedParticle, + "Vertex": Vertex, + "Track": Track, }; const loadersConfig = [ @@ -23,23 +24,22 @@ const loadersConfig = [ "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; - }, - }; +export function buildLoader(config, version) { + const newLoader = {}; + + if (typeof config === "string") config = [config]; for (const particle of config) { - if (loaders.hasOwnProperty(particle)) { - newLoader.types[particle] = loaders[particle]; + if (types.hasOwnProperty(particle)) { + const isCompatible = compatible(types[particle], version); + + if (isCompatible) { + newLoader[particle] = types[particle].load; + } else { + newLoader[particle] = () => { + return []; + }; + } } } @@ -47,23 +47,22 @@ export function buildLoader(config) { } export function loadParticles(jsonData, event, loadersConfig) { - const eventData = Object.values(jsonData["Event " + event]); + const eventData = jsonData["Event " + event]; + const version = eventData.edm4hepVersion; + delete eventData["edm4hepVersion"]; - const particles = {}; - - if (typeof loadersConfig === "string") loadersConfig = [loadersConfig]; - const loader = buildLoader(loadersConfig); + const loader = buildLoader(loadersConfig, version); - Object.keys(loader.getAllLoaders()).forEach((key) => (particles[key] = [])); + const particles = {}; + Object.keys(loader).forEach((key) => (particles[key] = [])); - for (const [type, loadFunction] of Object.entries(loader.getAllLoaders())) { - const particlesType = eventData.filter( + for (const [type, loadFunction] of Object.entries(loader)) { + const particlesType = Object.values(eventData).filter( (element) => element.collType === `edm4hep::${type}Collection` ); - particlesType.forEach((collection) => { - const loadedParticles = loadFunction(collection.collection); - particles[type] = loadedParticles; + particlesType.forEach(({ collection }) => { + const loadedParticles = loadFunction(collection); }); } diff --git a/js/types/reconstruction.js b/js/types/reconstruction.js index f072440f..d40d5321 100644 --- a/js/types/reconstruction.js +++ b/js/types/reconstruction.js @@ -1,4 +1,19 @@ +function dynamicLoad(object, data, ignore = null) { + if (ignore !== null) { + for (const key of ignore) { + delete data[key]; + } + } + + for (const [key, value] of Object.entries(data)) { + object[key] = value; + } +} + export class Cluster { + static MIN_VERSION = "0.7.0"; // may vary per type of particle + static MAX_VERSION = "1.0.0"; + constructor() { // Physics properties this.type = 0; @@ -22,18 +37,7 @@ export class Cluster { 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; + dynamicLoad(cluster, particle); particles.push(cluster); } @@ -43,6 +47,9 @@ export class Cluster { } export class ParticleID { + static MIN_VERSION = "0.7.0"; + static MAX_VERSION = "1.0.0"; + constructor() { // Physics properties this.type = 0; @@ -60,11 +67,7 @@ export class ParticleID { 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; + dynamicLoad(particleID, particle); particles.push(particleID); } @@ -74,6 +77,9 @@ export class ParticleID { } export class ReconstructedParticle { + static MIN_VERSION = "0.7.0"; + static MAX_VERSION = "1.0.0"; + constructor() { // Physics properties this.pdg = 0; @@ -97,17 +103,7 @@ export class ReconstructedParticle { 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; + dynamicLoad(reconstructedParticle, particle); particles.push(reconstructedParticle); } @@ -117,6 +113,9 @@ export class ReconstructedParticle { } export class Vertex { + static MIN_VERSION = "0.7.0"; + static MAX_VERSION = "1.0.0"; + constructor() { // Physics properties this.primary = 0; @@ -133,6 +132,9 @@ export class Vertex { } export class Track { + static MIN_VERSION = "0.7.0"; + static MAX_VERSION = "1.0.0"; + constructor() { // Physics properties this.type = 0; @@ -155,17 +157,7 @@ export class Track { 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; + dynamicLoad(track, particle); particles.push(track); } diff --git a/js/types/version.js b/js/types/version.js new file mode 100644 index 00000000..dd710ca4 --- /dev/null +++ b/js/types/version.js @@ -0,0 +1,43 @@ +export class Version { + constructor(string) { + const [major, minor, patch] = string.split("."); + this.major = parseInt(major); + this.minor = parseInt(minor); + this.patch = parseInt(patch); + } + + toString() { + return `${this.major}.${this.minor}.${this.patch}`; + } + + greaterOrEqualThan(version) { + return ( + this.major >= version.major || + (this.major === version.major && + (this.minor >= version.minor || + (this.minor === version.minor && this.patch >= version.patch))) + ); + } + + lessOrEqualThan(version) { + return ( + this.major <= version.major || + (this.major === version.major && + (this.minor <= version.minor || + (this.minor === version.minor && this.patch <= version.patch))) + ); + } + + isBetween(version1, version2) { + return this.greaterOrEqualThan(version1) && this.lessOrEqualThan(version2); + } +} + +export function compatible(type, version) { + const minV = new Version(type.MIN_VERSION); + const maxV = new Version(type.MAX_VERSION); + + const v = new Version(version); + + return v.isBetween(minV, maxV); +}