Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 07:55:43

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