CMS 3D CMS Logo

OutputDD4hepToDDL.cc
Go to the documentation of this file.
17 
18 #include "TGeoManager.h"
19 
20 #include <cstddef>
21 #include <fstream>
22 #include <iomanip>
23 #include <map>
24 #include <memory>
25 #include <ostream>
26 #include <set>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 class OutputDD4hepToDDL : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
32 public:
33  explicit OutputDD4hepToDDL(const edm::ParameterSet &iConfig);
34  ~OutputDD4hepToDDL() override;
35 
36  void beginJob() override {}
37  void beginRun(edm::Run const &iEvent, edm::EventSetup const &) override;
38  void analyze(edm::Event const &iEvent, edm::EventSetup const &) override {}
39  void endRun(edm::Run const &iEvent, edm::EventSetup const &) override {}
40  void endJob() override {}
41 
42 private:
45  std::ostream *m_xos;
46 };
47 
49  : cpvTokendd4hep_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "make-payload"))), m_fname() {
50  m_fname = iConfig.getUntrackedParameter<std::string>("fileName");
51  if (m_fname.empty()) {
52  m_xos = &std::cout;
53  } else {
54  m_xos = new std::ofstream(m_fname.c_str());
55  }
56  (*m_xos) << "<?xml version=\"1.0\"?>" << std::endl;
57  (*m_xos) << "<DDDefinition>" << std::endl;
58 }
59 
61  (*m_xos) << "</DDDefinition>" << std::endl;
62  (*m_xos) << std::endl;
63  m_xos->flush();
64 }
65 
67  std::cout << "OutputDD4hepToDDL::beginRun" << std::endl;
68 
70 
71  const cms::DDDetector *det = cpv->detector();
72  const dd4hep::Detector &detector = *det->description();
73  const dd4hep::SpecParRegistry &specStore = det->specpars();
74 
76 
77  std::string rn = m_fname;
78  size_t foundLastDot = rn.find_last_of('.');
79  size_t foundLastSlash = rn.find_last_of('/');
80  if (foundLastSlash > foundLastDot && foundLastSlash != std::string::npos) {
81  std::cout << "What? last . before last / in path for filename... this should die..." << std::endl;
82  }
83  std::string ns_("none");
84  if (foundLastDot != std::string::npos && foundLastSlash != std::string::npos) {
85  ns_ = rn.substr(foundLastSlash, foundLastDot);
86  } else if (foundLastDot != std::string::npos) {
87  ns_ = rn.substr(0, foundLastDot);
88  } else {
89  std::cout << "What? no file name? Attempt at namespace =\"" << ns_ << "\" filename was " << m_fname << std::endl;
90  }
91  std::cout << "m_fname = " << m_fname << " namespace = " << ns_ << std::endl;
92 
93  (*m_xos) << std::fixed << std::setprecision(5); // Precison to 1e-5 mm
94  cms::DDParsingContext *const parsingContext = detector.extension<cms::DDParsingContext>();
95 
96  {
97  // Add rotation for reference and to ease validation
98  using namespace angle_units::operators;
99  cms::DDNamespace nameSpace(*parsingContext);
100  dd4hep::Rotation3D rotation(1., 0., 0., 0., 1., 0., 0., 0., -1.);
101  rotation = rotation * dd4hep::RotationY(1._pi);
102  nameSpace.addRotation("ebalgo:reflZRotY", rotation);
103  }
104 
105  (*m_xos) << "<PosPartSection label=\"" << ns_ << "\">" << std::endl;
106  const TGeoManager &mgr = detector.manager();
107  for (const auto &&iter : *mgr.GetListOfVolumes()) {
108  auto *vol = dynamic_cast<TGeoVolume *>(iter);
109  int numDaughters = vol->GetNdaughters();
110  if (numDaughters > 0) {
111  auto nodeArray = vol->GetNodes();
112  for (int index = 0; index < numDaughters; ++index) {
113  auto *node = dynamic_cast<TGeoNode *>((*nodeArray)[index]);
114  auto *childVol = node->GetVolume();
115  out.position(*vol, *node, childVol->GetName(), *parsingContext, *m_xos);
116  }
117  }
118  }
119  (*m_xos) << "</PosPartSection>" << std::endl;
120 
121  (*m_xos) << "<MaterialSection label=\"" << ns_ << "\">" << std::endl;
122  for (const auto &&iter : *mgr.GetListOfMaterials()) {
123  out.element(dynamic_cast<const TGeoMaterial *>(iter), *m_xos);
124  }
125  // Output composite materials
126  for (const auto &it : parsingContext->compMaterialsVec) {
127  const std::string &matName = it.first;
128  out.material(matName, it.second, parsingContext->compMaterialsRefs[matName], *m_xos);
129  }
130  (*m_xos) << "</MaterialSection>" << std::endl;
131  (*m_xos) << "<RotationSection label=\"" << ns_ << "\">" << std::endl;
132  (*m_xos) << std::fixed << std::setprecision(10); // Rounds angles to integer values w/o loss of accuracy
133  // rotRevMap has the unique rotations
134  for (const auto &rotPair : parsingContext->rotRevMap) {
135  out.rotation(parsingContext->rotations[rotPair.second], *m_xos, *parsingContext, rotPair.second);
136  }
137  (*m_xos) << "</RotationSection>" << std::endl;
138 
139  (*m_xos) << std::fixed << std::setprecision(5);
140  (*m_xos) << "<SolidSection label=\"" << ns_ << "\">" << std::endl;
141  for (const auto &&iter : *mgr.GetListOfShapes()) {
142  auto *shape = dynamic_cast<TGeoShape *>(iter);
143  if (shape->IsValid()) {
144  dd4hep::Solid solid(shape);
145  if (strlen(shape->GetTitle()) > 1) {
146  out.solid(solid, *parsingContext, *m_xos);
147  } else {
149  if (name != "Box" && name != "Tubs") { // Skip solids with degenerate names
150  if (dd4hep::isA<dd4hep::Tube>(solid)) {
151  shape->SetTitle("Tube");
152  out.solid(solid, *parsingContext, *m_xos);
153  } else if (dd4hep::isA<dd4hep::Box>(solid)) {
154  shape->SetTitle("Box");
155  out.solid(solid, *parsingContext, *m_xos);
156  } else if (dd4hep::isA<dd4hep::Trd1>(solid)) {
157  shape->SetTitle("Trd1");
158  out.solid(solid, *parsingContext, *m_xos);
159  } else
160  std::cout << "Division solid not a box, trd1, or tube = " << solid.name() << std::endl;
161  }
162  }
163  }
164  }
165  for (const auto &asmEntry : parsingContext->assemblySolids) {
166  (*m_xos) << "<Assembly name=\"" << asmEntry << "\"/>" << std::endl;
167  }
168  (*m_xos) << "</SolidSection>" << std::endl;
169 
170  (*m_xos) << "<LogicalPartSection label=\"" << ns_ << "\">" << std::endl;
171  for (const auto &&iter : *mgr.GetListOfVolumes()) {
172  auto *vol = dynamic_cast<TGeoVolume *>(iter);
173  // Skip unused logical parts and assemblies, which are listed separately
174  if (vol->GetRefCount() > 0 && vol->IsAssembly() == false) {
175  out.logicalPart(*vol, *m_xos);
176  }
177  }
178  for (const auto &asEntry : parsingContext->assemblies) {
179  out.logicalPart(asEntry.first, *m_xos);
180  }
181  (*m_xos) << "</LogicalPartSection>" << std::endl;
182 
183  (*m_xos) << std::fixed << std::setprecision(10); // Some Tracker values specified to 1e-9
184  (*m_xos) << "<SpecParSection label=\"" << ns_ << "\">" << std::endl;
185  for (const auto &specPar : specStore.specpars) {
186  out.specpar(specPar.first, specPar.second, *m_xos);
187  }
188  (*m_xos) << "</SpecParSection>" << std::endl;
189 }
190 
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
cms::DDDetector::specpars
dd4hep::SpecParRegistry const & specpars() const
Definition: DDDetector.h:21
EDAnalyzer.h
DDCoreToDDXMLOutput
Definition: DDCoreToDDXMLOutput.h:40
ESTransientHandle.h
cms::DDNamespace::addRotation
void addRotation(const std::string &name, const dd4hep::Rotation3D &rot) const
Definition: DDNamespace.cc:158
ESInputTag
OutputDD4hepToDDL::m_fname
std::string m_fname
Definition: OutputDD4hepToDDL.cc:44
edm::Run
Definition: Run.h:45
OutputDD4hepToDDL
Definition: OutputDD4hepToDDL.cc:31
cms::DDParsingContext
Definition: DDParsingContext.h:14
cms::DDNamespace
Definition: DDNamespace.h:22
edm
HLT enums.
Definition: AlignableModifier.h:19
OutputDD4hepToDDL::endRun
void endRun(edm::Run const &iEvent, edm::EventSetup const &) override
Definition: OutputDD4hepToDDL.cc:39
gather_cfg.cout
cout
Definition: gather_cfg.py:144
cms::DDParsingContext::compMaterialsVec
std::vector< std::pair< std::string, double > > compMaterialsVec
Definition: DDParsingContext.h:88
OutputDD4hepToDDL::OutputDD4hepToDDL
OutputDD4hepToDDL(const edm::ParameterSet &iConfig)
Definition: OutputDD4hepToDDL.cc:48
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
OutputDD4hepToDDL::analyze
void analyze(edm::Event const &iEvent, edm::EventSetup const &) override
Definition: OutputDD4hepToDDL.cc:38
ESGetToken.h
cms::DDDetector::description
dd4hep::Detector const * description() const
Definition: DDDetector.h:35
cms::DDParsingContext::rotations
std::unordered_map< std::string, dd4hep::Rotation3D > rotations
Definition: DDParsingContext.h:81
MakerMacros.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
cms::DDParsingContext::rotRevMap
std::unordered_map< std::string, std::string > rotRevMap
Definition: DDParsingContext.h:82
DDSolidShapes.h
OutputDD4hepToDDL::endJob
void endJob() override
Definition: OutputDD4hepToDDL.cc:40
DDFilteredView.h
DDCoreToDDXMLOutput.h
DDCompactView.h
ParameterSetDescription.h
DDAlgoArguments.h
OutputDD4hepToDDL::~OutputDD4hepToDDL
~OutputDD4hepToDDL() override
Definition: OutputDD4hepToDDL.cc:60
OutputDD4hepToDDL::beginRun
void beginRun(edm::Run const &iEvent, edm::EventSetup const &) override
Definition: OutputDD4hepToDDL.cc:66
edm::ParameterSet
Definition: ParameterSet.h:47
idealTransformation.rotation
dictionary rotation
Definition: idealTransformation.py:1
edm::Transition
Transition
Definition: Transition.h:12
Event.h
angle_units::operators
Definition: angle_units.h:11
OutputDD4hepToDDL::m_xos
std::ostream * m_xos
Definition: OutputDD4hepToDDL.cc:45
cms::DDParsingContext::assemblies
std::unordered_map< std::string, dd4hep::Assembly > assemblies
Definition: DDParsingContext.h:79
iEvent
int iEvent
Definition: GenABIO.cc:224
IdealGeometryRecord.h
edm::EventSetup
Definition: EventSetup.h:58
edm::EventSetup::getTransientHandle
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:166
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord >
edm::ESTransientHandle
Definition: ESTransientHandle.h:41
align::Detector
Definition: StructureType.h:92
OutputDD4hepToDDL::cpvTokendd4hep_
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord > cpvTokendd4hep_
Definition: OutputDD4hepToDDL.cc:43
cms::DDDetector
Definition: DDDetector.h:12
DDCoreToDDXMLOutput::trimShapeName
static std::string trimShapeName(const std::string &solidName)
Definition: DDCoreToDDXMLOutput.cc:93
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
hgcalTestNeighbor_cfi.detector
detector
Definition: hgcalTestNeighbor_cfi.py:6
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
ConsumesCollector.h
ParameterSet.h
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
angle_units.h
edm::Event
Definition: Event.h:73
cms::DDParsingContext::compMaterialsRefs
std::unordered_map< std::string, std::vector< CompositeMaterial > > compMaterialsRefs
Definition: DDParsingContext.h:89
cms::DDParsingContext::assemblySolids
std::unordered_set< std::string > assemblySolids
Definition: DDParsingContext.h:80
cms::DDCompactView::detector
const cms::DDDetector * detector() const
Definition: DDCompactView.h:34
OutputDD4hepToDDL::beginJob
void beginJob() override
Definition: OutputDD4hepToDDL.cc:36