CMS 3D CMS Logo

DDLRotationAndReflection.cc

Go to the documentation of this file.
00001 /***************************************************************************
00002                           DDLRotationAndReflection.cc  -  description
00003                              -------------------
00004     begin                : Tue Aug 6 2002
00005     email                : case@ucdhep.ucdavis.edu
00006 ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *           DDDParser sub-component of DDD                                *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 
00015 
00016 // -------------------------------------------------------------------------
00017 // Includes
00018 // -------------------------------------------------------------------------
00019 #include "DDLRotationAndReflection.h"
00020 #include "DDLElementRegistry.h"
00021 
00022 // DDCore dependencies
00023 #include "DetectorDescription/Core/interface/DDPosPart.h"
00024 #include "DetectorDescription/Core/interface/DDName.h"
00025 #include "DetectorDescription/Core/interface/DDTransform.h"
00026 #include "DetectorDescription/Base/interface/DDdebug.h"
00027 #include "DetectorDescription/Base/interface/DDException.h"
00028 
00029 #include "DetectorDescription/ExprAlgo/interface/ExprEvalSingleton.h"
00030 
00031 //CLHEP dependency
00032 #include "CLHEP/Units/SystemOfUnits.h"
00033 
00034 #include <string>
00035 #include <cmath>
00036 
00037 // CMSSW Framework Dependency
00038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00039 
00040 // Default constructor
00041 DDLRotationAndReflection::DDLRotationAndReflection() 
00042 {
00043 }
00044 
00045 // Default destructor
00046 DDLRotationAndReflection::~DDLRotationAndReflection()
00047 {
00048 }
00049 
00050 void DDLRotationAndReflection::processElement (const std::string& name, const std::string& nmspace)
00051 {
00052 
00053   DCOUT_V('P', "DDLRotationAndReflection::processElement started " << name);
00054 
00055   DD3Vector x = makeX(nmspace);
00056   DD3Vector y = makeY(nmspace);
00057   DD3Vector z = makeZ(nmspace);
00058 
00059   DDXMLAttribute atts = getAttributeSet();
00060 
00061 
00062   if ((name == "Rotation") && isLeftHanded(x, y, z, nmspace) == 0)
00063     {
00064       DDRotationMatrix* ddr = new DDRotationMatrix(x, y, z);
00065       DDRotation ddrot = DDrot(getDDName(nmspace), ddr);
00066       DCOUT_V ('p', "Rotation created: " << ddrot << std::endl);
00067     }
00068   else if ((name == "Rotation")  && isLeftHanded(x, y, z, nmspace) == 1)
00069     {
00070       std::string msg("\nDDLRotationAndReflection attempted to make a");
00071       msg += " left-handed rotation with a Rotation element. If";
00072       msg += " you meant to make a reflection, use ReflectionRotation";
00073       msg += " elements, otherwise, please check your matrix.  Other";
00074       msg += " errors may follow.  Rotation  matrix not created.";
00075       edm::LogError("DetectorDescription_Parser_Rotation_and_Reflection") << msg << std::endl; // this could become a throwWarning or something.
00076     }
00077   else if (name == "ReflectionRotation" && isLeftHanded(x, y, z, nmspace) == 1) 
00078     {
00079       ExprEvalInterface & ev = ExprEvalSingleton::instance();
00080       DDRotation ddrot = 
00081         DDrotReflect(getDDName(nmspace)
00082                      , ev.eval(nmspace, atts.find("thetaX")->second)
00083                      , ev.eval(nmspace, atts.find("phiX")->second)
00084                      , ev.eval(nmspace, atts.find("thetaY")->second)
00085                      , ev.eval(nmspace, atts.find("phiY")->second)
00086                      , ev.eval(nmspace, atts.find("thetaZ")->second)
00087                      , ev.eval(nmspace, atts.find("phiZ")->second));
00088       DCOUT_V ('p', "Rotation created: " << ddrot << std::endl);
00089     }
00090   else if (name == "ReflectionRotation" && isLeftHanded(x, y, z, nmspace) == 0)
00091     {
00092       std::string msg("WARNING:  Attempted to make a right-handed");
00093       msg += " rotation using a ReflectionRotation element. ";
00094       msg += " If you meant to make a Rotation, use Rotation";
00095       msg += " elements, otherwise, please check your matrix.";
00096       msg += "  Other errors may follow.  ReflectionRotation";
00097       msg += " matrix not created.";
00098       edm::LogError("DetectorDescription_Parser_Rotation_and_Reflection") << msg << std::endl; // this could be a throwWarning or something.
00099     }
00100   else
00101     {
00102       std::string msg = "\nDDLRotationAndReflection::processElement tried to process wrong element.";
00103       throwError(msg);
00104     }
00105   // after a rotation or reflection rotation has been processed, clear it
00106   clear();
00107 
00108   DCOUT_V('P', "DDLRotationAndReflection::processElement completed");
00109 }
00110 
00111 
00112 // returns 1 if it is a left-handed CLHEP rotation matrix, 0 if not, but is okay, -1 if 
00113 // it is not an orthonormal matrix.
00114 //
00115 // Upon encountering the end tag of a Rotation element, we've got to feed
00116 // the appropriate rotation in to the DDCore.  This is an attempt to do so.
00117 //
00118 // Basically, I cannibalized code from g3tog4 (see https link below) and then
00119 // provided the information from our DDL to the same calls.  Tim Cox showed me
00120 // how to build the rotation matrix (mathematically) and the g3tog4 code basically
00121 // did the rest.
00122 //
00123 
00124 int DDLRotationAndReflection::isLeftHanded (DD3Vector x, DD3Vector y, DD3Vector z, const std::string & nmspace)
00125 {
00126   DCOUT_V('P', "DDLRotation::isLeftHanded started");
00127 
00128   int ret = 0;
00129 
00130   /**************** copied and cannibalized code:
00131  
00132   from g3tog4
00133  
00134   https://atlassw1.phy.bnl.gov/lxr/source/external/geant4.3.1/source/g3tog4/src/G4gsrotm.cc
00135 
00136  48         // Construct unit std::vectors 
00137  49     
00138  50     G4ThreeVector x(sin(th1r)*cos(phi1r), sin(th1r)*sin(phi1r), cos(th1r)->second;
00139  51     G4ThreeVector y(sin(th2r)*cos(phi2r), sin(th2r)*sin(phi2r), cos(th2r));
00140  52     G4ThreeVector z(sin(th3r)*cos(phi3r), sin(th3r)*sin(phi3r), cos(th3r));
00141  53 
00142  54         // check for orthonormality and left-handedness
00143  55 
00144  56     G4double check = (x.cross(y))*z;
00145  57     G4double tol = 1.0e-3;
00146  58         
00147  59     if (1-abs(check)>tol) {
00148  60         G4cerr << "Coordinate axes forming rotation matrix "
00149  61                << irot << " are not orthonormal.(" << 1-abs(check) << ")" 
00150  62          << G4std::endl;
00151  63         G4cerr << " thetaX=" << theta1;
00152  64         G4cerr << " phiX=" << phi1;
00153  65         G4cerr << " thetaY=" << theta2;
00154  66         G4cerr << " phiY=" << phi2;
00155  67         G4cerr << " thetaZ=" << theta3;
00156  68         G4cerr << " phiZ=" << phi3;
00157  69         G4cerr << G4std::endl;
00158  70         G4Exception("G4gsrotm error");
00159  71     }
00160  72     else if (1+check<=tol) {
00161  73         G4cerr << "G4gsrotm warning: coordinate axes forming rotation "
00162  74                << "matrix " << irot << " are left-handed" << G4std::endl;
00163  75     }   
00164  76
00165  77     G3toG4RotationMatrix* rotp = new G3toG4RotationMatrix;
00166  78 
00167  79     rotp->SetRotationMatrixByRow(x, y, z);
00168 
00169   ****************/
00170 
00171 
00172   // check for orthonormality and left-handedness
00173   
00174   double check = (x.Cross(y)).Dot(z);
00175   double tol = 1.0e-3;
00176   ExprEvalInterface & ev = ExprEvalSingleton::instance();
00177   DDXMLAttribute atts = getAttributeSet();
00178   
00179   if (1.0-std::abs(check)>tol) {
00180     std::cout << "DDLRotationAndReflection Coordinate axes forming rotation matrix "
00181               << getDDName(nmspace) 
00182               << " are not orthonormal.(tolerance=" << tol 
00183               << " check=" << std::abs(check)  << ")" 
00184               << std::endl
00185               << " thetaX=" << (atts.find("thetaX")->second) 
00186               << ' ' << ev.eval(nmspace, atts.find("thetaX")->second)/deg << std::endl
00187               << " phiX=" << (atts.find("phiX")->second) 
00188               << ' ' << ev.eval(nmspace, atts.find("phiX")->second)/deg << std::endl
00189               << " thetaY=" << (atts.find("thetaY")->second) 
00190               << ' ' << ev.eval(nmspace, atts.find("thetaY")->second)/deg << std::endl
00191               << " phiY=" << (atts.find("phiY")->second)
00192               << ' ' << ev.eval(nmspace, atts.find("phiY")->second)/deg << std::endl
00193               << " thetaZ=" << (atts.find("thetaZ")->second)
00194               << ' ' << ev.eval(nmspace, atts.find("thetaZ")->second)/deg << std::endl
00195               << " phiZ=" << (atts.find("phiZ")->second)
00196               << ' ' << ev.eval(nmspace, atts.find("phiZ")->second)/deg 
00197               << std::endl
00198               << "  WAS NOT CREATED!" << std::endl;
00199     ret = -1;
00200   }
00201   else if (1.0+check<=tol) {
00202     ret = 1;    
00203   }
00204   DCOUT_V('P', "DDLRotation::isLeftHanded completed");
00205   return ret;
00206 }
00207 
00208 DD3Vector DDLRotationAndReflection::makeX(std::string nmspace)
00209 {
00210   DD3Vector x;
00211   DDXMLAttribute atts = getAttributeSet();
00212   if (atts.find("thetaX") != atts.end())
00213     {
00214       ExprEvalInterface & ev = ExprEvalSingleton::instance(); 
00215       double thetaX = ev.eval(nmspace, atts.find("thetaX")->second.c_str());
00216       double phiX = ev.eval(nmspace, atts.find("phiX")->second.c_str());
00217       // colx
00218       x.SetX(sin(thetaX) * cos(phiX));
00219       x.SetY(sin(thetaX) * sin(phiX));
00220       x.SetZ(cos(thetaX));
00221     }
00222   return x;
00223 }
00224 
00225 DD3Vector DDLRotationAndReflection::makeY(std::string nmspace)
00226 {
00227   DD3Vector y;
00228   DDXMLAttribute atts = getAttributeSet();
00229   if (atts.find("thetaY") != atts.end())
00230     {
00231       ExprEvalInterface & ev = ExprEvalSingleton::instance(); 
00232       double thetaY = ev.eval(nmspace, atts.find("thetaY")->second.c_str());
00233       double phiY = ev.eval(nmspace, atts.find("phiY")->second.c_str());
00234       
00235       // coly
00236       y.SetX(sin(thetaY) * cos(phiY));
00237       y.SetY(sin(thetaY) * sin(phiY));
00238       y.SetZ(cos(thetaY));
00239     }
00240   return y;
00241 }
00242 
00243 DD3Vector DDLRotationAndReflection::makeZ(std::string nmspace)
00244 {
00245   DD3Vector z;
00246   DDXMLAttribute atts = getAttributeSet();
00247   if (atts.find("thetaZ") != atts.end())
00248     {
00249       ExprEvalInterface & ev = ExprEvalSingleton::instance(); 
00250       double thetaZ = ev.eval(nmspace, atts.find("thetaZ")->second.c_str());
00251       double phiZ = ev.eval(nmspace, atts.find("phiZ")->second.c_str());
00252       
00253       // colz
00254       z.SetX(sin(thetaZ) * cos(phiZ));
00255       z.SetY(sin(thetaZ) * sin(phiZ));
00256       z.SetZ(cos(thetaZ));
00257     }
00258   return z;
00259 }

Generated on Tue Jun 9 17:32:24 2009 for CMSSW by  doxygen 1.5.4