| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# GaudiAlgs.py

Go to the documentation of this file.

    00001 #!/usr/bin/env python
    00002 # =============================================================================
    00003 # $Id: GaudiAlgs.py,v 1.2 2008/07/15 16:05:53 marcocle Exp $
    00004 # =============================================================================
    00005 ## @file
    00006 #
    00007 # Helper module, which effectively 'imports' few useful C++ algorithmic
    00008 # base classes into Python
    00009 #
    00010 #
    00011 #              The major imported classes are :
    00012 #
    00013 # - GaudiAlgo - analogue for GaudiAlgorithm C++ class from GaudiAlg package
    00014 # - HistoAlgo - analogue for GaudiHistoAlg  C++ class from GaudiAlg package
    00015 # - TupleAlgo - analogue for GaudiTupleAlg  C++ class from GaudiAlg package
    00016 #
    00017 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00018 # @date 2006-11-26
    00019 # =============================================================================
    00020 """
    00021 *******************************************************************************
    00022 *                                                * 'Physisics do not like it, *
    00023 *                                                *  physisics do not need it, *
    00024 *                                                *  physisics do not use  it' *
    00025 *                                                * ****************************
    00026 *                                                                             *
    00027 * Helper module, which effectively 'imports' few useful C++ algorithmic       *
    00028 * base classes into Python                                                    *
    00029 *                                                                             *
    00030 *******************************************************************************
    00031 *              The major imported classes are :                               *
    00032 *                                                                             *
    00033 * (1) GaudiAlgo - analogue for GaudiAlgorithm C++ class from GaudiAlg package *
    00034 * (2) HistoAlgo - analogue for GaudiHistoAlg  C++ class from GaudiAlg package *
    00035 * (3) TupleAlgo - analogue for GaudiTupleAlg  C++ class from GaudiAlg package *
    00036 *******************************************************************************
    00037 """
    00038 # =============================================================================
    00039 __author__ = 'Vanya BELYAEV  Ivan.Belyaev@lapp.in2p3.fr'
    00040 # =============================================================================
    00041
    00042 # import core of Gaudi
    00043 import GaudiPython as gaudimodule
    00044
    00045 ## get the namespaces
    00046 GaudiPython        = gaudimodule.gbl.GaudiPython
    00047 GaudiAlg           = gaudimodule.gbl.GaudiAlg
    00048 ## "typedef" for GaudiPython::Vector
    00049 GaudiPython.Vector = gaudimodule.gbl.std.vector('double')
    00050 ## "typedef" for GaudiPython::Matrix
    00051 GaudiPython.Matrix = gaudimodule.gbl.std.vector('std::vector<double>')
    00052 ## histogram and N-Tuple universal identifier
    00053 HID       = GaudiAlg.ID
    00054 HistoID   = HID
    00055 TID       = HID
    00056 TupleID   = TID
    00057 ##
    00058 SUCCESS    = gaudimodule.SUCCESS
    00059 iAlgorithm = gaudimodule.iAlgorithm
    00060
    00061 # =============================================================================
    00062 ## Useful method to locate the tool a certain
    00063 #
    00064 #  Usage:
    00065 #
    00066 #  @code
    00067 #
    00068 #      # locate public tool
    00069 #      t1 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator')
    00070 #      # locate private tool
    00071 #      t2 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator',parent=self)
    00072 #      # locate public tool with defined name
    00073 #      t3 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt1')
    00074 #      # locate private tool with defined name
    00075 #      t4 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt2',parent=self)
    00076 #      # locate public tool with defined name
    00077 #      t5 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt3')
    00078 #      # locate private tool with defined name
    00079 #      t6 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt4',parent=self)
    00080 #
    00081 #  @endcode
    00082 #
    00083 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00084 #  @date 2006-11-26
    00085 def _tool_ ( self             ,
    00086              interface        ,
    00087              typename         ,
    00088              name     = None  ,
    00089              parent   = None  ,
    00090              create   = True  ) :
    00091     """
    00092     Useful method to locate the tool a certain
    00093
    00094     Usage:
    00095
    00096     # locate public tool
    00097     t1 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator')
    00098     # locate private tool
    00099     t2 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator',parent=self)
    00100     # locate public tool with defined name
    00101     t3 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt1')
    00102     # locate private tool with defined name
    00103     t4 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt2',parent=self)
    00104     # locate public tool with defined name
    00105     t5 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt3')
    00106     # locate private tool with defined name
    00107     t6 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt4',parent=self)
    00108
    00109     """
    00110     if not interface : interface = gaudimodule.GaudiPython.IAlgTool
    00111     if not parent    : parent    = self
    00112     if name          : typename  += '/' + name
    00113     _tool = GaudiPython.AlgDecorator.tool_( self , typename , parent , create )
    00114     if not _tool : return None
    00115     _tool = gaudimodule.InterfaceCast(interface)(_tool)
    00116     if not _tool :
    00117         self.Warning('Invalid cast to interface %s' % interface )
    00118         return None
    00119     return _tool
    00120
    00121 # =============================================================================
    00122 ## Useful method to locate a service:
    00123 #
    00124 #     Usage:
    00125 #
    00126 #  @code
    00127 #
    00128 #     ntsvc = self.svc( INTupleSvc , 'NTUpleSvc' )
    00129 #
    00130 #  @endcode
    00131 #
    00132 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00133 #  @date 2006-11-26
    00134 def _service_ ( self              ,
    00135                 interface         ,
    00136                 name              ,
    00137                 create   = False  ) :
    00138     """
    00139     Useful method to locate a service:
    00140
    00141     Usage:
    00142
    00143     ntsvc = self.svc( INTupleSvc , 'NTUpleSvc' )
    00144
    00145     """
    00146     if not interface : interface = gaudimodule.GaudiPython.IInterface
    00147     _svc = GaudiPython.AlgDecorator.svc_ ( self , name , create )
    00148     if not _svc : return None
    00149     _svc = gaudimodule.InterfaceCast(interface)(_svc)
    00150     if not _svc :
    00151         self.Warning('Invalid cast to interface %s' % interface )
    00152         return None
    00153     return _svc
    00154
    00155 # =============================================================================
    00156 ## The constructor from unique algorithm instance name,
    00157 #
    00158 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00159 #  @date 2006-11-26
    00160 def _init_ ( self , name ) :
    00161     """
    00162     The constructor from unique algorithm instance name,
    00163     """
    00164     self._Base.__init__( self , self , name )
    00165     appMgr = gaudimodule.AppMgr()
    00166     algMgr = appMgr._algmgr
    00167     status = algMgr.addAlgorithm( self )
    00168     if status.isFailure() :
    00169         raise RuntimeError, 'Unable to add Algorithm "' + name + '"'
    00170     iAlgorithm.__init__ ( self , name , self )
    00171
    00172 # =============================================================================
    00173 ## The default initialization (initilization of base C++ class + data
    00174 #
    00175 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00176 #  @date 2006-11-26
    00177 def _initialize_ ( self ) :
    00178     """
    00179     The default initialzation (initilization of base C++ class + data
    00180     """
    00181     status = self._Base.initialize_( self )
    00182     if status.isFailure() : return status
    00183
    00184     # set the basic services
    00185     _e = self._Base.evtSvc( self )
    00186     _s = gaudimodule.InterfaceCast(gaudimodule.gbl.IService)(_e)
    00187     self._evtSvc_ = gaudimodule.iDataSvc ( _s.name() , _e )
    00188
    00189     _d = self._Base.detSvc( self )
    00190     _s = gaudimodule.InterfaceCast(gaudimodule.gbl.IService)(_d)
    00191     self._detSvc_ = gaudimodule.iDataSvc ( _s.name() , _d )
    00192
    00193     return status
    00194
    00195 # =============================================================================
    00196 ## The default initialization (initilization of base C++ class + data memebrs)
    00197 #
    00198 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00199 #  @date 2006-11-26
    00200 def _initialize_histo_ ( self ) :
    00201     """
    00202     The default initialzation (initilization of base C++ class + data memebrs)
    00203     """
    00204     status = _initialize_( self )
    00205     if status.isFailure() : return status
    00206
    00207     # set the basic services
    00208     _h = self._Base.histoSvc( self )
    00209     _s = gaudimodule.InterfaceCast(gaudimodule.gbl.IService)(_h)
    00210     self._histoSvc_ = gaudimodule.iHistogramSvc ( _s.name() , _h )
    00211
    00212     return status
    00213
    00214 # =============================================================================
    00215 ## The default initilization (initialization of base C++ class + data memebrs)
    00216 #
    00217 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00218 #  @date 2006-11-26
    00219 def _initialize_tuple_ ( self ) :
    00220     """
    00221     The default initialzation (initilization of base C++ class + data memebrs)
    00222     """
    00223     status = _initialize_histo_( self )
    00224     if status.isFailure() : return status
    00225
    00226     # set the basic services
    00227     if self.produceNTuples() :
    00228         _n = self._Base.ntupleSvc( self )
    00229         _s = gaudimodule.InterfaceCast(gaudimodule.gbl.IService)(_n)
    00230         self._ntupleSvc_ = gaudimodule.iNTupleSvc ( _s.name() , _n )
    00231
    00232     if self.produceEvtCols() :
    00233         _n = self._Base.evtColSvc( self )
    00234         _s = gaudimodule.InterfaceCast(gaudimodule.gbl.IService)(_n)
    00235         self._evtcolSvc_ = gaudimodule.iNTupleSvc ( _s.name() , _n )
    00236
    00237     return status
    00238
    00239 # =============================================================================
    00240 ## Trivial helper function to access Event Data and Event Data Service
    00241 #
    00242 #    Usage:
    00243 #
    00244 #  @code
    00245 #
    00246 #    # get event data service
    00247 #    svc = self.evtSvc()
    00248 #
    00249 #    # get the data
    00250 #    hits = self.evtSvc('MC/Calo/Hits')
    00251 #
    00252 #  @endcode
    00253 #
    00254 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00255 #  @date 2006-11-26
    00256 def _evtSvc   ( self , location = None ) :
    00257     """
    00258     Trivial helper function to access Event Data and Event Data Service
    00259
    00260     Usage:
    00261
    00262     # get event data service
    00263     svc = self.evtSvc()
    00264
    00265     # get the data
    00266     hits = self.evtSvc('MC/Calo/Hits')
    00267     """
    00268     if not location :
    00269         return self._evtSvc_
    00270     return self._evtSvc_[location]
    00271
    00272 # =============================================================================
    00273 ## Trivial helper function to access Detector Data and Detector  Data Service
    00274 #
    00275 #    Usage:
    00276 #
    00277 #  @code
    00278 #
    00279 #    # get detector data service
    00280 #    svc = self.detSvc()
    00281 #
    00282 #    # get the data
    00283 #    lhcb = self.detSvc('/dd/Structure/LHCb')
    00284 #
    00285 #  @endcode
    00286 #
    00287 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00288 #  @date 2006-11-26
    00289 def _detSvc   ( self ) :
    00290     """
    00291     Trivial helper function to access Detector Data and Event Data Service
    00292
    00293     Usage:
    00294     # get detector data service
    00295     svc = self.detSvc()
    00296
    00297     # get the data
    00298     lhcb = self.detSvc('/dd/Structure/LHCb')
    00299     """
    00300     if not location :
    00301         return self._detSvc_
    00302     return self._detSvc_[location]
    00303
    00304 # =============================================================================
    00305 ## Trivial helper function to access Histogram  Data and Histogram  Data Service
    00306 #
    00307 #    Usage:
    00308 #
    00309 #  @code
    00310 #
    00311 #    # get histogram data service
    00312 #    svc = self.histoSvc()
    00313 #
    00314 #    # get the data
    00315 #    histo = self.histoSvc('/stat/Calo/1')
    00316 #
    00317 #  @endcode
    00318 #
    00319 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00320 #  @date 2006-11-26
    00321 def _histoSvc  ( self , address = None ) :
    00322     """
    00323     Trivial helper function to access Histogram  Data and Histogram  Data Service
    00324
    00325     Usage:
    00326
    00327     # get histogram data service
    00328     svc = self.histoSvc()
    00329
    00330     # get the data
    00331     histo = self.histoSvc('/stat/Calo/1')
    00332     """
    00333     if not address : return self._histoSvc_
    00334     return self._histoSvc_[ address ]
    00335
    00336 # =============================================================================
    00337 ## Trivial function to access the data in TES
    00338 def _get      ( self , location ) :
    00339     """
    00340     Trivial function to access the data in TES
    00341     """
    00342     return self._evtSvc_[location]
    00343
    00344 # =============================================================================
    00345 ## Trivial function to access the data in TDS
    00346 def _getDet   ( self , location ) :
    00347     """
    00348     Trivial function to access the data in TDS
    00349     """
    00350     return self._detSvc_[location]
    00351
    00352 # =============================================================================
    00353 ## Trivial helper function to access NTuple Service
    00354 def _ntupleSvc ( self ) :
    00355     """
    00356     Trivial function to access N-Tuple Service
    00357     """
    00358     return self._ntupleSvc_
    00359
    00360 # =============================================================================
    00361 ## Trivial helper function to access Event Collection Service
    00362 def _evtcolSvc ( self ) :
    00363     """
    00364     Trivial function to access Event Collection Service
    00365     """
    00366     return self._evtcolSvc_
    00367
    00368
    00369 # =============================================================================
    00370 ## The default finalization (finalization of base C++ class)
    00371 def _finalize_   ( self ) :
    00372     """
    00373     The default finalization : finalize the base C++ class
    00374     """
    00375     status = self._Base.finalize_  ( self )
    00376     return status
    00377 # =============================================================================
    00378 ## Dummy method returning success
    00379 def _success_ ( self ) : return gaudimodule.SUCCESS
    00380
    00381
    00382 # =============================================================================
    00383 ## check the existency of the proeprty with the given name
    00384 def _hasProperty_ ( self , pname ) :
    00385     """
    00386     The trivial function which checks the existence of the property with given name
    00387     """
    00388     return gaudimodule.gbl.Gaudi.Utils.hasProperty ( self , pname )
    00389
    00390 # =============================================================================
    00391 ## get the value of the given property
    00392 def _getProperty_ ( self , pname ) :
    00393     """
    00394     Get the property by name
    00395     """
    00396     if not self.hasProperty( pname ) :
    00397         raise AttributeError, 'property %s does not exist' % pname
    00398     return iAlgorithm.__getattr__( self , pname )
    00399
    00400 # =============================================================================
    00401 ## set the value for the given property
    00402 def _setProperty_ ( self , pname , pvalue ) :
    00403     """
    00404     Set the property from the value
    00405     """
    00406     if not self.hasProperty( pname ) :
    00407         raise AttributeError, 'property %s does not exist' % pname
    00408     return iAlgorithm.__setattr__ ( self , pname , pvalue )
    00409
    00410 # =============================================================================
    00411 ## get the attribute or property
    00412 def _get_attr_ ( self , pname ) :
    00413     """
    00414     Get the atribute (or property)
    00415     - if the attribute name corresponds to the property name, property value is returned
    00416     """
    00417     if self.hasProperty( pname ) :
    00418         return iAlgorithm.__getattr__ ( self , pname )
    00419     raise AttributeError, 'attribute/property %s does not exist' % pname
    00420
    00421 # =============================================================================
    00422 ## set the attribute or property
    00423 def _set_attr_ ( self , pname , pvalue ) :
    00424     """
    00425     Set the attribute (or property) :
    00426     - if the attribute name corresponds to the property name, the property is updated
    00427     """
    00428     if not self.hasProperty( pname ) : self.__dict__[pname] = pvalue
    00429     else : iAlgorithm.__setattr__ ( self , pname , pvalue )
    00430
    00431
    00432 _GaudiAlgorithm = GaudiPython.PyAlg( 'GaudiAlgorithm' )
    00433 _GaudiHistoAlg  = GaudiPython.PyAlg( 'GaudiHistoAlg'  )
    00434 _GaudiTupleAlg  = GaudiPython.PyAlg( 'GaudiTupleAlg'  )
    00435
    00436 # =============================================================================
    00437 ## @class GaudiAlgo
    00438 #  the base class for all algorithm
    00439 #  Python-image of C++ clkass GaudiAlgorithm
    00440 #
    00441 #  Usage:
    00442 #
    00443 #  @code
    00444 #
    00445 #  from gaudimodule import SUCCESS
    00446 #  from GaudiAlgs   import GaudiAlgo
    00447 #
    00448 #  class MyClass(GaudiAlgo) :
    00449 #       ' My specific Algorithm, derived from GaudiAlgo base class '
    00450 #       def __init__( self , name ) :
    00451 #            'Constructor from algorithm instance name'
    00452 #            #invoke the constructor of base class
    00453 #             GaudiAlgo.__init__(self , name )
    00454 #
    00455 #       def initialize ( self ) :
    00456 #           'Algorithm initialization'
    00457 #           # initialize the base class
    00458 #           status = GaudiAlgo.initialize( self )
    00459 #           if status.isFailure() : return status
    00460 #
    00461 #           # locate the services and tools
    00462 #
    00463 #           # locate some tool:
    00464 #           extrapolator = self.tool(ITrExtrapolator,'TrExtrapolator')
    00465 #
    00466 #           # locate the service
    00467 #           rndmSvc = self.svc(IRndmGenSvc, 'RndmGenSvc')
    00468 #
    00469 #           return SUCCESS
    00470 #
    00471 #
    00472 #       def execute ( self ) :
    00473 #            'Major method (from IAlgorithm interface)'
    00474 #
    00475 #           # get some data from Transient Event Store
    00476 #           tracks = self.get('/Event/Rec/Tracks')
    00477 #
    00478 #           # use counters
    00479 #           c1 = self.counter('#Tracks')
    00480 #           c2 = self.counter('No Tracks')
    00481 #           if tracks.empty :
    00482 #              c2+=1
    00483 #           c1 += tracks->size()
    00484 #
    00485 #           if 1000 < tracks.size() :
    00486 #                return self.Error('The event is *VERY* busy')
    00487 #
    00488 #           return SUCCESS
    00489 #
    00490 #  @endcode
    00491 #
    00492 #  @see GaudiAlgorithm
    00493 #
    00494 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00495 #  @date 2006-11-26
    00496 class GaudiAlgo ( _GaudiAlgorithm , iAlgorithm ) :
    00497     """
    00498 *******************************************************************************
    00499 *                                                * 'Physisics do not like it, *
    00500 *                                                *  physisics do not need it, *
    00501 *                                                *  physisics do not use  it' *
    00502 *                                                * ****************************
    00503 *  Usage:                                                                     *
    00504 *                                                                             *
    00505 *  from gaudimodule import SUCCESS                                            *
    00506 *  from GaudiAlgs   import GaudiAlgo                                          *
    00507 *                                                                             *
    00508 *  class MyClass(GaudiAlgo) :                                                 *
    00509 *       ' My specific Algorithm, derived from GaudiAlgo base class '          *
    00510 *       def __init__( self , name ) :                                         *
    00511 *            'Constructor from algorithm instance name'                       *
    00512 *             #invoke the constructor of base class                           *
    00513 *             GaudiAlgo.__init__(self , name )                                *
    00514 *                                                                             *
    00515 *       def initialize ( self ) :                                             *
    00516 *           'Algorithm initialization'                                        *
    00517 *           # initialize the base class                                       *
    00518 *           status = GaudiAlgo.initialize( self )                             *
    00519 *           if status.isFailure() : return status                             *
    00520 *                                                                             *
    00521 *           # locate the services and tools                                   *
    00522 *                                                                             *
    00523 *           # locate some tool:                                               *
    00524 *           extrapolator = self.tool(ITrExtrapolator,'TrExtrapolator')        *
    00525 *                                                                             *
    00526 *           # locate the service                                              *
    00527 *           rndmSvc = self.svc(IRndmGenSvc, 'RndmGenSvc')                     *
    00528 *                                                                             *
    00529 *           return SUCCESS                                                    *
    00530 *                                                                             *
    00531 *                                                                             *
    00532 *       def execute ( self ) :                                                *
    00533 *            'Major method (from IAlgorithm interface)'                       *
    00534 *                                                                             *
    00535 *           # get some data from Transient Event Store                        *
    00536 *           tracks = self.get('/Event/Rec/Tracks')                            *
    00537 *                                                                             *
    00538 *           # use counters                                                    *
    00539 *           c1 = self.counter('#Tracks')                                      *
    00540 *           c2 = self.counter('No Tracks')                                    *
    00541 *           if tracks.empty :                                                 *
    00542 *              c2+=1                                                          *
    00543 *           c1 += tracks->size()                                              *
    00544 *                                                                             *
    00545 *           if 1000 < tracks.size() :                                         *
    00546 *                return self.Error('The event is *VERY* busy')                *
    00547 *                                                                             *
    00548 *           return SUCCESS                                                    *
    00549 *                                                                             *
    00550 *******************************************************************************
    00551     """
    00552     pass
    00553
    00554 # =============================================================================
    00555 ## @class HistoAlgo
    00556 #  The base class for easy histogramming
    00557 #
    00558 #  Usage:
    00559 #
    00560 #
    00561 #  @code
    00562 #
    00563 #  from gaudimodule import SUCCESS
    00564 #  from GaudiAlgs   import HistoAlgo
    00565 #
    00566 #  class MyClass(HistoAlgo) :
    00567 #       ' My specific Algorithm, derived from GaudiAlgo base class '
    00568 #       def __init__( self , name ) :
    00569 #            'Constructor from algorithm instance name'
    00570 #             #invoke the constructor of base class
    00571 #             HistoAlgo.__init__(self , name )
    00572 #
    00573 #       def execute ( self ) :
    00574 #            'Major method (from IAlgorithm interface)'
    00575 #
    00576 #           # get some data from Transient Event Store
    00577 #           tracks = self.get('/Event/Rec/Tracks')
    00578 #
    00579 #           self.plot1D ( tracks->size() , '#tracks' , 0 , 100 )
    00580 #
    00581 #           return SUCCESS
    00582 #
    00583 #  @endcode
    00584 #
    00585 # Alternatively the histogram  could be booked in advance:
    00586 #
    00587 #  @code
    00588 #
    00589 #  class MyClass(HistoAlgo) :
    00590 #       ' My specific Algorithm, derived from GaudiAlgo base class '
    00591 #       def __init__( self , name ) :
    00592 #            'Constructor from algorithm instance name'
    00593 #             #invoke the constructor of base class
    00594 #             HistoAlgo.__init__(self , name )
    00595 #
    00596 #       def initialize ( self ) :
    00597 #           'Algorithm initialization'
    00598 #           # initialize the base class
    00599 #           status = HistoAlgo.initialize( self )
    00600 #           if status.isFailure() : return status
    00601 #
    00602 #           # book the histogram
    00603 #           self.h1 = selff.book1D ( '#tracks' , 0 , 100 )
    00604 #
    00605 #           return SUCCESS
    00606 #
    00607 #
    00608 #       def execute ( self ) :
    00609 #            'Major method (from IAlgorithm interface)'
    00610 #
    00611 #           # get some data from Transient Event Store
    00612 #           tracks = self.get('/Event/Rec/Tracks')
    00613 #
    00614 #           # fill the histogram
    00615 #           self.h1.fill ( tracks->size() )
    00616 #
    00617 #            return SUCCESS
    00618 #  @endcode
    00619 #
    00620 #  @see GaudiHistoAlg
    00621 #
    00622 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00623 #  @date 2006-11-26
    00624 class HistoAlgo ( _GaudiHistoAlg , iAlgorithm ) :
    00625     """
    00626 *******************************************************************************
    00627 *                                                * 'Physisics do not like it, *
    00628 *                                                *  physisics do not need it, *
    00629 *                                                *  physisics do not use  it' *
    00630 *                                                * ****************************
    00631 *  Usage:                                                                     *
    00632 *                                                                             *
    00633 *  from gaudimodule import SUCCESS                                            *
    00634 *  from GaudiAlgs   import HistoAlgo                                          *
    00635 *                                                                             *
    00636 *  class MyClass(HistoAlgo) :                                                 *
    00637 *       ' My specific Algorithm, derived from GaudiAlgo base class '          *
    00638 *       def __init__( self , name ) :                                         *
    00639 *            'Constructor from algorithm instance name'                       *
    00640 *             #invoke the constructor of base class                           *
    00641 *             HistoAlgo.__init__(self , name )                                *
    00642 *                                                                             *
    00643 *       def execute ( self ) :                                                *
    00644 *            'Major method (from IAlgorithm interface)'                       *
    00645 *                                                                             *
    00646 *           # get some data from Transient Event Store                        *
    00647 *           tracks = self.get('/Event/Rec/Tracks')                            *
    00648 *                                                                             *
    00649 *           self.plot1D ( tracks->size() , '#tracks' , 0 , 100 )              *
    00650 *                                                                             *
    00651 *           return SUCCESS                                                    *
    00652 *                                                                             *
    00653 * Alternatively the histogram  could be booked in advance:                    *
    00654 *                                                                             *
    00655 *  class MyClass(HistoAlgo) :                                                 *
    00656 *       ' My specific Algorithm, derived from GaudiAlgo base class '          *
    00657 *       def __init__( self , name ) :                                         *
    00658 *            'Constructor from algorithm instance name'                       *
    00659 *             #invoke the constructor of base class                           *
    00660 *             HistoAlgo.__init__(self , name )                                *
    00661 *                                                                             *
    00662 *       def initialize ( self ) :                                             *
    00663 *           'Algorithm initialization'                                        *
    00664 *           # initialize the base class                                       *
    00665 *           status = HistoAlgo.initialize( self )                             *
    00666 *           if status.isFailure() : return status                             *
    00667 *                                                                             *
    00668 *           # book the histogram                                              *
    00669 *           self.h1 = selff.book1D ( '#tracks' , 0 , 100 )                    *
    00670 *                                                                             *
    00671 *           return SUCCESS                                                    *
    00672 *                                                                             *
    00673 *                                                                             *
    00674 *       def execute ( self ) :                                                *
    00675 *            'Major method (from IAlgorithm interface)'                       *
    00676 *                                                                             *
    00677 *           # get some data from Transient Event Store                        *
    00678 *           tracks = self.get('/Event/Rec/Tracks')                            *
    00679 *                                                                             *
    00680 *           # fill the histogram                                              *
    00681 *           self.h1.fill ( tracks->size() )                                   *
    00682 *                                                                             *
    00683 *           return SUCCESS                                                    *
    00684 *                                                                             *
    00685 *******************************************************************************
    00686     """
    00687     pass
    00688
    00689 # =============================================================================
    00690 ## @class TupleAlgo
    00691 #  The base class for easy manupulations with N-Tuples
    00692 #
    00693 #  Usage:
    00694 #
    00695 #  @code
    00696 #
    00697 #  from gaudimodule import SUCCESS
    00698 #  from GaudiAlgs   import TupleAlgo
    00699 #
    00700 #  class MyClass(TupleAlgo) :
    00701 #       ' My specific Algorithm, derived from TupleAlgo base class '
    00702 #       def __init__( self , name ) :
    00703 #            'Constructor from algorithm instance name'
    00704 #             #invoke the constructor of base class
    00705 #             TupleAlgo.__init__(self , name )
    00706 #
    00707 #       def execute ( self ) :
    00708 #            'Major method (from IAlgorithm interface)'
    00709 #
    00710 #           # get some data from Transient Event Store
    00711 #           tracks = self.get('/Event/Rec/Tracks')
    00712 #
    00713 #           tup = self.nTuple('My N-Tuple')
    00714 #
    00715 #           for track in tracks :
    00716 #
    00717 #                 pt   = track.pt   ()
    00718 #                 p    = track.p    ()
    00719 #                 chi2 = track.chi2 ()
    00720 #
    00721 #                 #fill N-tuple:
    00722 #                 tup.column ( 'pt'   ,  pt   )
    00723 #                 tup.column ( 'p'    ,  p    )
    00724 #                 tup.column ( 'chi2' ,  chi2 )
    00725 #                 #commit the row
    00726 #                 tup.write  ()
    00727 #
    00728 #           return SUCCESS
    00729 #
    00730 #  @endcode
    00731 #
    00732 #  @see GaudiTupleAlg
    00733 #
    00734 #  @author Vanya BELYAEV ibelyaev@physics.syr.edu
    00735 #  @date 2006-11-26
    00736 class TupleAlgo ( _GaudiTupleAlg  , iAlgorithm ) :
    00737     """
    00738 *******************************************************************************
    00739 *                                                * 'Physisics do not like it, *
    00740 *                                                *  physisics do not need it, *
    00741 *                                                *  physisics do not use  it' *
    00742 *                                                * ****************************
    00743 *  Usage:                                                                     *
    00744 *                                                                             *
    00745 *  from gaudimodule import SUCCESS                                            *
    00746 *  from GaudiAlgs   import TupleAlgo                                          *
    00747 *                                                                             *
    00748 *  class MyClass(TupleAlgo) :                                                 *
    00749 *       ' My specific Algorithm, derived from TupleAlgo base class '          *
    00750 *       def __init__( self , name ) :                                         *
    00751 *            'Constructor from algorithm instance name'                       *
    00752 *             #invoke the constructor of base class                           *
    00753 *             TupleAlgo.__init__(self , name )                                *
    00754 *                                                                             *
    00755 *       def execute ( self ) :                                                *
    00756 *            'Major method (from IAlgorithm interface)'                       *
    00757 *                                                                             *
    00758 *           # get some data from Transient Event Store                        *
    00759 *           tracks = self.get('/Event/Rec/Tracks')                            *
    00760 *                                                                             *
    00761 *           tup = self.nTuple('My N-Tuple')                                   *
    00762 *                                                                             *
    00763 *           for track in tracks :                                             *
    00764 *                                                                             *
    00765 *                 pt   = track.pt   ()                                        *
    00766 *                 p    = track.p    ()                                        *
    00767 *                 chi2 = track.chi2 ()                                        *
    00768 *                                                                             *
    00769 *                 #fill N-tuple:                                              *
    00770 *                 tup.column ( 'pt'   ,  pt   )                               *
    00771 *                 tup.column ( 'p'    ,  p    )                               *
    00772 *                 tup.column ( 'chi2' ,  chi2 )                               *
    00773 *                 #commit the row                                             *
    00774 *                 tup.write  ()                                               *
    00775 *                                                                             *
    00776 *           return SUCCESS                                                    *
    00777 *                                                                             *
    00778 *******************************************************************************
    00779     """
    00780     pass
    00781 class objectmethod(object) :
    00782   def __init__(self, m) :
    00783     self.method = m
    00784   def __call__(self, *args) :
    00785     print args
    00786     return self.method(*args )
    00787
    00788 GaudiAlgo._Base = _GaudiAlgorithm
    00789 HistoAlgo._Base = _GaudiHistoAlg
    00790 TupleAlgo._Base = _GaudiTupleAlg
    00791
    00792 # initialize is 'unique' method :
    00793 GaudiAlgo.initialize = _initialize_
    00794 HistoAlgo.initialize = _initialize_histo_
    00795 TupleAlgo.initialize = _initialize_tuple_
    00796
    00797 def _start_ ( self ) :
    00798     """
    00799     The stub 'start' method needed by the internal implementation of PyAlg<>.
    00800     """
    00801     # return self._Base.start_(self)
    00802     return SUCCESS
    00803
    00804 GaudiAlgo.start = _start_
    00805 HistoAlgo.start = _start_
    00806 TupleAlgo.start = _start_
    00807
    00808 def _execute_ ( self ) :
    00809     """
    00810     The fictive 'execute' method, which MUST be overwitten by user
    00811     """
    00812     raise RuntimeError, 'Execute method is not implemented for %s' % self.name()
    00813
    00814 GaudiAlgo.execute = _execute_
    00815 HistoAlgo.execute = _execute_
    00816 TupleAlgo.execute = _execute_
    00817
    00818 def _stop_ ( self ) :
    00819     """
    00820     The stub 'stop' method needed by the internal implementation of PyAlg<>.
    00821     """
    00822     # return self._Base.stop_(self)
    00823     return SUCCESS
    00824
    00825 GaudiAlgo.stop = _stop_
    00826 HistoAlgo.stop = _stop_
    00827 TupleAlgo.stop = _stop_
    00828
    00829 def _plot1D_    ( s, *a ) :
    00830     """
    00831     The basic method to fill (book-on-demand) 1D-histogram
    00832
    00833     The histogram will be created/booked dautomatically according to the
    00834     specifications:
    00835
    00836        - literal or numerical ID (optional)
    00837        - title
    00838        - low edge
    00839        - high edge
    00840        - number of bins (default is 100)
    00841
    00842     The reference to the histogram is returned and could be used for later manipulations
    00843
    00844     """
    00845     return GaudiPython.HistoDecorator.plot1D    (s,*a)
    00846 def _plot2D_    ( s, *a ) :
    00847     """
    00848     The basic method to fill (book-on-demand) 2D-histogram
    00849
    00850     The histogram will be created/booked dautomatically according to the
    00851     specifications:
    00852
    00853        - literal or numerical ID (optional)
    00854        - title
    00855        - low X-edge
    00856        - high X-edge
    00857        - low Y-edge
    00858        - high Y-edge
    00859        - number of X-bins (default is 50)
    00860        - number of Y-bins (default is 50)
    00861
    00862     The reference to the histogram is returned and could be used for later manipulations
    00863
    00864     """
    00865     return GaudiPython.HistoDecorator.plot2D    (s,*a)
    00866 def _plot3D_    ( s, *a ) :
    00867     """
    00868     The basic method to fill (book-on-demand) 3D-histogram
    00869
    00870     The histogram will be created/booked dautomatically according to the
    00871     specifications:
    00872
    00873        - literal or numerical ID (optional)
    00874        - title
    00875        - low X-edge
    00876        - high X-edge
    00877        - low Y-edge
    00878        - high Y-edge
    00879        - low Z-edge
    00880        - high Z-edge
    00881        - number of X-bins (default is 10)
    00882        - number of Y-bins (default is 10)
    00883        - number of Y-bins (default is 10)
    00884
    00885     The reference to the histogram is returned and could be used for later manipulations
    00886
    00887     """
    00888     return GaudiPython.HistoDecorator.plot3D    (s,*a)
    00889 def _profile1D_ ( s, *a ) :
    00890     """
    00891     The basic method to fill (book-on-demand) 1D profile histogram
    00892
    00893     The profile histogram will be created/booked dautomatically
    00894     according to the specifications:
    00895
    00896        - literal or numerical ID (optional)
    00897        - title
    00898        - low X-edge
    00899        - high X-edge
    00900        - number of X-bins (default is 100)
    00901
    00902     The reference to the histogram is returned and could be used for later manipulations
    00903
    00904     """
    00905     return GaudiPython.HistoDecorator.profile1D (s,*a)
    00906 def _profile2D_ ( s, *a ) :
    00907     """
    00908     The basic method to fill (book-on-demand) 2D profile histiogram
    00909
    00910     The profile histogram will be created/booked automatically
    00911     according to the specifications:
    00912
    00913        - literal or numerical ID (optional)
    00914        - title
    00915        - low X-edge
    00916        - high X-edge
    00917        - low Y-edge
    00918        - high Y-edge
    00919        - number of X-bins (default is 50)
    00920        - number of Y-bins (default is 50)
    00921
    00922     The reference to the histogram is returned and could be used for later manipulations
    00923
    00924     """
    00925     return GaudiPython.HistoDecorator.profile2D (s,*a)
    00926
    00927 _plot1D_    .__doc__ += GaudiPython.HistoDecorator.plot1D    .__doc__
    00928 _plot2D_    .__doc__ += GaudiPython.HistoDecorator.plot2D    .__doc__
    00929 _plot3D_    .__doc__ += GaudiPython.HistoDecorator.plot3D    .__doc__
    00930 _profile1D_ .__doc__ += GaudiPython.HistoDecorator.profile1D .__doc__
    00931 _profile2D_ .__doc__ += GaudiPython.HistoDecorator.profile2D .__doc__
    00932
    00933 def _decorate_plots_ ( klasses ) :
    00934     t = type( klasses )
    00935     if not issubclass ( t , list  ) and  \
    00936        not issubclass ( t , tuple ) : klasses = [ klasses ]
    00937     for klass in klasses :
    00938         klass .plot       = _plot1D_
    00939         klass .plot1D     = _plot1D_
    00940         klass .plot2D     = _plot2D_
    00941         klass .plot3D     = _plot3D_
    00942         klass .profile1D  = _profile1D_
    00943         klass .profile2D  = _profile2D_
    00944
    00945 _decorate_plots_ ( HistoAlgo )
    00946 _decorate_plots_ ( TupleAlgo )
    00947
    00948
    00949 def _nTuple_ ( s , *a )  :
    00950     """
    00951     Retrieve (book-on-demand) N-Tuple object
    00952     """
    00953     return GaudiPython.TupleAlgDecorator.nTuple ( s , *a )
    00954 def _evtCol_ ( s , *a )  :
    00955     """
    00956     Retrieve (book-on-demand) N-Tuple object for Event Tag Collections
    00957     """
    00958     return GaudiPython.TupleAlgDecorator.evtCol ( s , *a )
    00959
    00960 _nTuple_.__doc__  += GaudiPython.TupleAlgDecorator.nTuple.__doc__
    00961 _evtCol_.__doc__  += GaudiPython.TupleAlgDecorator.evtCol.__doc__
    00962
    00963 def _decorate_tuples_ ( klasses ) :
    00964     t = type( klasses )
    00965     if not issubclass ( t , list  ) and  \
    00966        not issubclass ( t , tuple ) : klasses = [ klasses ]
    00967     for klass in klasses :
    00968         klass . nTuple    = _nTuple_
    00969         klass . evtCol    = _evtCol_
    00970         klass . ntupleSvc = _ntupleSvc
    00971         klass .  tupleSvc = _ntupleSvc
    00972         klass .   ntupSvc = _ntupleSvc
    00973         klass .    tupSvc = _ntupleSvc
    00974         klass . evtColSvc = _evtcolSvc
    00975         klass . evtcolSvc = _evtcolSvc
    00976
    00977 # ==========================================================
    00978 _decorate_tuples_ ( TupleAlgo )
    00979
    00980
    00981 # "decorate N-Tuple object
    00982 Tuple = gaudimodule.gbl.Tuples.Tuple
    00983 _Dec  = GaudiPython.TupleDecorator
    00984 def _t_nTuple_  (s,*a) : return _Dec.nTuple (s,*a)
    00985 def _t_ntuple_  (s,*a) : return _Dec.ntuple (s,*a)
    00986 def _t_valid_   (s,*a) : return _Dec.valid  (s,*a)
    00987 def _t_write_   (s,*a) : return _Dec.write  (s,*a)
    00988 def _t_column_  (s,*a) : return _Dec.column (s,*a)
    00989 def _t_array_   (s,*a) : return _Dec.array  (s,*a)
    00990 def _t_matrix_  (s,*a) : return _Dec.matrix (s,*a)
    00991 def _t_farray_  (s,*a) : return _Dec.farray (s,*a)
    00992 def _t_fmatrix_ (s,*a) : return _Dec.fmatrix (s,*a)
    00993
    00994 _t_nTuple_ .__doc__ = _Dec.nTuple  .__doc__
    00995 _t_ntuple_ .__doc__ = _Dec.ntuple  .__doc__
    00996 _t_valid_  .__doc__ = _Dec.valid   .__doc__
    00997 _t_write_  .__doc__ = _Dec.write   .__doc__
    00998 _t_column_ .__doc__ = _Dec.column  .__doc__
    00999 _t_array_  .__doc__ = _Dec.array   .__doc__
    01000 _t_matrix_ .__doc__ = _Dec.matrix  .__doc__
    01001 _t_farray_ .__doc__ = _Dec.farray  .__doc__
    01002 _t_fmatrix_.__doc__ = _Dec.fmatrix .__doc__
    01003
    01004 Tuple.nTuple  = _t_nTuple_
    01005 Tuple.ntuple  = _t_ntuple_
    01006 Tuple.valid   = _t_valid_
    01007 Tuple.write   = _t_write_
    01008 Tuple.column  = _t_column_
    01009 Tuple.array   = _t_array_
    01010 Tuple.matrix  = _t_matrix_
    01011 Tuple.farray  = _t_farray_
    01012 Tuple.fmatrix = _t_fmatrix_
    01013
    01014 _alg_map_ = {
    01015     '__init__'    : _init_        , # constructor
    01016     'tool'        : _tool_        , # service locator
    01017     'svc'         : _service_     , # tool locator
    01018     'evtSvc'      : _evtSvc       , # event data service
    01019     'eventSvc'    : _evtSvc       , # event data service
    01020     'detSvc'      : _detSvc       , # detector data service
    01021     'histoSvc'    : _histoSvc     , # histogram data service
    01022     'histSvc'     : _histoSvc     , # histogram data service
    01023     'get'         : _get          , # access to event data
    01024     'getDet'      : _getDet       , # access to detector data
    01025     'finalize'    : _finalize_    , # algorithm finalization
    01026     'beginRun'    : _success_     , # dummy function returning success
    01027     'endRun'      : _success_     , # dummy function returning success
    01028     #
    01029     'hasProperty' : _hasProperty_ , # check the existence of property with given name
    01030     'getProperty' : _getProperty_ , # get the property value with given name
    01031     'setProperty' : _setProperty_ , # set the property with given name
    01032     '__setattr__' : _set_attr_    , # set the attribute/property with given name
    01033     '__getattr__' : _get_attr_      # set the attribute/property with given name
    01034     }
    01035
    01036
    01037 # decorate the classes with the useful methods
    01038 def _decorate_algs_ ( klasses ) :
    01039     t = type( klasses )
    01040     if not issubclass ( t , list  ) and  \
    01041        not issubclass ( t , tuple ) : klasses = [ klasses ]
    01042     for _alg in klasses :
    01043         for key in _alg_map_ : setattr( _alg , key , _alg_map_[key] )
    01044
    01045 # =
    01046 _decorate_algs_ ( GaudiAlgo )
    01047 _decorate_algs_ ( HistoAlgo )
    01048 _decorate_algs_ ( TupleAlgo )
    01049
    01050 # =============================================================================
    01051 # Helper function to fill histogram/ntuple using 'map'-operation
    01052 # =============================================================================
    01053 def mapvct ( func , sequence , ovct = None ) :
    01054     """ Helper function to fill histogram/ntuple using 'map'-operation """
    01055     if not ovct :
    01056         vct = GaudiPython.Vector
    01057     else :
    01058         vct = ovct
    01059     if   hasattr( sequence, 'size' ) :
    01060         vct.reserve ( vct.size() + sequence.size() )
    01061     elif hasattr( sequence, '__len__' ) :
    01062         vct.reserve ( vct.size() + len( sequence ) )
    01063     for object in sequence :
    01064         vct.push_back( func( object ) )
    01065     if not ovct : return vct
    01066
    01067
    01068 GaudiPython.mapvct = mapvct
    01069
    01070 # =============================================================================
    01071 # pseudo help
    01072 # =============================================================================
    01073 def _help_() :
    01074     print __doc__ , __author__
    01075     print '\t\t\tDoc-string for class GaudiAlgo \n' , GaudiAlgo.__doc__
    01076     print '\t\t\tDoc-string for class HistoAlgo \n' , HistoAlgo.__doc__
    01077     print '\t\t\tDoc-string for class TupleAlgo \n' , TupleAlgo.__doc__
    01078
    01079 # =============================================================================
    01080 # pseudo-test suite
    01081 # =============================================================================
    01082 if __name__ == '__main__' :
    01083     _help_()
    01084
    01085 # =============================================================================
    01086 # The END
    01087 # =============================================================================