CMS 3D CMS Logo

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