Example #1
0
    def process ( evnts = 100 ) :
        """helper function to process the events 
        """
        if evnts < 0 : evnts = 100
        
        ievent = 0
        mc     = True 
        while ievent < evnts : 
            
            ievent += 1
            
            mc    = get ( 'MC/Particles' )
            if not mc :
                logger.warning ('No MC-container found: do you run over simulated data?')
                break 
        
            decs1 = get ( 'MC/Particles' , decay1          )
            decs2 = get ( 'MC/Particles' , decay1 & decay2 )
            
            l1 = len ( decs1 )
            l2 = len ( decs2 )

            cnts[0] += l1
            cnts[1] += l2
            
            if l1 :
                cnts[2] +=   ( 0 < l2 )
            
            if l1 and not l2 :

                for d in decs1 :
                    decays.add( d.decay() ) 
                    
                if config.Quiet and 3 >=config.OutputLevel :
                    for d in decs1 : print d.decay()
        
            run ( 1 )
            if not get ( '/Event') : break 

        setLogging(3) 
        logger.info ( 100*'*')
        logger.info ( "Generic decays     found     %s " % cnts[0] )
        logger.info ( "Decays-in-question found(1)  %s " % cnts[1] )
        logger.info ( "Decays-in-question found(2)  %s " % cnts[2] )
        if decays : 
            logger.info ( 100*'*')
            logger.info ( "Problematic decays [%d] are: " % len(decays) )
            for d in decays : logger.info ( "  %s " % d)
            logger.info ( 100*'*')
        setLogging(config.OutputLevel) 

        
        return SUCCESS
Example #2
0
def configure(config, colors=False):
    """Configure the application from parser data 
    """

    #
    if config.OutputLevel <= 3 and not config.Quiet:
        _vars = vars(config)
        _keys = _vars.keys()
        _keys.sort()
        logger.info('Configuration:')
        for _k in _keys:
            logger.info('  %15s : %-s ' % (_k, _vars[_k]))

    ## redefine output level for 'quiet'-mode
    if config.OutputLevel > 5:
        config.OutputLevel = 5
        logger.info('set OutputLevel to be %s ' % config.OutputLevel)

    if config.OutputLevel < 0:
        config.OutputLevel = 0
        logger.info('set OutputLevel to be %s ' % config.OutputLevel)

    if config.Quiet and 4 > config.OutputLevel:
        config.OutputLevel = 4
        logger.info('set OutputLevel to be %s ' % config.OutputLevel)
        from BenderTools.Utils import silence
        silence()
    #
    ## use coherent C++/Python logging levels
    setLogging(config.OutputLevel)
    #
    # some sanity actions:
    #
    config.RootInTES = config.RootInTES.strip()
    config.files = [i.strip() for i in config.files if i.strip()]
    #
    ## start the actual action:
    #
    from Configurables import DaVinci
    #
    ## get the file type for the file extension
    #
    from BenderTools.Parser import dataType
    pyfiles = [i for i in config.files if len(i) == 3 + i.rfind('.py')]
    files = [i for i in config.files if i not in pyfiles]

    from BenderTools.Parser import fileList
    for f in config.FileList:
        files += fileList(f)

    if not files and not config.ImportOptions:
        raise AttributeError('No data files are specified!')

    ## get some info from file names/extensision
    dtype, simu, ext = None, None, None
    if files:
        dtype, simu, ext = dataType(files)
        logger.debug('DataType,Simu&extension:"%s",%s,"%s" (from files)' %
                     (dtype, simu, ext))
    elif config.ImportOptions:
        from Bender.DataUtils import evtSelInput
        ifiles = evtSelInput(config.ImportOptions)
        dtype, simu, ext = dataType(ifiles)
        logger.debug(
            'DataType,Simu&extension:"%s",%s&"%s" (from EventSelector)' %
            (dtype, simu, ext))

    if '2013' == dtype:
        logger.info('Data type 2013 is redefined to be 2012')
        dtype = '2012'

        #
    if ext in ('gen', 'xgen', 'GEN', 'XGEN', 'ldst', 'LDST') and not simu:
        simu = True

    if dtype and dtype != config.DataType:
        logger.info('Redefine DataType from  %s to %s ' %
                    (config.DataType, dtype))
        config.DataType = dtype

    if simu and not config.Simulation:
        logger.info('Redefine Simulation from  %s to %s ' %
                    (config.Simulation, simu))
        config.Simulation = simu

    if config.Simulation and config.Lumi:
        logger.info('suppress Lumi for Simulated data')
        config.Lumi = False

    ## summary information (when available)
    from Configurables import LHCbApp
    LHCbApp().XMLSummary = 'summary.xml'

    daVinci = DaVinci(
        DataType=config.DataType,
        Simulation=config.Simulation,
        Lumi=config.Lumi,
    )

    if hasattr(config, 'TupleFile') and config.TupleFile:
        logger.info('Define TupleFile to be %s' % config.TupleFile)
        daVinci.TupleFile = config.TupleFile

    if hasattr(config, 'HistoFile') and config.HistoFile:
        logger.info('Define HistogramFile to be %s' % config.HistoFile)
        daVinci.HistogramFile = config.HistoFile

    if config.MicroDST or 'mdst' == ext or 'MDST' == ext or 'uDST' == ext:
        logger.info('Define input type as micro-DST')
        daVinci.InputType = 'MDST'

    #
    ## try to guess RootInTES
    #
    from BenderTools.Parser import hasInFile
    if hasInFile(files, 'CHARM.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/Charm'
        logger.info('RootInTES is set according to CHARM.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'LEPTONIC.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/Leptonic'
        logger.info('RootInTES is set according to LEPTONIC.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'BHADRON.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/Bhadron'
        logger.info('RootInTES is set according to BHADRON.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'PID.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/PID'
        logger.info('RootInTES is set according to PID.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'PSIX.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/PSIX'
        logger.info('RootInTES is set according to PSIX.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'PSIX0.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/PSIX0'
        logger.info('RootInTES is set according to PSIX0.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'BOTTOM.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/BOTTOM'
        logger.info('RootInTES is set according to BOTTOM.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'TURBO.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/Turbo'
        logger.info('RootInTES is set according to TURBO.MDST')
        daVinci.InputType = 'MDST'
    elif hasInFile(files, 'ALLSTREAMS.MDST') and not config.RootInTES:
        config.RootInTES = '/Event/AllStreams'
        logger.info('RootInTES is set according to ALLSTREAMS.MDST')
        daVinci.InputType = 'MDST'

    if config.RootInTES and 0 != config.RootInTES.find('/Event'):
        config.RootInTES = '/Event/' + config.RootInTES
    if config.RootInTES and '/' == config.RootInTES[-1]:
        config.RootInTES = config.RootInTES[:-1]
    if config.RootInTES and '/Event' != config.RootInTES:
        daVinci.RootInTES = config.RootInTES
    #
    ## check for Grid-access
    #
    if config.Grid:
        from Bender.DataUtils import hasGridProxy
        if not hasGridProxy():
            logger.warning(
                'GRID proxy is not available, switch off GRID-lookup')
            config.Grid = ''  ## SWITCH OFF Grid-lookup

    if not config.Simulation and config.DataType in ('2010', '2011', '2012',
                                                     '2013', '2015'):
        #
        ## try to use the latest available tags:
        #
        from Configurables import CondDB
        ## CondDB ( UseLatestTags = [ options.DataType ] )
        ## logger.info('Use latest tags for %s' % options.DataType )
        CondDB(LatestGlobalTagByDataType=config.DataType)
        logger.info('Use latest global tag for data type %s' % config.DataType)

    if config.Simulation:
        #
        ## try to get the tags from Rec/Header
        from BenderTools.GetDBtags import useDBTagsFromData
        tags = useDBTagsFromData(files,
                                 castor=config.Castor,
                                 grid=config.Grid,
                                 daVinci=daVinci,
                                 importOpts=config.ImportOptions,
                                 catalogs=config.XmlCatalogs)

    if config.IgnoreDQFlags:
        logger.info('DataQuality flags will be ignored')
        daVinci.IgnoreDQFlags = config.IgnoreDQFlags

    ## specific action for (x)gen files
    if ext in ('gen', 'xgen', 'GEN', 'XGEN'):
        from BenderTools.GenFiles import genAction
        genAction(ext)

    ## prepare to copy good/marked/tagged evenst
    if hasattr(config, 'OutputFile') and config.OutputFile:
        from BenderTools.GoodEvents import copyGoodEvents
        if 0 <= config.OutputFile.find('.'):
            copyGoodEvents(config.OutputFile)
        else:
            copyGoodEvents("%s.%s" % (config.OutputFile, ext))

    ##  OutptuLevel
    from Configurables import MessageSvc
    msgSvc = MessageSvc(OutputLevel=config.OutputLevel)

    ## import options (if specified)
    for i in config.ImportOptions:
        logger.info("Import options from file %s'" % i)
        from Gaudi.Configuration import importOptions
        importOptions(i)

    if colors:

        logger.debug('Add colorization to MessageSvc')

        from Configurables import MessageSvc
        msgsvc = MessageSvc(
            useColors=True,
            errorColorCode=['yellow', 'red'],
            warningColorCode=['red'],
            fatalColorCode=['blue', 'red'],
        )

        def _color_pre_start_action_():

            logger.debug('Add colorization to MessageSvc')
            from GaudiPython.Bindings import AppMgr
            _g = AppMgr()
            if not _g: return
            _s = _g.service('MessageSvc')
            if not _s: return
            _s.useColors = True
            _s.errorColorCode = ['yellow', 'red']
            _s.warningColorCode = ['red']
            _s.fatalColorCode = ['blue', 'red']
            ##_s.alwaysColorCode  = [ 'blue'            ]
            ##_s.infoColorCode    = [ 'green'           ]
            del _g, _s

        from Bender.Utils import addPreInitAction
        addPreInitAction(_color_pre_start_action_)

    ## set input data
    from Bender.Utils import setData
    setData(
        files,
        config.XmlCatalogs,  ## XML-catalogues 
        config.Castor,  ## use Castor/EOS lookup 
        config.Grid)  ## Use GRID to locate files

    return pyfiles
Example #3
0
By usage of this code one clearly states the disagreement 
with the smear campaign of Dr.O.Callot et al.: 
    ``No Vanya's lines are allowed in LHCb/Gaudi software.''

                  $Revision$
Last modification $Date$
               by $Author$
"""
# =============================================================================
__author__ = " Vanya BELYAEV [email protected] "
__date__ = " 2006-10-12 "
__version__ = " $Revision$ "
# =============================================================================
## import everything from bender
from Bender.Logger import setLogging
setLogging(3)
## import everything from bender
from Bender.MainMC import *
# =============================================================================
## optional logging
# =============================================================================
from Bender.Logger import getLogger
if '__main__' == __name__: logger = getLogger('Xuhao')
else: logger = getLogger(__name__)


# =============================================================================
## @class Jascha
#  Get list of trigger lines, efficient for D0 -> K- mu+ nu decays
#  @date 2016-01-14
#  @author Vanya BELYAEV [email protected]
Example #4
0
def noDecays ( config  , colors = False ) :
    """
    Look for ``no-decays'' events 
    """
    if not config.Simulation  :
        logger.warning ( 'Redefine "Simulation" to be true')
        
    if config.nEvents < 0 :
        logger.info ( 'Redefine "nEvents" to be 10000' ) 
        config.nEvents = 10000 
    if config.OutputLevel < 4 : config.OutputLevel = 4

    setLogging(3) 
    logger.info ( 100*'*')
    logger.info ( "Generic decay      %s " % config.GenericDecay    )
    logger.info ( "Decays-in-question %s " % config.DecayInQuestion )
    
    #
    ## configure it!
    #
    from BenderTools.DstExplorer import configure 
    configure ( config , colors = colors )
    
    from Bender.MainMC  import cpp,appMgr,get,run,MCDECTREE,SUCCESS, FAILURE  
    
    ## instantiate the application manager 
    gaudi  = appMgr ()
    
    ## initialize and read the first event
    
    sc = run ( 1 )
    mc = get ('/Event/MC/Particles')

    ievent = 0 

    decay1 = MCDECTREE ( config.GenericDecay )
    if not decay1.valid() :
        raise TypeError ( 'Invalid decay descriptor "%s"' % config.GenericDecay )

    decay2 = MCDECTREE ( config.DecayInQuestion )
    if not decay2.valid() :
        raise TypeError ( 'Invalid decay descriptor "%s"' % config.DecayInQuestion ) 

    cnts = [ cpp.StatEntity() , cpp.StatEntity() , cpp.StatEntity() ] 

    decays = set() 

    ## helper function to process the events 
    def process ( evnts = 100 ) :
        """helper function to process the events 
        """
        if evnts < 0 : evnts = 100
        
        ievent = 0
        mc     = True 
        while ievent < evnts : 
            
            ievent += 1
            
            mc    = get ( 'MC/Particles' )
            if not mc :
                logger.warning ('No MC-container found: do you run over simulated data?')
                break 
        
            decs1 = get ( 'MC/Particles' , decay1          )
            decs2 = get ( 'MC/Particles' , decay1 & decay2 )
            
            l1 = len ( decs1 )
            l2 = len ( decs2 )

            cnts[0] += l1
            cnts[1] += l2
            
            if l1 :
                cnts[2] +=   ( 0 < l2 )
            
            if l1 and not l2 :

                for d in decs1 :
                    decays.add( d.decay() ) 
                    
                if config.Quiet and 3 >=config.OutputLevel :
                    for d in decs1 : print d.decay()
        
            run ( 1 )
            if not get ( '/Event') : break 

        setLogging(3) 
        logger.info ( 100*'*')
        logger.info ( "Generic decays     found     %s " % cnts[0] )
        logger.info ( "Decays-in-question found(1)  %s " % cnts[1] )
        logger.info ( "Decays-in-question found(2)  %s " % cnts[2] )
        if decays : 
            logger.info ( 100*'*')
            logger.info ( "Problematic decays [%d] are: " % len(decays) )
            for d in decays : logger.info ( "  %s " % d)
            logger.info ( 100*'*')
        setLogging(config.OutputLevel) 

        
        return SUCCESS

    ## run it! 
    return process ( config.nEvents )
Example #5
0
def dumpDst ( config ) :
    ##
    from Bender.Logger import setLogging
    
    logger.info ( 100*'*') 
    if config.Quiet : 

        logger.info(' Trivial Bender-based script to explore the content of (x,mu,s,r,...)DSTs ' ) 
        import logging
        logging.disable ( logging.INFO  )
        config.OutputLevel = 5
        setLogging ( config.OutputLevel ) 
        
    else :
        
        setLogging ( config.OutputLevel )
        
        logger.info ( 100*'*')
        logger.info ( __doc__ ) 
        logger.info ( 100*'*')
        logger.info ( ' Author  : %s ' % __author__   ) 
        logger.info ( ' Version : %s ' % __version__  ) 
        logger.info ( ' Date    : %s ' % __date__     )
        logger.info ( 100*'*')

    from BenderTools.DstExplorer import configure 
    configure ( config , colors = config.Color ) 

    from BenderTools.Utils import totalSilence
    totalSilence( dod = config.DataOnDemand )
    
    from Bender.Main import appMgr, run, cpp
    if config.Simulation : import Bender.MainMC

    if 1000 < config.nEvents :
        logger.warning('Large number of events is required to process %s ' % config.nEvents )
        
    #
    ## instantiate the application manager
    #
    gaudi=appMgr ()

    import LHCbMath.Types 
    #
    ## copy the lines from Juan's script check_dst_contents.py
    #
    evtSvc = gaudi.evtSvc()
    SE     = cpp.StatEntity 
    def addEntry ( dct , key , val ) :
        if not dct.has_key(key) : dct[key] = SE()             
        dct[key] += float(val)
    
    dodSvc = gaudi.service('DataOnDemandSvc')
    
    nSelEvents = {}
    nObjects   = {}

    root = config.RootInTES 
    if not root :
        root = '/Event'
        logger.warning('Use "%s" as root, could be non-efficient' % root )

    from Ostap.progress_bar import progress_bar
    from itertools          import count 
    for iEvent in  progress_bar (
        xrange ( config.nEvents ) if 0 < config.nEvents else count () ,
        silent = not config.Color ) :
        #
        sc   = run(1)
        if sc.isFailure()       : break
        #
        if not evtSvc['/Event'] : break
        ##
        ## iEvent += 1
        #
        nodes = evtSvc.nodes ( node      = root ,
                               forceload = True )
        if not nodes :
            logger.warning ( "No nodes are selected for Root:'%s'" % root  )

        nodes = set ( nodes ) 
        links = set ()
        dods  = set ()
    
        #
        ## explore the regular nodes
        #
        for loc in nodes :
            loc0 = loc 
            data = evtSvc[loc]
            loc  = loc[:7] + ' ' + loc[7:] 
            if not data :
                addEntry ( nSelEvents , loc , 0 )
                addEntry ( nObjects   , loc , 0 )
            elif type( data ) == cpp.DataObject  : continue 
            else :
                #
                if   hasattr ( data , 'size'   ) : addEntry ( nObjects , loc , data.size()  )
                elif hasattr ( data , '__len__') : addEntry ( nObjects , loc , len ( data ) )
                else                             : addEntry ( nObjects , loc , 1            )
                #
                ## collect the links (if needed) 
                if config.FollowLinks: 
                    lnks = data.links()
                    for l in lnks : links.add ( l )
                    if '/pRec/DecReport' in loc0 : 
                        links.add ( loc0.replace ( '/pRec/'  , '/Rec/'  ) )
        #
        ## follow the links? Useful for packed (u)DST
        #
        if config.FollowLinks: 
            links = links - nodes 
            for loc in links:
                
                if config.RootInTES and not config.RootInTES in ( '/Event' , '/Event/' ) :  
                    if not config.RootInTES in loc : continue 

                data = evtSvc[loc]
                loc  = loc[:7] + '*' + loc[7:] 
                if          data is None             : continue 
                elif type ( data ) == cpp.DataObject : continue 
                elif not data :
                    addEntry ( nSelEvents , loc , 0 )
                    addEntry ( nObjects   , loc , 0 )
                elif hasattr ( data , 'size'   ) : addEntry ( nObjects , loc , data.size()  )
                elif hasattr ( data , '__len__') : addEntry ( nObjects , loc , len ( data ) )
                else                             : addEntry ( nObjects , loc , 1            )
                    
        #
        ## explore locations known for DOD
        #
        if config.DataOnDemand :
        
            for k in dodSvc.AlgMap .keys () : dods.add ( k ) 
            for k in dodSvc.NodeMap.keys () :
                obj = dodSvc.NodeMap[k]
                if 'DataObject' == obj : continue 
                dods.add ( k )
            
            dods = dods - nodes 
            dods = dods - links
        
            for loc in dods :

                if config.RootInTES :
                    if not config.RootInTES in loc : continue 
                
                if not config.Simulation :
                    if 'MC'   in loc : continue 
                    if 'Prev' in loc : continue 
                    if 'Next' in loc : continue 
                    
                data = evtSvc[loc]
                loc = loc[:7] + '+' + loc[7:] 
                if not data :
                    addEntry ( nSelEvents , loc , 0 )
                    addEntry ( nObjects   , loc , 0 )
                elif type( data ) == cpp.DataObject : continue 
                else :
                #
                    if   hasattr ( data , 'size'   ) : addEntry ( nObjects , loc , data.size()  )
                    elif hasattr ( data , '__len__') : addEntry ( nObjects , loc , len ( data ) )
                    else                             : addEntry ( nObjects , loc , 1            )

                
    keys = nObjects.keys()
    keys.sort()

    length  = 25
    for key in keys : length = max ( length , len  ( key ) ) 
    length += 2-7

    _printMessage = []

    lline   = ' +' + ( length + 58 ) * '-' + '+'
    _printMessage += [ lline ] 

    message = _fhdr_ % ( 'Total ' , '     Mean     ' , '  rms  ' ,  'min' , ' max ' ) 
    message = " | %s %s" % ( 'Location'.ljust(length) , message )

    _printMessage += [ message ] 
    _printMessage += [ lline   ]

    for loc in keys :
        item     = nObjects[loc]

        l = loc.replace('/Event/','')

        ## the actual formating 
        message = formatItem ( item )
        
        message = " | %s %s" % ( l.ljust(length) , message )
    
        _printMessage += [ message ] 
        
    _printMessage += [ lline ]
    iEvent += 1 
    _printMessage += [ "   Analysed %d events" % iEvent  ] 


    logger.info ( 100*'*')
    print '\n\n\n'
    ofile  = open ( config.SummaryFile , 'w' )     
    for line in _printMessage :
        print           line   
        print >> ofile, line 
    ofile.close()
    print '\n\n\n'
    logger.info ( 100*'*')