6 Transate arbitrary root file into a han config file with the "GatherData" algorithm
7 @author: ponyisi@hep.uchicago.edu
9 Adapted for fast physics monitoring 14 April 2011
14 from DQConfMakerBase.DQElements
import DQReference, DQRegion, DQAlgorithm
15 from DQHanConfMaker.hanwriter
import writeHanConfiguration
18 gatherdata = DQAlgorithm(id=
'GatherData',
19 libname=
'libdqm_algorithms.so')
20 worst = DQAlgorithm(id=
'WorstCaseSummary',libname=
'libdqm_summaries.so')
22 def recurse(rdir, dqregion, ignorepath, reffile=None):
24 for key
in rdir.GetListOfKeys():
25 cl = key.GetClassName(); rcl = ROOT.TClass.GetClass(cl)
27 if ' ' in key.GetName():
28 print(
'WARNING: cannot have spaces in histogram names for han config; not including %s %s' % (cl, key.GetName()))
30 if rcl.InheritsFrom(
'TH1'):
31 if '/' in key.GetName():
32 print(
'WARNING: cannot have slashes in histogram names, encountered in directory %s, histogram %s' % (rdir.GetPath(), key.GetName()))
34 if key.GetName() ==
'summary':
35 print(
'WARNING: cannot have histogram named summary, encountered in %s' % rdir.GetPath())
37 name = rdir.GetPath().
replace(ignorepath,
'') +
'/' + key.GetName()
38 dqpargs = {
'id' :name,
39 'algorithm': gatherdata,
40 'inputdatasource': name,
43 dqpargs[
'references'] = DQReference(reference=
'%s:same_name' % reffile)
44 dqpar = dqregion.newDQParameter( **dqpargs)
45 dqpar.addAnnotation(
'display',
'NoNorm')
47 elif rcl.InheritsFrom(
'TDirectory'):
49 newregion = dqregion.newDQRegion( key.GetName(), algorithm=worst )
50 recurse(key.ReadObj(), newregion, ignorepath, reffile)
54 returns True if we should kill this node
55 False if we should not
57 params = dqregion.getDQParameters()
60 subregions = dqregion.getSubRegions()
61 if subregions
is None:
64 subregions = subregions[:]
70 dqregion.delRelation(
'DQRegions', sr)
71 subregions = dqregion.getSubRegions()
72 if subregions
is None:
74 if len(subregions) + len(params) == 0:
80 params = dqregion.getDQParameters()
83 subregions = dqregion.getSubRegions()
84 if subregions
is None:
87 return len(params) +
sum([
paramcount(region)
for region
in subregions])
89 def process(infname, confname, reffile=None):
90 f = ROOT.TFile(infname,
'READ')
92 print(
'ERROR: cannot open %s' % infname)
95 top_level = DQRegion(id=
'topRegion',algorithm=worst)
96 print(
'Building tree...')
97 recurse(f, top_level, f.GetPath(), reffile)
98 print(
'Pruning dead branches...')
102 sublevel = top_level.getSubRegions()[:]
104 top_level.delRelation(
'DQRegions', x)
106 print(
'Writing output')
111 import shutil, os, sys, contextlib
113 han_is_found = (ROOT.gSystem.Load(
'libDataQualityInterfaces') == 0)
115 print(
'ERROR: unable to load offline DQMF; unable to proceed')
117 bname = os.path.basename(fname)
125 prebuilt_hcfg =
False
127 @contextlib.contextmanager
130 td = tempfile.mkdtemp()
134 with tmpdir()
as hantmpdir:
136 print(
'====> Processing file %s' % (fname))
137 print(
'====> Generating han configuration file')
138 hantmpinput = os.path.join(hantmpdir, bname)
139 shutil.copyfile(fname, hantmpinput)
140 haninput = hantmpinput
141 hanconfig = os.path.join(hantmpdir,
'han.config')
142 rv =
process(hantmpinput, hanconfig, options.reffile)
145 print(
'No histograms to display; exiting with code 0')
148 print(
'====> Compiling han configuration')
149 hanhcfg = os.path.join(hantmpdir,
'han.hcfg')
151 ROOT.dqi.HanConfig().AssembleAndSave( hanconfig, hanhcfg )
153 print(
'====> Executing han')
155 memlimit = resource.getrlimit(resource.RLIMIT_AS)
156 resource.setrlimit(resource.RLIMIT_AS, (memlimit[1], memlimit[1]))
157 hanoutput = haninput.rpartition(
'.')[0] +
'_han.root'
159 rv = ROOT.dqi.HanApp().Analyze( hanhcfg, haninput, hanoutput,
'' )
162 raise Exception(
'failure in han')
163 hantargetdir = os.path.join(options.webdir,
str(options.iteration),
164 options.dispname,
'run_%s' % run)
165 print(
'====> Copying to', hantargetdir)
166 hantargetfile = os.path.join(hantargetdir,
'run_%s_han.root' % run)
167 if not os.access(hantargetdir, os.W_OK):
169 os.makedirs(hantargetdir)
170 except Exception
as e:
171 print(
'Unable to create %s for some reason: %s' % (hantargetdir, e))
172 raise Exception(
'Error during execute')
173 shutil.copy2(hanoutput, hantargetfile)
174 print(
'====> Cleaning up')
175 except Exception
as e:
177 if 'canonical format' not in str(e):
181 if not prebuilt_hcfg:
182 os.unlink(hantmpinput)
192 if __name__==
"__main__":
194 parser = optparse.OptionParser(usage=
'usage: %prog [options] inputfile run')
195 parser.add_option(
'--webdir', default=
'/afs/cern.ch/user/a/atlasdqm/dqmdisk/han_results/fastphysmon',
196 help=
'Change directory to store web display files')
197 parser.add_option(
'--dispname', default=
'Summary',
198 help=
'Set "stream" name on web display')
199 parser.add_option(
'--iteration', default=1, type=
'int',
200 help=
'Set iteration number for web display')
201 parser.add_option(
'--reffile', default=
None,
202 help=
'Reference file to use. Must have same structure as inputfile')
204 options, args = parser.parse_args()
206 if not 2 == len(args):
215 print(
'Specified run', args[1],
'doesn\'t seem to be an integer')