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