Skip to content

Commit

Permalink
[wip] Start restoring backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Feb 4, 2025
1 parent 8c42452 commit 2b0ec7f
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 2 deletions.
12 changes: 10 additions & 2 deletions edm4hep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ endif()
PODIO_ADD_ROOT_IO_DICT(edm4hepDict edm4hep "${headers}" src/selection.xml)
add_library(EDM4HEP::edm4hepDict ALIAS edm4hepDict )

add_library(edm4hepOldSchemas SHARED schema_evolution/src/OldLinkEvolution.cc)
add_library(edm4hepOldSchemas SHARED
schema_evolution/src/OldLinkEvolution.cc
schema_evolution/src/MCParticleEvolution.cc
)
target_include_directories(edm4hepOldSchemas PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/edm4hep/schema_evolution/include>
)
target_link_libraries(edm4hepOldSchemas PUBLIC podio::podio EDM4HEP::edm4hep)
add_library(EDM4HEP::oldSchemas ALIAS edm4hepOldSchemas)

PODIO_ADD_ROOT_IO_DICT(edm4hepOldSchemasDict edm4hepOldSchemas ${PROJECT_SOURCE_DIR}/edm4hep/schema_evolution/include/edm4hep/schema_evolution/OldLinkData.h schema_evolution/src/selection.xml)
set(evolution_headers
${PROJECT_SOURCE_DIR}/edm4hep/schema_evolution/include/edm4hep/schema_evolution/OldLinkData.h
${PROJECT_SOURCE_DIR}/edm4hep/schema_evolution/include/edm4hep/schema_evolution/OldMCParticleData.h
)

PODIO_ADD_ROOT_IO_DICT(edm4hepOldSchemasDict edm4hepOldSchemas "${evolution_headers}" schema_evolution/src/selection.xml)
add_library(EDM4HEP::oldSchemasDict ALIAS edm4hepOldSchemasDict)

list(APPEND EDM4HEP_INSTALL_LIBS edm4hep edm4hepDict edm4hepOldSchemas edm4hepOldSchemasDict)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef EDM4HEP_SCHEMA_EVOLUTION_OLDMCPARTICLEDATA_H
#define EDM4HEP_SCHEMA_EVOLUTION_OLDMCPARTICLEDATA_H

#include <edm4hep/Vector3d.h>
#include <edm4hep/Vector3f.h>

#include <cstdint>

namespace edm4hep::v2 {

struct Vector2i {
int a{};
int b{};
};

struct MCParticleData {
std::int32_t PDG{}; ///< PDG code of the particle
std::int32_t generatorStatus{}; ///< status of the particle as defined by the generator
std::int32_t simulatorStatus{}; ///< status of the particle from the simulation program - use BIT constants below
float charge{}; ///< particle charge
float time{}; ///< creation time of the particle in wrt. the event, e.g. for preassigned decays or decays in flight
///< from the simulator [ns]
double mass{}; ///< mass of the particle [GeV]
::edm4hep::Vector3d vertex{}; ///< production vertex of the particle [mm]
::edm4hep::Vector3d endpoint{}; ///< endpoint of the particle [mm]
::edm4hep::Vector3d momentum{}; ///< particle 3-momentum at the production vertex [GeV]
::edm4hep::Vector3d momentumAtEndpoint{}; ///< particle 3-momentum at the endpoint [GeV]
::edm4hep::Vector3f spin{}; ///< particle spin
Vector2i colorFlow{};

unsigned int parents_begin{};
unsigned int parents_end{};
unsigned int daughters_begin{};
unsigned int daughters_end{};
};
} // namespace edm4hep::v2

#endif // EDM4HEP_SCHEMA_EVOLUTION_OLDMCPARTICLEDATA_H
103 changes: 103 additions & 0 deletions edm4hep/schema_evolution/src/MCParticleEvolution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "edm4hep/MCParticleData.h"
#include <edm4hep/DatamodelDefinition.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/MCParticleCollectionData.h>
#include <edm4hep/schema_evolution/OldMCParticleData.h>

#include <podio/CollectionBufferFactory.h>
#include <podio/CollectionBuffers.h>
#include <podio/SchemaEvolution.h>

#include <iostream>
#include <memory>
#include <string_view>

namespace edm4hep {
namespace {
auto evolveSpinHelicity(podio::CollectionReadBuffers buffers, podio::SchemaVersionT fromVersion) {
if (fromVersion > 2) {
return buffers;
}
buffers.schemaVersion = edm4hep::meta::schemaVersion;

// We only have to evolve the data, all other members are already created
// correctly for working after schema evolution below
auto* oldData = podio::CollectionWriteBuffers::asVector<edm4hep::v2::MCParticleData>(buffers.data);
auto* newData = new std::vector<edm4hep::MCParticleData>();
newData->reserve(oldData->size());
for (const auto& oldMC : *oldData) {
newData->emplace_back(oldMC.PDG, oldMC.generatorStatus, oldMC.simulatorStatus, oldMC.charge, oldMC.time,
oldMC.mass, oldMC.vertex, oldMC.endpoint, oldMC.momentum, oldMC.momentumAtEndpoint,
int(oldMC.spin.z), oldMC.parents_begin, oldMC.parents_end, oldMC.daughters_begin,
oldMC.daughters_end);
}
delete oldData;
buffers.data = newData;

return buffers;
}

auto createOldBufferFunction(bool isSubset) {
auto readBuffers = podio::CollectionReadBuffers{};
readBuffers.type = "edm4hep::MCParticleCollection";
readBuffers.schemaVersion = 2;
readBuffers.data = isSubset ? nullptr : new std::vector<edm4hep::v2::MCParticleData>;
// The number of ObjectID vectors is either 1 or the sum of OneToMany and
// OneToOne relations
const auto nRefs = isSubset ? 1 : 2 + 0;
readBuffers.references = new podio::CollRefCollection(nRefs);
for (auto& ref : *readBuffers.references) {
// Make sure to place usable buffer pointers here
ref = std::make_unique<std::vector<podio::ObjectID>>();
}

readBuffers.vectorMembers = new podio::VectorMembersInfo();
if (!isSubset) {
readBuffers.vectorMembers->reserve(0);
}

readBuffers.createCollection = [](const podio::CollectionReadBuffers& buffers, bool isSubsetColl) {
MCParticleCollectionData data(buffers, isSubsetColl);
return std::make_unique<MCParticleCollection>(std::move(data), isSubsetColl);
};

readBuffers.recast = [](podio::CollectionReadBuffers& buffers) {
// We only have any of these buffers if this is not a subset collection
if (buffers.data) {
buffers.data = podio::CollectionWriteBuffers::asVector<edm4hep::v2::MCParticleData>(buffers.data);
}
};

readBuffers.deleteBuffers = [](podio::CollectionReadBuffers& buffers) {
if (buffers.data) {
// If we have data then we are not a subset collection and we have to
// clean up all type erased buffers by casting them back to something that
// we can delete. We have also not undergone schema evolution
delete static_cast<std::vector<edm4hep::v2::MCParticleData>*>(buffers.data);
}
delete buffers.references;
delete buffers.vectorMembers;
};

return readBuffers;
};

bool registerTransition() {
const static auto registerEvoFuncs = []() {
std::cout << "HERE I AM REGISTERING FUNCTIONS AND STUFF\n";
podio::CollectionBufferFactory::mutInstance().registerCreationFunc("edm4hep::MCParticleCollection", 2,
createOldBufferFunction);

podio::SchemaEvolution::mutInstance().registerEvolutionFunc("edm4hep::MCParticleCollection", 2,
edm4hep::meta::schemaVersion, evolveSpinHelicity,
podio::SchemaEvolution::Priority::UserDefined);
return true;
}();

return registerEvoFuncs;
}

const static auto reg_MCParticleSpinHelicityEvo = registerTransition();

} // namespace
} // namespace edm4hep
3 changes: 3 additions & 0 deletions edm4hep/schema_evolution/src/selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
<class name="edm4hep::CaloHitSimCaloHitLinkData" ClassVersion="2"/>
<class name="edm4hep::TrackerHitSimTrackerHitLinkData" ClassVersion="2"/>
<class name="edm4hep::VertexRecoParticleLinkData" ClassVersion="2"/>

<class name="edm4hep::v2::Vector2i"/>
<class name="edm4hep::v2::MCParticleData" ClassVersion="2"/>
</selection>
</lcgdict>

0 comments on commit 2b0ec7f

Please sign in to comment.