File indexing completed on 2025-02-21 09:58:00
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 enum {
0105 FIRST_KEY = 0x0ULL,
0106 LAST_KEY = ~0x0ULL
0107 };
0108
0109
0110
0111
0112
0113
0114
0115 class Processor {
0116 protected:
0117
0118 Processor(Processor&& copy) = default;
0119
0120 Processor(const Processor& copy) = default;
0121
0122 Processor& operator=(const Processor& copy) = delete;
0123 public:
0124
0125 Processor() = default;
0126
0127 virtual ~Processor() = default;
0128
0129 virtual int process(Condition c) const = 0;
0130
0131 virtual int operator()(Condition c) const
0132 { return this->process(c); }
0133
0134 virtual int operator()(const std::pair<Condition::key_type,Condition>& e) const
0135 { return this->process(e.second); }
0136 };
0137
0138
0139 Condition() = default;
0140
0141 Condition(Condition&& c) = default;
0142
0143 Condition(const Condition& c) = default;
0144
0145 Condition(Object* p);
0146
0147 template <typename Q> Condition(const Handle<Q>& e);
0148
0149 Condition(key_type hash_key);
0150
0151 Condition(const std::string& name, const std::string& type);
0152
0153 Condition(const std::string& name, const std::string& type, size_t memory);
0154
0155 Condition& operator=(Condition&& c) = default;
0156
0157 Condition& operator=(const Condition& c) = default;
0158
0159
0160 std::string str(int with_data=WITH_IOV|WITH_ADDRESS|WITH_DATATYPE) const;
0161
0162
0163
0164 int dataType() const;
0165
0166
0167
0168 const IOVType& iovType() const;
0169
0170 const IOV& iov() const;
0171
0172
0173
0174 key_type key() const;
0175
0176 detkey_type detector_key() const;
0177
0178 itemkey_type item_key() const;
0179
0180
0181 #if defined(DD4HEP_CONDITIONS_DEBUG) || !defined(DD4HEP_MINIMAL_CONDITIONS)
0182
0183 const std::string& type() const;
0184
0185 const std::string& value() const;
0186
0187 const std::string& comment() const;
0188
0189 const std::string& address() const;
0190 #endif
0191
0192 mask_type flags() const;
0193
0194 void setFlag(mask_type option);
0195
0196 void unFlag(mask_type option);
0197
0198 bool testFlag(mask_type option) const;
0199
0200
0201
0202 OpaqueDataBlock& data() const;
0203
0204 const std::type_info& typeInfo() const;
0205
0206 const BasicGrammar& descriptor() const;
0207
0208 bool is_bound() const { return isValid() ? data().is_bound() : false; }
0209
0210
0211
0212
0213
0214 template <typename T, typename... Args> T& construct(Args... args);
0215
0216
0217
0218
0219
0220 template <typename T> T& bind();
0221
0222
0223
0224
0225
0226 template <typename T> T& bind(const std::string& val);
0227
0228 template <typename T> T& get();
0229
0230 template <typename T> const T& get() const;
0231
0232 template <typename T> T& as();
0233
0234 template <typename T> const T& as() const;
0235
0236
0237 static int haveInventory(int value = -1);
0238 };
0239
0240
0241 inline Condition::Condition(Condition::Object* p)
0242 : Handle<Condition::Object>(p) {}
0243
0244
0245 template <typename Q> inline Condition::Condition(const Handle<Q>& e)
0246 : Handle<Condition::Object>(e) {}
0247
0248
0249 template <typename T, typename... Args> T& Condition::construct(Args... args) {
0250 return data().construct<T,Args...>(args...);
0251 }
0252
0253 template <typename T> inline T& Condition::bind() {
0254 return data().bind<T>();
0255 }
0256
0257 template <typename T> inline T& Condition::bind(const std::string& val) {
0258 return data().bind<T>(val);
0259 }
0260
0261 template <typename T> inline T& Condition::get() {
0262 return data().get<T>();
0263 }
0264
0265 template <typename T> inline const T& Condition::get() const {
0266 return data().get<T>();
0267 }
0268
0269 template <typename T> inline T& Condition::as() {
0270 return data().as<T>();
0271 }
0272
0273 template <typename T> inline const T& Condition::as() const {
0274 return data().as<T>();
0275 }
0276
0277
0278
0279
0280
0281
0282
0283 class ConditionKey {
0284 public:
0285 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0286
0287 std::string name;
0288 #endif
0289
0290 Condition::key_type hash = 0;
0291
0292
0293
0294
0295
0296
0297
0298 union KeyMaker {
0299 Condition::key_type hash;
0300
0301
0302
0303
0304 struct {
0305 Condition::itemkey_type item_key;
0306 Condition::detkey_type det_key;
0307 } values;
0308 KeyMaker() {
0309 this->hash = 0;
0310 }
0311 KeyMaker(Condition::key_type k) {
0312 this->hash = k;
0313 }
0314 KeyMaker(Condition::detkey_type det, Condition::itemkey_type item) {
0315 this->values.det_key = det;
0316 this->values.item_key = item;
0317 }
0318
0319 KeyMaker(Condition::detkey_type det, const std::string& value);
0320
0321 KeyMaker(DetElement detector, const std::string& value);
0322
0323 KeyMaker(DetElement detector, Condition::itemkey_type item_key);
0324 };
0325
0326
0327
0328
0329
0330
0331
0332 struct HashCompare {
0333 Condition::key_type key;
0334 HashCompare(Condition::key_type k) : key(k) {}
0335 bool operator==(const ConditionKey& k) const { return key==k.hash; }
0336 };
0337 public:
0338
0339 ConditionKey() = default;
0340
0341 ConditionKey(Condition::key_type key) : hash(key) {}
0342
0343 ConditionKey(DetElement detector, const std::string& identifier);
0344
0345 ConditionKey(Condition::detkey_type det_key, const std::string& identifier);
0346
0347 ConditionKey(DetElement detector, Condition::itemkey_type item_key);
0348
0349 ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key);
0350
0351 ConditionKey(const ConditionKey& c) = default;
0352
0353
0354 Condition::detkey_type detector_key() const {
0355 return KeyMaker(hash).values.det_key;
0356 }
0357
0358 Condition::itemkey_type item_key() const {
0359 return KeyMaker(hash).values.item_key;
0360 }
0361
0362 static Condition::key_type hashCode(DetElement detector, const char* value);
0363
0364 static Condition::key_type hashCode(DetElement detector, const std::string& value);
0365
0366 static Condition::itemkey_type itemCode(const char* value);
0367
0368 static Condition::itemkey_type itemCode(const std::string& value);
0369
0370
0371 ConditionKey& operator=(const ConditionKey& key) = default;
0372
0373 bool operator==(const ConditionKey& compare) const;
0374
0375 bool operator==(const Condition::key_type compare) const;
0376
0377
0378
0379
0380 bool operator<(const ConditionKey& compare) const;
0381
0382 bool operator<(const Condition::key_type compare) const;
0383
0384 operator Condition::key_type () const { return hash; }
0385
0386 std::string toString() const;
0387 };
0388
0389
0390 inline ConditionKey::ConditionKey(Condition::detkey_type det_key, Condition::itemkey_type item_key) {
0391 hash = KeyMaker(det_key,item_key).hash;
0392 }
0393
0394
0395 inline bool ConditionKey::operator==(const ConditionKey& compare) const
0396 { return hash == compare.hash; }
0397
0398
0399 inline bool ConditionKey::operator==(const Condition::key_type compare) const
0400 { return hash == compare; }
0401
0402
0403 inline bool ConditionKey::operator<(const ConditionKey& compare) const
0404 { return hash < compare.hash; }
0405
0406
0407 inline bool ConditionKey::operator<(const Condition::key_type compare) const
0408 { return hash < compare; }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425 class ConditionsSelect {
0426 protected:
0427
0428 ConditionsSelect() = default;
0429
0430 ConditionsSelect(const ConditionsSelect& copy) = default;
0431
0432 virtual ~ConditionsSelect();
0433
0434 ConditionsSelect& operator=(const ConditionsSelect& copy) = default;
0435
0436 public:
0437
0438 bool operator()(Condition cond) const { return (*this)(cond.ptr()); }
0439
0440 bool operator()(std::pair<Condition::key_type,Condition::Object*> cond) const
0441
0442 { return (*this)(cond.second); }
0443
0444
0445 bool operator()(std::pair<Condition::key_type,Condition> cond) const
0446 { return (*this)(cond.second.ptr()); }
0447
0448
0449 virtual size_t size() const { return 0; }
0450
0451 virtual bool operator()(Condition::Object* cond) const = 0;
0452 };
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471 template <typename OBJECT> class ConditionsSelectWrapper : public ConditionsSelect {
0472 private:
0473
0474 ConditionsSelectWrapper() = delete;
0475
0476 bool operator==(const ConditionsSelectWrapper& compare) = delete;
0477
0478 public:
0479
0480 OBJECT& object;
0481
0482 public:
0483
0484 ConditionsSelectWrapper(OBJECT& o) : ConditionsSelect(), object(o) {}
0485
0486 ConditionsSelectWrapper(const ConditionsSelectWrapper& copy) = default;
0487
0488 virtual ~ConditionsSelectWrapper() = default;
0489
0490 ConditionsSelectWrapper& operator=(const ConditionsSelectWrapper& copy) = default;
0491 };
0492
0493
0494 typedef std::vector<Condition> RangeConditions;
0495
0496
0497 namespace detail {
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514 int have_condition_item_inventory(int value = -1);
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527 std::string get_condition_item_name(Condition::itemkey_type key);
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539 std::string get_condition_item_name(Condition::key_type key);
0540 }
0541
0542 }
0543 #endif