ATLAS Offline Software
SGout2dot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #TODO : prune standalone alg and objects
3 # remove number of read write
4 # remove monitoring, L1, CTP
5 # arrow show be single side !
6 # @file: SGout2dot.py
7 # @purpose: read a SGAudSvc produced file and make dot file.
8 # @author: Ilija Vukotic <ivukotic@cern.ch>
9 # @date: May 2007
10 #
11 # @example:
12 #
13 # checkFile SGAudSvc.out
14 
15 __version__ = "$Revision: 1.2 $"
16 __author__ = "Ilija Vukotic <ivukotic@cern.ch>"
17 
18 import sys
19 import os
20 import shutil
21 import glob
22 
23 # some configuration booleans
24 debug=True
25 debugRemove=False
26 drawNReadWrite=False # if draw on each line the number of read and write
27 
28 
29 #Algorithms to be masked. Might need to be tuned
30 lRemove=[]
31 lRemove+= ["MboyMuonChamberT0"]
32 lRemove+= ['Stream','StreamAOD','noKey',"basic_"]
33 lRemove+= ["Trig","HLT","EF","L1","Mon","CTP","RoIB","GlobalManager"]
34 #lRemove += ['LArRawChannelCollection','TileRawChannelCollection','DetStatusAlg','DetectorStatus','_Data','_Link']
35 #lRemove+= ['EventSelector','Input']
36 #lRemove+= ['iPatStatistics']
37 def chLabel(inpVal):
38  result=inpVal
39  result=result.replace('>','_GT_')
40  result=result.replace('<','_LT_')
41  result=result.replace('::','_')
42  result=result.replace('.','_')
43  result=result.replace('+','_')
44  return result
45 
46 
47 
48 def isNumb(inpVal):
49  result = False
50  try:
51  stripped = str(int(inpVal))
52  result=True
53  except:
54  result=False
55  return result
56 
57 def toRemove(inpVal):
58  result=False
59  if isNumb(inpVal)==True:
60  if debugRemove:
61  print "remove ", inpVal, "is a number"
62  result=True
63  for thing in lRemove:
64  if inpVal.count(thing)>0:
65  if debugRemove:
66  print "remove ", inpVal ,"since it contains", thing
67  result=True
68  return result
69 
70 nArgs= len (sys.argv)
71 outFN="dot.dot"
72 inFN=""
73 if nArgs==1:
74  print "Usage: SGout2dot.py input_filename [output dot file]"
75  sys.exit()
76 if nArgs>1:
77  print "Input file",sys.argv[1]
78  inFN=sys.argv[1]
79 if nArgs==3:
80  print "Output file",sys.argv[2]
81  outFN=sys.argv[2]
82 try:
83  fi = open (inFN)
84 except:
85  print "No such file!"
86  sys.exit()
87 
88 fo = open (outFN, 'w' )
89 
90 lines = fi.readlines()
91 fi.close()
92 
93 # get number of algs
94 line=lines.pop(0)
95 words=line.split(' ')
96 nAlgs=int(words[1])
97 print '%(a)d algorithms found'%{'a':nAlgs}
98 
99 #read all algs
100 #note that algs to be removed cannot be masked before reading the alg <-> obj relation
101 algs=[]
102 nGoodAlgs=0
103 for i in range(nAlgs):
104  line=lines.pop(0).strip('\n')
105  line=line.replace('/','_')
106  algs.append(line)
107  if toRemove(line):
108  print "alg %s to be removed" % line
109  else:
110  nGoodAlgs+=1
111 
112 print " number of good algs ",nGoodAlgs," bad algs ",nAlgs-nGoodAlgs
113 if debug:
114  print algs
115 
116 # get number of objs
117 line=lines.pop(0)
118 words=line.split(' ')
119 nObjs=int(words[1])
120 print '%(a)d objects found'%{'a':nObjs}
121 
122 # get all object names, and replace possible annoying characters
123 #note that objs to be removed cannot be masked before reading the alg <-> obj relation
124 objs=[]
125 nGoodObjs=0
126 for i in range(nObjs):
127  line=lines.pop(0).strip('\n')
128  line=line.replace('/','_')
129  line=line.replace('<','LT')
130  line=line.replace('>','GT')
131  line=line.replace('+','_')
132  objs.append(line)
133  if toRemove(line):
134  print "obj %s to be removed" % line
135  else:
136  nGoodObjs+=1
137 
138 print " number of good objs ",nGoodObjs," bad objs ",nObjs-nGoodObjs
139 if debug:
140  print objs
141 
142 fo.write("// %s algorithms and %s objects \n" % (nGoodAlgs,nGoodObjs))
143 
144 
145 #write out dot file header
146 fo.write("digraph SGAudSvc{\nrankdir=LR;\n") # begining and graph stuff
147 fo.write("ratio=0.7;\nnodesep=0.05;\n")
148 fo.write("subgraph c_0{\nnode[style=filled,color=red];\n") # alg subgraph
149 fo.write("label=\"Algs READING\";\ncolor=red;\n")
150 
151 # get the relations betwen alg and obj
152 # TODO number write or read not kept at this point
153 objsWrittenByAlg={} #A->many O (A write O may read it as well)
154 objsReadByAlg={} #many O->A (A read O may do not read it )
155 algsWritingObj={} #many A->O (A write O may read it as well).
156 algsReadingObj={} # O->many A (A does not write O)
157 for alg in algs:
158  line=lines.pop(0)
159  words=line.strip('\n').split('\t')
160  for obj in objs:
161  rw=words.pop(0).split(':')
162  reads=int(rw[0])
163  writes=int(rw[1])
164  if reads==0 and writes==0:
165  continue
166  if toRemove(obj) or toRemove(alg):
167  continue
168  #print alg,obj,reads, writes
169 
170  if writes==0 and reads>0:
171  if not obj in algsReadingObj.keys():
172  algsReadingObj[obj]=[]
173  algsReadingObj[obj]+=[(alg,reads,writes)]
174  if not alg in objsReadByAlg.keys():
175  objsReadByAlg[alg]=[]
176  objsReadByAlg[alg]+=[obj]
177  elif writes>0:
178  if not obj in algsWritingObj.keys():
179  algsWritingObj[obj]=[]
180  algsWritingObj[obj]+=[alg]
181 
182  if not alg in objsWrittenByAlg.keys():
183  objsWrittenByAlg[alg]=[]
184  objsWrittenByAlg[alg]+=[(obj,reads,writes)]
185 
186 
187 #write out all algorithms, but only the ones that have at least one connection (and are not masked)
188 for alg in algs:
189  if toRemove(alg)==False:
190  if alg in objsWrittenByAlg or alg in objsReadByAlg:
191  towrite='a_%(a)s [label=\"%(a)s\"];\n'%{'a':chLabel(alg),'b':alg}
192  fo.write(towrite)
193  else:
194  print "alg %s masked because not connected" % alg
195 
196 fo.write("}\n\nsubgraph c_1{\nnode[style=filled,shape=box];\ncolor=green;label=\"Objects\";\n")
197 #write out all objecs
198 for obj in objs:
199  if toRemove(obj)==False:
200  if obj in algsWritingObj or obj in algsReadingObj:
201  towrite='o_%(a)s [label=\"%(b)s\"];\n'%{'a':chLabel(obj),'b':obj}
202  fo.write(towrite)
203  else:
204  print "obj %s masked because not connected" % obj
205 fo.write("}\n\n")
206 
207 
208 if debug:
209  print " "
210  print "objsWrittenByAlg:", objsWrittenByAlg
211  print " "
212  print "objsReadByAlg:", objsReadByAlg
213  print " "
214  print "algsWritingObj:", algsWritingObj
215  print " "
216  print "algsReadingObj:", algsReadingObj
217  print " "
218 
219 #write the connection a to obj
220 for alg in objsWrittenByAlg.keys():
221  for objrec in objsWrittenByAlg[alg]:
222  obj=objrec[0]
223  reads=objrec[1]
224  writes=objrec[2]
225  towrite='a_%(ALG)s -> o_%(OBJ)s [ '%{'ALG':chLabel(alg),'OBJ':chLabel(obj)}
226  if drawNReadWrite:
227  if reads==0:
228  towrite+='label=\"%s\" ' % writes
229  else:
230  # if read in addition to write, write both number of read and write plus double arrow
231  # this happen when an alg writes an object and read it back to modify it
232  towrite+='label=\"w:%(WRITES)s r:%(READS)s\" arrowtail=\"normal\" '%{'WRITES':writes,'READS':reads}
233  else:
234  if reads>0: # double arrow
235  towrite+='arrowtail=\"normal\" '
236  towrite+='];\n'
237  fo.write(towrite)
238 
239 #write the connection obj to alg
240 # by definition only read no write
241 for obj in algsReadingObj.keys():
242  for algrec in algsReadingObj[obj]:
243  alg=algrec[0]
244  reads=algrec[1]
245  writes=algrec[2]
246 
247  towrite='o_%(OBJ)s -> a_%(ALG)s [ '%{'ALG':chLabel(alg),'OBJ':chLabel(obj)}
248  if drawNReadWrite:
249  towrite+='label=\"%s\" ' % reads
250  towrite+='];\n'
251  fo.write(towrite)
252 
253 
275 
276 fo.write("}")
277 fo.close()
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
SGout2dot.chLabel
def chLabel(inpVal)
Definition: SGout2dot.py:37
SGout2dot.toRemove
def toRemove(inpVal)
Definition: SGout2dot.py:57
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
SGout2dot.isNumb
def isNumb(inpVal)
Definition: SGout2dot.py:48
str
Definition: BTagTrackIpAccessor.cxx:11
Trk::split
@ split
Definition: LayerMaterialProperties.h:38