CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DetectorDescription/OfflineDBLoader/bin/stubs/OutputDDToDDL.cc

Go to the documentation of this file.
00001 #include "OutputDDToDDL.h"
00002 
00003 #include <FWCore/Framework/interface/ESTransientHandle.h>
00004 
00005 #include <DetectorDescription/Core/interface/DDSpecifics.h>
00006 #include <DetectorDescription/Core/interface/DDName.h>
00007 #include <DetectorDescription/Core/interface/DDPosData.h>
00008 #include <DetectorDescription/OfflineDBLoader/interface/DDCoreToDDXMLOutput.h>
00009 #include <Geometry/Records/interface/IdealGeometryRecord.h>
00010 
00011 #include <iostream>
00012 #include <fstream>
00013 #include <string>
00014 #include <vector>
00015 #include <utility>
00016 
00017 bool ddsvaluesCmp::operator() ( const  DDsvalues_type& sv1, const DDsvalues_type& sv2 ) {
00018   if ( sv1.size() < sv2.size() ) return true;
00019   if ( sv2.size() < sv1.size() ) return false;
00020   size_t ind = 0;
00021   for (; ind < sv1.size(); ++ind) {
00022     if ( sv1[ind].first < sv2[ind].first ) return true;
00023     if ( sv2[ind].first < sv1[ind].first ) return false;
00024     if ( sv1[ind].second < sv2[ind].second ) return true;
00025     if ( sv2[ind].second < sv1[ind].second ) return false;
00026   }
00027   return false;
00028 }
00029 
00030 OutputDDToDDL::OutputDDToDDL(const edm::ParameterSet& iConfig) : fname_()
00031 {
00032   //  std::cout<<"OutputDDToDDL::OutputDDToDDL"<<std::endl;
00033   rotNumSeed_ = iConfig.getParameter<int>("rotNumSeed");
00034   fname_ = iConfig.getUntrackedParameter<std::string>("fileName");
00035   if ( fname_ == "" ) {
00036     xos_ = &std::cout;
00037   } else {
00038     xos_ = new std::ofstream(fname_.c_str());
00039   }
00040   (*xos_) << "<?xml version=\"1.0\"?>" << std::endl;
00041   (*xos_) << "<DDDefinition xmlns=\"https://www.cern.ch/cms/DDL\"" << std::endl;
00042   (*xos_) << " xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\"" << std::endl;
00043   (*xos_) << "xsi:schemaLocation=\"https://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd\">" << std::endl;
00044   (*xos_) << std::fixed << std::setprecision(18);
00045 }
00046 OutputDDToDDL::~OutputDDToDDL()
00047 {
00048   (*xos_) << "</DDDefinition>" << std::endl;
00049   (*xos_) << std::endl;
00050   xos_->flush();
00051 
00052 }
00053 
00054 void
00055 OutputDDToDDL::beginRun( const edm::Run&, edm::EventSetup const& es) 
00056 {
00057   std::cout<<"OutputDDToDDL::beginRun"<<std::endl;
00058 
00059   edm::ESTransientHandle<DDCompactView> pDD;
00060 
00061   es.get<IdealGeometryRecord>().get( pDD );
00062 
00063   DDCompactView::DDCompactView::graph_type gra = pDD->graph();
00064   // temporary stores:
00065   std::set<DDLogicalPart> lpStore;
00066   std::set<DDMaterial> matStore;
00067   std::set<DDSolid> solStore;
00068   // 2009-08-19: MEC: I've tried this with set<DDPartSelection> and
00069   // had to write operator< for DDPartSelection and DDPartSelectionLevel
00070   // the output from such an effort is different than this one.
00071   std::map<DDsvalues_type, std::set<DDPartSelection*>, ddsvaluesCmp > specStore;
00072   std::set<DDRotation> rotStore;
00073 
00074   DDCoreToDDXMLOutput out;
00075   
00076   std::string rn = fname_;
00077   size_t foundLastDot= rn.find_last_of('.');
00078   size_t foundLastSlash= rn.find_last_of('/');
00079   if ( foundLastSlash > foundLastDot && foundLastSlash != std::string::npos) {
00080     std::cout << "What? last . before last / in path for filename... this should die..." << std::endl;
00081   }
00082   if ( foundLastDot != std::string::npos && foundLastSlash != std::string::npos ) {
00083     out.ns_ = rn.substr(foundLastSlash,foundLastDot);
00084   } else if ( foundLastDot != std::string::npos ) {
00085     out.ns_ = rn.substr(0, foundLastDot);
00086   } else {
00087     std::cout << "What? no file name? Attempt at namespace =\"" << out.ns_ << "\" filename was " << fname_ <<  std::endl;
00088   }
00089   std::cout << "fname_=" << fname_ << " namespace = " << out.ns_ << std::endl;
00090   std::string ns_ = out.ns_;
00091 
00092   (*xos_) << std::fixed << std::setprecision(18);
00093   typedef  DDCompactView::graph_type::const_adj_iterator adjl_iterator;
00094 
00095   adjl_iterator git = gra.begin();
00096   adjl_iterator gend = gra.end();    
00097     
00098   DDCompactView::graph_type::index_type i=0;
00099   (*xos_) << "<PosPartSection label=\"" << ns_ << "\">" << std::endl;
00100   git = gra.begin();
00101   for (; git != gend; ++git) 
00102     {
00103       const DDLogicalPart & ddLP = gra.nodeData(git);
00104       if ( lpStore.find(ddLP) != lpStore.end() ) {
00105         addToSpecStore(ddLP, specStore);
00106       }
00107       lpStore.insert(ddLP);
00108       addToMatStore( ddLP.material(), matStore );
00109       addToSolStore( ddLP.solid(), solStore, rotStore );
00110       ++i;
00111       if (git->size()) 
00112         {
00113           // ask for children of ddLP  
00114           DDCompactView::graph_type::edge_list::const_iterator cit  = git->begin();
00115           DDCompactView::graph_type::edge_list::const_iterator cend = git->end();
00116           for (; cit != cend; ++cit) 
00117             {
00118               const DDLogicalPart & ddcurLP = gra.nodeData(cit->first);
00119               if (lpStore.find(ddcurLP) != lpStore.end()) {
00120                 addToSpecStore(ddcurLP, specStore);
00121               }
00122               lpStore.insert(ddcurLP);
00123               addToMatStore(ddcurLP.material(), matStore);
00124               addToSolStore(ddcurLP.solid(), solStore, rotStore);
00125               rotStore.insert(gra.edgeData(cit->second)->rot_);
00126               out.position(ddLP, ddcurLP, gra.edgeData(cit->second), rotNumSeed_, *xos_);
00127             } // iterate over children
00128         } // if (children)
00129     } // iterate over graph nodes  
00130   //  std::cout << "specStore.size() = " << specStore.size() << std::endl;
00131 
00132   (*xos_) << "</PosPartSection>" << std::endl;
00133   
00134   (*xos_) << std::scientific << std::setprecision(18);
00135   std::set<DDMaterial>::const_iterator it(matStore.begin()), ed(matStore.end());
00136   (*xos_) << "<MaterialSection label=\"" << ns_ << "\">" << std::endl;
00137   for (; it != ed; ++it) {
00138     if (! it->isDefined().second) continue;
00139     out.material(*it, *xos_);
00140   }
00141   (*xos_) << "</MaterialSection>" << std::endl;
00142 
00143   (*xos_) << "<RotationSection label=\"" << ns_ << "\">" << std::endl;
00144   (*xos_) << std::fixed << std::setprecision(18);
00145   std::set<DDRotation>::iterator rit(rotStore.begin()), red(rotStore.end());
00146   for (; rit != red; ++rit) {
00147     if (! rit->isDefined().second) continue;
00148     if ( rit->toString() != ":" ) {
00149       DDRotation r(*rit);
00150       out.rotation(r, *xos_);
00151     }
00152   } 
00153   (*xos_) << "</RotationSection>" << std::endl;
00154 
00155   (*xos_) << std::fixed << std::setprecision(18);
00156   std::set<DDSolid>::const_iterator sit(solStore.begin()), sed(solStore.end());
00157   (*xos_) << "<SolidSection label=\"" << ns_ << "\">" << std::endl;
00158   for (; sit != sed; ++sit) {
00159     if (! sit->isDefined().second) continue;  
00160     out.solid(*sit, *xos_);
00161   }
00162   (*xos_) << "</SolidSection>" << std::endl;
00163 
00164   std::set<DDLogicalPart>::iterator lpit(lpStore.begin()), lped(lpStore.end());
00165   (*xos_) << "<LogicalPartSection label=\"" << ns_ << "\">" << std::endl;
00166   for (; lpit != lped; ++lpit) {
00167     if (! lpit->isDefined().first) continue;  
00168     const DDLogicalPart & lp = *lpit;
00169       out.logicalPart(lp, *xos_);
00170   }
00171   (*xos_) << "</LogicalPartSection>" << std::endl;
00172 
00173   (*xos_) << std::fixed << std::setprecision(18);
00174   std::map<DDsvalues_type, std::set<DDPartSelection*> >::const_iterator mit(specStore.begin()), mend (specStore.end());
00175   (*xos_) << "<SpecParSection label=\"" << ns_ << "\">" << std::endl;
00176   for (; mit != mend; ++mit) {
00177     out.specpar ( *mit, *xos_ );
00178   } 
00179   (*xos_) << "</SpecParSection>" << std::endl;
00180 
00181 }
00182 
00183 void  OutputDDToDDL::addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore) {
00184   matStore.insert(mat);
00185   if ( mat.noOfConstituents() != 0 ) {
00186     DDMaterial::FractionV::value_type frac;
00187     int findex(0);
00188     while ( findex < mat.noOfConstituents() ) {
00189       if ( matStore.find(mat.constituent(findex).first) == matStore.end() ) {
00190         addToMatStore( mat.constituent(findex).first, matStore );
00191       }
00192       ++findex;
00193     }
00194   }
00195 }
00196 
00197 void  OutputDDToDDL::addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore ) {
00198       solStore.insert(sol);
00199       if ( sol.shape() == ddunion || sol.shape() == ddsubtraction || sol.shape() == ddintersection ) {
00200         const DDBooleanSolid& bs (sol);
00201         if ( solStore.find(bs.solidA()) == solStore.end()) {
00202           addToSolStore(bs.solidA(), solStore, rotStore);
00203         }
00204         if ( solStore.find(bs.solidB()) == solStore.end()) {
00205           addToSolStore(bs.solidB(), solStore, rotStore);
00206         }
00207         rotStore.insert(bs.rotation());
00208       }
00209 }
00210 
00211 void OutputDDToDDL::addToSpecStore( const DDLogicalPart& lp, std::map<DDsvalues_type, std::set<DDPartSelection*>, ddsvaluesCmp > & specStore ) {
00212   std::vector<std::pair<DDPartSelection*, DDsvalues_type*> >::const_iterator spit(lp.attachedSpecifics().begin()), spend(lp.attachedSpecifics().end());
00213   for ( ; spit != spend; ++spit ) {
00214     specStore[*spit->second].insert(spit->first);
00215   }
00216 }
00217 
00218 #include "FWCore/Framework/interface/MakerMacros.h"
00219 DEFINE_FWK_MODULE(OutputDDToDDL);