#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# $Id:$ 
# =============================================================================
## @file 	
#  Simple  script to extract DB-tags from the file 
#
#  @author Thomas RUF 
#  @date   2012-10-24
#
#                    $Revision:$
#  Last modification $Date:$
#                 by $Author:$
#
# =============================================================================
"""
Simple script to extract DB-tags from the file

"""
__author__  = "Thomas RUF, Vanya BELYAEV"
__date__    = "2012-10-24"
__version__ = "Revision"
# =============================================================================
## some "fix"
# @todo check if it really needed 
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
# =============================================================================
## helper function to get DB-tags
#  @author Thomas RUF
#  @author Vanya Belyaev Ivan.Belyaev@itep.ru
#  @date  2013-02-18
def _get_db_tags_ ( files ) :
    """
    Helper function to get DB-tags 
    """
    tags = {}

    from GaudiConf.IOHelper import IOHelper
    evtsel = IOHelper().inputFiles ( files )
       
    from Configurables import LHCbApp
    app = LHCbApp()
    
    from GaudiPython.Bindings import AppMgr
 
    import Bender.Fixes_Gaudi

    gaudi = AppMgr( outputlevel = 6 )

    gaudi.run(1)
    evtsvc = gaudi.evtSvc()

    for i in range (0,5) : print 100*'*'
    
    for header in ( '/Event/Rec/Header' ,
                    '/Event/MC/Header'  ,
                    '/Event/Gen/Header' ) : 
        
        hdr = evtsvc [ header ]
        if not hdr : continue 
        tgs = hdr.condDBTags()
        for t in tgs :
            tag = t.first
            val = t.second 
            if not tags.has_key( tag ) : tags [ tag ] = set()
            tags[tag] . add ( val )
            
    for t in tags :
        ts = tags[t]
        if 1 != len ( ts ) : tags[t] = list ( ts ) 
        else               : tags[t] = list ( ts ) [0]

    gaudi.stop     ()
    gaudi.finalize ()    
    del gaudi
    
    return tags
    

# =============================================================================
## helper function to get DB-tags
#  @author Thomas RUF
#  @author Vanya Belyaev Ivan.Belyaev@itep.ru
#  @date  2013-02-18
def get_db_tags ( silent = True , files = [] ) :
    """
    Helper function to get DB-tags 
    """
    import sys 
    if not files :
        if len(sys.argv) > 1 : 
            files = sys.argv[1:] 

    ## no valid 
    if not files : return {}
    
    tags = {}

    try:
        
        from Ostap.Utils import mute 
        with mute ( silent , silent ) :
            tags = _get_db_tags_ ( files )

    except: tags = {}
                    
    return tags


# =============================================================================
if __name__ == '__main__' :

    print 'DBTags: %s ' % get_db_tags ( silent = True ) 
        
# =============================================================================
# The END 
# =============================================================================
