113def getPlotsDefList( flags ):
114 '''
115 Open json files and load all merged contents
116 in a dictionary, which is later converted to a
117 list of strings, each to be parsed in a
118 (flattened) json format
119 '''
120 log = logging.getLogger( "getPlotsDefList" )
121
122
123 log.debug( "plotsDefFileList : %s", flags.PhysVal.IDTPM.plotsDefFileList )
124 listPath = custom_find_datafile( flags.PhysVal.IDTPM.plotsDefFileList )
125 if listPath is None:
126 log.error( "plotsDefFileList not found" )
127 return None
128
129 plotsDefFileNames = []
130 with open( listPath, "r" ) as input_flist :
131 plotsDefFileNames = input_flist.read().splitlines()
132
133
134 plotsDefDict = {}
135
136 for plotsDefFileName in plotsDefFileNames :
137 dataPath = custom_find_datafile( plotsDefFileName )
138 log.debug( "Reading input plots definitions : %s", dataPath )
139 if dataPath is None:
140 log.error( "plotsDefFile %s not found", plotsDefFileName )
141 return None
142
143 with open( dataPath, "r" ) as input_json_file :
144 plotsDefDict.update( json.load( input_json_file ) )
145
146
147
148 plotsDefDict = updateResolutionPlots( plotsDefDict )
149
150
151
152 def flatten_json( y ) :
153 out = {}
154 def flatten(x, name=''):
156 for a in x:
157 flatten(x[a], name + a + '_')
158 else:
159 out[name[:-1]] = x
160 flatten(y)
161 return out
162
163 plotsDefStrList_v1 = []
164 for plotName, plotDict in plotsDefDict.items():
165 newPlotDict = plotDict.copy()
166 newPlotDict[ "name" ] = plotName
167
168
169 plotDictFlat = flatten_json( newPlotDict )
170
171
172 plotDefStr = str( json.dumps( plotDictFlat ) )
173
174
175 plotsDefStrList_v1.append( plotDefStr )
176
177
178
179 plotsCommonValuesFileName = flags.PhysVal.IDTPM.plotsCommonValuesFile
180 if not plotsCommonValuesFileName :
181 if flags.Detector.GeometryID :
182 plotsCommonValuesFileName = "InDetTrackPerfMon/PlotsDefCommonValues.json"
183 elif flags.Detector.GeometryITk :
184 plotsCommonValuesFileName = "InDetTrackPerfMon/PlotsDefCommonValues_ITk.json"
185 else :
186 log.error( "Could not get detector geometry for plotsCommonValuesFile" )
187 return None
188
189 commonValuesPath = custom_find_datafile( plotsCommonValuesFileName )
190 if commonValuesPath is None :
191 log.error( "plotsCommonValuesFile not found: %s", plotsCommonValuesFileName )
192 return None
193
194 commonValuesDict = {}
195 with open( commonValuesPath, "r" ) as input_commonValues :
196 commonValuesDict.update( json.load( input_commonValues ) )
197
198 plotsDefStrList_v2 = []
199 for plotDefStr in plotsDefStrList_v1 :
200 newPlotDefStr = plotDefStr
201 if commonValuesDict :
202 for key, value in commonValuesDict.items() :
203 plotDefStr_tmp = newPlotDefStr.replace( "$"+key, value[0] )
204 newPlotDefStr = plotDefStr_tmp
205 plotsDefStrList_v2.append( newPlotDefStr )
206
207
208 testLabel = getLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.TestType )
209 refLabel = getLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.RefType )
210 trkLabels = [ testLabel, refLabel ]
211
212 testAllLabel = getAllTruthLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.TestType )
213 refAllLabel = getAllTruthLabel( flags, flags.PhysVal.IDTPM.currentTrkAna.RefType )
214
215
216 if flags.PhysVal.IDTPM.currentTrkAna.MatchingType == "EFTruthMatch":
217 trkLabels.append( getLabel( flags, "Truth" ) )
218
219
220 plotsDefStrList = []
221 for plotsDefStr in plotsDefStrList_v2 :
222 plotsDefStr = plotsDefStr.replace(
"$TESTALL", testAllLabel ).
replace(
"$REFALL", refAllLabel )
223 plotsDefStr = plotsDefStr.replace(
"$TESTTYPE", testLabel[1] ).
replace(
"$TESTTAG", testLabel[0] )
224 plotsDefStr = plotsDefStr.replace(
"$REFTYPE", refLabel[1] ).
replace(
"$REFTAG", refLabel[0] )
225 if ( "$TRKTAG" not in plotsDefStr ) and ( "$TRKTYPE" not in plotsDefStr ) :
226 plotsDefStrList.append( plotsDefStr )
227 continue
228 for trkLabel in trkLabels :
229 newPlotsDefStr = plotsDefStr.replace(
"$TRKTYPE", trkLabel[1] ).
replace(
"$TRKTAG", trkLabel[0] )
230 if ( "$TRK2TAG" not in newPlotsDefStr ) and ( "$TRK2TYPE" not in newPlotsDefStr ) :
231 plotsDefStrList.append( newPlotsDefStr )
232 continue
233 for trk2Label in trkLabels :
234 newPlotsDefStr2 = newPlotsDefStr.replace(
"$TRK2TYPE", trk2Label[1] ).
replace(
"$TRK2TAG", trk2Label[0] )
235 plotsDefStrList.append( newPlotsDefStr2 )
236
237
238 plotsDefStrList_v2 = []
239 for plotsDefStr in plotsDefStrList :
240 plotsDefStr = plotsDefStr.replace(
"$TESTVTXTYPE", testLabel[2] ).
replace(
"$TESTVTXTAG", testLabel[0] )
241 plotsDefStr = plotsDefStr.replace(
"$REFVTXTYPE", refLabel[2] ).
replace(
"$REFVTXTAG", refLabel[0] )
242 if ( "$VTXTAG" not in plotsDefStr ) and ( "$VTXTYPE" not in plotsDefStr ) :
243 plotsDefStrList_v2.append( plotsDefStr )
244 continue
245 for trkLabel in trkLabels :
246 newPlotsDefStr = plotsDefStr.replace(
"$VTXTYPE", trkLabel[2] ).
replace(
"$VTXTAG", trkLabel[0] )
247 if ( "$VTX2TAG" not in newPlotsDefStr ) and ( "$VTX2TYPE" not in newPlotsDefStr ) :
248 plotsDefStrList_v2.append( newPlotsDefStr )
249 continue
250 for trk2Label in trkLabels :
251 newPlotsDefStr2 = newPlotsDefStr.replace(
"$VTX2TYPE", trk2Label[2] ).
replace(
"$VTX2TAG", trk2Label[0] )
252 plotsDefStrList_v2.append( newPlotsDefStr2 )
253
254 return plotsDefStrList_v2
255
256
std::string replace(std::string s, const std::string &s2, const std::string &s3)