CMS 3D CMS Logo

DDAlgoArguments.cc
Go to the documentation of this file.
1 #include "DD4hep/Path.h"
2 #include "DD4hep/Printout.h"
3 #include "DD4hep/Detector.h"
4 #include "DD4hep/BasicGrammar.h"
6 
7 #include <TClass.h>
8 
9 #include <stdexcept>
10 
11 using namespace std;
12 using namespace cms;
13 using namespace dd4hep;
14 
15 //
16 // Helpers to create 3D rotation matrix from angles
17 //
18 dd4hep::Rotation3D
19 cms::makeRotation3D( double thetaX, double phiX,
20  double thetaY, double phiY,
21  double thetaZ, double phiZ) {
22  dd4hep::Position posX( sin( thetaX ) * cos( phiX ),
23  sin( thetaX ) * sin( phiX ),
24  cos( thetaX ));
25  dd4hep::Position posY( sin( thetaY ) * cos( phiY ),
26  sin( thetaY ) * sin( phiY ),
27  cos( thetaY ));
28  dd4hep::Position posZ( sin( thetaZ ) * cos( phiZ ),
29  sin( thetaZ ) * sin( phiZ ),
30  cos( thetaZ ));
31  dd4hep::Rotation3D rotation( posX, posY, posZ );
32 
33  return rotation;
34 }
35 
36 // makes sure that the RotationMatrix built is
37 // LEFT-handed coordinate system (i.e. reflected)
38 dd4hep::Rotation3D
39 cms::makeRotReflect( double thetaX, double phiX,
40  double thetaY, double phiY,
41  double thetaZ, double phiZ ) {
42  // define 3 unit std::vectors forming the new left-handed axes
43  DD3Vector x( cos( phiX ) * sin( thetaX ), sin( phiX ) * sin( thetaX ), cos( thetaX ));
44  DD3Vector y( cos( phiY ) * sin( thetaY ), sin( phiY ) * sin( thetaY ), cos( thetaY ));
45  DD3Vector z( cos( phiZ ) * sin( thetaZ ), sin( phiZ ) * sin( thetaZ ), cos( thetaZ ));
46 
47  constexpr double tol = 1.0e-3; // Geant4 compatible
48  double check = ( x.Cross( y )).Dot( z ); // in case of a LEFT-handed orthogonal system this must be -1
49  if( abs( 1. + check ) > tol ) {
50  except("DD4CMS","+++ FAILED to construct Rotation is not LEFT-handed!");
51  }
52 
53  dd4hep::Rotation3D rotation( x.x(), y.x(), z.x(),
54  x.y(), y.y(), z.y(),
55  x.z(), y.z(), z.z());
56  return rotation;
57 }
58 
59 dd4hep::Rotation3D
60 cms::makeRotation3D( dd4hep::Rotation3D rotation, const std::string& axis, double angle )
61 {
62  switch( hash( axis ))
63  {
64  case hash("x"):
65  rotation = dd4hep::RotationX( angle );
66  break;
67  case hash("y"):
68  rotation = dd4hep::RotationY( angle );
69  break;
70  case hash("z"):
71  rotation = dd4hep::RotationZ( angle );
72  break;
73  default:
74  throw std::runtime_error( "Axis is not valid." );
75  }
76  return rotation;
77 }
78 
79 DDAlgoArguments::DDAlgoArguments( cms::DDParsingContext& ctxt, xml_h elt )
80  : context( ctxt ),
81  element( elt )
82 {
83  name = xml_dim_t( element ).nameStr();
84 }
85 
87 string
90  xml_dim_t e( element );
91  string val = n.realName(xml_dim_t(e.child(DD_CMU(rParent))).nameStr());
92  return val;
93 }
94 
98  return n.realName(value<string>("ChildName"));
99 }
100 
102 bool DDAlgoArguments::find(const string& nam) const {
103  for(xml_coll_t p(element,_U(star)); p; ++p) {
104  string n = p.attr<string>(_U(name));
105  if ( n == nam ) {
106  return true;
107  }
108  }
109  return false;
110 }
111 
113 xml_h DDAlgoArguments::rawArgument(const string& nam) const {
114  for(xml_coll_t p(element,_U(star)); p; ++p) {
115  string n = p.attr<string>(_U(name));
116  if ( n == nam ) {
117  return std::move( p );
118  }
119  }
120  except("DD4CMS","+++ Attempt to access non-existing algorithm option %s[%s]",name.c_str(),nam.c_str());
121  throw runtime_error("DDCMS: Attempt to access non-existing algorithm option.");
122 }
123 
125 string DDAlgoArguments::resolved_scalar_arg(const string& nam) const {
127  xml_h arg = rawArgument(nam);
128  string val = arg.attr<string>(_U(value));
129  return ns.realName(val);
130 }
131 
132 namespace {
133 
135  vector<string> raw_vector(const DDAlgoArguments* a, xml_h arg) {
136  xml_dim_t xp(arg);
137  vector<string> data;
138  cms::DDNamespace ns(a->context);
139  string val = xp.text();
140  string nam = xp.nameStr();
141  string typ = xp.typeStr();
142  int num = xp.attr<int>(DD_CMU(nEntries));
143  const BasicGrammar& gr = BasicGrammar::instance<vector<string> >();
144 
145  val = '['+ns.realName(val)+']';
146  val = remove_whitespace(val);
147  int res = gr.fromString(&data,val);
148  if ( !res ) {
149  except("DD4CMS","+++ VectorParam<%s>: %s -> %s [Invalid conversion:%d]",
150  typ.c_str(), nam.c_str(), val.c_str(), res);
151  }
152  else if ( num != (int)data.size() ) {
153  except("DD4CMS","+++ VectorParam<%s>: %s -> %s [Invalid entry count: %d <> %ld]",
154  typ.c_str(), nam.c_str(), val.c_str(), num, data.size());
155  }
156  printout(DEBUG,"DD4CMS","+++ VectorParam<%s>: ret=%d %s -> %s",
157  typ.c_str(), res, nam.c_str(), gr.str(&data).c_str());
158  return data;
159  }
160 
161 
162  template <typename T> T __cnv(const string&) { return 0;}
163  template <> double __cnv<double>(const string& arg) { return _toDouble(arg); }
164  template <> float __cnv<float> (const string& arg) { return _toFloat(arg); }
165  template <> long __cnv<long> (const string& arg) { return _toLong(arg); }
166  template <> int __cnv<int> (const string& arg) { return _toInt(arg); }
167  template <> string __cnv<string>(const string& arg) { return arg; }
168 
169  template <typename T> vector<T> __cnvVect(const DDAlgoArguments* a, const char* req_typ, xml_h xp) {
170  cms::DDNamespace ns(a->context);
171  string piece;
172  string nam = xp.attr<string>(_U(name));
173  string typ = xp.attr<string>(_U(type));
174  string val = xp.text();
175  int num = xp.attr<int>(DD_CMU(nEntries));
176  if ( typ != req_typ ) {
177  except("DD4CMS",
178  "+++ VectorParam<%s | %s>: %s -> <%s> %s [Incompatible vector-type]",
179  req_typ, typ.c_str(), nam.c_str(), typeName(typeid(T)).c_str(),
180  val.c_str());
181  }
182  vector<T> data;
183  val = remove_whitespace(val);
184  if ( !val.empty() ) val += ',';
185  for(size_t idx=0, idq=val.find(',',idx);
186  idx != string::npos && idq != string::npos;
187  idx=++idq, idq=val.find(',',idx))
188  {
189  piece = ns.realName(val.substr(idx,idq-idx));
190  T d = __cnv<T>(piece);
191  data.push_back(d);
192  }
193  printout(DEBUG,"DD4CMS","+++ VectorParam<%s>: %s[%d] -> %s",
194  typ.c_str(), nam.c_str(), num, val.c_str());
195  return data;
196  }
197 }
198 
200 namespace cms {
201 
203  template<typename T> T DDAlgoArguments::value(const string& nam) const {
204  return __cnv<T>(resolved_scalar_arg(nam));
205  }
206 
207  template double DDAlgoArguments::value<double>(const string& nam) const;
208  template float DDAlgoArguments::value<float>(const string& nam) const;
209  template long DDAlgoArguments::value<long>(const string& nam) const;
210  template int DDAlgoArguments::value<int>(const string& nam) const;
211  template string DDAlgoArguments::value<string>(const string& nam) const;
212 
214  template<> vector<string> DDAlgoArguments::value<vector<string> >(const string& nam) const
215  { return raw_vector(this,rawArgument(nam)); }
216 
218  template<> vector<double> DDAlgoArguments::value<vector<double> >(const string& nam) const
219  { return __cnvVect<double>(this,"numeric",rawArgument(nam)); }
220 
222  template<> vector<float> DDAlgoArguments::value<vector<float> >(const string& nam) const
223  { return __cnvVect<float>(this,"numeric",rawArgument(nam)); }
224 
226  template<> vector<long> DDAlgoArguments::value<vector<long> >(const string& nam) const
227  { return __cnvVect<long>(this,"numeric",rawArgument(nam)); }
228 
230  template<> vector<int> DDAlgoArguments::value<vector<int> >(const string& nam) const
231  { return __cnvVect<int>(this,"numeric",rawArgument(nam)); }
232 }
233 
235 string DDAlgoArguments::str(const string& nam) const
236 { return this->value<string>(nam); }
237 
239 double DDAlgoArguments::dble(const string& nam) const
240 { return this->value<double>(nam); }
241 
243 int DDAlgoArguments::integer(const string& nam) const
244 { return this->value<int>(nam); }
245 
247 vector<double> DDAlgoArguments::vecDble(const string& nam) const
248 { return this->value<vector<double> >(nam); }
249 
251 vector<int> DDAlgoArguments::vecInt(const string& nam) const
252 { return this->value<vector<int> >(nam); }
253 
255 vector<string> DDAlgoArguments::vecStr(const string& nam) const
256 { return this->value<vector<string> >(nam); }
type
Definition: HCALResponse.h:21
#define DEBUG
std::string childName() const
Access value of child&#39;name from the xml element.
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T value(const std::string &name) const
A arg
Definition: Factorize.h:38
Definition: Electron.h:6
int integer(const std::string &nam) const
Shortcut to access integer arguments.
dd4hep::Rotation3D makeRotation3D(double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
xml_h rawArgument(const std::string &name) const
Access raw argument as a string by name.
std::string realName(const std::string &) const
Definition: DDNamespace.cc:69
dd4hep::Rotation3D makeRotReflect(double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: value.py:1
#define DD_CMU(a)
Definition: DDXMLTags.h:188
Namespace of DDCMS conversion namespace.
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >> DD3Vector
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
double a
Definition: hdecay.h:121
std::vector< double > vecDble(const std::string &nam) const
Shortcut to access vector<double> arguments.
double dble(const std::string &nam) const
Shortcut to access double arguments.
cms::DDParsingContext & context
std::vector< std::string > vecStr(const std::string &nam) const
Shortcut to access vector<string> arguments.
def check(config)
Definition: trackerTree.py:14
bool find(const std::string &name) const
Check the existence of an argument by name.
long double T
std::vector< int > vecInt(const std::string &nam) const
Shortcut to access vector<int> arguments.
std::string resolved_scalar_arg(const std::string &name) const
Access namespace resolved argument as a string by name.
std::string parentName() const
Access value of rParent child node.
def move(src, dest)
Definition: eostools.py:511
#define constexpr
std::string str(const std::string &nam) const
Shortcut to access string arguments.
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11