DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
addVariantParameters.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 : P.Gessinger
11 //
12 //==========================================================================
13 #include "DD4hep/Detector.h"
14 #include "DD4hep/Factories.h"
15 #include "DD4hep/Printout.h"
16 #include "DD4hep/DetectorTools.h"
17 
18 #include "DDRec/DetectorData.h"
19 
20 #include <regex>
21 
22 
23 namespace dd4hep::rec {
24 
43 static long addVariantParameters(Detector& description, int argc, char** argv) {
44 
45  if(argc < 2) {
46  printout(ERROR,"ParametersPlugin","+++ Invalid arguments");
47  return 0;
48  }
49 
50  printout(INFO,"ParametersPlugin","+++ Applying %d parameters", argc);
51 
52 
53  static std::regex expr{"^(\\w+)(?:: ?(.*?)?)? ?= ?(.*)"};
54 
55  std::string detector = argv[0];
56 
57  for(int i=1;i<argc;i++) {
58  std::string kv = argv[i];
59  std::smatch m;
60  if(!std::regex_match(kv, m, expr)) {
61  throw std::runtime_error("Unable to parse key-value pair to assign");
62  }
63  if (m.size() != 4) {
64  throw std::runtime_error("Unable to parse key-value pair to assign");
65  }
66 
67  std::string key = m.str(1);
68  std::string type = m.str(2);
69  if (type == "") {
70  type = "str";
71  }
72  std::string value = m.str(3);
73 
74  printout(INFO,"ParametersPlugin","+++ %s -> %s: %s = %s", detector.c_str(), key.c_str(), type.c_str(), value.c_str());
75 
76  DetElement elt = description.detector(detector);
77  if(!elt.isValid()){
78  printout(ERROR,"ParametersPlugin","+++ Did not find element with name '%s'", detector.c_str());
79  return 0;
80  }
81 
82  VariantParameters* extension = elt.extension<VariantParameters>(false);
83  if(extension == nullptr) {
84  extension = new VariantParameters();
85  elt.addExtension<VariantParameters>(extension);
86  }
87 
88  if (type == "str" || type == "string") {
89  extension->variantParameters[key] = value;
90  }
91  else if (type == "double") {
92  extension->variantParameters[key] = dd4hep::_toDouble(value);
93  }
94  else if (type == "int") {
95  extension->variantParameters[key] = dd4hep::_toInt(value);
96  }
97  else if (type == "bool") {
98  if (value == "true") {
99  extension->variantParameters[key] = true;
100  }
101  else if (value == "false") {
102  extension->variantParameters[key] = false;
103  }
104  else {
105  throw std::runtime_error{"Invalid boolean value: " + value};
106  }
107  }
108  else {
109  throw std::runtime_error{"Unknown type '"+type+"'"};
110  }
111  }
112 
113 
114  return 1;
115 }
116 
117 }
118 
119 DECLARE_APPLY(DD4hep_ParametersPlugin, dd4hep::rec::addVariantParameters)
120 
DetectorData.h
dd4hep::rec
Namespace for the reconstruction part of the AIDA detector description toolkit.
Definition: SurfaceInstaller.h:28
Detector.h
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
dd4hep::_toInt
int _toInt(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:90
Factories.h
DetectorTools.h
dd4hep::rec::VariantParameters
StructExtension< MapStringVariantStruct > VariantParameters
Definition: DetectorData.h:552
dd4hep::_toDouble
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:116
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
Printout.h