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 );
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 )
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 == "" )
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  DDCompactView::DDCompactView::graph_type 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  typedef DDCompactView::graph_type::const_adj_iterator adjl_iterator;
147 
148  adjl_iterator git = gra.begin();
149  adjl_iterator gend = gra.end();
150 
152  ( *m_xos) << "<PosPartSection label=\"" << ns_ << "\">\n";
153  git = gra.begin();
154  for( ; git != gend; ++git )
155  {
156  const DDLogicalPart & ddLP = gra.nodeData( git );
157  if( lpStore.find(ddLP) != lpStore.end())
158  {
159  addToSpecStore( ddLP, specStore );
160  }
161  lpStore.insert( ddLP );
162  addToMatStore( ddLP.material(), matStore );
163  addToSolStore( ddLP.solid(), solStore, rotStore );
164  ++i;
165  if( !git->empty())
166  {
167  // ask for children of ddLP
168  DDCompactView::graph_type::edge_list::const_iterator cit = git->begin();
169  DDCompactView::graph_type::edge_list::const_iterator cend = git->end();
170  for( ; cit != cend; ++cit )
171  {
172  const DDLogicalPart & ddcurLP = gra.nodeData( cit->first );
173  if( lpStore.find(ddcurLP) != lpStore.end())
174  {
175  addToSpecStore( ddcurLP, specStore );
176  }
177  lpStore.insert( ddcurLP );
178  addToMatStore( ddcurLP.material(), matStore );
179  addToSolStore( ddcurLP.solid(), solStore, rotStore );
180  rotStore.insert( gra.edgeData( cit->second )->ddrot() );
181  out.position( ddLP, ddcurLP, gra.edgeData( cit->second ), m_rotNumSeed, *m_xos );
182  } // iterate over children
183  } // if (children)
184  } // iterate over graph nodes
185 
186  ( *m_xos ) << "</PosPartSection>\n";
187 
188  ( *m_xos ) << std::scientific << std::setprecision( 18 );
189  std::set<DDMaterial>::const_iterator it( matStore.begin()), ed( matStore.end());
190  ( *m_xos) << "<MaterialSection label=\"" << ns_ << "\">\n";
191  for( ; it != ed; ++it )
192  {
193  if( ! it->isDefined().second ) continue;
194  out.material( *it, *m_xos );
195  }
196  ( *m_xos ) << "</MaterialSection>\n";
197 
198  ( *m_xos ) << "<RotationSection label=\"" << ns_ << "\">\n";
199  ( *m_xos ) << std::fixed << std::setprecision( 18 );
200  std::set<DDRotation>::iterator rit( rotStore.begin()), red( rotStore.end());
201  for( ; rit != red; ++rit )
202  {
203  if( ! rit->isDefined().second ) continue;
204  if( rit->toString() != ":" )
205  {
206  DDRotation r( *rit );
207  out.rotation( r, *m_xos );
208  }
209  }
210  ( *m_xos ) << "</RotationSection>\n";
211 
212  ( *m_xos ) << std::fixed << std::setprecision( 18 );
213  std::set<DDSolid>::const_iterator sit( solStore.begin()), sed( solStore.end());
214  ( *m_xos ) << "<SolidSection label=\"" << ns_ << "\">\n";
215  for( ; sit != sed; ++sit)
216  {
217  if( ! sit->isDefined().second ) continue;
218  out.solid( *sit, *m_xos );
219  }
220  ( *m_xos ) << "</SolidSection>\n";
221 
222  std::set<DDLogicalPart>::iterator lpit( lpStore.begin()), lped( lpStore.end());
223  ( *m_xos ) << "<LogicalPartSection label=\"" << ns_ << "\">\n";
224  for( ; lpit != lped; ++lpit )
225  {
226  if( ! lpit->isDefined().first ) continue;
227  const DDLogicalPart & lp = *lpit;
228  out.logicalPart( lp, *m_xos );
229  }
230  ( *m_xos ) << "</LogicalPartSection>\n";
231 
232  ( *m_xos ) << std::fixed << std::setprecision( 18 );
233  std::map<DDsvalues_type, std::set<const DDPartSelection*> >::const_iterator mit( specStore.begin()), mend( specStore.end());
234  ( *m_xos ) << "<SpecParSection label=\"" << ns_ << "\">\n";
235  for( ; mit != mend; ++mit )
236  {
237  out.specpar( *mit, *m_xos );
238  }
239  ( *m_xos ) << "</SpecParSection>\n";
240 }
241 
242 void
243 OutputMagneticFieldDDToDDL::addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore )
244 {
245  matStore.insert( mat );
246  if( mat.noOfConstituents() != 0 )
247  {
249  int findex( 0 );
250  while( findex < mat.noOfConstituents())
251  {
252  if( matStore.find( mat.constituent( findex ).first ) == matStore.end())
253  {
254  addToMatStore( mat.constituent( findex ).first, matStore );
255  }
256  ++findex;
257  }
258  }
259 }
260 
261 void
262 OutputMagneticFieldDDToDDL::addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore )
263 {
264  solStore.insert( sol );
265  if( sol.shape() == ddunion || sol.shape() == ddsubtraction || sol.shape() == ddintersection )
266  {
267  const DDBooleanSolid& bs ( sol );
268  if( solStore.find(bs.solidA()) == solStore.end())
269  {
270  addToSolStore( bs.solidA(), solStore, rotStore );
271  }
272  if( solStore.find( bs.solidB()) == solStore.end())
273  {
274  addToSolStore( bs.solidB(), solStore, rotStore );
275  }
276  rotStore.insert( bs.rotation());
277  }
278 }
279 
280 void
282  std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > & specStore )
283 {
284  std::vector<std::pair<const DDPartSelection*, const DDsvalues_type*> >::const_iterator spit( lp.attachedSpecifics().begin()), spend( lp.attachedSpecifics().end());
285  for( ; spit != spend; ++spit )
286  {
287  specStore[ *spit->second ].insert( spit->first );
288  }
289 }
290 
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:41
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const graph_type & graph() const
Provides read-only access to the data structure of the compact-view.
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:38
DDSolid solidB(void) const
Definition: DDSolid.cc:668
Represents a uniquely identifyable rotation matrix.
Definition: DDTransform.h:67
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:650
void position(const DDLogicalPart &parent, const DDLogicalPart &child, DDPosData *edgeToChild, int &rotNameSeed, std::ostream &xos)
std::vector< std::pair< unsigned int, DDValue > > DDsvalues_type
std::maps an index to a DDValue. The index corresponds to the index assigned to the name of the std::...
Definition: DDsvalues.h:20
FractionV::value_type constituent(int i) const
returns the i-th compound material and its fraction-mass
Definition: DDMaterial.cc:90
A DDLogicalPart aggregates information concerning material, solid and sensitveness ...
Definition: DDLogicalPart.h:92
DDSolidShape shape(void) const
The type of the solid.
Definition: DDSolid.cc:144
void addToSolStore(const DDSolid &sol, std::set< DDSolid > &solStore, std::set< DDRotation > &rotStore)
void logicalPart(const DDLogicalPart &lp, std::ostream &xos)
DDSolid solidA(void) const
Definition: DDSolid.cc:662
const T & get() const
Definition: EventSetup.h:55
int noOfConstituents() const
returns the number of compound materials or 0 for elementary materials
Definition: DDMaterial.cc:84
OutputMagneticFieldDDToDDL(const edm::ParameterSet &iConfig)
void material(const DDMaterial &material, std::ostream &xos)
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:42