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 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 profile2histo
def setTDRStyle
def wget

Variables

int _log_level = 10
 theargv = sys.argv

Function Documentation

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

Definition at line 424 of file utils.py.

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

def utils::get_relval_cmssw_version (   file)

Definition at line 537 of file utils.py.

00538                                   :
00539     cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
00540     gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
00541     if cmssw_release and gr_r_version:
00542         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 543 of file utils.py.

00544                        :
00545     """Returns unique relval ID (dataset name) for a given file."""
00546     dataset_name = re.findall('R\d{9}__([\w\d]*)__CMSSW_', file)
00547     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 525 of file utils.py.

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

00519                             :
00520     """Returns tuple (CMSSW version, run version) for specified file."""
00521     cmssw_version = re.findall('DQM_V(\d*)_', file)
00522     run_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-[\w\d]*_V\d*\w?(?:_[\w\d]*)?-v(\d*)__', file)
00523     if cmssw_version and run_version:
00524         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 487 of file utils.py.

00488                                       :
00489     """Returns tuple (CMSSW release, GR_R version) for specified RelValData file."""
00490     cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
00491     gr_r_version = re.findall('-(GR_R_\d*_V\d*\w?)(?:_RelVal)?_', file)
00492     if not gr_r_version:
00493         gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-(\w*)_RelVal_', file)
00494     if cmssw_release and gr_r_version:
00495         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 477 of file utils.py.

00478                            :
00479     """Returns unique relvaldata ID for a given file."""
00480     run_id = re.search('R\d{9}', file)
00481     run = re.search('_RelVal_([\w\d]*)-v\d__', file)
00482     if not run:
00483         run = re.search('GR_R_\d*_V\d*C?_([\w\d]*)-v\d__', file)
00484     if run_id and run:
00485         return (run_id.group(), run.group(1))
00486     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 505 of file utils.py.

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

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

Definition at line 496 of file utils.py.

00497                                 :
00498     """Returns tuple (CMSSW version, run version) for specified file."""
00499     cmssw_version = re.findall('DQM_V(\d*)_', file)
00500     run_version = re.findall('_RelVal_[\w\d]*-v(\d)__', file)
00501     if not run_version:
00502         run_version = re.findall('GR_R_\d*_V\d*C?_[\w\d]*-v(\d)__', file)
00503     if cmssw_version and run_version:
00504         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 549 of file utils.py.

00550                         :
00551     is_relvaldata_re = re.compile('_RelVal_')
00552     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 553 of file utils.py.

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

Definition at line 452 of file utils.py.

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

Variable Documentation

Definition at line 46 of file utils.py.

utils::theargv = sys.argv

Definition at line 24 of file utils.py.