4 Helper functions for CherryPy application ``browse_db.py``.
6 Author: Albertas Gimbutas, Vilnius University (LT)
7 e-mail: albertasgim@gmail.com
12 from os
import getcwd, listdir
13 from os.path
import join
14 from urllib
import quote
18 'MessageLogger':
'Miscellanea',
'FourVector':
'Generic',
19 'Castor':
'Castor Calorimeter',
'RPCDigisV':
'Resistive Plate Chambers',
20 'GlobalRecHitsV':
'Miscellanea: Sim.',
'Top':
'Top',
'HLTJETMET':
'JetMet',
21 'GlobalDigisV':
'Miscellanea: Sim.',
'L1TEMU':
'Level 1 Trigger',
22 'TrackerRecHitsV':
'Tracking System',
'MuonDTHitsV':
'Muon Objects',
23 'EcalDigisV':
'Ecal Calorimeter',
'EcalHitsV':
'Ecal Calorimeter',
24 'Muons':
'Muon Objects',
'DT':
'Drift Tubes',
'TrackerDigisV':
'Tracking System',
25 'Pixel':
'Tracking System',
'EcalPreshower':
'Ecal Calorimeter',
26 'EgammaV':
'Photons',
'AlCaEcalPi0':
'Alca',
'SusyExo':
'SusyExo',
27 'MuonDTDigisV':
'Muon Objects',
'TauRelVal':
'Tau',
28 'HcalHitsV':
'Hcal Calorimeter',
'RPC':
'Resistive Plate Chambers',
29 'EcalRecHitsV':
'Ecal Calorimeter',
'EgOffline':
'EGamma',
30 'MuonCSCDigisV':
'Muon Objects',
'ParticleFlow':
'Miscellanea',
31 'Info':
'Miscellanea',
'Tracking':
'Tracking',
32 'NoiseRatesV':
'Miscellanea: Sim.',
'Generator':
'Miscellanea: Sim.',
33 'Btag':
'B Tagging',
'Higgs':
'Higgs',
'GlobalHitsV':
'Miscellanea: Sim.',
34 'HcalRecHitsV':
'Hcal Calorimeter',
'TrackerHitsV':
'Tracking System',
35 'CSC':
'Cathode Strip Chambers',
'Muon,HLTMonMuon':
'Muon',
36 'Hcal':
'Hcal Calorimeter',
'TauOffline':
'Tau',
37 'HeavyFlavor':
'HeavyFlavor',
'JetMET':
'Jet',
'Physics':
'Miscellanea',
38 'CaloTowersV':
'Hcal Calorimeter',
'SiStrip':
'Tracking System',
39 'EcalClusterV':
'Ecal Calorimeter',
'HLTEgammaValidation':
'EGamma',
40 'EcalPhiSym':
'Alca',
'L1T':
'Level 1 Trigger',
'MixingV':
'Miscellanea: Sim.',
41 'FourVector_Val':
'Generic',
'EcalEndcap':
'Ecal Calorimeter',
42 'TauOnline':
'Tau',
'Egamma':
'Photons',
'HcalIsoTrack':
'Alca',
43 'EcalBarrel':
'Ecal Calorimeter'
48 '''Returns image path for https://cmsweb.cern.ch/dqm histogram
49 visualisation service'''
50 run = int(re.findall(
'_R(\d*)__', filename)[0])
51 parts = [e.rstrip(
'.root')
for e
in filename.split(
'__')]
52 path = path.replace(
'Run summary/',
'')
53 return 'archive/%s/%s/%s/%s/%s' % (run, parts[1], parts[2], parts[3], path)
57 '''Returns full URL of histogram (or histogram overlay) image for
58 https://cmsweb.cern.ch/dqm visualisation service.'''
59 base =
'https://cmsweb.cern.ch/dqm/relval/plotfairy'
61 return '%s/%s?w=%s;h=%s' % (base,
get_img_path(f1, path), w, h)
62 return '%s/overlay?obj=%s;obj=%s;w=%s;h=%s' % (base,
67 '''Returns extracted dataset name from the given ROOT filename.'''
68 if re.search(
'RelVal', name):
69 run = str(int(re.findall(
'_R(\d{9})_', name)[0]))
70 ds = re.findall(
'GR_R_\d*_V\d*C?_(?:RelVal)?_([\w\d]*-v\d+)_', name)[0]
72 run, ds = re.findall(
'R(\d{9})__([\w\d]*)__CMSSW_', name)[0:1]
73 return '_'.
join([ds, str(int(run))])
77 '''Returns extracted release from the given ROOT filename.'''
78 return re.findall(
'R\d{9}__([\w\d_-]*)__DQM.root', name)[0]
82 '''Returns ``successes``, ``fails``, ``nulls`` for the given dir_ranges.'''
83 successes, nulls, fails = 0, 0, 0
84 for from_id, till_id
in dir_ranges:
85 c.execute(
'''SELECT count(*) FROM HistogramComparison
86 WHERE p_value >= 0 AND p_value > ? AND
87 id >= ? and id <= ?''', (threshold, from_id, till_id))
88 successes += c.fetchone()[0]
89 c.execute(
'''SELECT count(*) FROM HistogramComparison WHERE
90 p_value < 0 AND id >= ? AND id <= ?''', (from_id, till_id))
91 nulls += c.fetchone()[0]
92 c.execute(
'''SELECT count(*) FROM HistogramComparison
93 WHERE p_value >= 0 AND p_value <= ? AND
94 id >= ? AND id <= ?''', (threshold, from_id, till_id))
95 fails += c.fetchone()[0]
96 return successes, nulls, fails
100 '''Converts integers ``successes``, ``nulls`` and ``fails`` to percents.'''
101 if successes
is None:
102 return None,
None,
None
103 total = successes + fails + nulls
105 return None,
None,
None
106 success = round(100. * successes / total, 2)
107 null = round(100. * nulls / total, 2)
108 fail = round(100. * fails / total, 2)
109 return success, null, fail
113 '''Returns file folder stats for one "summary table" column.'''
115 c.execute(
'''SELECT name, from_histogram_id, till_histogram_id FROM
116 Directory WHERE parent_id=?''', (dir_id,))
118 file_folders =
dict()
119 total_successes, total_nulls, total_fails = 0, 0, 0
120 for name, from_id, till_id
in dirs:
121 successes, nulls, fails =
get_stats(c, threshold, ((from_id, till_id),))
122 total_successes += successes
125 if file_folders.has_key(name):
126 file_folders[name].
append([file_id, ds_name, successes, nulls, fails])
128 file_folders[name] = [file_id, ds_name, successes, nulls, fails]
129 return [(
'Summary', [file_id, ds_name, total_successes, total_nulls, total_fails])] + file_folders.items()
133 '''To do less DB calls, joins [(from_id, till_id), ...] ranges.'''
134 if type(ranges) == tuple:
136 if ranges[-1][-1] + 1 == elem[0]:
137 ranges[-1] = (ranges[-1][0], elem[1])
144 '''Returns all ``ReleaseComparisons`` found on database.'''
145 c.execute(
'SELECT title, statistical_test FROM ReleaseComparison')
150 '''Returns available database list and their releases.'''
151 db_list = [db
for db
in listdir(path)
if db.endswith(
'.db')]
152 db_list_with_releases = []
154 conn = sqlite3.connect(
join(path, db))
156 db_list_with_releases.append((db[:-3], releases))
158 return db_list_with_releases
163 '''Returns context for ``release_summary.html`` template.'''
166 c.execute(
'''SELECT release1, release2, id FROM ReleaseComparison
167 WHERE title = ? AND statistical_test = ?''', (release_title, st_test))
168 context[
'release1'], context[
'release2'], release_comp_id = c.fetchone()
171 c.execute(
'''SELECT from_histogram_id, till_histogram_id FROM Directory
172 WHERE id IN (SELECT directory_id FROM RootFileComparison
173 WHERE release_comparison_id = ?)''', (release_comp_id,))
174 dir_ranges = c.fetchall()
176 if len(dir_ranges) > 1:
177 dir_ranges = reduce(join_ranges, dir_ranges)
179 context[
'successes'], context[
'nulls'], context[
'fails'], =
get_stats(c, threshold, dir_ranges)
181 context[
'total'] = context[
'successes'] + context[
'fails'] + context[
'nulls']
183 context[
'success'], context[
'null'], context[
'fail'] = \
184 get_percentage(context[
'successes'], context[
'nulls'], context[
'fails'])
187 c.execute(
'''SELECT id, filename1, directory_id FROM RootFileComparison
188 WHERE release_comparison_id = ?''', (release_comp_id,))
193 for file_id, filename, dir_id
in files:
195 file_folders =
get_folders(c, file_id, filename, dir_id, threshold)
196 for folder_name, file_folder_stats
in file_folders:
197 if folders.has_key(folder_name):
199 folders[folder_name].
append(file_folder_stats)
201 folders[folder_name][0][2] += file_folder_stats[2]
202 folders[folder_name][0][3] += file_folder_stats[3]
203 folders[folder_name][0][4] += file_folder_stats[4]
205 folder_summary = [
None,
'Summary', file_folder_stats[2],
206 file_folder_stats[3], file_folder_stats[4]]
207 folders[folder_name] = [folder_summary, file_folder_stats]
210 folders = [(
'Summary', folders.pop(
'Summary'))] + sorted(folders.items(), key=
lambda x: x[0])
211 for folder, file_stats
in folders:
213 if len(file_stats) != len(files)+1:
214 for i, file_
in enumerate(files):
215 if file_[0] != file_stats[i][0]:
216 file_stats = file_stats[:i] + [[
None,
"N/A",
None,
None,
None]] + file_stats[i:]
218 for i, stats
in enumerate(file_stats):
220 context[
'folders'] = folders
224 for folder
in folders:
232 c.execute(
'''SELECT name, from_histogram_id, till_histogram_id FROM Directory
233 WHERE parent_id IN (SELECT directory_id FROM RootFileComparison
234 WHERE release_comparison_id = ?)''', (release_comp_id,))
235 lvl3_dir_ranges = c.fetchall()
237 cum_lvl3_dir_ranges =
dict()
238 for name, from_id, till_id
in lvl3_dir_ranges:
239 if cum_lvl3_dir_ranges.has_key(name):
240 cum_lvl3_dir_ranges[name].
append((from_id, till_id))
242 cum_lvl3_dir_ranges[name] = [(from_id, till_id)]
245 summary_stats =
dict()
246 detailed_stats =
dict()
247 for name, ranges
in cum_lvl3_dir_ranges.iteritems():
248 successes, nulls, fails =
get_stats(c, threshold, ranges)
249 if detailed_stats.has_key(name):
250 detailed_stats[name][0] += successes
251 detailed_stats[name][1] += nulls
252 detailed_stats[name][2] += fails
254 detailed_stats[name] = [successes, nulls, fails]
255 if renaming.has_key(name):
256 if summary_stats.has_key(renaming[name]):
257 summary_stats[renaming[name]][0] += successes
258 summary_stats[renaming[name]][1] += nulls
259 summary_stats[renaming[name]][2] += fails
261 summary_stats[renaming[name]] = [successes, nulls, fails]
265 for name, stats
in summary_stats.iteritems():
268 ratio = float(stats[0]) / sum(stats)
269 summary_ratios.append((name, ratio))
271 for name, stats
in detailed_stats.iteritems():
274 ratio = float(stats[0]) / sum(stats)
275 detailed_ratios.append((name, ratio))
277 context[
'summary_ratios'] = sorted(summary_ratios, key=
lambda x: x[0])
278 context[
'detailed_ratios'] = sorted(detailed_ratios, key=
lambda x: x[0])
283 '''Returns context for ``directory_summary.html`` template.'''
285 c.execute(
'''SELECT directory_id, filename1, filename2 FROM RootFileComparison
286 WHERE id = ?''', (file_id,))
287 dir_id, f1, f2 = c.fetchone()
296 for dir_name
in url_args:
297 c.execute(
'''SELECT id, name FROM Directory WHERE name = ? AND
298 parent_id = ?''', (dir_name, dir_id))
299 dir_id, name = c.fetchone()
300 directory_names.append(name)
301 context[
'parent_name'] =
'/'.
join(directory_names)
304 c.execute(
'''SELECT from_histogram_id, till_histogram_id FROM
305 Directory WHERE id = ?''', (dir_id,))
306 ranges = c.fetchone()
307 successes, nulls, fails =
get_stats(c, threshold, (ranges,))
310 'successes': successes,
'nulls': nulls,
'fails': fails,
311 'success': success,
'null': null,
'fail': fail,
312 'total': successes + nulls + fails,
'dir_name': dir_name
315 c.execute(
'''SELECT name, from_histogram_id, till_histogram_id FROM Directory
316 WHERE parent_id = ?''', (dir_id,))
317 subdirs = c.fetchall()
319 for name, from_id, till_id
in subdirs:
320 successes, nulls, fails =
get_stats(c, threshold, [(from_id, till_id,)])
322 subdir_stats.append((name, successes + nulls + fails, successes,
323 nulls, fails, success, null, fail))
324 context[
'subdirs'] = sorted(subdir_stats, key=
lambda x: x[4], reverse=
True)
327 c.execute(
'''SELECT name, p_value FROM HistogramComparison
328 WHERE directory_id = ?''', (dir_id,))
330 successful_histos = []
332 for name, p_value
in c.fetchall():
333 path = quote(
'%s/%s' % (
'/'.
join(url_args), name))
338 null_histos.append((name, p_value, url1, url2, overlay))
339 elif p_value <= threshold:
340 failed_histos.append((name, p_value, url1, url2, overlay))
342 successful_histos.append((name, p_value, url1, url2, overlay))
344 context[
'failed_histos'] = sorted(failed_histos, key=
lambda x: x[1], reverse=
True)
345 context[
'null_histos'] = null_histos
346 context[
'successful_histos'] = sorted(successful_histos, key=
lambda x: x[1], reverse=
True)
def get_release_summary_stats
def db_list_with_releases
static std::string join(char **cmd)
def get_directory_summary_stats