diff --git a/DetectorDescription/DDCMS/interface/DDNamespace.h b/DetectorDescription/DDCMS/interface/DDNamespace.h index ddadd38665444..805819e8b31a1 100644 --- a/DetectorDescription/DDCMS/interface/DDNamespace.h +++ b/DetectorDescription/DDCMS/interface/DDNamespace.h @@ -10,6 +10,12 @@ namespace cms { + namespace rotation_utils { + std::string rotHash(const Double_t* rot, bool reflection = false); + std::string rotHash(const dd4hep::Rotation3D& rot); + double roundBinary(double value); + } + class DDParsingContext; using DDVectorsMap = std::unordered_map>; diff --git a/DetectorDescription/DDCMS/interface/DDParsingContext.h b/DetectorDescription/DDCMS/interface/DDParsingContext.h index fcd2912fb183e..0a19c4f7eddf1 100644 --- a/DetectorDescription/DDCMS/interface/DDParsingContext.h +++ b/DetectorDescription/DDCMS/interface/DDParsingContext.h @@ -58,10 +58,6 @@ namespace cms { double fraction; }; - static std::string rotHash(const Double_t* rot, bool reflection = false); - static std::string rotHash(const dd4hep::Rotation3D& rot); - static double roundBinary(double value); - // Debug flags bool debug_includes = false; bool debug_constants = false; diff --git a/DetectorDescription/DDCMS/src/DDNamespace.cc b/DetectorDescription/DDCMS/src/DDNamespace.cc index a38783dacefd7..7e4db1871cd8b 100644 --- a/DetectorDescription/DDCMS/src/DDNamespace.cc +++ b/DetectorDescription/DDCMS/src/DDNamespace.cc @@ -1,5 +1,6 @@ #include "DetectorDescription/DDCMS/interface/DDNamespace.h" #include "DetectorDescription/DDCMS/interface/DDParsingContext.h" +#include "DataFormats/Math/interface/Rounding.h" #include "DD4hep/Path.h" #include "DD4hep/Printout.h" #include "XML/XML.h" @@ -11,6 +12,41 @@ using namespace std; using namespace cms; +double cms::rotation_utils::roundBinary(double value) { + value = cms_rounding::roundIfNear0(value); + static constexpr double roundingVal = 1 << 14; + value = (round(value * roundingVal) / roundingVal); + // Set -0 to 0 + return (cms_rounding::roundIfNear0(value)); +} + +std::string cms::rotation_utils::rotHash(const Double_t* rot, bool reflection) { + std::string hashVal; + // If the rotation is a reflection, try to find matching RH rotation. + // Multiply 1,1,-1 z-reflection matrix from left to get RH coord system. + // RH rot = z-reflection * LH rot + // The RH rotation is the final rotation that is actually applied + for (int row = 0; row <= 2; ++row) { + const double signFac = ((row == 2 && reflection) ? -1. : 1.); + for (int col = 0; col <= 2; ++col) { + hashVal += std::to_string(roundBinary(signFac * rot[(3 * row) + col])); + } + } + return (hashVal); +} + +std::string cms::rotation_utils::rotHash(const dd4hep::Rotation3D& rot) { + std::string hashVal; + std::vector matrix; + matrix.assign(9, 0.); + rot.GetComponents(matrix.begin()); + for (double val : matrix) { + hashVal += std::to_string(roundBinary(val)); + } + return (hashVal); +} + + DDNamespace::DDNamespace(DDParsingContext* context, xml_h element) : m_context(context) { dd4hep::Path path(xml_handler_t::system_path(element)); m_name = path.filename().substr(0, path.filename().rfind('.')); @@ -121,7 +157,7 @@ void DDNamespace::addRotation(const string& name, const dd4hep::Rotation3D& rot) string n = prepend(name); m_context->rotations[n] = rot; if (m_context->makePayload) { - string hashVal = m_context->rotHash(rot); + string hashVal = cms::rotation_utils::rotHash(rot); m_context->rotRevMap[hashVal] = n; } } diff --git a/DetectorDescription/DDCMS/src/DDParsingContext.cc b/DetectorDescription/DDCMS/src/DDParsingContext.cc deleted file mode 100644 index 8e94a50c3bd01..0000000000000 --- a/DetectorDescription/DDCMS/src/DDParsingContext.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "DetectorDescription/DDCMS/interface/DDParsingContext.h" -#include "DataFormats/Math/interface/Rounding.h" - - -using namespace cms; - -double DDParsingContext::roundBinary(double value) { - value = cms_rounding::roundIfNear0(value); - static constexpr double roundingVal = 1 << 14; - value = (round(value * roundingVal) / roundingVal); - // Set -0 to 0 - return (cms_rounding::roundIfNear0(value)); -} - -std::string DDParsingContext::rotHash(const Double_t* rot, bool reflection) { - std::string hashVal; - // If the rotation is a reflection, try to find matching RH rotation. - // Multiply 1,1,-1 z-reflection matrix from left to get RH coord system. - // RH rot = z-reflection * LH rot - // The RH rotation is the final rotation that is actually applied - for (int row = 0; row <= 2; ++row) { - const double signFac = ((row == 2 && reflection) ? -1. : 1.); - for (int col = 0; col <= 2; ++col) { - hashVal += std::to_string(roundBinary(signFac * rot[(3 * row) + col])); - } - } - return (hashVal); -} - -std::string DDParsingContext::rotHash(const dd4hep::Rotation3D& rot) { - std::string hashVal; - std::vector matrix; - matrix.assign(9, 0.); - rot.GetComponents(matrix.begin()); - for (double val : matrix) { - hashVal += std::to_string(roundBinary(val)); - } - return (hashVal); -} diff --git a/DetectorDescription/OfflineDBLoader/plugins/OutputDD4hepToDDL.cc b/DetectorDescription/OfflineDBLoader/plugins/OutputDD4hepToDDL.cc index 9e25eb070d3bf..9d313b69c528e 100644 --- a/DetectorDescription/OfflineDBLoader/plugins/OutputDD4hepToDDL.cc +++ b/DetectorDescription/OfflineDBLoader/plugins/OutputDD4hepToDDL.cc @@ -127,7 +127,7 @@ void OutputDD4hepToDDL::beginRun(const edm::Run &, edm::EventSetup const &es) { out.position(*logVol, *childVol, *det->m_context, *m_xos); } } - } + } else std::cout << "Reflect volume = " << volName << std::endl; } (*m_xos) << "" << std::endl; diff --git a/DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc b/DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc index cab1131abd878..daaea9dc4f833 100644 --- a/DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc +++ b/DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc @@ -39,58 +39,47 @@ static inline constexpr NumType convertGPerCcToMgPerCc(NumType gPerCc) // g/cm^ return (gPerCc * 1000.); } -static void addRotWithNewName(cms::DDNamespace& ns, std::string& name, const G4RotationMatrix& rot) { - dd4hep::Rotation3D rot2(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()); - name = name + "RLast"; - ns.addRotation(name, rot2); -} +namespace cms::rotation_utils { + static void addRotWithNewName(cms::DDNamespace& ns, std::string& name, const G4RotationMatrix& rot) { + dd4hep::Rotation3D rot2(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()); + name = name + "RLast"; + ns.addRotation(name, rot2); + } -namespace { - class DDParsingContextG4 : public cms::DDParsingContext { - // Specialization of DDParsingContext for G4 rotations - public: - static std::string rotHash(const G4RotationMatrix& rot, bool reflection = false) { - std::string hashVal; - const double signFac = (reflection ? -1. : 1.); - // If the rotation is a reflection, try to find matching RH rotation. - // Multiply 1,1,-1 z-reflection matrix from left to get RH coord system. - // RH rot = z-reflection * LH rot - // The RH rotation is the final rotation that is actually applied - hashVal += std::to_string(roundBinary(rot.xx())); - hashVal += std::to_string(roundBinary(rot.xy())); - hashVal += std::to_string(roundBinary(rot.xz())); - hashVal += std::to_string(roundBinary(rot.yx())); - hashVal += std::to_string(roundBinary(rot.yy())); - hashVal += std::to_string(roundBinary(rot.yz())); - hashVal += std::to_string(roundBinary(rot.zx() * signFac)); // For reflection,negate z row to give RH coord sys - hashVal += std::to_string(roundBinary(rot.zy() * signFac)); - hashVal += std::to_string(roundBinary(rot.zz() * signFac)); - return (hashVal); - } - }; -} // namespace - -// Brute force way to get overloading of inherited static member function. Is there a more elegant way? -static std::string rotationHash(const G4RotationMatrix& rot, bool reflection = false) { - return (DDParsingContextG4::rotHash(rot, reflection)); -} -static std::string rotationHash(const Double_t* rot, bool reflection = false) { - return (cms::DDParsingContext::rotHash(rot, reflection)); -} + static std::string rotHash(const G4RotationMatrix& rot, bool reflection = false) { + std::string hashVal; + const double signFac = (reflection ? -1. : 1.); + // If the rotation is a reflection, try to find matching RH rotation. + // Multiply 1,1,-1 z-reflection matrix from left to get RH coord system. + // RH rot = z-reflection * LH rot + // The RH rotation is the final rotation that is actually applied + hashVal += std::to_string(roundBinary(rot.xx())); + hashVal += std::to_string(roundBinary(rot.xy())); + hashVal += std::to_string(roundBinary(rot.xz())); + hashVal += std::to_string(roundBinary(rot.yx())); + hashVal += std::to_string(roundBinary(rot.yy())); + hashVal += std::to_string(roundBinary(rot.yz())); + hashVal += std::to_string(roundBinary(rot.zx() * signFac)); // For reflection,negate z row to give RH coord sys + hashVal += std::to_string(roundBinary(rot.zy() * signFac)); + hashVal += std::to_string(roundBinary(rot.zz() * signFac)); + return (hashVal); + } -template -static const std::string& rotName(const T& rot, const cms::DDParsingContext& context) { - std::string hashVal = rotationHash(rot); - auto rotNameIter = context.rotRevMap.find(hashVal); - if (rotNameIter != context.rotRevMap.end()) - return (rotNameIter->second); - hashVal = rotationHash(rot, true); // Look for reflection rotation - rotNameIter = context.rotRevMap.find(hashVal); - if (rotNameIter != context.rotRevMap.end()) - return (rotNameIter->second); - static const std::string nullStr{"NULL"}; - return (nullStr); -} + template + static const std::string& rotName(const T& rot, const cms::DDParsingContext& context) { + std::string hashVal = rotHash(rot, true); // Look for reflection rotation + auto rotNameIter = context.rotRevMap.find(hashVal); + if (rotNameIter != context.rotRevMap.end()) + return (rotNameIter->second); + hashVal = rotHash(rot); + rotNameIter = context.rotRevMap.find(hashVal); + if (rotNameIter != context.rotRevMap.end()) + return (rotNameIter->second); + static const std::string nullStr{"NULL"}; + return (nullStr); + } + +} // namespace cms::rotation_utils std::string DDCoreToDDXMLOutput::trimShapeName(const std::string& solidName) { size_t trimPt = solidName.find("_shape_0x"); @@ -123,7 +112,7 @@ void DDCoreToDDXMLOutput::solid(const dd4hep::Solid& solid, const cms::DDParsing xos << " y=\"" << trans[1] << "*mm\""; xos << " z=\"" << trans[2] << "*mm\""; xos << "/>" << std::endl; - std::string rotNameStr = rotName(rs.rightMatrix()->GetRotationMatrix(), context); + std::string rotNameStr = cms::rotation_utils::rotName(rs.rightMatrix()->GetRotationMatrix(), context); xos << "" << std::endl; if (shape == cms::DDSolidShape::ddunion) { xos << "" << std::endl; @@ -372,9 +361,9 @@ void DDCoreToDDXMLOutput::solid(const DDSolid& solid, std::ostream& xos) { xos << " y=\"" << rs.translation().Y() << "*mm\""; xos << " z=\"" << rs.translation().Z() << "*mm\""; xos << "/>" << std::endl; - std::string rotNameStr = rs.rotation().toString(); - if (rotNameStr == ":") { - rotNameStr = "gen:ID"; + std::string rotName = rs.rotation().toString(); + if (rotName == ":") { + rotName = "gen:ID"; } xos << "" << std::endl; if (solid.shape() == DDSolidShape::ddunion) { @@ -699,10 +688,10 @@ void DDCoreToDDXMLOutput::rotation(const DDRotation& rotation, std::ostream& xos double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system // this must be -1 bool reflection((1. - check) > tol); - std::string rotNameStr = rotation.toString(); - if (rotNameStr == ":") { + std::string rotName = rotation.toString(); + if (rotName == ":") { if (!rotn.empty()) { - rotNameStr = rotn; + rotName = rotn; std::cout << "about to try to make a new DDRotation... should fail!" << std::endl; DDRotation rot(DDName(rotn), std::make_unique(rotation.rotation())); std::cout << "new rotation: " << rot << std::endl; @@ -716,7 +705,7 @@ void DDCoreToDDXMLOutput::rotation(const DDRotation& rotation, std::ostream& xos xos << "isIdentity()) { - std::string rotNameStr = rotName(*rot, context); + std::string rotNameStr = cms::rotation_utils::rotName(*rot, context); if (rotNameStr == "NULL") { if (assembly) { rotNameStr = child.GetName(); // Use unique assembly volume name } else rotNameStr = childName + parent.GetName(); cms::DDNamespace nameSpace(context); - addRotWithNewName(nameSpace, rotNameStr, *rot); + cms::rotation_utils::addRotWithNewName(nameSpace, rotNameStr, *rot); } xos << "" << std::endl; } @@ -862,17 +851,17 @@ void DDCoreToDDXMLOutput::position(const DDLogicalPart& parent, DDPosData* edgeToChild, int& rotNameSeed, std::ostream& xos) { - std::string rotNameStr = edgeToChild->ddrot().toString(); + std::string rotName = edgeToChild->ddrot().toString(); DDRotationMatrix myIDENT; xos << "copyno() << "\">" << std::endl; xos << "" << std::endl; xos << "" << std::endl; if ((edgeToChild->ddrot().rotation()) != myIDENT) { - if (rotNameStr == ":") { + if (rotName == ":") { rotation(edgeToChild->ddrot(), xos); } else { - xos << "" << std::endl; + xos << "" << std::endl; } } // else let default Rotation matrix be created? using namespace cms_rounding;