9 from __future__
import print_function
11 from AthenaConfiguration.ComponentAccumulator
import ComponentAccumulator
12 from AthenaConfiguration.ComponentFactory
import CompFactory
13 from AthenaCommon.Logging
import logging
14 from fnmatch
import fnmatchcase
16 from CaloClusterCorrection.poolfiles
import poolfiles
18 from CaloClusterCorrection.constants
import \
19 CALOCORR_JO, CALOCORR_POOL, CALOCORR_COOL, \
20 CALOCORR_NOPOOL, CALOCORR_TOPOOL, CALOCORR_DEFAULT_KEY, \
130 if ii < len(cspec)
and isinstance (cspec[ii], str):
133 if ii < len(cspec)
and isinstance (cspec[ii], int):
136 extra_args = cspec[ii:]
137 return (func, version, order, extra_args)
143 if coolTag ==
'GLOBAL':
144 coolTag = flags.IOVDb.GlobalTag
147 folderset =
"/CALO/Ofl/" + corrclass
148 connstring =
'COOLOFL_CALO/OFLP200'
150 folderset =
"/CALO/" + corrclass
151 connstring =
'COOLONL_CALO/' + flags.IOVDb.DatabaseInstance
153 from CoolConvUtilities
import AtlCoolLib
154 db = AtlCoolLib.indirectOpen (connstring, readOnly=
True)
155 ff = db.getFolderSet (folderset)
156 t = ff.resolveTag (coolTag)
159 return '-'.join (l[4:])
203 log = logging.getLogger (
'CaloClusterCorrection')
212 v = getattr (flags.Calo.ClusterCorrection, self.version_override_flag_name)
220 geom = flags.GeoModel.AtlasVersion
221 datasource =
'geant4' if flags.Input.isMC
else 'data'
222 (version, tryhier) = self.
geom_match (datasource, geom)
229 source = flags.Calo.ClusterCorrection.defaultSource
231 if not isinstance (source, list):
233 if CALOCORR_COOL
not in source:
236 if tryhier
and version[0] !=
'@':
239 cl = cl[0:4] +
'Ofl' + cl[4:]
240 version =
"@%s-%s%s" % (cl, generation, version)
245 if version[0] !=
'@' and len(source) > 0:
246 if CALOCORR_COOL
in source:
247 source.remove (CALOCORR_COOL)
249 (vcorrlist, version) = self.
lookup_version (flags, version, corrclass)
255 log.info (
"%s corrections for %s (%s) using version %s" %
256 (self.name, key, suffix, version))
261 for cspec
in corrlist:
262 (func, this_version, this_order, extra_args) = \
263 split_version_spec (cspec)
267 if this_version ==
'' or extra_args == []:
268 for vcspec
in vcorrlist:
269 if vcspec[0] == func:
270 (vfunc, vversion, vorder, vextra_args) = \
271 split_version_spec (vcspec)
272 if this_version ==
'':
273 this_version = vversion
275 extra_args = vextra_args
280 elif version[0] ==
'@' and not corrlist:
281 this_version = version
283 if this_version ==
'@':
284 this_version = version
287 this_args = dict (extra_args)
290 this_cells_name = cells_name
293 corrname = func.__name__
296 for (k, v)
in kw.items():
297 if k == corrname +
'_suffix':
299 elif k == corrname +
'_key':
301 elif k == corrname +
'_cells_name':
303 elif k == corrname +
'_source':
305 elif k == corrname +
'_version':
307 elif k == corrname +
'_order':
309 elif k.startswith (corrname +
'_'):
310 this_args[k[len(corrname)+1:]] = v
319 generation = generation,
322 tools.append (out.popToolsAndMerge (ca))
324 out.setPrivateTools (tools)
332 vcorrlist = self.versions.get (version)
333 if vcorrlist
is None and version.startswith (
'@'):
334 findvers = _find_version_from_cool_tag (flags, version[1:], corrclass)
335 vcorrlist = self.versions.get (findvers)
336 if vcorrlist
is None:
337 vcorrlist = self.versions.get (
'@')
338 if vcorrlist
is None:
339 raise CaloCorrectionConfigError\
340 (
"Can't find global correction version %s." % version)
341 return (vcorrlist, version)
346 for (pat, v)
in self.geom_versions:
347 if fnmatchcase (geom, pat):
351 if not geom: geom =
''
356 vv = self.newest_version
358 if vv.startswith (
'NOHIER#'):
367 generation = cls.correction_generation_default
368 gen2 = getattr (flags.Calo.ClusterCorrection, cls.correction_generation_flag_name)
372 generation = generation +
'-'
395 for (v,l)
in self.versions.
items():
396 if v.startswith (
'@'):
continue
398 last_funcname =
'(none)'
401 (func, version, order, extra_args) = split_version_spec (c)
402 if order <= last_order:
403 raise CaloCorrectionConfigError\
404 (
"Tools for global version %s are not in order!"
405 " %d(%s) < %d(%s)" % (v, order, func.__name__,
406 last_order, last_funcname))
408 last_funcname = func.__name__
412 okey = (func, version)
413 if okey
not in ordermap:
414 ordermap[okey] = (order, v)
415 elif ordermap[okey][0] != order:
416 raise CaloCorrectionConfigError\
417 (
"Tool %s has order %d in %s but order %d in %s!" %
418 (func.__name__, order, v,
419 ordermap[okey][0], ordermap[okey][1]))
422 valid_keys = func (flags,
426 if valid_keys
is None:
428 for key
in valid_keys:
435 tools.append (ca.popPrivateTools())
438 result.setPrivateTools (tools)
440 return (sgkeys, result)
444 sgkeys, tool_names, key, cspec,
446 (func, version, order, extra_args) = split_version_spec (cspec)
447 name = func.__name__ +
'_' + version +
'_' + key
448 if name
in tool_names:
451 None,
'', version, key, CALOCORR_TOPOOL,
456 tool = ca.popPrivateTools()
457 if tool
is not None and folder(tool).find (
'/' + corrclass +
'/') >= 0:
458 tool_names.add (name)
459 sgkeys.setdefault (
sgkey(tool), []).append (tool)
460 ca.setPrivateTools (tool)
466 CaloDummyCorrection = CompFactory.CaloDummyCorrection
468 for sgkey
in sgkeys.keys():
469 ll = sgkey.split (
'-')
475 for name
in toolnames.keys():
476 (corrclass, basename) = name.split (
'.')
477 sgkey = name +
'-dummy'
478 tagname =
"%s.%s%s-dummy" % (corrclass, generation, basename)
479 folder =
'/CALO/%s/%s' % (corrclass, basename)
481 for key
in valid_keys:
482 toolname = basename + key +
'-dummy'
483 tool = CaloDummyCorrection (toolname)
484 tool.prefix = key +
'.'
485 sgkeys[sgkey].append (tool)
486 _alltools[toolname] = (folder, tagname, sgkey)
494 corr_output_list = []
499 (sgkeys, ca) = self.
make_ForPool (flags, keys, corrclass)
504 for (sgkey, tools)
in sgkeys.items():
505 if len (tools) == 0:
continue
506 name =
'write_' + sgkey
507 name = name.replace (
'.',
'_')
510 alg = CompFactory.CaloClusterCorrDBWriter (name,
512 ClusterCorrectionTools = tools)
513 result.addEventAlgo (alg)
516 corr_output_list.append (
'CaloRec::ToolConstants#' +
519 tag_list.append (tag (tools[0]))
521 print (
"Final OutputList:")
522 print (corr_output_list)
525 return (corr_output_list, tag_list, result)
662 version = versions[-1][0]
669 if version.startswith (
'@'):
678 raise CaloCorrectionConfigError \
679 (
"Can't find version `%s' for correction named `%s'." %
688 if key ==
'@VALID_KEYS':
693 source = flags.Calo.ClusterCorrection.defaultSource
696 nopool = CALOCORR_NOPOOL
in v[2]
699 if nopool
and source == CALOCORR_TOPOOL:
703 if version.startswith (
'@'):
705 if version ==
'@GLOBAL':
708 fulltag = version[1:]
712 source = CALOCORR_COOL
717 tmp = tmp +
"-" + version
718 sgkey =
"%s.%s" % (corrclass, tmp)
719 fulltag =
"%s.%s%s" % (corrclass, generation, tmp)
721 fulltag = fulltag[0:4] +
'Ofl' + fulltag[4:]
724 if not flags.Input.isMC:
725 folder =
"/CALO/%s/%s" % (corrclass, basename)
727 folder =
"/CALO/Ofl/%s/%s" % (corrclass, basename)
730 prefix = key + sampnames[sampling] +
"."
735 name = name + sampnames[sampling]
740 vv = vv.split(
'-')[-1]
741 name = name +
"_" + vv
743 name = name +
"_" + key
746 if suffix
is not None:
750 if name
in _alltools:
751 if _alltools[name] != (folder, fulltag, sgkey):
752 raise CaloCorrectionConfigError (
753 "Inconsistent configuration of tool %s. Old (folder,tag,sgkey)=%s; new=%s" %
754 (name, _alltools[name], (folder, fulltag, sgkey)))
756 _alltools[name] = (folder, fulltag, sgkey)
759 if confclass
is None:
763 if isinstance (confclass, dict)
and sampling
is not None:
764 confclass = confclass[sampling]
769 corr = confclass (name)
776 if not isinstance (source, list):
781 if s == CALOCORR_JO
or s == CALOCORR_TOPOOL:
782 sel = [x
for x
in avail
if _is_jo_source (x)]
784 sampling , valid_keys,
789 elif s == CALOCORR_POOL:
790 sel = [x
for x
in avail
if _is_pool_source (x)]
792 ca2 = _config_from_pool (flags, corr, sel[0], sgkey)
795 _mung_prefix (corr, key, valid_keys)
799 elif s == CALOCORR_COOL:
800 sel = [x
for x
in avail
if _is_cool_source (x)]
802 ca2 = _config_from_cool (flags, corr, folder, fulltag)
805 _mung_prefix (corr, key, valid_keys)
809 elif _is_jo_source (s):
810 if _config_from_jo (corr, s, key, sampling, valid_keys, order):
814 elif _is_pool_source (s):
815 ca2 = _config_from_pool (flags, corr, s, sgkey)
818 _mung_prefix (corr, key, valid_keys)
822 if wherefrom
is None:
823 raise CaloCorrectionConfigError \
824 (
"Can't find any source to configure tool `%s'. Sources: %s" %
827 log = logging.getLogger (
'CaloClusterCorrection')
828 log.debug (
" correction %s (%s, from %s)" % (name, confclass.__name__,
833 for (k, val)
in kw.items():
835 setattr (corr, k, val)
838 result.setPrivateTools (corr)
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
853 return s
in poolfiles
858 return s == CALOCORR_COOL
866 modname =
'.'.join (xjo[:-1])
870 if modname.find (
'.') < 0:
871 modname =
'CaloClusterCorrection.' + modname
874 mod = __import__ (modname, globals(), locals(), [paramclass])
875 parms = getattr (mod, paramclass)
878 if isinstance (parms, dict)
and sampling
is not None:
879 parms = parms[sampling]
881 if order != 0
and hasattr (corr,
'order'):
883 if hasattr (corr,
'isDummy'):
886 log = logging.getLogger (
'CaloClusterCorrection')
889 for (k, val)
in parms.__dict__.items():
901 default_keys = parms.__dict__.get (
'_default_keys')
903 defkey = _longest_match (key, default_keys)
905 defkey = _longest_match (key, valid_keys)
907 defkey = CALOCORR_DEFAULT_KEY
910 log.debug (
" correction %s from JO using %s instead of %s" %
911 (corr.getName(), defkey, key))
917 setattr (corr, k, val)
923 if not poolfile
or not poolfiles[poolfile]:
930 if poolfile
not in _poolfiles_seen:
931 from EventSelectorAthenaPool.CondProxyProviderConfig
import CondProxyProviderCfg
932 ca.merge (CondProxyProviderCfg (flags, [poolfiles[poolfile]]))
935 corr.DBHandleKey = sgkey
939 ToolConstantsCondAlg = CompFactory.ToolConstantsCondAlg
941 name =
'ToolConstantsCondAlg_' + sgkey.replace (
'.',
'_')
942 alg = ToolConstantsCondAlg (name,
944 ToolConstantsKey = sgkey)
953 fsplit = folder.split (
'/')
954 if fsplit[2] ==
'Ofl':
955 corrclass = folder.split (
'/')[4]
957 corrclass = folder.split (
'/')[3]
958 sndict = flags.Calo.ClusterCorrection.dbSubdetName
959 subdetname = sndict.get (corrclass)
961 subdetname = sndict.get (
None)
963 corr.DBHandleKey = folder
966 oldtag = _folders_used.get (folder)
967 if oldtag
is not None and oldtag != tag:
972 _folders_used[folder] = tag
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,
979 detDb = subdetname + sdsuffix,
980 className =
'CaloRec::ToolConstants',
983 log = logging.getLogger (
'CaloClusterCorrection')
984 log.debug (
"Adding cool folder `%s' for subdetector name %s" %
985 (folder, subdetname))
994 while i < len(a)
and i < len(b)
and a[i] == b[i]:
998 if not isinstance (valid_keys, list):
999 valid_keys = [valid_keys]
1000 if valid_keys
is None or key
in valid_keys:
1002 new_key = valid_keys[0]
1003 for k
in valid_keys[1:]:
1004 if _matchlen (key, k) > _matchlen (key, new_key):
1008 if valid_keys
is None or key
in valid_keys:
return
1011 new_key = _longest_match (key, valid_keys)
1013 new_prefix = new_key + corr.prefix[len(key):]
1014 corr.prefix = new_prefix
1019 return _alltools[tool.getName()][0]
1024 return _alltools[tool.getName()][1]
1029 return _alltools[tool.getName()][2]