DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
Evaluators.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 #include "Parsers/config.h"
15 
16 // Framework include files
17 #if defined(DD4HEP_PARSER_HEADER)
18 
19 #define DD4HEP_NEED_EVALUATOR
20 // This is the case, if the parsers are externalized
21 // and the dd4hep namespace is renamed!
22 #include DD4HEP_PARSER_HEADER
23 
24 #else
25 
26 // Standard dd4hep parser handling
27 #include "Parsers/spirit/ToStream.h"
28 
29 #endif
30 #include "Evaluator/Evaluator.h"
31 
32 // C/C++ include files
33 #include <sstream>
34 #include <iostream>
35 #include <stdexcept>
36 
37 namespace dd4hep {
38  const dd4hep::tools::Evaluator& g4Evaluator();
39 }
40 namespace {
41  const dd4hep::tools::Evaluator& eval(dd4hep::g4Evaluator());
42 }
43 
44 //==============================================================================
45 namespace dd4hep { namespace Parsers {
46  template <typename T> T evaluate_string(const std::string& /* value */) {
47  throw "Bad undefined call";
48  }
49 
50  template <> double evaluate_string<double>(const std::string& value) {
51  std::stringstream err;
52  auto result = eval.evaluate(value, err);
53  if (result.first != tools::Evaluator::OK) {
54  throw std::runtime_error("dd4hep::Properties: Severe error during expression evaluation of " +
55  value + " : " + err.str());
56  }
57  return result.second;
58  }
59  template <> float evaluate_string<float>(const std::string& value) {
60  std::stringstream err;
61  auto result = eval.evaluate(value, err);
62  if (result.first != tools::Evaluator::OK) {
63  throw std::runtime_error("dd4hep::Properties: Severe error during expression evaluation of " +
64  value + " : " + err.str());
65  }
66  return (float) result.second;
67  }
68  }
69 }
dd4hep::g4Evaluator
const dd4hep::tools::Evaluator & g4Evaluator()
Access to G4 evaluator. Note: Uses Geant4 units!
Definition: ExpressionEvaluator.cpp:68
dd4hep::Parsers::evaluate_string
T evaluate_string(const std::string &)
Definition: Evaluators.cpp:46
dd4hep::detail::eval
Definition: RootDictionary.h:84
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Parsers::evaluate_string< double >
double evaluate_string< double >(const std::string &value)
Definition: Evaluators.cpp:50
dd4hep::Parsers::evaluate_string< float >
float evaluate_string< float >(const std::string &value)
Definition: Evaluators.cpp:59