13 #ifndef DDCOND_CONDITIONSMAPPEDUSERPOOL_H
14 #define DDCOND_CONDITIONSMAPPEDUSERPOOL_H
22 #include <unordered_map>
46 template<
typename MAPPING>
67 virtual void print(
const std::string& opt)
const override;
69 virtual size_t size()
const override;
71 virtual void clear()
override;
88 virtual size_t registerMany(
const IOV& iov,
const std::vector<Condition>& values)
override;
124 bool force)
override;
178 virtual int operator()(
const std::pair<dd4hep::Condition::key_type,dd4hep::Condition>& i)
const override {
180 dd4hep::printout(dd4hep::INFO,
"UserPool",
"++ %16llX/%16llX Val:%s %s",i.first, c->hash, c->
value.c_str(), c.
str().c_str());
186 MapSelector(T& o) : m(o) {}
188 {
return m.emplace(o->
hash,o).second; }
190 template <
typename T> MapSelector<T> mapSelector(T& container)
191 {
return MapSelector<T>(container); }
193 template <
typename T>
struct Inserter {
199 m.emplace(o->
hash,o);
202 void operator()(
const std::pair<dd4hep::Condition::key_type,dd4hep::Condition>& e) { (*this)(e.second); }
207 template<
typename MAPPING>
217 except(
"UserPool",
"The conditions manager is not properly setup. No conditions loader present.");
219 except(
"UserPool",
"FAILED to create mapped user pool. [Invalid IOV pool]");
221 except(
"UserPool",
"FAILED to create mapped user pool. [Invalid conditions manager]");
225 template<
typename MAPPING>
233 typename MAPPING::const_iterator i=m_conditions.find(
key);
235 if ( i == m_conditions.end() ) {
239 return i != m_conditions.end() ? (*i).second : 0;
242 template<
typename MAPPING>
inline bool
244 int ret = m_conditions.emplace(o->hash,o).second;
245 if ( flags&PRINT_INSERT ) {
246 printout(INFO,
"UserPool",
"++ %s condition [%016llX]"
247 #
if defined(DD4HEP_CONDITIONS_HAVE_NAME)
248 ": %s [%s].", ret ?
"Successfully inserted" :
"FAILED to insert",
249 o->hash, o->GetName(), o->GetTitle());
251 , ret ?
"Successfully inserted" :
"FAILED to insert", o->hash);
258 template<
typename MAPPING>
260 return m_conditions.size();
264 template<
typename MAPPING>
266 const IOV* iov = &m_iov;
267 printout(INFO,
"UserPool",
"+++ %s Conditions for USER pool with IOV: %-32s [%4d entries]",
268 opt.c_str(), iov->
str().c_str(), size());
276 template<
typename MAPPING>
278 if ( flags&PRINT_CLEAR ) {
279 printout(INFO,
"UserPool",
"++ Cleared %ld conditions from pool.",m_conditions.size());
282 m_conditions.clear();
286 template<
typename MAPPING>
288 return i_findCondition(
hash) != 0;
292 template<
typename MAPPING>
294 return i_findCondition(
key.hash) != 0;
298 template<
typename MAPPING>
300 return i_findCondition(
hash);
304 template<
typename MAPPING>
306 return i_findCondition(
key.hash);
310 template<
typename MAPPING>
bool
316 return m_manager.registerUnlocked(*pool,
cond);
318 except(
"UserPool",
"++ Failed to register IOV: %s",iov.
str().c_str());
320 except(
"UserPool",
"++ Cannot register conditions with invalid IOV.");
325 template<
typename MAPPING> std::size_t
327 const std::vector<Condition>& conds) {
331 std::size_t result = m_manager.blockRegister(*pool, conds);
332 if ( result == conds.size() ) {
333 for(
auto c : conds) i_insert(c.
ptr());
336 except(
"UserPool",
"++ Conditions registration was incomplete: "
337 "registerd only %ld out of %ld conditions.",
338 result, conds.size());
340 except(
"UserPool",
"++ Failed to register IOV: %s",iov.
str().c_str());
342 except(
"UserPool",
"++ Cannot register conditions with invalid IOV.");
347 template<
typename MAPPING>
349 bool result = i_insert(
cond.ptr());
350 if ( result )
return true;
351 except(
"UserPool",
"++ Attempt to double insert condition: %16llX Name:%s",
cond->hash,
cond.name());
356 template<
typename MAPPING>
358 unsigned int item_key,
365 template<
typename MAPPING>
371 template<
typename MAPPING> std::vector<dd4hep::Condition>
381 template<
typename MAPPING> std::vector<dd4hep::Condition>
383 std::vector<Condition> result;
384 if ( !m_conditions.empty() ) {
385 typename MAPPING::const_iterator first = m_conditions.lower_bound(lower);
386 for(; first != m_conditions.end(); ++first ) {
387 if ( (*first).first > upper )
break;
388 result.emplace_back((*first).second);
395 template<
typename MAPPING>
397 for(
const auto& i : m_conditions )
402 template<
typename MAPPING>
415 template<
typename MAPPING>
420 typename MAPPING::const_iterator first = m_conditions.lower_bound(lower);
421 for(; first != m_conditions.end() && (*first).first <= upper; ++first )
422 processor((*first).second);
426 template<
typename MAPPING>
428 return remove(
key.hash);
432 template<
typename MAPPING>
434 typename MAPPING::iterator i = m_conditions.find(hash_key);
435 if ( i != m_conditions.end() ) {
436 m_conditions.erase(i);
443 template<
typename MAPPING>
448 if ( !deps.empty() ) {
451 for (
const auto& i : deps ) {
452 typename MAPPING::iterator j = m_conditions.find(i.first);
453 if ( j != m_conditions.end() ) {
460 m_conditions.erase(j);
466 m_conditions.erase(j);
471 if ( !missing.empty() ) {
486 typedef std::pair<dd4hep::Condition::key_type,const ConditionDependency*> Dep;
487 typedef std::pair<const dd4hep::Condition::key_type,dd4hep::detail::ConditionObject*> Cond;
488 typedef std::pair<const dd4hep::Condition::key_type,ConditionsLoadInfo* > Info;
489 typedef std::pair<const dd4hep::Condition::key_type,dd4hep::Condition> Cond2;
491 bool operator()(
const Dep& a,
const Cond& b)
const {
return a.first < b.first; }
492 bool operator()(
const Cond& a,
const Dep& b)
const {
return a.first < b.first; }
494 bool operator()(
const Info& a,
const Cond& b)
const {
return a.first < b.first; }
495 bool operator()(
const Cond& a,
const Info& b)
const {
return a.first < b.first; }
497 bool operator()(
const Info& a,
const Cond2& b)
const {
return a.first < b.first; }
498 bool operator()(
const Cond2& a,
const Info& b)
const {
return a.first < b.first; }
507 typedef std::vector<std::pair<Condition::key_type,ConditionDependency*> > CalcMissing;
508 typedef std::vector<std::pair<Condition::key_type,ConditionsLoadInfo*> > CondMissing;
509 const auto& slice_cond = slice.
content->conditions();
510 const auto& slice_calc = slice.
content->derived();
513 bool do_load = m_manager->doLoadConditions();
514 bool do_output_miss = m_manager->doOutputUnloaded();
521 static std::mutex lock;
522 std::lock_guard<std::mutex> guard(lock);
524 m_conditions.clear();
525 slice_miss_cond.clear();
526 slice_miss_calc.clear();
530 CondMissing cond_missing(slice_cond.size()+m_conditions.size());
531 CalcMissing calc_missing(slice_calc.size()+m_conditions.size());
533 CondMissing::iterator last_cond = set_difference(begin(slice_cond), end(slice_cond),
534 begin(m_conditions), end(m_conditions),
535 begin(cond_missing), COMP());
536 long num_cond_miss = last_cond-begin(cond_missing);
537 cond_missing.resize(num_cond_miss);
538 printout((flags&PRINT_LOAD) ? INFO : DEBUG,
"UserPool",
539 "%ld conditions out of %ld conditions are MISSING.",
540 num_cond_miss, slice_cond.size());
541 CalcMissing::iterator last_calc = set_difference(begin(slice_calc), end(slice_calc),
542 begin(m_conditions), end(m_conditions),
543 begin(calc_missing), COMP());
544 long num_calc_miss = last_calc-begin(calc_missing);
545 calc_missing.resize(num_calc_miss);
546 printout((flags&PRINT_COMPUTE) ? INFO : DEBUG,
"UserPool",
547 "%ld derived conditions out of %ld conditions are MISSING.",
548 num_calc_miss, slice_calc.size());
552 result.
selected = m_conditions.size();
553 result.
missing = num_cond_miss+num_calc_miss;
557 if ( num_cond_miss > 0 ) {
560 size_t updates = m_loader->load_many(required, cond_missing, loaded, pool_iov);
563 CondMissing load_missing(cond_missing.size()+loaded.size());
565 CondMissing::iterator load_last = set_difference(begin(cond_missing), last_cond,
566 begin(loaded), end(loaded),
567 begin(load_missing), COMP());
568 long num_load_miss = load_last-begin(load_missing);
569 printout(num_load_miss==0 ? DEBUG : ERROR,
"UserPool",
570 "%ld out of %ld conditions CANNOT be loaded...",
571 num_load_miss, loaded.size());
572 if ( do_output_miss ) {
573 copy(begin(load_missing), load_last, inserter(slice_miss_cond, slice_miss_cond.begin()));
574 for (
const auto& missing : slice_miss_cond ) {
575 printout (ERROR,
"TEST",
"Unloaded: %s",missing.second->toString().c_str());
578 for_each(loaded.begin(),loaded.end(),Inserter<MAPPING>(m_conditions,&m_iov));
579 result.
loaded = slice_cond.size()-num_load_miss;
580 result.
missing = num_load_miss+num_calc_miss;
581 if ( cond_missing.size() != loaded.size() ) {
583 printout(ERROR,
"UserPool",
"Could not load %ld out of %ld conditions. "
584 "[Unhandled error]",cond_missing.size(), slice_cond.size());
588 else if ( do_output_miss ) {
589 copy(begin(cond_missing), last_cond, inserter(slice_miss_cond, slice_miss_cond.begin()));
590 for (
const auto& missing : slice_miss_cond ) {
591 printout (ERROR,
"TEST",
"Unloaded: %s",missing.second->toString().c_str());
598 if ( num_calc_miss > 0 ) {
600 std::map<Condition::key_type,const ConditionDependency*> deps(calc_missing.begin(),last_calc);
609 if ( do_output_miss && result.
computed < deps.size() ) {
611 for(
auto i = calc_missing.begin(); i != last_calc; ++i ) {
612 typename MAPPING::iterator j = m_conditions.find((*i).first);
613 if ( j == m_conditions.end() )
614 slice_miss_calc.emplace(*i);
618 else if ( do_output_miss ) {
619 copy(begin(calc_missing), last_calc, inserter(slice_miss_calc, slice_miss_calc.begin()));
625 m_iovPool->select(required, slice.
used_pools);
635 typedef std::vector<std::pair<Condition::key_type,ConditionsLoadInfo*> > CondMissing;
636 const auto& slice_cond = slice.
content->conditions();
638 bool do_load = m_manager->doLoadConditions();
639 bool do_output_miss = m_manager->doOutputUnloaded();
646 static std::mutex lock;
647 std::lock_guard<std::mutex> guard(lock);
649 m_conditions.clear();
650 slice_miss_cond.clear();
654 CondMissing cond_missing(slice_cond.size()+m_conditions.size());
655 CondMissing::iterator last_cond = set_difference(begin(slice_cond), end(slice_cond),
656 begin(m_conditions), end(m_conditions),
657 begin(cond_missing), COMP());
658 long num_cond_miss = last_cond-begin(cond_missing);
659 cond_missing.resize(num_cond_miss);
660 printout((flags&PRINT_LOAD) ? INFO : DEBUG,
"UserPool",
661 "Found %ld missing conditions out of %ld conditions.",
662 num_cond_miss, slice_cond.size());
665 result.
missing = num_cond_miss;
666 result.
selected = m_conditions.size();
670 if ( num_cond_miss > 0 ) {
673 size_t updates = m_loader->load_many(required, cond_missing, loaded, pool_iov);
676 CondMissing load_missing(cond_missing.size()+loaded.size());
678 CondMissing::iterator load_last = set_difference(begin(cond_missing), last_cond,
679 begin(loaded), end(loaded),
680 begin(load_missing), COMP());
681 long num_load_miss = load_last-begin(load_missing);
682 printout(num_load_miss==0 ? DEBUG : ERROR,
"UserPool",
683 "+++ %ld out of %ld conditions CANNOT be loaded... [Not found by loader]",
684 num_load_miss, loaded.size());
685 if ( do_output_miss ) {
686 copy(begin(load_missing), load_last, inserter(slice_miss_cond, slice_miss_cond.begin()));
688 for_each(loaded.begin(),loaded.end(),Inserter<MAPPING>(m_conditions,&m_iov));
689 result.
loaded = slice_cond.size()-num_load_miss;
690 result.
missing = num_load_miss;
691 if ( cond_missing.size() != loaded.size() ) {
696 else if ( do_output_miss ) {
697 copy(begin(cond_missing), last_cond, inserter(slice_miss_cond, slice_miss_cond.begin()));
709 typedef std::vector<std::pair<Condition::key_type,ConditionDependency*> > CalcMissing;
710 const auto& slice_calc = slice.
content->derived();
712 bool do_load = m_manager->doLoadConditions();
713 bool do_output = m_manager->doOutputUnloaded();
720 static std::mutex lock;
721 std::lock_guard<std::mutex> guard(lock);
723 slice_miss_calc.clear();
724 CalcMissing calc_missing(slice_calc.size()+m_conditions.size());
725 CalcMissing::iterator last_calc = set_difference(begin(slice_calc), end(slice_calc),
726 begin(m_conditions), end(m_conditions),
727 begin(calc_missing), COMP());
728 long num_calc_miss = last_calc-begin(calc_missing);
729 calc_missing.resize(num_calc_miss);
730 printout((flags&PRINT_COMPUTE) ? INFO : DEBUG,
"UserPool",
731 "Found %ld missing derived conditions out of %ld conditions.",
732 num_calc_miss, m_conditions.size());
736 result.
missing = num_calc_miss;
737 result.
selected = m_conditions.size();
741 if ( num_calc_miss > 0 ) {
743 std::map<Condition::key_type,const ConditionDependency*> deps(calc_missing.begin(),last_calc);
753 if ( do_output && result.
computed < deps.size() ) {
754 for(
auto i=calc_missing.begin(); i != last_calc; ++i) {
755 typename MAPPING::iterator j = m_conditions.find((*i).first);
756 if ( j == m_conditions.end() )
757 slice_miss_calc.emplace(*i);
761 else if ( do_output ) {
762 copy(begin(calc_missing), last_calc, inserter(slice_miss_calc, slice_miss_calc.begin()));
768 m_iovPool->select(required, slice.
used_pools);
780 typedef std::unordered_map<Condition::key_type,Condition::Object*>
umap_t;
789 for(
const auto& e : m_conditions )
790 if ( e.second->hash >= lower && e.second->hash <= upper )
796 template<> std::vector<Condition>
798 std::vector<Condition> result;
799 for(
const auto& e : m_conditions ) {
800 if ( e.second->hash >= lower && e.second->hash <= upper )
801 result.emplace_back(e.second);
809 template <
typename MAPPING>
817 dd4hep::except(
"ConditionsMappedUserPool",
"++ Insufficient arguments: arg[0] = ConditionManager!");
824 {
return create_pool<std::map<dd4hep::Condition::key_type,dd4hep::Condition::Object*> >(description, argc, argv); }
829 {
return create_pool<std::unordered_map<dd4hep::Condition::key_type,dd4hep::Condition::Object*> >(description, argc, argv); }