DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
DetectorFields.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 <DD4hep/Printout.h>
16 #include <DD4hep/FieldTypes.h>
17 #include <DD4hep/MatrixHelpers.h>
19 
20 // C/C++ include files
21 #include <cmath>
22 
23 using namespace dd4hep;
24 
25 static Handle<NamedObject> convert_constant_field(Detector&, xml_h field, Handle<NamedObject> object) {
26  xml_doc_t doc = xml_elt_t(field).document();
27  ConstantField* fld = object.data<ConstantField>();
28  field.setAttr(_U(name), object->GetName());
29  field.setAttr(_U(type), object->GetTitle());
30  field.setAttr(_U(lunit), "mm");
31  //field.setAttr(_U(funit),"tesla");
32  if (fld->type == CartesianField::ELECTRIC)
33  field.setAttr(_U(field), "electric");
34  else if (fld->type == CartesianField::MAGNETIC)
35  field.setAttr(_U(field), "magnetic");
36 
37  xml_elt_t strength = xml_elt_t(doc, _U(strength));
38  strength.setAttr(_U(x), fld->direction.X());
39  strength.setAttr(_U(y), fld->direction.Y());
40  strength.setAttr(_U(z), fld->direction.Z());
41  field.append(strength);
42  return object;
43 }
44 DECLARE_XML_PROCESSOR(ConstantField_Convert2Detector,convert_constant_field)
45 
46 static Handle<NamedObject> convert_solenoid(Detector&, xml_h field, Handle<NamedObject> object) {
47  char text[128];
48  SolenoidField* fld = object.data<SolenoidField>();
49  field.setAttr(_U(name), object->GetName());
50  field.setAttr(_U(type), object->GetTitle());
51  field.setAttr(_U(lunit), "mm");
52  field.setAttr(_U(funit), "tesla");
53  ::snprintf(text, sizeof(text), "%g/mm", fld->outerRadius);
54  field.setAttr(_U(outer_radius), dd4hep::_toDouble(text));
55  ::snprintf(text, sizeof(text), "%g/mm", fld->innerRadius);
56  field.setAttr(_U(inner_radius), dd4hep::_toDouble(text));
57  ::snprintf(text, sizeof(text), "%g/tesla", fld->innerField);
58  field.setAttr(_U(inner_field), dd4hep::_toDouble(text));
59  ::snprintf(text, sizeof(text), "%g/tesla", fld->outerField);
60  field.setAttr(_U(outer_field), dd4hep::_toDouble(text));
61  field.setAttr(_U(zmin), fld->minZ);
62  field.setAttr(_U(zmax), fld->maxZ);
63  return object;
64 }
65 DECLARE_XML_PROCESSOR(solenoid_Convert2Detector,convert_solenoid)
66 DECLARE_XML_PROCESSOR(SolenoidMagnet_Convert2Detector,convert_solenoid)
67 
68 static Handle<NamedObject> convert_dipole(Detector&, xml_h field, Handle<NamedObject> object) {
69  char text[128];
70  xml_doc_t doc = xml_elt_t(field).document();
71  DipoleField* fld = object.data<DipoleField>();
72  field.setAttr(_U(lunit), "mm");
73  field.setAttr(_U(funit), "tesla");
74  ::snprintf(text, sizeof(text), "%g/mm", fld->rmax);
75  field.setAttr(_U(rmax), dd4hep::_toDouble(text));
76  ::snprintf(text, sizeof(text), "%g/mm", fld->zmax);
77  field.setAttr(_U(zmax), dd4hep::_toDouble(text));
78  ::snprintf(text, sizeof(text), "%g/mm", fld->zmin);
79  field.setAttr(_U(zmin), dd4hep::_toDouble(text));
80  for (auto c : fld->coefficents ) {
81  xml_elt_t coeff = xml_elt_t(doc, _U(dipole_coeff));
82  coeff.setValue(dd4hep::_toString(c));
83  field.append(coeff);
84  }
85  return object;
86 }
87 DECLARE_XML_PROCESSOR(DipoleMagnet_Convert2Detector,convert_dipole)
88 //DECLARE_XML_PROCESSOR(DipoleField_Convert2Detector,convert_dipole)
89 
90 
91 static Handle<NamedObject> convert_multipole(Detector&, xml_h field, Handle<NamedObject> object) {
92  xml_doc_t doc = xml_elt_t(field).document();
93  MultipoleField* ptr = object.data<MultipoleField>();
94  RotationZYX rot;
95  Position pos;
96 
97  field.setAttr(_U(lunit), "mm");
98  field.setAttr(_U(funit), "tesla");
99 
100  field.setAttr(_U(name), object->GetName());
101  field.setAttr(_U(type), object->GetTitle());
102  field.setAttr(_U(Z), ptr->B_z);
103  detail::matrix::_decompose(ptr->transform, pos, rot);
104  xml_elt_t x_pos = xml_elt_t(doc, _U(position));
105  x_pos.setAttr(_U(x),pos.x());
106  x_pos.setAttr(_U(y),pos.y());
107  x_pos.setAttr(_U(z),pos.z());
108  field.append(x_pos);
109 
110  xml_elt_t x_rot = xml_elt_t(doc, _U(rotation));
111  x_pos.setAttr(_U(x),rot.Theta());
112  x_pos.setAttr(_U(y),rot.Phi());
113  x_pos.setAttr(_U(z),rot.Psi());
114  field.append(x_rot);
115  if ( ptr->volume.isValid() ) {
116  // Disentangle shape ?
117  }
118 
119  for( double c : ptr->coefficents ) {
120  xml_dim_t x_coeff = xml_elt_t(doc, _U(coefficient));
121  x_coeff.setAttr(_U(value),c);
122  field.append(x_coeff);
123  }
124  return object;
125 }
126 DECLARE_XML_PROCESSOR(MultipoleMagnet_Convert2Detector,convert_multipole)
dd4hep::xml::Element::document
Document document() const
Access the hosting document handle of this DOM element.
Definition: XMLElements.cpp:1089
dd4hep::DipoleField::zmin
double zmin
Definition: FieldTypes.h:80
dd4hep::DipoleField::rmax
double rmax
Definition: FieldTypes.h:81
dd4hep::detail::matrix::_decompose
void _decompose(const TGeoMatrix &matrix, Position &pos, Rotation3D &rot)
Decompose a generic ROOT Matrix into a translation (Position) and a Rotation3D.
Definition: MatrixHelpers.cpp:203
MatrixHelpers.h
dd4hep::MultipoleField
Implementation object of a Multipole magnetic field.
Definition: FieldTypes.h:152
dd4hep::xml::Handle_t::append
void append(Handle_t e) const
Append a DOM element to the current node.
Definition: XMLElements.cpp:727
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::Handle< NamedObject >
dd4hep::CartesianField::MAGNETIC
@ MAGNETIC
Definition: Fields.h:43
dd4hep::_toString
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:332
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
DECLARE_XML_PROCESSOR
#define DECLARE_XML_PROCESSOR(name, func)
Definition: Factories.h:337
dd4hep::DipoleField
Implementation object of a dipole magnetic field.
Definition: FieldTypes.h:77
dd4hep::SolenoidField::maxZ
double maxZ
Definition: FieldTypes.h:58
dd4hep::xml::Element::setValue
void setValue(const T &val) const
Set element value.
Definition: XMLElements.h:904
dd4hep::ConstantField
Implementation object of a field with constant strength.
Definition: FieldTypes.h:34
dd4hep::DipoleField::zmax
double zmax
Definition: FieldTypes.h:79
dd4hep::SolenoidField::innerRadius
double innerRadius
Definition: FieldTypes.h:59
dd4hep::DipoleField::coefficents
Coefficents coefficents
Definition: FieldTypes.h:82
dd4hep::MultipoleField::B_z
double B_z
Constant Z field overlay.
Definition: FieldTypes.h:167
dd4hep::SolenoidField::minZ
double minZ
Definition: FieldTypes.h:57
dd4hep::ConstantField::direction
Direction direction
Field direction.
Definition: FieldTypes.h:37
_U
#define _U(a)
Definition: Tags.h:23
dd4hep::MultipoleField::transform
Transform3D transform
Position transformation of the field. Only stored here for reference.
Definition: FieldTypes.h:161
dd4hep::xml::Document
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:697
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
DetFactoryHelper.h
dd4hep::SolenoidField::outerField
double outerField
Definition: FieldTypes.h:56
dd4hep::_toDouble
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:116
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep::MultipoleField::volume
Solid volume
Boundary volume (optional)
Definition: FieldTypes.h:159
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::xml::Element::setAttr
Attribute setAttr(const XmlChar *nam, const T &val) const
Set single attribute.
Definition: XMLElements.h:900
dd4hep::MultipoleField::coefficents
Coefficents coefficents
Multi-pole coefficients.
Definition: FieldTypes.h:155
xml_elt_t
dd4hep::xml::Element xml_elt_t
Definition: ConditionsRepository.cpp:33
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::xml::Handle_t::setAttr
Attribute setAttr(const XmlChar *t, const XmlChar *v) const
Generic attribute setter with unicode value.
Definition: XMLElements.cpp:919
dd4hep::CartesianField::ELECTRIC
@ ELECTRIC
Definition: Fields.h:43
dd4hep::NamedObject::type
std::string type
The object type.
Definition: NamedObject.h:35
dd4hep::RotationZYX
ROOT::Math::RotationZYX RotationZYX
Definition: Objects.h:105
dd4hep::SolenoidField::outerRadius
double outerRadius
Definition: FieldTypes.h:60
FieldTypes.h
Printout.h
xml_dim_t
dd4hep::xml::Dimension xml_dim_t
Definition: XML.h:31
dd4hep::SolenoidField::innerField
double innerField
Definition: FieldTypes.h:55
dd4hep::SolenoidField
Implementation object of a solenoidal magnetic field.
Definition: FieldTypes.h:53