Skip to content

Commit

Permalink
Fix issue with relations after reading and add test
Browse files Browse the repository at this point in the history
Fix an issue where it's not possible to push_back to a cloned object that has
been read because all the OneToManyRelations in the same collection
point to the same vector. The fix is to actually make a copy of the
part of the vector that belongs to that object.
  • Loading branch information
jmcarcell committed Apr 15, 2024
1 parent 428150f commit 3d73a06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/templates/Object.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

{{ utils.namespace_open(class.namespace) }}

{{ macros.constructors_destructors(class.bare_type, Members) }}
{{ macros.constructors_destructors(class.bare_type, Members, OneToManyRelations) }}

{{ class.bare_type }}::{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other): {{ class.bare_type }}(other.m_obj) {}

Expand Down
10 changes: 8 additions & 2 deletions python/templates/macros/implementations.jinja2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro constructors_destructors(type, members, prefix='') %}
{% macro constructors_destructors(type, members, relations=[], prefix='') %}
{% set full_type = prefix + type %}

{{ full_type }}::{{ full_type }}() :
Expand All @@ -19,7 +19,13 @@
}

Mutable{{ type }} {{ full_type }}::clone() const {
return Mutable{{ type }}(podio::utils::MaybeSharedPtr(new {{ type }}Obj(*m_obj), podio::utils::MarkOwned));
auto tmp = Mutable{{ type }}(podio::utils::MaybeSharedPtr(new {{ type }}Obj(*m_obj), podio::utils::MarkOwned));
{% for relation in relations %}
if (m_obj->data.{{ relation.name }}_end != m_obj->m_{{ relation.name }}->size()) {
tmp.m_obj->m_{{ relation.name }} = new std::vector<{{ relation.full_type }}>(m_obj->m_{{ relation.name }}->begin() + m_obj->data.{{ relation.name }}_begin, m_obj->m_{{ relation.name }}->begin() + m_obj->data.{{ relation.name }}_end);
}
{% endfor %}
return tmp;
}

{{ full_type }}::{{ full_type }}(podio::utils::MaybeSharedPtr<{{ type }}Obj> obj) : m_obj(std::move(obj)) {}
Expand Down
9 changes: 9 additions & 0 deletions tests/read_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ void processExtensions(const podio::Frame& event, int iEvent, podio::version::Ve
ASSERT(structs[0].y == 0, "struct value not as expected");
ASSERT(structs[1].y == iEvent, "struct value not as expected");
ASSERT(structs[2].y == 2 * iEvent, "struct value not as expected");

auto ncluster = clusters[0].clone();
ASSERT(ncluster.Hits().size() == 1, "cluster should have 1 hit");

auto hit = ExampleHit(420, {}, {}, {}, {});
ncluster.addHits(hit);
ASSERT(ncluster.Hits().size() == 2, "cluster should have 2 hits");
ASSERT(ncluster.Hits(1).cellID() == 420, "cellID should be 420");

}

void checkVecMemSubsetColl(const podio::Frame& event) {
Expand Down

0 comments on commit 3d73a06

Please sign in to comment.