Beispiel #1
0
def py_tool(toolName, createIf=True, iface=None):
    """
    Helper function to retrieve a tool (owned by the ToolSvc) by name, using
    Gaudi python bindings.
     @param toolName: the name of the tool one wants to retrieve (possibly a
            fully qualified name as in: 'MyToolClass/TheToolName')
     @param createIf: If True, the tool will be created if it hasn't been yet
            instantiated.
     @param iface: type one wants to cast the tool to (can be a string or the
            PyCintex type)

    Ex:
     ## retrieve default interface (ie: GaudiKernel/IAlgTool)
     tool = py_tool('LArOnlDbPrepTool')
     assert(type(tool) == PyCintex.gbl.IAlgTool)

     ## retrieve with specified interface
     tool = py_tool('LArOnlDbPrepTool', iface='ILArOnlDbPrepTool')
     assert(type(tool) == PyCintex.gbl.ILArOnlDbPrepTool)

    """
    fullName = toolName
    t = toolName.split('/')
    toolType = t[0]
    if len(t) == 2: toolName = t[1]

    # handle pycomponents...
    from .Configurables import PyComponents
    if toolType in _PyAthenaBindingsCatalog.instances:
        pytype = _PyAthenaBindingsCatalog.init(toolType)
        # for types which have been pythonized, help the user
        # find the good interface...
        if iface is None: iface = pytype

    from GaudiPython.Bindings import gbl, InterfaceCast
    _py_tool = gbl.GaudiPython.Helper.tool
    toolSvc = py_svc('ToolSvc', iface='IToolSvc')
    tool = _py_tool(toolSvc, toolType, toolName, 0, createIf)
    if tool and not (iface is None):
        tool = InterfaceCast(iface).cast(tool)

    # if the component is actually a py-component,
    # retrieve the python object from the registry
    if toolName in PyComponents.instances:
        tool = PyComponents.instances[toolName]

    if tool:
        import PyAthena
        setattr(PyAthena.services.ToolSvc, toolName, tool)
    return tool
Beispiel #2
0
    def getJobOptions(self):
        self.StreamToFileTool = PyAthena.py_tool('JiveXML::StreamToFileTool',
                                                 iface='IProperty')
        self.StreamToServerTool = PyAthena.py_tool(
            'JiveXML::StreamToServerTool', iface='IProperty')

        # Some casting magic is needed to access algorithm properties
        from GaudiPython.Bindings import gbl, InterfaceCast
        vp1alg = PyAthena.py_alg('VP1EventProd')
        self.VP1EventProducer = InterfaceCast(gbl.IProperty).cast(vp1alg)

        self.msg.info("StreamToFileTool: %s" % self.StreamToFileTool)
        self.msg.info("StreamToServerTool: %s" % self.StreamToServerTool)
        self.msg.info("VP1EventProducer: %s" % self.VP1EventProducer)
Beispiel #3
0
def py_svc(svcName, createIf=True, iface=None):
    """
    Helper function to retrieve a service by name, using Gaudi python bindings.
     @param svcName: the name of the service one wants to retrieve (possibly a
            fully qualified name as in: 'MySvcClass/TheSvcName')
     @param createIf: If True, the service will be created if it hasn't been yet
            instantiated.
     @param iface: type one wants to cast the service to (can be a string or the
            PyCintex type)

    Ex:
     ## retrieve default interface (ie: GaudiKernel/IService)
     svc = py_svc('ThinningSvc/AodThinningSvc')
     assert(type(svc) == PyCintex.gbl.IService)

     ## retrieve special interface IThinningSvc
     ## Warning: a dict has to have been generated beforehand !!
     svc = py_svc('ThinningSvc/AodThinningSvc', createIf=True,
                  iface=PyCintex.gbl.IThinningSvc)
     assert(type(svc) == PyCintex.gbl.IThinningSvc)
    """
    fullName = svcName
    s = svcName.split('/')
    svcType = s[0]
    if len(s) == 2: svcName = s[1]

    # handle pycomponents...
    from .Configurables import PyComponents
    if svcType in _PyAthenaBindingsCatalog.instances:
        pytype = _PyAthenaBindingsCatalog.init(svcType)
        # for types which have been pythonized, help the user
        # find the good interface...
        if iface is None: iface = pytype

    from GaudiPython.Bindings import gbl, InterfaceCast
    svcLocator = gbl.Gaudi.svcLocator()
    svc = gbl.GaudiPython.Helper.service(svcLocator, fullName, createIf)
    if svc and not (iface is None):
        svc = InterfaceCast(iface).cast(svc)

    # if the component is actually a py-component,
    # retrieve the python object from the registry
    if svcName in PyComponents.instances:
        svc = PyComponents.instances[svcName]

    if svc:
        import PyAthena
        setattr(PyAthena.services, svcName, svc)
    return svc
#see evgen2xaod_peek.C for the c++ equivalent

import AthenaRootComps.ReadAthenaxAODHybrid #Fast xAOD reading
svcMgr.EventSelector.InputCollections=["../derivation/DAOD_TRUTH0.my.daod.root"]

algseq = CfgMgr.AthSequencer("AthAlgSeq")

import ROOT
tool = ROOT.AthAnalysisHelper.createTool("DerivationFramework::TruthDressingTool/TruthDressing")
theApp.initialize()

evt = PyAthena.py_svc("StoreGateSvc")

from GaudiPython.Bindings import InterfaceCast

tool = InterfaceCast('DerivationFramework::IAugmentationTool').cast(tool)
tool.initialize()

m_ll = [ ROOT.TH1D("m_ll","Dilepton mass",100,0,200) ];
m_ll += [ ROOT.TH1D("m_ll_dressed","Dressed Dilepton mass",100,0,200) ];

for i in range(0,theApp.size()):
  theApp.nextEvent()
  tool.addBranches()
  mc = evt['TruthParticles']
  elplus = [[],[]]
  elminus = [[],[]]
  muplus = [[],[]]
  muminus = [[],[]]
  for p in mc:
    if p.status() != 1: continue
#this program loops over an evgen file, converting it to xAOD on the fly, dresses the leptons (using the TruthDressingTool)
#and then builds the dilepton invariant mass
#see evgen2xaod_peek.C for the c++ equivalent

import ROOT

evt = ROOT.POOL.TEvent()
evt.readFrom("../evgen/my.evgen.root")
evt.getEntry(0) #initializes

from GaudiPython.Bindings import InterfaceCast

#Create instance of converter alg, configuring a property of it
#Note that this procedure will hopefully be improved soon
alg = ROOT.AthAnalysisHelper.createAlgorithm("xAODMaker::xAODTruthCnvAlg/myAlg")
myalg = InterfaceCast('IProperty').cast(alg)
myalg.setProperty( "AODContainerName" , "GEN_EVENT" )
alg.initialize()

tool = ROOT.AthAnalysisHelper.createTool("DerivationFramework::TruthDressingTool/TruthDressing")
#this is how to cast the tool in python so you have access to the addBranches method
tool = InterfaceCast('DerivationFramework::IAugmentationTool').cast(tool)
tool.initialize()

#these next two lines are needed for now, 
#until python bindings for retrieve method of TEvent is working
from AthenaPython import PyAthena
sg = PyAthena.py_svc("StoreGateSvc")

m_ll = [ ROOT.TH1D("m_ll","Dilepton mass",100,0,200) ];
m_ll += [ ROOT.TH1D("m_ll_dressed","Dressed Dilepton mass",100,0,200) ];
import AthenaPoolCnvSvc.ReadAthenaPool
svcMgr.EventSelector.InputCollections=["../evgen/my.evgen.root"]

algseq = CfgMgr.AthSequencer("AthAlgSeq")
algseq += CfgMgr.xAODMaker__xAODTruthCnvAlg("myAlg",AODContainerName="GEN_EVENT")


import ROOT
tool = ROOT.AthAnalysisHelper.createTool("DerivationFramework::TruthDressingTool/TruthDressing")
theApp.initialize()

evt = PyAthena.py_svc("StoreGateSvc")

from GaudiPython.Bindings import InterfaceCast

tool = InterfaceCast('DerivationFramework::IAugmentationTool').cast(tool)
tool.initialize()

m_ll = [ ROOT.TH1D("m_ll","Dilepton mass",100,0,200) ];
m_ll += [ ROOT.TH1D("m_ll_dressed","Dressed Dilepton mass",100,0,200) ];

for i in range(0,theApp.size()):
  theApp.nextEvent()
  tool.addBranches() #does the lepton dressing
  mc = evt['TruthParticles']
  elplus = [[],[]]
  elminus = [[],[]]
  muplus = [[],[]]
  muminus = [[],[]]
  for p in mc:
    if p.status() != 1: continue
Beispiel #7
0
ROOT.POOL.TEvent.Init("POOLRootAccess/basicxAOD.opts") #fast xAOD reading

evt = ROOT.POOL.TEvent()
evt.readFrom("DAOD_TRUTH0.my.daod.root")
evt.getEntry(0) #initializes

tool = ROOT.AthAnalysisHelper.createTool("DerivationFramework::TruthDressingTool/TruthDressing")

#these next two lines are needed for now, 
#until python bindings for retrieve method of TEvent is working
from AthenaPython import PyAthena
sg = PyAthena.py_svc("StoreGateSvc")

#this is how to cast the tool in python so you have access to the addBranches method
from GaudiPython.Bindings import InterfaceCast
tool = InterfaceCast('DerivationFramework::IAugmentationTool').cast(tool)
tool.initialize()

m_ll = [ ROOT.TH1D("m_ll","Dilepton mass",100,0,200) ];
m_ll += [ ROOT.TH1D("m_ll_dressed","Dressed Dilepton mass",100,0,200) ];

for i in range(0,evt.getEntries()):
  evt.getEntry(i)
  tool.addBranches() #decorates leptons with pt_dressed etc
  mc = sg['TruthParticles'] #retrieve objects via the sg storegate obj
  elplus = [[],[]]
  elminus = [[],[]]
  muplus = [[],[]]
  muminus = [[],[]]
  for p in mc:
    if p.status() != 1: continue