Project
Loading...
Searching...
No Matches
VariantPropertyTreeHelpers.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef FRAMEWORK_VARIANTPTREEHELPERS_H
12#define FRAMEWORK_VARIANTPTREEHELPERS_H
13
14#include "Array2D.h"
15#include <boost/property_tree/ptree.hpp>
16
17namespace o2::framework
18{
19template <typename T>
20boost::property_tree::ptree basicVectorToBranch(T* values, size_t size)
21{
22 boost::property_tree::ptree branch;
23 for (auto i = 0u; i < size; ++i) {
24 boost::property_tree::ptree leaf;
25 leaf.put("", values[i]);
26 branch.push_back(std::make_pair("", leaf));
27 }
28 return branch;
29}
30
31template <typename T>
32boost::property_tree::ptree basicVectorToBranch(std::vector<T>&& values)
33{
34 return basicVectorToBranch(values.data(), values.size());
35}
36
37template <typename T>
38boost::property_tree::ptree vectorToBranch(T const* values, size_t size)
39{
40 boost::property_tree::ptree branch;
41 branch.put_child("values", basicVectorToBranch(values, size));
42 return branch;
43}
44
45template <typename T>
46boost::property_tree::ptree vectorToBranch(std::vector<T>&& values)
47{
48 return vectorToBranch(values.data(), values.size());
49}
50
51template <typename T>
52boost::property_tree::ptree basicArray2DToBranch(Array2D<T>&& array)
53{
54 boost::property_tree::ptree subtree;
55 for (auto i = 0u; i < array.rows; ++i) {
56 boost::property_tree::ptree branch;
57 for (auto j = 0u; j < array.cols; ++j) {
58 boost::property_tree::ptree leaf;
59 leaf.put("", array(i, j));
60 branch.push_back(std::make_pair("", leaf));
61 }
62 subtree.push_back(std::make_pair("", branch));
63 }
64 return subtree;
65}
66
67template <typename T>
68boost::property_tree::ptree array2DToBranch(Array2D<T>&& array)
69{
70 boost::property_tree::ptree subtree;
71 subtree.put_child("values", basicArray2DToBranch(std::forward<Array2D<T>>(array)));
72 return subtree;
73}
74
75template <typename T>
76std::vector<T> basicVectorFromBranch(boost::property_tree::ptree const& branch)
77{
78 std::vector<T> result(branch.size());
79 auto count = 0U;
80 for (auto const& entry : branch) {
81 result[count++] = entry.second.get_value<T>();
82 }
83 return result;
84}
85
86template <typename T>
87std::vector<T> vectorFromBranch(boost::property_tree::ptree const& branch)
88{
89 return basicVectorFromBranch<T>(branch.get_child("values"));
90}
91
92template <typename T>
93Array2D<T> basicArray2DFromBranch(boost::property_tree::ptree const& branch)
94{
95 std::vector<T> cache;
96 uint32_t nrows = branch.size();
97 uint32_t ncols = 0;
98 bool first = true;
99 auto irow = 0u;
100 for (auto const& row : branch) {
101 if (first) {
102 ncols = row.second.size();
103 first = false;
104 }
105 auto icol = 0u;
106 for (auto const& entry : row.second) {
107 cache.push_back(entry.second.get_value<T>());
108 ++icol;
109 }
110 ++irow;
111 }
112 return Array2D<T>{cache, nrows, ncols};
113}
114
115template <typename T>
116Array2D<T> array2DFromBranch(boost::property_tree::ptree const& ptree)
117{
118 return basicArray2DFromBranch<T>(ptree.get_child("values"));
119}
120
121std::pair<std::vector<std::string>, std::vector<std::string>> extractLabels(boost::property_tree::ptree const& tree);
122
123template <typename T>
124LabeledArray<T> labeledArrayFromBranch(boost::property_tree::ptree const& tree)
125{
127 auto values = basicArray2DFromBranch<T>(tree.get_child("values"));
128
130}
131
132template <typename T>
133boost::property_tree::ptree labeledArrayToBranch(LabeledArray<T>&& array)
134{
135 boost::property_tree::ptree subtree;
136 subtree.put_child(labels_rows_str, basicVectorToBranch(array.getLabelsRows()));
137 subtree.put_child(labels_cols_str, basicVectorToBranch(array.getLabelsCols()));
138 subtree.put_child("values", basicArray2DToBranch(array.getData()));
139
140 return subtree;
141}
142} // namespace o2::framework
143
144extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(std::vector<float>&& values);
145extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(std::vector<int>&& values);
146extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(std::vector<double>&& values);
147extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(std::vector<std::string>&& values);
148extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(float*, size_t);
149extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(int*, size_t);
150extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(double*, size_t);
151extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(bool*, size_t);
152extern template boost::property_tree::ptree o2::framework::basicVectorToBranch(std::basic_string<char> const*, size_t);
153
154extern template boost::property_tree::ptree o2::framework::vectorToBranch(std::vector<float>&& values);
155extern template boost::property_tree::ptree o2::framework::vectorToBranch(std::vector<int>&& values);
156extern template boost::property_tree::ptree o2::framework::vectorToBranch(std::vector<double>&& values);
157extern template boost::property_tree::ptree o2::framework::vectorToBranch(std::vector<std::string>&& values);
158extern template boost::property_tree::ptree o2::framework::vectorToBranch(float const*, size_t);
159extern template boost::property_tree::ptree o2::framework::vectorToBranch(int const*, size_t);
160extern template boost::property_tree::ptree o2::framework::vectorToBranch(double const*, size_t);
161extern template boost::property_tree::ptree o2::framework::vectorToBranch(bool const*, size_t);
162extern template boost::property_tree::ptree o2::framework::vectorToBranch(std::basic_string<char> const*, size_t);
163
164extern template boost::property_tree::ptree o2::framework::labeledArrayToBranch(o2::framework::LabeledArray<float>&& array);
165extern template boost::property_tree::ptree o2::framework::labeledArrayToBranch(o2::framework::LabeledArray<int>&& array);
166extern template boost::property_tree::ptree o2::framework::labeledArrayToBranch(o2::framework::LabeledArray<double>&& array);
167extern template boost::property_tree::ptree o2::framework::labeledArrayToBranch(o2::framework::LabeledArray<std::string>&& array);
168
169extern template std::vector<float> o2::framework::basicVectorFromBranch<float>(boost::property_tree::ptree const& tree);
170extern template std::vector<int> o2::framework::basicVectorFromBranch<int>(boost::property_tree::ptree const& tree);
171extern template std::vector<std::basic_string<char>> o2::framework::basicVectorFromBranch<std::basic_string<char>>(boost::property_tree::ptree const& tree);
172extern template std::vector<double> o2::framework::basicVectorFromBranch<double>(boost::property_tree::ptree const& tree);
173
174extern template o2::framework::LabeledArray<float> o2::framework::labeledArrayFromBranch<float>(boost::property_tree::ptree const& tree);
175extern template o2::framework::LabeledArray<int> o2::framework::labeledArrayFromBranch<int>(boost::property_tree::ptree const& tree);
176extern template o2::framework::LabeledArray<std::string> o2::framework::labeledArrayFromBranch<std::string>(boost::property_tree::ptree const& tree);
177extern template o2::framework::LabeledArray<double> o2::framework::labeledArrayFromBranch<double>(boost::property_tree::ptree const& tree);
178
179extern template o2::framework::Array2D<float> o2::framework::array2DFromBranch<float>(boost::property_tree::ptree const& tree);
180extern template o2::framework::Array2D<int> o2::framework::array2DFromBranch<int>(boost::property_tree::ptree const& tree);
181extern template o2::framework::Array2D<std::string> o2::framework::array2DFromBranch<std::string>(boost::property_tree::ptree const& tree);
182extern template o2::framework::Array2D<double> o2::framework::array2DFromBranch<double>(boost::property_tree::ptree const& tree);
183
184extern template boost::property_tree::ptree o2::framework::array2DToBranch(o2::framework::Array2D<float>&& array);
185extern template boost::property_tree::ptree o2::framework::array2DToBranch(o2::framework::Array2D<int>&& array);
186extern template boost::property_tree::ptree o2::framework::array2DToBranch(o2::framework::Array2D<double>&& array);
187extern template boost::property_tree::ptree o2::framework::array2DToBranch(o2::framework::Array2D<std::string>&& array);
188#endif // FRAMEWORK_VARIANTPTREEHELPERS_H
int32_t i
uint32_t j
Definition RawData.h:0
std::vector< std::string > labels_rows
std::vector< std::string > labels_cols
GLint GLsizei count
Definition glcorearb.h:399
GLuint64EXT * result
Definition glcorearb.h:5662
GLsizeiptr size
Definition glcorearb.h:659
GLenum array
Definition glcorearb.h:4274
GLint first
Definition glcorearb.h:399
GLenum GLsizei GLsizei GLint * values
Definition glcorearb.h:1576
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
LabeledArray< T > labeledArrayFromBranch(boost::property_tree::ptree const &tree)
Array2D< T > basicArray2DFromBranch(boost::property_tree::ptree const &branch)
boost::property_tree::ptree labeledArrayToBranch(LabeledArray< T > &&array)
boost::property_tree::ptree basicVectorToBranch(T *values, size_t size)
boost::property_tree::ptree basicArray2DToBranch(Array2D< T > &&array)
std::pair< std::vector< std::string >, std::vector< std::string > > extractLabels(boost::property_tree::ptree const &tree)
Definition Variant.cxx:267
boost::property_tree::ptree array2DToBranch(Array2D< T > &&array)
Array2D< T > array2DFromBranch(boost::property_tree::ptree const &ptree)
std::vector< T > vectorFromBranch(boost::property_tree::ptree const &branch)
std::vector< T > basicVectorFromBranch(boost::property_tree::ptree const &branch)
boost::property_tree::ptree vectorToBranch(T const *values, size_t size)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::vector< int > row