Skip to content

Commit

Permalink
Deploy preview for PR 76 🛫
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed Aug 15, 2024
1 parent ea69215 commit 083df26
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 105 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
114 changes: 52 additions & 62 deletions pr-preview/pr-76/js/filters/reconnect/mcparticletree.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,77 @@
import { linkTypes } from "../../types/links.js";

const findParentRow = (object, uniqueRows, rowToIndex) => {
const thisRowIndex = rowToIndex[object.row];
if (thisRowIndex > 0 && thisRowIndex < uniqueRows.length) {
return uniqueRows[thisRowIndex - 1];
const findParticles = (otherObject, relationName, ids) => {
let oneToManyRelations;
if (otherObject.relations) {
oneToManyRelations = otherObject.relations.oneToManyRelations;
} else {
oneToManyRelations = otherObject.oneToManyRelations;
}
return NaN;
};

const findDaughterRow = (object, uniqueRows, rowToIndex) => {
const thisRowIndex = rowToIndex[object.row];
if (thisRowIndex >= 0 && thisRowIndex < uniqueRows.length - 1) {
return uniqueRows[thisRowIndex + 1];
}
return NaN;
};

export function reconnectMCParticleTree(viewCurrentObjects) {
const { collection, oneToMany } =
viewCurrentObjects.datatypes["edm4hep::MCParticle"];

const sortedCollection = collection.sort((a, b) => a.row - b.row);
const relations = oneToManyRelations[relationName];

const beginRowsIndex = {};
sortedCollection.forEach((object, index) => {
if (beginRowsIndex[object.row] === undefined) {
beginRowsIndex[object.row] = index;
}
});
if (relations.length === 0) return [];

const rows = sortedCollection.map((object) => object.row);
const uniqueRows = [...new Set(rows)];
let hasAny = 0;

const rowToIndex = {};
for (const [index, row] of uniqueRows.entries()) {
rowToIndex[row] = index;
}
relations.forEach((object) =>
ids.has(`${object.index}-${object.collectionId}`) ? (hasAny += 1) : null
);

const rowToObjectsCount = {};
return hasAny > 0
? relations
: relations
.map((parentLink) => findParticles(parentLink.to, relationName, ids))
.flat();
};

sortedCollection.forEach((object) => {
if (rowToObjectsCount[object.row] === undefined) {
rowToObjectsCount[object.row] = 1;
return;
}
rowToObjectsCount[object.row] += 1;
});
export function reconnectMCParticleTree(viewCurrentObjects, ids) {
const { collection, oneToMany } =
viewCurrentObjects.datatypes["edm4hep::MCParticle"];

for (const object of sortedCollection) {
for (const object of collection) {
const { oneToManyRelations } = object;
object.saveRelations();

const parentRelations = oneToManyRelations["parents"];
const daughterRelations = oneToManyRelations["daughters"];

object.oneToManyRelations = {
"parents": [],
"daughters": [],
};

const parentRow = findParentRow(object, uniqueRows, rowToIndex);
if (parentRow !== NaN) {
const beginIndex = beginRowsIndex[parentRow];
const endIndex = beginIndex + rowToObjectsCount[parentRow];
for (const parentRelation of parentRelations) {
const parent = parentRelation.to;
const parentIndex = `${parent.index}-${parent.collectionId}`;

for (let i = beginIndex; i < endIndex; i++) {
const newParentLink = new linkTypes["parents"](
object,
sortedCollection[i]
);
object.oneToManyRelations["parents"].push(newParentLink);
oneToMany["parents"].push(newParentLink);
if (ids.has(parentIndex)) {
object.oneToManyRelations["parents"].push(parentRelation);
oneToMany["parents"].push(parentRelation);
} else {
const newParents = findParticles(parent, "parents", ids);
for (const newParent of newParents) {
const link = new linkTypes["parents"](object, newParent);
object.oneToManyRelations["parents"].push(link);
oneToMany["parents"].push(link);
}
}
}

const daughterRow = findDaughterRow(object, uniqueRows, rowToIndex);
if (daughterRow !== NaN) {
const beginIndex = beginRowsIndex[daughterRow];
const endIndex = beginIndex + rowToObjectsCount[daughterRow];
for (const daughterRelation of daughterRelations) {
const daughter = daughterRelation.to;
const daughterIndex = `${daughter.index}-${daughter.collectionId}`;

for (let i = beginIndex; i < endIndex; i++) {
const newDaughterLink = new linkTypes["daughters"](
object,
sortedCollection[i]
);
object.oneToManyRelations["daughters"].push(newDaughterLink);
oneToMany["daughters"].push(newDaughterLink);
if (ids.has(daughterIndex)) {
object.oneToManyRelations["daughters"].push(daughterRelation);
oneToMany["daughters"].push(daughterRelation);
} else {
const newDaughters = findParticles(daughter, "daughters", ids);
for (const newDaughter of newDaughters) {
const link = new linkTypes["daughters"](object, newDaughter);
object.oneToManyRelations["daughters"].push(link);
oneToMany["daughters"].push(link);
}
}
}
}
Expand Down
54 changes: 12 additions & 42 deletions pr-preview/pr-76/js/filters/reconnect/tree.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
import { datatypes } from "../../../output/datatypes.js";
import { linkTypes } from "../../types/links.js";

export function reconnectTree(viewCurrentObjects, ids) {
const tree = Object.entries(viewCurrentObjects.datatypes).filter(
([_, { collection }]) => collection.length !== 0
)[0];
const collectionName = tree[0];
const { collection: unsortedCollection } = tree[1];
const sortedCollection = unsortedCollection.sort((a, b) => a.row - b.row);
const rows = sortedCollection.map((object) => object.row);
const uniqueRows = [...new Set(rows)];
uniqueRows.sort((a, b) => a - b);

const beginRowsIndex = {};
sortedCollection.forEach((object, index) => {
if (beginRowsIndex[object.row] === undefined) {
beginRowsIndex[object.row] = index;
}
});

const rowsCount = {};
rows.forEach((row) => {
if (rowsCount[row] === undefined) {
rowsCount[row] = 1;
return;
}
rowsCount[row] += 1;
});
const { collection } = tree[1];

// Assuming al trees are oneToManyRelations
const relationName = datatypes[collectionName].oneToManyRelations.filter(
({ type }) => type === collectionName
)[0].name;
const relationClass = linkTypes[relationName];

for (const object of sortedCollection) {
for (const object of collection) {
const { oneToManyRelations } = object;
const oldRelations = oneToManyRelations[relationName];
object.saveRelations();

object.oneToManyRelations = {
[relationName]: [],
};

const objectRow = object.row;
const nextRow = objectRow + 1;

if (beginRowsIndex[nextRow] !== undefined) {
const beginIndex = beginRowsIndex[nextRow];
const count = rowsCount[nextRow];
const endIndex = beginIndex + count;

for (let i = beginIndex; i < endIndex; i++) {
const daughter = sortedCollection[i];
const daughterId = `${daughter.index}-${daughter.collectionId}`;
for (const relation of oldRelations) {
const child = relation.to;
const childIndex = `${child.index}-${child.collectionId}`;

if (ids.has(daughterId)) {
const relation = new relationClass(object, daughter);
object.oneToManyRelations[relationName].push(relation);
viewCurrentObjects.datatypes[collectionName].oneToMany[
relationName
].push(relation);
}
if (ids.has(childIndex)) {
object.oneToManyRelations[relationName].push(relation);
viewCurrentObjects.datatypes[collectionName].oneToMany[
relationName
].push(relation);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pr-preview/pr-76/js/views/templates/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function buildTree(collection, relationOfReference) {

matrix.forEach((row, i) => {
row.forEach((object, j) => {
const row = i + startingRow;
const row = i + startingRow - 1;
const col = j;
object.x = col * horizontalGap + col * boxWidth + horizontalGap;
object.y = row * verticalGap + row * boxHeight + verticalGap;
Expand Down

0 comments on commit 083df26

Please sign in to comment.