From 685b359d62123a24499a0355fd1279485f8bbdb8 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Fri, 16 Mar 2018 21:44:49 +0100
Subject: [PATCH] Add study for new Velo-Pixel Detector element conditions.
 Include fixes from Chris Burr for alignment computations

---
 DDCore/src/AlignmentsCalculator.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/DDCore/src/AlignmentsCalculator.cpp b/DDCore/src/AlignmentsCalculator.cpp
index 71e2afa58..88cfede65 100644
--- a/DDCore/src/AlignmentsCalculator.cpp
+++ b/DDCore/src/AlignmentsCalculator.cpp
@@ -188,9 +188,23 @@ Result AlignmentsCalculator::compute(const std::map<DetElement, Delta>& deltas,
   Result  result;
   Calculator obj;
   Calculator::Context context(alignments);
+  // This is a tricky one. We absolutely need the detector elements ordered
+  // by their depth aka. the distance to /world.
+  // Unfortunately one cannot use the raw pointer of the DetElement here,
+  // But has to insert them in a map which is ordered by the DetElement path.
+  //
+  // Otherwise memory randomization gives us the wrong order and the
+  // corrections are calculated in the wrong order ie. not top -> down the
+  // hierarchy, but in "some" order depending on the pointer values!
+  //
+  std::map<DetElement,Delta,Calculator::Context::PathOrdering> ordered_deltas;
+
   for( const auto& i : deltas )
+    ordered_deltas.insert(i);
+  
+  for( const auto& i : ordered_deltas )
     context.insert(i.first, &(i.second));
-  for( const auto& i : deltas )
+  for( const auto& i : ordered_deltas )
     obj.resolve(context,i.first);
   for( auto& i : context.entries )
     result += obj.compute(context, i);
-- 
GitLab