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