From d1537e727cb51b336369f0318f236d9365330bf3 Mon Sep 17 00:00:00 2001
From: Markus Frank <markus.frank@cern.ch>
Date: Thu, 9 Oct 2014 21:53:24 +0000
Subject: [PATCH] Fix bug in DetElement transformations - test example with
 boxes

---
 examples/ClientTests/compact/BoxTrafos.xml |  50 +++++++++
 examples/ClientTests/scripts/BoxTrafos.C   | 118 +++++++++++++++++++++
 2 files changed, 168 insertions(+)
 create mode 100644 examples/ClientTests/compact/BoxTrafos.xml
 create mode 100644 examples/ClientTests/scripts/BoxTrafos.C

diff --git a/examples/ClientTests/compact/BoxTrafos.xml b/examples/ClientTests/compact/BoxTrafos.xml
new file mode 100644
index 000000000..58d41b104
--- /dev/null
+++ b/examples/ClientTests/compact/BoxTrafos.xml
@@ -0,0 +1,50 @@
+<lccdd xmlns:compact="http://www.lcsim.org/schemas/compact/1.0" 
+       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
+       xs:noNamespaceSchemaLocation="http://www.lcsim.org/schemas/compact/1.0/compact.xsd">
+  
+  <info name="alignment_boxes"
+	title="Alignment test with 2 simple boxes"
+	author="Markus Frank"
+	url="http://www.cern.ch/lhcb"
+	status="development"
+	version="$Id: compact.xml 513 2013-04-05 14:31:53Z gaede $">
+    <comment>Alignment test with 2 simple boxes</comment>        
+  </info>
+  
+  <includes>
+    <gdmlFile  ref="elements.xml"/>
+    <gdmlFile  ref="materials.xml"/>
+  </includes>
+  
+  <define>
+    <constant name="world_side" value="30000"/>
+    <constant name="world_x" value="world_side"/>
+    <constant name="world_y" value="world_side"/>
+    <constant name="world_z" value="world_side"/>
+  </define>
+
+  <display>
+    <vis name="InvisibleNoDaughters"      showDaughters="false" visible="false"/>
+    <vis name="InvisibleWithDaughters"    showDaughters="true" visible="false"/>
+    <vis name="B1_vis" alpha="1.0" r="1" g="0" b="0" showDaughters="true" visible="true"/>
+    <vis name="B2_vis" alpha="1.0" r="0" g="1" b="0" showDaughters="true" visible="true"/>
+  </display>
+
+  <detectors>
+    <comment>Boxes</comment>
+    <detector id="3" name="B3" type="BoxSegment" vis="B2_vis">
+      <comment>Vertical box</comment>
+      <material name="Steel235"/>
+      <box      x="10"  y="20"   z="30"/>
+      <position x="-10" y="30"   z="10"/>
+      <rotation x="0"   y="0"    z="0"/>
+    </detector>
+    <detector id="3" name="B4" type="BoxSegment" vis="B1_vis">
+      <comment>Vertical box</comment>
+      <material name="Steel235"/>
+      <box      x="10"  y="20"   z="30"/>
+      <position x="0"   y="0"    z="0"/>
+      <rotation x="0"   y="0"    z="0"/>
+    </detector>
+  </detectors>
+</lccdd>
diff --git a/examples/ClientTests/scripts/BoxTrafos.C b/examples/ClientTests/scripts/BoxTrafos.C
new file mode 100644
index 000000000..e60255739
--- /dev/null
+++ b/examples/ClientTests/scripts/BoxTrafos.C
@@ -0,0 +1,118 @@
+namespace {
+  struct Loader {  Loader() { gSystem->Load("libDD4hepCore"); }  } _load;
+}
+
+using namespace DD4hep::Geometry;
+
+void printPos(const char* com, const Position& p)  {
+  printf("%-24s:  %7.3f %7.3f %7.3f\n",com,p.x(),p.y(),p.z());
+}
+
+void printCoord(const Position& global, const Position& local)  {
+  printPos("Global",global);
+  printPos("Local",local);
+}
+
+void local_to_world(const char* com,const DetElement de, const Position& pos,const Position& loc)  {
+  Position glob;
+  printf("Local to World: Transformation '%s'(%7.3f, %7.3f, %7.3f)->world (Should be: (%7.3f, %7.3f, %7.3f) :\n",
+	 com, loc.x(),loc.y(),loc.z(),
+	 loc.x()+pos.x(),loc.y()+pos.y(),loc.z()+pos.z() );
+  de.localToWorld(loc,glob);
+  printCoord(glob,loc);
+}
+
+void world_to_local(const char* com,const DetElement de, const Position& pos,const Position& glob)  {
+  Position loc;
+  printf("World to Local: Transformation '%s'(%7.3f, %7.3f, %7.3f)->world (Should be: (%7.3f, %7.3f, %7.3f) :\n",
+	 com, glob.x(),glob.y(),glob.z(),
+	 glob.x()-pos.x(),glob.y()-pos.y(),glob.z()-pos.z() );
+  de.worldToLocal(glob,loc);
+  printCoord(glob,loc);
+}
+
+int BoxTrafos()  {
+  string xml = "file:";
+  xml += gSystem->Getenv("DD4hepINSTALL");
+  xml += "/examples/ClientTests/compact/BoxTrafos.xml";
+  const char* argv[] = {xml.c_str(), "BUILD_DEFAULT", 0};
+
+  gSystem->Load("libDD4hepCore");
+  LCDD& lcdd = LCDD::getInstance();
+  lcdd.apply("DD4hepCompactLoader",2,(char**)argv);
+  lcdd.apply("DD4hepGeometryDisplay",0,0);
+
+
+  DetElement de = lcdd.detector("B3");
+  PlacedVolume pv = de.placement();
+  Volume vol = pv.volume();
+  Solid solid = vol.solid();
+  TGeoBBox* box = (TGeoBBox*)(solid.ptr());
+  Position glob,loc, pos(-10,30,10);
+
+  printf("\n++++  local->world:\n\n");
+
+  loc = Position(-pos.x(),-pos.y(),-pos.z());
+  local_to_world("origine",de,pos,loc);
+
+  loc = Position();
+  local_to_world("center",de,pos,loc);
+
+  loc = Position(box->GetDX(),box->GetDY(),box->GetDZ());
+  local_to_world("top edge",de,pos,loc);
+
+  loc = Position(box->GetDX(),box->GetDY(),-box->GetDZ());
+  local_to_world("top edge",de,pos,loc);
+
+  loc = Position(-box->GetDX(),box->GetDY(),box->GetDZ());
+  local_to_world("top edge",de,pos,loc);
+
+  loc = Position(-box->GetDX(),box->GetDY(),-box->GetDZ());
+  local_to_world("top edge",de,pos,loc);
+
+  loc = Position(box->GetDX(),-box->GetDY(),box->GetDZ());
+  local_to_world("bottom edge",de,pos,loc);
+
+  loc = Position(box->GetDX(),-box->GetDY(),-box->GetDZ());
+  local_to_world("bottom edge",de,pos,loc);
+
+  loc = Position(-box->GetDX(),-box->GetDY(),box->GetDZ());
+  local_to_world("bottom edge",de,pos,loc);
+
+  loc = Position(-box->GetDX(),-box->GetDY(),-box->GetDZ());
+  local_to_world("bottom edge",de,pos,loc);
+
+  printf("\n++++  world->local:\n\n");
+
+  glob = Position(0,0,0);
+  world_to_local("world center",de,pos,glob);
+
+  glob = Position(pos.x(),pos.y(),pos.z());
+  world_to_local("position",de,pos,glob);
+
+  glob = Position( box->GetDX()+pos.x(),  box->GetDY()+pos.y(),  box->GetDZ()+pos.z());
+  world_to_local("top edge",de,pos,glob);
+
+  glob = Position( box->GetDX()+pos.x(),  box->GetDY()+pos.y(), -box->GetDZ()+pos.z());
+  world_to_local("top edge",de,pos,glob);
+
+  glob = Position(-box->GetDX()+pos.x(),  box->GetDY()+pos.y(),  box->GetDZ()+pos.z());
+  world_to_local("top edge",de,pos,glob);
+
+  glob = Position(-box->GetDX()+pos.x(),  box->GetDY()+pos.y(), -box->GetDZ()+pos.z());
+  world_to_local("top edge",de,pos,glob);
+
+  glob = Position( box->GetDX()+pos.x(), -box->GetDY()+pos.y(),  box->GetDZ()+pos.z());
+  world_to_local("bottom edge",de,pos,glob);
+
+  glob = Position( box->GetDX()+pos.x(), -box->GetDY()+pos.y(), -box->GetDZ()+pos.z());
+  world_to_local("bottom edge",de,pos,glob);
+
+  glob = Position(-box->GetDX()+pos.x(), -box->GetDY()+pos.y(),  box->GetDZ()+pos.z());
+  world_to_local("bottom edge",de,pos,glob);
+
+  glob = Position(-box->GetDX()+pos.x(), -box->GetDY()+pos.y(), -box->GetDZ()+pos.z());
+  world_to_local("bottom edge",de,pos,glob);
+
+  return 1;
+}
-- 
GitLab