CMS 3D CMS Logo

OutputMagneticFieldDDToDDL.cc
Go to the documentation of this file.
15 
16 #include <cstddef>
17 #include <fstream>
18 #include <iomanip>
19 #include <map>
20 #include <memory>
21 #include <ostream>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 namespace {
29  struct ddsvaluesCmp {
30  bool operator() ( const DDsvalues_type& sv1, const DDsvalues_type& sv2 ) const;
31  };
32 }
33 
34 class OutputMagneticFieldDDToDDL : public edm::one::EDAnalyzer<edm::one::WatchRuns>
35 {
36 public:
37  explicit OutputMagneticFieldDDToDDL( const edm::ParameterSet& iConfig );
38  ~OutputMagneticFieldDDToDDL( void ) override;
39 
40  void beginJob() override {}
41  void beginRun(edm::Run const& iEvent, edm::EventSetup const&) override;
42  void analyze(edm::Event const& iEvent, edm::EventSetup const&) override {}
43  void endRun(edm::Run const& iEvent, edm::EventSetup const&) override {}
44  void endJob() override {}
45 
46 private:
47  void addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore );
48  void addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore );
49  void addToSpecStore( const DDLogicalPart& lp, std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > & specStore );
50 
53  std::ostream* m_xos;
54 };
55 
56 bool
57 ddsvaluesCmp::operator() ( const DDsvalues_type& sv1, const DDsvalues_type& sv2 ) const
58 {
59  if( sv1.size() < sv2.size()) return true;
60  if( sv2.size() < sv1.size()) return false;
61  size_t ind = 0;
62  for( ; ind < sv1.size(); ++ind )
63  {
64  if( sv1[ ind ].first < sv2[ ind ].first ) return true;
65  if( sv2[ ind ].first < sv1[ ind ].first ) return false;
66  if( sv1[ ind ].second < sv2[ ind ].second ) return true;
67  if( sv2[ ind ].second < sv1[ ind ].second ) return false;
68  }
69  return false;
70 }
71 
73  : m_fname()
74 {
75  m_rotNumSeed = iConfig.getParameter<int>( "rotNumSeed" );
76  m_fname = iConfig.getUntrackedParameter<std::string>( "fileName" );
77  if( m_fname.empty() )
78  {
79  m_xos = &std::cout;
80  }
81  else
82  {
83  m_xos = new std::ofstream( m_fname.c_str());
84  }
85 
86  ( *m_xos ) << "<?xml version=\"1.0\"?>\n";
87  ( *m_xos ) << "<DDDefinition xmlns=\"http://www.cern.ch/cms/DDL\"\n";
88  ( *m_xos ) << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
89  ( *m_xos ) << "xsi:schemaLocation=\"http://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd\">\n";
90  ( *m_xos ) << std::fixed << std::setprecision( 18 );
91 }
92 
94 {
95  ( *m_xos ) << "</DDDefinition>\n";
96  ( *m_xos ) << std::endl;
97  m_xos->flush();
98 }
99 
100 void
102 {
103  edm::LogInfo( "OutputMagneticFieldDDToDDL" ) << "OutputMagneticFieldDDToDDL::beginRun";
104 
106  es.get<IdealMagneticFieldRecord>().get( pDD );
107 
108  const auto& gra = pDD->graph();
109 
110  // Temporary stores:
111  std::set<DDLogicalPart> lpStore;
112  std::set<DDMaterial> matStore;
113  std::set<DDSolid> solStore;
114 
115  std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > specStore;
116  std::set<DDRotation> rotStore;
117 
119 
120  std::string rn = m_fname;
121  size_t foundLastDot= rn.find_last_of('.');
122  size_t foundLastSlash= rn.find_last_of('/');
123 
124  if( foundLastSlash > foundLastDot && foundLastSlash != std::string::npos )
125  {
126  edm::LogError( "OutputMagneticFieldDDToDDL" ) << "What? last . before last / in path for filename... this should die...";
127  }
128  if( foundLastDot != std::string::npos && foundLastSlash != std::string::npos )
129  {
130  out.ns_ = rn.substr( foundLastSlash, foundLastDot );
131  }
132  else if ( foundLastDot != std::string::npos )
133  {
134  out.ns_ = rn.substr(0, foundLastDot);
135  }
136  else
137  {
138  edm::LogError( "OutputMagneticFieldDDToDDL" ) << "What? no file name? Attempt at namespace =\"" << out.ns_ << "\" filename was " << m_fname;
139  }
140 
141  edm::LogInfo( "OutputMagneticFieldDDToDDL" ) << "m_fname=" << m_fname << " namespace = " << out.ns_;
142  std::string ns_ = out.ns_;
143 
144  ( *m_xos ) << std::fixed << std::setprecision( 18 );
145 
146  using Graph = DDCompactView::Graph;
148 
149  adjl_iterator git = gra.begin();
150  adjl_iterator gend = gra.end();
151 
153  ( *m_xos) << "<PosPartSection label=\"" << ns_ << "\">\n";
154  git = gra.begin();
155  for( ; git != gend; ++git )
156  {
157  const DDLogicalPart & ddLP = gra.nodeData( git );
158  if( lpStore.find(ddLP) != lpStore.end())
159  {
160  addToSpecStore( ddLP, specStore );
161  }
162  lpStore.insert( ddLP );
163  addToMatStore( ddLP.material(), matStore );
164  addToSolStore( ddLP.solid(), solStore, rotStore );
165  ++i;
166  if( !git->empty())
167  {
168  // ask for children of ddLP
169  auto cit = git->begin();
170  auto cend = git->end();
171  for( ; cit != cend; ++cit )
172  {
173  const DDLogicalPart & ddcurLP = gra.nodeData( cit->first );
174  if( lpStore.find(ddcurLP) != lpStore.end())
175  {
176  addToSpecStore( ddcurLP, specStore );
177  }
178  lpStore.insert( ddcurLP );
179  addToMatStore( ddcurLP.material(), matStore );
180  addToSolStore( ddcurLP.solid(), solStore, rotStore );
181  rotStore.insert( gra.edgeData( cit->second )->ddrot() );
182  out.position( ddLP, ddcurLP, gra.edgeData( cit->second ), m_rotNumSeed, *m_xos );
183  } // iterate over children
184  } // if (children)
185  } // iterate over graph nodes
186 
187  ( *m_xos ) << "</PosPartSection>\n";
188 
189  ( *m_xos ) << std::scientific << std::setprecision( 18 );
190  std::set<DDMaterial>::const_iterator it( matStore.begin()), ed( matStore.end());
191  ( *m_xos) << "<MaterialSection label=\"" << ns_ << "\">\n";
192  for( ; it != ed; ++it )
193  {
194  if( ! it->isDefined().second ) continue;
195  out.material( *it, *m_xos );
196  }
197  ( *m_xos ) << "</MaterialSection>\n";
198 
199  ( *m_xos ) << "<RotationSection label=\"" << ns_ << "\">\n";
200  ( *m_xos ) << std::fixed << std::setprecision( 18 );
201  std::set<DDRotation>::iterator rit( rotStore.begin()), red( rotStore.end());
202  for( ; rit != red; ++rit )
203  {
204  if( ! rit->isDefined().second ) continue;
205  if( rit->toString() != ":" )
206  {
207  DDRotation r( *rit );
208  out.rotation( r, *m_xos );
209  }
210  }
211  ( *m_xos ) << "</RotationSection>\n";
212 
213  ( *m_xos ) << std::fixed << std::setprecision( 18 );
214  std::set<DDSolid>::const_iterator sit( solStore.begin()), sed( solStore.end());
215  ( *m_xos ) << "<SolidSection label=\"" << ns_ << "\">\n";
216  for( ; sit != sed; ++sit)
217  {
218  if( ! sit->isDefined().second ) continue;
219  out.solid( *sit, *m_xos );
220  }
221  ( *m_xos ) << "</SolidSection>\n";
222 
223  std::set<DDLogicalPart>::iterator lpit( lpStore.begin()), lped( lpStore.end());
224  ( *m_xos ) << "<LogicalPartSection label=\"" << ns_ << "\">\n";
225  for( ; lpit != lped; ++lpit )
226  {
227  if( ! lpit->isDefined().first ) continue;
228  const DDLogicalPart & lp = *lpit;
229  out.logicalPart( lp, *m_xos );
230  }
231  ( *m_xos ) << "</LogicalPartSection>\n";
232 
233  ( *m_xos ) << std::fixed << std::setprecision( 18 );
234  std::map<DDsvalues_type, std::set<const DDPartSelection*> >::const_iterator mit( specStore.begin()), mend( specStore.end());
235  ( *m_xos ) << "<SpecParSection label=\"" << ns_ << "\">\n";
236  for( ; mit != mend; ++mit )
237  {
238  out.specpar( *mit, *m_xos );
239  }
240  ( *m_xos ) << "</SpecParSection>\n";
241 }
242 
243 void
244 OutputMagneticFieldDDToDDL::addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore )
245 {
246  matStore.insert( mat );
247  if( mat.noOfConstituents() != 0 )
248  {
250  int findex( 0 );
251  while( findex < mat.noOfConstituents())
252  {
253  if( matStore.find( mat.constituent( findex ).first ) == matStore.end())
254  {
255  addToMatStore( mat.constituent( findex ).first, matStore );
256  }
257  ++findex;
258  }
259  }
260 }
261 
262 void
263 OutputMagneticFieldDDToDDL::addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore )
264 {
265  solStore.insert( sol );
267  {
268  const DDBooleanSolid& bs ( sol );
269  if( solStore.find(bs.solidA()) == solStore.end())
270  {
271  addToSolStore( bs.solidA(), solStore, rotStore );
272  }
273  if( solStore.find( bs.solidB()) == solStore.end())
274  {
275  addToSolStore( bs.solidB(), solStore, rotStore );
276  }
277  rotStore.insert( bs.rotation());
278  }
279 }
280 
281 void
283  std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > & specStore )
284 {
285  std::vector<std::pair<const DDPartSelection*, const DDsvalues_type*> >::const_iterator spit( lp.attachedSpecifics().begin()), spend( lp.attachedSpecifics().end());
286  for( ; spit != spend; ++spit )
287  {
288  specStore[ *spit->second ].insert( spit->first );
289  }
290 }
291 
T getParameter(std::string const &) const
void addToSpecStore(const DDLogicalPart &lp, std::map< const DDsvalues_type, std::set< const DDPartSelection * >, ddsvaluesCmp > &specStore)
T getUntrackedParameter(std::string const &, T const &) const
std::vector< double >::size_type index_type
Definition: Graph.h:16
void beginRun(edm::Run const &iEvent, edm::EventSetup const &) override
DDMaterial is used to define and access material information.
Definition: DDMaterial.h:43
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void specpar(const DDSpecifics &sp, std::ostream &xos)
void addToMatStore(const DDMaterial &mat, std::set< DDMaterial > &matStore)
const DDSolid & solid(void) const
Returns a reference object of the solid being the shape of this LogicalPart.
A DDSolid represents the shape of a part.
Definition: DDSolid.h:39
DDSolid solidB(void) const
Definition: DDSolid.cc:616
Represents a uniquely identifyable rotation matrix.
Definition: DDTransform.h:68
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:230
void analyze(edm::Event const &iEvent, edm::EventSetup const &) override
DDRotation rotation(void) const
Definition: DDSolid.cc:598
void position(const DDLogicalPart &parent, const DDLogicalPart &child, DDPosData *edgeToChild, int &rotNameSeed, std::ostream &xos)
FractionV::value_type constituent(int i) const
returns the i-th compound material and its fraction-mass
Definition: DDMaterial.cc:87
A DDLogicalPart aggregates information concerning material, solid and sensitveness ...
Definition: DDLogicalPart.h:93
DDSolidShape shape(void) const
The type of the solid.
Definition: DDSolid.cc:138
void addToSolStore(const DDSolid &sol, std::set< DDSolid > &solStore, std::set< DDRotation > &rotStore)
void logicalPart(const DDLogicalPart &lp, std::ostream &xos)
Graph::const_adj_iterator adjl_iterator
DDSolid solidA(void) const
Definition: DDSolid.cc:610
const Graph & graph() const
Provides read-only access to the data structure of the compact-view.
int noOfConstituents() const
returns the number of compound materials or 0 for elementary materials
Definition: DDMaterial.cc:82
OutputMagneticFieldDDToDDL(const edm::ParameterSet &iConfig)
T get() const
Definition: EventSetup.h:68
std::vector< std::pair< unsigned int, DDValue > > DDsvalues_type
Definition: DDsvalues.h:12
void material(const DDMaterial &material, std::ostream &xos)
math::Graph< DDLogicalPart, DDPosData * > Graph
Definition: DDCompactView.h:83
void endRun(edm::Run const &iEvent, edm::EventSetup const &) override
adj_list::const_iterator const_adj_iterator
Definition: Graph.h:125
void rotation(const DDRotation &rotation, std::ostream &xos, const std::string &rotn="")
void solid(const DDSolid &solid, std::ostream &xos)
const std::vector< std::pair< const DDPartSelection *, const DDsvalues_type * > > & attachedSpecifics(void) const
const DDMaterial & material(void) const
Returns a reference object of the material this LogicalPart is made of.
Definition: Run.h:44