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
Objects.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework include files
16 #include "DD4hep/LCDD.h"
17 #include "DD4hep/Printout.h"
18 #include "DD4hep/IDDescriptor.h"
19 #include "DD4hep/InstanceCount.h"
21 
22 #include "TMap.h"
23 #include "TROOT.h"
24 #include "TColor.h"
25 #include "TGeoMatrix.h"
26 #include "TGeoManager.h"
27 #include "TGeoElement.h"
28 #include "TGeoMaterial.h"
29 
30 // C/C++ include files
31 #include <cmath>
32 #include <sstream>
33 #include <iomanip>
34 
35 using namespace std;
36 using namespace DD4hep::Geometry;
37 
39 Author::Author(LCDD& /* lcdd */) {
40  m_element = new NamedObject("", "author");
41 }
42 
44 std::string Author::authorName() const {
45  return m_element->GetName();
46 }
47 
49 void Author::setAuthorName(const std::string& nam) {
50  m_element->SetName(nam.c_str());
51 }
52 
54 std::string Author::authorEmail() const {
55  return m_element->GetTitle();
56 }
57 
59 void Author::setAuthorEmail(const std::string& addr) {
60  m_element->SetTitle(addr.c_str());
61 }
62 
64 Header::Header(const string& author_name, const string& descr_url) {
65  Object* obj_ptr = new Object();
66  assign(obj_ptr, author_name, descr_url);
67 }
68 
70 const std::string Header::name() const {
71  return m_element->GetName();
72 }
73 
75 void Header::setName(const std::string& new_name) {
76  m_element->SetName(new_name.c_str());
77 }
78 
80 const std::string Header::title() const {
81  return m_element->GetTitle();
82 }
83 
85 void Header::setTitle(const std::string& new_title) {
86  m_element->SetTitle(new_title.c_str());
87 }
88 
90 const std::string& Header::url() const {
91  return data<Object>()->url;
92 }
93 
95 void Header::setUrl(const std::string& new_url) {
96  data<Object>()->url = new_url;
97 }
98 
100 const std::string& Header::author() const {
101  return data<Object>()->author;
102 }
103 
105 void Header::setAuthor(const std::string& new_author) {
106  data<Object>()->author = new_author;
107 }
108 
110 const std::string& Header::status() const {
111  return data<Object>()->status;
112 }
113 
115 void Header::setStatus(const std::string& new_status) {
116  data<Object>()->status = new_status;
117 }
118 
120 const std::string& Header::version() const {
121  return data<Object>()->version;
122 }
123 
125 void Header::setVersion(const std::string& new_version) {
126  data<Object>()->version = new_version;
127 }
128 
130 const std::string& Header::comment() const {
131  return data<Object>()->comment;
132 }
133 
135 void Header::setComment(const std::string& new_comment) {
136  data<Object>()->comment = new_comment;
137 }
138 
140 Constant::Constant(const string& nam, const string& val, const string& typ) {
141  m_element = new Object(nam, val, typ);
142 }
143 
145 Constant::Constant(const string& nam) {
146  m_element = new Object(nam.c_str(), "", "number");
147 }
148 
150 string Constant::dataType() const {
151  if ( isValid() ) {
152  return m_element->dataType;
153  }
154  throw runtime_error("DD4hep: Attempt to access internals from invalid Constant handle!");
155 }
156 
158 string Constant::toString() const {
159  stringstream os;
160  os << m_element->GetName() << " \"" << m_element->GetTitle() << "\" ";
161  if ( m_element->dataType == "string" ) os << "Value:" << m_element->GetTitle();
162  else os << "Value:" << _toDouble(m_element->GetTitle());
163  return os.str();
164 }
165 
167 Atom::Atom(const string& nam, const string& formula, int Z, int N, double density) {
168  TGeoElementTable* t = TGeoElement::GetElementTable();
169  TGeoElement* e = t->FindElement(nam.c_str());
170  if (!e) {
171  t->AddElement(nam.c_str(), formula.c_str(), Z, N, density);
172  e = t->FindElement(nam.c_str());
173  }
174  m_element = e;
175 }
176 
178 double Material::Z() const {
179  Handle < TGeoMedium > val(*this);
180  if (val.isValid()) {
181  TGeoMaterial* m = val->GetMaterial();
182  if (m)
183  return m->GetZ();
184  throw runtime_error("DD4hep: The medium " + string(val->GetName()) + " has an invalid material reference!");
185  }
186  throw runtime_error("DD4hep: Attempt to access proton number from invalid material handle!");
187 }
189 double Material::A() const {
190  Handle < TGeoMedium > val(*this);
191  if (val.isValid()) {
192  TGeoMaterial* m = val->GetMaterial();
193  if (m)
194  return m->GetA();
195  throw runtime_error("DD4hep: The medium " + string(val->GetName()) + " has an invalid material reference!");
196  }
197  throw runtime_error("DD4hep: Attempt to access atomic number from invalid material handle!");
198 }
199 
201 double Material::density() const {
202  Handle < TGeoMedium > val(*this);
203  if (val.isValid()) {
204  TGeoMaterial* m = val->GetMaterial();
205  if (m)
206  return m->GetDensity();
207  throw runtime_error("DD4hep: The medium " + string(val->GetName()) + " has an invalid material reference!");
208  }
209  throw runtime_error("DD4hep: Attempt to access density from invalid material handle!");
210 }
211 
213 double Material::radLength() const {
214  Handle < TGeoMedium > val(*this);
215  if (val.isValid()) {
216  TGeoMaterial* m = val->GetMaterial();
217  if (m)
218  return m->GetRadLen();
219  throw runtime_error("DD4hep: The medium " + string(val->GetName()) + " has an invalid material reference!");
220  }
221  throw runtime_error("DD4hep: Attempt to access radiation length from invalid material handle!");
222 }
223 
225 double Material::intLength() const {
226  Handle < TGeoMedium > val(*this);
227  if (val.isValid()) {
228  TGeoMaterial* m = val->GetMaterial();
229  if (m)
230  return m->GetIntLen();
231  throw runtime_error("The medium " + string(val->GetName()) + " has an invalid material reference!");
232  }
233  throw runtime_error("Attempt to access interaction length from invalid material handle!");
234 }
235 
237 string Material::toString() const {
238  Handle < TGeoMedium > val(*this);
239  stringstream os;
240  os << val->GetName() << " " << val->GetTitle() << " id:" << hex << val->GetId() << " Pointer:" << val->GetPointerName();
241  return os.str();
242 }
243 
245 VisAttr::VisAttr(const string& nam) {
246  Object* obj = new Object();
247  assign(obj, nam, "vis");
248  obj->color = 2;
249  setLineStyle (SOLID);
250  setDrawingStyle(SOLID);
251  setShowDaughters(true);
252  setAlpha(0.9f);
253 }
254 
256 VisAttr::VisAttr(const char* nam) {
257  Object* obj = new Object();
258  assign(obj, nam, "vis");
259  obj->color = 2;
260  setLineStyle (SOLID);
261  setDrawingStyle(SOLID);
262  setShowDaughters(true);
263  setAlpha(0.9f);
264 }
265 
267 bool VisAttr::showDaughters() const {
268  return object<Object>().showDaughters;
269 }
270 
272 void VisAttr::setShowDaughters(bool value) {
273  object<Object>().showDaughters = value;
274 }
275 
277 bool VisAttr::visible() const {
278  return object<Object>().visible;
279 }
280 
282 void VisAttr::setVisible(bool value) {
283  object<Object>().visible = value;
284 }
285 
287 int VisAttr::lineStyle() const {
288  return object<Object>().lineStyle;
289 }
290 
292 void VisAttr::setLineStyle(int value) {
293  object<Object>().lineStyle = value;
294 }
295 
297 int VisAttr::drawingStyle() const {
298  return object<Object>().drawingStyle;
299 }
300 
302 void VisAttr::setDrawingStyle(int value) {
303  object<Object>().drawingStyle = value;
304 }
305 
307 float VisAttr::alpha() const {
308  //NamedObject* obj = first_value<NamedObject>(*this);
309  //obj->SetAlpha(value);
310  return object<Object>().alpha;
311 }
312 
314 void VisAttr::setAlpha(float value) {
315  object<Object>().alpha = value;
316  //NamedObject* obj = first_value<NamedObject>(*this);
317  //obj->SetAlpha(value);
318 }
319 
321 int VisAttr::color() const {
322  return object<Object>().color;
323 }
324 
326 void VisAttr::setColor(float red, float green, float blue) {
327  Object& o = object<Object>();
328  o.color = TColor::GetColor(red, green, blue);
329  o.col = gROOT->GetColor(o.color);
330 }
331 
333 bool VisAttr::rgb(float& red, float& green, float& blue) const {
334  Object& o = object<Object>();
335  if (o.col) {
336  TColor* c = (TColor*) o.col;
337  c->GetRGB(red, green, blue);
338  return true;
339  }
340  return false;
341 }
342 
344 string VisAttr::toString() const {
345  const VisAttr::Object* obj = &object<Object>();
346  TColor* col = gROOT->GetColor(obj->color);
347  char text[256];
348  ::snprintf(text, sizeof(text), "%-20s RGB:%-8s [%d] %7.2f Style:%d %d ShowDaughters:%3s Visible:%3s", ptr()->GetName(),
349  col->AsHexString(), obj->color, col->GetAlpha(), int(obj->drawingStyle), int(obj->lineStyle),
350  yes_no(obj->showDaughters), yes_no(obj->visible));
351  return text;
352 }
353 
355 AlignmentEntry::AlignmentEntry(const string& path) {
356  TGeoPhysicalNode* obj = new TGeoPhysicalNode(path.c_str());
357  assign(obj, path, "*");
358 }
359 
361 int AlignmentEntry::align(const Position& pos, bool check, double overlap) {
362  return align(pos, RotationZYX(), check, overlap);
363 }
364 
366 int AlignmentEntry::align(const RotationZYX& rot, bool check, double overlap) {
367  return align(Position(), rot, check, overlap);
368 }
369 
371 int AlignmentEntry::align(const Position& pos, const RotationZYX& rot, bool check, double overlap) {
372 
373  if (isValid()) {
374  TGeoHMatrix* new_matrix = dynamic_cast<TGeoHMatrix*>(m_element->GetOriginalMatrix()->MakeClone());
375  TGeoRotation rotation("", rot.Phi() * RAD_2_DEGREE, rot.Theta() * RAD_2_DEGREE, rot.Psi() * RAD_2_DEGREE);
376  TGeoCombiTrans m(pos.X(), pos.Y(), pos.Z(), 0);
377  m.SetRotation(rotation);
378  new_matrix->Multiply(&m);
379  m_element->Align(new_matrix, 0, check, overlap);
380  return 1;
381  }
382  throw runtime_error("DD4hep: Cannot align non existing physical node.");
383 }
384 
386 Limit& Limit::operator=(const Limit& c) {
387  if (this != &c) {
388  particles = c.particles;
389  name = c.name;
390  unit = c.unit;
391  value = c.value;
392  content = c.content;
393  }
394  return *this;
395 }
396 
398 bool Limit::operator==(const Limit& c) const {
399  return value == c.value && name == c.name && particles == c.particles;
400 }
401 
403 bool Limit::operator<(const Limit& c) const {
404  if (value < c.value)
405  return true;
406  if (name < c.name)
407  return true;
408  if (particles < c.particles)
409  return true;
410  return false;
411 }
412 
414 string Limit::toString() const {
415  string res = name + " = " + content;
416  if (!unit.empty())
417  res += unit + " ";
418  res + " (" + particles + ")";
419  return res;
420 }
421 
423 LimitSet::LimitSet(const string& nam) {
424  assign(new Object(), nam, "limitset");
425 }
426 
428 bool LimitSet::addLimit(const Limit& limit) {
429  pair<Object::iterator, bool> ret = data<Object>()->insert(limit);
430  return ret.second;
431 }
432 
434 const set<Limit>& LimitSet::limits() const {
435  const Object* o = data<Object>();
436  return *o;
437 }
438 
440 Region::Region(const string& nam) {
441  Object* p = new Object();
442  assign(p, nam, "region");
443  p->magic = magic_word();
444  p->store_secondaries = false;
445  p->threshold = 10.0;
446  p->cut = 10.0;
447 }
448 
449 Region& Region::setStoreSecondaries(bool value) {
450  object<Object>().store_secondaries = value;
451  return *this;
452 }
453 
454 Region& Region::setThreshold(double value) {
455  object<Object>().threshold = value;
456  return *this;
457 }
458 
459 Region& Region::setCut(double value) {
460  object<Object>().cut = value;
461  return *this;
462 }
463 
465 vector<string>& Region::limits() const {
466  return object<Object>().user_limits;
467 }
468 
470 double Region::cut() const {
471  return object<Object>().cut;
472 }
473 
475 double Region::threshold() const {
476  return object<Object>().threshold;
477 }
478 
480 bool Region::storeSecondaries() const {
481  return object<Object>().store_secondaries;
482 }
483 
484 #undef setAttr
485 
486 #if 0
487 
493 struct IDSpec : public Ref_t {
495  template <typename Q>
496  IDSpec(const Handle<Q>& e) : Ref_t(e) {}
498  IDSpec(LCDD& doc, const std::string& name, const IDDescriptor& dsc);
499  void addField(const std::string& name, const std::pair<int,int>& field);
500 };
501 
502 IDSpec::IDSpec(LCDD& lcdd, const string& name, const IDDescriptor& dsc)
503  : RefElement(doc,Tag_idspec,name)
504 {
505  const IDDescriptor::FieldIDs& f = dsc.ids();
506  const IDDescriptor::FieldMap& m = dsc.fields();
507  object<Object>().Attr_length = dsc.maxBit();
508  for(IDDescriptor::FieldIDs::const_iterator i=f.begin(); i!=f.end();++i) {
509  int ident = (*i).first;
510  const string& nam = (*i).second;
511  const pair<int,int>& fld = m.find(nam)->second;
512  addField(nam,fld);
513  }
514 }
515 
516 void IDSpec::addField(const string& name, const pair<int,int>& field) {
517  addField(Strng_t(name),field);
518 }
519 
520 void IDSpec::addField(const string& name, const pair<int,int>& field) {
521  Element e(document(),Tag_idfield);
522  e.object<Object>().Attr_signed = field.second<0;
523  e.object<Object>().Attr_label = name;
524  e.object<Object>().Attr_start = field.first;
525  e.object<Object>().Attr_length = abs(field.second);
526  m_element.append(e);
527 }
528 #endif
std::vector< std::pair< std::string, Field > > FieldMap
Definition: IDDescriptor.h:44
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:274
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
Concrete object implementation of the Region Handle.
const char * yes_no(bool value)
Helper function to print booleans in format YES/NO.
Definition: Printout.h:295
std::vector< std::pair< size_t, std::string > > FieldIDs
Definition: IDDescriptor.h:45
return e
Definition: Volumes.cpp:297
Concrete object implementation for the Constant handle.
std::string content
Definition: Objects.h:449
Implementation of a named object.
Definition: NamedObject.h:31
ROOT::Math::RotationZYX RotationZYX
Definition: Objects.h:98
std::string particles
Definition: Objects.h:446
const char * GetName(T *p)
Definition: Utilities.h:45
std::string name
Definition: Objects.h:447
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:269
ROOT::Math::XYZVector Position
Definition: Objects.h:75
Concrete object implementation for the Header handle.
std::string dataType
Constant type.
std::string unit
Definition: Objects.h:448
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:176
Small object describing a limit structure acting on a particle type.
Definition: Objects.h:444
const FieldIDs & ids() const
Access the field-id container.
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:103
Handle class describing a region as used in simulation.
Definition: Objects.h:516
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
Concrete object implementation of the LimitSet Handle.
Class implementing the ID encoding of detector response.
Definition: IDDescriptor.h:40
unsigned maxBit() const
The total number of encoding bits for this descriptor.
const FieldMap & fields() const
Access the fieldmap container.
TGeoShape TGeoMedium * m
Definition: Volumes.cpp:294
unsigned long long int magic_word()
Access to the magic word, which is protecting some objects against memory corruptions.
Definition: Handle.h:55
Concrete object implementation of the VisAttr Handle.
#define RAD_2_DEGREE
Definition: Handle.h:26
std::string toString(const PlacedVolume::VolIDs &ids)
Convert VolumeID to string.