/DetectorDescription/OfflineDBLoader/bin/stubs/OutputDDToDDL.cc

https://github.com/aivanov-cern/cmssw · C++ · 219 lines · 186 code · 26 blank · 7 comment · 63 complexity · 39364308c2bdd13fd875e62d3f0e441a MD5 · raw file

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