Skip to content

Commit

Permalink
Re-arrange rotation utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cvuosalo committed Jun 14, 2021
1 parent a9a4d4e commit b70681b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 108 deletions.
6 changes: 6 additions & 0 deletions DetectorDescription/DDCMS/interface/DDNamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::vector<double>>;

Expand Down
4 changes: 0 additions & 4 deletions DetectorDescription/DDCMS/interface/DDParsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 37 additions & 1 deletion DetectorDescription/DDCMS/src/DDNamespace.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<double> 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('.'));
Expand Down Expand Up @@ -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;
}
}
Expand Down
39 changes: 0 additions & 39 deletions DetectorDescription/DDCMS/src/DDParsingContext.cc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) << "</PosPartSection>" << std::endl;

Expand Down
115 changes: 52 additions & 63 deletions DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
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 <typename T>
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");
Expand Down Expand Up @@ -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 << "<rRotation name=\"" << rotNameStr << "\"/>" << std::endl;
if (shape == cms::DDSolidShape::ddunion) {
xos << "</UnionSolid>" << std::endl;
Expand Down Expand Up @@ -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 << "<rRotation name=\"" << rs.rotation().toString() << "\"/>" << std::endl;
if (solid.shape() == DDSolidShape::ddunion) {
Expand Down Expand Up @@ -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<DDRotationMatrix>(rotation.rotation()));
std::cout << "new rotation: " << rot << std::endl;
Expand All @@ -716,7 +705,7 @@ void DDCoreToDDXMLOutput::rotation(const DDRotation& rotation, std::ostream& xos
xos << "<ReflectionRotation ";
}
using namespace cms_rounding;
xos << "name=\"" << rotNameStr << "\""
xos << "name=\"" << rotName << "\""
<< " phiX=\"" << roundIfNear0(convertRadToDeg(x.phi()), 4.e-4) << "*deg\""
<< " thetaX=\"" << roundIfNear0(convertRadToDeg(x.theta()), 4.e-4) << "*deg\""
<< " phiY=\"" << roundIfNear0(convertRadToDeg(y.phi()), 4.e-4) << "*deg\""
Expand Down Expand Up @@ -837,14 +826,14 @@ void DDCoreToDDXMLOutput::position(const G4LogicalVolume& parent,

const auto rot = child.GetObjectRotation();
if (rot != nullptr && !rot->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 << "<rRotation name=\"" << rotNameStr << "\"/>" << std::endl;
}
Expand All @@ -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 << "<PosPart copyNumber=\"" << edgeToChild->copyno() << "\">" << std::endl;
xos << "<rParent name=\"" << parent.toString() << "\"/>" << std::endl;
xos << "<rChild name=\"" << child.toString() << "\"/>" << std::endl;
if ((edgeToChild->ddrot().rotation()) != myIDENT) {
if (rotNameStr == ":") {
if (rotName == ":") {
rotation(edgeToChild->ddrot(), xos);
} else {
xos << "<rRotation name=\"" << rotNameStr << "\"/>" << std::endl;
xos << "<rRotation name=\"" << rotName << "\"/>" << std::endl;
}
} // else let default Rotation matrix be created?
using namespace cms_rounding;
Expand Down

0 comments on commit b70681b

Please sign in to comment.