ATLAS Offline Software
Classes | Functions | Variables
common Namespace Reference

Classes

class  CaloClusterCorrSetup
 
class  CaloCorrectionConfigError
 

Functions

def split_version_spec (cspec)
 Code to handle creating the list of correction tools. More...
 
def _find_version_from_cool_tag (flags, coolTag, corrclass)
 
def makecorr (flags, versions, name, basename, suffix, version, key, sampling, source, confclass, corrclass, generation='', order=0, **kw)
 
def _is_jo_source (s)
 
def _is_pool_source (s)
 
def _is_cool_source (s)
 
def _config_from_jo (corr, jo, key, sampling, valid_keys, order)
 
def _config_from_pool (flags, corr, poolfile, sgkey)
 
def _config_from_cool (flags, corr, folder, tag)
 
def _matchlen (a, b)
 
def _longest_match (key, valid_keys)
 
def _mung_prefix (corr, key, valid_keys)
 
def folder (tool)
 
def tag (tool)
 
def sgkey (tool)
 

Variables

dictionary _poolfiles_seen = {}
 
dictionary _folders_used = {}
 
dictionary _alltools = {}
 

Function Documentation

◆ _config_from_cool()

def common._config_from_cool (   flags,
  corr,
  folder,
  tag 
)
private

Definition at line 950 of file common.py.

950 def _config_from_cool (flags, corr, folder, tag):
951  # Folder name has form /CALO/CORRCLASS/NAME
952  # Find the subdetector name string to use.
953  fsplit = folder.split ('/')
954  if fsplit[2] == 'Ofl':
955  corrclass = folder.split ('/')[4]
956  else:
957  corrclass = folder.split ('/')[3]
958  sndict = flags.Calo.ClusterCorrection.dbSubdetName
959  subdetname = sndict.get (corrclass)
960  if not subdetname:
961  subdetname = sndict.get (None)
962 
963  corr.DBHandleKey = folder
964 
965  # We can't use more than one tag from a folder.
966  oldtag = _folders_used.get (folder)
967  if oldtag is not None and oldtag != tag:
968  return False
969 
970  ca = ComponentAccumulator()
971 
972  _folders_used[folder] = tag
973  if oldtag is None:
974  from IOVDbSvc.IOVDbSvcConfig import addFolders
975  tagstr = '' if tag =='@GLOBAL' else tag
976  sdsuffix = '_OFL' if 'Ofl' in folder else ''
977  ca.merge (addFolders (flags,
978  folder,
979  detDb = subdetname + sdsuffix,
980  className = 'CaloRec::ToolConstants',
981  tag = tagstr))
982 
983  log = logging.getLogger ('CaloClusterCorrection')
984  log.debug ("Adding cool folder `%s' for subdetector name %s" %
985  (folder, subdetname))
986  return ca
987 
988 
989 # When we're reading from pool/cool, the prefix _must_ be one that
990 # exists in the file. Here, we see if the key is on the list of known
991 # keys. If not, then we substitute.

◆ _config_from_jo()

def common._config_from_jo (   corr,
  jo,
  key,
  sampling,
  valid_keys,
  order 
)
private

Definition at line 862 of file common.py.

862 def _config_from_jo (corr, jo, key, sampling, valid_keys, order):
863  # Split off the last dotted field as paramclass.
864  xjo = jo.split ('.')
865  paramclass = xjo[-1]
866  modname = '.'.join (xjo[:-1])
867 
868  # If not otherwise specified, try to find the module
869  # in CaloClusterCorrection.
870  if modname.find ('.') < 0:
871  modname = 'CaloClusterCorrection.' + modname
872 
873  # Import the module and look up the class.
874  mod = __import__ (modname, globals(), locals(), [paramclass])
875  parms = getattr (mod, paramclass)
876 
877  # It may be sampling-dependent.
878  if isinstance (parms, dict) and sampling is not None:
879  parms = parms[sampling]
880 
881  if order != 0 and hasattr (corr, 'order'):
882  corr.order = order
883  if hasattr (corr, 'isDummy'):
884  corr.isDummy = 0
885 
886  log = logging.getLogger ('CaloClusterCorrection')
887 
888  # Go through the names in the class and assign them to the tool.
889  for (k, val) in parms.__dict__.items():
890  # Skip internal names.
891  if k[0] != '_':
892  try:
893  # First try looking up the key.
894  # Note: don't test on val's type here --- it may
895  # be an object instance that implements __getitem__.
896  # We have to try the lookup and catch the error.
897  val = val[key]
898  except KeyError:
899  # We can do a lookup, but didn't find the key.
900  # Use the default key instead.
901  default_keys = parms.__dict__.get ('_default_keys')
902  if default_keys:
903  defkey = _longest_match (key, default_keys)
904  elif valid_keys:
905  defkey = _longest_match (key, valid_keys)
906  else:
907  defkey = CALOCORR_DEFAULT_KEY
908  val = val[defkey]
909 
910  log.debug (" correction %s from JO using %s instead of %s" %
911  (corr.getName(), defkey, key))
912 
913  except TypeError:
914  # Can't look up a key in val --- just use val as-is.
915  pass
916 
917  setattr (corr, k, val)
918  return True
919 
920 
921 # Configure a correction tool from POOL.

◆ _config_from_pool()

def common._config_from_pool (   flags,
  corr,
  poolfile,
  sgkey 
)
private

Definition at line 922 of file common.py.

922 def _config_from_pool (flags, corr, poolfile, sgkey):
923  if not poolfile or not poolfiles[poolfile]:
924  return False
925 
926  ca = ComponentAccumulator()
927 
928  # If this is the first time we've seen this file,
929  # add it to CondProxyProvider.
930  if poolfile not in _poolfiles_seen:
931  from EventSelectorAthenaPool.CondProxyProviderConfig import CondProxyProviderCfg
932  ca.merge (CondProxyProviderCfg (flags, [poolfiles[poolfile]]))
933 
934  # Tell the tool to look in pool for this key.
935  corr.DBHandleKey = sgkey
936 
937  # Set up a conditions algorithm to convert from the data in DetectorStore
938  # to a conditions object.
939  ToolConstantsCondAlg = CompFactory.ToolConstantsCondAlg # CaloRec
940 
941  name = 'ToolConstantsCondAlg_' + sgkey.replace ('.', '_')
942  alg = ToolConstantsCondAlg (name,
943  DetStoreKey = sgkey,
944  ToolConstantsKey = sgkey)
945  ca.addCondAlgo (alg)
946  return ca
947 
948 
949 # Configure a correction tool from COOL.

◆ _find_version_from_cool_tag()

def common._find_version_from_cool_tag (   flags,
  coolTag,
  corrclass 
)
private

Definition at line 142 of file common.py.

142 def _find_version_from_cool_tag (flags, coolTag, corrclass):
143  if coolTag == 'GLOBAL':
144  coolTag = flags.IOVDb.GlobalTag
145 
146  if flags.Input.isMC:
147  folderset = "/CALO/Ofl/" + corrclass
148  connstring = 'COOLOFL_CALO/OFLP200'
149  else:
150  folderset = "/CALO/" + corrclass
151  connstring = 'COOLONL_CALO/' + flags.IOVDb.DatabaseInstance
152 
153  from CoolConvUtilities import AtlCoolLib
154  db = AtlCoolLib.indirectOpen (connstring, readOnly=True)
155  ff = db.getFolderSet (folderset)
156  t = ff.resolveTag (coolTag)
157  # CaloOflSwClusterCorrections.00-02-12-calhits-v9
158  l = t.split ('-')
159  return '-'.join (l[4:])
160 
161 

◆ _is_cool_source()

def common._is_cool_source (   s)
private

Definition at line 857 of file common.py.

857 def _is_cool_source (s):
858  return s == CALOCORR_COOL
859 
860 
861 # Configure a correction tool from job options.

◆ _is_jo_source()

def common._is_jo_source (   s)
private

Definition at line 843 of file common.py.

843 def _is_jo_source (s):
844  if s == CALOCORR_COOL: return 0
845  if s == CALOCORR_POOL: return 0
846  if s in poolfiles: return 0
847  if s.find ('.') > 0: return 1
848  return 0
849 
850 
851 # Test to see if S looks like a pool source.

◆ _is_pool_source()

def common._is_pool_source (   s)
private

Definition at line 852 of file common.py.

852 def _is_pool_source (s):
853  return s in poolfiles
854 
855 
856 # Test to see if S looks like a cool source.

◆ _longest_match()

def common._longest_match (   key,
  valid_keys 
)
private

Definition at line 997 of file common.py.

997 def _longest_match (key, valid_keys):
998  if not isinstance (valid_keys, list):
999  valid_keys = [valid_keys]
1000  if valid_keys is None or key in valid_keys:
1001  return key
1002  new_key = valid_keys[0]
1003  for k in valid_keys[1:]:
1004  if _matchlen (key, k) > _matchlen (key, new_key):
1005  new_key = k
1006  return new_key

◆ _matchlen()

def common._matchlen (   a,
  b 
)
private

Definition at line 992 of file common.py.

992 def _matchlen (a, b):
993  i = 0
994  while i < len(a) and i < len(b) and a[i] == b[i]:
995  i = i+1
996  return i

◆ _mung_prefix()

def common._mung_prefix (   corr,
  key,
  valid_keys 
)
private

Definition at line 1007 of file common.py.

1007 def _mung_prefix (corr, key, valid_keys):
1008  if valid_keys is None or key in valid_keys: return
1009 
1010  # Find the best match.
1011  new_key = _longest_match (key, valid_keys)
1012 
1013  new_prefix = new_key + corr.prefix[len(key):]
1014  corr.prefix = new_prefix
1015  return
1016 
1017 # Return the folder for a tool.

◆ folder()

def common.folder (   tool)

Definition at line 1018 of file common.py.

1018 def folder (tool):
1019  return _alltools[tool.getName()][0]
1020 
1021 
1022 # Return the tag for a tool.

◆ makecorr()

def common.makecorr (   flags,
  versions,
  name,
  basename,
  suffix,
  version,
  key,
  sampling,
  source,
  confclass,
  corrclass,
  generation = '',
  order = 0,
**  kw 
)

Definition at line 645 of file common.py.

645 def makecorr (flags,
646  versions,
647  name,
648  basename,
649  suffix,
650  version,
651  key,
652  sampling,
653  source,
654  confclass,
655  corrclass,
656  generation = '',
657  order = 0,
658  **kw):
659 
660  # If no version specified, use the last one in the table.
661  if version is None:
662  version = versions[-1][0]
663 
664  # Try to find the requested version.
665  for v in versions:
666  if v[0] == version:
667  break
668  else:
669  if version.startswith ('@'):
670  # A COOL tag is requested.
671  # Look for a table entry of `@'; otherwise, use the last entry.
672  for v in versions:
673  if v[0] == '@':
674  break
675  else:
676  v = versions[-1]
677  else:
678  raise CaloCorrectionConfigError \
679  ("Can't find version `%s' for correction named `%s'." %
680  (version, basename))
681 
682  # The valid keys.
683  valid_keys = None
684  if len (v) >= 4:
685  valid_keys = v[3]
686 
687  # Handle @VALID_KEYS request.
688  if key == '@VALID_KEYS':
689  return valid_keys
690 
691  # Use the default source if one wasn't specified.
692  if source is None:
693  source = flags.Calo.ClusterCorrection.defaultSource
694 
695  # Test to see if this correction specifies nopool.
696  nopool = CALOCORR_NOPOOL in v[2]
697 
698  # Don't try to write a nopool tool to pool.
699  if nopool and source == CALOCORR_TOPOOL:
700  return None
701 
702  # Find the SG key and cool tag.
703  if version.startswith ('@'):
704  sgkey = None
705  if version == '@GLOBAL':
706  fulltag = version
707  else:
708  fulltag = version[1:]
709 
710  if not nopool:
711  # Must read from cool in this case.
712  source = CALOCORR_COOL
713  else:
714  # Construct the SG key name.
715  tmp = basename
716  if version != '':
717  tmp = tmp + "-" + version
718  sgkey = "%s.%s" % (corrclass, tmp)
719  fulltag = "%s.%s%s" % (corrclass, generation, tmp)
720  if flags.Input.isMC:
721  fulltag = fulltag[0:4] + 'Ofl' + fulltag[4:]
722 
723  # The cool folder name.
724  if not flags.Input.isMC:
725  folder = "/CALO/%s/%s" % (corrclass, basename)
726  else:
727  folder = "/CALO/Ofl/%s/%s" % (corrclass, basename)
728 
729  # Prefix to locate constants in the ToolConstants block.
730  prefix = key + sampnames[sampling] + "."
731 
732  # Construct a default tool name if one isn't specified.
733  if name is None:
734  name = basename
735  name = name + sampnames[sampling]
736  if version != '':
737  vv = version
738  if vv[0] == '@':
739  vv = vv[1:]
740  vv = vv.split('-')[-1]
741  name = name + "_" + vv
742  if key != '':
743  name = name + "_" + key
744 
745  # Add a suffix if given.
746  if suffix is not None:
747  name = name + suffix
748 
749  # If this tool was defined earlier, check consistency.
750  if name in _alltools:
751  if _alltools[name] != (folder, fulltag, sgkey):
752  raise CaloCorrectionConfigError ( #pragma: NO COVER
753  "Inconsistent configuration of tool %s. Old (folder,tag,sgkey)=%s; new=%s" % #pragma: NO COVER
754  (name, _alltools[name], (folder, fulltag, sgkey))) #pragma: NO COVER
755  else:
756  _alltools[name] = (folder, fulltag, sgkey)
757 
758  # If no class was explicitly specified, take it from the table.
759  if confclass is None:
760  confclass = v[1]
761 
762  # It may be sampling-dependent.
763  if isinstance (confclass, dict) and sampling is not None:
764  confclass = confclass[sampling]
765 
766  result = ComponentAccumulator()
767 
768  # Create the tool!
769  corr = confclass (name)
770 
771  # Set the prefix for all pool-capable tools.
772  if not nopool:
773  corr.prefix = prefix
774 
775  # Try to find a source from which to configure it.
776  if not isinstance (source, list):
777  source = [source]
778  avail = v[2]
779  wherefrom = None
780  for s in source:
781  if s == CALOCORR_JO or s == CALOCORR_TOPOOL:
782  sel = [x for x in avail if _is_jo_source (x)]
783  if len (sel) > 0 and _config_from_jo(corr, sel[0], key,
784  sampling , valid_keys,
785  order):
786  wherefrom = sel[0]
787  break
788 
789  elif s == CALOCORR_POOL:
790  sel = [x for x in avail if _is_pool_source (x)]
791  if len (sel) > 0:
792  ca2 = _config_from_pool (flags, corr, sel[0], sgkey)
793  if ca2:
794  result.merge (ca2)
795  _mung_prefix (corr, key, valid_keys)
796  wherefrom = sel[0]
797  break
798 
799  elif s == CALOCORR_COOL:
800  sel = [x for x in avail if _is_cool_source (x)]
801  if len (sel) > 0:
802  ca2 = _config_from_cool (flags, corr, folder, fulltag)
803  if ca2:
804  result.merge (ca2)
805  _mung_prefix (corr, key, valid_keys)
806  wherefrom = 'cool'
807  break
808 
809  elif _is_jo_source (s):
810  if _config_from_jo (corr, s, key, sampling, valid_keys, order):
811  wherefrom = s
812  break
813 
814  elif _is_pool_source (s):
815  ca2 = _config_from_pool (flags, corr, s, sgkey)
816  if ca2:
817  result.merge (ca2)
818  _mung_prefix (corr, key, valid_keys)
819  wherefrom = s
820  break
821 
822  if wherefrom is None:
823  raise CaloCorrectionConfigError \
824  ("Can't find any source to configure tool `%s'. Sources: %s" %
825  (name, source))
826 
827  log = logging.getLogger ('CaloClusterCorrection')
828  log.debug (" correction %s (%s, from %s)" % (name, confclass.__name__,
829  wherefrom))
830 
831  # If any other keyword arguments were passed, make those assignments.
832  # This will override anything otherwise read from JO/pool.
833  for (k, val) in kw.items():
834  if val is not None:
835  setattr (corr, k, val)
836 
837  # Done!
838  result.setPrivateTools (corr)
839  return result
840 
841 
842 # Test to see if S looks like a job options source.

◆ sgkey()

def common.sgkey (   tool)

Definition at line 1028 of file common.py.

1028 def sgkey (tool):
1029  return _alltools[tool.getName()][2]
1030 
1031 
1032 

◆ split_version_spec()

def common.split_version_spec (   cspec)

Code to handle creating the list of correction tools.

The common code is in the class CaloClusterCorrSetup. You are expected to derive from it for your cluster type, and then create an instance. The following class variables must be defined in the derived class: name: A short descriptive name of the type of cluster (to be written to the log file)

version_override_flag_name: Name of flag. If set, it overrides the selected correction version.

correction_generation_flag_name: Name of flag. If set, this gives the correction generation string that's embedded in tags written to the database. This string should be changed for each write to the database.

correction_generation_default: Default value for the generation string, if the job property isn't set.

versions: The table of all known correction versions. Should be a dictionary; the keys are correction version names, and the values are correction lists. A correction list is a list of individual corrections:

[[CORR], [CORR], ...]

where each individual correction here is also a list:

[CORRFUNC, [VERSION,] [ORDER,] ARGS...]

CORRFUNC is a function to call to create the correction; it should have the signature:

def CORRFUNC (cells_name, suffix, version, key, source, **kw):

VERSION is a string giving the correction version. It may be omitted, and defaults to an empty string. It may also be ‘&rsquo;, in which case the global version string is used instead.

ORDER is an integer, giving the order in which the tools are intended to run. Not currently used (they were used by the metatool), but retained in case we want to use this for checking.

The remainder of the list should be a set of (NAME,VALUE) tuples giving correction parameters to override. This may be (and usually should be) empty.

A key of ‘&rsquo; is used if the global correction version is tag-based (starts with ‘&rsquo;).

geom_versions: The mapping between geometry versions and corrections versions. A list of lists, each of which has the form

[ GEOM-PATTERN, CORRECTION-VERSION ]

Here, GEOM-PATTERN is a string that's matched against the geometry version string. It may contain glob-style wildcards (as implemented by fnmatch). CORRECTION-VERSION is a string giving the correction version to use. The patterns are tried in order; first to match wins.

newest_version: The name of the newest version. This will be used if there's no match for the geometry string.

Definition at line 125 of file common.py.

125 def split_version_spec (cspec):
126  func = cspec[0]
127  version = ''
128  order = 0
129  ii = 1
130  if ii < len(cspec) and isinstance (cspec[ii], str):
131  version = cspec[ii]
132  ii += 1
133  if ii < len(cspec) and isinstance (cspec[ii], int):
134  order = cspec[ii]
135  ii += 1
136  extra_args = cspec[ii:]
137  return (func, version, order, extra_args)
138 
139 
140 # Given a cool tag (or GLOBAL for the currently-configured global tag),
141 # return the version of the correction set to use.

◆ tag()

def common.tag (   tool)

Definition at line 1023 of file common.py.

1023 def tag (tool):
1024  return _alltools[tool.getName()][1]
1025 
1026 
1027 # Return the SG key for a tool.

Variable Documentation

◆ _alltools

dictionary common._alltools = {}
private

Definition at line 35 of file common.py.

◆ _folders_used

dictionary common._folders_used = {}
private

Definition at line 31 of file common.py.

◆ _poolfiles_seen

dictionary common._poolfiles_seen = {}
private

Definition at line 28 of file common.py.

common.makecorr
def makecorr(flags, versions, name, basename, suffix, version, key, sampling, source, confclass, corrclass, generation='', order=0, **kw)
Definition: common.py:645
common.sgkey
def sgkey(tool)
Definition: common.py:1028
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
common._config_from_pool
def _config_from_pool(flags, corr, poolfile, sgkey)
Definition: common.py:922
common._longest_match
def _longest_match(key, valid_keys)
Definition: common.py:997
common._mung_prefix
def _mung_prefix(corr, key, valid_keys)
Definition: common.py:1007
common.split_version_spec
def split_version_spec(cspec)
Code to handle creating the list of correction tools.
Definition: common.py:125
common._find_version_from_cool_tag
def _find_version_from_cool_tag(flags, coolTag, corrclass)
Definition: common.py:142
common._config_from_jo
def _config_from_jo(corr, jo, key, sampling, valid_keys, order)
Definition: common.py:862
common._is_jo_source
def _is_jo_source(s)
Definition: common.py:843
common._is_pool_source
def _is_pool_source(s)
Definition: common.py:852
common._matchlen
def _matchlen(a, b)
Definition: common.py:992
common.tag
def tag(tool)
Definition: common.py:1023
common._config_from_cool
def _config_from_cool(flags, corr, folder, tag)
Definition: common.py:950
common.folder
def folder(tool)
Definition: common.py:1018
common._is_cool_source
def _is_cool_source(s)
Definition: common.py:857