File indexing completed on 2025-01-30 09:17:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "ConditionsTest.h"
0016 #include "DD4hep/Conditions.h"
0017 #include "DD4hep/DetectorTools.h"
0018 #include "DD4hep/ConditionsDebug.h"
0019 #include "DDCond/ConditionsManager.h"
0020 #include "DDCond/ConditionsDataLoader.h"
0021 #include "DD4hep/detail/DetectorInterna.h"
0022 #include "DD4hep/detail/ConditionsInterna.h"
0023
0024
0025 #include <vector>
0026 #include <list>
0027 #include <set>
0028 #include <map>
0029 #include <deque>
0030
0031 using namespace std;
0032 using namespace dd4hep::ConditionExamples;
0033
0034
0035 namespace dd4hep {
0036
0037
0038 namespace ConditionExamples {
0039
0040 template <typename T> void print_bound_condition(Condition , const char* ) {}
0041
0042 template<typename T> void __print_bound_val(Condition c, const char* norm) {
0043 char text_format[1024];
0044 const T& value = access_val<T>(c);
0045 if ( norm ) {
0046 T val = _multiply(c.get<T>(),norm);
0047 ::snprintf(text_format,sizeof(text_format)," Bound value %%s : value:%s [%s] Type: %%s",
0048 detail::Primitive<T>::default_format(),detail::Primitive<T>::default_format());
0049 printout(INFO,"Cond_Value",text_format, c.name(), value, val, typeName(c.typeInfo()).c_str());
0050 return;
0051 }
0052 ::snprintf(text_format,sizeof(text_format)," Bound value %%s : value:%s Type: %%s",
0053 detail::Primitive<T>::default_format());
0054 printout(INFO,"Cond_Value",text_format, c.name(), value, typeName(c.typeInfo()).c_str());
0055 }
0056 template <> void __print_bound_val<string>(Condition c, const char*) {
0057 const string& v = access_val<string>(c);
0058 printout(INFO,"Cond_Value"," Bound value %s : string value:%s Type: %s Ptr:%016X",
0059 cond::cond_name(c).c_str(), c.get<string>().c_str(),typeName(c.typeInfo()).c_str(),
0060 (void*)&v);
0061 }
0062 template <typename T> void __print_bound_container(Condition c, const char*) {
0063 const T& v = access_val<T>(c);
0064 printout(INFO,"Cond_Value"," Bound value %s : size:%d = %s Type: %s Ptr:%016X",
0065 cond::cond_name(c).c_str(), int(v.size()), c.data().str().c_str(),
0066 typeName(c.typeInfo()).c_str(), (void*)&v);
0067 }
0068
0069
0070 #define TEMPLATE_SIMPLE_TYPE(x) \
0071 template <> void print_bound_value<x>(Condition c, const char* norm) \
0072 { __print_bound_val<x>(c, norm); }
0073
0074 #define TEMPLATE_CONTAINER_TYPE(container,x) \
0075 template void __print_bound_container<container >(Condition c, const char* norm); \
0076 template <> void print_bound_value<container >(Condition c, const char* norm) \
0077 { __print_bound_container<container >(c, norm); }
0078
0079 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0080 #define TEMPLATE_TYPE(x,f) \
0081 TEMPLATE_SIMPLE_TYPE(x) \
0082 TEMPLATE_CONTAINER_TYPE(vector<x>,x) \
0083 TEMPLATE_CONTAINER_TYPE(list<x>,x) \
0084 TEMPLATE_CONTAINER_TYPE(set<x>,x) \
0085 TEMPLATE_CONTAINER_TYPE(detail::Primitive<x>::int_map_t,x) \
0086 TEMPLATE_CONTAINER_TYPE(detail::Primitive<x>::size_map_t,x) \
0087 TEMPLATE_CONTAINER_TYPE(detail::Primitive<x>::string_map_t,x)
0088
0089 TEMPLATE_TYPE(char,"%c")
0090 TEMPLATE_TYPE(unsigned char,"%02X")
0091 TEMPLATE_TYPE(short,"%d")
0092 TEMPLATE_TYPE(unsigned short,"%04X")
0093 TEMPLATE_TYPE(unsigned int,"%08X")
0094 TEMPLATE_TYPE(unsigned long,"%016X")
0095 #else
0096 #define TEMPLATE_TYPE(x,f) \
0097 TEMPLATE_SIMPLE_TYPE(x) \
0098 TEMPLATE_CONTAINER_TYPE(vector<x>,x) \
0099 TEMPLATE_CONTAINER_TYPE(list<x>,x) \
0100 TEMPLATE_CONTAINER_TYPE(set<x>,x) \
0101 TEMPLATE_CONTAINER_TYPE(detail::Primitive<x>::int_map_t,x) \
0102 TEMPLATE_CONTAINER_TYPE(detail::Primitive<x>::string_map_t,x)
0103 #endif
0104
0105 TEMPLATE_TYPE(int,"%d")
0106 TEMPLATE_TYPE(long,"%ld")
0107 TEMPLATE_TYPE(float,"%f")
0108 TEMPLATE_TYPE(double,"%g")
0109 TEMPLATE_TYPE(std::string,"%c")
0110
0111 template <> void print_condition<void>(Condition c) {
0112 #if defined(DD4HEP_MINIMAL_CONDITIONS)
0113 string type = "";
0114 #else
0115 string type = c.type();
0116 #endif
0117 printout(INFO,"Cond_Value","%-32s [%16s] : %s [%s] ",
0118 cond_name(c).c_str(),
0119 #if defined(DD4HEP_MINIMAL_CONDITIONS)
0120 "", "", ""
0121 #else
0122 c.type().c_str(), c.value().c_str(), c->validity.c_str()
0123 #endif
0124 );
0125 if ( type == "alignment" )
0126 print_bound_value<string>(c);
0127 else if ( type == "temperature" )
0128 print_bound_value<double>(c);
0129 else if ( type == "pressure" )
0130 print_bound_value<double>(c,"1.0/hPa");
0131 else if ( type == "whatever" )
0132 print_bound_value<vector<double> >(c);
0133 else if ( type == "integer" )
0134 print_bound_value<int>(c);
0135 else if ( type == "long" )
0136 print_bound_value<long>(c);
0137 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0138 else if ( type == "short" )
0139 print_bound_value<short>(c);
0140 else if ( type == "unsigned short" )
0141 print_bound_value<unsigned short>(c);
0142 else if ( type == "unsigned integer" )
0143 print_bound_value<unsigned int>(c);
0144 else if ( type == "unsigned long" )
0145 print_bound_value<unsigned long>(c);
0146 #endif
0147 else if ( type == "float" )
0148 print_bound_value<float>(c);
0149 else if ( type == "double" )
0150 print_bound_value<double>(c);
0151 else if ( type == "vector<double>" )
0152 print_bound_value<vector<double> >(c);
0153 }
0154
0155 template <typename T> void print_conditions(const RangeConditions& rc) {
0156 for(Condition cond : rc)
0157 print_condition<T>(cond);
0158 }
0159
0160 template void print_conditions<void>(const RangeConditions& rc);
0161
0162
0163 void check_discrete_condition(Condition c, const IOV& iov) {
0164 const IOV* i = c->iov;
0165 if ( i->is_discrete() ) {
0166 if ( iov.contains(*i) ) {
0167 return;
0168 }
0169 except("Example", "+++ The condition %s [%s] is not fully contained in iov:%s",
0170 cond_name(c).c_str(), i->str().c_str(), iov.str().c_str());
0171 }
0172 except("Example", "+++ The condition %s [%s] has no discrete type matching iov:%s",
0173 cond_name(c).c_str(), i->str().c_str(), iov.str().c_str());
0174 }
0175 }
0176 }
0177
0178 TestEnv::TestEnv(Detector& _description, const string& detector_name)
0179 : description(_description), detector(), manager()
0180 {
0181 manager = ConditionsManager::from(description);
0182 manager["LoaderType"] = "DD4hep_Conditions_multi_Loader";
0183 manager["PoolType"] = "DD4hep_ConditionsLinearPool";
0184 manager["UpdatePoolType"] = "DD4hep_ConditionsLinearUpdatePool";
0185 manager["UserPoolType"] = "DD4hep_ConditionsLinearUserPool";
0186 manager.initialize();
0187 detector = description.detector(detector_name);
0188 if ( detector.isValid() ) {
0189 pair<bool, const IOVType*> e = manager.registerIOVType(0, "epoch");
0190 pair<bool, const IOVType*> r = manager.registerIOVType(1, "run");
0191 loader = &manager.loader();
0192 if ( e.first && r.first ) {
0193 epoch = e.second;
0194 run = r.second;
0195 return;
0196 }
0197 invalidHandleError<IOVType>();
0198 }
0199 invalidHandleError<DetElement>();
0200 }
0201
0202
0203 dd4hep::DetElement TestEnv::daughter(const string& sub_path) const {
0204 return dd4hep::detail::tools::findDaughterElement(detector,sub_path);
0205 }
0206
0207 void TestEnv::add_xml_data_source(const string& file, const string& iov_str) {
0208 string source = "xml:"+string(::getenv("DD4hepINSTALL"))+file;
0209 IOV iov(0);
0210 this->manager.fromString(iov_str, iov);
0211 this->loader->addSource(source, iov);
0212 }
0213
0214
0215 void TestEnv::dump_detector_element(DetElement elt, ConditionsMap& map)
0216 {
0217 vector<Condition> conditions;
0218 conditionsCollector(map,conditions)(elt);
0219 if ( conditions.empty() ) {
0220 printout(INFO,"conditions_tree","DetElement:%s NO CONDITIONS present",elt.path().c_str());
0221 }
0222 else {
0223 printout(INFO,"conditions","DetElement:%s # of conditons:%ld",elt.path().c_str(),conditions.size());
0224 for(const auto& c : conditions )
0225 print_condition<void>(c);
0226 }
0227 }
0228
0229
0230 void TestEnv::dump_conditions_tree(DetElement elt, ConditionsMap& map)
0231 {
0232 const DetElement::Children& children = elt.children();
0233 dump_detector_element(elt, map);
0234 for(DetElement::Children::const_iterator j=children.begin(); j!=children.end(); ++j)
0235 dump_conditions_tree((*j).second, map);
0236 }