PageRenderTime 81ms CodeModel.GetById 19ms app.highlight 57ms RepoModel.GetById 2ms app.codeStats 0ms

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