CMS 3D CMS Logo

Classes | Functions | Variables
utils_v2 Namespace Reference

Classes

class  Chi2Test
 
class  ComparisonError
 
class  KolmogorovTest
 
class  StatisticalTest
 StatisticalTests. More...
 

Functions

def auth_download_file (url, chunk_size=1048576)
 
def auth_wget (url)
 -----------------— Recursife file downloader --------------------— More...
 
def check_disk_for_space (work_path, size_needed)
 
def get_relval_cmssw_version (file)
 
def get_relval_id (file)
 
def get_relval_max_version (files)
 
def get_relval_version (file)
 -------------—— Make files pairs: RelVal utils ---------------—— More...
 
def get_relvaldata_cmssw_version (file)
 
def get_relvaldata_id (file)
 -----------—— Make files pairs: RelValData utils --------------—— More...
 
def get_relvaldata_max_version (files)
 
def get_relvaldata_version (file)
 
def get_size_to_download (work_path, files_with_urls)
 
def get_version (filename)
 
def init_database (db_path)
 Utils. More...
 
def is_relvaldata (files)
 --------------------— Make file pairs -----------------------— More...
 
def make_file_pairs (files1, files2)
 
def recursive_search_online (url, rel1, frags1, rel2, frags2)
 
def search_on_disk (work_path, rel1, frags1, rel2, frags2)
 
def show_status_bar (total_size)
 

Variables

 comparison_errors
 Exception definitions. More...
 
 tests
 

Detailed Description

Help functions for ValidationMatrix_v2.py.

Author:  Albertas Gimbutas,  Vilnius University (LT)
e-mail:  albertasgim@gmail.com

Function Documentation

def utils_v2.auth_download_file (   url,
  chunk_size = 1048576 
)

Definition at line 197 of file utils_v2.py.

References estimatePileup.basename, and join().

197 def auth_download_file(url, chunk_size=1048576):
198  filename = basename(url)
199  file_path = join(auth_download_file.work_dir, filename)
200 
201  file = open(file_path, 'wb')
202  opener = build_opener(X509CertOpen())
203  url_file = opener.open(Request(url))
204  chunk = url_file.read(chunk_size)
205  while chunk:
206  file.write(chunk)
207  auth_download_file.q.put((1,)) # reports, that downloaded 1MB
208  chunk = url_file.read(chunk_size)
209  print '\rDownloaded: %s ' % (filename,)
210  file.close()
211 
212 
def auth_download_file(url, chunk_size=1048576)
Definition: utils_v2.py:197
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def utils_v2.auth_wget (   url)

-----------------— Recursife file downloader --------------------—

Definition at line 183 of file utils_v2.py.

References cmsRelvalreport.exit.

Referenced by recursive_search_online().

183 def auth_wget(url):
184  try:
185  opener = build_opener(X509CertOpen())
186  return opener.open(Request(url)).read()
187  except HTTPError as e:
188  print '\nError: DQM GUI is temporarily unavailable. Probably maintainance hours. '+\
189  'Please try again later. Original error message: ``%s``. \nExiting...\n' % (e,)
190  exit()
191  except BadStatusLine as e:
192  print '\nYou do not have permissions to access DQM GUI. Please check if your certificates '+\
193  'in ``~/.globus`` directory are configured correctly. Exitting...'
194  exit()
195 
196 
def auth_wget(url)
-----------------— Recursife file downloader --------------------—
Definition: utils_v2.py:183
def utils_v2.check_disk_for_space (   work_path,
  size_needed 
)
Checks afs file system for space.

Definition at line 459 of file utils_v2.py.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

459 def check_disk_for_space(work_path, size_needed):
460  '''Checks afs file system for space.'''
461  pass
462  # try:
463  # fs_proc = subprocess.Popen(['fs', 'listquota', work_path], stdout=subprocess.PIPE)
464  # except OSError:
465  # return
466  # fs_response = fs_proc.communicate()[0]
467  # quota, used = re.findall('([\d]+)', fs_response)[:2]
468  # free_space = int(quota) - int(used)
469  # if free_space * 1024 < size_needed:
470  # print '\nNot enougth free space on disk.',
471  # print 'Free space: %d MB. Need: %d MB. Exiting...\n' % (free_space / 1024, size_needed /1048576)
472  # exit()
473  # elif size_needed:
474  # print 'Free space on disk: %d MB.\n' % (free_space / 1024,)
475 
476 
def check_disk_for_space(work_path, size_needed)
Definition: utils_v2.py:459
def utils_v2.get_relval_cmssw_version (   file)

Definition at line 89 of file utils_v2.py.

Referenced by get_version().

90  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
91  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
92  if cmssw_release and gr_r_version:
93  return (cmssw_release[0], gr_r_version[0])
94 
def get_relval_cmssw_version(file)
Definition: utils_v2.py:89
def utils_v2.get_relval_id (   file)
Returns unique relval ID (dataset name) for a given file.

Definition at line 95 of file utils_v2.py.

95 def get_relval_id(file):
96  """Returns unique relval ID (dataset name) for a given file."""
97  dataset_name = re.findall('R\d{9}__([\w\d]*)__CMSSW_', file)
98  return dataset_name[0]
99 
def get_relval_id(file)
Definition: utils_v2.py:95
def utils_v2.get_relval_max_version (   files)
Returns file with maximum version at a) beggining of the file,
e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max.

Definition at line 77 of file utils_v2.py.

References get_relval_version().

78  """Returns file with maximum version at a) beggining of the file,
79  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
80  max_file = files[0]
81  max_v = get_relval_version(files[0])
82  for file in files:
83  file_v = get_relval_version(file)
84  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
85  max_file = file
86  max_v = file_v
87  return max_file
88 
def get_relval_max_version(files)
Definition: utils_v2.py:77
def get_relval_version(file)
-------------—— Make files pairs: RelVal utils ---------------——
Definition: utils_v2.py:70
def utils_v2.get_relval_version (   file)

-------------—— Make files pairs: RelVal utils ---------------——

Returns tuple (CMSSW version, run version) for specified file.

Definition at line 70 of file utils_v2.py.

References createfilelist.int.

Referenced by get_relval_max_version().

71  """Returns tuple (CMSSW version, run version) for specified file."""
72  cmssw_version = re.findall('DQM_V(\d*)_', file)
73  run_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-[\w\d]*_V\d*\w?(?:_[\w\d]*)?-v(\d*)__', file)
74  if cmssw_version and run_version:
75  return (int(cmssw_version[0]), int(run_version[0]))
76 
def get_relval_version(file)
-------------—— Make files pairs: RelVal utils ---------------——
Definition: utils_v2.py:70
def utils_v2.get_relvaldata_cmssw_version (   file)
Returns tuple (CMSSW release, GR_R version) for specified RelValData file.

Definition at line 39 of file utils_v2.py.

Referenced by get_version().

40  """Returns tuple (CMSSW release, GR_R version) for specified RelValData file."""
41  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
42  gr_r_version = re.findall('-(GR_R_\d*_V\d*\w?)(?:_RelVal)?_', file)
43  if not gr_r_version:
44  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-(\w*)_RelVal_', file)
45  if cmssw_release and gr_r_version:
46  return (cmssw_release[0], gr_r_version[0])
47 
def get_relvaldata_cmssw_version(file)
Definition: utils_v2.py:39
def utils_v2.get_relvaldata_id (   file)

-----------—— Make files pairs: RelValData utils --------------——

Returns unique relvaldata ID for a given file.

Definition at line 29 of file utils_v2.py.

30  """Returns unique relvaldata ID for a given file."""
31  run_id = re.search('R\d{9}', file)
32  run = re.search('_RelVal_([\w\d]*)-v\d__', file)
33  if not run:
34  run = re.search('GR_R_\d*_V\d*C?_([\w\d]*)-v\d__', file)
35  if run_id and run:
36  return (run_id.group(), run.group(1))
37  return None
38 
def get_relvaldata_id(file)
-----------—— Make files pairs: RelValData utils --------------——
Definition: utils_v2.py:29
def utils_v2.get_relvaldata_max_version (   files)
Returns file with maximum version at a) beggining of the file,
e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max.

Definition at line 57 of file utils_v2.py.

References get_relvaldata_version().

58  """Returns file with maximum version at a) beggining of the file,
59  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
60  max_file = files[0]
61  max_v = get_relvaldata_version(files[0])
62  for file in files:
63  file_v = get_relvaldata_version(file)
64  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
65  max_file = file
66  max_v = file_v
67  return max_file
68 
def get_relvaldata_max_version(files)
Definition: utils_v2.py:57
def get_relvaldata_version(file)
Definition: utils_v2.py:48
def utils_v2.get_relvaldata_version (   file)
Returns tuple (CMSSW version, run version) for specified file.

Definition at line 48 of file utils_v2.py.

References createfilelist.int.

Referenced by get_relvaldata_max_version().

49  """Returns tuple (CMSSW version, run version) for specified file."""
50  cmssw_version = re.findall('DQM_V(\d*)_', file)
51  run_version = re.findall('_RelVal_[\w\d]*-v(\d)__', file)
52  if not run_version:
53  run_version = re.findall('GR_R_\d*_V\d*C?_[\w\d]*-v(\d)__', file)
54  if cmssw_version and run_version:
55  return (int(cmssw_version[0]), int(run_version[0]))
56 
def get_relvaldata_version(file)
Definition: utils_v2.py:48
def utils_v2.get_size_to_download (   work_path,
  files_with_urls 
)
Returns file list to download and total size to download.

Definition at line 443 of file utils_v2.py.

References createfilelist.int, and join().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

443 def get_size_to_download(work_path, files_with_urls):
444  """Returns file list to download and total size to download."""
445  opener = build_opener(X509CertOpen())
446  size_to_download = 0
447  files_to_download = []
448  for filename, url in files_with_urls:
449  url_file = opener.open(Request(url))
450  size = int(url_file.headers["Content-Length"])
451  file_path = join(work_path, filename)
452  if exists(file_path) and getsize(file_path) / 1024 == size / 1024:
453  print "Exists on disk %s." % filename
454  else:
455  size_to_download += size
456  files_to_download.append(url)
457  return size_to_download, files_to_download
458 
def get_size_to_download(work_path, files_with_urls)
Definition: utils_v2.py:443
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def utils_v2.get_version (   filename)
Returns CMSSW and GR_R versions for the given filename.

Definition at line 432 of file utils_v2.py.

References get_relval_cmssw_version(), get_relvaldata_cmssw_version(), is_relvaldata(), join(), and digi_MixPreMix_cfi.strip.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

432 def get_version(filename):
433  """Returns CMSSW and GR_R versions for the given filename."""
434  if is_relvaldata([filename]):
435  version_elems = get_relvaldata_cmssw_version(filename)
436  else:
437  relval_version = get_relval_cmssw_version(filename)
438  version_elems = (relval_version[0], relval_version[1][0], relval_version[1][1])
439  version_elems = [elem.strip('_').strip('RelVal_') for elem in version_elems]
440  return '___'.join([elem for elem in version_elems if elem])
441 
442 
def get_version(filename)
Definition: utils_v2.py:432
def get_relval_cmssw_version(file)
Definition: utils_v2.py:89
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def is_relvaldata(files)
--------------------— Make file pairs -----------------------—
Definition: utils_v2.py:101
def get_relvaldata_cmssw_version(file)
Definition: utils_v2.py:39
def utils_v2.init_database (   db_path)

Utils.

Definition at line 388 of file utils_v2.py.

References estimatePileup.basename.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

388 def init_database(db_path):
389  print 'Initialising DB: %s...' % basename(db_path),
390  conn = sqlite3.connect(db_path)
391 
392  ## Creates tables
393  c = conn.cursor()
394  c.execute("""CREATE TABLE IF NOT EXISTS ReleaseComparison (
395  id INTEGER PRIMARY KEY,
396  title TEXT,
397  release1 TEXT,
398  release2 TEXT,
399  statistical_test TEXT
400  );""")
401  c.execute("""CREATE TABLE IF NOT EXISTS Directory (
402  id INTEGER PRIMARY KEY,
403  name TEXT,
404  parent_id INTEGER,
405  from_histogram_id INTEGER,
406  till_histogram_id INTEGER,
407  FOREIGN KEY (parent_id) REFERENCES Directory(id)
408  FOREIGN KEY (from_histogram_id) REFERENCES HistogramComparison(id)
409  FOREIGN KEY (till_histogram_id) REFERENCES HistogramComparison(id)
410  )""")
411  c.execute("""CREATE TABLE IF NOT EXISTS RootFileComparison (
412  id INTEGER PRIMARY KEY,
413  filename1 TEXT,
414  filename2 TEXT,
415  release_comparison_id INTEGER,
416  directory_id INTEGER,
417  FOREIGN KEY (release_comparison_id) REFERENCES ReleaseComparison(id),
418  FOREIGN KEY (directory_id) REFERENCES Directory(id)
419  )""")
420  c.execute("""CREATE TABLE IF NOT EXISTS HistogramComparison (
421  id INTEGER PRIMARY KEY,
422  name TEXT,
423  p_value REAL,
424  directory_id INTEGER,
425  FOREIGN KEY (directory_id) REFERENCES Directory(id)
426  )""")
427 
428  print 'Done.'
429  return db_path
430 
431 
def init_database(db_path)
Utils.
Definition: utils_v2.py:388
def utils_v2.is_relvaldata (   files)

--------------------— Make file pairs -----------------------—

Definition at line 101 of file utils_v2.py.

References any().

Referenced by get_version(), and make_file_pairs().

101 def is_relvaldata(files):
102  is_relvaldata_re = re.compile('_RelVal_')
103  return any([is_relvaldata_re.search(filename) for filename in files])
104 
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
def is_relvaldata(files)
--------------------— Make file pairs -----------------------—
Definition: utils_v2.py:101
def utils_v2.make_file_pairs (   files1,
  files2 
)

Definition at line 105 of file utils_v2.py.

References mps_alisetup.append, cmsPerfStripChart.dict, cmsRelvalreport.exit, is_relvaldata(), and harvestTrackValidationPlots.str.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

105 def make_file_pairs(files1, files2):
106  print '\n################# Analyzing files ###################'
107  ## Select functions to use
108  if is_relvaldata(files1):
109  is_relval_data = True
110  get_cmssw_version = get_relvaldata_cmssw_version
111  get_id = get_relvaldata_id
112  get_max_version = get_relvaldata_max_version
113  else:
114  is_relval_data = False
115  get_cmssw_version = get_relval_cmssw_version
116  get_id = get_relval_id
117  get_max_version = get_relval_max_version
118 
119  ## Divide files into groups
120  versions1, versions2 = dict(), dict() # {version1: [file1, file2, ...], version2: [...], ...}
121  for files, versions in (files1, versions1), (files2, versions2):
122  for file in files:
123  version = get_cmssw_version(file)
124  if version:
125  if version in versions:
126  versions[version].append(file)
127  else:
128  versions[version] = [file]
129 
130  ## Print the division into groups
131  print 'For RELEASE1 found file groups:'
132  for version in versions1:
133  print ' %s: %d files' % (str(version), len(versions1[version]))
134  if not versions1:
135  print 'None.'
136 
137  print '\nFor RELEASE2 found file groups:'
138  for version in versions2:
139  print ' %s: %d files' % (str(version), len(versions2[version]))
140  if not versions2:
141  print 'None.'
142 
143  if not len(versions1) or not len(versions2):
144  print '\nNot enough file groups. Exiting...\n'
145  exit()
146 
147  ## Pair till you find pairs.
148  pairs = []
149  for v1 in sorted(versions1, key=lambda x: len(versions1[x]), reverse=True):
150  for v2 in sorted(versions2, key=lambda x: len(versions2[x]), reverse=True):
151  if v1 == v2:
152  continue
153  ## Print the groups.
154  print '\n################# Pairing the files ###################'
155  print '%s (%d files) VS %s (%d files):\n' % (str(v1),
156  len(versions1[v1]), str(v2), len(versions2[v2]))
157 
158  ## Pairing two versions
159  for unique_id in set([get_id(file) for file in versions1[v1]]):
160  if is_relval_data:
161  dataset_re = re.compile(unique_id[0] + '_')
162  run_re = re.compile(unique_id[1])
163  c1_files = [file for file in versions1[v1] if dataset_re.search(file) and run_re.search(file)]
164  c2_files = [file for file in versions2[v2] if dataset_re.search(file) and run_re.search(file)]
165  else:
166  dataset_re = re.compile(unique_id + '_')
167  c1_files = [file for file in versions1[v1] if dataset_re.search(file)]
168  c2_files = [file for file in versions2[v2] if dataset_re.search(file)]
169 
170  if len(c1_files) > 0 and len(c2_files) > 0:
171  first_file = get_max_version(c1_files)
172  second_file = get_max_version(c2_files)
173  print '%s\n%s\n' % (first_file, second_file)
174  pairs.append((first_file, second_file))
175 
176  print "Got %d pairs." % (len(pairs))
177  if pairs:
178  return pairs
179  print 'Found no file pairs. Exiting..\n'
180  exit()
181 
def make_file_pairs(files1, files2)
Definition: utils_v2.py:105
def is_relvaldata(files)
--------------------— Make file pairs -----------------------—
Definition: utils_v2.py:101
def utils_v2.recursive_search_online (   url,
  rel1,
  frags1,
  rel2,
  frags2 
)
Recursively searches for files, that matches the pattern.

Definition at line 213 of file utils_v2.py.

References Vispa.Plugins.EdmBrowser.EdmDataAccessor.all(), auth_wget(), cmsPerfStripChart.dict, and join().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

213 def recursive_search_online(url, rel1, frags1, rel2, frags2):
214  """Recursively searches for files, that matches the pattern."""
215  if not url:
216  url = 'https://cmsweb.cern.ch/dqm/relval/data/browse/ROOT/RelValData/'
217  g1, g2 = recursive_search_online(url, rel1, frags1, rel2, frags2)
218  url = 'https://cmsweb.cern.ch/dqm/relval/data/browse/ROOT/RelVal/'
219  g3, g4 = recursive_search_online(url, rel1, frags1, rel2, frags2)
220  g1.update(g3), g2.update(g4)
221  return g1, g2
222 
223  domain = '://'.join(urlparse(url)[:2])
224 
225  ## Compile regular expressions
226  href_re = re.compile(r"<a href='([-./\w]*)'>([-./\w]*)<")
227 
228  def compile_res(rel, frags):
229  frags = frags.split(',')
230  regexps = [s for s in frags if not s.startswith('!')]
231  regexps += ['^((?%s).)*$' % s for s in frags if s.startswith('!')]
232  regexps += [rel + '-', '.root']
233  return [re.compile(r) for r in regexps]
234 
235  res1 = compile_res(rel1, frags1)
236  res2 = compile_res(rel2, frags2)
237 
238  ## Recursively find files that matches regular expressions
239  hrefs = [(name, path) for path, name in href_re.findall(auth_wget(url))[1:]]
240  files_with_urls1, files_with_urls2 = dict(), dict()
241  for name, path in hrefs:
242  if splitext(name)[1]: # If file
243  if all([r.search(name) for r in res1]):
244  files_with_urls1[name] = domain + path
245  if all([r.search(name) for r in res2]):
246  files_with_urls2[name] = domain + path
247  else:
248  print domain + path
249  new_hrefs = href_re.findall(auth_wget(domain + path))[1:]
250  hrefs.extend([(name, path) for path, name in new_hrefs])
251  return files_with_urls1, files_with_urls2
252 
def recursive_search_online(url, rel1, frags1, rel2, frags2)
Definition: utils_v2.py:213
def auth_wget(url)
-----------------— Recursife file downloader --------------------—
Definition: utils_v2.py:183
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def utils_v2.search_on_disk (   work_path,
  rel1,
  frags1,
  rel2,
  frags2 
)

Definition at line 253 of file utils_v2.py.

References Vispa.Plugins.EdmBrowser.EdmDataAccessor.all(), and cmsRelvalreport.exit.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

253 def search_on_disk(work_path, rel1, frags1, rel2, frags2):
254  if not work_path:
255  print 'No working directory specified. Use "--dir DIR" option to ' +\
256  'specify working directory. Exiting...'
257  exit()
258  ## Compile regular expressions
259  def compile_res(rel, frags):
260  frags = frags.split(',')
261  regexps = [s for s in frags if not s.startswith('!')]
262  regexps += ['^((?%s).)*$' % s for s in frags if s.startswith('!')]
263  regexps += [rel + '-', '.root']
264  return [re.compile(r) for r in regexps]
265 
266  res1 = compile_res(rel1, frags1)
267  res2 = compile_res(rel2, frags2)
268 
269  ## Recursively find files that matches regular expressions
270  files = listdir(work_path)
271  files1, files2 = [], []
272  for name in files:
273  if splitext(name)[1]:
274  if all([r.search(name) for r in res1]):
275  files1.append(name)
276  if all([r.search(name) for r in res2]):
277  files2.append(name)
278  return files1, files2
279 
280 
def search_on_disk(work_path, rel1, frags1, rel2, frags2)
Definition: utils_v2.py:253
def utils_v2.show_status_bar (   total_size)
Shows download status.

Definition at line 477 of file utils_v2.py.

477 def show_status_bar(total_size):
478  """Shows download status."""
479  q = show_status_bar.q
480  total_size = total_size / (1024*1024)
481  downloaded = 0
482  while downloaded < total_size:
483  try:
484  o = q.get(timeout=20)
485  downloaded += 1
486  print '\r %d/%d MB %d%% ' % (downloaded, total_size, 100*downloaded/total_size),
487  sys.stdout.flush()
488  except Empty:
489  time.sleep(1)
490  break
491 
def show_status_bar(total_size)
Definition: utils_v2.py:477

Variable Documentation

utils_v2.comparison_errors

Exception definitions.

Definition at line 282 of file utils_v2.py.

utils_v2.tests