3 from cppyy.gbl
import dqi
8 from typing
import List, Dict, Tuple, Optional, Iterable
10 log = logging.getLogger(
'DataQualityInterfaces')
17 input: str=
None, algrefname: str=
None) ->
None:
18 """Copy the configuration of an old algorithm to a new one, but without the
20 newgroup.SetAlgName(oldgroup.GetAlgName())
21 newgroup.SetAlgLibName(oldgroup.GetAlgLibName())
22 if algrefname
is None:
23 newgroup.SetAlgRefName(oldgroup.GetAlgRefName())
25 newgroup.SetAlgRefName(algrefname)
26 for par
in oldgroup.GetAllAlgPars():
27 newgroup.AddAlgPar(par)
28 for par
in oldgroup.GetAllAlgStrPars():
29 newgroup.AddAlgStrPar(par)
30 for lim
in oldgroup.GetAllAlgLimits():
31 newgroup.AddAlgLimit(lim)
32 for ann
in oldgroup.GetAllAnnotations():
33 if input
is not None and ann.GetName() ==
'inputname':
35 newgroup.AddAnnotation(ann)
36 newgroup.SetWeight(oldgroup.GetWeight())
40 from collections
import defaultdict
41 rv = defaultdict(list)
43 rv[s.count(
'/')].
append((s,g))
48 """ Handle everything in Python, testing only """
50 m_config = ROOT.TFile.Open( configName,
"READ" )
52 log.error(f
"HanConfig::Initialize() cannot open file \"{configName}\"\n")
55 key = m_config.FindKey(
"top_level")
57 log.error(f
"HanConfig::Initialize() cannot find configuration in file \"{configName}\"\n")
60 top_level = key.ReadObj()
62 log.error(f
"HanConfig::Initialize() cannot find configuration in file \"{configName}\"\n")
65 inputFile = ROOT.TFile.Open(inputFileName,
'READ')
67 return FixRegion(m_config, top_level, inputFile.Get(prefix))
71 """Main code to translate the configuration given by 'top_level' into the
72 final configuration by expanding the regexes using the objects in 'td'"""
73 log.info(
'Translating regexes...')
79 mapping_groups = {
'top_level': top_level}
80 mapping_assessors_final = {}
82 iterate_hcfg(top_level, mapping, mapping_regex, mapping_groups)
83 mapping_assessors_final.update(mapping)
84 for path, g
in mapping_groups.items():
85 newgroup = HanConfigGroup()
86 newgroup.SetName(g.GetName())
87 newgroup.SetPathName(path)
89 mapping_groups_final[path] = newgroup
94 for path, (a, p)
in mapping_regex.items():
99 log.debug(f
'match {p} with {h}')
100 tokenized_path = path.split(
'/')
103 for tok
in tokenized_path[:-1]:
104 formatted_tok = string.Template(tok).
substitute(m.groupdict())
105 orig_fullpath.append(tok)
106 new_fullpath.append(formatted_tok)
107 orig =
'/'.
join(orig_fullpath)
108 target =
'/'.
join(new_fullpath)
109 if target
not in mapping_groups_final:
110 log.debug(f
'Need to adapt {orig} to {target}')
111 if orig
in mapping_groups_final:
112 log.debug(f
'Deleting {orig}')
113 del mapping_groups_final[orig]
114 newgroup = HanConfigGroup()
115 newgroup.SetName(formatted_tok)
116 newgroup.SetPathName(target)
117 oldgroup = mapping_groups[orig]
119 mapping_groups_final[target] = newgroup
121 tok = tokenized_path[-1]
122 orig_fullpath.append(tok)
123 hname = h.split(
'/')[-1]
125 hextra =
'' if '@' not in tok
else (
'@' + tok.rsplit(
'@',1)[1])
127 new_fullpath.append(hname)
128 orig =
'/'.
join(orig_fullpath)
129 target =
'/'.
join(new_fullpath)
130 log.debug(f
'Map from {orig} to {target}')
133 newass.SetName(h + hextra)
134 algrefname = a.GetAlgRefName()
136 if a.GetAlgRefName() !=
"":
137 ref = refmapcache.get(a.GetAlgRefName())
139 ref = config.Get(a.GetAlgRefName())
140 refmapcache[a.GetAlgRefName()] = ref
142 log.error(
'Unable to find references for', orig)
144 if isinstance(ref, ROOT.TMap):
145 algrefnameptr = ref.GetValue(h)
146 algrefname = algrefnameptr.GetString().
Data()
if algrefnameptr
else ""
147 if algrefname
and isinstance(config.Get(algrefname), ROOT.TMap):
148 log.error(f
'Reference for {newass} is somehow still a TMap')
149 copyMetadata(newass, mapping_regex[orig][0], input=h, algrefname=algrefname)
150 mapping_assessors_final[target] = (newass, newass.GetHistPath())
151 log.debug(f
'change {orig} to {target}')
155 for p, (a, _)
in mapping_assessors_final.items():
157 mapping_groups_final[os.path.dirname(p)].AddAssessor(a)
158 except Exception
as e:
159 log.error(f
'Unable to look up assessor parent directory. Full error follows\n{e}: path {p}, assessor {a}, assessor name {a.GetName()}')
162 for _, l2
in sorted(keydepths.items(), reverse=
True):
164 if log.level <= logging.DEBUG:
165 log.debug(f
'Final additions for {p}, {g.GetName()}, {g.GetPathName()} into {os.path.dirname(p)}')
168 mapping_groups_final[os.path.dirname(p)].AddGroup(g)
170 log.error(f
'Unable to attach group to parent. Details: group path {g.GetPathName()}, attempted parent is {os.path.dirname(p)}')
172 for v
in refmapcache.values():
174 if isinstance(v, ROOT.TMap):
177 return mapping_groups_final[
'top_level']
184 """Recurse to collect all information on DQ configuration"""
186 for a
in group.GetAllAssessors():
187 m = mapping_regex
if a.GetIsRegex()
else mapping
188 m[os.path.join(group.GetPathName(), os.path.basename(a.GetName()))] = (a, a.GetHistPath())
189 for g
in group.GetAllGroups():
190 mapping_groups[g.GetPathName()] = g
195 """Recurse to find the names of all objects"""
198 prefix = os.path.normpath(prefix)
199 prefixlen = len(prefix) + 1
200 fixeddirname = os.path.normpath(thisdir.GetPath())[prefixlen:]
201 for k
in thisdir.GetListOfKeys():
202 if k.GetClassName().startswith(
'TDirectory'):
205 if k.GetName() !=
'metadata':
206 l.append(os.path.join(fixeddirname, k.GetName()))