File indexing completed on 2025-07-11 08:29:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_CONDITIONS_H
0014 #define DD4HEP_CONDITIONS_H
0015
0016
0017 #include <DD4hep/IOV.h>
0018 #include <DD4hep/Handle.h>
0019 #include <DD4hep/OpaqueData.h>
0020
0021
0022 #include <vector>
0023
0024
0025 namespace dd4hep {
0026
0027
0028 class BasicGrammar;
0029 class DetElement;
0030 class Detector;
0031
0032
0033 namespace detail {
0034 class ConditionObject;
0035 }
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 class Condition: public Handle<detail::ConditionObject> {
0052 public:
0053
0054 typedef unsigned long long int key_type;
0055
0056 typedef unsigned int detkey_type;
0057
0058 typedef unsigned int itemkey_type;
0059
0060 typedef unsigned int mask_type;
0061
0062 public:
0063
0064 enum StringFlags {
0065 WITH_IOV = 1<<0,
0066 WITH_ADDRESS = 1<<1,
0067 WITH_TYPE = 1<<2,
0068 WITH_COMMENT = 1<<4,
0069 WITH_DATATYPE = 1<<5,
0070 WITH_DATA = 1<<6,
0071 NO_NAME = 1<<20,
0072 NONE
0073 };
0074
0075 enum ConditionState {
0076 INACTIVE = 0,
0077 ACTIVE = 1<<0,
0078 CHECKED = 1<<2,
0079 DERIVED = 1<<3,
0080 ONSTACK = 1<<4,
0081
0082 TEMPERATURE = 1<<5,
0083 TEMPERATURE_DERIVED = 1<<6|DERIVED,
0084 PRESSURE = 1<<7,
0085 PRESSURE_DERIVED = 1<<8|DERIVED,
0086 ALIGNMENT_DELTA = 1<<9,
0087 ALIGNMENT_DERIVED = 1<<10|DERIVED,
0088
0089
0090 USER_FLAGS_FIRST = 1<<16,
0091 USER_FLAGS_LAST = 1<<31
0092 };
0093
0094 enum ConditionItemRangeKeys {
0095 FIRST_ITEM_KEY = 0x0U,
0096 LAST_ITEM_KEY = ~0x0U
0097 };
0098
0099 enum ConditionDetectorRangeKeys {
0100 FIRST_DET_KEY = 0x0U,
0101 LAST_DET_KEY = ~0x0U
0102 };
0103
0104 static constexpr unsigned long long int FIRST_KEY = 0x0ULL, LAST_KEY = ~0x0ULL;
0105
0106
0107
0108
0109
0110
0111
0112 class Processor {
0113 protected:
0114
0115 Processor(Processor&& copy) = default;
0116
0117 Processor(const Processor& copy) = default;
0118
0119 Processor& operator=(const Processor& copy) = delete;
0120 public:
0121
0122 Processor() = default;
0123
0124 virtual ~Processor() = default;
0125
0126 virtual int process(Condition c) const = 0;
0127
0128 virtual int operator()(Condition c) const
0129 { return this->process(c); }
0130
0131 virtual int operator()(const std::pair<Condition::key_type,Condition>& e) const
0132 { return this->process(e.second); }
0133 };
0134
0135
0136 Condition() = default;
0137
0138 Condition(Condition&& c) = default;
0139
0140 Condition(const Condition& c) = default;
0141
0142 Condition(Object* p);
0143
0144 template <typename Q> Condition(const Handle<Q>& e);
0145
0146 Condition(key_type hash_key);
0147
0148 Condition(const std::string& name, const std::string& type);
0149
0150 Condition(const std::string& name, const std::string& type, size_t memory);
0151
0152 Condition& operator=(Condition&& c) = default;
0153
0154 Condition& operator=(const Condition& c) = default;
0155
0156
0157 std::string str(int with_data=WITH_IOV|WITH_ADDRESS|WITH_DATATYPE) const;
0158
0159
0160
0161 int dataType() const;
0162
0163
0164
0165 const IOVType& iovType() const;
0166
0167 const IOV& iov() const;
0168
0169
0170
0171 key_type key() const;
0172
0173 detkey_type detector_key() const;
0174
0175 itemkey_type item_key() const;
0176
0177
0178 #if defined(DD4HEP_CONDITIONS_DEBUG) || !defined(DD4HEP_MINIMAL_CONDITIONS)
0179
0180 const std::string& type() const;
0181
0182 const std::string& value() const;
0183
0184 const std::string& comment() const;
0185
0186 const std::string& address() const;
0187 #endif
0188
0189 mask_type flags() const;
0190
0191 void setFlag(mask_type option);
0192
0193 void unFlag(mask_type option);
0194
0195 bool testFlag(mask_type option) const;
0196
0197
0198
0199 OpaqueDataBlock& data() const;
0200
0201 const std::type_info& typeInfo() const;
0202
0203 const BasicGrammar& descriptor() const;
0204
0205 bool is_bound() const { return isValid() ? data().is_bound() : false; }
0206
0207
0208
0209
0210
0211 template <typename T, typename... Args> T& construct(Args... args);
0212
0213
0214
0215
0216
0217 template <typename T> T& bind();
0218
0219
0220
0221
0222
0223 template <typename T> T& bind(const std::string& val);
0224
0225 template <typename T> T& get();
0226
0227 template <typename T> const T& get() const;
0228
0229 template <typename T> T& as();
0230
0231 template <typename T> const T& as() const;
0232
0233
0234 static int haveInventory(int value = -1);
0235 };
0236
0237
0238 inline Condition::Condition(Condition::Object* p)
0239 : Handle<Condition::Object>(p) {}
0240
0241
0242 template <typename Q> inline Condition::Condition(const Handle<Q>& e)
0243 : Handle<Condition::Object>(e) {}
0244
0245
0246 template <typename T, typename... Args> T& Condition::construct(Args... args) {
0247 return data().construct<T,Args...>(args...);
0248 }
0249
0250 template <typename T> inline T& Condition::bind() {
0251 return data().bind<T>();
0252 }
0253
0254 template <typename T> inline T& Condition::bind(const std::string& val) {
0255 return data().bind<T>(val);
0256 }
0257
0258 template <typename T> inline T& Condition::get() {
0259 return data().get<T>();
0260 }
0261
0262 template <typename T> inline const T& Condition::get() const {
0263 return data().get<T>();
0264 }
0265
0266 template <typename T> inline T& Condition::as() {
0267 return data().as<T>();
0268 }
0269
0270 template <typename T> inline const T& Condition::as() const {
0271 return data().as<T>();
0272 }
0273
0274
0275
0276
0277
0278
0279
0280 class ConditionKey {
0281 public:
0282 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0283
0284 std::string name;
0285 #endif
0286
0287 Condition::key_type hash = 0;
0288
0289
0290
0291
0292
0293
0294
0295 union KeyMaker {
0296 Condition::key_type hash;
0297
0298
0299
0300
0301 struct {
0302 Condition::itemkey_type item_key;
0303 Condition::detkey_type det_key;
0304 } values;
0305 KeyMaker() {
0306 this->hash = 0;
0307 }
0308 KeyMaker(Condition::key_type k) {
0309 this->hash = k;
0310 }
0311 KeyMaker(Condition::detkey_type det, Condition::itemkey_type item) {
0312 this->values.det_key = det;
0313 this->values.item_key = item;
0314 }
0315
0316 KeyMaker(Condition::detkey_type det, const std::string& value);
0317
0318 KeyMaker(DetElement detector, const std::string& value);
0319
0320 KeyMaker(DetElement detector, Condition::itemkey_type item_key);
0321 };
0322
0323
0324
0325
0326
0327
0328
0329 struct HashCompare {
0330 Condition::key_type key;
0331 HashCompare(Condition::key_type k) : key(k) {}
0332 bool operator==(const ConditionKey& k) const { return key==k.hash; }
0333 };
0334 public:
0335
0336 ConditionKey() = default;
0337
0338 ConditionKey(Condition::key_type key) : hash(key) {}
0339
0340 ConditionKey(DetElement detector, const std::string& identifier);
0341
0342 ConditionKey(Condition::detkey_type det_key, const std::string& identifier);
0343
0344 ConditionKey(DetElement detector, Condition::itemkey_type item_key);
0345
0346 ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key);
0347
0348 ConditionKey(const ConditionKey& c) = default;
0349
0350
0351 Condition::detkey_type detector_key() const {
0352 return KeyMaker(hash).values.det_key;
0353 }
0354
0355 Condition::itemkey_type item_key() const {
0356 return KeyMaker(hash).values.item_key;
0357 }
0358
0359 static Condition::key_type hashCode(DetElement detector, const char* value);
0360
0361 static Condition::key_type hashCode(DetElement detector, const std::string& value);
0362
0363 static Condition::itemkey_type itemCode(const char* value);
0364
0365 static Condition::itemkey_type itemCode(const std::string& value);
0366
0367
0368 ConditionKey& operator=(const ConditionKey& key) = default;
0369
0370 bool operator==(const ConditionKey& compare) const;
0371
0372 bool operator==(const Condition::key_type compare) const;
0373
0374
0375
0376
0377 bool operator<(const ConditionKey& compare) const;
0378
0379 bool operator<(const Condition::key_type compare) const;
0380
0381 operator Condition::key_type () const { return hash; }
0382
0383 std::string toString() const;
0384 };
0385
0386
0387 inline ConditionKey::ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key) {
0388 hash = KeyMaker(det_key,item_key).hash;
0389 }
0390
0391
0392 inline bool ConditionKey::operator==(const ConditionKey& compare) const
0393 { return hash == compare.hash; }
0394
0395
0396 inline bool ConditionKey::operator==(const Condition::key_type compare) const
0397 { return hash == compare; }
0398
0399
0400 inline bool ConditionKey::operator<(const ConditionKey& compare) const
0401 { return hash < compare.hash; }
0402
0403
0404 inline bool ConditionKey::operator<(const Condition::key_type compare) const
0405 { return hash < compare; }
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422 class ConditionsSelect {
0423 protected:
0424
0425 ConditionsSelect() = default;
0426
0427 ConditionsSelect(const ConditionsSelect& copy) = default;
0428
0429 virtual ~ConditionsSelect();
0430
0431 ConditionsSelect& operator=(const ConditionsSelect& copy) = default;
0432
0433 public:
0434
0435 bool operator()(Condition cond) const { return (*this)(cond.ptr()); }
0436
0437 bool operator()(std::pair<Condition::key_type,Condition::Object*> cond) const
0438
0439 { return (*this)(cond.second); }
0440
0441
0442 bool operator()(std::pair<Condition::key_type,Condition> cond) const
0443 { return (*this)(cond.second.ptr()); }
0444
0445
0446 virtual size_t size() const { return 0; }
0447
0448 virtual bool operator()(Condition::Object* cond) const = 0;
0449 };
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468 template <typename OBJECT> class ConditionsSelectWrapper : public ConditionsSelect {
0469 private:
0470
0471 ConditionsSelectWrapper() = delete;
0472
0473 bool operator==(const ConditionsSelectWrapper& compare) = delete;
0474
0475 public:
0476
0477 OBJECT& object;
0478
0479 public:
0480
0481 ConditionsSelectWrapper(OBJECT& o) : ConditionsSelect(), object(o) {}
0482
0483 ConditionsSelectWrapper(const ConditionsSelectWrapper& copy) = default;
0484
0485 virtual ~ConditionsSelectWrapper() = default;
0486
0487 ConditionsSelectWrapper& operator=(const ConditionsSelectWrapper& copy) = default;
0488 };
0489
0490
0491 typedef std::vector<Condition> RangeConditions;
0492
0493
0494 namespace detail {
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511 int have_condition_item_inventory(int value = -1);
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524 std::string get_condition_item_name(Condition::itemkey_type key);
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536 std::string get_condition_item_name(Condition::key_type key);
0537 }
0538
0539 }
0540 #endif