File indexing completed on 2023-03-17 11:25:50
0001
0002
0003 from __future__ import print_function
0004 import argparse
0005 import os, sys
0006 import pprint
0007 from shutil import copy2
0008 import xml.etree.ElementTree as ET
0009
0010 TAG_PREFIX='{http://www.cern.ch/cms/DDL}'
0011 CMSSW_NOT_SET=1
0012 TRACKER_MATERIAL_FILE_MISSING=2
0013
0014 HEADER = """
0015 #ifndef SIMTRACKER_TRACKERMATERIALANALYSIS_LISTGROUPS_MATERIALDIFFERENCE_H
0016 #define SIMTRACKER_TRACKERMATERIALANALYSIS_LISTGROUPS_MATERIALDIFFERENCE_H
0017
0018 void ListGroups::fillMaterialDifferences() {
0019 """
0020
0021 TRAILER = """
0022 }
0023
0024 #endif // SIMTRACKER_TRACKERMATERIALANALYSIS_LISTGROUPS_MATERIALDIFFERENCE_H
0025 """
0026
0027 def checkEnvironment():
0028 if not 'CMSSW_RELEASE_BASE' in os.environ.keys():
0029 print('CMSSW Environments not setup, quitting\n')
0030 sys.exit(CMSSW_NOT_SET)
0031
0032 def getTrackerRecoMaterialCopy(filename):
0033
0034 tracker_reco_material = os.path.join(os.environ['CMSSW_BASE'],
0035 'src/Geometry/TrackerRecoData/data/PhaseII/TiltedTracker613/trackerRecoMaterial.xml')
0036 if not os.path.exists(tracker_reco_material):
0037 tracker_reco_material = os.path.join(os.environ['CMSSW_RELEASE_BASE'],
0038 'src/Geometry/TrackerRecoData/data/PhaseII/TiltedTracker613/trackerRecoMaterial.xml')
0039 if not os.path.exists(tracker_reco_material):
0040 print('Something is wrong with the CMSSW installation. The file %s is missing. Quitting.\n' % tracker_reco_material)
0041 sys.exit(TRACKER_MATERIAL_FILE_MISSING)
0042 copy2(tracker_reco_material, filename)
0043
0044 def produceXMLFromParameterFile():
0045 """
0046 Starting from the file parameters.xml produced by the
0047 TrackingMaterialAnalyser via cmsRun, it writes out a new XML,
0048 taking into account the proper names and grouping of detectors
0049 together.
0050
0051 The skeleton of the XML is taken directly from the release the
0052 user is currently using, i.e. for 2026D41 from
0053 $CMSSW_RELEASE_BASE/src/Geometry/TrackerRecoData/data/PhaseII/TiltedTracker613/trackerRecoMaterial.xml.
0054
0055 A new file, named trackerRecoMaterial.xml, is saved in the
0056 current directory.
0057 """
0058
0059 tracker_reco_material = './trackerRecoMaterialFromRelease.xml'
0060 tracker_reco_material_updated = './parameters.xml'
0061 ET.register_namespace('', "http://www.cern.ch/cms/DDL")
0062 tree = ET.parse(tracker_reco_material)
0063 root = tree.getroot()
0064 tree_updated = ET.parse(tracker_reco_material_updated)
0065 root_updated = tree_updated.getroot()
0066 sections = root.getchildren()
0067 for child in sections[0]:
0068 print(child.attrib['name'])
0069
0070 for spec_par in root.iter('%sSpecPar' % TAG_PREFIX):
0071 current_detector = spec_par.attrib['name']
0072 for parameter in spec_par.iter('%sParameter' % TAG_PREFIX):
0073 print(current_detector, parameter.attrib['name'], parameter.attrib['value'])
0074 updated_current_detector_node = root_updated.find(".//Group[@name='%s']" % current_detector)
0075 if updated_current_detector_node:
0076 for child in updated_current_detector_node:
0077 if child.attrib['name'] == parameter.attrib['name']:
0078 parameter.set('name', child.attrib['name'])
0079 parameter.set('value', child.attrib['value'])
0080 print(current_detector, parameter.attrib['name'], parameter.attrib['value'])
0081 else:
0082 print("Missing group: %s" % current_detector)
0083 tree.write('trackerRecoMaterial.xml', encoding='UTF-8', xml_declaration=True)
0084
0085 def compareNewXMLWithOld(format_for_twiki):
0086 """
0087 Computes the difference between the old values, stored in the
0088 central repository for the current release, i.e. for 2026D41 from
0089 $CMSSW_RELEASE_BASE/src/Geometry/TrackerRecoData/data/PhaseII/TiltedTracker613/trackerRecoMaterial.xml,
0090 and the new values that we assume are present in the same file
0091 under the locally installed release, i.e. for 2026D41 under
0092 $CMSSW_BASE/src/Geometry/TrackerRecoData/data/PhaseII/TiltedTracker613/trackerRecoMaterial.xml. No
0093 check is performed to guarantee that the files are already
0094 there. If the file is not there, it is searched in the current
0095 folder. A missing file will result in an exception.
0096
0097 The output of this function is a formatted structured as:
0098 ComponentsName KindOfParameter OldValue NewValue Difference
0099 where the Difference is computed as (NewValue-OldValue)/OldValue.
0100
0101 Results are flushed at the terminal, nothing is saved.
0102 """
0103
0104 tracker_reco_material = './trackerRecoMaterialFromRelease.xml'
0105 tracker_reco_material_updated = os.path.join(os.environ['CMSSW_BASE'],
0106 'src/SimTracker/TrackerMaterialAnalysis/test/trackerRecoMaterial.xml')
0107 if not os.path.exists(tracker_reco_material_updated):
0108 tracker_reco_material_updated = './trackerRecoMaterial.xml'
0109 if not os.path.exists(tracker_reco_material_updated):
0110 raise os.error('Missing trackerRecoMaterial.xml file.')
0111 ET.register_namespace('', "http://www.cern.ch/cms/DDL")
0112 tree = ET.parse(tracker_reco_material)
0113 root = tree.getroot()
0114 tree_updated = ET.parse(tracker_reco_material_updated)
0115 root_updated = tree_updated.getroot()
0116 sections = root.getchildren()
0117
0118 header = open(os.path.join(os.environ['CMSSW_BASE'],
0119 'src/SimTracker/TrackerMaterialAnalysis/plugins/ListGroupsMaterialDifference.h'), 'w')
0120 header.write(HEADER)
0121 differences = {}
0122 values = {}
0123 ordered_keys = []
0124 for spec_par in root.iter('%sSpecPar' % TAG_PREFIX):
0125 current_detector = spec_par.attrib['name']
0126 ordered_keys.append(current_detector)
0127 for parameter in spec_par.iter('%sParameter' % TAG_PREFIX):
0128 updated_current_detector_node = root_updated.find(".//%sSpecPar[@name='%s']" % (TAG_PREFIX,current_detector))
0129 if updated_current_detector_node:
0130 for child in updated_current_detector_node:
0131 name = child.get('name', None)
0132 if name and name == parameter.attrib['name']:
0133 differences.setdefault(current_detector, {}).setdefault(name, [float(parameter.attrib['value']),
0134 float(child.attrib['value']),
0135 ((float(child.attrib['value'])-float(parameter.attrib['value']))
0136 /float(parameter.attrib['value'])*100.)]
0137 )
0138 else:
0139 print('Element not found: %s' % current_detector)
0140 for group in differences.keys():
0141 header.write(' m_diff["%s"] = std::make_pair<float, float>(%f, %f);\n' % (group,
0142 differences[group]['TrackerRadLength'][2],
0143 differences[group]['TrackerXi'][2]))
0144 for group in differences.keys():
0145 header.write(' m_values["%s"] = std::make_pair<float, float>(%f, %f);\n' % (group,
0146 differences[group]['TrackerRadLength'][1],
0147 differences[group]['TrackerXi'][1]))
0148
0149 for i in xrange(len(ordered_keys)):
0150 key = ordered_keys[i]
0151 if format_for_twiki:
0152 print("| %s | %f | %f | %f%% | %f | %f | %f%% |" % (key,
0153 differences[key]['TrackerRadLength'][0],
0154 differences[key]['TrackerRadLength'][1],
0155 differences[key]['TrackerRadLength'][2],
0156 differences[key]['TrackerXi'][0],
0157 differences[key]['TrackerXi'][1],
0158 differences[key]['TrackerXi'][2]
0159 ))
0160 else:
0161 print("%s %f %f %f%% %f %f %f%%" % (key,
0162 differences[key]['TrackerRadLength'][0],
0163 differences[key]['TrackerRadLength'][1],
0164 differences[key]['TrackerRadLength'][2],
0165 differences[key]['TrackerXi'][0],
0166 differences[key]['TrackerXi'][1],
0167 differences[key]['TrackerXi'][2]
0168 ))
0169 header.write(TRAILER)
0170 header.close
0171
0172
0173 if __name__ == '__main__':
0174 parser = argparse.ArgumentParser(description='Easily manipulate and inspect XML files related to Tracking Material.')
0175 parser.add_argument('-p', '--produce', action='store_true',
0176 default=False,
0177 help='Produce a trackerRecoMaterial.xml starting from the paramters.xml file produced by the trackingMaterialProducer.')
0178 parser.add_argument('-c', '--compare', action='store_true',
0179 default=False,
0180 help='Compares a local trackerRecoMaterial.xml against the one bundled with the release.')
0181 parser.add_argument('-w', '--twiki', action='store_true',
0182 default=False,
0183 help="""Compares a local trackerRecoMaterial.xml against the one bundled
0184 with the release and produces and output that is Twiki compatible
0185 to be put into a table.""")
0186 args = parser.parse_args()
0187 checkEnvironment()
0188 getTrackerRecoMaterialCopy('trackerRecoMaterialFromRelease.xml')
0189 if args.produce:
0190 produceXMLFromParameterFile()
0191 if args.compare or args.twiki:
0192 compareNewXMLWithOld(args.twiki)