33 #include <type_traits>
41 template<
typename NamedTuple>
67 void append(
const NamedTuple& record);
71 using Tuple =
typename NamedTuple::Tuple;
77 template<std::size_t...
I>
82 template<
typename NamedTuple>
111 bool read(NamedTuple& record);
115 using Tuple =
typename NamedTuple::Tuple;
122 template<std::size_t...
I>
128 template<
typename NamedTuple>
130 const std::string& path,
const std::string& tree_name)
131 : m_file(new TFile(path.c_str(),
"RECREATE"))
132 , m_tree(new TTree(tree_name.c_str(),
"", 99, m_file)) {
134 throw std::runtime_error(
"Could not create file");
136 if (not m_file->IsOpen()) {
137 throw std::runtime_error(
"Could not open file");
140 throw std::runtime_error(
"Could not create tree");
145 template<
typename NamedTuple>
147 TDirectory*
dir,
const std::string& tree_name)
149 , m_tree(new TTree(tree_name.c_str(),
"", 99, dir)) {
151 throw std::runtime_error(
"Invalid output directory given");
154 throw std::runtime_error(
"Could not create tree");
159 namespace namedtuple_root_impl {
162 template<
typename T,
typename Enable =
void>
180 template<
typename T,
char Un
signed,
char Signed>
184 template<
typename T, std::
size_t S>
186 and (
sizeof(
T) ==
S);
202 template<
typename NamedTuple>
203 template<std::size_t...
I>
210 std::array<std::string,
sizeof...(I)> names = NamedTuple::names();
211 std::array<std::string,
sizeof...(I)> leafs = {
213 + namedtuple_root_impl::TypeCode<
214 std::tuple_element_t<I, Tuple>>
::value)...};
220 (
void)std::array<TBranch*,
sizeof...(
I)>{m_tree->Branch(
221 names[
I].c_str(), &std::get<I>(
m_data), leafs[
I].c_str())...};
224 template<
typename NamedTuple>
228 m_tree->Write(
nullptr, TObject::kOverwrite);
237 template<
typename NamedTuple>
241 if (m_tree->Fill() == -1) {
242 throw std::runtime_error(
"Could not fill an entry");
248 template<
typename NamedTuple>
250 const std::string& path,
const std::string& tree_name)
251 : m_file(new TFile(path.c_str(),
"READ")), m_tree(nullptr), m_next(0) {
253 throw std::runtime_error(
"Could not open file");
255 if (not m_file->IsOpen()) {
256 throw std::runtime_error(
"Could not open file");
258 m_tree =
static_cast<TTree*
>(m_file->Get(tree_name.c_str()));
260 throw std::runtime_error(
"Could not read tree");
265 template<
typename NamedTuple>
267 TDirectory*
dir,
const std::string& tree_name)
272 throw std::runtime_error(
"Invalid input directory given");
274 m_tree =
static_cast<TTree*
>(dir->Get(tree_name.c_str()));
276 throw std::runtime_error(
"Could not read tree");
281 namespace io_root_impl {
288 sizeof(ULong64_t) ==
sizeof(uint64_t),
"Inconsistent type sizes");
289 return reinterpret_cast<ULong64_t*
>(&
x);
293 static_assert(
sizeof(
char) ==
sizeof(int8_t),
"Inconsistent type sizes");
294 return reinterpret_cast<char*
>(&
x);
298 static_assert(
sizeof(Long64_t) ==
sizeof(int64_t),
"Inconsistent type sizes");
299 return reinterpret_cast<Long64_t*
>(&
x);
309 template<
typename NamedTuple>
310 template<std::size_t...
I>
319 std::array<std::string,
sizeof...(I)> names = NamedTuple::names();
321 (
void)std::array<
Int_t,
sizeof...(
I)>{m_tree->SetBranchAddress(
325 template<
typename NamedTuple>
334 template<
typename NamedTuple>
337 auto ret = m_tree->GetEntry(m_next);
340 throw std::runtime_error(
"Could not read entry");