DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignmentsPrinter.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework includes
15 #include "DD4hep/Printout.h"
16 #include "DD4hep/DetAlign.h"
19 
20 // C/C++ include files
21 #include <sstream>
22 
23 using std::string;
24 using std::stringstream;
25 using namespace DD4hep;
26 using namespace DD4hep::Alignments;
27 
29 AlignmentsPrinter::AlignmentsPrinter(const string& pref, int flg)
30  : AlignmentsProcessor(0), name("Alignment"), prefix(pref), m_flag(flg)
31 {
32 }
33 
36  printAlignment(name, a);
37  return 1;
38 }
39 
42  printContainer(name, container, m_pool);
43  return 1;
44 }
45 
48  printElement(name, de, m_pool);
49  return 1;
50 }
51 
52 
54 void DD4hep::Alignments::printAlignment(const string& prefix, Alignment a) {
55  if ( a.isValid() ) {
56  Alignment::Object* ptr = a.ptr();
57  const Alignment::Data& data = a.data();
58  Conditions::Condition cond = data.condition;
59  const Delta& D = data.delta;
60  string new_prefix = prefix;
61  new_prefix.assign(prefix.length(),' ');
62  printout(INFO,prefix,"++ %s \tPath:%s [%p] Typ:%s",
63  new_prefix.c_str(), cond.name(), a.ptr(),
64  typeName(typeid(*ptr)).c_str());
65  printout(INFO,prefix,"++ %s \tData:(%11s-%8s-%5s)",
66  new_prefix.c_str(),
67  D.hasTranslation() ? "Translation" : "",
68  D.hasRotation() ? "Rotation" : "",
69  D.hasPivot() ? "Pivot" : "");
70  printf("WorldTrafo: "); data.worldTrafo.Print();
71  printf("DetTrafo: "); data.detectorTrafo.Print();
72  }
73 }
74 
76 void DD4hep::Alignments::printContainer(const string& prefix, Container container, UserPool* pool) {
77  string tag = prefix+"Cont";
78  if ( pool ) {
79  for(const auto& k : container.keys() ) {
80  try {
81  Alignment align = container.get(k.first,*pool);
82  printout(INFO,tag,"++ %s Alignment [%08X] -> [%08X] %s",
83  prefix.c_str(), k.first, k.second.first, k.second.second.c_str());
84  printAlignment(prefix,align);
85  }
86  catch(...) {
87  printout(ERROR,tag,"++ %s %s [%08X] -> [%08X]",
88  prefix.c_str(), "FAILED Alignment:", k.first, k.second.first);
89  }
90  }
91  return;
92  }
93  except(tag,"Cannot dump alignments container without valid user-pool.");
94 }
95 
97 void DD4hep::Alignments::printElement(const string& prefix, DetElement de, UserPool* pool) {
98  string tag = prefix+"Element";
99  if ( de.isValid() ) {
100  if ( pool ) {
101  DetAlign a(de);
102  Container c = a.alignments();
103  printout(INFO,tag,"++ Alignments of DE %s [%d entries]",
104  de.path().c_str(), int(c.keys().size()));
105  printContainer(prefix, c, pool);
106  return;
107  }
108  except(tag,"Cannot process DetElement alignments from '%s' without valid user-pool",de.name());
109  }
110  except(tag,"Cannot process alignments of an invalid detector element");
111 }
112 
113 
114 #include "TClass.h"
115 #include "DD4hep/ToStream.h"
116 static string replace_all(const string& in, const string& from, const string& to) {
117  string res = in;
118  size_t idx;
119  while( string::npos != (idx=res.find(from)) )
120  res.replace(idx,from.length(),to);
121  return res;
122 }
123 static string _transformPoint2World(const Alignment::Data& data, const Position& local) {
124  char text[256];
125  Position world = data.localToWorld(local);
126  ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > World: (%7.3f , %7.3f , %7.3f )",
127  local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
128  return text;
129 }
130 
131 static string _transformPoint2Detector(const Alignment::Data& data, const Position& local) {
132  char text[256];
133  Position world = data.localToDetector(local);
134  ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > Parent: (%7.3f , %7.3f , %7.3f )",
135  local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
136  return text;
137 }
138 
139 static void printAlignmentEx(const string& prefix, const string& opt, DetElement de, Alignment alignment) {
140  using Geometry::Box;
141  DetAlign a(de);
142  const string& tag = prefix;
143  const Alignment::Data& align_data = alignment.data();
144  Conditions::Condition align_cond = align_data.condition;
145  const Delta& align_delta = align_data.delta;
146  string par = de.parent().isValid() ? de.parent().path() : string();
147  Box bbox = de.placement().volume().solid();
149  Position p1( bbox.x(), bbox.y(), bbox.z());
150  Position p2( bbox.x(),-bbox.y(), bbox.z());
151  Position p3(-bbox.x(), bbox.y(), bbox.z());
152  Position p4(-bbox.x(),-bbox.y(), bbox.z());
153  Position p5( bbox.x(), bbox.y(),-bbox.z());
154  Position p6( bbox.x(),-bbox.y(),-bbox.z());
155  Position p7(-bbox.x(), bbox.y(),-bbox.z());
156  Position p8(-bbox.x(),-bbox.y(),-bbox.z());
157 
158  if ( align_cond.isValid() ) {
159  printout(INFO,tag,"++ %s DATA: (%11s-%8s-%5s) %p IOV:%s", opt.c_str(),
160  align_delta.hasTranslation() ? "Translation" : "",
161  align_delta.hasRotation() ? "Rotation" : "",
162  align_delta.hasPivot() ? "Pivot" : "",
163  alignment.ptr(),
164  align_cond.iov().str().c_str());
165  }
166  else {
167  printout(INFO,tag,"++ %s DATA: (%11s-%8s-%5s) %p", opt.c_str(),
168  align_delta.hasTranslation() ? "Translation" : "",
169  align_delta.hasRotation() ? "Rotation" : "",
170  align_delta.hasPivot() ? "Pivot" : "",
171  alignment.ptr());
172  }
173  if ( align_delta.hasTranslation() ) {
174  stringstream str;
175  Utils::toStream(align_delta.translation, str);
176  printout(INFO,tag,"++ %s DELTA Translation: %s", opt.c_str(), replace_all(str.str(),"\n","").c_str());
177  }
178  if ( align_delta.hasPivot() ) {
179  stringstream str;
180  Utils::toStream(align_delta.pivot, str);
181  string res = replace_all(str.str(),"\n","");
182  res = "( "+replace_all(res," "," , ")+" )";
183  printout(INFO,tag,"++ %s DELTA Pivot: %s", opt.c_str(), res.c_str());
184  }
185  if ( align_delta.hasRotation() ) {
186  stringstream str;
187  Utils::toStream(align_delta.rotation, str);
188  printout(INFO,tag,"++ %s DELTA Rotation: %s", opt.c_str(), replace_all(str.str(),"\n","").c_str());
189  }
190  printf("%s %s WorldTrafo (to %s): ",opt.c_str(), tag.c_str(), de.world().path().c_str());
191  align_data.worldTrafo.Print();
192  printf("%s %s DetTrafo (to %s): ",opt.c_str(), tag.c_str(), par.c_str());
193  align_data.detectorTrafo.Print();
194 
195  printout(INFO,tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p1).c_str());
196  printout(INFO,tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p2).c_str());
197  printout(INFO,tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p3).c_str());
198  printout(INFO,tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p4).c_str());
199  printout(INFO,tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p5).c_str());
200  printout(INFO,tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p6).c_str());
201  printout(INFO,tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p7).c_str());
202  printout(INFO,tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p8).c_str());
203 
204  printout(INFO,tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p1).c_str());
205  printout(INFO,tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p2).c_str());
206  printout(INFO,tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p3).c_str());
207  printout(INFO,tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p4).c_str());
208  printout(INFO,tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p5).c_str());
209  printout(INFO,tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p6).c_str());
210  printout(INFO,tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p7).c_str());
211  printout(INFO,tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p8).c_str());
212 }
213 
215 void DD4hep::Alignments::printElementPlacement(const string& prefix, DetElement de, UserPool* pool) {
216  using Geometry::Box;
217  using Geometry::Solid;
218  using Geometry::Volume;
220  string tag = prefix+"Element";
221  if ( de.isValid() ) {
222  if ( pool ) {
223  char text[132];
224  DetAlign a(de);
225  Container container = a.alignments();
226  Alignment nominal = de.nominal();
227  Box bbox = de.placement().volume().solid();
228  ::memset(text,'=',sizeof(text));
229  text[sizeof(text)-1] = 0;
230  printout(INFO,tag,text);
231  printout(INFO,tag,"++ Alignments of DE %s [%d entries]",
232  de.path().c_str(), int(container.keys().size()));
233  printout(INFO,tag,"++ Volume: %s BBox: x=%7.3f y=%7.3f z=%7.3f",bbox.type(),bbox.x(),bbox.y(),bbox.z());
234  printAlignmentEx(tag,"NOMINAL",de,nominal);
235 
236  for(const auto& k : container.keys() ) {
237  try {
238  Alignment align = container.get(k.first,*pool);
239  const Alignment::Data& align_data = align.data();
240  Conditions::Condition align_cond = align_data.condition;
241  if ( k.first != k.second.first ) {
242  printout(INFO,tag,"++ Alignment %p [%08X] -> [%08X] %s (SYNONYM) ignored.",
243  a.ptr(), k.first, k.second.first, k.second.second.c_str());
244  continue;
245  }
246  printout(INFO,tag,"++ Alignment %p [%08X] -> [%08X] %s",
247  a.ptr(), k.first, k.second.first, k.second.second.c_str());
248  if ( k.second.second != align_cond.name() ) {
249  printout(INFO,prefix,"++ \tPath:%s [%p]", align_cond.name(), a.ptr());
250  }
251  printAlignmentEx(tag,"ALIGNMENT",de,align);
252  }
253  catch(...) {
254  printout(ERROR,tag,"++ %s %s [%08X] -> [%08X]",
255  prefix.c_str(), "FAILED Alignment:", k.first, k.second.first);
256  }
257  }
258  return;
259  }
260  except(tag,"Cannot process DetElement alignments from '%s' without valid user-pool",de.name());
261  }
262  except(tag,"Cannot process alignments of an invalid detector element");
263 }
bool hasRotation() const
Access flags: Check if the delta operation contains a rotation.
Definition: AlignmentData.h:96
static string _transformPoint2World(const Alignment::Data &data, const Position &local)
void printContainer(const std::string &prefix, Container container, UserPool *pool)
Default printout of a container entry.
AlignmentsPrinter(const std::string &prefix="", int flags=0)
Initializing constructor.
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:135
Solid solid() const
Access to Solid (Shape)
Definition: Volumes.cpp:693
Volume volume() const
Logical volume of this placement.
Definition: Volumes.cpp:389
TGeoHMatrix detectorTrafo
Intermediate buffer to store the transformation to the parent detector element.
DetElement world() const
Access to the world object. Only possible once the geometry is closed.
Definition: Detector.cpp:228
double z() const
Access half "depth" of the box.
Definition: Shapes.cpp:205
Ref_t condition
Reference to the original condition object (May not be present!)
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
pool_type * m_pool
Reference to the user pool.
bool hasPivot() const
Access flags: Check if the delta operation contains a pivot.
Definition: AlignmentData.h:98
static string _transformPoint2Detector(const Alignment::Data &data, const Position &local)
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
Main handle class to hold an alignment object.
Definition: Alignments.h:65
Main condition object handle.
Definition: Conditions.h:70
std::string name
Printer name. Want to know who is printing what.
Class describing a box shape.
Definition: Shapes.h:120
Generic Alignments processor.
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
T * ptr() const
Access to the held object.
Definition: Handle.h:149
DD4hep::Geometry::DetElement DetElement
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:237
static string replace_all(const string &in, const string &from, const string &to)
const Keys & keys() const
Known keys of conditions in this container.
Definition: Alignments.cpp:132
Delta delta
Alignment changes.
virtual int operator()(Alignment cond)
Callback to output alignments information.
const Transform3D & localToWorld() const
Access the currently applied alignment/placement matrix.
void printElement(const std::string &prefix, DetElement element, UserPool *pool)
Default printout of a detector element entry.
void printAlignment(const std::string &prefix, Alignment alignment)
Default printout of an alignment entry.
Class describing an condition to re-adjust an alignment.
Definition: AlignmentData.h:49
ROOT::Math::XYZVector Position
Definition: Objects.h:75
Data & data()
Data accessor for the use of decorators.
Definition: Alignments.h:117
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: Detector.cpp:160
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: Detector.cpp:279
Solid_type< TGeoShape > Solid
Definition: Shapes.h:108
static void printAlignmentEx(const string &prefix, const string &opt, DetElement de, Alignment alignment)
const iov_type & iov() const
Access the IOV block.
Definition: Conditions.cpp:102
Container alignments() const
Access to the alignments information.
Definition: DetAlign.cpp:45
DetElement parent() const
Access to the detector elements's parent.
Definition: Detector.cpp:222
double x() const
Access half "length" of the box.
Definition: Shapes.cpp:195
std::ostream & toStream(const Property &result, std::ostream &os)
Handle class describing a detector element.
Definition: Detector.h:172
Handle class describing the access to DetElement dependent alignments.
Definition: DetAlign.h:42
Container class for alignment handles aggregated by a detector element.
Definition: Alignments.h:206
void printElementPlacement(const std::string &prefix, DetElement detector, UserPool *pool)
PrintElement placement with/without alignment applied.
virtual int processElement(DetElement de)
Callback to output alignments information of an entire DetElement.
double y() const
Access half "width" of the box.
Definition: Shapes.cpp:200
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
bool hasTranslation() const
Access flags: Check if the delta operation contains a translation.
Definition: AlignmentData.h:94
Derived condition data-object definition.
void localToDetector(const Position &local, Position &detector) const
Transformation from local coordinates of the placed volume to the detector system.
Alignment get(const std::string &alignment_key, const iov_type &iov)
Access to alignment objects by key and IOV.
Definition: Alignments.cpp:141
const char * type() const
Access to shape type (The TClass name of the ROOT implementation)
Definition: Shapes.cpp:176
TGeoHMatrix worldTrafo
Intermediate buffer to store the transformation to the world coordination system. ...
std::string str() const
Create string representation of the IOV.
Definition: IOV.cpp:126