DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
AlignmentsPrinter.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
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 <Parsers/Parsers.h>
16 #include <DD4hep/Printout.h>
20 #include <TClass.h>
21 
22 // C/C++ include files
23 #include <sstream>
24 
25 using namespace dd4hep;
26 using namespace dd4hep::align;
27 
29 AlignmentsPrinter::AlignmentsPrinter(ConditionsMap* cond_map, const std::string& pref, int flg)
30  : mapping(cond_map), name("Alignment"), prefix(pref), printLevel(INFO), m_flag(flg)
31 {
32 }
33 
37  return 1;
38 }
39 
41 int AlignmentsPrinter::operator()(DetElement de, int level) const {
42  if ( mapping ) {
43  std::vector<Alignment> alignments;
44  alignmentsCollector(*mapping,alignments)(de, level);
45  printout(printLevel, name, "++ %s %-3ld Alignments for DE %s",
46  prefix.c_str(), alignments.size(), de.path().c_str());
47  for( auto alignment : alignments )
48  (*this)(alignment);
49  return int(alignments.size());
50  }
51  except(name,"Failed to dump conditions for DetElement:%s [No slice availible]",
52  de.path().c_str());
53  return 0;
54 }
55 
57 AlignedVolumePrinter::AlignedVolumePrinter(ConditionsMap* cond_map, const std::string& pref,int flg)
58  : AlignmentsPrinter(cond_map, pref, flg)
59 {
60  name = "Alignment";
61 }
62 
66  return 1;
67 }
68 
70 void dd4hep::align::printAlignment(PrintLevel lvl, const std::string& prefix, Alignment a) {
71  if ( a.isValid() ) {
72  Alignment::Object* ptr = a.ptr();
73  const AlignmentData& data = a.data();
74  const Delta& D = data.delta;
75  std::string new_prefix = prefix;
76  new_prefix.assign(prefix.length(),' ');
77  printout(lvl,prefix,"++ %s \t [%p] Typ:%s",
78  new_prefix.c_str(), a.ptr(),
79  typeName(typeid(*ptr)).c_str());
80  printout(lvl,prefix,"++ %s \tData:(%11s-%8s-%5s)",
81  new_prefix.c_str(),
82  D.hasTranslation() ? "Translation" : "",
83  D.hasRotation() ? "Rotation" : "",
84  D.hasPivot() ? "Pivot" : "");
85  if ( isActivePrintLevel(lvl) ) {
86  printf("WorldTrafo: "); data.worldTrafo.Print();
87  printf("DetTrafo: "); data.detectorTrafo.Print();
88  }
89  }
90 }
91 
92 static std::string replace_all(const std::string& in, const std::string& from, const std::string& to) {
93  std::string res = in;
94  std::size_t idx;
95  while( std::string::npos != (idx=res.find(from)) )
96  res.replace(idx,from.length(),to);
97  return res;
98 }
99 static std::string _transformPoint2World(const AlignmentData& data, const Position& local) {
100  char text[256];
101  Position world = data.localToWorld(local);
102  ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > World: (%7.3f , %7.3f , %7.3f )",
103  local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
104  return text;
105 }
106 
107 static std::string _transformPoint2Detector(const AlignmentData& data, const Position& local) {
108  char text[256];
109  Position world = data.localToDetector(local);
110  ::snprintf(text,sizeof(text),"Local: (%7.3f , %7.3f , %7.3f ) -- > Parent: (%7.3f , %7.3f , %7.3f )",
111  local.x(), local.y(), local.z(), world.x(), world.y(), world.z());
112  return text;
113 }
114 
115 void dd4hep::align::printAlignment(PrintLevel lvl, const std::string& prefix,
116  const std::string& opt, DetElement de, Alignment alignment)
117 {
118  const std::string& tag = prefix;
119  const AlignmentData& align_data = alignment.data();
120  Condition align_cond;// = align_data.condition;
121  const Delta& align_delta = align_data.delta;
122  std::string par = de.parent().isValid() ? de.parent().path() : std::string();
123  Box bbox = de.placement().volume().solid();
125  Position p1( bbox.x(), bbox.y(), bbox.z());
126  Position p2( bbox.x(),-bbox.y(), bbox.z());
127  Position p3(-bbox.x(), bbox.y(), bbox.z());
128  Position p4(-bbox.x(),-bbox.y(), bbox.z());
129  Position p5( bbox.x(), bbox.y(),-bbox.z());
130  Position p6( bbox.x(),-bbox.y(),-bbox.z());
131  Position p7(-bbox.x(), bbox.y(),-bbox.z());
132  Position p8(-bbox.x(),-bbox.y(),-bbox.z());
133 
134  if ( align_cond.isValid() ) {
135  printout(lvl,tag,"++ %s DATA: (%11s-%8s-%5s) %p IOV:%s", opt.c_str(),
136  align_delta.hasTranslation() ? "Translation" : "",
137  align_delta.hasRotation() ? "Rotation" : "",
138  align_delta.hasPivot() ? "Pivot" : "",
139  alignment.ptr(),
140  align_cond.iov().str().c_str());
141  }
142  else {
143  printout(lvl,tag,"++ %s DATA: (%11s-%8s-%5s) %p", opt.c_str(),
144  align_delta.hasTranslation() ? "Translation" : "",
145  align_delta.hasRotation() ? "Rotation" : "",
146  align_delta.hasPivot() ? "Pivot" : "",
147  alignment.ptr());
148  }
149  if ( align_delta.hasTranslation() ) {
150  std::stringstream str;
151  Position copy(align_delta.translation * (1./dd4hep::cm));
152  Parsers::toStream(copy, str);
153  printout(lvl,tag,"++ %s DELTA Translation: %s [cm]",
154  opt.c_str(), replace_all(str.str(),"\n","").c_str());
155  }
156  if ( align_delta.hasPivot() ) {
157  std::stringstream str;
158  Delta::Pivot copy(align_delta.pivot.Vect() * (1./dd4hep::cm));
159  Parsers::toStream(copy, str);
160  std::string res = replace_all(str.str(),"\n","");
161  res = "( "+replace_all(res," "," , ")+" )";
162  printout(lvl,tag,"++ %s DELTA Pivot: %s [cm]", opt.c_str(), res.c_str());
163  }
164  if ( align_delta.hasRotation() ) {
165  std::stringstream str;
166  Parsers::toStream(align_delta.rotation, str);
167  printout(lvl,tag,"++ %s DELTA Rotation: %s [rad]", opt.c_str(), replace_all(str.str(),"\n","").c_str());
168  }
169  if ( isActivePrintLevel(lvl) ) {
170  printf("%s %s WorldTrafo (to %s): ",opt.c_str(), tag.c_str(), de.world().path().c_str());
171  align_data.worldTrafo.Print();
172  printf("%s %s DetTrafo (to %s): ",opt.c_str(), tag.c_str(), par.c_str());
173  align_data.detectorTrafo.Print();
174  }
175  printout(PrintLevel(lvl-1),tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p1).c_str());
176  printout(PrintLevel(lvl-1),tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p2).c_str());
177  printout(PrintLevel(lvl-1),tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p3).c_str());
178  printout(PrintLevel(lvl-1),tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p4).c_str());
179  printout(PrintLevel(lvl-1),tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p5).c_str());
180  printout(PrintLevel(lvl-1),tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p6).c_str());
181  printout(PrintLevel(lvl-1),tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p7).c_str());
182  printout(PrintLevel(lvl-1),tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2World(align_data, p8).c_str());
183 
184  printout(PrintLevel(lvl-1),tag,"++ %s: P1(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p1).c_str());
185  printout(PrintLevel(lvl-1),tag,"++ %s: P2(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p2).c_str());
186  printout(PrintLevel(lvl-1),tag,"++ %s: P3(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p3).c_str());
187  printout(PrintLevel(lvl-1),tag,"++ %s: P4(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p4).c_str());
188  printout(PrintLevel(lvl-1),tag,"++ %s: P5(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p5).c_str());
189  printout(PrintLevel(lvl-1),tag,"++ %s: P6(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p6).c_str());
190  printout(PrintLevel(lvl-1),tag,"++ %s: P7(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p7).c_str());
191  printout(PrintLevel(lvl-1),tag,"++ %s: P8(x,y,z) %s", opt.c_str(), _transformPoint2Detector(align_data, p8).c_str());
192 }
193 
195 void dd4hep::align::printElement(PrintLevel prt_level, const std::string& prefix, DetElement de, ConditionsMap& pool) {
196  std::string tag = prefix+"Element";
197  if ( de.isValid() ) {
198  std::vector<Alignment> alignments;
199  alignmentsCollector(pool,alignments)(de);
200  printout(prt_level,tag,"++ Alignments of DE %s [%d entries]",
201  de.path().c_str(), int(alignments.size()));
202  for(const auto& align : alignments )
203  printAlignment(prt_level, prefix, align);
204  return;
205  }
206  except(tag,"Cannot process alignments of an invalid detector element");
207 }
208 
210 void dd4hep::align::printElementPlacement(PrintLevel lvl, const std::string& prefix, DetElement de, ConditionsMap& pool) {
211  std::string tag = prefix+"Element";
212  if ( de.isValid() ) {
213  char text[132];
214  Alignment nominal = de.nominal();
215  Box bbox = de.placement().volume().solid();
216  std::vector<Alignment> alignments;
217 
218  alignmentsCollector(pool,alignments)(de);
219  ::memset(text,'=',sizeof(text));
220  text[sizeof(text)-1] = 0;
221  printout(lvl, tag, text);
222  printout(lvl, tag, "++ Alignments of DE %s [%d entries]",
223  de.path().c_str(), int(alignments.size()));
224  printout(lvl, tag, "++ Volume: %s BBox: x=%7.3f y=%7.3f z=%7.3f",
225  bbox.type(), bbox.x(), bbox.y(), bbox.z());
226  printAlignment(lvl, tag, "NOMINAL", de, nominal);
227 
228  for(const auto& align : alignments ) {
229  AlignmentCondition cond(align);
230  try {
231  printout(lvl, tag, "++ Alignment %p [%16llX]", align.ptr(), cond.key());
232  printout(lvl, prefix, "++ \tPath:%s [%p]", cond.name(), align.ptr());
233  printAlignment(lvl, tag, "ALIGNMENT", de, align);
234  }
235  catch(...) {
236  printout(ERROR, tag, "++ %s %s [%16llX]",
237  prefix.c_str(), "FAILED Alignment:", cond.key());
238  }
239  }
240  return;
241  }
242  except(tag, "Cannot process alignments of an invalid detector element");
243 }
dd4hep::DetElement::path
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: DetElement.cpp:158
cond
AlignmentCondition::Object * cond
Definition: AlignmentsCalculator.cpp:68
dd4hep::align::printAlignment
void printAlignment(PrintLevel prt_level, const std::string &prefix, Alignment alignment)
Default printout of an alignment entry.
Definition: AlignmentsPrinter.cpp:70
dd4hep::DetElement::parent
DetElement parent() const
Access to the detector elements's parent.
Definition: DetElement.cpp:239
dd4hep::align::AlignmentsPrinter::name
std::string name
Printer name. Want to know who is printing what.
Definition: AlignmentsPrinter.h:43
dd4hep::align::alignmentsCollector
AlignmentsCollector< typename std::remove_reference< T >::type > alignmentsCollector(ConditionsMap &m, T &&alignments)
Creator function for alignment collector objects.
Definition: AlignmentsProcessor.h:193
dd4hep::Alignment::data
AlignmentData & data()
Data accessor for the use of decorators.
Definition: Alignments.cpp:53
AlignmentsPrinter.h
dd4hep::AlignmentCondition
Main handle class to hold an alignment conditions object.
Definition: Alignments.h:68
dd4hep::Box::x
double x() const
Access half "length" of the box.
Definition: Shapes.cpp:155
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
dd4hep::Delta::rotation
RotationZYX rotation
Definition: AlignmentData.h:43
dd4hep::AlignmentData::localToDetector
void localToDetector(const Position &local, Position &detector) const
Transformation from local coordinates of the placed volume to the detector system.
Definition: AlignmentData.cpp:194
dd4hep::align::AlignedVolumePrinter::operator()
virtual int operator()(DetElement de, int level) const override
Callback to output alignments information of an entire DetElement.
Definition: AlignmentsPrinter.h:90
dd4hep::Volume::solid
Solid solid() const
Access to Solid (Shape)
Definition: Volumes.cpp:1223
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::align::AlignmentsPrinter::printLevel
PrintLevel printLevel
Printout level.
Definition: AlignmentsPrinter.h:47
dd4hep::IOV::str
std::string str() const
Create string representation of the IOV.
Definition: IOV.cpp:141
dd4hep::Delta::hasRotation
bool hasRotation() const
Access flags: Check if the delta operation contains a rotation.
Definition: AlignmentData.h:85
dd4hep::Box::z
double z() const
Access half "depth" of the box.
Definition: Shapes.cpp:165
dd4hep::Box::y
double y() const
Access half "width" of the box.
Definition: Shapes.cpp:160
dd4hep::Delta
Class describing an condition to re-adjust an alignment.
Definition: AlignmentData.h:38
dd4hep::Delta::hasPivot
bool hasPivot() const
Access flags: Check if the delta operation contains a pivot.
Definition: AlignmentData.h:87
dd4hep::align::AlignmentsPrinter::AlignmentsPrinter
AlignmentsPrinter()=delete
No default constructor.
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Delta::translation
Position translation
Definition: AlignmentData.h:41
dd4hep::align::AlignmentsPrinter::prefix
std::string prefix
Printout prefix.
Definition: AlignmentsPrinter.h:45
dd4hep::AlignmentData::delta
Delta delta
Alignment changes.
Definition: AlignmentData.h:116
dd4hep::Delta::pivot
Pivot pivot
Definition: AlignmentData.h:42
dd4hep::Parsers::toStream
std::ostream & toStream(const Property &result, std::ostream &os)
Definition: ComponentProperties.cpp:191
dd4hep::Alignment
Main handle class to hold an alignment object.
Definition: Alignments.h:115
dd4hep::AlignmentData
Derived condition data-object definition.
Definition: AlignmentData.h:98
dd4hep::DetElement::nominal
Alignment nominal() const
Access to the constant ideal (nominal) alignment information.
Definition: DetElement.cpp:185
dd4hep::AlignmentData::localToWorld
const Transform3D & localToWorld() const
Access the currently applied alignment/placement matrix.
Definition: AlignmentData.h:152
dd4hep::AlignmentData::detectorTrafo
TGeoHMatrix detectorTrafo
Intermediate buffer to store the transformation to the parent detector element.
Definition: AlignmentData.h:120
dd4hep::Delta::hasTranslation
bool hasTranslation() const
Access flags: Check if the delta operation contains a translation.
Definition: AlignmentData.h:83
dd4hep::AlignmentData::worldTrafo
TGeoHMatrix worldTrafo
Intermediate buffer to store the transformation to the world coordination system.
Definition: AlignmentData.h:118
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
dd4hep::align::AlignedVolumePrinter::AlignedVolumePrinter
AlignedVolumePrinter()=delete
No default constructor.
AlignmentsProcessor.h
dd4hep::Box
Class describing a box shape.
Definition: Shapes.h:294
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
mapping
ConditionsMap & mapping
Definition: AlignmentsCalculator.cpp:82
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::align::AlignmentsPrinter::mapping
ConditionsMap * mapping
Conditionsmap to resolve things.
Definition: AlignmentsPrinter.h:41
dd4hep::Solid_type::type
const char * type() const
Access to shape type (The TClass name of the ROOT implementation)
Definition: Shapes.cpp:82
dd4hep::PlacedVolume::volume
Volume volume() const
Logical volume of this placement.
Definition: Volumes.cpp:452
dd4hep::align::AlignmentsPrinter::operator()
virtual int operator()(DetElement de, int level) const
Callback to output alignments information of an entire DetElement.
Definition: AlignmentsPrinter.cpp:41
dd4hep::align::printElementPlacement
void printElementPlacement(PrintLevel prt_level, const std::string &prefix, DetElement detector, ConditionsMap &pool)
PrintElement placement with/without alignment applied.
Definition: AlignmentsPrinter.cpp:210
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::Delta::Pivot
Translation3D Pivot
Definition: AlignmentData.h:40
AlignmentsInterna.h
dd4hep::align::AlignmentsPrinter
Generic Alignments data dumper.
Definition: AlignmentsPrinter.h:38
dd4hep::DetElement::world
DetElement world() const
Access to the world object. Only possible once the geometry is closed.
Definition: DetElement.cpp:245
dd4hep::align::printElement
void printElement(PrintLevel prt_level, const std::string &prefix, DetElement element, ConditionsMap &pool)
Default printout of a detector element entry.
Definition: AlignmentsPrinter.cpp:195
Printout.h
dd4hep::Condition::iov
const IOV & iov() const
Access the IOV block.
Definition: Conditions.cpp:139
dd4hep::detail::AlignmentObject
The data class behind an alignments handle.
Definition: AlignmentsInterna.h:56
dd4hep::ConditionsMap
ConditionsMap class.
Definition: ConditionsMap.h:59
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31