4 from AthenaCommon.Utils.unixtools
import find_datafile
5 from AthenaCommon.Logging
import logging
9 utility function to retrieve the flag dictionary
10 for every TrackAnalysis from an input JSON file
14 input_file = flags.PhysVal.IDTPM.trkAnaCfgFile
17 if input_file ==
"Default":
22 if dataPath
is None and input_file !=
"Default":
23 raise Exception(f
"Input file with analyses definition: {input_file} could not be found, for files given with absolute path use ./ prefix")
29 with open( dataPath,
"r" )
as input_json_file:
30 analysesDictTmp = json.load( input_json_file )
34 for trkAnaName, trkAnaDict
in analysesDictTmp.items():
36 analysesDict.update( { trkAnaName : trkAnaDict } )
38 analysesDict[trkAnaName][
"anaTag"] =
"_" + trkAnaName
40 if (
"SubFolder" not in analysesDict[trkAnaName].
keys()
or
41 analysesDict[trkAnaName][
"SubFolder"] ==
"" ) :
42 analysesDict[trkAnaName][
"SubFolder"] = trkAnaName
43 analysesDict[trkAnaName][
"SubFolder"] +=
"/"
45 if "ChainNames" in analysesDict[trkAnaName]:
46 fullChainList =
getChainList( flags, analysesDict[trkAnaName][
"ChainNames"] )
47 analysesDict[trkAnaName][
"ChainNames"] = fullChainList
49 return unpackTrkAnaDicts( analysesDict )
if flags.PhysVal.IDTPM.unpackTrigChains
else analysesDict
54 utility function to define a separate TrackAnalysis
55 for each configured trigger chain
57 if not analysesDictIn :
return analysesDictIn
60 for trkAnaName, trkAnaDict
in analysesDictIn.items():
61 if "ChainNames" in trkAnaDict:
62 chainList = trkAnaDict[
"ChainNames"]
63 for chain
in chainList:
64 trkAnaName_new = trkAnaName +
"_" + chain
65 trkAnaDict_new = dict( trkAnaDict )
66 trkAnaDict_new[
"anaTag"] = trkAnaDict[
"anaTag"] +
"_" + chain
67 trkAnaDict_new[
"ChainNames"] = [ chain ]
68 analysesDictOut.update( { trkAnaName_new : trkAnaDict_new } )
70 analysesDictOut.update( { trkAnaName : trkAnaDict } )
72 return analysesDictOut
78 utility function to retrieve full list of
79 configured trigger chains matching
80 the passed regex list of chains
82 if not regexChainList:
return regexChainList
84 if not flags.locked():
85 flags_tmp = flags.clone()
89 from TrigConfigSvc.TriggerConfigAccess
import getHLTMenuAccess
94 for regexChain
in regexChainList:
95 for item
in chainsMenu:
96 chains = re.findall( regexChain, item )
98 if chain
is not None and chain == item:
99 configChains.append( chain )
106 Open json files and load all merged contents
107 in a dictionary, which is later converted to a
108 list of strings, each to be parsed in a
109 (flattened) json format
111 log = logging.getLogger(
"getPlotsDefList" )
114 log.debug(
"plotsDefFileList : %s", flags.PhysVal.IDTPM.plotsDefFileList )
115 listPath =
find_datafile( flags.PhysVal.IDTPM.plotsDefFileList )
117 log.error(
"plotsDefFileList not found" )
120 plotsDefFileNames = []
121 with open( listPath,
"r" )
as input_flist :
122 plotsDefFileNames = input_flist.read().splitlines()
127 for plotsDefFileName
in plotsDefFileNames :
129 log.debug(
"Reading input plots definitions : %s", dataPath )
131 log.error(
"plotsDefFile %s not found", plotsDefFileName )
134 with open( dataPath,
"r" )
as input_json_file :
135 plotsDefDict.update( json.load( input_json_file ) )
143 def flatten_json( y ) :
145 def flatten(x, name=''):
148 flatten(x[a], name + a +
'_')
154 plotsDefStrList_v1 = []
155 for plotName, plotDict
in plotsDefDict.items():
156 newPlotDict = plotDict.copy()
157 newPlotDict[
"name" ] = plotName
160 plotDictFlat = flatten_json( newPlotDict )
163 plotDefStr =
str( json.dumps( plotDictFlat ) )
166 plotsDefStrList_v1.append( plotDefStr )
170 plotsCommonValuesFileName = flags.PhysVal.IDTPM.plotsCommonValuesFile
171 if not plotsCommonValuesFileName :
172 if flags.Detector.GeometryID :
173 plotsCommonValuesFileName =
"InDetTrackPerfMon/PlotsDefCommonValues.json"
174 elif flags.Detector.GeometryITk :
175 plotsCommonValuesFileName =
"InDetTrackPerfMon/PlotsDefCommonValues_ITk.json"
177 log.error(
"Could not get detector geometry for plotsCommonValuesFile" )
180 commonValuesPath =
find_datafile( plotsCommonValuesFileName )
181 if commonValuesPath
is None :
182 log.error(
"plotsCommonValuesFile not found: %s", plotsCommonValuesFileName )
185 commonValuesDict = {}
186 with open( commonValuesPath,
"r" )
as input_commonValues :
187 commonValuesDict.update( json.load( input_commonValues ) )
189 plotsDefStrList_v2 = []
190 for plotDefStr
in plotsDefStrList_v1 :
191 newPlotDefStr = plotDefStr
192 if commonValuesDict :
193 for key, value
in commonValuesDict.items() :
194 plotDefStr_tmp = newPlotDefStr.replace(
"$"+key, value[0] )
195 newPlotDefStr = plotDefStr_tmp
196 plotsDefStrList_v2.append( newPlotDefStr )
199 testLabel =
getLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.TestType )
200 refLabel =
getLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.RefType )
201 trkLabels = [ testLabel, refLabel ]
203 if flags.PhysVal.IDTPM.currentTrkAna.MatchingType ==
"EFTruthMatch":
204 trkLabels.append(
getLabel( flags,
"Truth" ) )
208 for plotsDefStr
in plotsDefStrList_v2 :
209 plotsDefStr = plotsDefStr.replace(
"$TESTTYPE", testLabel[1] ).
replace(
"$TESTTAG", testLabel[0] )
210 plotsDefStr = plotsDefStr.replace(
"$REFTYPE", refLabel[1] ).
replace(
"$REFTAG", refLabel[0] )
211 if (
"$TRKTAG" not in plotsDefStr )
and (
"$TRKTYPE" not in plotsDefStr ) :
212 plotsDefStrList.append( plotsDefStr )
214 for trkLabel
in trkLabels :
215 newPlotsDefStr = plotsDefStr.replace(
"$TRKTYPE", trkLabel[1] ).
replace(
"$TRKTAG", trkLabel[0] )
216 if (
"$TRK2TAG" not in newPlotsDefStr )
and (
"$TRK2TYPE" not in newPlotsDefStr ) :
217 plotsDefStrList.append( newPlotsDefStr )
219 for trk2Label
in trkLabels :
220 newPlotsDefStr2 = newPlotsDefStr.replace(
"$TRK2TYPE", trk2Label[1] ).
replace(
"$TRK2TAG", trk2Label[0] )
221 plotsDefStrList.append( newPlotsDefStr2 )
224 plotsDefStrList_v2 = []
225 for plotsDefStr
in plotsDefStrList :
226 plotsDefStr = plotsDefStr.replace(
"$TESTVTXTYPE", testLabel[2] ).
replace(
"$TESTVTXTAG", testLabel[0] )
227 plotsDefStr = plotsDefStr.replace(
"$REFVTXTYPE", refLabel[2] ).
replace(
"$REFVTXTAG", refLabel[0] )
228 if (
"$VTXTAG" not in plotsDefStr )
and (
"$VTXTYPE" not in plotsDefStr ) :
229 plotsDefStrList_v2.append( plotsDefStr )
231 for trkLabel
in trkLabels :
232 newPlotsDefStr = plotsDefStr.replace(
"$VTXTYPE", trkLabel[2] ).
replace(
"$VTXTAG", trkLabel[0] )
233 if (
"$VTX2TAG" not in newPlotsDefStr )
and (
"$VTX2TYPE" not in newPlotsDefStr ) :
234 plotsDefStrList_v2.append( newPlotsDefStr )
236 for trk2Label
in trkLabels :
237 newPlotsDefStr2 = newPlotsDefStr.replace(
"$VTX2TYPE", trk2Label[2] ).
replace(
"$VTX2TAG", trk2Label[0] )
238 plotsDefStrList_v2.append( newPlotsDefStr2 )
240 return plotsDefStrList_v2
244 if key ==
"Offline" and flags.PhysVal.IDTPM.currentTrkAna.SelectOfflineObject:
245 if "Truth" not in flags.PhysVal.IDTPM.currentTrkAna.SelectOfflineObject:
246 key += flags.PhysVal.IDTPM.currentTrkAna.SelectOfflineObject
249 "EFTrigger" : [
"eftrig",
"EF Trigger track",
"EF Trigger vertex" ],
250 "Trigger" : [
"trig",
"Trigger track",
"Trigger vertex" ],
251 "Offline" : [
"offl",
"Offline track",
"Offline vertex" ],
252 "OfflineElectron" : [
"offEle",
"Offline e^{#pm} track",
"Offline e^{#pm} vertex" ],
253 "OfflineMuon" : [
"offMu",
"Offline #mu^{#pm} track",
"Offline #mu^{#pm} vertex" ],
254 "OfflineTau" : [
"offTau",
"Offline #tau^{#pm} track",
"Offline #tau^{#pm} vertex" ],
255 "Truth" : [
"truth",
"Truth particle",
"Truth vertex" ],
256 "TruthElectron" : [
"truthEle",
"Truth e^{#pm}",
"Truth e^{#pm} vertex" ],
257 "TruthMuon" : [
"truthMu",
"Truth #mu^{#pm}",
"Truth #mu^{#pm} vertex" ],
258 "TruthTau" : [
"truthTau",
"Truth #tau^{#pm}",
"Truth #tau^{#pm} vertex" ]
260 return trkLabelsDict[ key ]
270 outDict = myPlotsDefDict.copy()
272 items = [
"res",
"pull" ]
273 plist = [
"mean",
"width" ]
276 "helper" : {
"name" :
"resHelper",
"yTitle" :
"residual" },
277 "mean" : {
"name" :
"resmean",
"yTitle" :
"bias" },
278 "width" : {
"name" :
"resolution",
"yTitle" :
"resolution" }
281 "helper" : {
"name" :
"pullHelper",
"yTitle" :
"pull" },
282 "mean" : {
"name" :
"pullmean",
"yTitle" :
"pull mean" },
283 "width" : {
"name" :
"pullwidth",
"yTitle" :
"pull width" }
287 for plotName, plotDict
in myPlotsDefDict.items() :
290 if iDict[i][
"helper"][
"name"]
in plotName :
294 pName = plotName.replace( iDict[i][
"helper"][
"name"], iDict[i][p][
"name"] )
297 if pName
in outDict :
299 pDict = outDict[ pName ]
304 yTitle = plotDict[
"yAxis"][
"title"]
305 yTitle = yTitle.replace( iDict[i][
"helper"][
"yTitle"], iDict[i][p][
"yTitle"] )
306 pDict.update( {
"yAxis" : {
"title" : yTitle } } )
311 "xAxis" : plotDict[
"xAxis"]
315 outDict.update( { pName : pDict } )