Skip to content

Commit 7e10c8a

Browse files
shahor02sawenzel
authored andcommitted
extended version MCTruthContainer::addToBack
Allows adding to the end of the MCTruthContainer an arbitrary range of other container
1 parent 75836f4 commit 7e10c8a

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

DataFormats/simulation/include/SimulationDataFormat/MCTruthContainer.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,28 @@ class MCTruthContainer
283283
}
284284
}
285285

286+
// merge part of another container ("n" entries starting from "from") to the back of this one
287+
void mergeAtBack(MCTruthContainer<TruthElement> const& other, size_t from, size_t n)
288+
{
289+
const auto oldtruthsize = mTruthArray.size();
290+
const auto oldheadersize = mHeaderArray.size();
291+
auto endIdx = from + n;
292+
assert(endIdx <= other.mHeaderArray.size());
293+
const auto* headBeg = &other.mHeaderArray[from];
294+
const auto* headEnd = headBeg + n;
295+
const auto* trtArrBeg = &other.mTruthArray[other.getMCTruthHeader(from).index];
296+
const auto* trtArrEnd = (endIdx == other.mHeaderArray.size()) ? (&other.mTruthArray.back()) + 1 : &other.mTruthArray[other.getMCTruthHeader(endIdx).index];
297+
298+
// copy from other
299+
std::copy(headBeg, headEnd, std::back_inserter(mHeaderArray));
300+
std::copy(trtArrBeg, trtArrEnd, std::back_inserter(mTruthArray));
301+
long offset = long(oldtruthsize) - other.getMCTruthHeader(from).index;
302+
// adjust information of newly attached part
303+
for (uint32_t i = oldheadersize; i < mHeaderArray.size(); ++i) {
304+
mHeaderArray[i].index += offset;
305+
}
306+
}
307+
286308
/// Flatten the internal arrays to the provided container
287309
/// Copies the content of the two vectors of PODs to a contiguous container.
288310
/// The flattened data starts with a specific header @ref FlatHeader describing

DataFormats/simulation/test/testMCTruthContainer.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,22 @@ BOOST_AUTO_TEST_CASE(MCTruth)
9595
container2.addElement(1, TruthElement(1));
9696
container2.addElement(2, TruthElement(10));
9797

98+
dataformats::MCTruthContainer<TruthElement> containerA;
99+
98100
container1.mergeAtBack(container2);
101+
102+
containerA.mergeAtBack(container1, 0, 2);
103+
containerA.mergeAtBack(container1, 2, 2);
104+
99105
auto lview = container1.getLabels(3); //
106+
auto lviewA = containerA.getLabels(3);
100107
BOOST_CHECK(lview.size() == 2);
101108
BOOST_CHECK(lview[0] == 11);
102109
BOOST_CHECK(lview[1] == 12);
103110
BOOST_CHECK(container1.getIndexedSize() == 6);
104111
BOOST_CHECK(container1.getNElements() == 8);
112+
BOOST_CHECK(lview.size() == lviewA.size());
113+
BOOST_CHECK(lview[0] == lviewA[0] && lview[1] == lviewA[1]);
105114
}
106115
}
107116

0 commit comments

Comments
 (0)