CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DetectorDescription/OfflineDBLoader/src/DDCoreToDDXMLOutput.cc

Go to the documentation of this file.
00001 #include <DetectorDescription/OfflineDBLoader/interface/DDCoreToDDXMLOutput.h>
00002 
00003 #include <DetectorDescription/Core/interface/DDSolid.h>
00004 #include <DetectorDescription/Core/interface/DDMaterial.h>
00005 
00006 #include <DetectorDescription/Core/interface/DDSpecifics.h>
00007 #include <DetectorDescription/Core/interface/DDPartSelection.h>
00008 #include <DetectorDescription/Core/interface/DDSolidShapes.h>
00009 
00010 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00011 
00012 #include <sstream>
00013 
00014 void DDCoreToDDXMLOutput::solid ( const DDSolid& solid, std::ostream& xos ) {
00015   //  std::cout << solid.name() << std::endl;
00016   switch (solid.shape()) 
00017     {
00018     case ddunion:
00019     case ddsubtraction:
00020     case ddintersection: 
00021       {      
00022         DDBooleanSolid rs(solid);
00023         if (solid.shape() == ddunion) {
00024           xos << "<UnionSolid ";
00025         } else if (solid.shape() == ddsubtraction) {
00026           xos << "<SubtractionSolid ";
00027         } else if (solid.shape() == ddintersection) {
00028           xos << "<IntersectionSolid ";
00029         }
00030         xos << "name=\""  << rs.toString() << "\">"  << std::endl;
00031         // if translation is == identity there are no parameters.
00032         // if there is no rotation the name will be ":"
00033         xos << "<rSolid name=\""  << rs.solidA().toString() << "\"/>" << std::endl;
00034         xos << "<rSolid name=\""  << rs.solidB().toString() << "\"/>" << std::endl;
00035         xos << "<Translation x=\"" << rs.translation().X() << "*mm\"";
00036         xos << " y=\"" << rs.translation().Y() << "*mm\"";
00037         xos << " z=\"" << rs.translation().Z() << "*mm\"";
00038         xos << "/>" << std::endl; 
00039         std::string rotName = rs.rotation().toString();
00040         if (rotName == ":") {
00041           rotName = "gen:ID";
00042         }
00043         xos << "<rRotation name=\""  << rs.rotation().toString() << "\"/>" << std::endl;
00044         if (solid.shape() == ddunion) {
00045           xos << "</UnionSolid>" << std::endl;
00046         } else if (solid.shape() == ddsubtraction) {
00047           xos << "</SubtractionSolid>" << std::endl;
00048         } else if (solid.shape() == ddintersection) {
00049           xos << "</IntersectionSolid>" << std::endl;
00050         }
00051         break;
00052       }
00053     case ddreflected:
00054       { 
00055         /*
00056     <ReflectionSolid name="trd2mirror">
00057       <rSolid name="trd2"/>
00058     </ReflectionSolid>
00059          */
00060         DDReflectionSolid rs(solid);
00061         xos << "<ReflectionSolid name=\""  << rs.toString() << "\">"  << std::endl;
00062         xos << "<rSolid name=\""  << rs.unreflected().toString() << "\">" << std::endl;
00063         xos << "</ReflectionSolid>" << std::endl;
00064         break;
00065       }
00066     case ddbox: {
00067       //    <Box name="box1" dx="10*cm" dy="10*cm" dz="10*cm"/>
00068       DDBox rs(solid);
00069       xos << "<Box name=\""  << rs.toString()  << "\"" //<< rs.toString() << "\"" //
00070           << " dx=\"" << rs.halfX() << "*mm\""
00071           << " dy=\"" << rs.halfY() << "*mm\""
00072           << " dz=\"" << rs.halfZ() << "*mm\"/>"
00073           << std::endl;
00074       break;
00075     }
00076     case ddtubs: {
00077 //      <Tubs name="TrackerSupportTubeNomex"         rMin="[SupportTubeR1]+[Tol]" 
00078 //            rMax="[SupportTubeR2]-[Tol]"           dz="[SupportTubeL]" 
00079 //            startPhi="0*deg"                       deltaPhi="360*deg"/>
00080       DDTubs rs(solid);
00081       xos << "<Tubs name=\""  << rs.toString() << "\""
00082           << " rMin=\"" << rs.rIn() << "*mm\""
00083           << " rMax=\"" << rs.rOut() << "*mm\""
00084           << " dz=\"" << rs.zhalf() << "*mm\""
00085           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00086           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\"/>"
00087           << std::endl;
00088       break;
00089     }
00090     case ddtrap: {
00091 //    <Trapezoid name="UpL_CSC_for_TotemT1_Plane_2_5_7" dz="[PCB_Epoxy_Thick_3P]/2."  alp1="-[Up_Signal_Side_alpL_3P]" alp2="-[Up_Signal_Side_alpL_3P]"  
00092 //     bl1="[Max_Base_Signal_SideL_3P]/2." tl1="[Up_Min_Base_Signal_SideL_3P]/2." h1="[Up_Height_Signal_SideL_3P]/2."
00093 //     h2="[Up_Height_Signal_SideL_3P]/2." bl2="[Max_Base_Signal_SideL_3P]/2." tl2="[Up_Min_Base_Signal_SideL_3P]/2."/>
00094       DDTrap rs(solid);
00095       xos << "<Trapezoid name=\""  << rs.toString() << "\"" //rs.toString() << "\"" //
00096           << " dz=\"" << rs.halfZ() << "*mm\""
00097           << " theta=\"" << rs.theta()/deg << "*deg\""
00098           << " phi=\"" << rs.phi()/deg << "*deg\""
00099           << " h1=\"" << rs.y1() << "*mm\""
00100           << " bl1=\"" << rs.x1() << "*mm\""
00101           << " tl1=\"" << rs.x2() << "*mm\""
00102           << " alp1=\"" << rs.alpha1()/deg << "*deg\""
00103           << " h2=\"" << rs.y2() << "*mm\""
00104           << " bl2=\"" << rs.x3() << "*mm\""
00105           << " tl2=\"" << rs.x4() << "*mm\""
00106           << " alp2=\"" << rs.alpha2()/deg << "*deg\"/>"
00107           << std::endl;
00108       break;
00109     }
00110     case ddcons: {
00111       DDCons rs(solid);
00112       xos << "<Cone name=\""  << rs.toString() << "\""
00113           << " dz=\"" << rs.zhalf() << "*mm\""
00114           << " rMin1=\"" << rs.rInMinusZ() << "*mm\""
00115           << " rMax1=\"" << rs.rOutMinusZ() << "*mm\""
00116           << " rMin2=\"" << rs.rInPlusZ() << "*mm\""
00117           << " rMax2=\"" << rs.rOutPlusZ() << "*mm\""
00118           << " startPhi=\"" << rs.phiFrom()/deg << "*deg\""
00119           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\"/>"
00120           << std::endl;
00121       break;
00122     }
00123     case ddpolycone_rz: {
00124       DDPolycone rs(solid);
00125       xos << "<Polycone name=\""  << rs.toString() << "\""
00126           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00127           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\">"
00128           << std::endl;
00129       const std::vector<double> & zV(rs.zVec());
00130       const std::vector<double> & rV(rs.rVec());
00131       for ( size_t i = 0; i < zV.size(); ++i ) {
00132         xos << "<RZPoint r=\"" << rV[i] << "*mm\""
00133             << " z=\"" << zV[i] << "*mm\"/>"
00134             << std::endl;
00135       }
00136       xos << "</Polycone>" << std::endl;
00137       break;
00138     }
00139     case ddpolyhedra_rz: {
00140       DDPolyhedra rs(solid);
00141       xos << "<Polyhedra name=\""  << rs.toString() << "\""
00142           << " numSide=\"" << rs.sides() << "\""
00143           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00144           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\">"
00145           << std::endl;
00146       const std::vector<double> & zV(rs.zVec());
00147       const std::vector<double> & rV(rs.rVec());
00148       for ( size_t i = 0; i < zV.size(); ++i ) {
00149         xos << "<RZPoint r=\"" << rV[i] << "*mm\""
00150             << " z=\"" << zV[i] << "*mm\"/>"
00151             << std::endl;
00152       }
00153       xos << "</Polyhedra>" << std::endl;
00154       break;
00155     }
00156     case ddpolycone_rrz:{
00157 //   <Polycone name="OCMS" startPhi="0*deg" deltaPhi="360*deg" >
00158 //    <ZSection z="-[CMSZ1]"  rMin="[Rmin]"  rMax="[CMSR2]" />
00159 //    <ZSection z="-[HallZ]"  rMin="[Rmin]"  rMax="[CMSR2]" /> 
00160 //    <ZSection z="-[HallZ]"  rMin="[Rmin]"  rMax="[HallR]" />
00161 //    <ZSection z="[HallZ]"   rMin="[Rmin]"  rMax="[HallR]" />
00162 //    <ZSection z="[HallZ]"   rMin="[Rmin]"  rMax="[CMSR2]" />
00163 //    <ZSection z="[CMSZ1]"   rMin="[Rmin]"  rMax="[CMSR2]" />
00164       DDPolycone rs(solid);
00165       xos << "<Polycone name=\""  << rs.toString() << "\""
00166           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00167           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\">"
00168           << std::endl;
00169       const std::vector<double> & zV(rs.zVec());
00170       const std::vector<double> & rMinV(rs.rMinVec());
00171       const std::vector<double> & rMaxV(rs.rMaxVec());
00172       for ( size_t i = 0; i < zV.size(); ++i ) {
00173         xos << "<ZSection z=\"" << zV[i] << "*mm\""
00174             << " rMin=\"" << rMinV[i] << "*mm\""
00175             << " rMax=\"" << rMaxV[i] << "*mm\"/>"
00176             << std::endl;
00177       }
00178       xos << "</Polycone>" << std::endl;
00179       break;
00180     }
00181     case ddpolyhedra_rrz:{
00182       DDPolyhedra rs(solid);
00183       xos << "<Polyhedra name=\""  << rs.toString() << "\""
00184           << " numSide=\"" << rs.sides() << "\""
00185           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00186           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\">"
00187           << std::endl;
00188       const std::vector<double> & zV(rs.zVec());
00189       const std::vector<double> & rMinV(rs.rMinVec());
00190       const std::vector<double> & rMaxV(rs.rMaxVec());
00191       for ( size_t i = 0; i < zV.size(); ++i ) {
00192         xos << "<ZSection z=\"" << zV[i] << "*mm\""
00193             << " rMin=\"" << rMinV[i] << "*mm\""
00194             << " rMax=\"" << rMaxV[i] << "*mm\"/>"
00195             << std::endl;
00196       }
00197       xos << "</Polyhedra>" << std::endl;
00198       break;
00199     }
00200     case ddpseudotrap:{
00201 // <PseudoTrap name="YE3_b" dx1="0.395967*m" dx2="1.86356*m" dy1="0.130*m" dy2="0.130*m" dz="2.73857*m" radius="-1.5300*m" atMinusZ="true"/> 
00202       DDPseudoTrap rs(solid);
00203       xos << "<PseudoTrap name=\""  << rs.toString() << "\""
00204           << " dx1=\"" << rs.x1() << "*mm\""
00205           << " dx2=\"" << rs.x2() << "*mm\""
00206           << " dy1=\"" << rs.y1() << "*mm\""
00207           << " dy2=\"" << rs.y2() << "*mm\""
00208           << " dz=\"" << rs.halfZ() << "*mm\""
00209           << " radius=\"" << rs.radius() << "*mm\""
00210           << " atMinusZ=\"" << ( rs.atMinusZ() ? "true" : "false" ) << "\"/>"
00211           << std::endl;
00212       break;
00213     }
00214     case ddtrunctubs:{
00215 // <TruncTubs name="trunctubs1" zHalf="50*cm" rMin="20*cm" rMax="40*cm" startPhi="0*deg" deltaPhi="90*deg" cutAtStart="25*cm" cutAtDelta="35*cm"/>
00216       DDTruncTubs rs(solid);
00217       xos << "<TruncTubs name=\""  << rs.toString() << "\""
00218           << " zHalf=\"" << rs.zHalf() << "*mm\""
00219           << " rMin=\"" << rs.rIn() << "*mm\""
00220           << " rMax=\"" << rs.rOut() << "*mm\""
00221           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00222           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\""
00223           << " cutAtStart=\"" << rs.cutAtStart() << "*mm\""
00224           << " cutAtDelta=\"" << rs.cutAtDelta() << "*mm\""
00225           << " cutInside=\"" << ( rs.cutInside() ? "true" : "false" ) << "\"/>"
00226           << std::endl;
00227       break;
00228     }
00229     case ddshapeless:{
00230       DDShapelessSolid rs(solid);
00231       xos << "<ShapelessSolid name=\""  << rs.toString() << "\"/>"
00232           << std::endl;
00233       break;
00234     }
00235     case ddtorus:{
00236 // <Torus name="torus" innerRadius="7.5*cm" outerRadius="10*cm" torusRadius="30*cm" startPhi="0*deg" deltaPhi="360*deg"/>
00237       DDTorus rs(solid);
00238       xos << "<Torus name=\""  << rs.toString() << "\""
00239           << " innerRadius=\"" << rs.rMin() << "*mm\""
00240           << " outerRadius=\"" << rs.rMax() << "*mm\""
00241           << " torusRadius=\"" << rs.rTorus() << "*mm\""
00242           << " startPhi=\"" << rs.startPhi()/deg << "*deg\""
00243           << " deltaPhi=\"" << rs.deltaPhi()/deg << "*deg\"/>"
00244           << std::endl;
00245       break;
00246     }
00247 //       return new PSolid( pstrs(solid.toString()), solid.parameters()
00248 //                       , solid.shape(), pstrs(""), pstrs(""), pstrs("") );
00249     case dd_not_init:
00250     default:
00251       throw DDException("DDCoreToDDXMLOutput::solid(...) either not inited or no such solid.");
00252       break;
00253     }
00254 
00255 }
00256   
00257 void DDCoreToDDXMLOutput::material ( const DDMaterial& material, std::ostream& xos ) {
00258    int noc = material.noOfConstituents();
00259    if ( noc == 0 ) {
00260      xos << "<ElementaryMaterial name=\""  << material.toString() << "\""
00261          << " density=\"" 
00262          << std::scientific << std::setprecision(5)
00263          << material.density() / mg * cm3 << "*mg/cm3\""
00264          << " atomicWeight=\"" 
00265          << std::fixed  
00266          << material.a() / g * mole << "*g/mole\""
00267          << std::setprecision(0) << std::fixed << " atomicNumber=\"" << material.z() << "\"/>"
00268          << std::endl;
00269    } else {
00270      xos << "<CompositeMaterial name=\""  << material.toString() << "\""
00271          << " density=\"" 
00272          << std::scientific << std::setprecision(5)
00273          << material.density() / mg * cm3 << "*mg/cm3\""
00274          << " method=\"mixture by weight\">" << std::endl;
00275      
00276      int j=0;
00277      for (; j<noc; ++j) {
00278        xos << "<MaterialFraction fraction=\"" 
00279            << std::fixed << std::setprecision(9)
00280            << material.constituent(j).second << "\">" << std::endl;
00281        xos << "<rMaterial name=\""  << material.constituent(j).first.name() << "\"/>" << std::endl;
00282        xos << "</MaterialFraction>" << std::endl;
00283      }
00284      
00285      xos << "</CompositeMaterial>" << std::endl;
00286    }
00287    //   return temp;
00288 }
00289 
00290 void DDCoreToDDXMLOutput::rotation (DDRotation& rotation, std::ostream& xos, const std::string& rotn) {
00291   double tol = 1.0e-3; // Geant4 compatible
00292   DD3Vector x,y,z; 
00293   rotation.matrix()->GetComponents(x,y,z); 
00294   double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system 
00295   // this must be -1
00296   bool reflection((1.-check)>tol);
00297   std::string rotName=rotation.toString();
00298   if ( rotName == ":" ) {
00299     if ( rotn != "" ) {
00300       rotName = rotn;
00301       std::cout << "about to try to make a new DDRotation... should fail!" << std::endl;
00302       DDRotation rot( DDName(rotn), rotation.matrix() );
00303       std:: cout << "new rotation: " << rot << std::endl;
00304     } else {
00305       std::cout << "WARNING: MAKING AN UNNAMED ROTATION" << std::endl;
00306     }
00307   }
00308   if ( !reflection ) {
00309     xos << "<Rotation ";
00310   } else {
00311     xos << "<ReflectionRotation ";
00312   }
00313   //  xos << std::fixed << std::setprecision(4);
00314   xos << "name=\"" << rotName << "\""
00315       << " phiX=\"" << x.phi()/deg << "*deg\""
00316       << " thetaX=\"" << x.theta()/deg << "*deg\""
00317       << " phiY=\"" << y.phi()/deg << "*deg\""
00318       << " thetaY=\"" << y.theta()/deg << "*deg\""
00319       << " phiZ=\"" << z.phi()/deg << "*deg\""
00320       << " thetaZ=\"" << z.theta()/deg << "*deg\"/>"
00321       << std::endl;
00322   //  xos << std::fixed << std::setprecision(6);
00323 }
00324 
00325 void DDCoreToDDXMLOutput::logicalPart ( const DDLogicalPart& lp, std::ostream& xos ) {
00326 
00327   xos << "<LogicalPart name=\""  << lp.toString() << "\">" << std::endl;
00328   xos << "<rSolid name=\""  << lp.solid().toString() << "\"/>" << std::endl;
00329   xos << "<rMaterial name=\""  << lp.material().toString() << "\"/>" << std::endl;
00330   xos << "</LogicalPart>" << std::endl;
00331 }
00332 
00333 void DDCoreToDDXMLOutput::position ( const DDLogicalPart& parent
00334                                      , const DDLogicalPart& child
00335                                      , DDPosData* edgeToChild 
00336                                      //                              , PIdealGeometry& geom
00337                                      , int& rotNameSeed
00338                                      , std::ostream& xos ) {
00339   std::string rotName = edgeToChild->rot_.toString();
00340   DDRotationMatrix myIDENT;
00341   
00342   xos << "<PosPart copyNumber=\"" << edgeToChild->copyno_ << "\">" << std::endl;
00343   xos << "<rParent name=\"" << parent.toString() << "\"/>" << std::endl;
00344   xos << "<rChild name=\"" << child.toString() << "\"/>" << std::endl;
00345   if ( *(edgeToChild->rot_.matrix()) != myIDENT ) {
00346     if ( rotName == ":" ) {
00347       rotation(edgeToChild->rot_, xos);
00348     } else {
00349       xos << "<rRotation name=\"" << rotName << "\"/>" << std::endl;
00350     }
00351   } // else let default Rotation matrix be created?
00352   //  xos  << std::fixed << std::setprecision(4);
00353   xos << "<Translation x=\"" << edgeToChild->translation().x() <<"*mm\""
00354       << " y=\"" << edgeToChild->translation().y() <<"*mm\""
00355       << " z=\"" << edgeToChild->translation().z() <<"*mm\"/>" << std::endl;
00356   //  xos  << std::fixed << std::setprecision(6);
00357   xos << "</PosPart>" << std::endl;
00358 }
00359 
00360 
00361 void DDCoreToDDXMLOutput::specpar ( const DDSpecifics& sp, std::ostream& xos ) {
00362 
00363   xos << "<SpecPar name=\"" << sp.toString() << "\" eval=\"false\">" << std::endl;
00364 
00365   // ========...  all the selection strings out as strings by using the DDPartSelection's std::ostream function...
00366   const std::vector<DDPartSelection> sels = sp.selection();
00367   std::vector<DDPartSelection>::const_iterator psit = sels.begin();
00368   std::vector<DDPartSelection>::const_iterator psendit = sels.end();
00369   for (; psit != psendit ; ++psit) {
00370     xos << "<PartSelector path=\"" << *psit << "\"/>" << std::endl;
00371   }
00372 
00373  // =========  ... and iterate over all DDValues...
00374   DDsvalues_type::const_iterator vit(sp.specifics().begin()), ved(sp.specifics().end());
00375    for (; vit != ved; ++vit) {
00376     const DDValue & v = vit->second;
00377     size_t s=v.size();
00378     size_t i=0;
00379     // ============  ... all actual values with the same name
00380     const std::vector<std::string>& strvec = v.strings();
00381     if ( v.isEvaluated() ) {
00382       for (; i<s; ++i) {
00383         xos << "<Parameter name=\"" << v.name() << "\""
00384             << " value=\"" << v[i] << "\""
00385             << " eval=\"true\"/>" << std::endl;
00386       }
00387     } else {
00388       for (; i<s; ++i ) {
00389         xos << "<Parameter name=\"" << v.name() << "\""
00390             << " value=\"" << strvec[i] << "\""
00391             << " eval=\"false\"/>" << std::endl;
00392       }
00393     }
00394     
00395    }
00396    xos << "</SpecPar>" << std::endl;
00397 }
00398 
00399 
00400 void DDCoreToDDXMLOutput::specpar ( const std::pair<DDsvalues_type, std::set<DDPartSelection*> >& pssv, std::ostream& xos ) {
00401   static std::string madeName("specparname");
00402   static int numspecpars(0);
00403   std::ostringstream ostr;
00404   ostr << numspecpars++;
00405   std::string spname = madeName + ostr.str(); 
00406   xos << "<SpecPar name=\"" << spname << "\" eval=\"false\">" << std::endl;
00407   std::set<DDPartSelection*>::const_iterator psit = pssv.second.begin();
00408   std::set<DDPartSelection*>::const_iterator psendit = pssv.second.end();
00409   for (; psit != psendit; ++psit) {
00410     xos << "<PartSelector path=\"" << *(*psit) << "\"/>" << std::endl;
00411   }
00412 
00413   // =========  ... and iterate over all DDValues...
00414   
00415   DDsvalues_type::const_iterator vit(pssv.first.begin()), ved(pssv.first.end());
00416   for (; vit != ved; ++vit) {
00417     const DDValue & v = vit->second;
00418     size_t s=v.size();
00419     size_t i=0;
00420     // ============  ... all actual values with the same name
00421     const std::vector<std::string>& strvec = v.strings();
00422     if ( v.isEvaluated() ) {
00423       for (; i<s; ++i) {
00424         xos << "<Parameter name=\"" << v.name() << "\""
00425             << " value=\"" << v[i] << "\""
00426             << " eval=\"true\"/>" << std::endl;
00427       }
00428     } else {
00429       for (; i<s; ++i ) {
00430         xos << "<Parameter name=\"" << v.name() << "\""
00431             << " value=\"" << strvec[i] << "\""
00432             << " eval=\"false\"/>" << std::endl;
00433       }
00434     }
00435   }
00436   
00437   xos << "</SpecPar>" << std::endl;
00438 }