CMS 3D CMS Logo

OutputDDToDDL.cc
Go to the documentation of this file.
17 
18 #include <cstddef>
19 #include <fstream>
20 #include <iomanip>
21 #include <map>
22 #include <memory>
23 #include <ostream>
24 #include <set>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace {
31  struct ddsvaluesCmp {
32  bool operator() ( const DDsvalues_type& sv1, const DDsvalues_type& sv2 ) const;
33  };
34 }
35 
36 class OutputDDToDDL : public edm::one::EDAnalyzer<edm::one::WatchRuns>
37 {
38 public:
39  explicit OutputDDToDDL( const edm::ParameterSet& iConfig );
40  ~OutputDDToDDL() override;
41 
42  void beginJob() override {}
43  void beginRun( edm::Run const& iEvent, edm::EventSetup const& ) override;
44  void analyze( edm::Event const& iEvent, edm::EventSetup const& ) override {}
45  void endRun( edm::Run const& iEvent, edm::EventSetup const& ) override {}
46  void endJob() override {}
47 
48 private:
49  void addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore );
50  void addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore );
51  void addToSpecStore( const DDLogicalPart& lp, std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > & specStore );
52 
55  std::ostream* m_xos;
56 };
57 
58 bool
59 ddsvaluesCmp::operator() ( const DDsvalues_type& sv1, const DDsvalues_type& sv2 ) const
60 {
61  if( sv1.size() < sv2.size()) return true;
62  if( sv2.size() < sv1.size()) return false;
63 
64  size_t ind = 0;
65  for( ; ind < sv1.size(); ++ind ) {
66  if ( sv1[ind].first < sv2[ind].first ) return true;
67  if ( sv2[ind].first < sv1[ind].first ) return false;
68  if ( sv1[ind].second < sv2[ind].second ) return true;
69  if ( sv2[ind].second < sv1[ind].second ) return false;
70  }
71  return false;
72 }
73 
75  : m_fname()
76 {
77  m_rotNumSeed = iConfig.getParameter<int>("rotNumSeed");
78  m_fname = iConfig.getUntrackedParameter<std::string>("fileName");
79  if( m_fname.empty() ) {
80  m_xos = &std::cout;
81  } else {
82  m_xos = new std::ofstream( m_fname.c_str());
83  }
84  (*m_xos) << "<?xml version=\"1.0\"?>" << std::endl;
85  (*m_xos) << "<DDDefinition xmlns=\"http://www.cern.ch/cms/DDL\"" << std::endl;
86  (*m_xos) << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" << std::endl;
87  (*m_xos) << "xsi:schemaLocation=\"http://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd\">" << std::endl;
88  (*m_xos) << std::fixed << std::setprecision(18);
89 }
90 
92 {
93  (*m_xos) << "</DDDefinition>" << std::endl;
94  (*m_xos) << std::endl;
95  m_xos->flush();
96 }
97 
98 void
100 {
101  std::cout << "OutputDDToDDL::beginRun" << std::endl;
102 
104  es.get<IdealGeometryRecord>().get( pDD );
105 
106  using Graph = DDCompactView::Graph;
108 
109  const auto& gra = pDD->graph();
110  // temporary stores:
111  std::set<DDLogicalPart> lpStore;
112  std::set<DDMaterial> matStore;
113  std::set<DDSolid> solStore;
114  // 2009-08-19: MEC: I've tried this with set<DDPartSelection> and
115  // had to write operator< for DDPartSelection and DDPartSelectionLevel
116  // the output from such an effort is different than this one.
117  std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > specStore;
118  std::set<DDRotation> rotStore;
119 
121 
122  std::string rn = m_fname;
123  size_t foundLastDot = rn.find_last_of('.');
124  size_t foundLastSlash = rn.find_last_of('/');
125  if( foundLastSlash > foundLastDot && foundLastSlash != std::string::npos ) {
126  std::cout << "What? last . before last / in path for filename... this should die..." << std::endl;
127  }
128  if( foundLastDot != std::string::npos && foundLastSlash != std::string::npos ) {
129  out.ns_ = rn.substr( foundLastSlash, foundLastDot );
130  } else if( foundLastDot != std::string::npos ) {
131  out.ns_ = rn.substr(0, foundLastDot);
132  } else {
133  std::cout << "What? no file name? Attempt at namespace =\"" << out.ns_ << "\" filename was " << m_fname << std::endl;
134  }
135  std::cout << "m_fname = " << m_fname << " namespace = " << out.ns_ << std::endl;
136  std::string ns_ = out.ns_;
137 
138  (*m_xos) << std::fixed << std::setprecision(18);
139 
140  adjl_iterator git = gra.begin();
141  adjl_iterator gend = gra.end();
142 
144  (*m_xos) << "<PosPartSection label=\"" << ns_ << "\">" << std::endl;
145  git = gra.begin();
146  for( ; git != gend; ++git ) {
147  const DDLogicalPart & ddLP = gra.nodeData( git );
148  if( lpStore.find( ddLP ) != lpStore.end()) {
149  addToSpecStore( ddLP, specStore );
150  }
151  lpStore.insert( ddLP );
152  addToMatStore( ddLP.material(), matStore );
153  addToSolStore( ddLP.solid(), solStore, rotStore );
154  ++i;
155  if( !git->empty()) {
156  // ask for children of ddLP
157  auto cit = git->begin();
158  auto cend = git->end();
159  for( ; cit != cend; ++cit ) {
160  const DDLogicalPart & ddcurLP = gra.nodeData( cit->first );
161  if( lpStore.find( ddcurLP ) != lpStore.end()) {
162  addToSpecStore( ddcurLP, specStore );
163  }
164  lpStore.insert( ddcurLP );
165  addToMatStore( ddcurLP.material(), matStore );
166  addToSolStore( ddcurLP.solid(), solStore, rotStore );
167  rotStore.insert( gra.edgeData( cit->second )->ddrot() );
168  out.position( ddLP, ddcurLP, gra.edgeData( cit->second ), m_rotNumSeed, *m_xos );
169  } // iterate over children
170  } // if (children)
171  } // iterate over graph nodes
172 
173  (*m_xos) << "</PosPartSection>" << std::endl;
174 
175  (*m_xos) << std::scientific << std::setprecision(18);
176 
177  (*m_xos) << "<MaterialSection label=\"" << ns_ << "\">" << std::endl;
178  for( auto it : matStore ) {
179  if( ! it.isDefined().second ) continue;
180  out.material( it, *m_xos );
181  }
182  (*m_xos) << "</MaterialSection>" << std::endl;
183  (*m_xos) << "<RotationSection label=\"" << ns_ << "\">" << std::endl;
184  (*m_xos) << std::fixed << std::setprecision(18);
185  std::set<DDRotation>::iterator rit( rotStore.begin()), red( rotStore.end());
186  for( ; rit != red; ++rit ) {
187  if( !rit->isDefined().second ) continue;
188  if( rit->toString() != ":" ) {
189  DDRotation r(*rit);
190  out.rotation(r, *m_xos);
191  }
192  }
193  (*m_xos) << "</RotationSection>" << std::endl;
194 
195  (*m_xos) << std::fixed << std::setprecision(18);
196  std::set<DDSolid>::const_iterator sit( solStore.begin()), sed( solStore.end());
197  (*m_xos) << "<SolidSection label=\"" << ns_ << "\">" << std::endl;
198  for( ; sit != sed; ++sit ) {
199  if( !sit->isDefined().second) continue;
200  out.solid( *sit, *m_xos );
201  }
202  (*m_xos) << "</SolidSection>" << std::endl;
203 
204  std::set<DDLogicalPart>::iterator lpit( lpStore.begin()), lped( lpStore.end());
205  (*m_xos) << "<LogicalPartSection label=\"" << ns_ << "\">" << std::endl;
206  for( ; lpit != lped; ++lpit ) {
207  if( !lpit->isDefined().first ) continue;
208  const DDLogicalPart & lp = *lpit;
209  out.logicalPart( lp, *m_xos);
210  }
211  (*m_xos) << "</LogicalPartSection>" << std::endl;
212 
213  (*m_xos) << std::fixed << std::setprecision(18);
214  std::map<DDsvalues_type, std::set<const DDPartSelection*> >::const_iterator mit( specStore.begin()), mend( specStore.end());
215  (*m_xos) << "<SpecParSection label=\"" << ns_ << "\">" << std::endl;
216  for( ; mit != mend; ++mit ) {
217  out.specpar ( *mit, *m_xos );
218  }
219  (*m_xos) << "</SpecParSection>" << std::endl;
220 }
221 
222 void
223 OutputDDToDDL::addToMatStore( const DDMaterial& mat, std::set<DDMaterial> & matStore )
224 {
225  matStore.insert( mat );
226  if( mat.noOfConstituents() != 0 ) {
227  int findex(0);
228  while( findex < mat.noOfConstituents()) {
229  if( matStore.find( mat.constituent( findex ).first) == matStore.end()) {
230  addToMatStore( mat.constituent( findex ).first, matStore );
231  }
232  ++findex;
233  }
234  }
235 }
236 
237 void
238 OutputDDToDDL::addToSolStore( const DDSolid& sol, std::set<DDSolid> & solStore, std::set<DDRotation>& rotStore )
239 {
240  solStore.insert(sol);
242  const DDBooleanSolid& bs (sol);
243  if( solStore.find( bs.solidA()) == solStore.end()) {
244  addToSolStore( bs.solidA(), solStore, rotStore );
245  }
246  if( solStore.find( bs.solidB()) == solStore.end()) {
247  addToSolStore( bs.solidB(), solStore, rotStore );
248  }
249  rotStore.insert( bs.rotation());
250  }
251 }
252 
253 void
255  std::map<const DDsvalues_type, std::set<const DDPartSelection*>, ddsvaluesCmp > & specStore )
256 {
257  for( auto spit : lp.attachedSpecifics()) {
258  specStore[*spit.second].insert( spit.first );
259  }
260 }
261 
void beginRun(edm::Run const &iEvent, edm::EventSetup const &) override
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
std::vector< double >::size_type index_type
Definition: Graph.h:16
void beginJob() override
DDMaterial is used to define and access material information.
Definition: DDMaterial.h:43
void addToSpecStore(const DDLogicalPart &lp, std::map< const DDsvalues_type, std::set< const DDPartSelection * >, ddsvaluesCmp > &specStore)
std::string m_fname
void addToMatStore(const DDMaterial &mat, std::set< DDMaterial > &matStore)
void specpar(const DDSpecifics &sp, std::ostream &xos)
const DDSolid & solid(void) const
Returns a reference object of the solid being the shape of this LogicalPart.
void addToSolStore(const DDSolid &sol, std::set< DDSolid > &solStore, std::set< DDRotation > &rotStore)
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:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
DDRotation rotation(void) const
Definition: DDSolid.cc:598
std::ostream * m_xos
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 endJob() override
void analyze(edm::Event const &iEvent, edm::EventSetup const &) override
void logicalPart(const DDLogicalPart &lp, std::ostream &xos)
Graph::const_adj_iterator adjl_iterator
~OutputDDToDDL() override
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
void endRun(edm::Run const &iEvent, edm::EventSetup const &) override
OutputDDToDDL(const edm::ParameterSet &iConfig)
T get() const
Definition: EventSetup.h:71
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
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:45