DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Fields.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 #include <DD4hep/Fields.h>
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/InstanceCount.h>
17 #include <DD4hep/detail/Handle.inl>
18 
19 using namespace dd4hep;
20 
23 
26 
27 namespace {
28  void calculate_combined_field(std::vector<CartesianField>& v, const Position& pos, double* field) {
29  for (const auto& i : v ) i.value(pos, field);
30  }
31 }
32 
35  // The field_type MUST be overriden by the concrete sublass!
38 }
39 
43 }
44 
46 const char* CartesianField::type() const {
47  return m_element->GetTitle();
48 }
49 
52  return ELECTRIC == (fieldType() & ELECTRIC);
53 }
54 
57  return data<Object>()->properties;
58 }
59 
61 void CartesianField::value(const Position& pos, Direction& field) const {
62  double fld[3] = {0e0, 0e0, 0e0};
63  double position[3] = {pos.X(), pos.Y(), pos.Z()};
64  data<Object>()->fieldComponents(position, fld);
65  field = Direction(fld[0], fld[1], fld[2]);
66 }
67 
69 void CartesianField::value(const Position& pos, double* field) const {
70  double position[3] = {pos.X(), pos.Y(), pos.Z()};
71  data<Object>()->fieldComponents(position, field);
72 }
73 
75 void CartesianField::value(const double* pos, double* field) const {
76  data<Object>()->fieldComponents(pos, field);
77 }
78 
80 OverlayedField::Object::Object() : TypedObject(), electric(), magnetic()
81 {
84 }
85 
89 }
90 
92 OverlayedField::OverlayedField(const std::string& nam) : Ref_t() {
93  auto* obj = new Object();
94  assign(obj, nam, "overlay_field");
95  obj->field_type = CartesianField::OVERLAY;
96 }
97 
100  return data<Object>()->properties;
101 }
102 
105  int field = data<Object>()->field_type;
107 }
108 
111  if (field.isValid()) {
112  Object* o = data<Object>();
113  if ( o ) {
114  int typ = field.fieldType();
115  bool isEle = field.ELECTRIC == (typ & field.ELECTRIC);
116  bool isMag = field.MAGNETIC == (typ & field.MAGNETIC);
117  if (isEle) {
118  std::vector < CartesianField > &v = o->electric_components;
119  v.emplace_back(field);
120  o->field_type |= field.ELECTRIC;
121  o->electric = (v.size() == 1) ? field : CartesianField();
122  }
123  if (isMag) {
124  std::vector < CartesianField > &v = o->magnetic_components;
125  v.emplace_back(field);
126  o->field_type |= field.MAGNETIC;
127  o->magnetic = (v.size() == 1) ? field : CartesianField();
128  }
129  if ( isMag || isEle ) {
130  return;
131  }
132  except("OverlayedField","add: Attempt to add an unknown field type.");
133  }
134  except("OverlayedField","add: Attempt to add an invalid object.");
135  }
136  except("OverlayedField","add: Attempt to add an invalid field.");
137 }
138 
140 void OverlayedField::magneticField(const Position& pos, double* field) const {
141  if ( isValid() ) {
142  field[0] = field[1] = field[2] = 0.0;
143  auto* obj = data<Object>();
144  CartesianField f = obj->magnetic;
145  if ( f.isValid() )
146  f.value(pos, field);
147  else
148  calculate_combined_field(obj->magnetic_components, pos, field);
149  return;
150  }
151  except("OverlayedField","add: Attempt to add an invalid field.");
152 }
153 
155 void OverlayedField::combinedElectric(const Position& pos, double* field) const {
156  field[0] = field[1] = field[2] = 0.;
157  calculate_combined_field(data<Object>()->electric_components, pos, field);
158 }
159 
161 void OverlayedField::combinedMagnetic(const Position& pos, double* field) const {
162  field[0] = field[1] = field[2] = 0.;
163  calculate_combined_field(data<Object>()->magnetic_components, pos, field);
164 }
165 
167 void OverlayedField::electromagneticField(const Position& pos, double* field) const {
168  Object* o = data<Object>();
169  field[0] = field[1] = field[2] = 0.;
170  calculate_combined_field(o->electric_components, pos, field);
171  calculate_combined_field(o->magnetic_components, pos, field + 3);
172 }
dd4hep::OverlayedField::combinedElectric
void combinedElectric(const Position &pos, double *field) const
Returns the 3 electric field components (x, y, z) if many components are present.
Definition: Fields.cpp:155
dd4hep::CartesianField::changesEnergy
bool changesEnergy() const
Does the field change the energy of charged particles?
Definition: Fields.cpp:51
v
View * v
Definition: MultiView.cpp:28
dd4hep::Handle< NamedObject >::Object
NamedObject Object
Extern accessible definition of the contained element type.
Definition: Handle.h:88
dd4hep::CartesianField::UNKNOWN
@ UNKNOWN
Definition: Fields.h:43
dd4hep::OverlayedField::Object::magnetic
CartesianField magnetic
Definition: Fields.h:157
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::CartesianField::Object
Internal data class shared by all handles of a given type.
Definition: Fields.h:67
dd4hep::Handle< NamedObject >
dd4hep::CartesianField::MAGNETIC
@ MAGNETIC
Definition: Fields.h:43
dd4hep::OverlayedField::add
void add(CartesianField field)
Add a new field component.
Definition: Fields.cpp:110
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::Direction
Position Direction
Definition: Fields.h:28
dd4hep::OverlayedField::properties
Properties & properties() const
Access to properties container.
Definition: Fields.cpp:99
dd4hep::OverlayedField::Object::~Object
virtual ~Object()
Default destructor.
Definition: Fields.cpp:87
dd4hep::OverlayedField::combinedMagnetic
void combinedMagnetic(const Position &pos, double *field) const
Returns the 3 magnetic field components (x, y, z) if many components are present.
Definition: Fields.cpp:161
OverlayedFieldObject
OverlayedField::Object OverlayedFieldObject
Definition: Fields.cpp:24
dd4hep::Handle< NamedObject >::assign
void assign(Object *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::CartesianField::Properties
std::map< std::string, std::map< std::string, std::string > > Properties
Definition: Fields.h:45
dd4hep::CartesianField::Object::Object
Object()
Default constructor.
Definition: Fields.cpp:34
dd4hep::CartesianField::fieldType
int fieldType() const
Access the field type.
Definition: Fields.h:99
dd4hep::OverlayedField::Object::electric_components
std::vector< CartesianField > electric_components
Definition: Fields.h:158
dd4hep::Handle< NamedObject >::m_element
NamedObject * m_element
Single and only data member: Reference to the actual element.
Definition: Handle.h:93
dd4hep::NamedObject::GetTitle
const char * GetTitle() const
Get name (used by Handle)
Definition: NamedObject.h:70
dd4hep::CartesianField::properties
Properties & properties() const
Access to properties container.
Definition: Fields.cpp:56
dd4hep::OverlayedField::Object
Internal data class shared by all handles.
Definition: Fields.h:154
dd4hep::CartesianField::value
void value(const Position &pos, Direction &field) const
Returns the 3 field components (x, y, z).
Definition: Fields.cpp:61
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
dd4hep::CartesianField::TypedObject::field_type
int field_type
Field type.
Definition: Fields.h:56
dd4hep::OverlayedField::Object::Object
Object()
Default constructor.
Definition: Fields.cpp:80
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::OverlayedField::OverlayedField
OverlayedField()=default
Default constructor.
dd4hep::OverlayedField::Object::magnetic_components
std::vector< CartesianField > magnetic_components
Definition: Fields.h:159
dd4hep::OverlayedField::changesEnergy
bool changesEnergy() const
Does the field change the energy of charged particles?
Definition: Fields.cpp:104
CartesianFieldObject
CartesianField::Object CartesianFieldObject
Definition: Fields.cpp:21
dd4hep::OverlayedField::Object::electric
CartesianField electric
Definition: Fields.h:156
dd4hep::OverlayedField::magneticField
void magneticField(const Position &pos, double *field) const
Returns the 3 magnetic field components (x, y, z).
Definition: Fields.cpp:140
dd4hep::CartesianField::ELECTRIC
@ ELECTRIC
Definition: Fields.h:43
dd4hep::OverlayedField::Properties
std::map< std::string, PropertyValues > Properties
Definition: Fields.h:146
dd4hep::CartesianField::TypedObject
Internal data class shared by all handles of a given type.
Definition: Fields.h:53
InstanceCount.h
Fields.h
Printout.h
dd4hep::OverlayedField::electromagneticField
void electromagneticField(const Position &pos, double *field) const
Returns the 3 electric (val[0]-val[2]) and magnetic field components (val[3]-val[5]).
Definition: Fields.cpp:167
dd4hep::CartesianField::OVERLAY
@ OVERLAY
Definition: Fields.h:43
dd4hep::CartesianField::type
const char * type() const
Access the field type (string)
Definition: Fields.cpp:46
DD4HEP_INSTANTIATE_HANDLE
DD4HEP_INSTANTIATE_HANDLE(CartesianFieldObject)
dd4hep::CartesianField::Object::~Object
virtual ~Object()
Default destructor.
Definition: Fields.cpp:41
dd4hep::CartesianField
Base class describing any field with 3D cartesian vectors for the field strength.
Definition: Fields.h:40