00001
00002 '''
00003 Example of doing simple efficiency with sim15 output
00004
00005
00006 This assumes you made a file with somthing like:
00007
00008 nuwa.py -n1000 -m Sim15.FullChainSimple -o s15fcs.root
00009
00010 Then run like
00011
00012 nuwa.py -n -1 -m Sim15.Eff s15fcs.root
00013 ...
00014 Importing module Sim15.Eff
00015 ...
00016
00017
00018 '''
00019
00020 from GaudiPython.GaudiAlgs import GaudiAlgo
00021 from GaudiPython import SUCCESS, FAILURE, Bindings, gbl
00022 from DybPython.Util import irange
00023
00024 def visitInputHeaders(obj,visitor):
00025 '''
00026 Recursively visit obj's inputHeaders and their inputHeaders.
00027 The visitor should be callable with a HeaderObject.
00028 '''
00029 visitor(obj)
00030 ihs = obj.inputHeaders()
00031 siz = ihs.size();
00032 for ind in range(siz):
00033 ih = ihs[ind]
00034 visitInputHeaders(ih,visitor)
00035 continue
00036 return
00037
00038 class Sim15Eff(GaudiAlgo):
00039 '''
00040 Give an example of calculating some efficiencies
00041 '''
00042
00043 def __init__(self,name='Sim15Eff'):
00044 GaudiAlgo.__init__(self,name)
00045 self.genCounts = {}
00046 self.trigCounts = {}
00047 return
00048
00049 def execute(self):
00050 tes = self.evtSvc()
00051
00052 rs = tes['/Event/RegistrationSequence']
00053
00054 regs = rs.registrations()
00055 nregs = regs.size()
00056
00057 for ireg in range(0,nregs):
00058 reg = regs[ireg]
00059 loc = reg.path()
00060 obj = reg.object()
00061
00062
00063 if loc == '/Event/Gen/GenHeader':
00064 self._add_generated(obj)
00065 pass
00066
00067 if loc == '/Event/Readout/ReadoutHeader':
00068 ghs = self._get_gen_headers(obj)
00069
00070 for gh in ghs:
00071 self._add_triggered(gh)
00072 pass
00073
00074 continue
00075 return SUCCESS
00076
00077 def finalize(self):
00078 '''
00079 Print efficiencies.
00080 '''
00081
00082 for nam in self.genCounts.keys():
00083 if nam not in self.trigCounts.keys():
00084 self.trigCounts[nam] = 0
00085 print 'Kin type: %s has %d/%d = %f'\
00086 %(nam,self.trigCounts[nam],self.genCounts[nam],
00087 (self.trigCounts[nam]*1.0)/(self.genCounts[nam]*1.0))
00088 continue
00089 return SUCCESS
00090
00091 def _get_gen_headers(self,obj):
00092 '''
00093 Descend through obj's inputHeaders and build up a list of GenHeaders
00094 '''
00095 class CollectGH:
00096 def __init__(self):
00097 self.gh_list = []
00098 return
00099 def __call__(self,obj):
00100 if obj.name() == '/GenHeader':
00101 self.gh_list.append(obj)
00102 return
00103 pass
00104
00105 cgh = CollectGH()
00106 visitInputHeaders(obj,cgh)
00107 return cgh.gh_list
00108
00109 def _add_generated(self,obj):
00110 '''
00111 Count a generated event
00112 '''
00113 nam = obj.generatorName()
00114 try:
00115 self.genCounts[nam] += 1
00116 except KeyError:
00117 self.genCounts[nam] = 1
00118 pass
00119 return
00120
00121 def _add_triggered(self,obj):
00122 '''
00123 Count a triggered event
00124 '''
00125 nam = obj.generatorName()
00126 try:
00127 self.trigCounts[nam] += 1
00128 except KeyError:
00129 self.trigCounts[nam] = 1
00130 pass
00131 return
00132
00133
00134 def configure(argv=None):
00135 return
00136
00137 def run(app):
00138 alg = Sim15Eff()
00139 app.addAlgorithm(alg)
00140 return
00141
00142