PandaRoot
JSONReadersHelper.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
20 #pragma once
21 
22 namespace pt = boost::property_tree;
23 
24 #include <FairLogger.h>
25 #include <stdexcept>
26 #include <string>
27 
28 namespace JSONReaders {
29 inline std::pair<double, double> readPair(pt::ptree &tree)
30 {
31  if (tree.size() != 2) {
32  throw std::range_error("Expected two values e.g min/max not " + std::to_string(tree.size()));
33  // LOG(error) << "Expected two values eg. min/max not " << tree.size();
34  }
35  auto it = tree.begin();
36  double min = it->second.get_value<double>();
37  it++;
38  double max = it->second.get_value<double>();
39  return std::make_pair(min, max);
40 }
41 
42 inline std::pair<std::string, std::string> readStringPair(pt::ptree &tree)
43 {
44  if (tree.size() != 2) {
45  throw std::range_error("Expected two values e.g min/max not " + std::to_string(tree.size()));
46  // LOG(error) << "Expected two values eg. min/max not " << tree.size();
47  }
48  auto it = tree.begin();
49  std::string first = it->second.data();
50  it++;
51  std::string second = it->second.data();
52  return std::make_pair(first, second);
53 }
54 
55 inline std::array<double, 3> readTriplet(pt::ptree &tree)
56 {
57  if (tree.size() != 3) {
58  throw std::range_error("Expected three values e.g x/y/z not " + std::to_string(tree.size()));
59  // LOG(error) << << tree.size();
60  }
61  std::array<double, 3> xyz;
62  int index = 0;
63  for (auto val : tree) {
64  xyz[index++] = val.second.get_value<double>();
65  }
66  return xyz;
67 }
68 
69 inline std::vector<double> readVector(pt::ptree &tree)
70 {
71  std::vector<double> result;
72  for (auto val : tree) {
73  result.push_back(val.second.get_value<double>());
74  }
75  return result;
76 }
77 } // namespace JSONReaders
std::pair< double, double > readPair(pt::ptree &tree)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:37
std::pair< std::string, std::string > readStringPair(pt::ptree &tree)
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:36
collection of helper methods to load in pairs, triplets or vectors of doubles from JSON files ...
std::array< double, 3 > readTriplet(pt::ptree &tree)
std::vector< double > readVector(pt::ptree &tree)