xml_writer.py 4.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
###############################################################################
# (c) Copyright 2000-2020 CERN for the benefit of the LHCb Collaboration      #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################
import xml.etree.ElementTree as ET
from xml.dom import minidom
from collections import OrderedDict
from Gaudi.Configuration import log
import tempfile
import os


# Combine the includes into a valid LHCb DD4hep xml and return name of temporary
# file
def create_xml(includesfiles):
    unique_includes = list(OrderedDict.fromkeys(includesfiles))
23
    detectorroot = os.path.join(os.environ['DETECTOR_PROJECT_ROOT'], 'compact/trunk/')
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

    def addroot(p):
        if p[0] != '.' and p[0] != '/':
            return detectorroot + p
        return p

    root = ET.Element("lccdd")
    ET.SubElement(root, "std_conditions", type="STP")
    debug = ET.SubElement(root, "debug")
    ET.SubElement(debug, "type", name="includes", value="0")
    ET.SubElement(debug, "type", name="materials", value="0")
    includes = ET.SubElement(root, "includes")
    ET.SubElement(includes, "gdmlFile", ref=addroot("defaults.xml"))
    define = ET.SubElement(root, "define")
    ET.SubElement(define, "constant", name="Bls:parent",
                  value="/world/BeforeMagnetRegion/BeforeVelo", type="string")
    ET.SubElement(define, "constant", name="BcmUp:parent",
                  value="/world/BeforeMagnetRegion/BeforeVelo", type="string")
    ET.SubElement(define, "constant", name="BcmDown:parent",
                  value="/world/DownstreamRegion/AfterMuon", type="string")
    ET.SubElement(define, "constant", name="GValve:parent",
                  value="/world/BeforeMagnetRegion/BeforeVelo", type="string")
    ET.SubElement(define, "constant", name="MBXWUp:parent",
                  value="/world/UpstreamRegion", type="string")
    ET.SubElement(define, "constant", name="Cavern:parent",
                  value="/world/Infrastructure", type="string")
    ET.SubElement(define, "constant", name="Tunnel:parent",
                  value="/world/Infrastructure", type="string")
    ET.SubElement(define, "constant", name="Bunker:parent",
                  value="/world/Infrastructure", type="string")
    ET.SubElement(define, "constant", name="VP:parent",
                  value="/world/BeforeMagnetRegion", type="string")
    ET.SubElement(define, "constant", name="UT:parent",
                  value="/world/BeforeMagnetRegion", type="string")
    ET.SubElement(define, "constant", name="FT:parent",
                  value="/world/AfterMagnetRegion/T", type="string")
60
61
    ET.SubElement(define, "constant", name="Magnet:parent",
                  value="/world/MagnetRegion", type="string")
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    ET.SubElement(define, "constant", name="Magnet:ignore", value="0")
    ET.SubElement(define, "constant", name="UpstreamRegion:ignore", value="0")
    ET.SubElement(define, "constant", name="BeforeMagnetRegion:ignore", value="0")  # NOQA
    ET.SubElement(define, "constant", name="MagnetRegion:ignore", value="0")
    ET.SubElement(define, "constant", name="AfterMagnetRegion:ignore", value="0")  # NOQA
    ET.SubElement(define, "constant", name="DownstreamRegion:ignore", value="0")

    ET.SubElement(root, "include", ref=addroot("global/conditions.xml"))

    # FIXME: Regions need something from this file
    ET.SubElement(root, "include", ref=addroot("T/parameters.xml"))
    ET.SubElement(root, "include", ref=addroot("Regions/detector.xml"))

    for inc in unique_includes:
        ET.SubElement(root, "include", ref=addroot(inc))

    _, tmpfile = tempfile.mkstemp('.xml')
    xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="   ")
    with open(tmpfile, "w") as f:
        f.write(xmlstr)
    log.info('Wrote xml file to {}'.format(tmpfile))
83
    return tempfile.tempdir, tmpfile.replace(tempfile.tempdir + '/', '')