Skip to content

Commit 2e1bee6

Browse files
andiwandnjacazio
authored andcommitted
feat: Add ROOT space point I/O (acts-project#4398)
Allows to read and write space points from and to ROOT `TTree`s. For now only the most common columns are supported. In the future this can be extended for the other columns, source links and dynamic columns.
1 parent 9b63533 commit 2e1bee6

File tree

4 files changed

+228
-4
lines changed

4 files changed

+228
-4
lines changed

Core/include/Acts/EventData/SpacePointProxy2.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class SpacePointProxy2 {
107107
}
108108
/// Mutable access to the extra time information of the space point.
109109
/// @return A mutable reference to the time information of the space point.
110-
std::optional<float> &time() noexcept
110+
float &time() noexcept
111111
requires(!ReadOnly)
112112
{
113113
return m_container->time(m_index);
@@ -191,9 +191,7 @@ class SpacePointProxy2 {
191191
float phi() const noexcept { return m_container->phi(m_index); }
192192
/// Const access to the time information of the space point.
193193
/// @return An optional containing the time information of the space point, or
194-
std::optional<float> time() const noexcept {
195-
return m_container->time(m_index);
196-
}
194+
float time() const noexcept { return m_container->time(m_index); }
197195
/// Const access to the variance in Z direction of the space point.
198196
/// @return The variance in Z direction of the space point.
199197
float varianceZ() const noexcept { return m_container->varianceZ(m_index); }

Plugins/Root/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(library_sources
22
src/RootMaterialMapIo.cpp
33
src/RootMaterialTrackIo.cpp
4+
src/RootSpacePointIo.cpp
45
src/TGeoCylinderDiscSplitter.cpp
56
src/TGeoDetectorElement.cpp
67
src/TGeoLayerBuilder.cpp
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file is part of the ACTS project.
2+
//
3+
// Copyright (C) 2016 CERN for the benefit of the ACTS project
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this
7+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
#pragma once
10+
11+
#include "Acts/EventData/SpacePointContainer2.hpp"
12+
13+
class TChain;
14+
class TTree;
15+
16+
namespace Acts {
17+
18+
/// Root space point reading and writing utility
19+
class RootSpacePointIo {
20+
public:
21+
/// @brief sets the branch connection for writing to a file
22+
///
23+
/// @param ttree the TTree to write to
24+
/// @param spacePoints the space points to write
25+
void connectForWrite(TTree& ttree,
26+
const Experimental::SpacePointContainer2& spacePoints);
27+
28+
/// @brief sets the branch connection for reading from a file
29+
///
30+
/// @param tchain the TChain to read from
31+
/// @param spacePoints the space points to read into
32+
void connectForRead(TChain& tchain,
33+
const Experimental::SpacePointContainer2& spacePoints);
34+
35+
/// @brief Write a space point to the tree
36+
/// @note the caller has to do the TTree::Fill() after this call
37+
///
38+
/// @param spacePoint the space point to write
39+
void write(const Experimental::ConstSpacePointProxy2& spacePoint);
40+
41+
/// @brief Write the space points to the tree
42+
///
43+
/// @param spacePoints the space points to write
44+
/// @param ttree the TTree to write to
45+
void write(const Experimental::SpacePointContainer2& spacePoints,
46+
TTree& ttree);
47+
48+
/// @brief Read a space point from the tree
49+
/// @note the caller has to do the TChain::GetEntry() before this call
50+
///
51+
/// @param spacePoint the space point to read into
52+
void read(Experimental::MutableSpacePointProxy2& spacePoint);
53+
54+
/// @brief Read the space points from the tree
55+
///
56+
/// @param tchain the TChain to read from
57+
/// @param spacePoints the space points to read into
58+
void read(TChain& tchain, Experimental::SpacePointContainer2& spacePoints);
59+
60+
private:
61+
std::uint32_t m_index = 0;
62+
63+
float m_x = 0;
64+
float m_y = 0;
65+
float m_z = 0;
66+
67+
float m_t = 0;
68+
69+
float m_r = 0;
70+
71+
float m_varZ = 0;
72+
float m_varR = 0;
73+
};
74+
75+
} // namespace Acts
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// This file is part of the ACTS project.
2+
//
3+
// Copyright (C) 2016 CERN for the benefit of the ACTS project
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this
7+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
#include "Acts/EventData/SpacePointContainer2.hpp"
10+
#include "Acts/Plugins/Root/RootSpacePoints2Accessor.hpp"
11+
12+
#include <TChain.h>
13+
#include <TTree.h>
14+
15+
namespace Acts {
16+
17+
void RootSpacePointIo::connectForRead(
18+
TChain& tchain, const Experimental::SpacePointContainer2& spacePoints) {
19+
tchain.SetBranchAddress("index", &m_index);
20+
21+
tchain.SetBranchAddress("x", &m_x);
22+
tchain.SetBranchAddress("y", &m_y);
23+
tchain.SetBranchAddress("z", &m_z);
24+
25+
if (spacePoints.hasExtraColumns(
26+
Experimental::SpacePointKnownExtraColumn::Time)) {
27+
tchain.SetBranchAddress("t", &m_t);
28+
}
29+
30+
if (spacePoints.hasExtraColumns(
31+
Experimental::SpacePointKnownExtraColumn::R)) {
32+
tchain.SetBranchAddress("r", &m_r);
33+
}
34+
35+
if (spacePoints.hasExtraColumns(
36+
Experimental::SpacePointKnownExtraColumn::VarianceZ)) {
37+
tchain.SetBranchAddress("var_z", &m_varZ);
38+
}
39+
if (spacePoints.hasExtraColumns(
40+
Experimental::SpacePointKnownExtraColumn::VarianceR)) {
41+
tchain.SetBranchAddress("var_r", &m_varR);
42+
}
43+
}
44+
45+
void RootSpacePointIo::connectForWrite(
46+
TTree& ttree, const Experimental::SpacePointContainer2& spacePoints) {
47+
ttree.Branch("index", &m_index);
48+
49+
ttree.Branch("x", &m_x);
50+
ttree.Branch("y", &m_y);
51+
ttree.Branch("z", &m_z);
52+
53+
if (spacePoints.hasExtraColumns(
54+
Experimental::SpacePointKnownExtraColumn::Time)) {
55+
ttree.Branch("t", &m_t);
56+
}
57+
58+
if (spacePoints.hasExtraColumns(
59+
Experimental::SpacePointKnownExtraColumn::R)) {
60+
ttree.Branch("r", &m_r);
61+
}
62+
63+
if (spacePoints.hasExtraColumns(
64+
Experimental::SpacePointKnownExtraColumn::VarianceZ)) {
65+
ttree.Branch("var_z", &m_varZ);
66+
}
67+
if (spacePoints.hasExtraColumns(
68+
Experimental::SpacePointKnownExtraColumn::VarianceR)) {
69+
ttree.Branch("var_r", &m_varR);
70+
}
71+
}
72+
73+
void RootSpacePointIo::write(
74+
const Experimental::ConstSpacePointProxy2& spacePoint) {
75+
m_index = spacePoint.index();
76+
77+
m_x = spacePoint.x();
78+
m_y = spacePoint.y();
79+
m_z = spacePoint.z();
80+
81+
if (spacePoint.container().hasExtraColumns(
82+
Experimental::SpacePointKnownExtraColumn::Time)) {
83+
m_t = spacePoint.time();
84+
}
85+
86+
if (spacePoint.container().hasExtraColumns(
87+
Experimental::SpacePointKnownExtraColumn::R)) {
88+
m_r = spacePoint.r();
89+
}
90+
91+
if (spacePoint.container().hasExtraColumns(
92+
Experimental::SpacePointKnownExtraColumn::VarianceZ)) {
93+
m_varZ = spacePoint.varianceZ();
94+
}
95+
if (spacePoint.container().hasExtraColumns(
96+
Experimental::SpacePointKnownExtraColumn::VarianceR)) {
97+
m_varR = spacePoint.varianceR();
98+
}
99+
}
100+
101+
void RootSpacePointIo::write(
102+
const Experimental::SpacePointContainer2& spacePoints, TTree& ttree) {
103+
connectForWrite(ttree, spacePoints);
104+
105+
for (Experimental::ConstSpacePointProxy2 spacePoint : spacePoints) {
106+
write(spacePoint);
107+
ttree.Fill();
108+
}
109+
}
110+
111+
void RootSpacePointIo::read(Experimental::MutableSpacePointProxy2& spacePoint) {
112+
spacePoint.x() = m_x;
113+
spacePoint.y() = m_y;
114+
spacePoint.z() = m_z;
115+
116+
if (spacePoint.container().hasExtraColumns(
117+
Experimental::SpacePointKnownExtraColumn::Time)) {
118+
spacePoint.time() = m_t;
119+
}
120+
121+
if (spacePoint.container().hasExtraColumns(
122+
Experimental::SpacePointKnownExtraColumn::R)) {
123+
spacePoint.r() = m_r;
124+
}
125+
126+
if (spacePoint.container().hasExtraColumns(
127+
Experimental::SpacePointKnownExtraColumn::VarianceZ)) {
128+
spacePoint.varianceZ() = m_varZ;
129+
}
130+
if (spacePoint.container().hasExtraColumns(
131+
Experimental::SpacePointKnownExtraColumn::VarianceR)) {
132+
spacePoint.varianceR() = m_varR;
133+
}
134+
}
135+
136+
void RootSpacePointIo::read(TChain& tchain,
137+
Experimental::SpacePointContainer2& spacePoints) {
138+
connectForRead(tchain, spacePoints);
139+
140+
std::size_t nEntries = tchain.GetEntries();
141+
for (std::size_t i = 0; i < nEntries; ++i) {
142+
tchain.GetEntry(i);
143+
144+
auto spacePoint = spacePoints.createSpacePoint(
145+
std::array<SourceLink, 1>{SourceLink(i)}, 0, 0, 0);
146+
read(spacePoint);
147+
}
148+
}
149+
150+
} // namespace Acts

0 commit comments

Comments
 (0)