From 9c6da01a750fcf55a208c97306a85d5b82325bf7 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Fri, 25 Sep 2020 17:20:13 +0200
Subject: [PATCH] Fixup for conditions to have names.

---
 DDCond/include/DDCond/ConditionsSlice.h       |  4 ++--
 DDCond/include/DDCond/ConditionsTags.h        |  1 +
 DDCond/src/ConditionsContent.cpp              |  3 +++
 DDCond/src/ConditionsSlice.cpp                |  1 -
 DDCond/src/Type1/Manager_Type1.cpp            |  9 ++++---
 .../plugins/ConditionsRepositoryParser.cpp    | 10 ++++----
 .../plugins/ConditionsRepositoryWriter.cpp    | 21 ++++++++++------
 DDCond/src/plugins/ConditionsUserPool.cpp     |  8 +++----
 DDCore/include/DD4hep/config.h                |  6 ++---
 DDCore/src/AlignmentsCalculator.cpp           |  3 ++-
 DDCore/src/ConditionsInterna.cpp              | 14 +++++------
 examples/AlignDet/CMakeLists.txt              |  4 ++--
 .../src/AlignmentExample_read_xml.cpp         |  2 +-
 examples/Conditions/CMakeLists.txt            |  2 +-
 .../data/Modules_run-1000...2000.xml          | 24 +++++++++----------
 15 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/DDCond/include/DDCond/ConditionsSlice.h b/DDCond/include/DDCond/ConditionsSlice.h
index b7f3b5bdd..3ac855b85 100644
--- a/DDCond/include/DDCond/ConditionsSlice.h
+++ b/DDCond/include/DDCond/ConditionsSlice.h
@@ -81,8 +81,8 @@ namespace dd4hep {
           iov_pool = s.manager.registerIOV(*iov.iovType,iov.key());
         }
         template <typename T> Inserter(ConditionsSlice& s, const T& data) : slice(s) {
-          const IOV& iov = s.pool->validity();
-          iov_pool = s.manager.registerIOV(*iov.iovType,iov.key());
+          const IOV& iov = slice.pool->validity();
+          iov_pool = slice.manager.registerIOV(*iov.iovType,iov.key());
           std::for_each(std::begin(data), std::end(data), *this);
         }
         void operator()(Condition c) const  {
diff --git a/DDCond/include/DDCond/ConditionsTags.h b/DDCond/include/DDCond/ConditionsTags.h
index 754e46fe6..1f8fb68c8 100644
--- a/DDCond/include/DDCond/ConditionsTags.h
+++ b/DDCond/include/DDCond/ConditionsTags.h
@@ -50,6 +50,7 @@ namespace dd4hep {
       UNICODE(mapping);
       UNICODE(sequence);
       UNICODE(alignment);
+      UNICODE(alignment_delta);
       UNICODE(repository);
     }
     // User must ensure there are no clashes. If yes, then the clashing entry is unnecessary.
diff --git a/DDCond/src/ConditionsContent.cpp b/DDCond/src/ConditionsContent.cpp
index 3e15a1d9f..a5f414599 100644
--- a/DDCond/src/ConditionsContent.cpp
+++ b/DDCond/src/ConditionsContent.cpp
@@ -98,6 +98,7 @@ bool ConditionsContent::remove(Condition::key_type hash)   {
 pair<Condition::key_type, ConditionsLoadInfo*>
 ConditionsContent::insertKey(Condition::key_type hash)   {
   auto ret = m_conditions.emplace(hash,(ConditionsLoadInfo*)0);
+  //printout(DEBUG,"ConditionsContent","++ Insert key: %016X",hash);
   if ( ret.second )  return pair<Condition::key_type, ConditionsLoadInfo*>(hash,0);
   return pair<Condition::key_type, ConditionsLoadInfo*>(0,0);
 }
@@ -106,6 +107,7 @@ ConditionsContent::insertKey(Condition::key_type hash)   {
 pair<Condition::key_type, ConditionsLoadInfo*>
 ConditionsContent::addLocationInfo(Condition::key_type hash, ConditionsLoadInfo* info)   {
   if ( info )   {
+    //printout(DEBUG,"ConditionsContent","++ Add location key: %016X",hash);
     auto ret = m_conditions.emplace(hash,info);
     if ( ret.second )  {
       info->addRef();
@@ -122,6 +124,7 @@ ConditionsContent::addDependency(ConditionDependency* dep)
 {
   auto ret = m_derived.emplace(dep->key(),dep);
   if ( ret.second )  {
+    //printout(DEBUG,"ConditionsContent","++ Add dependency key: %016X",dep->key());
     dep->addRef();
     return *(ret.first);
   }
diff --git a/DDCond/src/ConditionsSlice.cpp b/DDCond/src/ConditionsSlice.cpp
index f342cab19..0be5e5029 100644
--- a/DDCond/src/ConditionsSlice.cpp
+++ b/DDCond/src/ConditionsSlice.cpp
@@ -189,4 +189,3 @@ void dd4hep::cond::fill_content(ConditionsManager mgr,
   ConditionsIOVPool::Elements& pools = iovPool->elements;
   for_each(begin(pools),end(pools),SliceOper(content));
 }
-
diff --git a/DDCond/src/Type1/Manager_Type1.cpp b/DDCond/src/Type1/Manager_Type1.cpp
index f61056e58..3920bd77b 100644
--- a/DDCond/src/Type1/Manager_Type1.cpp
+++ b/DDCond/src/Type1/Manager_Type1.cpp
@@ -246,9 +246,12 @@ bool Manager_Type1::registerUnlocked(ConditionsPool& pool, Condition cond)   {
     cond->iov  = pool.iov;
     cond->setFlag(Condition::ACTIVE);
     pool.insert(cond);
-#if 0
-    printout(INFO,"ConditionsMgr","Register condition %016lX %s [%s] IOV:%s",
-             cond->hash, cond.name(), cond->address.c_str(), pool.iov->str().c_str());
+#if !defined(DD4HEP_MINIMAL_CONDITIONS)
+    printout(DEBUG,"ConditionsMgr","Register condition %016lX %s [%s] IOV:%s",
+             cond.key(), cond.name(), cond->address.c_str(), pool.iov->str().c_str());
+#else
+    printout(DEBUG,"ConditionsMgr","Register condition %016lX %s IOV:%s",
+             cond.key(), cond.name(), pool.iov->str().c_str());
 #endif
     if ( !m_onRegister.empty() )   {
       __callListeners(m_onRegister, &ConditionsListener::onRegisterCondition, cond);
diff --git a/DDCond/src/plugins/ConditionsRepositoryParser.cpp b/DDCond/src/plugins/ConditionsRepositoryParser.cpp
index ee5d27336..797a8f27b 100644
--- a/DDCond/src/plugins/ConditionsRepositoryParser.cpp
+++ b/DDCond/src/plugins/ConditionsRepositoryParser.cpp
@@ -150,10 +150,10 @@ namespace {
     string add = xml::DocumentHandler::system_path(e);
     string cond_nam = det.path()+"#"+nam;
     Condition cond(cond_nam, typ);
-    printout(s_parseLevel,"XMLConditions","++ Processing condition tag:%s name:%s type:%s [%s]",
-             tag.c_str(), nam.c_str(), typ.c_str(),
-             Path(add).filename().c_str());
-    cond->hash     = ConditionKey::hashCode(det,cond_nam);
+    cond->hash = ConditionKey::hashCode(det, nam);
+    printout(s_parseLevel,"XMLConditions","++ Processing condition tag:%s name:%s type:%s [%s] hash:%016X det:%p",
+             tag.c_str(), cond.name(), typ.c_str(),
+             Path(add).filename().c_str(), cond.key(), det.ptr());
 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
     cond->address  = add;
     cond->value    = "";
@@ -370,7 +370,7 @@ namespace dd4hep {
     xml_coll_t(e,_UC(mapping)).for_each(Converter<mapping>(description,param,optional));
     xml_coll_t(e,_UC(sequence)).for_each(Converter<sequence>(description,param,optional));
     xml_coll_t(e,_UC(pressure)).for_each(Converter<pressure>(description,param,optional));
-    xml_coll_t(e,_UC(alignment)).for_each(Converter<alignment>(description,param,optional));
+    xml_coll_t(e,_UC(alignment_delta)).for_each(Converter<alignment>(description,param,optional));
     xml_coll_t(e,_UC(temperature)).for_each(Converter<temperature>(description,param,optional));
     xml_coll_t(e,_UC(detelement)).for_each(Converter<detelement>(description,param,optional));
   }
diff --git a/DDCond/src/plugins/ConditionsRepositoryWriter.cpp b/DDCond/src/plugins/ConditionsRepositoryWriter.cpp
index 79840513e..fb7c395c0 100644
--- a/DDCond/src/plugins/ConditionsRepositoryWriter.cpp
+++ b/DDCond/src/plugins/ConditionsRepositoryWriter.cpp
@@ -146,7 +146,7 @@ namespace {
   };
   
   template <typename T> xml::Element _convert(xml::Element par, Condition c);
-
+  
   xml::Element make(xml::Element e, Condition c)  {
     char hash[64];
     std::string nam = c.name();
@@ -200,7 +200,7 @@ namespace {
     return temp;
   }
   template <> xml::Element _convert<Delta>(xml::Element par, Condition c)  {
-    xml::Element       align = make(xml::Element(par.document(),_UC(alignment)),c);
+    xml::Element       align = make(xml::Element(par.document(),_UC(alignment_delta)),c);
     const Delta&       delta = c.get<Delta>();
     if ( delta.flags&Delta::HAVE_TRANSLATION )
       align.append(_convert(align,delta.translation));
@@ -211,9 +211,16 @@ namespace {
     return align;
   }
   template <> xml::Element _convert<Alignment>(xml::Element par, Condition c)  {
-    xml::Element       align = make(xml::Element(par.document(),_UC(alignment)),c);
-    AlignmentCondition acond = c;
-    const Delta&       delta = acond.data().delta;
+    char hash[64];
+    typedef ConditionKey::KeyMaker KM;
+    AlignmentCondition  acond = c;
+    KM                  km(c.key());
+    const Delta&        delta = acond.data().delta;
+    xml::Element        align(xml::Element(par.document(),_UC(alignment_delta)));
+    Condition::key_type key = KM(km.values.det_key,align::Keys::deltaKey).hash;
+    ::snprintf(hash,sizeof(hash),"%llX",key);
+    align.setAttr(_U(name),align::Keys::deltaName);
+    align.setAttr(_U(key),hash);
     if ( delta.flags&Delta::HAVE_TRANSLATION )
       align.append(_convert(align,delta.translation));
     if ( delta.flags&Delta::HAVE_ROTATION )
@@ -409,8 +416,8 @@ size_t ConditionsXMLRepositoryWriter::collect(xml::Element root,
         ++m_numConverted;
       }
       else if ( cond_delta.isValid() )   {
-        conditions.append(_convert<Delta>(conditions,cond_delta));
-        ++m_numConverted;
+        //conditions.append(_convert<Delta>(conditions,cond_delta));
+        //++m_numConverted;
       }
     }
     for (const auto& i : detector.children())
diff --git a/DDCond/src/plugins/ConditionsUserPool.cpp b/DDCond/src/plugins/ConditionsUserPool.cpp
index 1805bf716..23227b1ca 100644
--- a/DDCond/src/plugins/ConditionsUserPool.cpp
+++ b/DDCond/src/plugins/ConditionsUserPool.cpp
@@ -244,12 +244,12 @@ template<typename MAPPING> inline bool
 ConditionsMappedUserPool<MAPPING>::i_insert(Condition::Object* o)   {
   int ret = m_conditions.emplace(o->hash,o).second;
   if ( flags&PRINT_INSERT )  {
-    printout(INFO,"UserPool","++ %s condition [%016llX]: %s.",
+    printout(INFO,"UserPool","++ %s condition [%016llX]: %s [%s].",
              ret ? "Successfully inserted" : "FAILED to insert", o->hash,
-#if defined(DD4HEP_MINIMAL_CONDITIONS)
-             "");
+#if defined(DD4HEP_CONDITIONS_HAVE_NAME)
+             o->GetName(), o->GetTitle());
 #else
-             o->name.c_str());
+             "");
 #endif
   }
   return ret;
diff --git a/DDCore/include/DD4hep/config.h b/DDCore/include/DD4hep/config.h
index 2c0345d8d..2e7579bdd 100644
--- a/DDCore/include/DD4hep/config.h
+++ b/DDCore/include/DD4hep/config.h
@@ -20,13 +20,13 @@
 /// If enabled it overrides DD4HEP_MINIMAL_CONDITIONS and sets it to true
 /// If enabled it overrides DD4HEP_CONDITIONS_HAVE_NAME and sets it to true
 #if defined(DD4HEP_DEBUG)
-#define DD4HEP_CONDITIONS_DEBUG  1
+#define DD4HEP_CONDITIONS_DEBUG     1
 #endif
 
-#if defined(DD4HEP_CONDITIONS_DEBUG)
 /// Enable flag to store conditions names to keys (needs some support from user code!)
 #define DD4HEP_CONDITIONS_HAVE_NAME 1
-#else
+
+#if !defined(DD4HEP_CONDITIONS_DEBUG)
 /// Enable this if you want to minimize the footprint of conditions
 #define DD4HEP_MINIMAL_CONDITIONS   1
 #endif
diff --git a/DDCore/src/AlignmentsCalculator.cpp b/DDCore/src/AlignmentsCalculator.cpp
index 5d4325ecc..49af590d1 100644
--- a/DDCore/src/AlignmentsCalculator.cpp
+++ b/DDCore/src/AlignmentsCalculator.cpp
@@ -130,7 +130,7 @@ Result Calculator::compute(Context& context, Entry& e)   const  {
     return result;
   }
   AlignmentCondition c = context.mapping.get(det, Keys::alignmentKey);
-  AlignmentCondition cond = c.isValid() ? c : AlignmentCondition("alignment");
+  AlignmentCondition cond = c.isValid() ? c : AlignmentCondition(det.path()+"#alignment");
   AlignmentData&     align = cond.data();
   const Delta*       delta = e.delta ? e.delta : &identity_delta;
   TGeoHMatrix        transform_for_delta;
@@ -165,6 +165,7 @@ Result Calculator::compute(Context& context, Entry& e)   const  {
   // Update mapping if the condition is freshly created
   if ( !c.isValid() )  {
     e.created = 1;
+    cond->flags |= Condition::ALIGNMENT_DERIVED;
     cond->hash = ConditionKey(e.det,Keys::alignmentKey).hash;
     context.mapping.insert(e.det, Keys::alignmentKey, cond);
   }
diff --git a/DDCore/src/ConditionsInterna.cpp b/DDCore/src/ConditionsInterna.cpp
index eca1e178e..c7de250d3 100644
--- a/DDCore/src/ConditionsInterna.cpp
+++ b/DDCore/src/ConditionsInterna.cpp
@@ -20,10 +20,10 @@
 using namespace std;
 using namespace dd4hep;
 
-#if !defined(DD4HEP_CONDITIONS_DEBUG) && defined(DD4HEP_MINIMAL_CONDITIONS)
-DD4HEP_INSTANTIATE_HANDLE_UNNAMED(detail::ConditionObject);
-#else
+#if defined(DD4HEP_CONDITIONS_HAVE_NAME)
 DD4HEP_INSTANTIATE_HANDLE_NAMED(detail::ConditionObject);
+#else
+DD4HEP_INSTANTIATE_HANDLE_UNNAMED(detail::ConditionObject);
 #endif
 
 namespace {
@@ -44,12 +44,12 @@ detail::ConditionObject::ConditionObject()
 }
 
 /// Standard constructor
-#if !defined(DD4HEP_CONDITIONS_DEBUG) && defined(DD4HEP_MINIMAL_CONDITIONS)
-detail::ConditionObject::ConditionObject(const string& ,const string& )
-  : data()
-#else
+#if defined(DD4HEP_CONDITIONS_HAVE_NAME)
 detail::ConditionObject::ConditionObject(const string& nam,const string& tit)
   : NamedObject(nam, tit), data()
+#else
+detail::ConditionObject::ConditionObject(const string& ,const string& )
+  : data()
 #endif
 {
   InstanceCount::increment(this);
diff --git a/examples/AlignDet/CMakeLists.txt b/examples/AlignDet/CMakeLists.txt
index 27eb9e128..1c626dea6 100644
--- a/examples/AlignDet/CMakeLists.txt
+++ b/examples/AlignDet/CMakeLists.txt
@@ -55,7 +55,7 @@ dd4hep_add_test_reg( AlignDet_Telescope_read_xml
   EXEC_ARGS  geoPluginRun -volmgr -destroy -plugin DD4hep_AlignmentExample_read_xml 
      -input  file:${AlignDet_INSTALL}/compact/Telescope.xml 
      -deltas file:${CMAKE_INSTALL_PREFIX}/examples/Conditions/data/repository.xml 
-  REGEX_PASS "20 conditions in slice. \\(T:20,S:20,L:0,C:0,M:0\\) Alignments accessed: 20 \\(A:19,M:0\\) for IOV:run\\(1\\)"
+  REGEX_PASS "39 conditions in slice. \\(T:20,S:20,L:0,C:0,M:0\\) Alignments accessed: 20 \\(A:19,M:0\\) for IOV:run\\(1\\)"
   REGEX_FAIL " ERROR ;EXCEPTION;Exception"
   )
 #
@@ -115,7 +115,7 @@ dd4hep_add_test_reg( AlignDet_Telescope_readback_xml
   EXEC_ARGS  geoPluginRun -volmgr -destroy -plugin DD4hep_AlignmentExample_read_xml
   -input  file:${AlignDet_INSTALL}/compact/Telescope.xml
   -deltas file:./new_cond.xml
-  REGEX_PASS "33 conditions in slice. \\(T:33,S:33,L:0,C:0,M:0\\) Alignments accessed: 20 \\(A:19,M:0\\) for IOV:run\\(1\\)"
+  REGEX_PASS "52 conditions in slice. \\(T:33,S:33,L:0,C:0,M:0\\) Alignments accessed: 20 \\(A:19,M:0\\) for IOV:run\\(1\\)"
   REGEX_FAIL " ERROR ;EXCEPTION;Exception"
   )
 #
diff --git a/examples/AlignDet/src/AlignmentExample_read_xml.cpp b/examples/AlignDet/src/AlignmentExample_read_xml.cpp
index 3a653e774..914619471 100644
--- a/examples/AlignDet/src/AlignmentExample_read_xml.cpp
+++ b/examples/AlignDet/src/AlignmentExample_read_xml.cpp
@@ -99,7 +99,7 @@ static int alignment_example (Detector& description, int argc, char** argv)  {
   printout(INFO,"Example",
            "%ld conditions in slice. (T:%ld,S:%ld,L:%ld,C:%ld,M:%ld) "
            "Alignments accessed: %ld (A:%ld,M:%ld) for IOV:%-12s",
-           slice->conditions().size(),
+           slice->pool->size(),
            cres.total(), cres.selected, cres.loaded, cres.computed, cres.missing, 
            total_accessed, ares.computed, ares.missing, iov_typ->str().c_str());
 
diff --git a/examples/Conditions/CMakeLists.txt b/examples/Conditions/CMakeLists.txt
index ce381820b..dbc58ed17 100644
--- a/examples/Conditions/CMakeLists.txt
+++ b/examples/Conditions/CMakeLists.txt
@@ -54,7 +54,7 @@ dd4hep_add_test_reg( Conditions_Telescope_cond_dump_by_detelement
   -compact file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml 
   -plugin DD4hep_ConditionsXMLRepositoryParser file:${CMAKE_INSTALL_PREFIX}/examples/Conditions/data/repository.xml 
   -plugin DD4hep_DetElementConditionsDump
-  REGEX_PASS "Path\\:/world/Telescope/module_9#alignment"
+  REGEX_PASS "Key\\:FA708B8A8CDE3019 Type\\:dd4hep\\:\\:Delta"
   REGEX_FAIL " ERROR ;EXCEPTION;Exception"
   )
 #
diff --git a/examples/Conditions/data/Modules_run-1000...2000.xml b/examples/Conditions/data/Modules_run-1000...2000.xml
index 86f0bfea5..6a2442937 100644
--- a/examples/Conditions/data/Modules_run-1000...2000.xml
+++ b/examples/Conditions/data/Modules_run-1000...2000.xml
@@ -4,10 +4,10 @@
 <conditions>
 
   <detelement path="/world/Telescope">
-    <alignment>
+    <alignment_delta>
       <position x="0" y="0" z="5*cm"/>
       <rotation x="0" y="0" z="0"/>     
-    </alignment>
+    </alignment_delta>
   </detelement>
 
   <detelement path="/world/Telescope/module_1">
@@ -26,35 +26,35 @@
   </detelement>
 
   <detelement path="/world/Telescope/module_6">
-    <alignment>
+    <alignment_delta>
       <position x="0" y="0"    z="290.0*mm"/>
       <rotation x="0" y="pi/2" z="0"/>     
-    </alignment>
+    </alignment_delta>
   </detelement>
 
   <detelement path="/world/Telescope/module_7">
-    <alignment>
+    <alignment_delta>
       <position x="0" y="0"    z="290.0*mm"/>
       <rotation x="0" y="pi/2" z="0"/>     
-    </alignment>
+    </alignment_delta>
   </detelement>
 
   <detelement path="/world/Telescope/module_8">
-    <alignment>
+    <alignment_delta>
       <position x="0" y="0"    z="290.0*mm"/>
       <rotation x="0" y="pi/2" z="0"/>     
-    </alignment>
+    </alignment_delta>
   </detelement>
 <!--
 -->
   <detelement path="/world/Telescope/module_9" ref="default_module.xml">
-    <alignment>
+    <alignment_delta>
       <position x="0" y="0" z="10.0*mm"/>
       <rotation x="0" y="0" z="0"/>     
       <pivot    x="0" y="0" z="0"/>     
-    </alignment>
+    </alignment_delta>
     <detelement path="/world/Telescope/module_9/sensor">
-      <alignment>
+      <alignment_delta>
         <position x="0" y="0" z="10.0*mm"/>
         <!-- This is a point symmetry of 180 degree. 
              Can be checked with the printer. 
@@ -62,7 +62,7 @@
          -->
         <rotation x="0" y="0" z="pi"/>     
         <pivot    x="0" y="0" z="0"/>     
-      </alignment>
+      </alignment_delta>
     </detelement>
 <!--
 -->
-- 
GitLab