Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-26 08:07:20

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 "ConditionExampleObjects.h"
0016 #include "DD4hep/DD4hepUnits.h"
0017 #include "DD4hep/ConditionsProcessor.h"
0018 
0019 using namespace std;
0020 using namespace dd4hep;
0021 using namespace dd4hep::ConditionExamples;
0022 using cond::DependencyBuilder;
0023 using cond::ConditionsLoadInfo;
0024 
0025 /// Install the conditions and the alignment manager
0026 ConditionsManager dd4hep::ConditionExamples::installManager(Detector& description)  {
0027   // Now we instantiate the conditions manager
0028   description.apply("DD4hep_ConditionsManagerInstaller",0,(char**)0);
0029   ConditionsManager manager = ConditionsManager::from(description);
0030   manager["PoolType"]       = "DD4hep_ConditionsLinearPool";
0031   manager["UserPoolType"]   = "DD4hep_ConditionsMapUserPool";
0032   manager["UpdatePoolType"] = "DD4hep_ConditionsLinearUpdatePool";
0033   manager.initialize();
0034   return manager;
0035 }
0036 
0037 /// Interface to client Callback in order to update the condition
0038 Condition ConditionNonDefaultCtorUpdate1::operator()(const ConditionKey& key, ConditionUpdateContext&)  {
0039 #ifdef DD4HEP_CONDITIONS_DEBUG
0040   printout(printLevel,"ConditionNonDefaultCtor1","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0041   Condition    target(key.name,"derived");
0042 #else
0043   printout(printLevel,"ConditionNonDefaultCtor1","++ Building dependent condition: %016llX",key.hash);
0044   Condition    target(key.hash);
0045 #endif
0046   target.construct<NonDefaultCtorCond>(1,2,3);
0047   return target;
0048 }
0049 
0050 /// Interface to client Callback in order to update the condition
0051 void ConditionNonDefaultCtorUpdate1::resolve(Condition target, ConditionUpdateContext& context)  {
0052   NonDefaultCtorCond& data  = target.get<NonDefaultCtorCond>();
0053   Condition    cond0 = context.condition(context.key(0));
0054   data.set(cond0.get<int>());
0055 }
0056 
0057 /// Interface to client Callback in order to update the condition
0058 Condition ConditionUpdateUnresolved::operator()(const ConditionKey& key, ConditionUpdateContext&)  {
0059 #ifdef DD4HEP_CONDITIONS_DEBUG
0060   printout(printLevel,"ConditionUpdateUnresolved","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0061   Condition    target(key.name,"derived");
0062 #else
0063   printout(printLevel,"ConditionUpdateUnresolved","++ Building dependent condition: %016llX",key.hash);
0064   Condition    target(key.hash);
0065 #endif
0066   target.construct<vector<int> >();
0067   return target;
0068 }
0069 
0070 /// Interface to client Callback in order to update the condition
0071 void ConditionUpdateUnresolved::resolve(Condition target, ConditionUpdateContext& context)  {
0072   vector<int>& data  = target.get<vector<int> >();
0073   Condition    cond0 = context.condition(context.key(0));
0074   data.emplace_back(cond0.get<int>());
0075   data.emplace_back(cond0.get<int>()*2);
0076 }
0077 
0078 /// Interface to client Callback in order to update the condition
0079 Condition ConditionUpdate1::operator()(const ConditionKey& key, ConditionUpdateContext&)  {
0080 #ifdef DD4HEP_CONDITIONS_DEBUG
0081   printout(printLevel,"ConditionUpdate1","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0082   Condition    target(key.name,"derived");
0083 #else
0084   printout(printLevel,"ConditionUpdate1","++ Building dependent condition: %016llX",key.hash);
0085   Condition    target(key.hash);
0086 #endif
0087   target.construct<vector<int> >();
0088   return target;
0089 }
0090 
0091 /// Interface to client Callback in order to update the condition
0092 void ConditionUpdate1::resolve(Condition target, ConditionUpdateContext& context)  {
0093   vector<int>& data  = target.get<vector<int> >();
0094   Condition    cond0 = context.condition(context.key(0));
0095   data.emplace_back(cond0.get<int>());
0096   data.emplace_back(cond0.get<int>()*2);
0097 }
0098 
0099 /// Interface to client Callback in order to update the condition
0100 Condition ConditionUpdate2::operator()(const ConditionKey& key, ConditionUpdateContext&)  {
0101 #ifdef DD4HEP_CONDITIONS_DEBUG
0102   printout(printLevel,"ConditionUpdate2","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0103   Condition    target(key.name,"derived");
0104 #else
0105   printout(printLevel,"ConditionUpdate2","++ Building dependent condition: %016llX",key.hash);
0106   Condition    target(key.hash);
0107 #endif
0108   target.construct<vector<int> >();
0109   return target;
0110 }
0111 
0112 /// Interface to client Callback in order to update the condition
0113 void ConditionUpdate2::resolve(Condition target, ConditionUpdateContext& context)  {
0114   vector<int>& data  = target.get<vector<int> >();
0115   Condition    cond0 = context.condition(context.key(0));
0116   Condition    cond1 = context.condition(context.key(1));
0117 
0118   data.push_back(cond0.get<int>());
0119   data.push_back(cond0.get<int>()*2);
0120   vector<int>& c1 = cond1.get<vector<int> >();
0121   data.insert(data.end(), c1.begin(), c1.end());
0122 }
0123 
0124 /// Interface to client Callback in order to update the condition
0125 Condition ConditionUpdate3::operator()(const ConditionKey& key, ConditionUpdateContext&)  {
0126 #ifdef DD4HEP_CONDITIONS_DEBUG
0127   printout(printLevel,"ConditionUpdate3","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0128   Condition    target(key.name,"derived");
0129 #else
0130   printout(printLevel,"ConditionUpdate3","++ Building dependent condition: %016llX",key.hash);
0131   Condition    target(key.hash);
0132 #endif
0133   target.bind<vector<int> >();
0134   return target;
0135 }
0136 
0137 /// Interface to client Callback in order to update the condition
0138 void ConditionUpdate3::resolve(Condition target, ConditionUpdateContext& context)  {
0139   vector<int>& data  = target.get<vector<int> >();
0140   Condition    cond0 = context.condition(context.key(0));
0141   Condition    cond1 = context.condition(context.key(1));
0142   Condition    cond2 = context.condition(context.key(2));
0143 
0144   data.push_back(cond0.get<int>());
0145   data.push_back(cond0.get<int>()*2);
0146   vector<int>& c1 = cond1.get<vector<int> >();
0147   data.insert(data.end(), c1.begin(), c1.end());
0148 
0149   vector<int>& c2 = cond2.get<vector<int> >();
0150   data.insert(data.end(), c2.begin(), c2.end());
0151 }
0152 
0153 /// Interface to client Callback in order to update the condition
0154 Condition ConditionUpdate4::operator()(const ConditionKey& key, ConditionUpdateContext& context)  {
0155 #ifdef DD4HEP_CONDITIONS_DEBUG
0156   printout(printLevel,"ConditionUpdate4","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0157   Condition    target(key.name,"derived");
0158 #else
0159   printout(printLevel,"ConditionUpdate4","++ Building dependent condition: %016llX",key.hash);
0160   Condition    target(key.hash);
0161 #endif
0162   vector<int>& data  = target.bind<vector<int> >();
0163   Condition    cond3 = context.condition(context.key(0));
0164   Condition    cond2 = context.condition(context.key(1));
0165   Condition    cond0 = context.condition(context.key(2));
0166   Condition    cond1 = context.condition(context.key(3));
0167 
0168   data.push_back(cond0.get<int>());
0169   data.push_back(cond0.get<int>()*2);
0170   vector<int>& c1 = cond1.get<vector<int> >();
0171   data.insert(data.end(), c1.begin(), c1.end());
0172 
0173   vector<int>& c2 = cond2.get<vector<int> >();
0174   data.insert(data.end(), c2.begin(), c2.end());
0175 
0176   vector<int>& c3 = cond3.get<vector<int> >();
0177   data.insert(data.end(), c3.begin(), c3.end());
0178   return target;
0179 }
0180 
0181 /// Interface to client Callback in order to update the condition
0182 Condition ConditionUpdate5::operator()(const ConditionKey& key, ConditionUpdateContext& context)  {
0183 #ifdef DD4HEP_CONDITIONS_DEBUG
0184   printout(printLevel,"ConditionUpdate5","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0185   Condition    target(key.name,"derived");
0186 #else
0187   printout(printLevel,"ConditionUpdate5","++ Building dependent condition: %016llX",key.hash);
0188   Condition    target(key.hash);
0189 #endif
0190   vector<int>& data  = target.bind<vector<int> >();
0191   Condition    cond3 = context.condition(context.key(0));
0192   Condition    cond2 = context.condition(context.key(1));
0193   Condition    cond0 = context.condition(context.key(2));
0194   Condition    cond1 = context.condition(context.key(3));
0195   Condition    cond4 = context.condition(context.key(4));
0196 
0197   data.push_back(cond0.get<int>());
0198   data.push_back(cond0.get<int>()*2);
0199   vector<int>& c1 = cond1.get<vector<int> >();
0200   data.insert(data.end(), c1.begin(), c1.end());
0201 
0202   vector<int>& c2 = cond2.get<vector<int> >();
0203   data.insert(data.end(), c2.begin(), c2.end());
0204 
0205   vector<int>& c3 = cond3.get<vector<int> >();
0206   data.insert(data.end(), c3.begin(), c3.end());
0207 
0208   vector<int>& c4 = cond4.get<vector<int> >();
0209   data.insert(data.end(), c4.begin(), c4.end());
0210   return target;
0211 }
0212 
0213 /// Interface to client Callback in order to update the condition
0214 Condition ConditionUpdate6::operator()(const ConditionKey& key, ConditionUpdateContext& context)  {
0215 #ifdef DD4HEP_CONDITIONS_DEBUG
0216   printout(printLevel,"ConditionUpdate6","++ Building dependent condition: %016llX  [%s]",key.hash, key.name.c_str());
0217   Condition    target(key.name,"derived");
0218 #else
0219   printout(printLevel,"ConditionUpdate6","++ Building dependent condition: %016llX",key.hash);
0220   Condition    target(key.hash);
0221 #endif
0222   auto* dep = context.dependency;
0223   vector<Condition>& data = target.bind<vector<Condition> >();
0224   data.reserve(dep->dependencies.size());
0225   for ( size_t i=0; i<dep->dependencies.size(); ++i )   {
0226     Condition c = context.condition(context.key(i));
0227     if ( c.get<string>().empty() )  {
0228       printout(printLevel,"ConditionUpdate6","++ Invalid dependent condition in: %016llX",key.hash);      
0229     }
0230     data.emplace_back(c);
0231   }
0232   max_deps = std::max(data.size(), max_deps);
0233   min_deps = std::min(data.size(), min_deps);
0234   num_deps += data.size();
0235   ++call_count;
0236   return target;
0237 }
0238 namespace {
0239   size_t num_depath_checks = 0;
0240   size_t num_depath_entries = 0;
0241 }
0242 /// Default destructor
0243 ConditionUpdate6::~ConditionUpdate6()   {
0244   printout(ALWAYS,"Statistics","+++ Variable dependencies:  MIN: %ld MAX: %ld MEAN:%.3f COUNT:%d",
0245            min_deps, max_deps, double(num_deps)/double(std::max(call_count,1UL)), call_count);
0246       /// Statistics
0247   printout(ALWAYS,"Statistics","+++ DePath: Entries: %ld  Checks:%ld",num_depath_entries,num_depath_checks);
0248 }
0249 
0250 /// Initializing constructor
0251 ConditionsDependencyCreator::ConditionsDependencyCreator(ConditionsContent& c, PrintLevel p, bool persist, int ex)
0252   : OutputLevel(p), content(c), persist_conditions(persist), extended(ex)
0253 {
0254   scall1 = std::shared_ptr<ConditionUpdateCall>(new ConditionNonDefaultCtorUpdate1(printLevel));
0255   call1  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate1(printLevel));
0256   call2  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate2(printLevel));
0257   call3  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate3(printLevel));
0258   call4  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate4(printLevel));
0259   call5  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate5(printLevel));
0260   call6  = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate6(printLevel));
0261   callUnresolved = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdateUnresolved(printLevel));
0262 }
0263 
0264 /// Callback to process a single detector element
0265 int ConditionsDependencyCreator::operator()(DetElement de, int)  const  {
0266   ConditionKey      key     (de,"derived_data");
0267   ConditionKey      key_path(de,"derived_data/de_path");
0268   ConditionKey      starget1(de,"derived_data/NonDefaultCtor_1");
0269   ConditionKey      target1(de,"derived_data/derived_1");
0270   ConditionKey      target2(de,"derived_data/derived_2");
0271   ConditionKey      target3(de,"derived_data/derived_3");
0272   ConditionKey      target4(de,"derived_data/derived_4");
0273   ConditionKey      target5(de,"derived_data/derived_5");
0274   DependencyBuilder sbuild_1(de, starget1.item_key(), scall1);
0275   DependencyBuilder build_1(de, target1.item_key(), call1);
0276   DependencyBuilder build_2(de, target2.item_key(), call2);
0277   DependencyBuilder build_3(de, target3.item_key(), call3);
0278 
0279   // Compute the derived stuff
0280   sbuild_1.add(key);
0281   build_1.add(key);
0282 
0283   build_2.add(key);
0284   build_2.add(target1);
0285   
0286   build_3.add(key);
0287   build_3.add(target1);
0288   build_3.add(target2);
0289 
0290   if ( extended >= 1 )   {
0291     DependencyBuilder build_4(de, target4.item_key(), call4);
0292     build_4.add(target3);
0293     build_4.add(target2);
0294     build_4.add(key);
0295     build_4.add(target1);
0296     content.addDependency(build_4.release());
0297   }
0298   if ( extended >= 2 )   {
0299     DependencyBuilder build_5(de, target5.item_key(), call5);
0300     build_5.add(target3);
0301     build_5.add(target2);
0302     build_5.add(key);
0303     build_5.add(target1);
0304     build_5.add(target4);
0305     content.addDependency(build_5.release());
0306   }
0307   if ( extended >= 3 )   {
0308     DependencyBuilder build_6(de, key_path.item_key(), call6);
0309     if ( de.parent().isValid() )
0310       build_6.add(ConditionKey(de.parent(),"de_path"));
0311     for(const auto& c : de.children())   {
0312       build_6.add(ConditionKey(c.second,"de_path"));
0313     }
0314     content.addDependency(build_6.release());
0315   }
0316   if ( extended >= 4 )   {
0317     DependencyBuilder build_7(de, "Unresolved_dependency", callUnresolved);
0318     build_7.add(ConditionKey(de.parent(),"unresolved_data"));
0319     content.addDependency(build_7.release());
0320   }
0321   if ( !persist_conditions )  {
0322     content.addDependency(sbuild_1.release());
0323   }
0324   content.addDependency(build_1.release());
0325   content.addDependency(build_2.release());
0326   content.addDependency(build_3.release());
0327   printout(printLevel,"Example","++ Added derived conditions dependencies for %s",de.path().c_str());
0328   return 1;
0329 }
0330 
0331 /// Callback to process a single detector element
0332 int ConditionsDataAccess::operator()(DetElement de, int level)  const  {
0333   vector<Condition> conditions;
0334   conditionsCollector(map,conditions)(de, level);
0335   return accessConditions(de, conditions);
0336 }
0337 
0338 /// Common call to access selected conditions
0339 int ConditionsDataAccess::accessConditions(DetElement de, const std::vector<Condition>& conditions)  const  {
0340   ConditionKey key_path        (de,"de_path");
0341   ConditionKey key_temperature (de,"temperature");
0342   ConditionKey key_pressure    (de,"pressure");
0343   ConditionKey key_double_table(de,"double_table");
0344   ConditionKey key_int_table   (de,"int_table");
0345   ConditionKey key_derived_data(de,"derived_data");
0346   ConditionKey key_depath      (de,"derived_data/de_path");
0347   ConditionKey key_noctor_1    (de,"derived_data/NonDefaultCtor_1");
0348   ConditionKey key_derived1    (de,"derived_data/derived_1");
0349   ConditionKey key_derived2    (de,"derived_data/derived_2");
0350   ConditionKey key_derived3    (de,"derived_data/derived_3");
0351   ConditionKey key_derived4    (de,"derived_data/derived_4");
0352   ConditionKey key_derived5    (de,"derived_data/derived_5");
0353   [[maybe_unused]] int result = 0;
0354   int count = 0;
0355 
0356   // Let's go for the deltas....
0357   for( auto cond : conditions )  {
0358     // const auto& info = cond.descriptor().type();
0359     if ( 0 == dynamic_cast<detail::ConditionObject*>(cond.ptr()) )  {
0360       printout(ERROR,"accessConditions","Condition with bad base class!");
0361     }
0362    
0363     if ( cond.item_key() == key_path.item_key() )  {
0364       ++result;
0365       if ( cond.get<string>() != de.path() )
0366         printout(ERROR,"CondAccess","++ string:%s <> %s",de.path().c_str(), cond.get<string>().c_str());
0367     }
0368     else if ( cond.item_key() == key_depath.item_key() )  {
0369       vector<Condition>& data = cond.get<vector<Condition> >();
0370       size_t cnt = 0;
0371       ++num_depath_entries;
0372       if ( de.parent().isValid() )  {
0373         ++num_depath_checks;
0374         if ( data[0].get<string>() != de.parent().path() )  {
0375           printout(ERROR,"CondAccess","++ string:%s <> %s",
0376                    de.parent().path().c_str(), data[0].get<string>().c_str());
0377         }
0378         ++cnt;
0379       }
0380       for(const auto& child : de.children())   {
0381         const Condition& c = data[cnt];
0382         ++num_depath_checks;
0383         if ( c.get<string>() != child.second.path() )  {
0384           printout(ERROR,"CondAccess","++ string:%s <> %s",
0385                    child.second.path().c_str(), c.get<string>().c_str());
0386         }
0387         ++cnt;
0388       }
0389     }
0390     else if ( cond.item_key() == key_temperature.item_key() )  {
0391       result += int(cond.get<double>());
0392     }
0393     else if ( cond.item_key() == key_pressure.item_key() )  {
0394       result += int(cond.get<double>());
0395     }
0396     else if ( cond.item_key() == key_double_table.item_key() )  {
0397       result += int(cond.get<vector<double> >().size());
0398     }
0399     else if ( cond.item_key() == key_int_table.item_key() )  {
0400       result += int(cond.get<vector<int> >().size());
0401     }
0402     else if ( cond.item_key() == key_derived_data.item_key() )  {
0403       result += int(cond.get<int>());
0404     }
0405     else if ( cond.item_key() == key_derived1.item_key() )  {
0406       result += int(cond.get<vector<int> >().size());
0407     }
0408     else if ( cond.item_key() == key_derived2.item_key() )  {
0409       result += int(cond.get<vector<int> >().size());
0410     }
0411     else if ( cond.item_key() == key_derived3.item_key() )  {
0412       result += int(cond.get<vector<int> >().size());
0413     }
0414     else if ( cond.item_key() == key_derived4.item_key() )  {
0415       result += int(cond.get<vector<int> >().size());
0416     }
0417     else if ( cond.item_key() == key_derived5.item_key() )  {
0418       result += int(cond.get<vector<int> >().size());
0419     }
0420     else if ( cond.item_key() == key_noctor_1.item_key() )  {
0421       const NonDefaultCtorCond& c = cond.get<NonDefaultCtorCond>(); 
0422       result += c.a + c.b + c.b + c.d;
0423     }
0424     if ( !IOV::key_is_contained(iov.key(),cond.iov().key()) )  {
0425       printout(ERROR,"CondAccess","++ IOV mismatch:%s <> %s",
0426                iov.str().c_str(), cond.iov().str().c_str());
0427       continue;
0428     }
0429     ++count;
0430   }
0431   return count;
0432 }
0433 
0434 /// Callback to process a single detector element
0435 int ConditionsKeys::operator()(DetElement de, int)  const   {
0436   content.insertKey(ConditionKey(de,"temperature").hash);
0437   content.insertKey(ConditionKey(de,"pressure").hash);
0438   content.insertKey(ConditionKey(de,"double_table").hash);
0439   content.insertKey(ConditionKey(de,"int_table").hash);
0440   content.insertKey(ConditionKey(de,"derived_data").hash);
0441   content.insertKey(ConditionKey(de,"de_path").hash);
0442   return 1;
0443 }
0444 
0445 template<typename T>
0446 Condition ConditionsCreator::make_condition(DetElement de, const string& name, const T& val)  const {
0447   Condition cond(de.path()+"#"+name, name);
0448   T& value   = cond.bind<T>();
0449   value      = val;
0450   cond->hash = ConditionKey::hashCode(de,name);
0451   return cond;
0452 }
0453 
0454 template<typename T, typename... Args>
0455 Condition ConditionsCreator::make_condition_args(DetElement de, const string& name, Args... args)  const {
0456   Condition cond(de.path()+"#"+name, name);
0457   T& value   = cond.construct<T>(std::forward<Args>(args)...);
0458   cond->hash = ConditionKey::hashCode(de,name);
0459   return cond;
0460 }
0461 
0462 /// Callback to process a single detector element
0463 int ConditionsCreator::operator()(DetElement de, int)  const  {
0464   Condition temperature = make_condition<double>(de,"temperature",1.222);
0465   Condition pressure    = make_condition<double>(de,"pressure",888.88);
0466   Condition derived     = make_condition<int>   (de,"derived_data",100);
0467   Condition dbl_table   = make_condition<vector<double> >(de,"double_table",{1.,2.,3.,4.,5.,6.,7.,8.,9.});
0468   Condition int_table   = make_condition<vector<int> >   (de,"int_table",{10,20,30,40,50,60,70,80,90});
0469   Condition path        = make_condition<std::string>    (de,"de_path",de.path());
0470 
0471   slice.manager.registerUnlocked(pool, temperature);
0472   slice.manager.registerUnlocked(pool, pressure);
0473   slice.manager.registerUnlocked(pool, derived);
0474   slice.manager.registerUnlocked(pool, dbl_table);
0475   slice.manager.registerUnlocked(pool, int_table);
0476   slice.manager.registerUnlocked(pool, path);
0477   printout(printLevel,"Creator","++ Adding manually conditions for %s",de.path().c_str());
0478   return 5;
0479 }