File indexing completed on 2025-01-30 09:17:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "DD4hep/Detector.h"
0014 #include "DD4hep/Factories.h"
0015 #include "DD4hep/Printout.h"
0016 #include "DD4hep/DetectorTools.h"
0017
0018 #include "DDRec/DetectorData.h"
0019
0020 #include <regex>
0021
0022
0023 namespace dd4hep::rec {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 static long addVariantParameters(Detector& description, int argc, char** argv) {
0044
0045 if(argc < 2) {
0046 printout(ERROR,"ParametersPlugin","+++ Invalid arguments");
0047 return 0;
0048 }
0049
0050 printout(INFO,"ParametersPlugin","+++ Applying %d parameters", argc);
0051
0052
0053 static std::regex expr{"^(\\w+)(?:: ?(.*?)?)? ?= ?(.*)"};
0054
0055 std::string detector = argv[0];
0056
0057 for(int i=1;i<argc;i++) {
0058 std::string kv = argv[i];
0059 std::smatch m;
0060 if(!std::regex_match(kv, m, expr)) {
0061 throw std::runtime_error("Unable to parse key-value pair to assign");
0062 }
0063 if (m.size() != 4) {
0064 throw std::runtime_error("Unable to parse key-value pair to assign");
0065 }
0066
0067 std::string key = m.str(1);
0068 std::string type = m.str(2);
0069 if (type == "") {
0070 type = "str";
0071 }
0072 std::string value = m.str(3);
0073
0074 printout(INFO,"ParametersPlugin","+++ %s -> %s: %s = %s", detector.c_str(), key.c_str(), type.c_str(), value.c_str());
0075
0076 DetElement elt = description.detector(detector);
0077 if(!elt.isValid()){
0078 printout(ERROR,"ParametersPlugin","+++ Did not find element with name '%s'", detector.c_str());
0079 return 0;
0080 }
0081
0082 VariantParameters* extension = elt.extension<VariantParameters>(false);
0083 if(extension == nullptr) {
0084 extension = new VariantParameters();
0085 elt.addExtension<VariantParameters>(extension);
0086 }
0087
0088 if (type == "str" || type == "string") {
0089 extension->variantParameters[key] = value;
0090 }
0091 else if (type == "double") {
0092 extension->variantParameters[key] = dd4hep::_toDouble(value);
0093 }
0094 else if (type == "int") {
0095 extension->variantParameters[key] = dd4hep::_toInt(value);
0096 }
0097 else if (type == "bool") {
0098 if (value == "true") {
0099 extension->variantParameters[key] = true;
0100 }
0101 else if (value == "false") {
0102 extension->variantParameters[key] = false;
0103 }
0104 else {
0105 throw std::runtime_error{"Invalid boolean value: " + value};
0106 }
0107 }
0108 else {
0109 throw std::runtime_error{"Unknown type '"+type+"'"};
0110 }
0111 }
0112
0113
0114 return 1;
0115 }
0116
0117 }
0118
0119 DECLARE_APPLY(DD4hep_ParametersPlugin, dd4hep::rec::addVariantParameters)
0120