DD4hep  1.36.0
Detector Description Toolkit for High Energy Physics
Vis2XmlExtract.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework includes
15 #include "Vis2XmlExtract.h"
16 #include <DD4hep/Printout.h>
17 #include <DD4hep/Volumes.h>
18 #include <XML/DocumentHandler.h>
19 
21 #include <fstream>
22 #include <iostream>
23 #include <sstream>
24 
25 namespace {
26  std::string genName(const std::string& n, const void* ptr) {
27  std::string nn = n;
28  char text[32];
29  ::snprintf(text,sizeof(text),"%p",ptr);
30  nn += "_";
31  nn += text;
32  return nn;
33  }
34 }
35 
38  : m_detDesc(description), m_dataPtr(0) {
39 }
40 
42  if (m_dataPtr)
43  delete m_dataPtr;
44  m_dataPtr = 0;
45 }
46 
48 xml_h dd4hep::detail::Vis2XmlExtract::handleVolume( const std::string& /* name */, Volume volume ) const {
49  GeometryInfo& geo = *this->m_dataPtr;
50  xml_h vol = geo.xmlVolumes[volume];
51  if (!vol) {
52  std::string n = genName(volume->GetName(),volume);
53  if (volume->IsAssembly()) {
54  vol = xml_elt_t(geo.doc, _U(assembly));
55  vol.setAttr(_U(name), n);
56  }
57  else {
58  vol = xml_elt_t(geo.doc, _U(volume));
59  vol.setAttr(_U(name), n);
60  }
61  geo.doc_structure.append(vol);
62  geo.xmlVolumes[volume] = vol;
63  if( volume.data() ) {
64  VisAttr vis = volume.visAttributes();
65  if (vis.isValid()) {
66  xml_ref_t xml_data = handleVis(vis.name(), vis);
67  vol.setRef(_U(visref), xml_data.name());
68  }
69  }
70  }
71  return vol;
72 }
73 
75 xml_h dd4hep::detail::Vis2XmlExtract::handleVolumeVis( const std::string& /* name */, const TGeoVolume* volume ) const {
76  GeometryInfo& geo = *this->m_dataPtr;
77  xml_h vol = geo.xmlVolumes[volume];
78  if (!vol) {
79  Volume v(volume);
80  if( v.data() ) {
81  VisAttr vis = v.visAttributes();
82  if (vis.isValid()) {
83  geo.doc_structure.append(vol = xml_elt_t(geo.doc, _U(volume)));
84  vol.setAttr(_U(name), v->GetName());
85  xml_ref_t xml_data = handleVis(vis.name(), vis);
86  vol.setRef(_U(visref), xml_data.name());
87  geo.xmlVolumes[v] = vol;
88  }
89  }
90  }
91  return vol;
92 }
93 
95 xml_h dd4hep::detail::Vis2XmlExtract::handleVis( const std::string& /* name */, VisAttr attr ) const {
96  GeometryInfo& geo = *this->m_dataPtr;
97  xml_h vis = geo.xmlVis[attr];
98  if (!vis) {
99  float red = 0, green = 0, blue = 0;
100  int style = attr.lineStyle();
101  int draw = attr.drawingStyle();
102 
103  geo.doc_display.append(vis = xml_elt_t(geo.doc, _U(vis)));
104  vis.setAttr(_U(name), attr.name());
105  vis.setAttr(_U(visible), attr.visible());
106  vis.setAttr(_U(show_daughters), attr.showDaughters());
107  if (style == VisAttr::SOLID)
108  vis.setAttr(_U(line_style), "unbroken");
109  else if (style == VisAttr::DASHED)
110  vis.setAttr(_U(line_style), "broken");
111  if (draw == VisAttr::SOLID)
112  vis.setAttr(_U(drawing_style), "solid");
113  else if (draw == VisAttr::WIREFRAME)
114  vis.setAttr(_U(drawing_style), "wireframe");
115 
116  xml_h col = xml_elt_t(geo.doc, _U(color));
117  attr.rgb(red, green, blue);
118  col.setAttr(_U(alpha), attr.alpha());
119  col.setAttr(_U(R), red);
120  col.setAttr(_U(B), blue);
121  col.setAttr(_U(G), green);
122  vis.append(col);
123  geo.xmlVis[attr] = vis;
124  }
125  return vis;
126 }
127 
130  if (!top.isValid()) {
131  throw std::runtime_error("Attempt to call createDetector with an invalid geometry!");
132  }
133 
134  GeometryInfo& geo = *(m_dataPtr = new GeometryInfo);
135  m_data->clear();
136  collect(top, geo);
137  printout(ALWAYS,"Vis2XmlExtract","++ ==> Dump visualisation attributes "
138  "from in memory detector description...");
140  xml_elt_t elt(0);
141  geo.doc = docH.create("visualization", docH.defaultComment());
142  geo.doc_root = geo.doc.root();
143  geo.doc_root.append(geo.doc_display = xml_elt_t(geo.doc, _U(display)));
144  geo.doc_root.append(geo.doc_structure = xml_elt_t(geo.doc, _U(structure)));
145 
146  for( Volume v : geo.volumes )
147  this->handleVolumeVis(v->GetName(), v.ptr());
148  printout(ALWAYS,"Vis2XmlExtract","++ Handled %ld volumes.",geo.volumes.size());
149  return geo.doc;
150 }
151 
154  : doc(0), doc_root(0), doc_display(0), doc_structure(0)
155 {
156 }
157 
158 static long dump_output( xml_doc_t doc, int argc, char** argv ) {
160  return docH.output(doc, argc > 0 ? argv[0] : "");
161 }
162 
163 static long create_vis( dd4hep::Detector& description, int argc, char** argv ) {
164  dd4hep::detail::Vis2XmlExtract wr(description);
165  dd4hep::xml::DocumentHolder doc(wr.createVis(description.world()).ptr());
166  return dump_output(doc, argc, argv);
167 }
168 
169 static long create_visASCII( dd4hep::Detector& description, int /* argc */, char** argv ) {
170  dd4hep::detail::Vis2XmlExtract wr(description);
171  /* xml_doc_t doc = */ wr.createVis(description.world());
172  auto& geo = *wr.m_dataPtr;
173  std::map<std::string, xml_comp_t> vis_map;
174  for (xml_coll_t c(geo.doc_display, _U(vis)); c; ++c)
175  vis_map.insert(make_pair(xml_comp_t(c).nameStr(), xml_comp_t(c)));
176 
177  const char* sep = ";";
178  std::ofstream os(argv[0]);
179  for (xml_coll_t c(geo.doc_structure, _U(volume)); c; ++c) {
180  xml_comp_t vol = c;
181  xml_comp_t ref = c.child(_U(visref));
182  auto iter = vis_map.find(ref.refStr());
183  if ( iter != vis_map.end() ) {
184  xml_comp_t vis = iter->second;
185  xml_comp_t col = vis.child(_U(color));
186  os << "vol:" << vol.nameStr() << sep << "vis:" << vis.nameStr() << sep
187  << "visible:" << vis.visible() << sep << "r:"
188  << col.R() << sep << "g:" << col.G() << sep << "b:" << col.B() << sep
189  << "alpha:" << col.alpha() << sep << "line_style:"
190  << vis.attr < std::string > (_U(line_style)) << sep
191  << "drawing_style:" << vis.attr < std::string> (_U(drawing_style)) << sep
192  << "show_daughters:" << vis.show_daughters() << sep << std::endl;
193  }
194  }
195  os.close();
196  return 1;
197 }
198 
199 DECLARE_APPLY(DD4hepGeometry2VIS, create_vis)
200 DECLARE_APPLY(DD4hepGeometry2VISASCII, create_visASCII)
dd4hep::detail::Vis2XmlExtract::GeometryInfo::GeometryInfo
GeometryInfo()
Helper constructor.
Definition: Vis2XmlExtract.cpp:153
dd4hep::xml::DocumentHandler::create
Document create(const char *tag, const char *comment=0) const
Create new XML document by parsing empty xml buffer.
Definition: DocumentHandler.cpp:680
dd4hep::xml::Collection_t
Class to support the access to collections of XmlNodes (or XmlElements)
Definition: XMLElements.h:636
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
Volumes.h
dd4hep::detail::Vis2XmlExtract::GeometryInfo::doc
xml_doc_t doc
Definition: Vis2XmlExtract.h:53
Vis2XmlExtract.h
dd4hep::xml::Element::append
void append(Handle_t handle) const
Append a new element to the existing tree.
Definition: XMLElements.h:839
v
View * v
Definition: MultiView.cpp:28
dd4hep::VisAttr
Handle class describing visualization attributes.
Definition: Objects.h:323
dd4hep::xml::Handle_t::setRef
Handle_t setRef(const XmlChar *tag, const XmlChar *ref)
Add reference child as a new child node. The obj must have the "name" attribute!
Definition: XMLElements.cpp:940
dd4hep::xml::Handle_t::append
void append(Handle_t e) const
Append a DOM element to the current node.
Definition: XMLElements.cpp:730
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
dd4hep::detail::Vis2XmlExtract
Geometry converter from dd4hep to Geant 4 in Detector format.
Definition: Vis2XmlExtract.h:40
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:126
dd4hep::detail::Vis2XmlExtract::Vis2XmlExtract
Vis2XmlExtract(Detector &description)
Initializing Constructor.
Definition: Vis2XmlExtract.cpp:37
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::VisAttr::alpha
float alpha() const
Get alpha value.
Definition: Objects.cpp:370
xml_comp_t
dd4hep::xml::Component xml_comp_t
Definition: XML.h:33
DocumentHandler.h
dd4hep::VisAttr::lineStyle
int lineStyle() const
Get line style.
Definition: Objects.cpp:350
dd4hep::VisAttr::drawingStyle
int drawingStyle() const
Get drawing style.
Definition: Objects.cpp:360
dd4hep::detail::Vis2XmlExtract::GeometryInfo::doc_structure
xml_elt_t doc_structure
Definition: Vis2XmlExtract.h:54
dd4hep::detail::Vis2XmlExtract::GeometryInfo
Data structure of the geometry converter from dd4hep to extract visualization information.
Definition: Vis2XmlExtract.h:49
dd4hep::VisAttr::showDaughters
bool showDaughters() const
Get Flag to show/hide daughter elements.
Definition: Objects.cpp:330
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:187
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:371
dd4hep::detail::Vis2XmlExtract::GeometryInfo::xmlVis
std::map< VisAttr, xml::XmlElement * > xmlVis
Definition: Vis2XmlExtract.h:52
dd4hep::VisAttr::DASHED
@ DASHED
Definition: Objects.h:326
dd4hep::detail::Vis2XmlExtract::handleVolumeVis
virtual xml_h handleVolumeVis(const std::string &name, const TGeoVolume *volume) const
Analyze Visualization attributes of Volume in the geometry hierarchy.
Definition: Vis2XmlExtract.cpp:75
_U
#define _U(a)
Definition: Tags.h:23
dd4hep::detail::Vis2XmlExtract::createVis
xml_doc_t createVis(DetElement top)
Create geometry conversion in Vis format.
Definition: Vis2XmlExtract.cpp:129
dd4hep::xml::RefElement::name
const XmlChar * name() const
Access the object's name in unicode.
Definition: XMLElements.cpp:1180
dd4hep::detail::Vis2XmlExtract::GeometryInfo::xmlVolumes
std::map< Volume, xml::XmlElement * > xmlVolumes
Definition: Vis2XmlExtract.h:51
dd4hep::VisAttr::rgb
bool rgb(float &red, float &green, float &blue) const
Get RGB values of the color (if valid)
Definition: Objects.cpp:404
dd4hep::detail::Vis2XmlExtract::~Vis2XmlExtract
virtual ~Vis2XmlExtract()
Standard destructor.
Definition: Vis2XmlExtract.cpp:41
dd4hep::xml::DocumentHandler::defaultComment
static std::string defaultComment()
Default comment string.
Definition: DocumentHandler.cpp:629
dd4hep::xml::DocumentHolder
Class supporting the basic functionality of an XML document including ownership.
Definition: XMLElements.h:741
dd4hep::detail::Vis2XmlExtract::handleVolume
virtual xml_h handleVolume(const std::string &name, Volume volume) const
Analyze Volume in the geometry hierarchy.
Definition: Vis2XmlExtract.cpp:48
dd4hep::detail::Vis2XmlExtract::GeometryInfo::doc_root
xml_elt_t doc_root
Definition: Vis2XmlExtract.h:54
dd4hep::xml::Document
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:697
dd4hep::detail::Vis2XmlExtract::handleVis
virtual xml_h handleVis(const std::string &name, VisAttr vis) const
Convert the geometry visualisation attributes to the corresponding Xml object(s).
Definition: Vis2XmlExtract.cpp:95
dd4hep::xml::RefElement
User abstraction class to manipulate named XML elements (references) within a document.
Definition: XMLElements.h:953
dd4hep::xml::DocumentHandler::output
virtual int output(Document doc, const std::string &fname) const
Write xml document to output file (stdout if file name empty)
Definition: DocumentHandler.cpp:395
dd4hep::VisAttr::visible
bool visible() const
Get visibility flag.
Definition: Objects.cpp:340
dd4hep::detail::Vis2XmlExtract::GeometryInfo::doc_display
xml_elt_t doc_display
Definition: Vis2XmlExtract.h:54
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
dd4hep::VisAttr::SOLID
@ SOLID
Definition: Objects.h:326
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep::VisAttr::WIREFRAME
@ WIREFRAME
Definition: Objects.h:326
xml_elt_t
dd4hep::xml::Element xml_elt_t
Definition: ConditionsRepository.cpp:33
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:89
dd4hep::xml::Handle_t::setAttr
Attribute setAttr(const XmlChar *t, const XmlChar *v) const
Generic attribute setter with unicode value.
Definition: XMLElements.cpp:922
dd4hep::Volume::visAttributes
VisAttr visAttributes() const
Access the visualisation attributes.
Definition: Volumes.cpp:1239
dd4hep::detail::GeoHandlerTypes::GeometryInfo::volumes
std::vector< Volume > volumes
Definition: GeoHandler.h:65
Printout.h
dd4hep::Volume::data
Object * data() const
Check if placement is properly instrumented.
Definition: Volumes.cpp:657
dd4hep::xml::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: XMLElements.cpp:1043