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
00204
00205
00206
00207
00208 def _initialize_ ( self ) :
00209 """
00210 The default initialization (initialization of base C++ class + data)
00211 """
00212 status = self._Base.initialize_( self )
00213 if status.isFailure() : return status
00214
00215
00216 _e = self._Base.evtSvc( self )
00217 _s = InterfaceCast(cpp.IService)(_e)
00218 self._evtSvc_ = iDataSvc ( _s.name() , _e )
00219
00220 _d = self._Base.detSvc( self )
00221 _s = InterfaceCast(cpp.IService)(_d)
00222 self._detSvc_ = iDataSvc ( _s.name() , _d )
00223
00224 return status
00225
00226
00227
00228
00229
00230
00231 def _initialize_histo_ ( self ) :
00232 """
00233 The default initialization (initialization of base C++ class + data members)
00234 """
00235 status = _initialize_( self )
00236 if status.isFailure() : return status
00237
00238
00239 _h = self._Base.histoSvc( self )
00240 _s = InterfaceCast(cpp.IService)(_h)
00241 self._histoSvc_ = iHistogramSvc ( _s.name() , _h )
00242
00243 return status
00244
00245
00246
00247
00248
00249
00250 def _initialize_tuple_ ( self ) :
00251 """
00252 The default initialization (initialization of base C++ class + data members)
00253 """
00254 status = _initialize_histo_( self )
00255 if status.isFailure() : return status
00256
00257
00258 if self.produceNTuples() :
00259 _n = self._Base.ntupleSvc( self )
00260 _s = InterfaceCast(cpp.IService)(_n)
00261 self._ntupleSvc_ = iNTupleSvc ( _s.name() , _n )
00262
00263 if self.produceEvtCols() :
00264 _n = self._Base.evtColSvc( self )
00265 _s = InterfaceCast(cpp.IService)(_n)
00266 self._evtcolSvc_ = iNTupleSvc ( _s.name() , _n )
00267
00268 return status
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 def _evtSvc ( self , location = None ) :
00288 """
00289 Trivial helper function to access Event Data and Event Data Service
00290
00291 Usage:
00292
00293 # get event data service
00294 svc = self.evtSvc()
00295
00296 # get the data
00297 hits = self.evtSvc('MC/Calo/Hits')
00298 """
00299 if not location :
00300 return self._evtSvc_
00301 return self._evtSvc_[location]
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 def _detSvc ( self ) :
00321 """
00322 Trivial helper function to access Detector Data and Event Data Service
00323
00324 Usage:
00325 # get detector data service
00326 svc = self.detSvc()
00327
00328 # get the data
00329 lhcb = self.detSvc('/dd/Structure/LHCb')
00330 """
00331 if not location :
00332 return self._detSvc_
00333 return self._detSvc_[location]
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 def _histoSvc ( self , address = None ) :
00353 """
00354 Trivial helper function to access Histogram Data and Histogram Data Service
00355
00356 Usage:
00357
00358 # get histogram data service
00359 svc = self.histoSvc()
00360
00361 # get the data
00362 histo = self.histoSvc('/stat/Calo/1')
00363 """
00364 if not address : return self._histoSvc_
00365 return self._histoSvc_[ address ]
00366
00367
00368
00369 def _get ( self , location ) :
00370 """
00371 Trivial function to access the data in TES
00372 """
00373 return self._evtSvc_[location]
00374
00375
00376
00377 def _getDet ( self , location ) :
00378 """
00379 Trivial function to access the data in TDS
00380 """
00381 return self._detSvc_[location]
00382
00383
00384
00385 def _ntupleSvc ( self ) :
00386 """
00387 Trivial function to access N-Tuple Service
00388 """
00389 return self._ntupleSvc_
00390
00391
00392
00393 def _evtcolSvc ( self ) :
00394 """
00395 Trivial function to access Event Collection Service
00396 """
00397 return self._evtcolSvc_
00398
00399
00400
00401
00402 def _finalize_ ( self ) :
00403 """
00404 The default finalization : finalize the base C++ class
00405 """
00406 status = self._Base.finalize_ ( self )
00407 return status
00408
00409
00410 def _success_ ( self ) : return SUCCESS
00411
00412
00413
00414
00415 def _hasProperty_ ( self , pname ) :
00416 """
00417 The trivial function which checks the existence of the property with given name
00418 """
00419 return cpp.Gaudi.Utils.hasProperty ( self , pname )
00420
00421
00422
00423 def _getProperty_ ( self , pname ) :
00424 """
00425 Get the property by name
00426 """
00427 if not self.hasProperty( pname ) :
00428 raise AttributeError, 'property %s does not exist' % pname
00429 return iAlgorithm.__getattr__( self , pname )
00430
00431
00432
00433 def _setProperty_ ( self , pname , pvalue ) :
00434 """
00435 Set the property from the value
00436 """
00437 if not self.hasProperty( pname ) :
00438 raise AttributeError, 'property %s does not exist' % pname
00439 return iAlgorithm.__setattr__ ( self , pname , pvalue )
00440
00441
00442
00443 def _get_attr_ ( self , pname ) :
00444 """
00445 Get the attribute (or property)
00446 - if the attribute name corresponds to the property name, property value is returned
00447 """
00448 if self.hasProperty( pname ) :
00449 return iAlgorithm.__getattr__ ( self , pname )
00450 raise AttributeError, 'attribute/property %s does not exist' % pname
00451
00452
00453
00454 def _set_attr_ ( self , pname , pvalue ) :
00455 """
00456 Set the attribute (or property) :
00457 - if the attribute name corresponds to the property name, the property is updated
00458 """
00459 if not self.hasProperty( pname ) : self.__dict__[pname] = pvalue
00460 else : iAlgorithm.__setattr__ ( self , pname , pvalue )
00461
00462
00463 _GaudiAlgorithm = cpp.GaudiPython.PyAlg( 'GaudiAlgorithm' )
00464 _GaudiHistoAlg = cpp.GaudiPython.PyAlg( 'GaudiHistoAlg' )
00465 _GaudiTupleAlg = cpp.GaudiPython.PyAlg( 'GaudiTupleAlg' )
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
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 class GaudiAlgo ( _GaudiAlgorithm , iAlgorithm ) :
00531 """
00532 *******************************************************************************
00533 * * 'Physisics do not like it, *
00534 * * physisics do not need it, *
00535 * * physisics do not use it' *
00536 * * ****************************
00537 * Usage: *
00538 * *
00539 * from GaudiPython.GaudiAlgs import GaudiAlgo, SUCCESS *
00540 * *
00541 * class MyClass(GaudiAlgo) : *
00542 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00543 * def __init__( self , name , **args ) : *
00544 * 'Constructor from algorithm instance name & parameters' *
00545 * #invoke the constructor of base class *
00546 * GaudiAlgo.__init__(self , name , **args ) *
00547 * *
00548 * def initialize ( self ) : *
00549 * 'Algorithm initialization' *
00550 * # initialize the base class *
00551 * status = GaudiAlgo.initialize( self ) *
00552 * if status.isFailure() : return status *
00553 * *
00554 * # locate the services and tools *
00555 * *
00556 * # locate some tool: *
00557 * extrapolator = self.tool(ITrExtrapolator,'TrExtrapolator') *
00558 * *
00559 * # locate the service *
00560 * rndmSvc = self.svc(IRndmGenSvc, 'RndmGenSvc') *
00561 * *
00562 * return SUCCESS *
00563 * *
00564 * *
00565 * def execute ( self ) : *
00566 * 'Major method (from IAlgorithm interface)' *
00567 * *
00568 * # get some data from Transient Event Store *
00569 * tracks = self.get('/Event/Rec/Tracks') *
00570 * *
00571 * # use counters *
00572 * c1 = self.counter('#Tracks') *
00573 * c2 = self.counter('No Tracks') *
00574 * if tracks.empty : *
00575 * c2+=1 *
00576 * c1 += tracks->size() *
00577 * *
00578 * if 1000 < tracks.size() : *
00579 * return self.Error('The event is *VERY* busy') *
00580 * *
00581 * return SUCCESS *
00582 * *
00583 *******************************************************************************
00584 """
00585 pass
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
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 class HistoAlgo ( _GaudiHistoAlg , iAlgorithm ) :
00657 """
00658 *******************************************************************************
00659 * * 'Physisics do not like it, *
00660 * * physisics do not need it, *
00661 * * physisics do not use it' *
00662 * * ****************************
00663 * Usage: *
00664 * *
00665 * from GaudiPython.GaudiAlgs import HistoAlgo, SUCCESS *
00666 * *
00667 * class MyClass(HistoAlgo) : *
00668 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00669 * def __init__( self , name , **args ) : *
00670 * 'Constructor from algorithm instance name' *
00671 * #invoke the constructor of base class *
00672 * HistoAlgo.__init__(self , name , **args ) *
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 * self.plot1D ( tracks->size() , '#tracks' , 0 , 100 ) *
00681 * *
00682 * return SUCCESS *
00683 * *
00684 * Alternatively the histogram could be booked in advance: *
00685 * *
00686 * class MyClass(HistoAlgo) : *
00687 * ' My specific Algorithm, derived from GaudiAlgo base class ' *
00688 * def __init__( self , name ) : *
00689 * 'Constructor from algorithm instance name' *
00690 * #invoke the constructor of base class *
00691 * HistoAlgo.__init__(self , name ) *
00692 * *
00693 * def initialize ( self ) : *
00694 * 'Algorithm initialization' *
00695 * # initialize the base class *
00696 * status = HistoAlgo.initialize( self ) *
00697 * if status.isFailure() : return status *
00698 * *
00699 * # book the histogram *
00700 * self.h1 = selff.book1D ( '#tracks' , 0 , 100 ) *
00701 * *
00702 * return SUCCESS *
00703 * *
00704 * *
00705 * def execute ( self ) : *
00706 * 'Major method (from IAlgorithm interface)' *
00707 * *
00708 * # get some data from Transient Event Store *
00709 * tracks = self.get('/Event/Rec/Tracks') *
00710 * *
00711 * # fill the histogram *
00712 * self.h1.fill ( tracks->size() ) *
00713 * *
00714 * return SUCCESS *
00715 * *
00716 *******************************************************************************
00717 """
00718 pass
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
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 class TupleAlgo ( _GaudiTupleAlg , iAlgorithm ) :
00767 """
00768 *******************************************************************************
00769 * * 'Physisics do not like it, *
00770 * * physisics do not need it, *
00771 * * physisics do not use it' *
00772 * * ****************************
00773 * Usage: *
00774 * *
00775 * from GaudiPython.GaudiAlgs import TupleAlgo, SUCCESS *
00776 * *
00777 * class MyClass(TupleAlgo) : *
00778 * ' My specific Algorithm, derived from TupleAlgo base class ' *
00779 * def __init__( self , name , **args ) : *
00780 * 'Constructor from algorithm instance name & parameters' *
00781 * #invoke the constructor of base class *
00782 * TupleAlgo.__init__(self , name , **args ) *
00783 * *
00784 * def execute ( self ) : *
00785 * 'Major method (from IAlgorithm interface)' *
00786 * *
00787 * # get some data from Transient Event Store *
00788 * tracks = self.get('/Event/Rec/Tracks') *
00789 * *
00790 * tup = self.nTuple('My N-Tuple') *
00791 * *
00792 * for track in tracks : *
00793 * *
00794 * pt = track.pt () *
00795 * p = track.p () *
00796 * chi2 = track.chi2 () *
00797 * *
00798 * #fill N-tuple: *
00799 * tup.column ( 'pt' , pt ) *
00800 * tup.column ( 'p' , p ) *
00801 * tup.column ( 'chi2' , chi2 ) *
00802 * #commit the row *
00803 * tup.write () *
00804 * *
00805 * return SUCCESS *
00806 * *
00807 *******************************************************************************
00808 """
00809 pass
00810 class objectmethod(object) :
00811 def __init__(self, m) :
00812 self.method = m
00813 def __call__(self, *args) :
00814 print args
00815 return self.method(*args )
00816
00817 GaudiAlgo._Base = _GaudiAlgorithm
00818 HistoAlgo._Base = _GaudiHistoAlg
00819 TupleAlgo._Base = _GaudiTupleAlg
00820
00821
00822 GaudiAlgo.initialize = _initialize_
00823 HistoAlgo.initialize = _initialize_histo_
00824 TupleAlgo.initialize = _initialize_tuple_
00825
00826 def _start_ ( self ) :
00827 """
00828 The stub 'start' method needed by the internal implementation of PyAlg<>.
00829 """
00830
00831 return SUCCESS
00832
00833 GaudiAlgo.start = _start_
00834 HistoAlgo.start = _start_
00835 TupleAlgo.start = _start_
00836
00837 def _execute_ ( self ) :
00838 """
00839 The fictive 'execute' method, which MUST be overwitten by user
00840 """
00841 raise RuntimeError, 'Execute method is not implemented for %s' % self.name()
00842
00843 GaudiAlgo.execute = _execute_
00844 HistoAlgo.execute = _execute_
00845 TupleAlgo.execute = _execute_
00846
00847 def _stop_ ( self ) :
00848 """
00849 The stub 'stop' method needed by the internal implementation of PyAlg<>.
00850 """
00851
00852 return SUCCESS
00853
00854 GaudiAlgo.stop = _stop_
00855 HistoAlgo.stop = _stop_
00856 TupleAlgo.stop = _stop_
00857
00858
00859 def _plot1D_ ( s, *a ) :
00860 """
00861 The basic method to fill (book-on-demand) 1D-histogram
00862
00863 The histogram will be created/booked dautomatically according to the
00864 specifications:
00865
00866 - literal or numerical ID (optional)
00867 - title
00868 - low edge
00869 - high edge
00870 - number of bins (default is 100)
00871
00872 The reference to the histogram is returned and could be used for later manipulations
00873
00874 """
00875 return HistoDecorator.plot1D (s,*a)
00876
00877 def _plot2D_ ( s, *a ) :
00878 """
00879 The basic method to fill (book-on-demand) 2D-histogram
00880
00881 The histogram will be created/booked dautomatically according to the
00882 specifications:
00883
00884 - literal or numerical ID (optional)
00885 - title
00886 - low X-edge
00887 - high X-edge
00888 - low Y-edge
00889 - high Y-edge
00890 - number of X-bins (default is 50)
00891 - number of Y-bins (default is 50)
00892
00893 The reference to the histogram is returned and could be used for later manipulations
00894
00895 """
00896 return HistoDecorator.plot2D (s,*a)
00897
00898 def _plot3D_ ( s, *a ) :
00899 """
00900 The basic method to fill (book-on-demand) 3D-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 - low Z-edge
00912 - high Z-edge
00913 - number of X-bins (default is 10)
00914 - number of Y-bins (default is 10)
00915 - number of Y-bins (default is 10)
00916
00917 The reference to the histogram is returned and could be used for later manipulations
00918
00919 """
00920 return HistoDecorator.plot3D (s,*a)
00921
00922 def _profile1D_ ( s, *a ) :
00923 """
00924 The basic method to fill (book-on-demand) 1D profile histogram
00925
00926 The profile histogram will be created/booked dautomatically
00927 according to the specifications:
00928
00929 - literal or numerical ID (optional)
00930 - title
00931 - low X-edge
00932 - high X-edge
00933 - number of X-bins (default is 100)
00934
00935 The reference to the histogram is returned and could be used for later manipulations
00936
00937 """
00938 return HistoDecorator.profile1D (s,*a)
00939
00940 def _profile2D_ ( s, *a ) :
00941 """
00942 The basic method to fill (book-on-demand) 2D profile histiogram
00943
00944 The profile histogram will be created/booked automatically
00945 according to the specifications:
00946
00947 - literal or numerical ID (optional)
00948 - title
00949 - low X-edge
00950 - high X-edge
00951 - low Y-edge
00952 - high Y-edge
00953 - number of X-bins (default is 50)
00954 - number of Y-bins (default is 50)
00955
00956 The reference to the histogram is returned and could be used for later manipulations
00957
00958 """
00959 return HistoDecorator.profile2D (s,*a)
00960
00961
00962 _plot1D_ .__doc__ += HistoDecorator.plot1D .__doc__
00963 _plot2D_ .__doc__ += HistoDecorator.plot2D .__doc__
00964 _plot3D_ .__doc__ += HistoDecorator.plot3D .__doc__
00965 _profile1D_ .__doc__ += HistoDecorator.profile1D .__doc__
00966 _profile2D_ .__doc__ += HistoDecorator.profile2D .__doc__
00967
00968 def _decorate_plots_ ( klasses ) :
00969 t = type( klasses )
00970 if not issubclass ( t , list ) and \
00971 not issubclass ( t , tuple ) : klasses = [ klasses ]
00972 for klass in klasses :
00973 klass .plot = _plot1D_
00974 klass .plot1D = _plot1D_
00975 klass .plot2D = _plot2D_
00976 klass .plot3D = _plot3D_
00977 klass .profile1D = _profile1D_
00978 klass .profile2D = _profile2D_
00979
00980 _decorate_plots_ ( HistoAlgo )
00981 _decorate_plots_ ( TupleAlgo )
00982
00983
00984
00985 def _nTuple_ ( s , *a ) :
00986 """
00987 Retrieve (book-on-demand) N-Tuple object
00988 """
00989 return TupleAlgDecorator.nTuple ( s , *a )
00990
00991 def _evtCol_ ( s , *a ) :
00992 """
00993 Retrieve (book-on-demand) N-Tuple object for Event Tag Collections
00994 """
00995 return TupleAlgDecorator.evtCol ( s , *a )
00996
00997 _nTuple_.__doc__ += TupleAlgDecorator.nTuple.__doc__
00998 _evtCol_.__doc__ += TupleAlgDecorator.evtCol.__doc__
00999
01000 def _decorate_tuples_ ( klasses ) :
01001 t = type( klasses )
01002 if not issubclass ( t , list ) and \
01003 not issubclass ( t , tuple ) : klasses = [ klasses ]
01004 for klass in klasses :
01005 klass . nTuple = _nTuple_
01006 klass . evtCol = _evtCol_
01007 klass . ntupleSvc = _ntupleSvc
01008 klass . tupleSvc = _ntupleSvc
01009 klass . ntupSvc = _ntupleSvc
01010 klass . tupSvc = _ntupleSvc
01011 klass . evtColSvc = _evtcolSvc
01012 klass . evtcolSvc = _evtcolSvc
01013
01014
01015 _decorate_tuples_ ( TupleAlgo )
01016
01017
01018
01019 Tuple = cpp.Tuples.Tuple
01020 _Dec = TupleDecorator
01021 def _t_nTuple_ (s,*a) : return _Dec.nTuple (s,*a)
01022 def _t_ntuple_ (s,*a) : return _Dec.ntuple (s,*a)
01023 def _t_valid_ (s,*a) : return _Dec.valid (s,*a)
01024 def _t_write_ (s,*a) : return _Dec.write (s,*a)
01025 def _t_column_ (s,*a) : return _Dec.column (s,*a)
01026 def _t_array_ (s,*a) : return _Dec.array (s,*a)
01027 def _t_matrix_ (s,*a) : return _Dec.matrix (s,*a)
01028 def _t_farray_ (s,*a) : return _Dec.farray (s,*a)
01029 def _t_fmatrix_ (s,*a) : return _Dec.fmatrix (s,*a)
01030
01031 _t_nTuple_ .__doc__ = _Dec.nTuple .__doc__
01032 _t_ntuple_ .__doc__ = _Dec.ntuple .__doc__
01033 _t_valid_ .__doc__ = _Dec.valid .__doc__
01034 _t_write_ .__doc__ = _Dec.write .__doc__
01035 _t_column_ .__doc__ = _Dec.column .__doc__
01036 _t_array_ .__doc__ = _Dec.array .__doc__
01037 _t_matrix_ .__doc__ = _Dec.matrix .__doc__
01038 _t_farray_ .__doc__ = _Dec.farray .__doc__
01039 _t_fmatrix_.__doc__ = _Dec.fmatrix .__doc__
01040
01041 Tuple.nTuple = _t_nTuple_
01042 Tuple.ntuple = _t_ntuple_
01043 Tuple.valid = _t_valid_
01044 Tuple.write = _t_write_
01045 Tuple.column = _t_column_
01046 Tuple.array = _t_array_
01047 Tuple.matrix = _t_matrix_
01048 Tuple.farray = _t_farray_
01049 Tuple.fmatrix = _t_fmatrix_
01050
01051 _alg_map_ = {
01052 '__init__' : _init_ ,
01053 'tool' : _tool_ ,
01054 'svc' : _service_ ,
01055 'evtSvc' : _evtSvc ,
01056 'eventSvc' : _evtSvc ,
01057 'detSvc' : _detSvc ,
01058 'histoSvc' : _histoSvc ,
01059 'histSvc' : _histoSvc ,
01060 'get' : _get ,
01061 'getDet' : _getDet ,
01062 'finalize' : _finalize_ ,
01063 'beginRun' : _success_ ,
01064 'endRun' : _success_ ,
01065
01066 'hasProperty' : _hasProperty_ ,
01067 'getProperty' : _getProperty_ ,
01068 'setProperty' : _setProperty_ ,
01069 '__setattr__' : _set_attr_ ,
01070 '__getattr__' : _get_attr_
01071 }
01072
01073
01074
01075 def _decorate_algs_ ( klasses ) :
01076 t = type( klasses )
01077 if not issubclass ( t , list ) and \
01078 not issubclass ( t , tuple ) : klasses = [ klasses ]
01079 for _alg in klasses :
01080 for key in _alg_map_ : setattr( _alg , key , _alg_map_[key] )
01081
01082
01083 _decorate_algs_ ( GaudiAlgo )
01084 _decorate_algs_ ( HistoAlgo )
01085 _decorate_algs_ ( TupleAlgo )
01086
01087
01088
01089
01090 def mapvct ( func , sequence , ovct = None ) :
01091 """ Helper function to fill histogram/ntuple using 'map'-operation """
01092 if not ovct :
01093 vct = GaudiPython.Vector
01094 else :
01095 vct = ovct
01096 if hasattr( sequence, 'size' ) :
01097 vct.reserve ( vct.size() + sequence.size() )
01098 elif hasattr( sequence, '__len__' ) :
01099 vct.reserve ( vct.size() + len( sequence ) )
01100 for object in sequence :
01101 vct.push_back( func( object ) )
01102 if not ovct : return vct
01103
01104
01105
01106
01107
01108
01109
01110
01111 def _get_all_tools_ ( self , method ) :
01112 """
01113 Get all tools
01114 """
01115 _tools = std.vector('IAlgTool*')()
01116 _func = getattr ( AlgDecorator , method )
01117 _num = _func ( self , _tools )
01118 if _tools.size() != _num :
01119 raise RuntimeError, 'Unable to extract Tools'
01120 _res = []
01121 for _tool in _tools : _res += [ iAlgTool ( _tool.name() , _tool ) ]
01122 return _res
01123
01124 def _Tools_a_ ( self ) :
01125 """
01126 Retrieve the list of tools,
01127 aquired by component through GaudiCommon<TYPE> base:
01128
01129 >>> alg = ... ## get the algorithm
01130 >>> tools = alg.Tools() ## get the tools
01131 >>> for tool in tools :
01132 ... print tool
01133
01134 """
01135 _cmp = getattr ( self , '_ialg' )
01136 if not _cmp : self.retrieveInterface()
01137 _cmp = getattr ( self , '_ialg' )
01138 return _get_all_tools_ ( _cmp , '_tools_a_' )
01139
01140 def _Tools_t_ ( self ) :
01141 """
01142 Retrieve the list of tools,
01143 aquired by component through GaudiCommon<TYPE> base:
01144
01145 >>> tool = ... ## get the tool
01146 >>> tools = tool.Tools() ## get the tools
01147 >>> for t in tools :
01148 ... print t
01149
01150 """
01151 _cmp = getattr ( self , '_itool' )
01152 if not _cmp : self.retrieveInterface()
01153 _cmp = getattr ( self , '_itool' )
01154 return _get_all_tools_ ( _cmp , '_tools_t_' )
01155
01156
01157
01158
01159
01160
01161 def _get_all_counters_ ( self , method , name = None ) :
01162 """
01163 get all counters
01164 """
01165 _cnts = std.vector('const StatEntity*')()
01166 _nams = std.vector('std::string') ()
01167 _func = getattr ( AlgDecorator , method )
01168 _num = _func ( self , _nams , _cnts )
01169 if _nams.size() != _num or _cnts.size() != _num :
01170 raise RuntimeError, 'Unable to extract Counters'
01171 _res = {}
01172 for _i in range(0,_num) :
01173 _nam = _nams[_i]
01174 _cnt = _cnts[_i]
01175 _res [ _nam ] = _cnt
01176 if not name : return _res
01177 return _res.get( name , None )
01178
01179
01180 def _Counters_a_ ( self , name = None ) :
01181 """
01182 Retrieve the counters, managed GaudiCommon<TYPE> base:
01183
01184 >>> alg = ... ## get the algorithm
01185 >>> cnts = alg.Counters() ## get the counters
01186 >>> for key in cnts :
01187 ... print key, cnts[key]
01188
01189
01190 Retrieve the counter, managed GaudiCommon<TYPE> base by name:
01191
01192 >>> alg = ... ## get the algorithm
01193 >>> cnt = alg.Counters('MyCounter') ## get the counter
01194 >>> print cnt
01195
01196 """
01197 _cmp = getattr ( self , '_ialg' )
01198 if not _cmp : self.retrieveInterface()
01199 _cmp = getattr ( self , '_ialg' )
01200 return _get_all_counters_ ( _cmp , '_counters_a_' , name )
01201
01202 def _Counters_t_ ( self , name = None ) :
01203 """
01204 Retrieve the counters, managed GaudiCommon<TYPE> base:
01205
01206 >>> tool = ... ## get the tool
01207 >>> cnts = tool.Counters() ## get the counters
01208 >>> for key in cnts :
01209 ... print key, cnts[key]
01210
01211
01212 Retrieve the counter, managed GaudiCommon<TYPE> base by name:
01213
01214 >>> tool = ... ## get the tool
01215 >>> cnt = tool.Counters('MyCounter') ## get the counter
01216 >>> print cnt
01217
01218 """
01219 _cmp = getattr ( self , '_itool' )
01220 if not _cmp : self.retrieveInterface()
01221 _cmp = getattr ( self , '_itool' )
01222 return _get_all_counters_ ( _cmp , '_counters_t_' , name )
01223
01224
01225
01226 def _get_counter_ ( self , method , name ) :
01227 """
01228 get the counter
01229 """
01230 _func = getattr ( AlgDecorator , method )
01231 return _func ( self , name )
01232
01233 def _Counter_a_ ( self , name ) :
01234 """
01235 Retrieve the counter managed GaudiCommon<TYPE> base by name:
01236
01237 >>> alg = ... ## get the algorithm
01238 >>> cnt = alg.Counter('#accept') ## get the counter
01239 >>> print cnt
01240
01241 """
01242 _cmp = getattr ( self , '_ialg' )
01243 if not _cmp : self.retrieveInterface()
01244 _cmp = getattr ( self , '_ialg' )
01245 return _get_counter_ ( _cmp , '_counter_a_' , name )
01246
01247 def _Counter_t_ ( self , name ) :
01248 """
01249 Retrieve the counter managed GaudiCommon<TYPE> base by name:
01250
01251 >>> tool = ... ## get the tool
01252 >>> cnt = tool.Counter('#accept') ## get the counter
01253 >>> print cnt
01254
01255 """
01256 _cmp = getattr ( self , '_itool' )
01257 if not _cmp : self.retrieveInterface()
01258 _cmp = getattr ( self , '_itool' )
01259 return _get_counter_ ( _cmp , '_counter_t_' , name )
01260
01261
01262
01263
01264 cpp.GaudiAlg.ID .__repr__ = cpp.GaudiAlg.ID.idAsString
01265 cpp.GaudiAlg.ID . __str__ = cpp.GaudiAlg.ID.idAsString
01266 cpp.StatEntity .__repr__ = cpp.StatEntity.toString
01267 cpp.StatEntity . __str__ = cpp.StatEntity.toString
01268
01269 def _get_all_histos_ ( component , method , name ) :
01270 """
01271 Get All histogram form the component
01272 """
01273 _res = {}
01274 for _his in ( std.vector('AIDA::IProfile2D*' ) ,
01275 std.vector('AIDA::IProfile1D*' ) ,
01276 std.vector('AIDA::IHistogram3D*' ) ,
01277 std.vector('AIDA::IHistogram2D*' ) ,
01278 std.vector('AIDA::IHistogram1D*' ) ) :
01279 _his = _his()
01280 _ids = std.vector('GaudiAlg::ID') ()
01281 _fun = getattr ( HistoDecorator , method )
01282 _num = _fun ( component , _ids , _his )
01283 if _ids.size() != _num or _his.size() != _num :
01284 raise RuntimeError, 'Unable to extract Histos!'
01285 for _i in range(0,_num) :
01286 _id = _ids[ _i ]
01287 if _id.numeric() : _id = _id.numericID ()
01288 elif _id.literal() : _id = _id.literalID ()
01289 else : _id = _is.idAsString ()
01290 _res [ _id ] = _his[ _i ]
01291
01292 if not name : return _res
01293
01294 id = cpp.GaudiAlg.ID ( name )
01295 for i in ( name ,
01296 id.literalID () ,
01297 id.numericID () ,
01298 id.idAsString() , id ) :
01299 h = _res.get( i , None )
01300 if not not h : return h
01301
01302 return None
01303
01304 def _Histos_a_ ( self , name = None ) :
01305 """
01306 Retrieve all histograms & profiles, booked through GauydiHistos<TYPE> base:
01307
01308 >>> alg = ... ## get the algorithm
01309 >>> histos = alg.Histos() ## get all histograms & profiles
01310 >>> for key in histos :
01311 ... print key, histos[key]
01312
01313 Retrive the histogram with the certain ID :
01314
01315 >>> alg = ... ## get the algorithm
01316 >>> histo = alg.Histos('some histo ID') ## get the histo by ID
01317 >>> print histo
01318
01319 """
01320 _cmp = getattr ( self , '_ialg' )
01321 if not _cmp : self.retrieveInterface()
01322 _cmp = getattr ( self , '_ialg' )
01323 return _get_all_histos_ ( _cmp , '_histos_a_' , name )
01324
01325 def _Histos_t_ ( self , name = None ) :
01326 """
01327 Retrieve all histograms & profiles, booked through GauydiHistos<TYPE> base:
01328
01329 >>> tool = ... ## get the tool
01330 >>> histos = tool.Histos() ## get all histograms & profiles
01331 >>> for key in histos :
01332 ... print key, histos[key]
01333
01334 Retrive the historgam with certain ID :
01335
01336 >>> tool = ... ## get the tool
01337 >>> histo = tool.Histos('some histo ID') ## get the histo by ID
01338 >>> print histo
01339
01340 """
01341 _cmp = getattr ( self , '_itool' )
01342 if not _cmp : self.retrieveInterface()
01343 _cmp = getattr ( self , '_itool' )
01344 return _get_all_histos_ ( _cmp , '_histos_t_' , name )
01345
01346
01347
01348 _Tools_a_ . __doc__ += AlgDecorator . _tools_a_ . __doc__
01349 _Tools_t_ . __doc__ += AlgDecorator . _tools_t_ . __doc__
01350 _Counters_a_ . __doc__ += AlgDecorator . _counters_a_ . __doc__
01351 _Counters_t_ . __doc__ += AlgDecorator . _counters_t_ . __doc__
01352 _Counter_a_ . __doc__ += AlgDecorator . _counter_a_ . __doc__
01353 _Counter_t_ . __doc__ += AlgDecorator . _counter_t_ . __doc__
01354 _Histos_a_ . __doc__ += HistoDecorator . _histos_a_ . __doc__
01355 _Histos_t_ . __doc__ += HistoDecorator . _histos_t_ . __doc__
01356
01357
01358 iAlgorithm . Tools = _Tools_a_
01359 iAlgTool . Tools = _Tools_t_
01360 iAlgorithm . Counters = _Counters_a_
01361 iAlgTool . Counters = _Counters_t_
01362 iAlgorithm . Counter = _Counter_a_
01363 iAlgTool . Counter = _Counter_t_
01364 iAlgorithm . Histos = _Histos_a_
01365 iAlgTool . Histos = _Histos_t_
01366
01367
01368 import GaudiPython.HistoUtils
01369
01370
01371
01372
01373 def _help_() :
01374 print __doc__ , __author__
01375 print '\t\t\tDoc-string for class GaudiAlgo \n' , GaudiAlgo.__doc__
01376 print '\t\t\tDoc-string for class HistoAlgo \n' , HistoAlgo.__doc__
01377 print '\t\t\tDoc-string for class TupleAlgo \n' , TupleAlgo.__doc__
01378
01379
01380
01381
01382 if __name__ == '__main__' :
01383 _help_()
01384
01385
01386
01387