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
157 import AthenaPython.PyAthena
as PyAthena
158 storeGate = PyAthena.py_svc(
'StoreGateSvc/DetectorStore')
160 ret = detStore.retrieve(aClass,aKey)
162 ret = detStore.retrieve(aClass)
167 def fill (hist, classAndKey, value, criteria="True", nEvent=100):
171 :param hist: reference to AIDA or ROOT histogram
172 :param classAndKey: combination of class name and key separeted with "#". "Class#Key"
173 :param value: physics parameter in string
174 :param criteria: selection criteria
175 :param nEvent: number of event to be processed
179 athena> fill(h,"ElectronContainer#ElectronCollection","$x.pt()")
180 fill hist with pt of electrons
181 "$x" denotes an element of "Class#Key", if "Class#Key" is a collection
183 athena> fill(h,"MissingET#MET_Calib","$x.sumet()")
184 fill hist with et of MissingET.
185 "$x" denotes "Class#Key" itself, if "Class#Key" is not a vector-like class
187 athena> fill(h,"ElectronContainer#ElectronCollection","$x.pt()","$x.pz()>0")
188 apply a selection criteria
190 For more detail of parameters, see `PyAnalysisExamples/PlotTest.py`_
192 .. _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
203 if classAndKey !=
"":
213 if callable(criteria):
215 commandC =
"criteria()"
227 for iE
in range(nEvent):
231 obj = eval(commandSG)
234 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
247 for iC
in range(lSize):
273 if (iE+1) == bufEvent:
300 lpath =
'/stat/tmp/PyKernelHist'
304 title = value.__name__
307 h = book(lpath, title, 100, minX, maxX)
319 def plot (classAndKey, value="$x", criteria="True", nEvent=100):
322 :param classAndKey: combination of class name and key separeted with '#'. 'Class#Key'
323 :param value: physics parameter in string
324 :param criteria: selection criteria
325 :param nEvent: number of event to be processed
329 athena> plot('ElectronContainer#ElectronCollection','$x.pt()')
332 For detail, see `PyAnalysisExamples/PlotTest.py`_
334 .. _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
339 h = fill (
None, classAndKey, value, criteria, nEvent)
348 def fill2 (hist, classAndKey, valueX, valueY, criteria="True", nEvent=100):
351 :param hist: reference to AIDA or ROOT histogram
352 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
353 :param valueX: physics parameter for X in string
354 :param valueY: physics parameter for Y in string
355 :param criteria: selection criteria
356 :param nEvent: number of event to be processed
358 For detail, see `fill`
369 if classAndKey !=
"":
375 commandX =
"valueX()"
381 commandY =
"valueY()"
385 if callable(criteria):
387 commandC =
"criteria()"
400 for iE
in range(nEvent):
404 obj = eval(commandSG)
407 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
420 for iC
in range(lSize):
448 if (iE+1) == bufEvent:
499 lpath =
'/stat/tmp/PyKernelHist'
503 titleX = valueX.__name__
507 titleY = valueY.__name__
510 h = book(lpath, titleY+
" vs "+titleX, 100, minX, maxX, 100, minY, maxY)
515 for iB
in range(len(bufX)):
524 def plot2 (classAndKey, valueX="$x", valueY="$x", criteria="True", nEvent=100):
527 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
528 :param valueX: physics parameter for X in string
529 :param valueY: physics parameter for Y in string
530 :param criteria: selection criteria
531 :param nEvent: number of event to be processed
533 For detail, see `plot`
536 h = fill2 (
None, classAndKey, valueX, valueY, criteria, nEvent)
545 def fillProf (hist, classAndKey, valueX, valueY, criteria="True", nEvent=100):
546 """Fill profile-histogram
548 :param hist: reference to AIDA or ROOT histogram
549 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
550 :param valueX: physics parameter for X in string
551 :param valueY: physics parameter for Y in string
552 :param criteria: selection criteria
553 :param nEvent: number of event to be processed
555 For detail, see `fill`
566 if classAndKey !=
"":
572 commandX =
"valueX()"
578 commandY =
"valueY()"
582 if callable(criteria):
584 commandC =
"criteria()"
597 for iE
in range(nEvent):
601 obj = eval(commandSG)
604 if hasattr(obj,
'size')
and hasattr(obj,
'__getitem__'):
617 for iC
in range(lSize):
645 if (iE+1) == bufEvent:
672 lpath =
'/stat/tmp/PyKernelHist'
676 titleX = valueX.__name__
680 titleY = valueY.__name__
683 h = bookProf(lpath, titleY+
" vs "+titleX, 100, minX, maxX)
688 for iB
in range(len(bufX)):
697 def plotProf (classAndKey, valueX="$x", valueY="$x", criteria="True", nEvent=100):
698 """Plot profile-histogram
700 :param classAndKey: combination of class name and key separeted with '#', 'Class#Key'
701 :param valueX: physics parameter for X in string
702 :param valueY: physics parameter for Y in string
703 :param criteria: selection criteria
704 :param nEvent: number of event to be processed
706 For detail, see `plot`
709 h = fillProf (
None, classAndKey, valueX, valueY, criteria, nEvent)
720 str = re.sub(
r"\$",
"", str)
722 cK = re.findall(
r'([\w_]+)#([\w_\*]+)',str)
727 aStr =
'retrieve(GNS.'+iCK[0]+
')'
729 bStr = iCK[0]+
"#"+iCK[1]
730 aStr =
'retrieve(GNS.'+iCK[0]+
',"'+iCK[1]+
'")'
731 str = re.sub(bStr, aStr, str)
738 Dump objects in StoreGate
745 print (GNS.StoreGate.pointer().
dump())
751 Unregister histogram from HistogramSvc
753 :param path: path to the histogram
757 athena> unregister("/stat/tmpHist")
760 return theApp.histSvc().unregisterObject(path)
766 Dump histograms in HistogramSvc
773 theApp.histSvc().
dump()
779 Set event loop type to pre-process
783 curEvent = theApp.curEvent()
785 eventLoopType = _PreProcess
791 Set event loop type to normal-process
795 eventLoopType = _NormalProcess
801 Set event loop type to hybrid-process
805 eventLoopType = _HybridProcess