File indexing completed on 2025-01-30 09:17:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "ConditionAnyExampleObjects.h"
0016 #include "DD4hep/DD4hepUnits.h"
0017 #include "DD4hep/ConditionAny.h"
0018 #include "DD4hep/ConditionsProcessor.h"
0019
0020 using namespace std;
0021 using namespace dd4hep;
0022 using namespace dd4hep::ConditionExamples;
0023 using cond::DependencyBuilder;
0024 using cond::ConditionsLoadInfo;
0025
0026 namespace {
0027 static int num_any_ingredients = 0;
0028 template <typename T> inline void __prt(ostream& os, const vector<T>& obj) {
0029 for(const auto& o : obj) {
0030 os << o << " ";
0031 ++num_any_ingredients;
0032 }
0033 }
0034 }
0035
0036
0037 Condition ConditionAnyUpdate1::operator()(const ConditionKey& key, ConditionUpdateContext&) {
0038 #ifdef DD4HEP_CONDITIONS_DEBUG
0039 printout(printLevel,"ConditionUpdate1", "++ Building dependent condition: %016llX [%s]",key.hash, key.name.c_str());
0040 ConditionAny target(key.name, "derived", vector<int>());
0041 #else
0042 printout(printLevel,"ConditionUpdate1", "++ Building dependent condition: %016llX",key.hash);
0043 ConditionAny target(key.hash, vector<int>());
0044 #endif
0045 return target;
0046 }
0047
0048
0049 void ConditionAnyUpdate1::resolve(Condition target, ConditionUpdateContext& context) {
0050 ConditionAny tar(target);
0051 vector<int>& data = tar.as<vector<int> >();
0052 ConditionAny cond0 = context.condition(context.key(0));
0053 data.emplace_back(cond0.value<int>());
0054 data.emplace_back(cond0.value<int>()*2);
0055 }
0056
0057
0058 Condition ConditionAnyUpdate2::operator()(const ConditionKey& key, ConditionUpdateContext&) {
0059 #ifdef DD4HEP_CONDITIONS_DEBUG
0060 printout(printLevel,"ConditionUpdate2", "++ Building dependent condition: %016llX [%s]",key.hash, key.name.c_str());
0061 ConditionAny target(key.name,"derived");
0062 #else
0063 printout(printLevel,"ConditionUpdate2", "++ Building dependent condition: %016llX",key.hash);
0064 ConditionAny target(key.hash);
0065 #endif
0066 target.get() = vector<int>();
0067 return target;
0068 }
0069
0070
0071 void ConditionAnyUpdate2::resolve(Condition target, ConditionUpdateContext& context) {
0072 ConditionAny tar(target);
0073 vector<int>& data = tar.as<vector<int> >();
0074 ConditionAny cond0 = context.condition(context.key(0));
0075 ConditionAny cond1 = context.condition(context.key(1));
0076
0077 data.push_back(cond0.value<int>());
0078 data.push_back(cond0.value<int>()*2);
0079 vector<int>& c1 = cond1.as<vector<int> >();
0080 data.insert(data.end(), c1.begin(), c1.end());
0081 }
0082
0083
0084 Condition ConditionAnyUpdate3::operator()(const ConditionKey& key, ConditionUpdateContext&) {
0085 #ifdef DD4HEP_CONDITIONS_DEBUG
0086 printout(printLevel,"ConditionUpdate3", "++ Building dependent condition: %016llX [%s]",key.hash, key.name.c_str());
0087 return ConditionAny(key.name,"derived");
0088 #else
0089 printout(printLevel,"ConditionUpdate3", "++ Building dependent condition: %016llX",key.hash);
0090 return ConditionAny(key.hash);
0091 #endif
0092 }
0093
0094
0095 void ConditionAnyUpdate3::resolve(Condition target, ConditionUpdateContext& context) {
0096 vector<int> data;
0097 ConditionAny cond0 = context.condition(context.key(0));
0098 ConditionAny cond1 = context.condition(context.key(1));
0099 ConditionAny cond2 = context.condition(context.key(2));
0100
0101 data.push_back(cond0.as<int>());
0102 data.push_back(cond0.as<int>()*2);
0103 vector<int>& c1 = cond1.as<vector<int> >();
0104 data.insert(data.end(), c1.begin(), c1.end());
0105
0106 vector<int>& c2 = cond2.as<vector<int> >();
0107 data.insert(data.end(), c2.begin(), c2.end());
0108 target.get<std::any>() = std::move(data);
0109 }
0110
0111
0112 Condition ConditionAnyUpdate4::operator()(const ConditionKey& key, ConditionUpdateContext& context) {
0113 #ifdef DD4HEP_CONDITIONS_DEBUG
0114 printout(printLevel,"ConditionUpdate4", "++ Building dependent condition: %016llX [%s]",key.hash, key.name.c_str());
0115 ConditionAny target(key.name,"derived", vector<int>());
0116 #else
0117 printout(printLevel,"ConditionUpdate4", "++ Building dependent condition: %016llX",key.hash);
0118 ConditionAny target(key.hash, vector<int>());
0119 #endif
0120 vector<int>& data = target.as<std::vector<int> >();
0121 ConditionAny cond3 = context.condition(context.key(0));
0122 ConditionAny cond2 = context.condition(context.key(1));
0123 ConditionAny cond0 = context.condition(context.key(2));
0124 ConditionAny cond1 = context.condition(context.key(3));
0125
0126 data.push_back(cond0.as<int>());
0127 data.push_back(cond0.as<int>()*2);
0128 vector<int>& c1 = cond1.as<vector<int> >();
0129 data.insert(data.end(), c1.begin(), c1.end());
0130
0131 vector<int>& c2 = cond2.as<vector<int> >();
0132 data.insert(data.end(), c2.begin(), c2.end());
0133
0134 vector<int>& c3 = cond3.as<vector<int> >();
0135 data.insert(data.end(), c3.begin(), c3.end());
0136 return target;
0137 }
0138
0139
0140 ConditionsAnyDependencyCreator::ConditionsAnyDependencyCreator(ConditionsContent& c, PrintLevel p, bool persist, int ex)
0141 : OutputLevel(p), content(c), persist_conditions(persist), extended(ex)
0142 {
0143 call1 = std::shared_ptr<ConditionUpdateCall>(new ConditionAnyUpdate1(printLevel));
0144 call2 = std::shared_ptr<ConditionUpdateCall>(new ConditionAnyUpdate2(printLevel));
0145 call3 = std::shared_ptr<ConditionUpdateCall>(new ConditionAnyUpdate3(printLevel));
0146 call4 = std::shared_ptr<ConditionUpdateCall>(new ConditionAnyUpdate4(printLevel));
0147 }
0148
0149
0150 int ConditionsAnyDependencyCreator::operator()(DetElement de, int) const {
0151 ConditionKey key (de,"derived_data");
0152 ConditionKey target1(de,"derived_data/derived_1");
0153 ConditionKey target2(de,"derived_data/derived_2");
0154 ConditionKey target3(de,"derived_data/derived_3");
0155 ConditionKey target4(de,"derived_data/derived_4");
0156 DependencyBuilder build_1(de, target1.item_key(), call1);
0157 DependencyBuilder build_2(de, target2.item_key(), call2);
0158 DependencyBuilder build_3(de, target3.item_key(), call3);
0159
0160
0161 build_1.add(key);
0162
0163 build_2.add(key);
0164 build_2.add(target1);
0165
0166
0167 build_3.add(key);
0168 build_3.add(target1);
0169 build_3.add(target2);
0170
0171 if ( extended >= 1 ) {
0172 DependencyBuilder build_4(de, target4.item_key(), call4);
0173 build_4.add(target3);
0174 build_4.add(target2);
0175 build_4.add(key);
0176 build_4.add(target1);
0177 content.addDependency(build_4.release());
0178 }
0179 content.addDependency(build_1.release());
0180 content.addDependency(build_2.release());
0181 content.addDependency(build_3.release());
0182 printout(printLevel,"Example", "++ Added derived conditions dependencies for %s",de.path().c_str());
0183 return 1;
0184 }
0185
0186
0187 ConditionsAnyDataAccess::~ConditionsAnyDataAccess() {
0188 printout(ALWAYS,"Example", "+=========================================================================");
0189 printout(ALWAYS,"Example", "+ Analyzed %d any object/elements", num_any_ingredients);
0190 printout(ALWAYS,"Example", "+=========================================================================");
0191 }
0192
0193
0194 int ConditionsAnyDataAccess::operator()(DetElement de, int level) const {
0195 vector<Condition> conditions;
0196 conditionsCollector(map,conditions)(de, level);
0197 return accessConditions(de, conditions);
0198 }
0199
0200
0201 int ConditionsAnyDataAccess::accessConditions(DetElement de, const std::vector<Condition>& conditions) const {
0202 ConditionKey key_temperature (de,"temperature");
0203 ConditionKey key_pressure (de,"pressure");
0204 ConditionKey key_double_table(de,"double_table");
0205 ConditionKey key_int_table (de,"int_table");
0206 ConditionKey key_derived_data(de,"derived_data");
0207 ConditionKey key_derived1 (de,"derived_data/derived_1");
0208 ConditionKey key_derived2 (de,"derived_data/derived_2");
0209 ConditionKey key_derived3 (de,"derived_data/derived_3");
0210 ConditionKey key_derived4 (de,"derived_data/derived_4");
0211 ConditionKey key_path (de,"de_path");
0212 int result = 0, count = 0;
0213
0214
0215 for( auto condition : conditions ) {
0216 stringstream str;
0217 ConditionAny cond = condition;
0218
0219 if ( 0 == dynamic_cast<detail::ConditionObject*>(cond.ptr()) ) {
0220 printout(ERROR,"accessConditions", "Condition with bad base class!");
0221 }
0222
0223 if ( cond.item_key() == key_path.item_key() ) {
0224 result += int(cond.as<string>().length());
0225 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0226 key_temperature.toString().c_str(),
0227 typeName(typeid(cond.get())).c_str(),
0228 typeName(cond.any_type()).c_str());
0229 printout(printLevel, "accessConditions", " value: %s", cond.value<string>().c_str());
0230 ++num_any_ingredients;
0231 }
0232 else if ( cond.item_key() == key_temperature.item_key() ) {
0233 result += int(cond.as<double>());
0234 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0235 key_temperature.toString().c_str(),
0236 typeName(typeid(cond.get())).c_str(),
0237 typeName(cond.any_type()).c_str());
0238 printout(printLevel, "accessConditions", " value: %f", cond.as<double>());
0239 ++num_any_ingredients;
0240 }
0241 else if ( cond.item_key() == key_pressure.item_key() ) {
0242 result += int(cond.as<double>());
0243 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0244 key_pressure.toString().c_str(),
0245 typeName(typeid(cond.get())).c_str(),
0246 typeName(cond.any_type()).c_str());
0247 printout(printLevel, "accessConditions", " value: %f", cond.as<double>());
0248 ++num_any_ingredients;
0249 }
0250 else if ( cond.item_key() == key_double_table.item_key() ) {
0251 result += int(cond.as<vector<double> >().size());
0252 __prt(str,cond.as<vector<double> >());
0253 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0254 key_double_table.toString().c_str(),
0255 typeName(typeid(cond.get())).c_str(),
0256 typeName(cond.any_type()).c_str());
0257 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0258 ++num_any_ingredients;
0259 }
0260 else if ( cond.item_key() == key_int_table.item_key() ) {
0261 result += int(cond.as<vector<int> >().size());
0262 __prt(str,cond.as<vector<int> >());
0263 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0264 key_int_table.toString().c_str(),
0265 typeName(typeid(cond.get())).c_str(),
0266 typeName(cond.any_type()).c_str());
0267 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0268 ++num_any_ingredients;
0269 }
0270 else if ( cond.item_key() == key_derived_data.item_key() ) {
0271 result += int(cond.as<int>());
0272 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0273 key_derived_data.toString().c_str(),
0274 typeName(typeid(cond.get())).c_str(),
0275 typeName(cond.any_type()).c_str());
0276 printout(printLevel, "accessConditions", " value: %d", cond.as<int>());
0277 ++num_any_ingredients;
0278 }
0279 else if ( cond.item_key() == key_derived1.item_key() ) {
0280 result += int(cond.as<vector<int> >().size());
0281 __prt(str,cond.as<vector<int> >());
0282 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0283 key_derived1.toString().c_str(),
0284 typeName(typeid(cond.get())).c_str(),
0285 typeName(cond.any_type()).c_str());
0286 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0287 ++num_any_ingredients;
0288 }
0289 else if ( cond.item_key() == key_derived2.item_key() ) {
0290 result += int(cond.as<vector<int> >().size());
0291 __prt(str,cond.as<vector<int> >());
0292 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0293 key_derived2.toString().c_str(),
0294 typeName(typeid(cond.get())).c_str(),
0295 typeName(cond.any_type()).c_str());
0296 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0297 ++num_any_ingredients;
0298 }
0299 else if ( cond.item_key() == key_derived3.item_key() ) {
0300 result += int(cond.as<vector<int> >().size());
0301 __prt(str,cond.as<vector<int> >());
0302 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0303 key_derived3.toString().c_str(),
0304 typeName(typeid(cond.get())).c_str(),
0305 typeName(cond.any_type()).c_str());
0306 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0307 ++num_any_ingredients;
0308 }
0309 else if ( cond.item_key() == key_derived4.item_key() ) {
0310 result += int(cond.as<vector<int> >().size());
0311 __prt(str,cond.as<vector<int> >());
0312 printout(printLevel, "accessConditions", "Condition: %s type: %s [%s]",
0313 key_derived4.toString().c_str(),
0314 typeName(typeid(cond.get())).c_str(),
0315 typeName(cond.any_type()).c_str());
0316 printout(printLevel, "accessConditions", " value: %s", str.str().c_str());
0317 ++num_any_ingredients;
0318 }
0319 if ( !IOV::key_is_contained(iov.key(),cond.iov().key()) ) {
0320 printout(ERROR,"CondAccess", "++ IOV mismatch:%s <> %s",
0321 iov.str().c_str(), cond.iov().str().c_str());
0322 continue;
0323 }
0324 ++count;
0325 }
0326 return count;
0327 }
0328
0329
0330 int ConditionsAnyKeys::operator()(DetElement de, int) const {
0331 content.insertKey(ConditionKey(de,"temperature").hash);
0332 content.insertKey(ConditionKey(de,"pressure").hash);
0333 content.insertKey(ConditionKey(de,"double_table").hash);
0334 content.insertKey(ConditionKey(de,"int_table").hash);
0335 content.insertKey(ConditionKey(de,"derived_data").hash);
0336 content.insertKey(ConditionKey(de,"de_path").hash);
0337 return 1;
0338 }
0339
0340 template<typename T>
0341 Condition ConditionsAnyCreator::make_condition(DetElement de, const string& name, const T& val) const {
0342 ConditionAny cond(de.path()+"#"+name, name);
0343 cond.get() = std::make_any<T>(val);
0344 cond->hash = ConditionKey::hashCode(de,name);
0345 return cond;
0346 }
0347
0348 template<typename T, typename... Args>
0349 Condition ConditionsAnyCreator::make_condition_args(DetElement de, const string& name, Args... args) const {
0350 ConditionAny cond(de.path()+"#"+name, name);
0351 cond.get() = std::make_any<T>(std::forward<Args>(args)...);
0352 cond->hash = ConditionKey::hashCode(de,name);
0353 return cond;
0354 }
0355
0356
0357 int ConditionsAnyCreator::operator()(DetElement de, int) const {
0358 Condition temperature = make_condition<double>(de,"temperature",1.222);
0359 Condition pressure = make_condition<double>(de,"pressure",888.88);
0360 Condition derived = make_condition<int> (de,"derived_data",102030);
0361 Condition dbl_table = make_condition<vector<double> >(de,"double_table",{1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.});
0362 Condition int_table = make_condition<vector<int> > (de,"int_table",{-30,-20,-10,0,10,20,30,40,50,60,70,80,90,100,110,120});
0363 Condition path = make_condition<std::string> (de,"de_path",de.path());
0364
0365 slice.manager.registerUnlocked(pool, temperature);
0366 slice.manager.registerUnlocked(pool, pressure);
0367 slice.manager.registerUnlocked(pool, derived);
0368 slice.manager.registerUnlocked(pool, dbl_table);
0369 slice.manager.registerUnlocked(pool, int_table);
0370 slice.manager.registerUnlocked(pool, path);
0371 printout(printLevel, "Creator", "++ Adding manually conditions for %s",de.path().c_str());
0372 return 5;
0373 }