File indexing completed on 2025-01-18 09:13:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/detail/ConditionsInterna.h>
0017
0018
0019 #include <climits>
0020 #include <iomanip>
0021 #include <cstdio>
0022
0023 using namespace dd4hep;
0024
0025 namespace {
0026 int s_have_inventory = 0;
0027 struct KeyTracer {
0028 std::map<Condition::itemkey_type,std::string> item_names;
0029 std::mutex lock;
0030 void add(Condition::itemkey_type key,const std::string& item) {
0031 if ( s_have_inventory > 0 ) {
0032 std::lock_guard<std::mutex> protect(lock);
0033 item_names.emplace(key, item);
0034 }
0035 }
0036 std::string get(Condition::itemkey_type key) const {
0037 auto i = item_names.find(key);
0038 if( i != item_names.end() ) {
0039 return (*i).second;
0040 }
0041 char text[32];
0042 std::snprintf(text,sizeof(text),"%08X",key);
0043 return text;
0044 }
0045 } s_key_tracer;
0046 }
0047
0048
0049 int dd4hep::detail::have_condition_item_inventory(int value) {
0050 if ( value >= 0 ) {
0051 s_have_inventory = value;
0052 }
0053 return s_have_inventory;
0054 }
0055
0056 std::string dd4hep::detail::get_condition_item_name(Condition::key_type key) {
0057 return s_key_tracer.get(ConditionKey::KeyMaker(key).values.item_key);
0058 }
0059
0060 std::string dd4hep::detail::get_condition_item_name(Condition::itemkey_type key) {
0061 return s_key_tracer.get(key);
0062 }
0063
0064
0065 Condition::Condition(key_type hash_key) : Handle<Object>()
0066 {
0067 if ( hash_key != 0 && hash_key != ~0x0ULL ) {
0068 Object* o = new Object();
0069 assign(o,"","");
0070 o->hash = hash_key;
0071 return;
0072 }
0073 except("Condition","+++ Cannot create a condition with an invalid key: %016llX",hash_key);
0074 }
0075
0076
0077 Condition::Condition(const std::string& nam, const std::string& typ) : Handle<Object>()
0078 {
0079 Object* o = new Object();
0080 assign(o,nam,typ);
0081 o->hash = 0;
0082 }
0083
0084
0085 Condition::Condition(const std::string& nam,const std::string& typ, size_t memory)
0086 : Handle<Object>()
0087 {
0088 void* ptr = ::operator new(sizeof(Object)+memory);
0089 Object* o = new(ptr) Object();
0090 assign(o,nam,typ);
0091 o->hash = 0;
0092 }
0093
0094
0095 std::string Condition::str(int flags) const {
0096 std::stringstream output;
0097 Object* o = access();
0098 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0099 if ( 0 == (flags&NO_NAME) )
0100 output << std::setw(16) << std::left << o->name;
0101 #endif
0102 if ( flags&WITH_IOV ) {
0103 const IOV* ptr_iov = o->iovData();
0104 output << " " << (ptr_iov ? ptr_iov->str().c_str() : "IOV:[UNKNOWN]");
0105 }
0106 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0107 if ( flags&WITH_TYPE )
0108 output << " (" << o->type << ")";
0109 #endif
0110 if ( flags&WITH_DATATYPE )
0111 output << " -> " << o->data.dataType();
0112 if ( flags&WITH_DATA )
0113 output << " Data:" << o->data.str();
0114 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0115 if ( flags&WITH_ADDRESS )
0116 output << " " << o->address;
0117 if ( flags&WITH_COMMENT )
0118 output << " \"" << o->comment << "\"";
0119 #endif
0120 return output.str();
0121 }
0122
0123
0124 int Condition::dataType() const {
0125 return access()->data.type;
0126 }
0127
0128
0129 dd4hep::OpaqueDataBlock& Condition::data() const {
0130 return access()->data;
0131 }
0132
0133
0134 const dd4hep::IOVType& Condition::iovType() const {
0135 return *(access()->iovType());
0136 }
0137
0138
0139 const dd4hep::IOV& Condition::iov() const {
0140 return *(access()->iovData());
0141 }
0142
0143 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0144
0145 const std::string& Condition::type() const {
0146 return access()->type;
0147 }
0148
0149
0150 const std::string& Condition::value() const {
0151 return access()->value;
0152 }
0153
0154
0155 const std::string& Condition::comment() const {
0156 return access()->comment;
0157 }
0158
0159
0160 const std::string& Condition::address() const {
0161 return access()->address;
0162 }
0163 #endif
0164
0165
0166 const std::type_info& Condition::typeInfo() const {
0167 return descriptor().type();
0168 }
0169
0170
0171 Condition::key_type Condition::key() const {
0172 return access()->hash;
0173 }
0174
0175
0176 Condition::detkey_type Condition::detector_key() const {
0177 return ConditionKey::KeyMaker(access()->hash).values.det_key;
0178 }
0179
0180
0181 Condition::itemkey_type Condition::item_key() const {
0182 return ConditionKey::KeyMaker(access()->hash).values.item_key;
0183 }
0184
0185
0186 Condition::mask_type Condition::flags() const {
0187 return access()->flags;
0188 }
0189
0190
0191 void Condition::setFlag(mask_type option) {
0192 access()->setFlag(option);
0193 }
0194
0195
0196 void Condition::unFlag(mask_type option) {
0197 access()->unFlag(option);
0198 }
0199
0200
0201 bool Condition::testFlag(mask_type option) const {
0202 return access()->testFlag(option);
0203 }
0204
0205
0206 const dd4hep::BasicGrammar& Condition::descriptor() const {
0207 const BasicGrammar* grammar = access()->data.grammar;
0208 if ( !grammar ) {
0209 invalidHandleError<Condition>();
0210
0211
0212 throw std::runtime_error("Null pointer in Grammar object");
0213 }
0214 return *grammar;
0215 }
0216
0217
0218 ConditionsSelect::~ConditionsSelect() {
0219 }
0220
0221
0222 ConditionKey::KeyMaker::KeyMaker(DetElement detector, const std::string& value) {
0223 KeyMaker key_maker(detector.key(), detail::hash32(value));
0224 hash = key_maker.hash;
0225 s_key_tracer.add(key_maker.values.item_key, value);
0226 }
0227
0228
0229 ConditionKey::KeyMaker::KeyMaker(DetElement detector, Condition::itemkey_type item_key) {
0230 hash = KeyMaker(detector.key(), item_key).hash;
0231 }
0232
0233
0234 ConditionKey::KeyMaker::KeyMaker(Condition::detkey_type det_key, const std::string& value) {
0235 KeyMaker key_maker(det_key, detail::hash32(value));
0236 hash = key_maker.hash;
0237 s_key_tracer.add(key_maker.values.item_key, value);
0238 }
0239
0240
0241 ConditionKey::ConditionKey(DetElement detector, const std::string& value) {
0242 KeyMaker key_maker(detector.key(), value);
0243 hash = key_maker.hash;
0244 s_key_tracer.add(key_maker.values.item_key, value);
0245 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0246 name = detector.path()+"#"+value;
0247 #endif
0248 }
0249
0250
0251 ConditionKey::ConditionKey(Condition::detkey_type det_key, const std::string& value) {
0252 KeyMaker key_maker(det_key, value);
0253 s_key_tracer.add(key_maker.values.item_key, value);
0254 hash = key_maker.hash;
0255 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0256 char text[32];
0257 ::snprintf(text,sizeof(text),"%08X#",det_key);
0258 name = text+value;
0259 #endif
0260 }
0261
0262
0263 ConditionKey::ConditionKey(DetElement detector, Condition::itemkey_type item_key) {
0264 hash = KeyMaker(detector.key(),item_key).hash;
0265 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0266 char text[32];
0267 ::snprintf(text,sizeof(text),"#%08X",item_key);
0268 name = detector.path()+text;
0269 #endif
0270 }
0271
0272
0273 Condition::key_type ConditionKey::hashCode(DetElement detector, const char* value) {
0274 KeyMaker key_maker(detector.key(), value);
0275 s_key_tracer.add(key_maker.values.item_key, value);
0276 return key_maker.hash;
0277 }
0278
0279
0280 Condition::key_type ConditionKey::hashCode(DetElement detector, const std::string& value) {
0281 KeyMaker key_maker(detector.key(), value);
0282 s_key_tracer.add(key_maker.values.item_key, value);
0283 return key_maker.hash;
0284 }
0285
0286
0287 Condition::itemkey_type ConditionKey::itemCode(const char* value) {
0288 Condition::itemkey_type code = detail::hash32(value);
0289 s_key_tracer.add(code, value);
0290 return code;
0291 }
0292
0293
0294 Condition::itemkey_type ConditionKey::itemCode(const std::string& value) {
0295 Condition::itemkey_type code = detail::hash32(value);
0296 s_key_tracer.add(code, value);
0297 return code;
0298 }
0299
0300
0301 std::string ConditionKey::toString() const {
0302 dd4hep::ConditionKey::KeyMaker key(hash);
0303 char text[64];
0304 ::snprintf(text,sizeof(text),"%08X-%08X",key.values.det_key, key.values.item_key);
0305 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0306 if ( !name.empty() ) {
0307 std::stringstream str;
0308 str << "(" << name << ") " << text;
0309 return str.str();
0310 }
0311 #endif
0312 return text;
0313 }
0314