/DetectorDescription/OfflineDBLoader/bin/stubs/OutputMagneticFieldDDToDDL.cc

https://github.com/aivanov-cern/cmssw · C++ · 252 lines · 220 code · 30 blank · 2 comment · 61 complexity · d64758a976a73944e05409f515b2f067 MD5 · raw file

  1. #include "OutputMagneticFieldDDToDDL.h"
  2. #include <FWCore/Framework/interface/ESTransientHandle.h>
  3. #include <DetectorDescription/Core/interface/DDSpecifics.h>
  4. #include <DetectorDescription/Core/interface/DDName.h>
  5. #include <DetectorDescription/Core/interface/DDPosData.h>
  6. #include <DetectorDescription/OfflineDBLoader/interface/DDCoreToDDXMLOutput.h>
  7. #include <MagneticField/Records/interface/IdealMagneticFieldRecord.h>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. #include <utility>
  13. bool
  14. ddsvaluesCmp::operator() ( const DDsvalues_type& sv1, const DDsvalues_type& sv2 )
  15. {
  16. if( sv1.size() < sv2.size()) return true;
  17. if( sv2.size() < sv1.size()) return false;
  18. size_t ind = 0;
  19. for( ; ind < sv1.size(); ++ind )
  20. {
  21. if( sv1[ ind ].first < sv2[ ind ].first ) return true;
  22. if( sv2[ ind ].first < sv1[ ind ].first ) return false;
  23. if( sv1[ ind ].second < sv2[ ind ].second ) return true;
  24. if( sv2[ ind ].second < sv1[ ind ].second ) return false;
  25. }
  26. return false;
  27. }
  28. OutputMagneticFieldDDToDDL::OutputMagneticFieldDDToDDL( const edm::ParameterSet& iConfig )
  29. : m_fname()
  30. {
  31. m_rotNumSeed = iConfig.getParameter<int>( "rotNumSeed" );
  32. m_fname = iConfig.getUntrackedParameter<std::string>( "fileName" );
  33. if( m_fname == "" )
  34. {
  35. m_xos = &std::cout;
  36. }
  37. else
  38. {
  39. m_xos = new std::ofstream( m_fname.c_str());
  40. }
  41. ( *m_xos ) << "<?xml version=\"1.0\"?>\n";
  42. ( *m_xos ) << "<DDDefinition xmlns=\"http://www.cern.ch/cms/DDL\"\n";
  43. ( *m_xos ) << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
  44. ( *m_xos ) << "xsi:schemaLocation=\"http://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd\">\n";
  45. ( *m_xos ) << std::fixed << std::setprecision( 18 );
  46. }
  47. OutputMagneticFieldDDToDDL::~OutputMagneticFieldDDToDDL()
  48. {
  49. ( *m_xos ) << "</DDDefinition>\n";
  50. ( *m_xos ) << std::endl;
  51. m_xos->flush();
  52. }
  53. void
  54. OutputMagneticFieldDDToDDL::beginRun( const edm::Run&, edm::EventSetup const& es )
  55. {
  56. edm::LogInfo( "OutputMagneticFieldDDToDDL" ) << "OutputMagneticFieldDDToDDL::beginRun";
  57. edm::ESTransientHandle<DDCompactView> pDD;
  58. es.get<IdealMagneticFieldRecord>().get( pDD );
  59. DDCompactView::DDCompactView::graph_type gra = pDD->graph();
  60. // Temporary stores:
  61. std::set<DDLogicalPart> lpStore;
  62. std::set<DDMaterial> matStore;
  63. std::set<DDSolid> solStore;
  64. std::map<DDsvalues_type, std::set<DDPartSelection*>, ddsvaluesCmp > specStore;
  65. std::set<DDRotation> rotStore;
  66. DDCoreToDDXMLOutput out;
  67. std::string rn = m_fname;
  68. size_t foundLastDot= rn.find_last_of('.');
  69. size_t foundLastSlash= rn.find_last_of('/');
  70. if( foundLastSlash > foundLastDot && foundLastSlash != std::string::npos )
  71. {
  72. edm::LogError( "OutputMagneticFieldDDToDDL" ) << "What? last . before last / in path for filename... this should die...";
  73. }
  74. if( foundLastDot != std::string::npos && foundLastSlash != std::string::npos )
  75. {
  76. out.ns_ = rn.substr( foundLastSlash, foundLastDot );
  77. }
  78. else if ( foundLastDot != std::string::npos )
  79. {
  80. out.ns_ = rn.substr(0, foundLastDot);
  81. }
  82. else
  83. {
  84. edm::LogError( "OutputMagneticFieldDDToDDL" ) << "What? no file name? Attempt at namespace =\"" << out.ns_ << "\" filename was " << m_fname;
  85. }
  86. edm::LogInfo( "OutputMagneticFieldDDToDDL" ) << "m_fname=" << m_fname << " namespace = " << out.ns_;
  87. std::string ns_ = out.ns_;
  88. ( *m_xos ) << std::fixed << std::setprecision( 18 );
  89. typedef DDCompactView::graph_type::const_adj_iterator adjl_iterator;
  90. adjl_iterator git = gra.begin();
  91. adjl_iterator gend = gra.end();
  92. DDCompactView::graph_type::index_type i=0;
  93. ( *m_xos) << "<PosPartSection label=\"" << ns_ << "\">\n";
  94. git = gra.begin();
  95. for( ; git != gend; ++git )
  96. {
  97. const DDLogicalPart & ddLP = gra.nodeData( git );
  98. if( lpStore.find(ddLP) != lpStore.end())
  99. {
  100. addToSpecStore( ddLP, specStore );
  101. }
  102. lpStore.insert( ddLP );
  103. addToMatStore( ddLP.material(), matStore );
  104. addToSolStore( ddLP.solid(), solStore, rotStore );
  105. ++i;
  106. if( git->size())
  107. {
  108. // ask for children of ddLP
  109. DDCompactView::graph_type::edge_list::const_iterator cit = git->begin();
  110. DDCompactView::graph_type::edge_list::const_iterator cend = git->end();
  111. for( ; cit != cend; ++cit )
  112. {
  113. const DDLogicalPart & ddcurLP = gra.nodeData( cit->first );
  114. if( lpStore.find(ddcurLP) != lpStore.end())
  115. {
  116. addToSpecStore( ddcurLP, specStore );
  117. }
  118. lpStore.insert( ddcurLP );
  119. addToMatStore( ddcurLP.material(), matStore );
  120. addToSolStore( ddcurLP.solid(), solStore, rotStore );
  121. rotStore.insert( gra.edgeData( cit->second )->rot_ );
  122. out.position( ddLP, ddcurLP, gra.edgeData( cit->second ), m_rotNumSeed, *m_xos );
  123. } // iterate over children
  124. } // if (children)
  125. } // iterate over graph nodes
  126. ( *m_xos ) << "</PosPartSection>\n";
  127. ( *m_xos ) << std::scientific << std::setprecision( 18 );
  128. std::set<DDMaterial>::const_iterator it( matStore.begin()), ed( matStore.end());
  129. ( *m_xos) << "<MaterialSection label=\"" << ns_ << "\">\n";
  130. for( ; it != ed; ++it )
  131. {
  132. if( ! it->isDefined().second ) continue;
  133. out.material( *it, *m_xos );
  134. }
  135. ( *m_xos ) << "</MaterialSection>\n";
  136. ( *m_xos ) << "<RotationSection label=\"" << ns_ << "\">\n";
  137. ( *m_xos ) << std::fixed << std::setprecision( 18 );
  138. std::set<DDRotation>::iterator rit( rotStore.begin()), red( rotStore.end());
  139. for( ; rit != red; ++rit )
  140. {
  141. if( ! rit->isDefined().second ) continue;
  142. if( rit->toString() != ":" )
  143. {
  144. DDRotation r( *rit );
  145. out.rotation( r, *m_xos );
  146. }
  147. }
  148. ( *m_xos ) << "</RotationSection>\n";
  149. ( *m_xos ) << std::fixed << std::setprecision( 18 );
  150. std::set<DDSolid>::const_iterator sit( solStore.begin()), sed( solStore.end());
  151. ( *m_xos ) << "<SolidSection label=\"" << ns_ << "\">\n";
  152. for( ; sit != sed; ++sit)
  153. {
  154. if( ! sit->isDefined().second ) continue;
  155. out.solid( *sit, *m_xos );
  156. }
  157. ( *m_xos ) << "</SolidSection>\n";
  158. std::set<DDLogicalPart>::iterator lpit( lpStore.begin()), lped( lpStore.end());
  159. ( *m_xos ) << "<LogicalPartSection label=\"" << ns_ << "\">\n";
  160. for( ; lpit != lped; ++lpit )
  161. {
  162. if( ! lpit->isDefined().first ) continue;
  163. const DDLogicalPart & lp = *lpit;
  164. out.logicalPart( lp, *m_xos );
  165. }
  166. ( *m_xos ) << "</LogicalPartSection>\n";
  167. ( *m_xos ) << std::fixed << std::setprecision( 18 );
  168. std::map<DDsvalues_type, std::set<DDPartSelection*> >::const_iterator mit( specStore.begin()), mend( specStore.end());
  169. ( *m_xos ) << "<SpecParSection label=\"" << ns_ << "\">\n";
  170. for( ; mit != mend; ++mit )
  171. {
  172. out.specpar( *mit, *m_xos );
  173. }
  174. ( *m_xos ) << "</SpecParSection>\n";
  175. }
  176. void
  177. OutputMagneticFieldDDToDDL::addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore )
  178. {
  179. matStore.insert( mat );
  180. if( mat.noOfConstituents() != 0 )
  181. {
  182. DDMaterial::FractionV::value_type frac;
  183. int findex( 0 );
  184. while( findex < mat.noOfConstituents())
  185. {
  186. if( matStore.find( mat.constituent( findex ).first ) == matStore.end())
  187. {
  188. addToMatStore( mat.constituent( findex ).first, matStore );
  189. }
  190. ++findex;
  191. }
  192. }
  193. }
  194. void
  195. OutputMagneticFieldDDToDDL::addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore )
  196. {
  197. solStore.insert( sol );
  198. if( sol.shape() == ddunion || sol.shape() == ddsubtraction || sol.shape() == ddintersection )
  199. {
  200. const DDBooleanSolid& bs ( sol );
  201. if( solStore.find(bs.solidA()) == solStore.end())
  202. {
  203. addToSolStore( bs.solidA(), solStore, rotStore );
  204. }
  205. if( solStore.find( bs.solidB()) == solStore.end())
  206. {
  207. addToSolStore( bs.solidB(), solStore, rotStore );
  208. }
  209. rotStore.insert( bs.rotation());
  210. }
  211. }
  212. void
  213. OutputMagneticFieldDDToDDL::addToSpecStore( const DDLogicalPart& lp, std::map<DDsvalues_type, std::set<DDPartSelection*>, ddsvaluesCmp > & specStore )
  214. {
  215. std::vector<std::pair<DDPartSelection*, DDsvalues_type*> >::const_iterator spit( lp.attachedSpecifics().begin()), spend( lp.attachedSpecifics().end());
  216. for( ; spit != spend; ++spit )
  217. {
  218. specStore[ *spit->second ].insert( spit->first );
  219. }
  220. }
  221. #include "FWCore/Framework/interface/MakerMacros.h"
  222. DEFINE_FWK_MODULE( OutputMagneticFieldDDToDDL );