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

◆ auth_download_file()

def utils_v2.auth_download_file (   url,
  chunk_size = 1048576 
)

Definition at line 200 of file utils_v2.py.

200 def auth_download_file(url, chunk_size=1048576):
201  filename = basename(url)
202  file_path = join(auth_download_file.work_dir, filename)
203 
204  file = open(file_path, 'wb')
205  opener = build_opener(X509CertOpen())
206  url_file = opener.open(Request(url))
207  chunk = url_file.read(chunk_size)
208  while chunk:
209  file.write(chunk)
210  auth_download_file.q.put((1,)) # reports, that downloaded 1MB
211  chunk = url_file.read(chunk_size)
212  print('\rDownloaded: %s ' % (filename,))
213  file.close()
214 
215 

References join(), and edm.print().

◆ auth_wget()

def utils_v2.auth_wget (   url)

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

Definition at line 186 of file utils_v2.py.

186 def auth_wget(url):
187  try:
188  opener = build_opener(X509CertOpen())
189  return opener.open(Request(url)).read()
190  except HTTPError as e:
191  print('\nError: DQM GUI is temporarily unavailable. Probably maintainance hours. '+\
192  'Please try again later. Original error message: ``%s``. \nExiting...\n' % (e,))
193  exit()
194  except BadStatusLine as e:
195  print('\nYou do not have permissions to access DQM GUI. Please check if your certificates '+\
196  'in ``~/.globus`` directory are configured correctly. Exitting...')
197  exit()
198 
199 

References beamvalidation.exit(), edm.print(), and readEcalDQMStatus.read.

Referenced by recursive_search_online().

◆ check_disk_for_space()

def utils_v2.check_disk_for_space (   work_path,
  size_needed 
)
Checks afs file system for space.

Definition at line 462 of file utils_v2.py.

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

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ get_relval_cmssw_version()

def utils_v2.get_relval_cmssw_version (   file)

Definition at line 92 of file utils_v2.py.

92 def get_relval_cmssw_version(file):
93  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
94  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
95  if cmssw_release and gr_r_version:
96  return (cmssw_release[0], gr_r_version[0])
97 

Referenced by get_version().

◆ get_relval_id()

def utils_v2.get_relval_id (   file)
Returns unique relval ID (dataset name) for a given file.

Definition at line 98 of file utils_v2.py.

98 def get_relval_id(file):
99  """Returns unique relval ID (dataset name) for a given file."""
100  dataset_name = re.findall('R\d{9}__([\w\d]*)__CMSSW_', file)
101  return dataset_name[0]
102 

◆ get_relval_max_version()

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 80 of file utils_v2.py.

80 def get_relval_max_version(files):
81  """Returns file with maximum version at a) beggining of the file,
82  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
83  max_file = files[0]
84  max_v = get_relval_version(files[0])
85  for file in files:
86  file_v = get_relval_version(file)
87  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
88  max_file = file
89  max_v = file_v
90  return max_file
91 

References get_relval_version().

◆ get_relval_version()

def utils_v2.get_relval_version (   file)

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

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

Definition at line 73 of file utils_v2.py.

73 def get_relval_version(file):
74  """Returns tuple (CMSSW version, run version) for specified file."""
75  cmssw_version = re.findall('DQM_V(\d*)_', file)
76  run_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-[\w\d]*_V\d*\w?(?:_[\w\d]*)?-v(\d*)__', file)
77  if cmssw_version and run_version:
78  return (int(cmssw_version[0]), int(run_version[0]))
79 

References createfilelist.int.

Referenced by get_relval_max_version().

◆ get_relvaldata_cmssw_version()

def utils_v2.get_relvaldata_cmssw_version (   file)
Returns tuple (CMSSW release, GR_R version) for specified RelValData file.

Definition at line 42 of file utils_v2.py.

43  """Returns tuple (CMSSW release, GR_R version) for specified RelValData file."""
44  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
45  gr_r_version = re.findall('-(GR_R_\d*_V\d*\w?)(?:_RelVal)?_', file)
46  if not gr_r_version:
47  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-(\w*)_RelVal_', file)
48  if cmssw_release and gr_r_version:
49  return (cmssw_release[0], gr_r_version[0])
50 

Referenced by get_version().

◆ get_relvaldata_id()

def utils_v2.get_relvaldata_id (   file)

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

Returns unique relvaldata ID for a given file.

Definition at line 32 of file utils_v2.py.

32 def get_relvaldata_id(file):
33  """Returns unique relvaldata ID for a given file."""
34  run_id = re.search('R\d{9}', file)
35  run = re.search('_RelVal_([\w\d]*)-v\d__', file)
36  if not run:
37  run = re.search('GR_R_\d*_V\d*C?_([\w\d]*)-v\d__', file)
38  if run_id and run:
39  return (run_id.group(), run.group(1))
40  return None
41 

◆ get_relvaldata_max_version()

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 60 of file utils_v2.py.

61  """Returns file with maximum version at a) beggining of the file,
62  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
63  max_file = files[0]
64  max_v = get_relvaldata_version(files[0])
65  for file in files:
66  file_v = get_relvaldata_version(file)
67  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
68  max_file = file
69  max_v = file_v
70  return max_file
71 

References get_relvaldata_version().

◆ get_relvaldata_version()

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

Definition at line 51 of file utils_v2.py.

51 def get_relvaldata_version(file):
52  """Returns tuple (CMSSW version, run version) for specified file."""
53  cmssw_version = re.findall('DQM_V(\d*)_', file)
54  run_version = re.findall('_RelVal_[\w\d]*-v(\d)__', file)
55  if not run_version:
56  run_version = re.findall('GR_R_\d*_V\d*C?_[\w\d]*-v(\d)__', file)
57  if cmssw_version and run_version:
58  return (int(cmssw_version[0]), int(run_version[0]))
59 

References createfilelist.int.

Referenced by get_relvaldata_max_version().

◆ get_size_to_download()

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 446 of file utils_v2.py.

446 def get_size_to_download(work_path, files_with_urls):
447  """Returns file list to download and total size to download."""
448  opener = build_opener(X509CertOpen())
449  size_to_download = 0
450  files_to_download = []
451  for filename, url in files_with_urls:
452  url_file = opener.open(Request(url))
453  size = int(url_file.headers["Content-Length"])
454  file_path = join(work_path, filename)
455  if exists(file_path) and getsize(file_path) / 1024 == size / 1024:
456  print("Exists on disk %s." % filename)
457  else:
458  size_to_download += size
459  files_to_download.append(url)
460  return size_to_download, files_to_download
461 

References createfilelist.int, join(), and edm.print().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ get_version()

def utils_v2.get_version (   filename)
Returns CMSSW and GR_R versions for the given filename.

Definition at line 435 of file utils_v2.py.

435 def get_version(filename):
436  """Returns CMSSW and GR_R versions for the given filename."""
437  if is_relvaldata([filename]):
438  version_elems = get_relvaldata_cmssw_version(filename)
439  else:
440  relval_version = get_relval_cmssw_version(filename)
441  version_elems = (relval_version[0], relval_version[1][0], relval_version[1][1])
442  version_elems = [elem.strip('_').strip('RelVal_') for elem in version_elems]
443  return '___'.join([elem for elem in version_elems if elem])
444 
445 

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

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ init_database()

def utils_v2.init_database (   db_path)

Utils.

Definition at line 391 of file utils_v2.py.

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

References edm.print().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ is_relvaldata()

def utils_v2.is_relvaldata (   files)

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

Definition at line 104 of file utils_v2.py.

104 def is_relvaldata(files):
105  is_relvaldata_re = re.compile('_RelVal_')
106  return any([is_relvaldata_re.search(filename) for filename in files])
107 

References any().

Referenced by get_version(), and make_file_pairs().

◆ make_file_pairs()

def utils_v2.make_file_pairs (   files1,
  files2 
)

Definition at line 108 of file utils_v2.py.

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

References mps_setup.append, beamvalidation.exit(), is_relvaldata(), edm.print(), and str.

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ recursive_search_online()

def utils_v2.recursive_search_online (   url,
  rel1,
  frags1,
  rel2,
  frags2 
)
Recursively searches for files, that matches the pattern.

Definition at line 216 of file utils_v2.py.

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

References python.cmstools.all(), auth_wget(), join(), and edm.print().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ search_on_disk()

def utils_v2.search_on_disk (   work_path,
  rel1,
  frags1,
  rel2,
  frags2 
)

Definition at line 256 of file utils_v2.py.

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

References python.cmstools.all(), beamvalidation.exit(), and edm.print().

Referenced by ValidationMatrix_v2.ReleaseComparison.compare().

◆ show_status_bar()

def utils_v2.show_status_bar (   total_size)
Shows download status.

Definition at line 480 of file utils_v2.py.

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

Variable Documentation

◆ comparison_errors

utils_v2.comparison_errors

Exception definitions.

Definition at line 285 of file utils_v2.py.

◆ tests

utils_v2.tests

Definition at line 388 of file utils_v2.py.

utils_v2.get_relvaldata_version
def get_relvaldata_version(file)
Definition: utils_v2.py:51
utils_v2.recursive_search_online
def recursive_search_online(url, rel1, frags1, rel2, frags2)
Definition: utils_v2.py:216
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
utils_v2.get_version
def get_version(filename)
Definition: utils_v2.py:435
utils_v2.search_on_disk
def search_on_disk(work_path, rel1, frags1, rel2, frags2)
Definition: utils_v2.py:256
python.cmstools.all
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:26
any
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:34
utils_v2.is_relvaldata
def is_relvaldata(files)
--------------------— Make file pairs -----------------------—
Definition: utils_v2.py:104
utils_v2.get_relval_cmssw_version
def get_relval_cmssw_version(file)
Definition: utils_v2.py:92
utils_v2.get_relval_id
def get_relval_id(file)
Definition: utils_v2.py:98
utils_v2.get_relval_max_version
def get_relval_max_version(files)
Definition: utils_v2.py:80
utils_v2.show_status_bar
def show_status_bar(total_size)
Definition: utils_v2.py:480
str
#define str(s)
Definition: TestProcessor.cc:48
utils_v2.make_file_pairs
def make_file_pairs(files1, files2)
Definition: utils_v2.py:108
mps_setup.append
append
Definition: mps_setup.py:85
utils_v2.get_relval_version
def get_relval_version(file)
----------------— Make files pairs: RelVal utils ------------------—
Definition: utils_v2.py:73
createfilelist.int
int
Definition: createfilelist.py:10
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
utils_v2.get_relvaldata_cmssw_version
def get_relvaldata_cmssw_version(file)
Definition: utils_v2.py:42
utils_v2.get_relvaldata_id
def get_relvaldata_id(file)
--------------— Make files pairs: RelValData utils -----------------—
Definition: utils_v2.py:32
utils_v2.get_relvaldata_max_version
def get_relvaldata_max_version(files)
Definition: utils_v2.py:60
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
utils_v2.check_disk_for_space
def check_disk_for_space(work_path, size_needed)
Definition: utils_v2.py:462
utils_v2.get_size_to_download
def get_size_to_download(work_path, files_with_urls)
Definition: utils_v2.py:446
utils_v2.init_database
def init_database(db_path)
Utils.
Definition: utils_v2.py:391
utils_v2.auth_download_file
def auth_download_file(url, chunk_size=1048576)
Definition: utils_v2.py:200
utils_v2.auth_wget
def auth_wget(url)
-----------------— Recursife file downloader --------------------—
Definition: utils_v2.py:186