CMS 3D CMS Logo

DDAngular.cc
Go to the documentation of this file.
1 #include "DD4hep/DetFactoryHelper.h"
5 #include <Math/Rotation3D.h>
6 #include <Math/AxisAngle.h>
7 #include <Math/DisplacementVector3D.h>
8 
9 using namespace std;
10 using namespace dd4hep;
11 using namespace cms;
12 using namespace geant_units;
13 using namespace geant_units::operators;
14 
15 using DD3Vector = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double> >;
16 using DDAxisAngle = ROOT::Math::AxisAngle;
17 
18 namespace {
19  DD3Vector
20  fUnitVector( double theta, double phi )
21  {
22  return DD3Vector( cos( phi ) * sin( theta ),
23  sin( phi ) * sin( theta ),
24  cos( theta ));
25  }
26 }
27 
28 static long algorithm( Detector& /* description */,
30  xml_h e,
31  SensitiveDetector& /* sens */)
32 {
33  cms::DDNamespace ns( ctxt, e, true );
34  DDAlgoArguments args( ctxt, e );
35 
36  int n = args.value<int>("N");
37  int startCopyNo = args.find("StartCopyNo") ? args.value<int>("StartCopyNo") : 1;
38  int incrCopyNo = args.find("IncrCopyNo") ? args.value<int>("IncrCopyNo") : 1;
39  double rangeAngle = args.value<double>("RangeAngle");
40  double startAngle = args.value<double>("StartAngle");
41  double radius = args.value<double>("Radius");
42  vector<double> center = args.value<vector<double> >("Center");
43  vector<double> rotateSolid = args.value<vector<double> >("RotateSolid");
44  Volume mother = ns.volume(args.parentName());
45  string childName = args.value<string>("ChildName");
46  if( strchr( childName.c_str(), NAMESPACE_SEP ) == nullptr )
47  childName = ns.name() + childName;
48  Volume child = ns.volume( childName );
49 
50  double delta = 0e0;
51 
52  if( fabs( rangeAngle - 360.0_deg ) < 0.001_deg ) {
53  delta = rangeAngle/double( n );
54  } else if( n > 1 ) {
55  delta = rangeAngle/double( n - 1 );
56  }
57 
58  LogDebug("DDAlgorithm") << "debug: Parameters for positioning:: n "
59  << n << " Start, Range, Delta "
60  << convertRadToDeg( startAngle ) << " "
61  << convertRadToDeg( rangeAngle ) << " " << convertRadToDeg( delta )
62  << " Radius " << radius << " Centre " << center[0]
63  << ", " << center[1] << ", " << center[2]
64  << ", Rotate solid " << rotateSolid[0] << ", " << rotateSolid[1]
65  << ", " << rotateSolid[2];
66  LogDebug("DDAlgorithm") << "debug: Parent " << mother.name()
67  << "\tChild " << child.name() << " NameSpace "
68  << ns.name();
69 
70  Rotation3D solidRot = Rotation3D(); // Identity rotation
71  auto sz = rotateSolid.size();
72  if( sz%3 )
73  {
74  LogDebug( "DDAlgorithm" ) << "\trotateSolid must occur 3*n times (defining n subsequent rotations)\n"
75  << "\t currently it appears " << sz << " times!\n";
76  }
77  for( unsigned int i = 0; i < sz; i += 3 )
78  {
79  if(( i > 180._deg ) || ( i < 0._deg ))
80  {
81  LogDebug( "DDAlgorithm" ) << "\trotateSolid \'theta\' must be in range [0,180*deg]\n"
82  << "\t currently it is " << convertRadToDeg( i )
83  << "*deg in rotateSolid[" << double(i) << "]!\n";
84  }
85  DDAxisAngle temp( fUnitVector( rotateSolid[i], rotateSolid[i + 1] ),
86  rotateSolid[i + 2] );
87  LogDebug( "DDAlgorithm" ) << " rotsolid[" << i << "] axis=" << temp.Axis() << " rot.angle=" << convertRadToDeg( temp.Angle() );
88  solidRot = temp * solidRot;
89  }
90 
91  double theta = 90._deg;
92  int copy = startCopyNo;
93  double phi = startAngle;
94  for( int i = 0; i < n; ++i )
95  {
96  double phix = phi;
97  double phiy = phix + 90._deg;
98  double phideg = convertRadToDeg( phix );
99 
100  Rotation3D rotation = makeRotation3D( theta, phix, theta, phiy, 0., 0. ) * solidRot;
101  string rotstr = ns.nsName( child.name()) + std::to_string( phideg * 10. );
102  auto irot = ctxt.rotations.find( ns.prepend( rotstr ));
103  if( irot != ctxt.rotations.end()) {
104  rotation = ns.rotation( ns.prepend( rotstr ));
105  }
106 
107  double xpos = radius * cos( phi ) + center[0];
108  double ypos = radius * sin( phi ) + center[1];
109  double zpos = center[2];
110  Position tran( xpos, ypos, zpos );
111  mother.placeVolume( child, copy, Transform3D( rotation, tran ));
112  LogDebug("DDAlgorithm") << "test " << child.name() << " number "
113  << copy << " positioned in " << mother.name() << " at "
114  << tran << " with " << rotstr << ": " << rotation;
115  copy += incrCopyNo;
116  phi += delta;
117  }
118  return 1;
119 }
120 
121 // first argument is the type from the xml file
122 DECLARE_DDCMS_DETELEMENT( DDCMS_global_DDAngular, algorithm )
#define LogDebug(id)
dbl * delta
Definition: mlp_gen.cc:36
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:193
def copy(args, dbName)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T value(const std::string &name) const
Geom::Theta< T > theta() const
constexpr NumType convertRadToDeg(NumType radians)
Definition: GeantUnits.h:98
dd4hep::Rotation3D makeRotation3D(double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
tbb::concurrent_unordered_map< std::string, dd4hep::Rotation3D > rotations
std::string_view name() const
Definition: DDNamespace.h:65
static std::string nsName(const std::string &)
Definition: DDNamespace.cc:88
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
A DD Translation is currently implemented with Root Vector3D.
Definition: DDTranslation.h:6
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:63
dd4hep::Volume Volume
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
Definition: DDAngular.cc:15
static long algorithm(Detector &, cms::DDParsingContext &ctxt, xml_h e, SensitiveDetector &)
Definition: DDAngular.cc:28
Namespace of DDCMS conversion namespace.
#define DECLARE_DDCMS_DETELEMENT(name, func)
Definition: DDPlugins.h:31
const dd4hep::Rotation3D & rotation(const std::string &name) const
Definition: DDNamespace.cc:138
bool find(const std::string &name) const
Check the existence of an argument by name.
std::string parentName() const
Access value of rParent child node.
ROOT::Math::AxisAngle DDAxisAngle
#define NAMESPACE_SEP
Definition: DDNamespace.h:77