Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DD4hep: Add Reflection Rotation #25345

Merged
merged 1 commit into from Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions DetectorDescription/DDCMS/data/cms-2015-muon-geometry.xml
Expand Up @@ -37,27 +37,27 @@
<Include ref='Geometry/CMSCommonData/data/extend/cmsextent.xml'/>
<Include ref='Geometry/CMSCommonData/data/cms.xml'/>
<Include ref='Geometry/CMSCommonData/data/cmsMother.xml'/>
<!-- Include ref='Geometry/CMSCommonData/data/cmsTracker.xml'/ -->
<!-- Include ref='Geometry/CMSCommonData/data/caloBase.xml'/ -->
<!-- Include ref='Geometry/CMSCommonData/data/cmsCalo.xml'/ -->
<Include ref='Geometry/CMSCommonData/data/cmsTracker.xml'/>
<Include ref='Geometry/CMSCommonData/data/caloBase.xml'/>
<Include ref='Geometry/CMSCommonData/data/cmsCalo.xml'/>
<Include ref='Geometry/CMSCommonData/data/muonBase.xml'/>
<Include ref='Geometry/CMSCommonData/data/cmsMuon.xml'/>
<Include ref='Geometry/CMSCommonData/data/mgnt.xml'/>
<!-- Include ref='Geometry/CMSCommonData/data/beampipe/2015/v1/beampipe.xml'/-->
<Include ref='Geometry/CMSCommonData/data/beampipe/2015/v1/beampipe.xml'/>
<Include ref='Geometry/CMSCommonData/data/cmsBeam.xml'/>
<Include ref='Geometry/CMSCommonData/data/muonMB.xml'/>
<Include ref='Geometry/CMSCommonData/data/muonMagnet.xml'/>
<!-- Include ref='Geometry/CMSCommonData/data/cavern.xml'/ -->
<Include ref='Geometry/CMSCommonData/data/cavern.xml'/>
<Include ref='Geometry/MuonCommonData/data/mbCommon/2015/v2/mbCommon.xml'/>
<Include ref='Geometry/MuonCommonData/data/mb1/2015/v2/mb1.xml'/>
<Include ref='Geometry/MuonCommonData/data/mb2/2015/v2/mb2.xml'/>
<Include ref='Geometry/MuonCommonData/data/mb3/2015/v2/mb3.xml'/>
<Include ref='Geometry/MuonCommonData/data/mb4/2015/v2/mb4.xml'/>
<Include ref='Geometry/MuonCommonData/data/design/muonYoke.xml'/>
<Include ref='Geometry/MuonCommonData/data/mf/2015/v1/mf.xml'/>
<!-- Include ref='Geometry/MuonCommonData/data/rpcf/2015/v1/rpcf.xml'/ -->
<!-- Include ref='Geometry/MuonCommonData/data/csc/2015/v1/csc.xml'/-->
<!-- Include ref='Geometry/MuonCommonData/data/mfshield/2015/v1/mfshield.xml'/-->
<Include ref='Geometry/MuonCommonData/data/rpcf/2015/v1/rpcf.xml'/>
<Include ref='Geometry/MuonCommonData/data/csc/2015/v2/csc.xml'/>
<Include ref='Geometry/MuonCommonData/data/mfshield/2015/v1/mfshield.xml'/>
</IncludeSection>

<PosPartSection label="">
Expand Down
6 changes: 6 additions & 0 deletions DetectorDescription/DDCMS/interface/DDAlgoArguments.h
Expand Up @@ -12,6 +12,8 @@

namespace cms
{
using DD3Vector = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;

constexpr unsigned int hash( const char* str, int h = 0 )
{
return !str[h] ? 5381 : ( hash( str, h+1 )*33 ) ^ str[h];
Expand All @@ -26,6 +28,10 @@ namespace cms
double thetaY, double phiY,
double thetaZ, double phiZ );

dd4hep::Rotation3D makeRotReflect( double thetaX, double phiX,
double thetaY, double phiY,
double thetaZ, double phiZ );

dd4hep::Rotation3D makeRotation3D( dd4hep::Rotation3D rotation,
const std::string& axis, double angle );

Expand Down
5 changes: 5 additions & 0 deletions DetectorDescription/DDCMS/interface/DDUnits.h
Expand Up @@ -22,6 +22,11 @@ constexpr long double operator"" _mm( long double length )
return length*1.;
}

constexpr long double operator"" _cm( long double length )
{
return length*10.;
}

constexpr long double operator "" _pi( long double x )
{ return x * _pi; }

Expand Down
2 changes: 2 additions & 0 deletions DetectorDescription/DDCMS/interface/DDXMLTags.h
Expand Up @@ -26,6 +26,7 @@ namespace cms {

UNICODE(RotationSection);
UNICODE(Rotation);
UNICODE(ReflectionRotation);
UNICODE(RotationSequence);
UNICODE(RotationByAxis);
UNICODE(axis);
Expand Down Expand Up @@ -141,6 +142,7 @@ namespace cms {
UNICODE(parent);
UNICODE(offset);
UNICODE(width);
UNICODE(nReplicas);

UNICODE(Algorithm);
UNICODE(String);
Expand Down
135 changes: 94 additions & 41 deletions DetectorDescription/DDCMS/plugins/DDDefinitions2Objects.cc
Expand Up @@ -10,6 +10,7 @@

#include "XML/Utilities.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"
#include "DetectorDescription/DDCMS/interface/DDUnits.h"
#include "DetectorDescription/DDCMS/interface/DDAlgoArguments.h"
#include "DetectorDescription/DDCMS/interface/DDNamespace.h"
#include "DetectorDescription/DDCMS/interface/DDParsingContext.h"
Expand Down Expand Up @@ -55,6 +56,7 @@ namespace dd4hep {

class RotationSection;
class DDLRotation;
class DDLReflectionRotation;
class DDLRotationSequence;
class DDLRotationByAxis;
class DDLTransform3D;
Expand Down Expand Up @@ -118,6 +120,8 @@ namespace dd4hep {
template <> void Converter<RotationSection>::operator()(xml_h element) const;
/// Converter for <DDLRotation/> tags
template <> void Converter<DDLRotation>::operator()(xml_h element) const;
/// Converter for <DDLReflectionRotation/> tags
template <> void Converter<DDLReflectionRotation>::operator()(xml_h element) const;
/// Converter for <DDLRotationSequence/> tags
template <> void Converter<DDLRotationSequence>::operator()(xml_h element) const;
/// Converter for <DDLRotationByAxis/> tags
Expand Down Expand Up @@ -209,6 +213,7 @@ template <> void Converter<MaterialSection>::operator()(xml_h element) const {
template <> void Converter<RotationSection>::operator()(xml_h element) const {
cms::DDNamespace ns(_param<cms::DDParsingContext>(), element);
xml_coll_t( element, DD_CMU(Rotation)).for_each(Converter<DDLRotation>(description,ns.context(),optional));
xml_coll_t( element, DD_CMU(ReflectionRotation)).for_each(Converter<DDLReflectionRotation>(description,ns.context(),optional));
xml_coll_t( element, DD_CMU(RotationSequence)).for_each(Converter<DDLRotationSequence>(description,ns.context(),optional));
xml_coll_t( element, DD_CMU(RotationByAxis)).for_each(Converter<DDLRotationByAxis>(description,ns.context(),optional));
}
Expand Down Expand Up @@ -506,6 +511,25 @@ template <> void Converter<DDLRotation>::operator()(xml_h element) const {
ns.addRotation(nam, rot);
}

/// Converter for <ReflectionRotation/> tags
template <> void Converter<DDLReflectionRotation>::operator()( xml_h element ) const {
cms::DDParsingContext* context = _param<cms::DDParsingContext>();
cms::DDNamespace ns( context );
xml_dim_t xrot( element );
string name = xrot.nameStr();
double thetaX = xrot.hasAttr( DD_CMU( thetaX )) ? ns.attr<double>( xrot, DD_CMU( thetaX )) : 0e0;
double phiX = xrot.hasAttr( DD_CMU( phiX )) ? ns.attr<double>( xrot, DD_CMU( phiX )) : 0e0;
double thetaY = xrot.hasAttr( DD_CMU( thetaY )) ? ns.attr<double>( xrot, DD_CMU( thetaY )) : 0e0;
double phiY = xrot.hasAttr( DD_CMU( phiY )) ? ns.attr<double>( xrot, DD_CMU( phiY )) : 0e0;
double thetaZ = xrot.hasAttr( DD_CMU( thetaZ )) ? ns.attr<double>( xrot, DD_CMU( thetaZ )) : 0e0;
double phiZ = xrot.hasAttr( DD_CMU( phiZ )) ? ns.attr<double>( xrot, DD_CMU( phiZ )) : 0e0;
printout( context->debug_rotations ? ALWAYS : DEBUG,
"MyDDCMS","+++ Adding reflection rotation: %-32s: (theta/phi)[rad] X: %6.3f %6.3f Y: %6.3f %6.3f Z: %6.3f %6.3f",
ns.prepend( name ).c_str(), thetaX, phiX, thetaY, phiY, thetaZ, phiZ );
Rotation3D rot = makeRotReflect( thetaX, phiX, thetaY, phiY, thetaZ, phiZ );
ns.addRotation( name, rot );
}

/// Converter for <RotationSequence/> tags
template <> void Converter<DDLRotationSequence>::operator()(xml_h element) const {
cms::DDParsingContext* context = _param<cms::DDParsingContext>();
Expand Down Expand Up @@ -561,32 +585,36 @@ template <> void Converter<DDLLogicalPart>::operator()(xml_h element) const {
}

/// Helper converter
template <> void Converter<DDLTransform3D>::operator()(xml_h element) const {
cms::DDNamespace ns(_param<cms::DDParsingContext>());
template <> void Converter<DDLTransform3D>::operator()( xml_h element ) const {
cms::DDNamespace ns( _param<cms::DDParsingContext>());
Transform3D* tr = _option<Transform3D>();
xml_dim_t e(element);
xml_dim_t translation = e.child(DD_CMU(Translation),false);
xml_dim_t rotation = e.child(DD_CMU(Rotation),false);
xml_dim_t refRotation = e.child(DD_CMU(rRotation),false);
xml_dim_t translation = e.child( DD_CMU( Translation ), false );
xml_dim_t rotation = e.child( DD_CMU( Rotation ), false );
xml_dim_t refRotation = e.child( DD_CMU( rRotation ), false );
Position pos;
Rotation3D rot;

if( translation.ptr() ) {
double x = ns.attr<double>(translation,_U(x));
double y = ns.attr<double>(translation,_U(y));
double z = ns.attr<double>(translation,_U(z));
if( translation.ptr()) {
double x = ns.attr<double>( translation, _U( x ));
double y = ns.attr<double>( translation, _U( y ));
double z = ns.attr<double>( translation, _U( z ));
pos = Position(x,y,z);
}
if( rotation.ptr() ) {
double x = ns.attr<double>(rotation,_U(x));
double y = ns.attr<double>(rotation,_U(y));
double z = ns.attr<double>(rotation,_U(z));
if( rotation.ptr()) {
double x = ns.attr<double>( rotation, _U( x ));
double y = ns.attr<double>( rotation, _U( y ));
double z = ns.attr<double>( rotation, _U( z ));
rot = RotationZYX(z,y,x);
}
else if( refRotation.ptr() ) {
rot = ns.rotation(refRotation.nameStr());
else if( refRotation.ptr()) {
string rotName = refRotation.nameStr();
if( strchr( rotName.c_str(), NAMESPACE_SEP ) == nullptr )
rotName = ns.name() + rotName;

rot = ns.rotation( rotName );
}
*tr = Transform3D(rot,pos);
*tr = Transform3D( rot, pos );
}

/// Converter for <PosPart/> tags
Expand Down Expand Up @@ -1001,24 +1029,28 @@ std::map<std::string, DDAxes> axesmap {{"x", DDAxes::x },
{"undefined", DDAxes::undefined }};
}

#include "DetectorDescription/DDCMS/interface/DDUnits.h"

/// Converter for <Division/> tags
template <> void Converter<DDLDivision>::operator()(xml_h element) const {
cms::DDNamespace ns(_param<cms::DDParsingContext>(), element);
xml_dim_t e(element);
template <> void Converter<DDLDivision>::operator()( xml_h element ) const {
cms::DDNamespace ns( _param<cms::DDParsingContext>(), element );
xml_dim_t e( element );
string childName = e.nameStr();
string parentName = ns.attr<string>( e, DD_CMU(parent));
string axis = ns.attr<string>( e, DD_CMU(axis));
if( strchr( childName.c_str(), NAMESPACE_SEP ) == nullptr )
childName = ns.name() + childName;

string parentName = ns.attr<string>( e, DD_CMU( parent ));
if( strchr( parentName.c_str(), NAMESPACE_SEP ) == nullptr )
parentName = ns.name() + parentName;
string axis = ns.attr<string>( e, DD_CMU( axis ));

// If you divide a tube of 360 degrees the offset displaces
// the starting angle, but you still fill the 360 degrees
double offset = e.hasAttr(DD_CMU(offset)) ? ns.attr<double>( e, DD_CMU(offset)) : 0e0;
double width = e.hasAttr(DD_CMU(width)) ? ns.attr<double>( e, DD_CMU(width)) : 0e0;
double offset = e.hasAttr( DD_CMU( offset )) ? ns.attr<double>( e, DD_CMU( offset )) : 0e0;
double width = e.hasAttr( DD_CMU( width )) ? ns.attr<double>( e, DD_CMU( width )) : 0e0;
int nReplicas = e.hasAttr( DD_CMU( nReplicas )) ? ns.attr<int>( e, DD_CMU( nReplicas )) : 0;

printout(ns.context()->debug_placements ? ALWAYS : DEBUG,
"MyDDCMS","+++ Start executing Division of %s along %s (%d) with offset %6.3f and %6.3f to produce %s....",
parentName.c_str(), axis.c_str(), axesmap[axis], offset, width, childName.c_str());
printout( ns.context()->debug_placements ? ALWAYS : DEBUG,
"MyDDCMS","+++ Start executing Division of %s along %s (%d) with offset %6.3f and %6.3f to produce %s....",
parentName.c_str(), axis.c_str(), axesmap[axis], offset, width, childName.c_str());

Volume parent = ns.volume( parentName );

Expand All @@ -1028,21 +1060,42 @@ template <> void Converter<DDLDivision>::operator()(xml_h element) const {
const TGeoTubeSeg* sh = ( const TGeoTubeSeg* )shape;
double widthInDeg = ConvertTo( width, deg );
double startInDeg = ConvertTo( offset, deg );
int numCopies = (int)(( sh->GetPhi2() - sh->GetPhi1())/ widthInDeg );
printout(ns.context()->debug_placements ? ALWAYS : DEBUG,
"MyDDCMS","+++ ...divide %s along %s (%d) with offset %6.3f deg and %6.3f deg to produce %d copies",
parent.solid().type(), axis.c_str(), axesmap[axis], startInDeg, widthInDeg, numCopies );
Volume child = parent->Divide( childName.c_str(), static_cast<int>(axesmap[axis]), numCopies, startInDeg, widthInDeg );
int numCopies = ( int )(( sh->GetPhi2() - sh->GetPhi1())/ widthInDeg );
printout( ns.context()->debug_placements ? ALWAYS : DEBUG,
"MyDDCMS","+++ ...divide %s along %s (%d) with offset %6.3f deg and %6.3f deg to produce %d copies",
parent.solid().type(), axis.c_str(), axesmap[axis], startInDeg, widthInDeg, numCopies );
Volume child = parent.divide( childName.c_str(), static_cast<int>( axesmap[axis]),
numCopies, startInDeg, widthInDeg );

ns.context()->volumes[childName] = child;

printout( ns.context()->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
"+++ %s Parent: %-24s [%s] Child: %-32s [%s] is multivolume [%s]",
e.tag().c_str(),
parentName.c_str(), parent.isValid() ? "VALID" : "INVALID",
child.name(), child.isValid() ? "VALID" : "INVALID",
child->IsVolumeMulti() ? "YES" : "NO" );

} else if( cl == TGeoTrd2::Class()) {
double widthInCm = ConvertTo( width, cm );
double offsetInCm = ConvertTo( offset, cm );
printout( ns.context()->debug_placements ? ALWAYS : DEBUG,
"MyDDCMS","+++ ...divide %s along %s (%d) with offset %6.3f cm and %6.3f cm to produce %d copies",
parent.solid().type(), axis.c_str(), axesmap[axis], offsetInCm, widthInCm, nReplicas );
if( axesmap[axis] == DDAxes::y )
std::cout << "DDDividedTrdY\n";
// Only Z divisions are supported for TGeoTrd2
Volume child = parent.divide( childName.c_str(), 3 /*static_cast<int>( axesmap[axis])*/,
nReplicas, offsetInCm, widthInCm );

ns.context()->volumes[childName] = child;

printout(ns.context()->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
"+++ %s Parent: %-24s [%s] Child: %-32s [%s] is multivolume [%s]",
e.tag().c_str(),
parentName.c_str(), parent.isValid() ? "VALID" : "INVALID",
child.name(), child.isValid() ? "VALID" : "INVALID",
child->IsVolumeMulti() ? "YES" : "NO" );

//assert( child.data());
printout( ns.context()->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
"+++ %s Parent: %-24s [%s] Child: %-32s [%s] is multivolume [%s]",
e.tag().c_str(),
parentName.c_str(), parent.isValid() ? "VALID" : "INVALID",
child.name(), child.isValid() ? "VALID" : "INVALID",
child->IsVolumeMulti() ? "YES" : "NO" );
}
else {
std::cout << "ERROR: Division of a " << parent.solid().type() << " is not implemented yet!\n";
Expand Down
5 changes: 4 additions & 1 deletion DetectorDescription/DDCMS/plugins/DDMuonAngular.cc
Expand Up @@ -22,7 +22,10 @@ static long algorithm( Detector& /* description */,
double zoffset = args.value<double>("zoffset");
string rotns = args.value<string>("RotNameSpace");
Volume mother = ns.volume(args.parentName());
Volume child = ns.volume(args.value<string>("ChildName"));
string childName = args.value<string>("ChildName");
if( strchr( childName.c_str(), NAMESPACE_SEP ) == nullptr )
childName = ns.name() + childName;
Volume child = ns.volume( childName );

LogDebug("DDAlgorithm") << "debug: Parameters for positioning:: n "
<< n << " Start, Step "
Expand Down
23 changes: 23 additions & 0 deletions DetectorDescription/DDCMS/src/DDAlgoArguments.cc
Expand Up @@ -33,6 +33,29 @@ cms::makeRotation3D( double thetaX, double phiX,
return rotation;
}

// makes sure that the RotationMatrix built is
// LEFT-handed coordinate system (i.e. reflected)
dd4hep::Rotation3D
cms::makeRotReflect( double thetaX, double phiX,
double thetaY, double phiY,
double thetaZ, double phiZ ) {
// define 3 unit std::vectors forming the new left-handed axes
DD3Vector x( cos( phiX ) * sin( thetaX ), sin( phiX ) * sin( thetaX ), cos( thetaX ));
DD3Vector y( cos( phiY ) * sin( thetaY ), sin( phiY ) * sin( thetaY ), cos( thetaY ));
DD3Vector z( cos( phiZ ) * sin( thetaZ ), sin( phiZ ) * sin( thetaZ ), cos( thetaZ ));

double tol = 1.0e-3; // Geant4 compatible
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constexpr double tol

double check = ( x.Cross( y )).Dot( z ); // in case of a LEFT-handed orthogonal system this must be -1
if( fabs( 1. + check ) > tol ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be using std::abs not fabs.

except("MyDDCMS","+++ FAILED to construct Rotation is not LEFT-handed!");
}

dd4hep::Rotation3D rotation( x.x(), y.x(), z.x(),
x.y(), y.y(), z.y(),
x.z(), y.z(), z.z());
return rotation;
}

dd4hep::Rotation3D
cms::makeRotation3D( dd4hep::Rotation3D rotation, const std::string& axis, double angle )
{
Expand Down