00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00043 __all__ = (
00044 'GaudiAlgo' ,
00045 'HistoAlgo' ,
00046 'TupleAlgo' ,
00047 'Tuple' ,
00048 'HistoID' ,
00049 'TupleID' ,
00050 'aida2root' ,
00051 'SUCCESS'
00052 )
00053
00054
00055 import GaudiPython.Bindings
00056 iAlgorithm = GaudiPython.Bindings.iAlgorithm
00057 iAlgTool = GaudiPython.Bindings.iAlgTool
00058
00059 from GaudiPython.Bindings import (
00060 SUCCESS ,
00061 InterfaceCast ,
00062 iDataSvc ,
00063 iHistogramSvc ,
00064 iNTupleSvc ,
00065 AppMgr
00066 )
00067
00068 from GaudiPython.Bindings import gbl as cpp
00069 from GaudiPython.HistoUtils import aida2root
00070
00071
00072 std = cpp.std
00073
00074
00075 Vector = std.vector('double')
00076
00077 Matrix = std.vector('std::vector<double>')
00078
00079
00080 HID = cpp.GaudiAlg.ID
00081 HistoID = HID
00082 TID = HID
00083 TupleID = TID
00084
00085
00086 AlgDecorator = cpp.GaudiPython.AlgDecorator
00087 HistoDecorator = cpp.GaudiPython.HistoDecorator
00088 TupleAlgDecorator = cpp.GaudiPython.TupleAlgDecorator
00089 TupleDecorator = cpp.GaudiPython.TupleDecorator
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 def _tool_ ( self ,
00116 interface ,
00117 typename ,
00118 name = None ,
00119 parent = None ,
00120 create = True ) :
00121 """
00122 Useful method to locate the tool a certain
00123
00124 Usage:
00125
00126 # locate public tool
00127 t1 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator')
00128 # locate private tool
00129 t2 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator',parent=self)
00130 # locate public tool with defined name
00131 t3 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt1')
00132 # locate private tool with defined name
00133 t4 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator/MyExt2',parent=self)
00134 # locate public tool with defined name
00135 t5 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt3')
00136 # locate private tool with defined name
00137 t6 = self.tool(ITrExtrapolator,'TrParabolicExtrapolator','MyExt4',parent=self)
00138
00139 """
00140 if not interface : interface = cpp.IAlgTool
00141 if not parent : parent = self
00142 if name : typename += '/' + name
00143 _tool = AlgDecorator.tool_( self , typename , parent , create )
00144 if not _tool : return None
00145 _tool = InterfaceCast(interface)(_tool)
00146 if not _tool :
00147 self.Warning('Invalid cast to interface %s' % interface )
00148 return None
00149 return _tool
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 def _service_ ( self ,
00165 interface ,
00166 name ,
00167 create = True ) :
00168 """
00169 Useful method to locate a service:
00170
00171 Usage:
00172
00173 ntsvc = self.svc( INTupleSvc , 'NTUpleSvc' )
00174
00175 """
00176 if not interface : interface = cpp.IInterface
00177 _svc = AlgDecorator.svc_ ( self , name , create )
00178 if not _svc : return None
00179 _svc = InterfaceCast(interface)(_svc)
00180 if not _svc :
00181 self.Warning('Invalid cast to interface %s' % interface )
00182 return None
00183 return _svc
00184
00185
00186
00187
00188
00189
00190 def _init_ ( self , name , **args ) :
00191 """
00192 The constructor from unique algorithm instance name & parameters
00193 """
00194 self._Base.__init__( self , self , name )
00195 appMgr = AppMgr()
00196 algMgr = appMgr._algmgr
00197 status = algMgr.addAlgorithm( self )
00198 if status.isFailure() :
00199 raise RuntimeError, 'Unable to add Algorithm "' + name + '"'
00200 iAlgorithm.__init__ ( self , name , self )
00201 for key in args : setattr ( self , key , args[key] )
00202
00203 if not appMgr.__dict__.has_key ( 'GaudiPythonAlgos') :
00204 appMgr.__dict__[ 'GaudiPythonAlgos'] = []
00205 appMgr.__dict__[ 'GaudiPythonAlgos'].append( self )
00206
00207
00208
00209
00210
00211
00212 def _initialize_ ( self ) :
00213 """
00214 The default initialization (initialization of base C++ class + data)
00215 """
00216 status = self._Base.initialize_( self )
00217 if status.isFailure() : return status
00218
00219
00220 _e = self._Base.evtSvc( self )
00221 _s = InterfaceCast(cpp.IService)(_e)
00222 self._evtSvc_ = iDataSvc ( _s.name() , _e )
00223
00224 _d = self._Base.detSvc( self )
00225 _s = InterfaceCast(cpp.IService)(_d)
00226 self._detSvc_ = iDataSvc ( _s.name() , _d )
00227
00228 return status
00229
00230
00231
00232
00233
00234
00235 def _initialize_histo_ ( self ) :
00236 """
00237 The default initialization (initialization of base C++ class + data members)
00238 """
00239 status = _initialize_( self )
00240 if status.isFailure() : return status
00241
00242
00243 _h = self._Base.histoSvc( self )
00244 _s = InterfaceCast(cpp.IService)(_h)
00245 self._histoSvc_ = iHistogramSvc ( _s.name() , _h )
00246
00247 return status
00248
00249
00250
00251
00252
00253
00254 def _initialize_tuple_ ( self ) :
00255 """
00256 The default initialization (initialization of base C++ class + data members)
00257 """
00258 status = _initialize_histo_( self )
00259 if status.isFailure() : return status
00260
00261
00262 if self.produceNTuples() :
00263 _n = self._Base.ntupleSvc( self )
00264 _s = InterfaceCast(cpp.IService)(_n)
00265 self._ntupleSvc_ = iNTupleSvc ( _s.name() , _n )
00266
00267 if self.produceEvtCols() :
00268 _n = self._Base.evtColSvc( self )
00269 _s = InterfaceCast(cpp.IService)(_n)
00270 self._evtcolSvc_ = iNTupleSvc ( _s.name() , _n )
00271
00272 return status
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 def _evtSvc ( self , location = None ) :
00292 """
00293 Trivial helper function to access Event Data and Event Data Service
00294
00295 Usage:
00296
00297 # get event data service
00298 svc = self.evtSvc()
00299
00300 # get the data
00301 hits = self.evtSvc('MC/Calo/Hits')
00302 """
00303 if not location :
00304 return self._evtSvc_
00305 return self._evtSvc_[location]
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 def _detSvc ( self ) :
00325 """
00326 Trivial helper function to access Detector Data and Event Data Service
00327
00328 Usage:
00329 # get detector data service
00330 svc = self.detSvc()
00331
00332 # get the data
00333 lhcb = self.detSvc('/dd/Structure/LHCb')
00334 """
00335 if not location :
00336 return self._detSvc_
00337 return self._detSvc_[location]
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 def _histoSvc ( self , address = None ) :
00357 """
00358 Trivial helper function to access Histogram Data and Histogram Data Service
00359
00360 Usage:
00361
00362 # get histogram data service
00363 svc = self.histoSvc()
00364
00365 # get the data
00366 histo = self.histoSvc('/stat/Calo/1')
00367 """
00368 if not address : return self._histoSvc_
00369 return self._histoSvc_[ address ]
00370
00371
00372
00373 def _get ( self , location ) :
00374 """
00375 Trivial function to access the data in TES using the data service
00376 """
00377 return self._evtSvc_[location]
00378
00379
00380
00381 def _getDet ( self , location ) :
00382 """
00383 Trivial function to access the data in TDS using data service
00384 """
00385 return self._detSvc_[location]
00386
00387
00388
00389 def _get_ ( self , location , rootInTES = True ) :
00390 """
00391 Get the object from Transient Event Store using GaudiCommon machinery,
00392 respecting RootInTES behaviour
00393 """
00394 return AlgDecorator.get_ ( self , location , rootInTES )
00395
00396
00397 def _exist_ ( self , location , rootInTES = True ) :
00398 """
00399 Check the object in Transient Event Store using GaudiCommon machinery,
00400 respecting RootInTES behaviour
00401 """
00402 return AlgDecorator.exist_ ( self , location , rootInTES )
00403
00404
00405
00406 def _ntupleSvc ( self ) :
00407 """
00408 Trivial function to access N-Tuple Service
00409 """
00410 return self._ntupleSvc_
00411
00412
00413
00414 def _evtcolSvc ( self ) :
00415 """
00416 Trivial function to access Event Collection Service
00417 """
00418 return self._evtcolSvc_
00419
00420
00421
00422
00423 def _finalize_ ( self ) :
00424 """
00425 The default finalization : finalize the base C++ class
00426 """
00427 status = self._Base.finalize_ ( self )
00428 return status
00429
00430
00431 def _success_ ( self ) : return SUCCESS
00432
00433
00434
00435
00436 def _hasProperty_ ( self , pname ) :
00437 """
00438 The trivial function which checks the existence of the property with given name
00439 """
00440 return cpp.Gaudi.Utils.hasProperty ( self , pname )
00441
00442
00443
00444 def _getProperty_ ( self , pname ) :
00445 """
00446 Get the property by name
00447 """
00448 if not self.hasProperty( pname ) :
00449 raise AttributeError, 'property %s does not exist' % pname
00450 return iAlgorithm.__getattr__( self , pname )
00451
00452
00453
00454 def _setProperty_ ( self , pname , pvalue ) :
00455 """
00456 Set the property from the value
00457 """
00458 if not self.hasProperty( pname ) :
00459 raise AttributeError, 'property %s does not exist' % pname
00460 return iAlgorithm.__setattr__ ( self , pname , pvalue )
00461
00462
00463
00464 def _get_attr_ ( self , pname ) :
00465 """
00466 Get the attribute (or property)
00467 - if the attribute name corresponds to the property name, property value is returned
00468 """
00469 if self.hasProperty( pname ) :
00470 return iAlgorithm.__getattr__ ( self , pname )
00471 raise AttributeError, 'attribute/property %s does not exist' % pname
00472
00473
00474
00475 def _set_attr_ ( self , pname , pvalue ) :
00476 """
00477 Set the attribute (or property) :
00478 - if the attribute name corresponds to the property name, the property is updated
00479 """
00480 if not self.hasProperty( pname ) : self.__dict__[pname] = pvalue
00481 else : iAlgorithm.__setattr__ ( self , pname , pvalue )
00482
00483
00484 _GaudiAlgorithm = cpp.GaudiPython.PyAlg( 'GaudiAlgorithm' )
00485 _GaudiHistoAlg = cpp.GaudiPython.PyAlg( 'GaudiHistoAlg' )
00486 _GaudiTupleAlg = cpp.GaudiPython.PyAlg( 'GaudiTupleAlg' )
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551 class GaudiAlgo ( _GaudiAlgorithm , iAlgorithm ) :
00552 """
00553 *******************************************************************************
00554 * * 'Physisics do not like it, *
00555 * * physisics do not need it, *
00556 * * physisics do not use it' *
00557 * * ****************************
00558 * Usage: *
00559 * *
00560 * from GaudiPython.GaudiAlgs import GaudiAlgo, SUCCESS *
00561 * *
00562 * class MyClass(GaudiAlgo) : *
00563 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00564 * def __init__( self , name , **args ) : *
00565 * 'Constructor from algorithm instance name & parameters' *
00566 * #invoke the constructor of base class *
00567 * GaudiAlgo.__init__(self , name , **args ) *
00568 * *
00569 * def initialize ( self ) : *
00570 * 'Algorithm initialization' *
00571 * # initialize the base class *
00572 * status = GaudiAlgo.initialize( self ) *
00573 * if status.isFailure() : return status *
00574 * *
00575 * # locate the services and tools *
00576 * *
00577 * # locate some tool: *
00578 * extrapolator = self.tool(ITrExtrapolator,'TrExtrapolator') *
00579 * *
00580 * # locate the service *
00581 * rndmSvc = self.svc(IRndmGenSvc, 'RndmGenSvc') *
00582 * *
00583 * return SUCCESS *
00584 * *
00585 * *
00586 * def execute ( self ) : *
00587 * 'Major method (from IAlgorithm interface)' *
00588 * *
00589 * # get some data from Transient Event Store *
00590 * tracks = self.get('/Event/Rec/Tracks') *
00591 * *
00592 * # use counters *
00593 * c1 = self.counter('#Tracks') *
00594 * c2 = self.counter('No Tracks') *
00595 * if tracks.empty : *
00596 * c2+=1 *
00597 * c1 += tracks->size() *
00598 * *
00599 * if 1000 < tracks.size() : *
00600 * return self.Error('The event is *VERY* busy') *
00601 * *
00602 * return SUCCESS *
00603 * *
00604 *******************************************************************************
00605 """
00606 pass
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 class HistoAlgo ( _GaudiHistoAlg , iAlgorithm ) :
00678 """
00679 *******************************************************************************
00680 * * 'Physisics do not like it, *
00681 * * physisics do not need it, *
00682 * * physisics do not use it' *
00683 * * ****************************
00684 * Usage: *
00685 * *
00686 * from GaudiPython.GaudiAlgs import HistoAlgo, SUCCESS *
00687 * *
00688 * class MyClass(HistoAlgo) : *
00689 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00690 * def __init__( self , name , **args ) : *
00691 * 'Constructor from algorithm instance name' *
00692 * #invoke the constructor of base class *
00693 * HistoAlgo.__init__(self , name , **args ) *
00694 * *
00695 * def execute ( self ) : *
00696 * 'Major method (from IAlgorithm interface)' *
00697 * *
00698 * # get some data from Transient Event Store *
00699 * tracks = self.get('/Event/Rec/Tracks') *
00700 * *
00701 * self.plot1D ( tracks->size() , '#tracks' , 0 , 100 ) *
00702 * *
00703 * return SUCCESS *
00704 * *
00705 * Alternatively the histogram could be booked in advance: *
00706 * *
00707 * class MyClass(HistoAlgo) : *
00708 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00709 * def __init__( self , name ) : *
00710 * 'Constructor from algorithm instance name' *
00711 * #invoke the constructor of base class *
00712 * HistoAlgo.__init__(self , name ) *
00713 * *
00714 * def initialize ( self ) : *
00715 * 'Algorithm initialization' *
00716 * # initialize the base class *
00717 * status = HistoAlgo.initialize( self ) *
00718 * if status.isFailure() : return status *
00719 * *
00720 * # book the histogram *
00721 * self.h1 = selff.book1D ( '#tracks' , 0 , 100 ) *
00722 * *
00723 * return SUCCESS *
00724 * *
00725 * *
00726 * def execute ( self ) : *
00727 * 'Major method (from IAlgorithm interface)' *
00728 * *
00729 * # get some data from Transient Event Store *
00730 * tracks = self.get('/Event/Rec/Tracks') *
00731 * *
00732 * # fill the histogram *
00733 * self.h1.fill ( tracks->size() ) *
00734 * *
00735 * return SUCCESS *
00736 * *
00737 *******************************************************************************
00738 """
00739 pass
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 class TupleAlgo ( _GaudiTupleAlg , iAlgorithm ) :
00788 """
00789 *******************************************************************************
00790 * * 'Physisics do not like it, *
00791 * * physisics do not need it, *
00792 * * physisics do not use it' *
00793 * * ****************************
00794 * Usage: *
00795 * *
00796 * from GaudiPython.GaudiAlgs import TupleAlgo, SUCCESS *
00797 * *
00798 * class MyClass(TupleAlgo) : *
00799 * ' My specific Algorithm, derived from TupleAlgo base class ' *
00800 * def __init__( self , name , **args ) : *
00801 * 'Constructor from algorithm instance name & parameters' *
00802 * #invoke the constructor of base class *
00803 * TupleAlgo.__init__(self , name , **args ) *
00804 * *
00805 * def execute ( self ) : *
00806 * 'Major method (from IAlgorithm interface)' *
00807 * *
00808 * # get some data from Transient Event Store *
00809 * tracks = self.get('/Event/Rec/Tracks') *
00810 * *
00811 * tup = self.nTuple('My N-Tuple') *
00812 * *
00813 * for track in tracks : *
00814 * *
00815 * pt = track.pt () *
00816 * p = track.p () *
00817 * chi2 = track.chi2 () *
00818 * *
00819 * #fill N-tuple: *
00820 * tup.column ( 'pt' , pt ) *
00821 * tup.column ( 'p' , p ) *
00822 * tup.column ( 'chi2' , chi2 ) *
00823 * #commit the row *
00824 * tup.write () *
00825 * *
00826 * return SUCCESS *
00827 * *
00828 *******************************************************************************
00829 """
00830 pass
00831 class objectmethod(object) :
00832 def __init__(self, m) :
00833 self.method = m
00834 def __call__(self, *args) :
00835 print args
00836 return self.method(*args )
00837
00838 GaudiAlgo._Base = _GaudiAlgorithm
00839 HistoAlgo._Base = _GaudiHistoAlg
00840 TupleAlgo._Base = _GaudiTupleAlg
00841
00842
00843 GaudiAlgo.initialize = _initialize_
00844 HistoAlgo.initialize = _initialize_histo_
00845 TupleAlgo.initialize = _initialize_tuple_
00846
00847 def _start_ ( self ) :
00848 """
00849 The stub 'start' method needed by the internal implementation of PyAlg<>.
00850 """
00851
00852 return SUCCESS
00853
00854 GaudiAlgo.start = _start_
00855 HistoAlgo.start = _start_
00856 TupleAlgo.start = _start_
00857
00858 def _execute_ ( self ) :
00859 """
00860 The fictive 'execute' method, which MUST be overwitten by user
00861 """
00862 raise RuntimeError, 'Execute method is not implemented for %s' % self.name()
00863
00864 GaudiAlgo.execute = _execute_
00865 HistoAlgo.execute = _execute_
00866 TupleAlgo.execute = _execute_
00867
00868 def _stop_ ( self ) :
00869 """
00870 The stub 'stop' method needed by the internal implementation of PyAlg<>.
00871 """
00872
00873 return SUCCESS
00874
00875 GaudiAlgo.stop = _stop_
00876 HistoAlgo.stop = _stop_
00877 TupleAlgo.stop = _stop_
00878
00879
00880 def _plot1D_ ( s, *a ) :
00881 """
00882 The basic method to fill (book-on-demand) 1D-histogram
00883
00884 The histogram will be created/booked dautomatically according to the
00885 specifications:
00886
00887 - literal or numerical ID (optional)
00888 - title
00889 - low edge
00890 - high edge
00891 - number of bins (default is 100)
00892
00893 The reference to the histogram is returned and could be used for later manipulations
00894
00895 """
00896 return HistoDecorator.plot1D (s,*a)
00897
00898 def _plot2D_ ( s, *a ) :
00899 """
00900 The basic method to fill (book-on-demand) 2D-histogram
00901
00902 The histogram will be created/booked dautomatically according to the
00903 specifications:
00904
00905 - literal or numerical ID (optional)
00906 - title
00907 - low X-edge
00908 - high X-edge
00909 - low Y-edge
00910 - high Y-edge
00911 - number of X-bins (default is 50)
00912 - number of Y-bins (default is 50)
00913
00914 The reference to the histogram is returned and could be used for later manipulations
00915
00916 """
00917 return HistoDecorator.plot2D (s,*a)
00918
00919 def _plot3D_ ( s, *a ) :
00920 """
00921 The basic method to fill (book-on-demand) 3D-histogram
00922
00923 The histogram will be created/booked dautomatically according to the
00924 specifications:
00925
00926 - literal or numerical ID (optional)
00927 - title
00928 - low X-edge
00929 - high X-edge
00930 - low Y-edge
00931 - high Y-edge
00932 - low Z-edge
00933 - high Z-edge
00934 - number of X-bins (default is 10)
00935 - number of Y-bins (default is 10)
00936 - number of Y-bins (default is 10)
00937
00938 The reference to the histogram is returned and could be used for later manipulations
00939
00940 """
00941 return HistoDecorator.plot3D (s,*a)
00942
00943 def _profile1D_ ( s, *a ) :
00944 """
00945 The basic method to fill (book-on-demand) 1D profile histogram
00946
00947 The profile histogram will be created/booked dautomatically
00948 according to the specifications:
00949
00950 - literal or numerical ID (optional)
00951 - title
00952 - low X-edge
00953 - high X-edge
00954 - number of X-bins (default is 100)
00955
00956 The reference to the histogram is returned and could be used for later manipulations
00957
00958 """
00959 return HistoDecorator.profile1D (s,*a)
00960
00961 def _profile2D_ ( s, *a ) :
00962 """
00963 The basic method to fill (book-on-demand) 2D profile histiogram
00964
00965 The profile histogram will be created/booked automatically
00966 according to the specifications:
00967
00968 - literal or numerical ID (optional)
00969 - title
00970 - low X-edge
00971 - high X-edge
00972 - low Y-edge
00973 - high Y-edge
00974 - number of X-bins (default is 50)
00975 - number of Y-bins (default is 50)
00976
00977 The reference to the histogram is returned and could be used for later manipulations
00978
00979 """
00980 return HistoDecorator.profile2D (s,*a)
00981
00982
00983 _plot1D_ .__doc__ += '\n' + HistoDecorator.plot1D .__doc__
00984 _plot2D_ .__doc__ += '\n' + HistoDecorator.plot2D .__doc__
00985 _plot3D_ .__doc__ += '\n' + HistoDecorator.plot3D .__doc__
00986 _profile1D_ .__doc__ += '\n' + HistoDecorator.profile1D .__doc__
00987 _profile2D_ .__doc__ += '\n' + HistoDecorator.profile2D .__doc__
00988
00989 def _decorate_plots_ ( klasses ) :
00990 t = type( klasses )
00991 if not issubclass ( t , list ) and \
00992 not issubclass ( t , tuple ) : klasses = [ klasses ]
00993 for klass in klasses :
00994 klass .plot = _plot1D_
00995 klass .plot1D = _plot1D_
00996 klass .plot2D = _plot2D_
00997 klass .plot3D = _plot3D_
00998 klass .profile1D = _profile1D_
00999 klass .profile2D = _profile2D_
01000
01001 _decorate_plots_ ( HistoAlgo )
01002 _decorate_plots_ ( TupleAlgo )
01003
01004
01005
01006 def _nTuple_ ( s , *a ) :
01007 """
01008 Retrieve (book-on-demand) N-Tuple object
01009 """
01010 return TupleAlgDecorator.nTuple ( s , *a )
01011
01012 def _evtCol_ ( s , *a ) :
01013 """
01014 Retrieve (book-on-demand) N-Tuple object for Event Tag Collections
01015 """
01016 return TupleAlgDecorator.evtCol ( s , *a )
01017
01018 _nTuple_.__doc__ += '\n' + TupleAlgDecorator.nTuple.__doc__
01019 _evtCol_.__doc__ += '\n' + TupleAlgDecorator.evtCol.__doc__
01020
01021 def _decorate_tuples_ ( klasses ) :
01022 t = type( klasses )
01023 if not issubclass ( t , list ) and \
01024 not issubclass ( t , tuple ) : klasses = [ klasses ]
01025 for klass in klasses :
01026 klass . nTuple = _nTuple_
01027 klass . evtCol = _evtCol_
01028 klass . ntupleSvc = _ntupleSvc
01029 klass . tupleSvc = _ntupleSvc
01030 klass . ntupSvc = _ntupleSvc
01031 klass . tupSvc = _ntupleSvc
01032 klass . evtColSvc = _evtcolSvc
01033 klass . evtcolSvc = _evtcolSvc
01034
01035
01036 _decorate_tuples_ ( TupleAlgo )
01037
01038
01039
01040 Tuple = cpp.Tuples.Tuple
01041 _Dec = TupleDecorator
01042 def _t_nTuple_ ( s , *a ) :
01043 """
01044 Access to underlying INTuple object
01045 """
01046 return _Dec.nTuple ( s , *a )
01047 def _t_ntuple_ ( s , *a ) :
01048 """
01049 Access to underlying NTuple::Tuple object
01050 """
01051 return _Dec.ntuple ( s , *a )
01052 def _t_valid_ ( s , *a ) :
01053 """
01054 Valid NTuple::Tuple object?
01055 """
01056 return _Dec.valid ( s , *a )
01057 def _t_write_ ( s , *a ) :
01058 """
01059 Commit the row/record to n-tuple
01060 """
01061 return _Dec.write ( s , *a )
01062 def _t_column_ ( s , *a ) :
01063 """
01064 Fill the certain column to n-tuple
01065 """
01066 return _Dec.column ( s , *a )
01067 def _t_column_ll_ ( s , *a ) :
01068 """
01069 Fill the 'long long' column
01070 """
01071 return _Dec.column_ll ( s , *a )
01072 def _t_column_ull_ ( s , *a ) :
01073 """
01074 Fill the 'unsigned long long' column
01075 """
01076 return _Dec.column_ull ( s , *a )
01077 def _t_array_ ( s , *a ) :
01078 """
01079 Fill the fixed-size array column
01080 """
01081 return _Dec.array ( s , *a )
01082 def _t_matrix_ ( s , *a ) :
01083 """
01084 Fill the fixed-size matrix column
01085 """
01086 return _Dec.matrix ( s , *a )
01087 def _t_farray_ ( s , *a ) :
01088 """
01089 Fill the floating-size array column
01090 """
01091 return _Dec.farray ( s , *a )
01092 def _t_fmatrix_ ( s , *a ) :
01093 """
01094 Fill the floating-size matrix column
01095 """
01096 return _Dec.fmatrix ( s , *a )
01097
01098 _t_nTuple_ . __doc__ += '\n' + _Dec.nTuple . __doc__
01099 _t_ntuple_ . __doc__ += '\n' + _Dec.ntuple . __doc__
01100 _t_valid_ . __doc__ += '\n' + _Dec.valid . __doc__
01101 _t_write_ . __doc__ += '\n' + _Dec.write . __doc__
01102 _t_column_ . __doc__ += '\n' + _Dec.column . __doc__
01103 _t_column_ll_ . __doc__ += '\n' + _Dec.column_ll . __doc__
01104 _t_column_ull_ . __doc__ += '\n' + _Dec.column_ull . __doc__
01105 _t_array_ . __doc__ += '\n' + _Dec.array . __doc__
01106 _t_matrix_ . __doc__ += '\n' + _Dec.matrix . __doc__
01107 _t_farray_ . __doc__ += '\n' + _Dec.farray . __doc__
01108 _t_fmatrix_ . __doc__ += '\n' + _Dec.fmatrix . __doc__
01109
01110 Tuple.nTuple = _t_nTuple_
01111 Tuple.ntuple = _t_ntuple_
01112 Tuple.valid = _t_valid_
01113 Tuple.write = _t_write_
01114 Tuple.column = _t_column_
01115 Tuple.column_ll = _t_column_ll_
01116 Tuple.column_ull = _t_column_ull_
01117 Tuple.array = _t_array_
01118 Tuple.matrix = _t_matrix_
01119 Tuple.farray = _t_farray_
01120 Tuple.fmatrix = _t_fmatrix_
01121
01122 _alg_map_ = {
01123 '__init__' : _init_ ,
01124 'tool' : _tool_ ,
01125 'svc' : _service_ ,
01126 'evtSvc' : _evtSvc ,
01127 'eventSvc' : _evtSvc ,
01128 'detSvc' : _detSvc ,
01129 'histoSvc' : _histoSvc ,
01130 'histSvc' : _histoSvc ,
01131 'get' : _get ,
01132 'get_' : _get_ ,
01133 'exist_' : _exist_ ,
01134 'getDet' : _getDet ,
01135 'finalize' : _finalize_ ,
01136 'beginRun' : _success_ ,
01137 'endRun' : _success_ ,
01138
01139 'hasProperty' : _hasProperty_ ,
01140 'getProperty' : _getProperty_ ,
01141 'setProperty' : _setProperty_ ,
01142 '__setattr__' : _set_attr_ ,
01143 '__getattr__' : _get_attr_
01144 }
01145
01146
01147
01148 def _decorate_algs_ ( klasses ) :
01149 t = type( klasses )
01150 if not issubclass ( t , list ) and \
01151 not issubclass ( t , tuple ) : klasses = [ klasses ]
01152 for _alg in klasses :
01153 for key in _alg_map_ : setattr( _alg , key , _alg_map_[key] )
01154
01155
01156 _decorate_algs_ ( GaudiAlgo )
01157 _decorate_algs_ ( HistoAlgo )
01158 _decorate_algs_ ( TupleAlgo )
01159
01160
01161
01162
01163 def mapvct ( func , sequence , ovct = None ) :
01164 """ Helper function to fill histogram/ntuple using 'map'-operation """
01165 if not ovct :
01166 vct = GaudiPython.Vector
01167 else :
01168 vct = ovct
01169 if hasattr( sequence, 'size' ) :
01170 vct.reserve ( vct.size() + sequence.size() )
01171 elif hasattr( sequence, '__len__' ) :
01172 vct.reserve ( vct.size() + len( sequence ) )
01173 for object in sequence :
01174 vct.push_back( func( object ) )
01175 if not ovct : return vct
01176
01177
01178
01179
01180
01181
01182
01183
01184 def _get_all_tools_ ( self , method ) :
01185 """
01186 Get all tools
01187 """
01188 _tools = std.vector('IAlgTool*')()
01189 _func = getattr ( AlgDecorator , method )
01190 _num = _func ( self , _tools )
01191 if _tools.size() != _num :
01192 raise RuntimeError, 'Unable to extract Tools'
01193 _res = []
01194 for _tool in _tools : _res += [ iAlgTool ( _tool.name() , _tool ) ]
01195 return _res
01196
01197 def _Tools_a_ ( self ) :
01198 """
01199 Retrieve the list of tools,
01200 aquired by component through GaudiCommon<TYPE> base:
01201
01202 >>> alg = ... ## get the algorithm
01203 >>> tools = alg.Tools() ## get the tools
01204 >>> for tool in tools :
01205 ... print tool
01206
01207 """
01208 _cmp = getattr ( self , '_ialg' )
01209 if not _cmp : self.retrieveInterface()
01210 _cmp = getattr ( self , '_ialg' )
01211 return _get_all_tools_ ( _cmp , '_tools_a_' )
01212
01213 def _Tools_t_ ( self ) :
01214 """
01215 Retrieve the list of tools,
01216 aquired by component through GaudiCommon<TYPE> base:
01217
01218 >>> tool = ... ## get the tool
01219 >>> tools = tool.Tools() ## get the tools
01220 >>> for t in tools :
01221 ... print t
01222
01223 """
01224 _cmp = getattr ( self , '_itool' )
01225 if not _cmp : self.retrieveInterface()
01226 _cmp = getattr ( self , '_itool' )
01227 return _get_all_tools_ ( _cmp , '_tools_t_' )
01228
01229
01230
01231
01232
01233
01234 def _get_all_counters_ ( self , method , name = None ) :
01235 """
01236 get all counters
01237 """
01238 _cnts = std.vector('const StatEntity*')()
01239 _nams = std.vector('std::string') ()
01240 _func = getattr ( AlgDecorator , method )
01241 _num = _func ( self , _nams , _cnts )
01242 if _nams.size() != _num or _cnts.size() != _num :
01243 raise RuntimeError, 'Unable to extract Counters'
01244 _res = {}
01245 for _i in range(0,_num) :
01246 _nam = _nams[_i]
01247 _cnt = _cnts[_i]
01248 _res [ _nam ] = _cnt
01249 if not name : return _res
01250 return _res.get( name , None )
01251
01252
01253 def _Counters_a_ ( self , name = None ) :
01254 """
01255 Retrieve the counters, managed GaudiCommon<TYPE> base:
01256
01257 >>> alg = ... ## get the algorithm
01258 >>> cnts = alg.Counters() ## get the counters
01259 >>> for key in cnts :
01260 ... print key, cnts[key]
01261
01262
01263 Retrieve the counter, managed GaudiCommon<TYPE> base by name:
01264
01265 >>> alg = ... ## get the algorithm
01266 >>> cnt = alg.Counters('MyCounter') ## get the counter
01267 >>> print cnt
01268
01269 """
01270 _cmp = getattr ( self , '_ialg' )
01271 if not _cmp : self.retrieveInterface()
01272 _cmp = getattr ( self , '_ialg' )
01273 return _get_all_counters_ ( _cmp , '_counters_a_' , name )
01274
01275 def _Counters_t_ ( self , name = None ) :
01276 """
01277 Retrieve the counters, managed GaudiCommon<TYPE> base:
01278
01279 >>> tool = ... ## get the tool
01280 >>> cnts = tool.Counters() ## get the counters
01281 >>> for key in cnts :
01282 ... print key, cnts[key]
01283
01284
01285 Retrieve the counter, managed GaudiCommon<TYPE> base by name:
01286
01287 >>> tool = ... ## get the tool
01288 >>> cnt = tool.Counters('MyCounter') ## get the counter
01289 >>> print cnt
01290
01291 """
01292 _cmp = getattr ( self , '_itool' )
01293 if not _cmp : self.retrieveInterface()
01294 _cmp = getattr ( self , '_itool' )
01295 return _get_all_counters_ ( _cmp , '_counters_t_' , name )
01296
01297
01298
01299 def _get_counter_ ( self , method , name ) :
01300 """
01301 get the counter
01302 """
01303 _func = getattr ( AlgDecorator , method )
01304 return _func ( self , name )
01305
01306 def _Counter_a_ ( self , name ) :
01307 """
01308 Retrieve the counter managed GaudiCommon<TYPE> base by name:
01309
01310 >>> alg = ... ## get the algorithm
01311 >>> cnt = alg.Counter('#accept') ## get the counter
01312 >>> print cnt
01313
01314 """
01315 _cmp = getattr ( self , '_ialg' )
01316 if not _cmp : self.retrieveInterface()
01317 _cmp = getattr ( self , '_ialg' )
01318 return _get_counter_ ( _cmp , '_counter_a_' , name )
01319
01320 def _Counter_t_ ( self , name ) :
01321 """
01322 Retrieve the counter managed GaudiCommon<TYPE> base by name:
01323
01324 >>> tool = ... ## get the tool
01325 >>> cnt = tool.Counter('#accept') ## get the counter
01326 >>> print cnt
01327
01328 """
01329 _cmp = getattr ( self , '_itool' )
01330 if not _cmp : self.retrieveInterface()
01331 _cmp = getattr ( self , '_itool' )
01332 return _get_counter_ ( _cmp , '_counter_t_' , name )
01333
01334
01335
01336
01337 cpp.GaudiAlg.ID .__repr__ = cpp.GaudiAlg.ID.idAsString
01338 cpp.GaudiAlg.ID . __str__ = cpp.GaudiAlg.ID.idAsString
01339 cpp.StatEntity .__repr__ = cpp.StatEntity.toString
01340 cpp.StatEntity . __str__ = cpp.StatEntity.toString
01341
01342 def _get_all_histos_ ( component , method , name ) :
01343 """
01344 Get All histogram form the component
01345 """
01346 _res = {}
01347 for _his in ( std.vector('AIDA::IProfile2D*' ) ,
01348 std.vector('AIDA::IProfile1D*' ) ,
01349 std.vector('AIDA::IHistogram3D*' ) ,
01350 std.vector('AIDA::IHistogram2D*' ) ,
01351 std.vector('AIDA::IHistogram1D*' ) ) :
01352 _his = _his()
01353 _ids = std.vector('GaudiAlg::ID') ()
01354 _fun = getattr ( HistoDecorator , method )
01355 _num = _fun ( component , _ids , _his )
01356 if _ids.size() != _num or _his.size() != _num :
01357 raise RuntimeError, 'Unable to extract Histos!'
01358 for _i in range(0,_num) :
01359 _id = _ids[ _i ]
01360 if _id.numeric() : _id = _id.numericID ()
01361 elif _id.literal() : _id = _id.literalID ()
01362 else : _id = _is.idAsString ()
01363 _res [ _id ] = _his[ _i ]
01364
01365 if not name : return _res
01366
01367 id = cpp.GaudiAlg.ID ( name )
01368 for i in ( name ,
01369 id.literalID () ,
01370 id.numericID () ,
01371 id.idAsString() , id ) :
01372 h = _res.get( i , None )
01373 if not not h : return h
01374
01375 return None
01376
01377 def _Histos_a_ ( self , name = None ) :
01378 """
01379 Retrieve all histograms & profiles, booked through GauydiHistos<TYPE> base:
01380
01381 >>> alg = ... ## get the algorithm
01382 >>> histos = alg.Histos() ## get all histograms & profiles
01383 >>> for key in histos :
01384 ... print key, histos[key]
01385
01386 Retrive the histogram with the certain ID :
01387
01388 >>> alg = ... ## get the algorithm
01389 >>> histo = alg.Histos('some histo ID') ## get the histo by ID
01390 >>> print histo
01391
01392 """
01393 _cmp = getattr ( self , '_ialg' )
01394 if not _cmp : self.retrieveInterface()
01395 _cmp = getattr ( self , '_ialg' )
01396 return _get_all_histos_ ( _cmp , '_histos_a_' , name )
01397
01398 def _Histos_t_ ( self , name = None ) :
01399 """
01400 Retrieve all histograms & profiles, booked through GauydiHistos<TYPE> base:
01401
01402 >>> tool = ... ## get the tool
01403 >>> histos = tool.Histos() ## get all histograms & profiles
01404 >>> for key in histos :
01405 ... print key, histos[key]
01406
01407 Retrive the historgam with certain ID :
01408
01409 >>> tool = ... ## get the tool
01410 >>> histo = tool.Histos('some histo ID') ## get the histo by ID
01411 >>> print histo
01412
01413 """
01414 _cmp = getattr ( self , '_itool' )
01415 if not _cmp : self.retrieveInterface()
01416 _cmp = getattr ( self , '_itool' )
01417 return _get_all_histos_ ( _cmp , '_histos_t_' , name )
01418
01419
01420
01421 _Tools_a_ . __doc__ += '\n' + AlgDecorator . _tools_a_ . __doc__
01422 _Tools_t_ . __doc__ += '\n' + AlgDecorator . _tools_t_ . __doc__
01423 _Counters_a_ . __doc__ += '\n' + AlgDecorator . _counters_a_ . __doc__
01424 _Counters_t_ . __doc__ += '\n' + AlgDecorator . _counters_t_ . __doc__
01425 _Counter_a_ . __doc__ += '\n' + AlgDecorator . _counter_a_ . __doc__
01426 _Counter_t_ . __doc__ += '\n' + AlgDecorator . _counter_t_ . __doc__
01427 _Histos_a_ . __doc__ += '\n' + HistoDecorator . _histos_a_ . __doc__
01428 _Histos_t_ . __doc__ += '\n' + HistoDecorator . _histos_t_ . __doc__
01429
01430 iAlgorithm . Tools = _Tools_a_
01431 iAlgTool . Tools = _Tools_t_
01432 iAlgorithm . Counters = _Counters_a_
01433 iAlgTool . Counters = _Counters_t_
01434 iAlgorithm . Counter = _Counter_a_
01435 iAlgTool . Counter = _Counter_t_
01436 iAlgorithm . Histos = _Histos_a_
01437 iAlgTool . Histos = _Histos_t_
01438
01439
01440 import GaudiPython.HistoUtils
01441
01442
01443
01444
01445 def _help_() :
01446 print __doc__ , __author__
01447 print '\t\t\tDoc-string for class GaudiAlgo \n' , GaudiAlgo.__doc__
01448 print '\t\t\tDoc-string for class HistoAlgo \n' , HistoAlgo.__doc__
01449 print '\t\t\tDoc-string for class TupleAlgo \n' , TupleAlgo.__doc__
01450
01451
01452
01453
01454 if __name__ == '__main__' :
01455 _help_()
01456
01457
01458
01459