8 @brief Helper functions for CostAnalysisPostProcessing script
13 from TrigCostAnalysis.CostMetadataUtil
import createOverflowSummary
14 from AthenaCommon.Logging
import logging
15 log = logging.getLogger(
'CostAnalysisPostProcessing')
18 def exploreTree(inputFile, dumpSummary=False, underflowThreshold=0.1, overflowThreshold=0.1, maxRanges=5, skipRanges=-1):
19 ''' @brief Explore ROOT Tree to find tables with histograms to be saved in csv
21 Per each found directory TableConstructor object is created.
22 Expected directory tree:
36 @param[in] inputFile ROOT.TFile object with histograms
39 processingWarnings = []
41 rangesToSkip = skipRanges
42 for timeRange
in inputFile.GetListOfKeys():
43 if timeRange.GetName() !=
"metadata" and rangesToSkip > 0:
45 log.debug(
"Skipping range {0}".
format(timeRange.GetName()))
48 if maxRanges > 0
and rangeCounter >= maxRanges:
49 log.info(
"{0} ranges were processed - exiting the postprocessing".
format(rangeCounter))
52 rangeObj = timeRange.ReadObj()
53 if not rangeObj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
continue
55 walltime =
getWalltime(inputFile, timeRange.GetName())
57 for table
in rangeObj.GetListOfKeys():
58 tableObj = table.ReadObj()
59 if not tableObj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
continue
60 log.info(
"Processing Table %s", table.GetName())
63 className = table.GetName() +
"_TableConstructor"
64 exec(
"from TrigCostAnalysis." + className +
" import " + className)
65 t = eval(className +
"(tableObj, underflowThreshold, overflowThreshold)")
67 if table.GetName() ==
"Chain_HLT" or table.GetName() ==
"Chain_Algorithm_HLT":
70 if table.GetName() ==
"Global_HLT":
73 if table.GetName() ==
"Algorithm_HLT":
74 t.dumpSummary = dumpSummary
76 fileName =
getFileName(table.GetName(), timeRange.GetName())
79 t.fillTable(histPrefix)
80 t.normalizeColumns(walltime)
81 t.saveToFile(fileName)
83 processingWarnings += t.getWarningMsgs()
86 log.error(
"Processing of table {0} failed!".
format(table.GetName()))
88 except (NameError, ImportError):
89 log.warning(
"Class {0} not defined - directory {1} will not be processed"
90 .
format(table.GetName()+
"_TableConstructor", table.GetName()))
93 log.debug(
"Range {0} was processed".
format(timeRange.GetName()))
97 summary[
"Summary"] += [
"Underflow threshold: {0}".
format(underflowThreshold),
"Overflow threshold: {0}".
format(overflowThreshold)]
98 return processingWarnings + [summary]
102 ''' @brief Extract walltime value from histogram
104 @param[in] inputFile ROOT TFile to look for histogram
105 @param[in] rootName Name of the root directory to search for tables
107 @return walltime value if found else 0 and an error
110 dirObj = inputFile.Get(rootName)
111 if not dirObj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
return 0
112 for hist
in dirObj.GetListOfKeys():
113 if '_walltime' in hist.GetName():
114 histObj = hist.ReadObj()
115 if histObj.IsA().InheritsFrom(ROOT.TProfile.Class()):
116 return histObj.Integral()
118 return histObj.GetBinContent(1)
120 log.error(
"Walltime not found")
125 ''' @brief Extract total time [s] of algorithms from histogram
127 @param[in] inputFile ROOT TFile to look for histogram
128 @param[in] rootName Name of the root directory to search for tables
130 @return total execution time [s] value if found else 0
134 alg = inputFile.Get(rootName).
Get(
"Global_HLT").
Get(
"Total")
135 hist = alg.Get(rootName +
"_Global_HLT_Total_AlgTime_perEvent")
136 for i
in range(1, hist.GetXaxis().GetNbins()):
137 totalTime += hist.GetBinContent(i) * hist.GetXaxis().GetBinCenterLog(i)
139 return totalTime * 1e-3
143 ''' @brief Save entry number in scientific notation'''
144 if type(entry)
is float
or type(entry)
is int:
148 elif fabs(entry) > 10000
or fabs(entry) < 0.0001:
149 return "{:.4e}".
format(entry)
150 elif int(entry) == entry:
154 return "{:.4}".
format(entry)
160 '''@brief Get name of file to save the table
162 @param[in] tableName Table name
163 @param[in] rootName Name of table's root directory
165 @return Filename for given table
167 return "Table_" + tableName +
"_" + rootName +
".csv"
171 '''@brief Construct full histogram name
173 @param[in] tableName Table name
174 @param[in] rootName Name of table's root directory
176 @return Histogram prefix for given table
179 return rootName +
'_' + tableName +
'_'