File indexing completed on 2025-01-30 09:16:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Detector.h>
0016 #include <XML/Conversions.h>
0017 #include <XML/XMLElements.h>
0018 #include <XML/DocumentHandler.h>
0019 #include <DD4hep/Printout.h>
0020 #include <DD4hep/DetectorTools.h>
0021 #include <DD4hep/DetFactoryHelper.h>
0022
0023 #include <DDCond/ConditionsTags.h>
0024 #include <DDCond/ConditionsEntry.h>
0025 #include <DDCond/ConditionsManager.h>
0026 #include <DDCond/ConditionsDataLoader.h>
0027
0028
0029 namespace dd4hep {
0030
0031 namespace {
0032
0033 class include;
0034 class arbitrary;
0035 class conditions;
0036
0037 class iov;
0038 class iov_type;
0039 class manager;
0040 class repository;
0041 class detelement;
0042 class align_conditions;
0043 class align_arbitrary;
0044
0045 class value;
0046 class mapping;
0047 class sequence;
0048 class alignment;
0049 class position;
0050 class rotation;
0051 class pivot;
0052 }
0053 template <> void Converter<include>::operator()(xml_h seq) const;
0054 template <> void Converter<arbitrary>::operator()(xml_h seq) const;
0055 template <> void Converter<conditions>::operator()(xml_h seq) const;
0056 }
0057
0058 using namespace dd4hep::cond;
0059
0060
0061 namespace dd4hep {
0062
0063
0064 struct ConversionArg {
0065 DetElement detector;
0066 ConditionsStack* stack;
0067 ConversionArg(DetElement det, ConditionsStack* stk)
0068 : detector(det), stack(stk)
0069 {
0070 }
0071 };
0072
0073
0074 std::string _getValidity(xml_h elt) {
0075 if ( !elt.ptr() )
0076 return "Infinite";
0077 else if ( !elt.hasAttr(_UC(validity)) )
0078 return _getValidity(elt.parent());
0079 return elt.attr<std::string>(_UC(validity));
0080 }
0081
0082
0083 DetElement _getDetector(void* param, xml_h e) {
0084 ConversionArg* arg = static_cast<ConversionArg*>(param);
0085 DetElement detector = arg ? arg->detector : DetElement();
0086 std::string subpath = e.hasAttr(_U(path)) ? e.attr<std::string>(_U(path)) : std::string();
0087 return subpath.empty() ? detector : detail::tools::findDaughterElement(detector,subpath);
0088 }
0089
0090
0091 Entry* _createStackEntry(void* param, xml_h element) {
0092 xml_comp_t e(element);
0093 DetElement elt = _getDetector(param, element);
0094 std::string name = e.hasAttr(_U(name)) ? e.nameStr() : e.tag();
0095 return new Entry(elt,name,e.tag(),_getValidity(element),detail::hash32(name));
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 template <> void Converter<arbitrary>::operator()(xml_h e) const {
0131 xml_comp_t elt(e);
0132 std::string tag = elt.tag();
0133 ConversionArg* arg = _param<ConversionArg>();
0134 if ( !arg )
0135 except("ConditionsParser","++ Invalid parser argument [Internal Error]");
0136 else if ( tag == "conditions" )
0137 Converter<conditions>(description,param,optional)(e);
0138 else if ( arg->stack && tag == "detelement" )
0139 Converter<conditions>(description,param,optional)(e);
0140 else if ( tag == "open_transaction" )
0141 return;
0142 else if ( tag == "close_transaction" )
0143 return;
0144 else if ( tag == "include" )
0145 Converter<include>(description,param,optional)(e);
0146 else if ( tag == "detelements" )
0147 xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
0148 else if ( tag == "subdetectors" )
0149 xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
0150 else if ( tag == "alignment" ) {
0151 dd4hep_ptr<Entry> val(_createStackEntry(param,e));
0152 val->value = elt.attr<std::string>(_U(ref));
0153 if ( !arg->stack )
0154 except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
0155 else
0156 arg->stack->emplace_back(val.release());
0157 }
0158 else {
0159 dd4hep_ptr<Entry> val(_createStackEntry(param,e));
0160 val->value = elt.hasAttr(_U(value)) ? elt.valueStr() : e.text();
0161 if ( !arg->stack )
0162 except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
0163 else
0164 arg->stack->emplace_back(val.release());
0165 }
0166 }
0167
0168
0169
0170
0171
0172
0173
0174 template <> void Converter<include>::operator()(xml_h element) const {
0175 xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
0176 xml_coll_t(doc.root(),_U(star)).for_each(Converter<arbitrary>(description,param,optional));
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 template <> void Converter<conditions>::operator()(xml_h e) const {
0195 ConversionArg* arg = _param<ConversionArg>();
0196 DetElement elt = arg->detector;
0197 arg->detector = _getDetector(param,e);
0198 xml_coll_t(e,_U(star)).for_each(Converter<arbitrary>(description,param,optional));
0199 arg->detector = elt;
0200 }
0201 }
0202
0203
0204
0205
0206
0207
0208
0209 static void* setup_global_Conditions(dd4hep::Detector& description, int argc, char** argv) {
0210 if ( argc == 2 ) {
0211 xml_h e = xml_h::Elt_t(argv[0]);
0212 ConditionsStack* stack = (ConditionsStack*)argv[1];
0213 dd4hep::ConversionArg args(description.world(), stack);
0214 (dd4hep::Converter<dd4hep::conditions>(description,&args))(e);
0215 return &description;
0216 }
0217 dd4hep::except("XML_DOC_READER","Invalid number of arguments to interprete conditions: %d != %d.",argc,2);
0218 return 0;
0219 }
0220 DECLARE_DD4HEP_CONSTRUCTOR(XMLConditionsParser,setup_global_Conditions)