From 2fc15cc46a8dbce06021c141a7804bced6e91693 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Wed, 21 Sep 2016 22:15:03 +0200
Subject: [PATCH] Change file names

---
 DDAlign/include/DDAlign/AlignmentStack.h      |  4 +-
 .../include/DDAlign/AlignmentTransaction.h    | 54 ---------------
 ...lignmentCache.h => GlobalAlignmentCache.h} | 12 ++--
 ...gnmentWriter.h => GlobalAlignmentWriter.h} |  0
 DDAlign/src/AlignmentStack.cpp                |  4 +-
 DDAlign/src/AlignmentTransaction.cpp          | 41 ------------
 ...mentCache.cpp => GlobalAlignmentCache.cpp} | 64 ++++++------------
 ...ntWriter.cpp => GlobalAlignmentWriter.cpp} |  1 +
 DDAlign/src/plugins/AlignmentParser.cpp       | 65 ++++++++++++-------
 9 files changed, 74 insertions(+), 171 deletions(-)
 delete mode 100644 DDAlign/include/DDAlign/AlignmentTransaction.h
 rename DDAlign/include/DDAlign/{AlignmentCache.h => GlobalAlignmentCache.h} (95%)
 rename DDAlign/include/DDAlign/{AlignmentWriter.h => GlobalAlignmentWriter.h} (100%)
 delete mode 100644 DDAlign/src/AlignmentTransaction.cpp
 rename DDAlign/src/{AlignmentCache.cpp => GlobalAlignmentCache.cpp} (88%)
 rename DDAlign/src/{AlignmentWriter.cpp => GlobalAlignmentWriter.cpp} (99%)

diff --git a/DDAlign/include/DDAlign/AlignmentStack.h b/DDAlign/include/DDAlign/AlignmentStack.h
index 052d77960..ad0dffbf2 100644
--- a/DDAlign/include/DDAlign/AlignmentStack.h
+++ b/DDAlign/include/DDAlign/AlignmentStack.h
@@ -112,9 +112,9 @@ namespace DD4hep {
       /// Check existence of alignment stack
       static bool exists();
       /// Add a new entry to the cache. The key is the placement path
-      static bool insert(const std::string& full_path, dd4hep_ptr<StackEntry>& new_entry);
+      bool insert(const std::string& full_path, dd4hep_ptr<StackEntry>& new_entry);
       /// Add a new entry to the cache. The key is the placement path. The placement path must be set in the entry
-      static bool insert(dd4hep_ptr<StackEntry>& new_entry);
+      bool insert(dd4hep_ptr<StackEntry>& new_entry);
       /// Clear data content and remove the slignment stack
       void release();
       /// Access size of the alignment stack
diff --git a/DDAlign/include/DDAlign/AlignmentTransaction.h b/DDAlign/include/DDAlign/AlignmentTransaction.h
deleted file mode 100644
index bb525291c..000000000
--- a/DDAlign/include/DDAlign/AlignmentTransaction.h
+++ /dev/null
@@ -1,54 +0,0 @@
-// $Id: $
-//==========================================================================
-//  AIDA Detector description implementation for LCD
-//--------------------------------------------------------------------------
-// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
-// All rights reserved.
-//
-// For the licensing terms see $DD4hepINSTALL/LICENSE.
-// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
-//
-// Author     : M.Frank
-//
-//==========================================================================
-#ifndef DD4HEP_ALIGNMENT_ALIGNMENTTRANSACTION_H
-#define DD4HEP_ALIGNMENT_ALIGNMENTTRANSACTION_H
-
-// Framework include files
-#include "DD4hep/Alignments.h"
-
-/// Namespace for the AIDA detector description toolkit
-namespace DD4hep {
-
-  namespace XML { class Handle_t; }
-
-  /// Namespace for the geometry part of the AIDA detector description toolkit
-  namespace Alignments {
-
-    // Forward declarations
-    class AlignmentCache;
-
-    /// Manage alignment transaction to the cache for a given LCDD instance
-    /**
-     *  \author   M.Frank
-     *  \version  1.0
-     *  \ingroup  DD4HEP_ALIGN
-     */
-    class AlignmentTransaction {
-    public:
-      /// Internal flag to remember transaction contexts
-      bool flag;
-      /// Reference to the current LCDD instance
-      LCDD& lcdd;
-      /// Reference to the alignment cache
-      AlignmentCache* m_cache;
-
-      /// Default constructor
-      AlignmentTransaction(LCDD& l, const XML::Handle_t& e);
-      /// Default destructor
-      ~AlignmentTransaction();
-    };
-
-  } /* End namespace Geometry        */
-} /* End namespace DD4hep            */
-#endif    /* DD4HEP_ALIGNMENT_ALIGNMENTTRANSACTION_H       */
diff --git a/DDAlign/include/DDAlign/AlignmentCache.h b/DDAlign/include/DDAlign/GlobalAlignmentCache.h
similarity index 95%
rename from DDAlign/include/DDAlign/AlignmentCache.h
rename to DDAlign/include/DDAlign/GlobalAlignmentCache.h
index a10fb19b9..5422b10ec 100644
--- a/DDAlign/include/DDAlign/AlignmentCache.h
+++ b/DDAlign/include/DDAlign/GlobalAlignmentCache.h
@@ -43,6 +43,7 @@ namespace DD4hep {
       friend class AlignmentOperator;
 
     public:
+      typedef AlignmentStack Stack;
       typedef AlignmentStack::StackEntry Entry;
       typedef std::map<unsigned int, TGeoPhysicalNode*> Cache;
       typedef std::map<std::string,AlignmentCache*> SubdetectorAlignments;
@@ -53,6 +54,7 @@ namespace DD4hep {
       SubdetectorAlignments m_detectors;
       /// The subdetector specific map of alignments caches
       Cache       m_cache;
+      /// 
       /// Branchg name: If it is not the main tree instance, the name of the subdetector
       std::string m_sdPath;
       /// The length of the branch name to optimize lookups....
@@ -63,10 +65,11 @@ namespace DD4hep {
       bool        m_top;
 
     protected:
-      /// Default constructor
+      /// Default constructor initializing variables
       AlignmentCache(LCDD& lcdd, const std::string& sdPath, bool top);
       /// Default destructor
       virtual ~AlignmentCache();
+
       /// Retrieve branch cache by name. If not present it will be created
       AlignmentCache* subdetectorAlignments(const std::string& name);
 
@@ -79,20 +82,17 @@ namespace DD4hep {
 
     public:
       /// Create and install a new instance tree
-      static void install(LCDD& lcdd);
+      static AlignmentCache* install(LCDD& lcdd);
       /// Unregister and delete a tree instance
       static void uninstall(LCDD& lcdd);
-
       /// Add reference count
       int addRef();
       /// Release object. If reference count goes to NULL, automatic deletion is triggered.
       int release();
       /// Access the section name
       const std::string& name() const   {   return m_sdPath;  }
-      /// Open a new transaction stack (Note: only one stack allowed!)
-      void openTransaction();
       /// Close existing transaction stack and apply all alignments
-      void closeTransaction();
+      void commit(AlignmentStack& stack);
       /// Retrieve the cache section corresponding to the path of an entry.
       AlignmentCache* section(const std::string& path_name) const;
       /// Retrieve an alignment entry by its lacement path
diff --git a/DDAlign/include/DDAlign/AlignmentWriter.h b/DDAlign/include/DDAlign/GlobalAlignmentWriter.h
similarity index 100%
rename from DDAlign/include/DDAlign/AlignmentWriter.h
rename to DDAlign/include/DDAlign/GlobalAlignmentWriter.h
diff --git a/DDAlign/src/AlignmentStack.cpp b/DDAlign/src/AlignmentStack.cpp
index ba51135fc..02450af33 100644
--- a/DDAlign/src/AlignmentStack.cpp
+++ b/DDAlign/src/AlignmentStack.cpp
@@ -136,14 +136,14 @@ void AlignmentStack::release()    {
 bool AlignmentStack::insert(const string& full_path, dd4hep_ptr<StackEntry>& entry)  {
   if ( entry.get() && !full_path.empty() )  {
     entry->path = full_path;
-    return get().add(entry);
+    return add(entry);
   }
   throw runtime_error("AlignmentStack> Attempt to apply an invalid alignment entry.");
 }
 
 /// Add a new entry to the cache. The key is the placement path
 bool AlignmentStack::insert(dd4hep_ptr<StackEntry>& entry)  {
-  return get().add(entry);
+  return add(entry);
 }
 
 /// Add a new entry to the cache. The key is the placement path
diff --git a/DDAlign/src/AlignmentTransaction.cpp b/DDAlign/src/AlignmentTransaction.cpp
deleted file mode 100644
index 0a3cd82c4..000000000
--- a/DDAlign/src/AlignmentTransaction.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-// $Id: $
-//==========================================================================
-//  AIDA Detector description implementation for LCD
-//--------------------------------------------------------------------------
-// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
-// All rights reserved.
-//
-// For the licensing terms see $DD4hepINSTALL/LICENSE.
-// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
-//
-// Author     : M.Frank
-//
-//==========================================================================
-
-// Framework include files
-#include "DD4hep/LCDD.h"
-#include "DDAlign/AlignmentTags.h"
-#include "DDAlign/AlignmentCache.h"
-#include "DDAlign/AlignmentTransaction.h"
-
-using namespace DD4hep;
-using namespace DD4hep::Alignments;
-
-/// Default constructor
-AlignmentTransaction::AlignmentTransaction(LCDD& l, const XML::Handle_t& e) : lcdd(l) {
-  flag = e.hasChild(_ALU(close_transaction));
-  /// First check if a transaction is to be opened
-  m_cache = lcdd.extension<Alignments::AlignmentCache>();
-  m_cache->addRef();
-  if ( e.hasChild(_ALU(open_transaction)) )  {
-    m_cache->openTransaction();
-  }
-}
-/// Default destructor
-AlignmentTransaction::~AlignmentTransaction()   {
-  /// Last check if a transaction is to be closed
-  if ( flag ) {
-    lcdd.extension<Alignments::AlignmentCache>()->closeTransaction();
-  }
-  m_cache->release();
-}
diff --git a/DDAlign/src/AlignmentCache.cpp b/DDAlign/src/GlobalAlignmentCache.cpp
similarity index 88%
rename from DDAlign/src/AlignmentCache.cpp
rename to DDAlign/src/GlobalAlignmentCache.cpp
index 4a6ae6d48..d3fc53c43 100644
--- a/DDAlign/src/AlignmentCache.cpp
+++ b/DDAlign/src/GlobalAlignmentCache.cpp
@@ -15,11 +15,9 @@
 // Framework include files
 #include "DD4hep/LCDD.h"
 #include "DD4hep/Printout.h"
-#include "DD4hep/DetectorTools.h"
 #include "DDAlign/AlignmentCache.h"
 #include "DDAlign/AlignmentOperators.h"
 #include "DD4hep/objects/DetectorInterna.h"
-#include "DD4hep/objects/ConditionsInterna.h"
 
 // ROOT include files
 #include "TGeoManager.h"
@@ -73,6 +71,22 @@ int AlignmentCache::release()   {
   return value;
 }
 
+/// Create and install a new instance tree
+AlignmentCache* AlignmentCache::install(LCDD& lcdd)   {
+  AlignmentCache* cache = lcdd.extension<AlignmentCache>(false);
+  if ( !cache )  {
+    lcdd.addExtension<AlignmentCache>(new AlignmentCache(lcdd,"world",true));
+  }
+  return cache;
+}
+
+/// Unregister and delete a tree instance
+void AlignmentCache::uninstall(LCDD& lcdd)   {
+  if ( lcdd.extension<AlignmentCache>(false) )  {
+    lcdd.removeExtension<AlignmentCache>(true);
+  }
+}
+
 /// Add a new entry to the cache. The key is the placement path
 bool AlignmentCache::insert(GlobalAlignment alignment)  {
   TGeoPhysicalNode* pn = alignment.ptr();
@@ -149,48 +163,12 @@ vector<GlobalAlignment> AlignmentCache::matches(const string& match, bool exclud
   return result;
 }
 
-/// Open a new transaction stack (Note: only one stack allowed!)
-void AlignmentCache::openTransaction()   {
-  /// Check if transaction already present. If not, open, else issue an error
-  if ( !AlignmentStack::exists() )  {
-    AlignmentStack::create();
-    return;
-  }
-  string msg = "Request to open a second alignment transaction stack -- not allowed!";
-  string err = format("Alignment<alignment>",msg);
-  printout(FATAL,"AlignmentCache",msg);
-  throw runtime_error(err);
-}
-
 /// Close existing transaction stack and apply all alignments
-void AlignmentCache::closeTransaction()   {
-  /// Check if transaction is open. If yes, close it and apply alignment stack.
-  /// If no transaction is active, ignore the staement, but issue a warning.
-  if ( AlignmentStack::exists() )  {
-    TGeoManager& mgr = m_lcdd.manager();
-    mgr.UnlockGeometry();
-    apply(AlignmentStack::get());
-    AlignmentStack::get().release();
-    mgr.LockGeometry();
-    return;
-  }
-  printout(WARNING,"Alignment<alignment>",
-           "Request to close a non-existing alignmant transaction.");
-}
-
-/// Create and install a new instance tree
-void AlignmentCache::install(LCDD& lcdd)   {
-  AlignmentCache* cache = lcdd.extension<AlignmentCache>(false);
-  if ( !cache )  {
-    lcdd.addExtension<AlignmentCache>(new AlignmentCache(lcdd,"world",true));
-  }
-}
-
-/// Unregister and delete a tree instance
-void AlignmentCache::uninstall(LCDD& lcdd)   {
-  if ( lcdd.extension<AlignmentCache>(false) )  {
-    lcdd.removeExtension<AlignmentCache>(true);
-  }
+void AlignmentCache::commit(AlignmentStack& stack)   {
+  TGeoManager& mgr = m_lcdd.manager();
+  mgr.UnlockGeometry();
+  apply(stack);
+  mgr.LockGeometry();
 }
 
 /// Retrieve branch cache by name. If not present it will be created
diff --git a/DDAlign/src/AlignmentWriter.cpp b/DDAlign/src/GlobalAlignmentWriter.cpp
similarity index 99%
rename from DDAlign/src/AlignmentWriter.cpp
rename to DDAlign/src/GlobalAlignmentWriter.cpp
index e568a7f85..3b5aae1b6 100644
--- a/DDAlign/src/AlignmentWriter.cpp
+++ b/DDAlign/src/GlobalAlignmentWriter.cpp
@@ -40,6 +40,7 @@ AlignmentWriter::AlignmentWriter(LCDD& lcdd)
   : m_lcdd(lcdd)
 {
   m_cache = lcdd.extension<Alignments::AlignmentCache>();
+  if ( m_cache ) m_cache->addRef();
 }
 
 /// Standard destructor
diff --git a/DDAlign/src/plugins/AlignmentParser.cpp b/DDAlign/src/plugins/AlignmentParser.cpp
index 2bef0931d..932c5c588 100644
--- a/DDAlign/src/plugins/AlignmentParser.cpp
+++ b/DDAlign/src/plugins/AlignmentParser.cpp
@@ -26,7 +26,6 @@
 #include "DDAlign/AlignmentTags.h"
 #include "DDAlign/AlignmentStack.h"
 #include "DDAlign/AlignmentCache.h"
-#include "DDAlign/AlignmentTransaction.h"
 #include "DDAlign/GlobalDetectorAlignment.h"
 
 // C/C++ include files
@@ -67,6 +66,12 @@ using namespace DD4hep::Alignments;
 using DD4hep::Geometry::Position;
 using DD4hep::Geometry::Translation3D;
 
+/** Convert to enable/disable debugging.
+ *
+ *  @author  M.Frank
+ *  @version 1.0
+ *  @date    01/04/2014
+ */
 template <> void Converter<debug>::operator()(xml_h e) const {
   bool value = e.attr<bool>(_U(value));
   GlobalDetectorAlignment::debug(value);
@@ -190,6 +195,7 @@ typedef AlignmentStack::StackEntry StackEntry;
 template <> void Converter<volume>::operator()(xml_h e) const {
   Delta val;
   pair<DetElement,string>* elt = (pair<DetElement,string>*)param;
+  AlignmentStack* stack = _option<AlignmentStack>();
   string subpath    = e.attr<string>(_ALU(path));
   bool   reset      = e.hasAttr(_ALU(reset)) ? e.attr<bool>(_ALU(reset)) : true;
   bool   reset_dau  = e.hasAttr(_ALU(reset_children)) ? e.attr<bool>(_ALU(reset_children)) : true;
@@ -212,7 +218,7 @@ template <> void Converter<volume>::operator()(xml_h e) const {
   if ( check_val ) val.flags |= AlignmentStack::CHECKOVL_VALUE;
 
   dd4hep_ptr<StackEntry> entry(new StackEntry(elt->first,placement,val,ovl));
-  AlignmentStack::insert(entry);
+  stack->insert(entry);
   pair<DetElement,string> vol_param(elt->first,subpath);
   xml_coll_t(e,_U(volume)).for_each(Converter<volume>(lcdd,&vol_param));
 }
@@ -234,7 +240,8 @@ template <> void Converter<volume>::operator()(xml_h e) const {
  *  @date    01/04/2014
  */
 template <> void Converter<detelement>::operator()(xml_h e) const {
-  DetElement det   = param ? *(DetElement*)param : DetElement();
+  DetElement det(_param<DetElement::Object>());
+  AlignmentStack* stack = _option<AlignmentStack>();
   string path      = e.attr<string>(_ALU(path));
   bool   check     = e.hasAttr(_ALU(check_overlaps));
   bool   check_val = check ? e.attr<bool>(_ALU(check_overlaps)) : false;
@@ -270,12 +277,12 @@ template <> void Converter<detelement>::operator()(xml_h e) const {
            yes_no(reset), yes_no(reset_dau));
 
   dd4hep_ptr<StackEntry> entry(new StackEntry(elt,placement,val,ovl));
-  AlignmentStack::insert(entry);
+  stack->insert(entry);
 
   pair<DetElement,string> vol_param(elt,"");
-  xml_coll_t(e,_U(volume)).for_each(Converter<volume>(lcdd,&vol_param));
-  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(lcdd,&elt));
-  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(lcdd,&elt));
+  xml_coll_t(e,_U(volume)).for_each(Converter<volume>(lcdd,&vol_param,optional));
+  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(lcdd,elt.ptr(),optional));
+  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(lcdd,elt.ptr(),optional));
 }
 
 /** Convert detelement_include objects
@@ -294,11 +301,11 @@ template <> void Converter<include_file>::operator()(xml_h element) const {
   xml_h node = doc.root();
   string tag = node.tag();
   if ( tag == "alignment" )
-    Converter<alignment>(lcdd,param)(node);
+    Converter<alignment>(lcdd,param,optional)(node);
   else if ( tag == "detelement" )
-    Converter<detelement>(lcdd,param)(node);
+    Converter<detelement>(lcdd,param,optional)(node);
   else if ( tag == "subdetectors" || tag == "detelements" )
-    xml_coll_t(node,_ALU(detelements)).for_each(Converter<detelement>(lcdd,param));
+    xml_coll_t(node,_ALU(detelements)).for_each(Converter<detelement>(lcdd,param,optional));
   else
     throw runtime_error("Undefined tag name in XML structure:"+tag+" XML parsing abandoned.");
 }
@@ -316,15 +323,13 @@ template <> void Converter<include_file>::operator()(xml_h element) const {
  *  @date    01/04/2014
  */
 template <> void Converter<alignment>::operator()(xml_h e)  const  {
-  DetElement top = param ? *(DetElement*)param : lcdd.world();
-
   /// Now we process all allowed elements within the alignment tag:
   /// <detelement/>, <detelements/>, <subdetectors/> and <include/>
-  xml_coll_t(e,_ALU(debug)).for_each(Converter<debug>(lcdd,&top));
-  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(lcdd,&top));
-  xml_coll_t(e,_ALU(detelements)).for_each(_ALU(detelement),Converter<detelement>(lcdd,&top));
-  xml_coll_t(e,_ALU(subdetectors)).for_each(_ALU(detelement),Converter<detelement>(lcdd,&top));
-  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(lcdd,&top));
+  xml_coll_t(e,_ALU(debug)).for_each(Converter<debug>(lcdd,param,optional));
+  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(lcdd,param,optional));
+  xml_coll_t(e,_ALU(detelements)).for_each(_ALU(detelement),Converter<detelement>(lcdd,param,optional));
+  xml_coll_t(e,_ALU(subdetectors)).for_each(_ALU(detelement),Converter<detelement>(lcdd,param,optional));
+  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(lcdd,param,optional));
 }
 
 /** Basic entry point to read alignment files
@@ -333,12 +338,26 @@ template <> void Converter<alignment>::operator()(xml_h e)  const  {
  *  @version 1.0
  *  @date    01/04/2014
  */
-static long setup_Alignment(lcdd_t& lcdd, const xml_h& e) {{
-    static dd4hep_mutex_t s_mutex;
-    dd4hep_lock_t lock(s_mutex);
-    AlignmentCache::install(lcdd);
-    AlignmentTransaction tr(lcdd, e);
-    (DD4hep::Converter<DD4hep::alignment>(lcdd))(e);
+static long setup_Alignment(lcdd_t& lcdd, const xml_h& e) {
+  static dd4hep_mutex_t s_mutex;
+  dd4hep_lock_t lock(s_mutex);
+  bool open_trans = e.hasChild(_ALU(close_transaction));
+  bool close_trans = e.hasChild(_ALU(close_transaction));
+
+  AlignmentCache::install(lcdd);
+  /// Check if transaction already present. If not, open, else issue an error
+  if ( open_trans )   {
+    if ( AlignmentStack::exists() )  {
+      except("AlignmentCache","Request to open a second alignment transaction stack -- not allowed!");
+    }
+    AlignmentStack::create();
+  }
+  AlignmentStack& stack = AlignmentStack::get();
+  (DD4hep::Converter<DD4hep::alignment>(lcdd,lcdd.world().ptr(),&stack))(e);
+  if ( close_trans )  {
+    AlignmentCache* cache = lcdd.extension<Alignments::AlignmentCache>();
+    cache->commit(stack);
+    AlignmentStack::get().release();
   }
   if ( GlobalDetectorAlignment::debug() )  {
     xml_elt_t elt(e);
-- 
GitLab