CMS 3D CMS Logo

Classes | Functions | Variables

utils Namespace Reference

Classes

class  BinToBin
class  BinToBin1percent
class  Chi2
class  KS
class  StatisticalTest
class  unpickler

Functions

def ask_ok
def code_generator
def functor
def get_relval_cmssw_version
def get_relval_id
def get_relval_max_version
def get_relval_version
 ------------------- Make files pairs: RelVal utils ---------------------
def get_relvaldata_cmssw_version
def get_relvaldata_id
 ----------------- Make files pairs: RelValData utils --------------------
def get_relvaldata_max_version
def get_relvaldata_version
def getNbins
def is_empty
def is_relvaldata
 ------------------------- Make files pairs --------------------------
def is_sparse
def literal2root
def logger
def make_files_pairs
def parse_word
def profile2histo
def setTDRStyle
def test_env
def tree
def user_info
def wget

Variables

int _log_level = 10
tuple TAG = re.compile(r'[a-zA-Z0-9]')
 theargv = sys.argv

Function Documentation

def utils::ask_ok (   prompt,
  retries = 4,
  complaint = 'yes or no' 
)

Definition at line 423 of file utils.py.

00424                                                     :
00425     while True:
00426         ok = raw_input(prompt)
00427         if ok in ('y', 'ye', 'yes'):
00428             return True
00429         if ok in ('n', 'no'):
00430             return False
00431         retries = retries - 1
00432         if retries < 0:
00433             raise IOError('refusenik user')
00434         print complaint
00435 
00436 #-------------------------------------------------------------------------------

def utils::code_generator (   kwds)
Code generator function, parse user arguments, load and
return appropriate template generator module.

Definition at line 106 of file utils.py.

00107                         :
00108     """
00109     Code generator function, parse user arguments, load and
00110     return appropriate template generator module.
00111     """
00112     debug = kwds.get('debug', None)
00113     if  debug:
00114         print "Configuration:"
00115         pprint.pprint(kwds)
00116     try:
00117         klass  = kwds.get('tmpl')
00118         mname  = 'FWCore.Skeletons.%s' % klass.lower()
00119         module = __import__(mname, fromlist=[klass])
00120     except ImportError as err:
00121         klass  = 'AbstractPkg'
00122         module = __import__('FWCore.Skeletons.pkg', fromlist=[klass])
00123         if  debug:
00124             print "%s, will use %s" % (str(err), klass)
00125     obj = getattr(module, klass)(kwds)
00126     return obj

def utils::functor (   code,
  kwds,
  debug = 0 
)
Auto-generate and execute function with given code and configuration
For details of compile/exec/eval see
http://lucumr.pocoo.org/2011/2/1/exec-in-python/

Definition at line 54 of file utils.py.

00055                                 :
00056     """
00057     Auto-generate and execute function with given code and configuration
00058     For details of compile/exec/eval see
00059     http://lucumr.pocoo.org/2011/2/1/exec-in-python/
00060     """
00061     args  = []
00062     for key, val in kwds.items():
00063         if  isinstance(val, basestring):
00064             arg = '%s="%s"' % (key, val)
00065         elif isinstance(val, list):
00066             arg = '%s=%s' % (key, val)
00067         else:
00068             msg = 'Unsupported data type "%s" <%s>' % (val, type(val)) 
00069             raise Exception(msg)
00070         args.append(arg)
00071     func  = '\nimport sys'
00072     func += '\nimport StringIO'
00073     func += "\ndef func(%s):\n" % ','.join(args)
00074     func += code
00075     func += """
00076 def capture():
00077     "Capture snippet printous"
00078     old_stdout = sys.stdout
00079     sys.stdout = StringIO.StringIO()
00080     func()
00081     out = sys.stdout.getvalue()
00082     sys.stdout = old_stdout
00083     return out\n
00084 capture()\n"""
00085     if  debug:
00086         print "\n### generated code\n"
00087         print func
00088     # compile python code as exec statement
00089     obj   = compile(func, '<string>', 'exec')
00090     # define execution namespace
00091     namespace = {}
00092     # execute compiled python code in given namespace
00093     exec obj in namespace
00094     # located generated function object, run it and return its results
00095     return namespace['capture']()

def utils::get_relval_cmssw_version (   file)

Definition at line 536 of file utils.py.

00537                                   :
00538     cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
00539     gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
00540     if cmssw_release and gr_r_version:
00541         return (cmssw_release[0], gr_r_version[0])

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

Definition at line 542 of file utils.py.

00543                        :
00544     """Returns unique relval ID (dataset name) for a given file."""
00545     dataset_name = re.findall('R\d{9}__([\w\d]*)__CMSSW_', file)
00546     return dataset_name[0]

def utils::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 524 of file utils.py.

00525                                  :
00526     """Returns file with maximum version at a) beggining of the file,
00527     e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
00528     max_file = files[0]
00529     max_v = get_relval_version(files[0])
00530     for file in files:
00531         file_v = get_relval_version(file)
00532         if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
00533             max_file = file
00534             max_v = file_v
00535     return max_file

def utils::get_relval_version (   file)

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

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

Definition at line 517 of file utils.py.

00518                             :
00519     """Returns tuple (CMSSW version, run version) for specified file."""
00520     cmssw_version = re.findall('DQM_V(\d*)_', file)
00521     run_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-[\w\d]*_V\d*\w?(?:_[\w\d]*)?-v(\d*)__', file)
00522     if cmssw_version and run_version:
00523         return (int(cmssw_version[0]), int(run_version[0]))

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

Definition at line 486 of file utils.py.

00487                                       :
00488     """Returns tuple (CMSSW release, GR_R version) for specified RelValData file."""
00489     cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
00490     gr_r_version = re.findall('-(GR_R_\d*_V\d*\w?)(?:_RelVal)?_', file)
00491     if not gr_r_version:
00492         gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-(\w*)_RelVal_', file)
00493     if cmssw_release and gr_r_version:
00494         return (cmssw_release[0], gr_r_version[0])

def utils::get_relvaldata_id (   file)

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

Returns unique relvaldata ID for a given file.

Definition at line 476 of file utils.py.

00477                            :
00478     """Returns unique relvaldata ID for a given file."""
00479     run_id = re.search('R\d{9}', file)
00480     run = re.search('_RelVal_([\w\d]*)-v\d__', file)
00481     if not run:
00482         run = re.search('GR_R_\d*_V\d*C?_([\w\d]*)-v\d__', file)
00483     if run_id and run:
00484         return (run_id.group(), run.group(1))
00485     return None

def utils::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 504 of file utils.py.

00505                                      :
00506     """Returns file with maximum version at a) beggining of the file,
00507     e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
00508     max_file = files[0]
00509     max_v = get_relvaldata_version(files[0])
00510     for file in files:
00511         file_v = get_relvaldata_version(file)
00512         if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
00513             max_file = file
00514             max_v = file_v
00515     return max_file

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

Definition at line 495 of file utils.py.

00496                                 :
00497     """Returns tuple (CMSSW version, run version) for specified file."""
00498     cmssw_version = re.findall('DQM_V(\d*)_', file)
00499     run_version = re.findall('_RelVal_[\w\d]*-v(\d)__', file)
00500     if not run_version:
00501         run_version = re.findall('GR_R_\d*_V\d*C?_[\w\d]*-v(\d)__', file)
00502     if cmssw_version and run_version:
00503         return (int(cmssw_version[0]), int(run_version[0]))

def utils::getNbins (   h)

Definition at line 92 of file utils.py.

00093                :
00094   biny=h.GetNbinsY()
00095   if biny>1:biny+=1
00096   binz=h.GetNbinsZ()
00097   if binz>1:binz+=1
00098   return (h.GetNbinsX()+1)*(biny)*(binz)
00099 
00100 #-------------------------------------------------------------------------------
00101 

def utils::is_empty (   h)

Definition at line 171 of file utils.py.

Referenced by DTSectColl::addTSTheta(), DEcompare< T >::get_ncand(), and DEcompare< T >::SortCollections().

00172                :
00173   for i in xrange(1,getNbins(h)):
00174     if h.GetBinContent(i)!=0: return False
00175   return True
00176   #return h.GetSumOfWeights()==0
00177 
00178 #-------------------------------------------------------------------------------

def utils::is_relvaldata (   files)

------------------------- Make files pairs --------------------------

Definition at line 548 of file utils.py.

00549                         :
00550     is_relvaldata_re = re.compile('_RelVal_')
00551     return any([is_relvaldata_re.search(filename) for filename in files])

def utils::is_sparse (   h)

Definition at line 179 of file utils.py.

00180                 :
00181   filled_bins=0.
00182   nbins=h.GetNbinsX()
00183   for ibin in xrange(nbins):
00184     if h.GetBinContent(ibin)>0:
00185       filled_bins+=1
00186   #print "%s %s --> %s" %(filled_bins,nbins,filled_bins/nbins)
00187   if filled_bins/nbins < .5:
00188     return True
00189   else:
00190     return False
00191 
00192 #-------------------------------------------------------------------------------

def utils::literal2root (   literal,
  rootType 
)

Definition at line 69 of file utils.py.

00070                                    :
00071   bitsarray = array.array('B')
00072   bitsarray.fromstring(literal.decode('hex'))
00073 
00074   tbuffer=0
00075   try:  
00076       tbuffer = TBufferFile(TBufferFile.kRead, len(bitsarray), bitsarray, False,0)
00077   except:
00078       print "could not transform to object array:"
00079       print [ i for i in  bitsarray ]
00080   
00081   # replace a couple of shortcuts with the real root class name
00082   if rootType == 'TPROF':
00083       rootType = 'TProfile'
00084   if rootType == 'TPROF2D':
00085       rootType = 'TProfile2D'
00086   
00087   root_class=eval(rootType+'.Class()')
00088   
00089   return tbuffer.ReadObject(root_class)
00090   
00091 #-------------------------------------------------------------------------------

def utils::logger (   msg_level,
  message 
)

Definition at line 47 of file utils.py.

00048                              :
00049   if msg_level>=_log_level:
00050     print "[%s] %s" %(asctime(),message)
00051 
#-------------------------------------------------------------------------------
def utils::make_files_pairs (   files,
  verbose = True 
)

Definition at line 552 of file utils.py.

00553                                          :
00554     ## Select functions to use
00555     if is_relvaldata(files):
00556         is_relval_data = True
00557         get_cmssw_version = get_relvaldata_cmssw_version
00558         get_id = get_relvaldata_id
00559         get_max_version = get_relvaldata_max_version
00560         # print 'Pairing Data RelVal files.'
00561     else:
00562         is_relval_data = False
00563         get_cmssw_version = get_relval_cmssw_version
00564         get_id = get_relval_id
00565         get_max_version = get_relval_max_version
00566         # print 'Pairing Monte Carlo RelVal files.'
00567 
00568     ## Divide files into groups
00569     versions_files = dict()
00570     for file in files:
00571         version = get_cmssw_version(file)
00572         if versions_files.has_key(version):
00573             versions_files[version].append(file)
00574         else:
00575             versions_files[version] = [file]
00576 
00577     ## Print the division into groups
00578     if verbose:
00579         print '\nFound versions:'
00580         for version in versions_files:
00581             print '%s: %d files' % (str(version),  len(versions_files[version]))
00582 
00583     if len(versions_files.keys()) <= 1:
00584         print '\nFound too little versions, there is nothing to pair. Exiting...\n'
00585         exit()
00586 
00587     ## Select two biggest groups.
00588     versions = versions_files.keys()
00589     sizes = [len(value) for value in versions_files.values()]
00590     v1 = versions[sizes.index(max(sizes))]
00591     versions.remove(v1)
00592     sizes.remove(max(sizes))
00593     v2 = versions[sizes.index(max(sizes))]
00594 
00595     ## Print two biggest groups.
00596     if verbose:
00597         print '\nPairing %s (%d files) and %s (%d files)' % (str(v1),
00598                 len(versions_files[v1]), str(v2), len(versions_files[v2]))
00599 
00600     ## Pairing two versions
00601     print '\nGot pairs:'
00602     pairs = []
00603     for unique_id in set([get_id(file) for file in versions_files[v1]]):
00604         if is_relval_data:
00605             dataset_re = re.compile(unique_id[0]+'_')
00606             run_re = re.compile(unique_id[1])
00607             c1_files = [file for file in versions_files[v1] if dataset_re.search(file) and run_re.search(file)]
00608             c2_files = [file for file in versions_files[v2] if dataset_re.search(file) and run_re.search(file)]
00609         else:
00610             dataset_re = re.compile(unique_id+'_')
00611             c1_files = [file for file in versions_files[v1] if dataset_re.search(file)]
00612             c2_files = [file for file in versions_files[v2] if dataset_re.search(file)]
00613 
00614         if len(c1_files) > 0 and len(c2_files) > 0:
00615             first_file = get_max_version(c1_files)
00616             second_file = get_max_version(c2_files)
00617             print '%s\n%s\n' % (first_file, second_file)
00618             pairs.extend((first_file, second_file))
00619     if verbose:
00620         print "Paired and got %d files.\n" % len(pairs)
00621     return pairs
def utils::parse_word (   word)

Definition at line 21 of file utils.py.

00022                     :
00023     "Parse word which contas double underscore tag"
00024     output = set()
00025     words  = word.split()
00026     for idx in xrange(0, len(words)):
00027         pat = words[idx]
00028         if  pat and len(pat) > 4 and pat[:2] == '__': # we found enclosure
00029             tag = pat[2:pat.rfind('__')]
00030             if  tag.find('__') != -1: # another pattern
00031                 for item in tag.split('__'):
00032                     if  TAG.match(item):
00033                         output.add('__%s__' % item)
00034             else:
00035                 output.add('__%s__' % tag)
00036     return output

def utils::profile2histo (   profile)

Definition at line 220 of file utils.py.

00221                           :
00222   if not profile.InheritsFrom("TH1"):
00223     return profile
00224     
00225   bin_low_edges=[]
00226   n_bins=profile.GetNbinsX()
00227   
00228   for ibin in xrange(1,n_bins+2):
00229     bin_low_edges.append(profile.GetBinLowEdge(ibin))
00230   bin_low_edges=array.array('f',bin_low_edges)
00231   histo=TH1F(profile.GetName(),profile.GetTitle(),n_bins,bin_low_edges)
00232   for ibin in xrange(0,n_bins+1):
00233     histo.SetBinContent(ibin,profile.GetBinContent(ibin))
00234     histo.SetBinError(ibin,profile.GetBinError(ibin))    
00235   
00236   return histo
00237 #-------------------------------------------------------------------------------

def utils::setTDRStyle ( )

Definition at line 52 of file utils.py.

References plotscripts::tdrStyle.

Referenced by FlavourHistograms< T >::plot().

00052                  :  
00053   this_dir=dirname(this_module_name)
00054   this_dir_one_up=this_dir[:this_dir.rfind("/")+1]
00055   #this_dir_two_up=this_dir_one_up[:this_dir_one_up.rfind("/")+1]
00056   style_file=''
00057   if os.environ.has_key("RELMON_SA"):
00058     style_file=this_dir_one_up+"data/tdrstyle_mod.C"
00059   else:
00060     style_file="%s/src/Utilities/RelMon/data/tdrstyle_mod.C"%(os.environ["CMSSW_BASE"])
00061   try:
00062     gROOT.ProcessLine(".L %s" %style_file)
00063     gROOT.ProcessLine("setTDRStyle()")
00064   except:
00065     "Print could not set the TDR style. File %s not found?" %style_file
00066     
00067 
00068 #-------------------------------------------------------------------------------
def utils::test_env (   tdir,
  tmpl 
)
Test user environment, look-up if user has run cmsenv, otherwise
provide meaningful error message back to the user.

Definition at line 37 of file utils.py.

00038                         :
00039     """
00040     Test user environment, look-up if user has run cmsenv, otherwise
00041     provide meaningful error message back to the user.
00042     """
00043     if  not tdir or not os.path.isdir(tdir):
00044         print "Unable to access template dir: %s" % tdir
00045         sys.exit(1)
00046     if  not os.listdir(tdir):
00047         print "No template files found in template dir %s" % tdir
00048         sys.exit(0)
00049     if  not tmpl:
00050         msg  = "No template type is provided, "
00051         msg += "see available templates via --templates option"
00052         print msg
00053         sys.exit(1)

def utils::tree (   idir)

Definition at line 127 of file utils.py.

00128               :
00129     "Print directory content, similar to tree UNIX command"
00130     if  idir[-1] == '/':
00131         idir = idir[-1]
00132     dsep = ''
00133     fsep = ''
00134     dtot = -1 # we'll not count initial directory
00135     ftot = 0
00136     for root, dirs, files in os.walk(idir):
00137         dirs  = root.split('/')
00138         ndirs = len(dirs)
00139         if  ndirs > 1:
00140             dsep  = '|  '*(ndirs-1)
00141         print '%s%s/' % (dsep, dirs[-1])
00142         dtot += 1
00143         for fname in files:
00144             fsep = dsep + '|--'
00145             print '%s %s' % (fsep, fname)
00146             ftot += 1
00147     if  dtot == -1 or not dtot:
00148         dmsg = ''
00149     else:
00150         dmsg = '%s directories,' % dtot
00151     if  ftot:
00152         fmsg = '%s file' % ftot
00153         if  ftot > 1:
00154             fmsg += 's'
00155     else:
00156         fmsg = ''
00157     if  dmsg and fmsg:
00158         print "Total: %s %s" % (dmsg, fmsg)
00159     else:
00160         print "No directories/files in %s" % idir
def utils::user_info (   ainput = None)

Definition at line 96 of file utils.py.

Referenced by cond::userInfo().

00097                           :
00098     "Return user name and office location, based on UNIX finger"
00099     if  ainput:
00100         return ainput
00101     pwdstr = pwd.getpwnam(os.getlogin())
00102     author = pwdstr.pw_gecos
00103     if  author and isinstance(author, basestring):
00104         author = author.split(',')[0]
00105     return author

def utils::wget (   url)
Fetch the WHOLE file, not in bunches... To be optimised.

Definition at line 451 of file utils.py.

00452              :
00453   """ Fetch the WHOLE file, not in bunches... To be optimised.
00454   """
00455   opener=build_opener(X509CertOpen())  
00456   datareq = Request(url)
00457   datareq.add_header('authenticated_wget', "The ultimate wgetter")    
00458   bin_content=None
00459   try:
00460     filename=basename(url)  
00461     print "Checking existence of file %s on disk..."%filename
00462     if not isfile("./%s"%filename):      
00463       bin_content=opener.open(datareq).read()
00464     else:
00465       print "File %s exists, skipping.." %filename
00466   except ValueError:
00467     print "Error: Unknown url %s" %url
00468   
00469   if bin_content!=None:  
00470     ofile = open(filename, 'wb')
00471     ofile.write(bin_content)
00472     ofile.close()
00473 
#-------------------------------------------------------------------------------

Variable Documentation

Definition at line 46 of file utils.py.

tuple utils::TAG = re.compile(r'[a-zA-Z0-9]')

Definition at line 19 of file utils.py.

utils::theargv = sys.argv

Definition at line 24 of file utils.py.