3 """core module for an interactive analysis
5 Examples are in `PyAnalysisExamples/PlotTest.py`_ and `PyAnalysisExamples/HistTest.py`_
7 .. _PyAnalysisExamples/HistTest.py: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/PhysicsAnalysis/PyAnalysis/PyAnalysisExamples/share/HistTest.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
9 .. _PyAnalysisExamples/PlotTest.py: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/PhysicsAnalysis/PyAnalysis/PyAnalysisExamples/share/PlotTest.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
11 :author: Tadashi Maeno
12 :contact: tmaeno@bnl.gov
15 __docformat__ =
"restructuredtext en"
36 GNS.AttributeList = _DummyClass
37 GNS.AANT = _DummyClass
45 def init (v_theApp, v_rootStream=None):
48 This method is called in `PyKernel/InitPyKernel.py`_.
50 :param v_theApp: reference to the application manager. theApp
54 athena> PyKernel.init(theApp)
56 .. _PyKernel/InitPyKernel.py: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/Control/PyKernel/share/InitPyKernel.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
64 rootStream = v_rootStream
67 eventLoopType = _HybridProcess
71 if hasattr (theApp,
'curEvent'):
75 setattr(theApp.__class__,method,mobj)
78 setattr(theApp.__class__,method,mobj)
83 """Function object to set event counter in ROOT stream.
84 This class is used to patch, e.g., theApp.nextEvent(), run() ...
86 :param methodObj: reference to method to be patched
95 if eventLoopType != _PreProcess:
96 curEvent = theApp.curEvent()
98 if eventLoopType != _NormalProcess
and rootStream:
99 rootStream.GetEntry(curEvent)
101 if eventLoopType == _PreProcess:
103 curEvent = curEvent+1
104 return GNS.StatusCode(1)
111 """Retrieve object from StoreGate
113 :param aClass: type of the class
114 :param aKey: key of the object
118 athena> obj = PyKernel.retrieve(g.MyClass,'mykey')
119 athena> obj = PyKernel.retrieve(g.MyClass) # when only one MyClass obj is in SG
120 where the prefix 'g' is the global namespace provided by cppyy
125 if aClass == GNS.AttributeList:
127 if aClass == GNS.AANT:
130 if storeGate
is None:
131 import AthenaPython.PyAthena
as PyAthena
132 storeGate = PyAthena.py_svc(
'StoreGateSvc/StoreGateSvc')
134 ret = storeGate.retrieve(aClass,aKey)
136 ret = storeGate.retrieve(aClass)
142 """Retrieve object from DetectorStore
144 :param aClass: type of the class
145 :param aKey: key of the object
149 athena> obj = PyKernel.retrieveDet(g.MyClass,'mykey')
150 athena> obj = PyKernel.retrieveDet(g.MyClass) # when only one MyClass obj is in SG
151 where the prefix 'g' is the global namespace provided by cppyy
158 import AthenaPython.PyAthena
as PyAthena
159 storeGate = PyAthena.py_svc(
'StoreGateSvc/DetectorStore')
161 ret = detStore.retrieve(aClass,aKey)
163 ret = detStore.retrieve(aClass)
168 def fill (hist, classAndKey, value, criteria="True", nEvent=100):
172 :param hist: reference to AIDA or ROOT histogram
173 :param classAndKey: combination of class name and key separeted with "#". "Class#Key"
174 :param value: physics parameter in string
175 :param criteria: selection criteria
176 :param nEvent: number of event to be processed
180 athena> fill(h,"ElectronContainer#ElectronCollection","$x.pt()")
181 fill hist with pt of electrons
182 "$x" denotes an element of "Class#Key", if "Class#Key" is a collection
184 athena> fill(h,"MissingET#MET_Calib","$x.sumet()")
185 fill hist with et of MissingET.
186 "$x" denotes "Class#Key" itself, if "Class#Key" is not a vector-like class
188 athena> fill(h,"ElectronContainer#ElectronCollection","$x.pt()","$x.pz()>0")
189 apply a selection criteria
191 For more detail of parameters, see `PyAnalysisExamples/PlotTest.py`_
193 .. _PyAnalysisExamples/PlotTest.py: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/PhysicsAnalysis/PyAnalysis/PyAnalysisExamples/share/PlotTest.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
204 if classAndKey !=
"":
214 if callable(criteria):
216 commandC =
"criteria()"
228 for iE
in range(nEvent):
232 obj = eval(commandSG)
235 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
248 for iC
in range(lSize):
274 if (iE+1) == bufEvent:
301 lpath =
'/stat/tmp/PyKernelHist'
305 title = value.__name__
308 h = book(lpath, title, 100, minX, maxX)
320 def plot (classAndKey, value="$x", criteria="True", nEvent=100):
323 :param classAndKey: combination of class name and key separeted with '#'. 'Class#Key'
324 :param value: physics parameter in string
325 :param criteria: selection criteria
326 :param nEvent: number of event to be processed
330 athena> plot('ElectronContainer#ElectronCollection','$x.pt()')
333 For detail, see `PyAnalysisExamples/PlotTest.py`_
335 .. _PyAnalysisExamples/PlotTest.py: http://atlas-sw.cern.ch/cgi-bin/viewcvs-atlas.cgi/offline/PhysicsAnalysis/PyAnalysis/PyAnalysisExamples/share/PlotTest.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
340 h = fill (
None, classAndKey, value, criteria, nEvent)
349 def fill2 (hist, classAndKey, valueX, valueY, criteria="True", nEvent=100):
352 :param hist: reference to AIDA or ROOT histogram
353 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
354 :param valueX: physics parameter for X in string
355 :param valueY: physics parameter for Y in string
356 :param criteria: selection criteria
357 :param nEvent: number of event to be processed
359 For detail, see `fill`
370 if classAndKey !=
"":
376 commandX =
"valueX()"
382 commandY =
"valueY()"
386 if callable(criteria):
388 commandC =
"criteria()"
401 for iE
in range(nEvent):
405 obj = eval(commandSG)
408 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
421 for iC
in range(lSize):
449 if (iE+1) == bufEvent:
500 lpath =
'/stat/tmp/PyKernelHist'
504 titleX = valueX.__name__
508 titleY = valueY.__name__
511 h = book(lpath, titleY+
" vs "+titleX, 100, minX, maxX, 100, minY, maxY)
516 for iB
in range(len(bufX)):
525 def plot2 (classAndKey, valueX="$x", valueY="$x", criteria="True", nEvent=100):
528 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
529 :param valueX: physics parameter for X in string
530 :param valueY: physics parameter for Y in string
531 :param criteria: selection criteria
532 :param nEvent: number of event to be processed
534 For detail, see `plot`
537 h = fill2 (
None, classAndKey, valueX, valueY, criteria, nEvent)
546 def fillProf (hist, classAndKey, valueX, valueY, criteria="True", nEvent=100):
547 """Fill profile-histogram
549 :param hist: reference to AIDA or ROOT histogram
550 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
551 :param valueX: physics parameter for X in string
552 :param valueY: physics parameter for Y in string
553 :param criteria: selection criteria
554 :param nEvent: number of event to be processed
556 For detail, see `fill`
567 if classAndKey !=
"":
573 commandX =
"valueX()"
579 commandY =
"valueY()"
583 if callable(criteria):
585 commandC =
"criteria()"
598 for iE
in range(nEvent):
602 obj = eval(commandSG)
605 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
618 for iC
in range(lSize):
646 if (iE+1) == bufEvent:
673 lpath =
'/stat/tmp/PyKernelHist'
677 titleX = valueX.__name__
681 titleY = valueY.__name__
684 h = bookProf(lpath, titleY+
" vs "+titleX, 100, minX, maxX)
689 for iB
in range(len(bufX)):
698 def plotProf (classAndKey, valueX="$x", valueY="$x", criteria="True", nEvent=100):
699 """Plot profile-histogram
701 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
702 :param valueX: physics parameter for X in string
703 :param valueY: physics parameter for Y in string
704 :param criteria: selection criteria
705 :param nEvent: number of event to be processed
707 For detail, see `plot`
710 h = fillProf (
None, classAndKey, valueX, valueY, criteria, nEvent)
721 str = re.sub(
r"\$",
"", str)
723 cK = re.findall(
r'([\w_]+)#([\w_\*]+)',str)
728 aStr =
'retrieve(GNS.'+iCK[0]+
')'
730 bStr = iCK[0]+
"#"+iCK[1]
731 aStr =
'retrieve(GNS.'+iCK[0]+
',"'+iCK[1]+
'")'
732 str = re.sub(bStr, aStr, str)
739 Dump objects in StoreGate
746 print (GNS.StoreGate.pointer().
dump())
752 Unregister histogram from HistogramSvc
754 :param path: path to the histogram
758 athena> unregister("/stat/tmpHist")
761 return theApp.histSvc().unregisterObject(path)
767 Dump histograms in HistogramSvc
774 theApp.histSvc().
dump()
780 Set event loop type to pre-process
784 curEvent = theApp.curEvent()
786 eventLoopType = _PreProcess
792 Set event loop type to normal-process
796 eventLoopType = _NormalProcess
802 Set event loop type to hybrid-process
806 eventLoopType = _HybridProcess