Skip to content

Commit bee4172

Browse files
committed
New addElements interface for MCTruthContainer
As per suggestion of David Rohr. Use new interface in TOF digitizer
1 parent 02f9c19 commit bee4172

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

DataFormats/simulation/include/SimulationDataFormat/MCTruthContainer.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cassert>
2020
#include <stdexcept>
2121
#include <gsl/gsl> // for guideline support library; array_view
22+
#include <type_traits>
2223

2324
namespace o2
2425
{
@@ -115,6 +116,28 @@ class MCTruthContainer
115116
mTruthArray.emplace_back(element);
116117
}
117118

119+
// convenience interface to add multiple labels at once
120+
// can use elements of any assignable type or sub-type
121+
template <typename CompatibleLabel>
122+
void addElements(uint dataindex, gsl::span<CompatibleLabel> elements)
123+
{
124+
static_assert(std::is_same<TruthElement, CompatibleLabel>::value ||
125+
std::is_assignable<TruthElement, CompatibleLabel>::value ||
126+
std::is_base_of<TruthElement, CompatibleLabel>::value,
127+
"Need to add compatible labels");
128+
for (auto& e : elements) {
129+
addElement(dataindex, e);
130+
}
131+
}
132+
133+
template <typename CompatibleLabel>
134+
void addElements(uint dataindex, const std::vector<CompatibleLabel>& v)
135+
{
136+
using B = typename std::remove_const<CompatibleLabel>::type;
137+
auto s = gsl::span<CompatibleLabel>(const_cast<B*>(&v[0]), v.size());
138+
addElements(dataindex, s);
139+
}
140+
118141
// Add element at last position or for a previous index
119142
// (at random access position).
120143
// This might be a slow process since data has to be moved internally

DataFormats/simulation/test/testMCTruthContainer.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ BOOST_AUTO_TEST_CASE(MCTruth)
6868
view = copy.getLabels(2);
6969
BOOST_CHECK(view.size() == 1);
7070
BOOST_CHECK(view[0] == 10);
71+
72+
// add multiple labels
73+
std::vector<TruthElement> newlabels = { 101, 102, 103 };
74+
container.addElements(2, newlabels);
75+
view = container.getLabels(2);
76+
BOOST_CHECK(view.size() == 4);
77+
BOOST_CHECK(view[0] == 10);
78+
BOOST_CHECK(view[1] == 101);
79+
BOOST_CHECK(view[2] == 102);
80+
BOOST_CHECK(view[3] == 103);
7181
}
7282

7383
BOOST_AUTO_TEST_CASE(MCTruth_RandomAccess)

Detectors/TOF/simulation/src/DigitizerTask.cxx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ void DigitizerTask::Exec(Option_t* option)
9292
// copy from transientTruthContainer to mMCTruthAray
9393
// a brute force solution for the moment; should be handled by a dedicated API
9494
for (int index = 0; index < transientTruthContainer.getIndexedSize(); ++index) {
95-
auto labels = transientTruthContainer.getLabels(index);
96-
for (auto& l : labels) {
97-
mMCTruthArray->addElement(index, l);
98-
}
95+
mMCTruthArray->addElements(index, transientTruthContainer.getLabels(index));
9996
}
10097
}
10198

0 commit comments

Comments
 (0)