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
11 from __future__
import print_function
15 from DQConfMakerBase.DQElements
import DQReference, DQRegion, DQAlgorithm
16 from DQHanConfMaker.hanwriter
import writeHanConfiguration
19 gatherdata = DQAlgorithm(id=
'GatherData',
20 libname=
'libdqm_algorithms.so')
21 worst = DQAlgorithm(id=
'WorstCaseSummary',libname=
'libdqm_summaries.so')
23 def recurse(rdir, dqregion, ignorepath, reffile=None):
25 for key
in rdir.GetListOfKeys():
26 cl = key.GetClassName(); rcl = ROOT.TClass.GetClass(cl)
28 if ' ' in key.GetName():
29 print(
'WARNING: cannot have spaces in histogram names for han config; not including %s %s' % (cl, key.GetName()))
31 if rcl.InheritsFrom(
'TH1'):
32 if '/' in key.GetName():
33 print(
'WARNING: cannot have slashes in histogram names, encountered in directory %s, histogram %s' % (rdir.GetPath(), key.GetName()))
35 if key.GetName() ==
'summary':
36 print(
'WARNING: cannot have histogram named summary, encountered in %s' % rdir.GetPath())
38 name = rdir.GetPath().
replace(ignorepath,
'') +
'/' + key.GetName()
39 dqpargs = {
'id' :name,
40 'algorithm': gatherdata,
41 'inputdatasource': name,
44 dqpargs[
'references'] = DQReference(reference=
'%s:same_name' % reffile)
45 dqpar = dqregion.newDQParameter( **dqpargs)
46 dqpar.addAnnotation(
'display',
'NoNorm')
48 elif rcl.InheritsFrom(
'TDirectory'):
50 newregion = dqregion.newDQRegion( key.GetName(), algorithm=worst )
51 recurse(key.ReadObj(), newregion, ignorepath, reffile)
55 returns True if we should kill this node
56 False if we should not
58 params = dqregion.getDQParameters()
61 subregions = dqregion.getSubRegions()
62 if subregions
is None:
65 subregions = subregions[:]
71 dqregion.delRelation(
'DQRegions', sr)
72 subregions = dqregion.getSubRegions()
73 if subregions
is None:
75 if len(subregions) + len(params) == 0:
81 params = dqregion.getDQParameters()
84 subregions = dqregion.getSubRegions()
85 if subregions
is None:
88 return len(params) +
sum([
paramcount(region)
for region
in subregions])
90 def process(infname, confname, reffile=None):
91 f = ROOT.TFile(infname,
'READ')
93 print(
'ERROR: cannot open %s' % infname)
96 top_level = DQRegion(id=
'topRegion',algorithm=worst)
97 print(
'Building tree...')
98 recurse(f, top_level, f.GetPath(), reffile)
99 print(
'Pruning dead branches...')
103 sublevel = top_level.getSubRegions()[:]
105 top_level.delRelation(
'DQRegions', x)
107 print(
'Writing output')
112 import shutil, os, sys, contextlib
114 han_is_found = (ROOT.gSystem.Load(
'libDataQualityInterfaces') == 0)
116 print(
'ERROR: unable to load offline DQMF; unable to proceed')
118 bname = os.path.basename(fname)
126 prebuilt_hcfg =
False
128 @contextlib.contextmanager
131 td = tempfile.mkdtemp()
135 with tmpdir()
as hantmpdir:
137 print(
'====> Processing file %s' % (fname))
138 print(
'====> Generating han configuration file')
139 hantmpinput = os.path.join(hantmpdir, bname)
140 shutil.copyfile(fname, hantmpinput)
141 haninput = hantmpinput
142 hanconfig = os.path.join(hantmpdir,
'han.config')
143 rv =
process(hantmpinput, hanconfig, options.reffile)
146 print(
'No histograms to display; exiting with code 0')
149 print(
'====> Compiling han configuration')
150 hanhcfg = os.path.join(hantmpdir,
'han.hcfg')
152 ROOT.dqi.HanConfig().AssembleAndSave( hanconfig, hanhcfg )
154 print(
'====> Executing han')
156 memlimit = resource.getrlimit(resource.RLIMIT_AS)
157 resource.setrlimit(resource.RLIMIT_AS, (memlimit[1], memlimit[1]))
158 hanoutput = haninput.rpartition(
'.')[0] +
'_han.root'
160 rv = ROOT.dqi.HanApp().Analyze( hanhcfg, haninput, hanoutput,
'' )
163 raise Exception(
'failure in han')
164 hantargetdir = os.path.join(options.webdir,
str(options.iteration),
165 options.dispname,
'run_%s' % run)
166 print(
'====> Copying to', hantargetdir)
167 hantargetfile = os.path.join(hantargetdir,
'run_%s_han.root' % run)
168 if not os.access(hantargetdir, os.W_OK):
170 os.makedirs(hantargetdir)
171 except Exception
as e:
172 print(
'Unable to create %s for some reason: %s' % (hantargetdir, e))
173 raise Exception(
'Error during execute')
174 shutil.copy2(hanoutput, hantargetfile)
175 print(
'====> Cleaning up')
176 except Exception
as e:
178 if 'canonical format' not in str(e):
182 if not prebuilt_hcfg:
183 os.unlink(hantmpinput)
193 if __name__==
"__main__":
195 parser = optparse.OptionParser(usage=
'usage: %prog [options] inputfile run')
196 parser.add_option(
'--webdir', default=
'/afs/cern.ch/user/a/atlasdqm/dqmdisk/han_results/fastphysmon',
197 help=
'Change directory to store web display files')
198 parser.add_option(
'--dispname', default=
'Summary',
199 help=
'Set "stream" name on web display')
200 parser.add_option(
'--iteration', default=1, type=
'int',
201 help=
'Set iteration number for web display')
202 parser.add_option(
'--reffile', default=
None,
203 help=
'Reference file to use. Must have same structure as inputfile')
205 options, args = parser.parse_args()
207 if not 2 == len(args):
216 print(
'Specified run', args[1],
'doesn\'t seem to be an integer')