From fe994f87d31565b7508d5bd61c5ca59be5957687 Mon Sep 17 00:00:00 2001 From: Leonhard Reichenbach Date: Thu, 18 Jul 2024 16:44:00 +0200 Subject: [PATCH 1/2] add dd4hep-drawer util --- utils/dd4hep-drawer.py | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 utils/dd4hep-drawer.py diff --git a/utils/dd4hep-drawer.py b/utils/dd4hep-drawer.py new file mode 100644 index 000000000..71446d9a5 --- /dev/null +++ b/utils/dd4hep-drawer.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# coding: utf-8 + +from dd4hep import Detector +import graphviz +import re +from collections import Counter + +from argparse import ArgumentParser + +parser = ArgumentParser() + +parser.add_argument( + "--compactFile", help="DD4hep compact description xml", required=True +) +parser.add_argument( + "--maxDepth", + help="Maximum traversal depth of the detector tree", + default=10, + type=int, +) +parser.add_argument( + "--maxEdges", help="Maximum number of edges per connection", default=5, type=int +) +parser.add_argument( + "--drawList", + action="extend", + nargs="+", + metavar=("Vertex", "InnerTrackers"), + help="Names of the immediate children of the world element that should be drawn.", + default=[], +) + +args = parser.parse_args() + +theDetector = Detector.getInstance() +theDetector.fromXML(args.compactFile) + +# take part between last / and before the .xml +detector_name = args.compactFile.split("/")[-1].split(".")[0] + +dot = graphviz.Digraph("detector_name") + +# start = theDetector.detector("OpenDataTracker") +start = theDetector.world() + +dot.node(start.name(), start.name()) + + +def process_name(raw_name): + name = re.sub(r"\d+", "X", raw_name) + return name + + +edge_counter = Counter() + + +def add_children(detElement, depth=0, children=[]): + depth += 1 + if not children: + children = detElement.children() + for raw_name, child in children: + name = process_name(raw_name) + parent_name = process_name(detElement.name()) + dot.node(name, name) + if edge_counter[(parent_name, name)] < args.maxEdges: + dot.edge(parent_name, name) + edge_counter[(parent_name, name)] += 1 + if depth < args.maxDepth: + add_children(child, depth) + + +start_children = [(name, start.child(name)) for name in args.drawList] +add_children(start, children=start_children) + +dot.render(f"{detector_name}.gv") From 2399c37bf32ddacafdfd38dae70b048df1dce6a9 Mon Sep 17 00:00:00 2001 From: Leonhard Reichenbach Date: Mon, 22 Jul 2024 16:43:25 +0200 Subject: [PATCH 2/2] add start node --- utils/dd4hep-drawer.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/utils/dd4hep-drawer.py b/utils/dd4hep-drawer.py index 71446d9a5..796517708 100644 --- a/utils/dd4hep-drawer.py +++ b/utils/dd4hep-drawer.py @@ -27,9 +27,12 @@ action="extend", nargs="+", metavar=("Vertex", "InnerTrackers"), - help="Names of the immediate children of the world element that should be drawn.", + help="Names of the immediate children of the start element that should be drawn.", default=[], ) +parser.add_argument( + "--startNode", help="Start of the traversal", default="world" +) args = parser.parse_args() @@ -41,8 +44,8 @@ dot = graphviz.Digraph("detector_name") -# start = theDetector.detector("OpenDataTracker") -start = theDetector.world() +start = theDetector.detector(args.startNode) +# start = theDetector.world() dot.node(start.name(), start.name()) @@ -62,7 +65,10 @@ def add_children(detElement, depth=0, children=[]): for raw_name, child in children: name = process_name(raw_name) parent_name = process_name(detElement.name()) - dot.node(name, name) + sens = child.volume().isSensitive() + sens_lbl = "sensitive" if sens else "" + dot.node(name, name, {"xlabel": sens_lbl}) + # dot.node(name, name) if edge_counter[(parent_name, name)] < args.maxEdges: dot.edge(parent_name, name) edge_counter[(parent_name, name)] += 1 @@ -73,4 +79,4 @@ def add_children(detElement, depth=0, children=[]): start_children = [(name, start.child(name)) for name in args.drawList] add_children(start, children=start_children) -dot.render(f"{detector_name}.gv") +dot.render(f"{detector_name}_{args.startNode}_{'-'.join(args.drawList)}.gv")