Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#include <cmath>
#include <iomanip>
#include <iostream>

#include <CLHEP/Units/SystemOfUnits.h>
#include <CLHEP/Vector/Rotation.h>
#include <CLHEP/Vector/RotationInterfaces.h>
#include <CLHEP/Vector/ThreeVector.h>
#include "DetectorDescription/Core/interface/DDRotationMatrix.h"
#include "DetectorDescription/Core/interface/DDTranslation.h"
#include "Math/GenVector/Cartesian3D.h"
#include "Math/GenVector/DisplacementVector3D.h"
#include "Math/GenVector/Rotation3D.h"

typedef CLHEP::Hep3Vector H3V;
typedef CLHEP::HepRotation HRM;
using CLHEP::deg;

using namespace std;

void hrmOut(const HRM& r) {
  cout << "row 1 = " << std::setw(12) << std::fixed << std::setprecision(5) << r.xx();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.xy();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.xz() << endl;
  cout << "row 2 =  " << std::setw(12) << std::fixed << std::setprecision(5) << r.yx();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.yy();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.yz() << endl;
  cout << "row 3 = " << std::setw(12) << std::fixed << std::setprecision(5) << r.zx();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.zy();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << r.zz() << endl;
}

void hepSetAxes(
    H3V& x, H3V& y, H3V& z, double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ) {
  x[0] = sin(thetaX) * cos(phiX);
  x[1] = sin(thetaX) * sin(phiX);
  x[2] = cos(thetaX);
  y[0] = sin(thetaY) * cos(phiY);
  y[1] = sin(thetaY) * sin(phiY);
  y[2] = cos(thetaY);
  z[0] = sin(thetaZ) * cos(phiZ);
  z[1] = sin(thetaZ) * sin(phiZ);
  z[2] = cos(thetaZ);
}

void hepOutVecs(const H3V& x, const H3V& y, const H3V& z) {
  cout << "Vectors used in construction:" << endl;
  cout << "x vector = " << std::setw(12) << std::fixed << std::setprecision(5) << x[0] << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << y[0] << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << z[0] << endl;
  cout << "y vector = " << std::setw(12) << std::fixed << std::setprecision(5) << x[1] << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << y[1] << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << z[1] << endl;
  cout << "z vector = " << std::setw(12) << std::fixed << std::setprecision(5) << x[2] << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << y[2] << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << z[2] << endl;
}

void ddRotOut(const DDRotationMatrix& r) {
  double xx, xy, xz, yx, yy, yz, zx, zy, zz;
  r.GetComponents(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  cout << "row 1 = " << std::setw(12) << std::fixed << std::setprecision(5) << xx;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << xy;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << xz << endl;
  cout << "row 2 =  " << std::setw(12) << std::fixed << std::setprecision(5) << yx;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << yy;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << yz << endl;
  cout << "row 3 = " << std::setw(12) << std::fixed << std::setprecision(5) << zx;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << zy;
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << zz << endl;
  cout << "USING INTERNAL << OPERATOR" << endl;
  cout << r << endl;
  cout << "USING VECTOR COMPONENTS" << endl;
  DD3Vector x, y, z;
  r.GetComponents(x, y, z);
  cout << "col 1 = " << std::setw(12) << std::fixed << std::setprecision(5) << x.X();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << x.Y();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << x.Z() << endl;
  cout << "col 2 =  " << std::setw(12) << std::fixed << std::setprecision(5) << y.X();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << y.Y();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << y.Z() << endl;
  cout << "col 3 = " << std::setw(12) << std::fixed << std::setprecision(5) << z.X();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << z.Y();
  cout << ", " << std::setw(12) << std::fixed << std::setprecision(5) << z.Z() << endl;
}

void ddSetAxes(DD3Vector& x,
               DD3Vector& y,
               DD3Vector& z,
               double thetaX,
               double phiX,
               double thetaY,
               double phiY,
               double thetaZ,
               double phiZ) {
  x.SetX(sin(thetaX) * cos(phiX));
  x.SetY(sin(thetaX) * sin(phiX));
  x.SetZ(cos(thetaX));
  y.SetX(sin(thetaY) * cos(phiY));
  y.SetY(sin(thetaY) * sin(phiY));
  y.SetZ(cos(thetaY));
  z.SetX(sin(thetaZ) * cos(phiZ));
  z.SetY(sin(thetaZ) * sin(phiZ));
  z.SetZ(cos(thetaZ));
}

void ddOutVecs(const DD3Vector& x, const DD3Vector& y, const DD3Vector& z) {
  cout << "Vectors used in construction:" << endl;
  cout << "x vector = " << std::setw(12) << std::fixed << std::setprecision(5) << x.X() << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << x.Y() << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << x.Z() << endl;
  cout << "y vector = " << std::setw(12) << std::fixed << std::setprecision(5) << y.X() << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << y.Y() << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << y.Z() << endl;
  cout << "z vector = " << std::setw(12) << std::fixed << std::setprecision(5) << z.X() << ", " << std::setw(12)
       << std::fixed << std::setprecision(5) << z.Y() << ", " << std::setw(12) << std::fixed << std::setprecision(5)
       << z.Z() << endl;
}

void checkNorm(double check) {
  double tol = 1.0e-3;
  if (1.0 - std::abs(check) > tol) {
    cout << "NOT orthonormal!" << endl;
  } else if (1.0 + check <= tol) {
    cout << "IS Left-handed (reflection)" << endl;
  } else {
    cout << "IS Right-handed (proper)" << endl;
  }
}

int main(int /*argc*/, char** /*argv[]*/) {
  cout << "====================== CLHEP: ========================" << endl;
  // Examples from DD XML
  // <ReflectionRotation name="180R" thetaX="90*deg" phiX="0*deg" thetaY="90*deg" phiY="90*deg" thetaZ="180*deg" phiZ="0*deg" />
  {
    H3V x, y, z;
    hepSetAxes(x, y, z, 90 * deg, 0 * deg, 90 * deg, 90 * deg, 180 * deg, 0 * deg);
    CLHEP::HepRotation R;
    R.rotateAxes(x, y, z);
    HRM ddr(R);
    cout << "   *** REFLECTION *** " << endl;
    checkNorm((x.cross(y)) * z);
    hepOutVecs(x, y, z);
    cout << "Matrix output built up from vectors:" << endl;
    hrmOut(ddr);
    cout << "Matrix build from CLHEP::HepRep3x3 to preserve left-handedness:" << endl;
    CLHEP::HepRep3x3 temp(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z());  //matrix representation
    HRM ddr2(temp);
    hrmOut(ddr2);
  }
  // <Rotation name="RM1509" thetaX="90*deg" phiX="-51.39999*deg" thetaY="90*deg" phiY="38.60001*deg" thetaZ="0*deg" phiZ="0*deg" />
  {
    H3V x, y, z;
    hepSetAxes(x, y, z, 90 * deg, -51.39999 * deg, 90 * deg, 38.60001 * deg, 0 * deg, 0 * deg);
    CLHEP::HepRotation R;
    R.rotateAxes(x, y, z);
    HRM ddr(R);
    cout << "   *** ROTATION *** " << endl;
    checkNorm((x.cross(y)) * z);
    hepOutVecs(x, y, z);
    cout << "Matrix output built up from vectors:" << endl;
    hrmOut(ddr);
    cout << "Matrix build from CLHEP::HepRep3x3 to preserve left-handedness:" << endl;
    CLHEP::HepRep3x3 temp(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z());  //matrix representation
    HRM ddr2(temp);
    hrmOut(ddr2);
  }

  cout << "====================== ROOT::Math ========================" << endl;
  // <ReflectionRotation name="180R" thetaX="90*deg" phiX="0*deg" thetaY="90*deg" phiY="90*deg" thetaZ="180*deg" phiZ="0*deg" />
  {
    DD3Vector x, y, z;
    ddSetAxes(x, y, z, 90 * deg, 0 * deg, 90 * deg, 90 * deg, 180 * deg, 0 * deg);
    DDRotationMatrix R(x, y, z);
    cout << "   *** REFLECTION *** " << endl;
    checkNorm((x.Cross(y)).Dot(z));
    ddOutVecs(x, y, z);
    cout << "Matrix output built up from vectors:" << endl;
    ddRotOut(R);
    cout << "Matrix built to preserve left-handedness:" << endl;
    DDRotationMatrix temp(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z());  //matrix representation
    ddRotOut(temp);
  }
  // <Rotation name="RM1509" thetaX="90*deg" phiX="-51.39999*deg" thetaY="90*deg" phiY="38.60001*deg" thetaZ="0*deg" phiZ="0*deg" />
  {
    DD3Vector x, y, z;
    ddSetAxes(x, y, z, 90 * deg, -51.39999 * deg, 90 * deg, 38.60001 * deg, 0 * deg, 0 * deg);
    DDRotationMatrix R(x, y, z);
    cout << "   *** ROTATION *** " << endl;
    checkNorm((x.Cross(y)).Dot(z));
    ddOutVecs(x, y, z);
    cout << "Matrix output built up from vectors:" << endl;
    ddRotOut(R);
    cout << "Matrix built to preserve left-handedness:" << endl;
    DDRotationMatrix temp(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z());  //matrix representation
    ddRotOut(temp);
  }
}