/search.css" rel="stylesheet" type="text/css"/> /search.js">
00001 #!/usr/bin/env python 00002 ''' 00003 00004 Check for expected tree/branch in input file using RootInputFile. 00005 Designed to check validity of output of MC jobs, both premix and mixed. 00006 Also can compare expected vs actual number of entries in input file. 00007 00008 usage parameters: 00009 00010 To use pre-defined tree list 00011 nuwa.py -l 5 --no-history -m"hdrValid -t mixed" file.root 00012 nuwa.py -l 5 --no-history -m"hdrValid -t premix" file.root 00013 To use user-specified tree list 00014 nuwa.py -l 5 --no-history -m"hdrValid -u Gen -u Sim" file.root 00015 Note that no events need be processed to examine the tree/branch list. 00016 00017 00018 To give compare an expected number of entries NENTRIES with the actual number 00019 of entries ON A SINGLE INPUT FILE (not a list) 00020 nuwa.py -l 5 -n -1 --no-history -m"hdrValid -E NENTRIES" file.root 00021 00022 00023 Note suppression of spurious output by -l 5 --no-history. 00024 00025 Note that if you specify a list of input files, all files must have 00026 the same tree/branch structure or RootInputFile will complain when 00027 evoked by Control.py 00028 00029 For general help: 00030 nuwa.py -m"hdrValid --help" 00031 ''' 00032 # 00033 # basic hists and checks of output 00034 00035 __all__ = ['basic'] 00036 00037 from DybPython.DybPythonAlg import DybPythonAlg 00038 from GaudiPython import SUCCESS, FAILURE 00039 from GaudiPython import gbl, loaddict 00040 00041 from DybPython.Util import irange 00042 from GaudiKernel import SystemOfUnits as units 00043 import PyCintex 00044 00045 from DybPython.Control import nuwa 00046 00047 debuglevel = 0 00048 nExec = 0 00049 00050 class hdrValid(DybPythonAlg): 00051 00052 def __init__(self,name): 00053 00054 DybPythonAlg.__init__(self,name) 00055 return 00056 00057 00058 def initialize(self): 00059 print "Initializing ", self.name() 00060 status = DybPythonAlg.initialize(self) 00061 if status.isFailure(): return status 00062 global debuglevel, filetype, userTrees, keepgoing, expected 00063 dbglvl = int( debuglevel ) 00064 00065 if dbglvl>0: print self.name()," filetype",filetype 00066 00067 # compose treename from base and trees given input filetype 00068 # OR create an empty treename and ignore trees in input file(s) 00069 # OR given trees by user on command line 00070 base = '/Event' 00071 if userTrees is None : 00072 if filetype.lower() == 'premix' : 00073 trees= ['Gen','Sim','SimReadout'] 00074 elif filetype.lower() == 'mixed' : 00075 trees= ['Gen','Sim','Readout'] 00076 elif (filetype.lower() == 'none') or (filetype.lower() == 'ignore'): 00077 trees = [] 00078 else: 00079 print self.name()," ERROR. Invalid input file type",filetype 00080 return FAILURE 00081 else: 00082 ''' User specified trees take precedence ''' 00083 trees = userTrees 00084 if dbglvl>0: print self.name()," userTrees=",userTrees 00085 00086 path = [] 00087 for tree in trees: 00088 path.append(base + '/' + tree + '/' + tree + 'Header') 00089 00090 if dbglvl>1: print self.name(),"path",path 00091 00092 # get input streams 00093 streams = nuwa.opts.input_streams 00094 00095 # make sure that # of executions in nuwa.py line is "-n -1" iff 00096 # we expect a certain number of executions 00097 if expected>-1 : 00098 if nuwa.opts.executions == -1 : 00099 pass 00100 else: 00101 print self.name(),"WARNING You expect",expected,"execution cycles in input file,",\ 00102 " but you specified `-n",nuwa.opts.executions,"` instead of `-n -1`" 00103 00104 if dbglvl>1: 00105 print self.name(),"streams",streams 00106 00107 RootInputFile = gbl.RootInputFile 00108 00109 nBad = 0 00110 nFiles = 0 00111 00112 for key in streams : 00113 if dbglvl>0 : print self.name(),"key",key,"has",len(streams[key]),"entries" 00114 for fname in streams[key]: 00115 if dbglvl>0 : print "fname",fname 00116 nFiles += 1 00117 for treename in path: 00118 branchname = treename.strip(base).replace("/","_") 00119 istat = RootInputFile.TestForObject(fname,treename, branchname) 00120 if dbglvl> 0: print "treename, branchname, ClassID",treename,branchname,istat 00121 if istat == 0 : 00122 print self.name(),"FATAL ERROR in hdrValid. File",fname,\ 00123 "\n",self.name(),"ERROR in hdrValid tree",treename,"branch",branchname 00124 nBad += 1 00125 if not keepgoing : 00126 return FAILURE 00127 00128 if expected>-1 and nFiles>1 : 00129 print self.name(),"WARNING Have",nFiles,"input files. You claim there are",expected,\ 00130 "execution cycles for the job, not the file. Is this what you want?" 00131 00132 if nBad == 0: 00133 print self.name(),"SUCCESS judges all",nFiles,"input files OK" 00134 else: 00135 print self.name(),"FATAL ERROR Found",nBad,"files with invalid header in a total of",nFiles,"files" 00136 return FAILURE 00137 00138 return SUCCESS 00139 00140 00141 def execute(self): 00142 00143 # ensure that we're dealing with an integer as the debug flag 00144 global debuglevel, nExec 00145 dbglvl = int( debuglevel ) 00146 00147 00148 nExec += 1 00149 if dbglvl>0 : 00150 if (nExec <= 10) or (nExec <=100 and nExec%10 == 0) or (nExec <=1000 and nExec%100 == 0) or (nExec<=10000 and nExec%1000 == 0) or (nExec%10000 == 0) : 00151 print self.name(),"execute() nExec=",nExec 00152 return SUCCESS 00153 00154 def finalize(self): 00155 global debuglevel, filetype, userTrees, keepgoing, expected, nExec 00156 status = DybPythonAlg.finalize(self) 00157 00158 print self.name()," Observed",nExec,"execution cycles" 00159 if expected > -1 : 00160 print "Finalizing ", self.name() 00161 if nExec == expected : 00162 print self.name(),"SUCCESS Observed number of executions",nExec,"is equal to expectation" 00163 else: 00164 print self.name(),"FATAL Observed number of executions",nExec,"is NOT equal to expectation",expected 00165 if nuwa.opts.executions is not -1:print self.name(),\ 00166 "This FATAL ERROR is not surprising because you specified `nuwa.py -n",nuwa.opts.executions,\ 00167 "` instead of `-n -1`" 00168 return FAILURE 00169 return status 00170 00171 def configure(argv=[]) : #None) : 00172 # note: if argv=None is specified as the arguments to configure, then 00173 # the parser expects to have at least one option explicitly specified. 00174 # with argv=[], one does not have specify any arguments (the defaults are used) 00175 00176 # deal with input arguments 00177 from optparse import OptionParser 00178 parser = OptionParser() #usage=self.__doc__) 00179 parser.add_option("-d","--debug-level", 00180 default=0, 00181 help="Debug level 0=none, 1=more. [default: %default]") 00182 00183 00184 parser.add_option("-t","--type",type="str", 00185 default="premix", 00186 help="Type of input file. [default: %default]\n If `none` or `ignore`, then NO trees are checked in inputfile") 00187 00188 00189 parser.add_option("-u","--user-tree",type="str",action="append", 00190 help="User-specified expected trees. Overrides --type.") 00191 00192 parser.add_option("-k","--keep-going",action="store_true", 00193 help="Keep-going even if one file does not have the correct headers. Good for examining a list of files.") 00194 00195 parser.add_option("-E","--expected-entries",type="int", 00196 default="-1", 00197 help="Expected number of execution cycles. Ignored if <0. Not good for examining a list of files. [default: %default]") 00198 00199 (options,args) = parser.parse_args(args=argv) 00200 00201 # pass arguments using global variable 00202 global debuglevel, filetype, userTrees, keepgoing, expected, nExec 00203 debuglevel = options.debug_level 00204 filetype = options.type 00205 userTrees = options.user_tree 00206 keepgoing = options.keep_going 00207 expected = options.expected_entries 00208 00209 return 00210 00211 def run(app): 00212 '''Configure and add this algorithm to job''' 00213 import sys 00214 00215 app.ExtSvc += [] 00216 hVal = hdrValid("hdrValid") 00217 app.addAlgorithm( hVal ) 00218 00219 pass