Skip to content

Commit

Permalink
add version check + dynamic load of object
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed Jun 3, 2024
1 parent 5f48b1d commit 00bb203
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 71 deletions.
63 changes: 31 additions & 32 deletions js/types/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -23,47 +24,45 @@ 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 [];
};
}
}
}

return newLoader;
}

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

Expand Down
70 changes: 31 additions & 39 deletions js/types/reconstruction.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
43 changes: 43 additions & 0 deletions js/types/version.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 00bb203

Please sign in to comment.