ATLAS Offline Software
Loading...
Searching...
No Matches
common Namespace Reference

Classes

class  CaloClusterCorrSetup
class  CaloCorrectionConfigError

Functions

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

Variables

dict _poolfiles_seen = {}
dict _folders_used = {}
dict _alltools = {}

Function Documentation

◆ _config_from_cool()

common._config_from_cool ( flags,
corr,
folder,
tag )
protected

Definition at line 949 of file common.py.

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

◆ _config_from_jo()

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

Definition at line 861 of file common.py.

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

◆ _config_from_pool()

common._config_from_pool ( flags,
corr,
poolfile,
sgkey )
protected

Definition at line 921 of file common.py.

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

◆ _find_version_from_cool_tag()

common._find_version_from_cool_tag ( flags,
coolTag,
corrclass )
protected

Definition at line 141 of file common.py.

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

◆ _is_cool_source()

common._is_cool_source ( s)
protected

Definition at line 856 of file common.py.

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

◆ _is_jo_source()

common._is_jo_source ( s)
protected

Definition at line 842 of file common.py.

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

◆ _is_pool_source()

common._is_pool_source ( s)
protected

Definition at line 851 of file common.py.

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

◆ _longest_match()

common._longest_match ( key,
valid_keys )
protected

Definition at line 996 of file common.py.

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

◆ _matchlen()

common._matchlen ( a,
b )
protected

Definition at line 991 of file common.py.

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

◆ _mung_prefix()

common._mung_prefix ( corr,
key,
valid_keys )
protected

Definition at line 1006 of file common.py.

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

◆ folder()

common.folder ( tool)

Definition at line 1017 of file common.py.

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

◆ makecorr()

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

Definition at line 644 of file common.py.

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

◆ sgkey()

common.sgkey ( tool)

Definition at line 1027 of file common.py.

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

◆ split_version_spec()

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 124 of file common.py.

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

◆ tag()

common.tag ( tool)

Definition at line 1022 of file common.py.

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

Variable Documentation

◆ _alltools

dict common._alltools = {}
protected

Definition at line 34 of file common.py.

◆ _folders_used

dict common._folders_used = {}
protected

Definition at line 30 of file common.py.

◆ _poolfiles_seen

dict common._poolfiles_seen = {}
protected

Definition at line 27 of file common.py.