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 dd4hep::Rotation3D
37 cms::makeRotation3D( dd4hep::Rotation3D rotation, const std::string& axis, double angle )
38 {
39  switch( hash( axis ))
40  {
41  case hash("x"):
42  rotation = dd4hep::RotationX( angle );
43  break;
44  case hash("y"):
45  rotation = dd4hep::RotationY( angle );
46  break;
47  case hash("z"):
48  rotation = dd4hep::RotationZ( angle );
49  break;
50  default:
51  throw std::runtime_error( "Axis is not valid." );
52  }
53  return rotation;
54 }
55 
56 DDAlgoArguments::DDAlgoArguments( cms::DDParsingContext& ctxt, xml_h elt )
57  : context( ctxt ),
58  element( elt )
59 {
60  name = xml_dim_t( element ).nameStr();
61 }
62 
64 string
67  xml_dim_t e( element );
68  string val = n.realName(xml_dim_t(e.child(_CMU(rParent))).nameStr());
69  return val;
70 }
71 
75  return n.realName(value<string>("ChildName"));
76 }
77 
79 bool DDAlgoArguments::find(const string& nam) const {
80  for(xml_coll_t p(element,_U(star)); p; ++p) {
81  string n = p.attr<string>(_U(name));
82  if ( n == nam ) {
83  return true;
84  }
85  }
86  return false;
87 }
88 
90 xml_h DDAlgoArguments::rawArgument(const string& nam) const {
91  for(xml_coll_t p(element,_U(star)); p; ++p) {
92  string n = p.attr<string>(_U(name));
93  if ( n == nam ) {
94  return p;
95  }
96  }
97  except("MyDDCMS","+++ Attempt to access non-existing algorithm option %s[%s]",name.c_str(),nam.c_str());
98  throw runtime_error("DDCMS: Attempt to access non-existing algorithm option.");
99 }
100 
102 string DDAlgoArguments::resolved_scalar_arg(const string& nam) const {
104  xml_h arg = rawArgument(nam);
105  string val = arg.attr<string>(_U(value));
106  return ns.realName(val);
107 }
108 
109 namespace {
110 
112  vector<string> raw_vector(const DDAlgoArguments* a, xml_h arg) {
113  xml_dim_t xp(arg);
114  vector<string> data;
115  cms::DDNamespace ns(a->context);
116  string val = xp.text();
117  string nam = xp.nameStr();
118  string typ = xp.typeStr();
119  int num = xp.attr<int>(_CMU(nEntries));
120  const BasicGrammar& gr = BasicGrammar::instance<vector<string> >();
121 
122  val = '['+ns.realName(val)+']';
123  val = remove_whitespace(val);
124  int res = gr.fromString(&data,val);
125  if ( !res ) {
126  except("MyDDCMS","+++ VectorParam<%s>: %s -> %s [Invalid conversion:%d]",
127  typ.c_str(), nam.c_str(), val.c_str(), res);
128  }
129  else if ( num != (int)data.size() ) {
130  except("MyDDCMS","+++ VectorParam<%s>: %s -> %s [Invalid entry count: %d <> %ld]",
131  typ.c_str(), nam.c_str(), val.c_str(), num, data.size());
132  }
133  printout(DEBUG,"MyDDCMS","+++ VectorParam<%s>: ret=%d %s -> %s",
134  typ.c_str(), res, nam.c_str(), gr.str(&data).c_str());
135  return data;
136  }
137 
138 
139  template <typename T> T __cnv(const string&) { return 0;}
140  template <> double __cnv<double>(const string& arg) { return _toDouble(arg); }
141  template <> float __cnv<float> (const string& arg) { return _toFloat(arg); }
142  template <> long __cnv<long> (const string& arg) { return _toLong(arg); }
143  template <> int __cnv<int> (const string& arg) { return _toInt(arg); }
144  template <> string __cnv<string>(const string& arg) { return arg; }
145 
146  template <typename T> vector<T> __cnvVect(const DDAlgoArguments* a, const char* req_typ, xml_h xp) {
147  cms::DDNamespace ns(a->context);
148  string piece;
149  string nam = xp.attr<string>(_U(name));
150  string typ = xp.attr<string>(_U(type));
151  string val = xp.text();
152  int num = xp.attr<int>(_CMU(nEntries));
153  if ( typ != req_typ ) {
154  except("MyDDCMS",
155  "+++ VectorParam<%s | %s>: %s -> <%s> %s [Incompatible vector-type]",
156  req_typ, typ.c_str(), nam.c_str(), typeName(typeid(T)).c_str(),
157  val.c_str());
158  }
159  vector<T> data;
160  val = remove_whitespace(val);
161  if ( !val.empty() ) val += ',';
162  for(size_t idx=0, idq=val.find(',',idx);
163  idx != string::npos && idq != string::npos;
164  idx=++idq, idq=val.find(',',idx))
165  {
166  piece = ns.realName(val.substr(idx,idq-idx));
167  T d = __cnv<T>(piece);
168  data.push_back(d);
169  }
170  printout(DEBUG,"MyDDCMS","+++ VectorParam<%s>: %s[%d] -> %s",
171  typ.c_str(), nam.c_str(), num, val.c_str());
172  return data;
173  }
174 }
175 
177 namespace cms {
178 
180  template<typename T> T DDAlgoArguments::value(const string& nam) const {
181  return __cnv<T>(resolved_scalar_arg(nam));
182  }
183 
184  template double DDAlgoArguments::value<double>(const string& nam) const;
185  template float DDAlgoArguments::value<float>(const string& nam) const;
186  template long DDAlgoArguments::value<long>(const string& nam) const;
187  template int DDAlgoArguments::value<int>(const string& nam) const;
188  template string DDAlgoArguments::value<string>(const string& nam) const;
189 
191  template<> vector<string> DDAlgoArguments::value<vector<string> >(const string& nam) const
192  { return raw_vector(this,rawArgument(nam)); }
193 
195  template<> vector<double> DDAlgoArguments::value<vector<double> >(const string& nam) const
196  { return __cnvVect<double>(this,"numeric",rawArgument(nam)); }
197 
199  template<> vector<float> DDAlgoArguments::value<vector<float> >(const string& nam) const
200  { return __cnvVect<float>(this,"numeric",rawArgument(nam)); }
201 
203  template<> vector<long> DDAlgoArguments::value<vector<long> >(const string& nam) const
204  { return __cnvVect<long>(this,"numeric",rawArgument(nam)); }
205 
207  template<> vector<int> DDAlgoArguments::value<vector<int> >(const string& nam) const
208  { return __cnvVect<int>(this,"numeric",rawArgument(nam)); }
209 }
210 
212 string DDAlgoArguments::str(const string& nam) const
213 { return this->value<string>(nam); }
214 
216 double DDAlgoArguments::dble(const string& nam) const
217 { return this->value<double>(nam); }
218 
220 int DDAlgoArguments::integer(const string& nam) const
221 { return this->value<int>(nam); }
222 
224 vector<double> DDAlgoArguments::vecDble(const string& nam) const
225 { return this->value<vector<double> >(nam); }
226 
228 vector<int> DDAlgoArguments::vecInt(const string& nam) const
229 { return this->value<vector<int> >(nam); }
230 
232 vector<string> DDAlgoArguments::vecStr(const string& nam) const
233 { 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.
#define _CMU(a)
Definition: DDXMLTags.h:173
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T value(const std::string &name) const
A arg
Definition: Factorize.h:37
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:91
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Definition: value.py:1
Namespace of DDCMS conversion namespace.
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.
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.
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