PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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