/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

UserTaggingAlg.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # zhang@caltech.edu 2011-03-01
00003 
00004 from DybPython.DybPythonAlg import DybPythonAlg
00005 from GaudiPython import SUCCESS, FAILURE
00006 from GaudiPython import gbl
00007 import ROOT
00008 from UserTagging.Models import TagSet, Tag, Data, Int, Float, IntArray, FloatArray
00009 
00010 class UserTaggingAlg(DybPythonAlg):
00011     '''
00012     Abstract Class for Saving UserTag and UserData
00013     
00014     The following demostrate the layout of UserTaggingAlg:
00015     
00016                                 UserTaggingAlg
00017                                       |
00018                                       |
00019                                ----------------
00020                                |              |
00021                                |              |
00022                              tagSet      'other members ...'
00023                                |
00024                                |
00025                           -------------
00026                           |     |     |
00027                           |     |     |
00028                          Tag1  Tag2  ...
00029                           |
00030                           |
00031                       ----------------------------------
00032                       |         |      |      |        |
00033                       |         |      |      |        |
00034                     dataSet    path  header isTagged   ...
00035                       |
00036                       |
00037                 -------------
00038                 |     |     |
00039                 |     |     |
00040                Data1 Data2  ...
00041                 |
00042                 |           
00043            ---------------------------
00044            |           |      |      |
00045            |           |      |      |
00046          paraSet      path  header   ...
00047            |
00048            |
00049      -------------
00050      |     |     |
00051      |     |     |
00052     Para1 Para2  ...
00053     '''
00054     
00055     def __init__(self,name):
00056         DybPythonAlg.__init__(self,name)
00057         
00058         # avoid a 'bug?' of UserDataHeader
00059         # gbl.std.vector('float')() has to be called at least once 
00060         # before instantiate UserDataHeader 
00061         tmp = gbl.std.vector('float')();
00062         return
00063       
00064     # ---------------------------------------
00065     def initialize(self):
00066 
00067         '''
00068         User need not implement, but if so, you must call this method on the
00069         super-class'''
00070 
00071         status = DybPythonAlg.initialize(self)
00072         if status.isFailure(): return status
00073         
00074         # Catch current Job ID
00075         jobInfoSvc = self.svc("IJobInfoSvc","JobInfoSvc")
00076         if not jobInfoSvc:
00077             self.error("Failed to initialize JobInfoSvc")
00078             return FAILURE
00079         self.jobId = jobInfoSvc.currentJobInfo().jobId()
00080         
00081         # self.tagSet = {}
00082         self.tagSet = TagSet()
00083         
00084         status = self.initTagList()
00085         if status == FAILURE: 
00086             self.error('error in initTagList() function')
00087             return FAILURE
00088 
00089         self.execNumber = 1
00090         
00091         return SUCCESS  
00092     
00093     # ---------------------------------------    
00094     def initTagList(self):
00095         '''
00096         Child Class should overwrite this function.
00097         Child Class should initialize UserTag and UserData here
00098         
00099         example adding Tag:
00100         self.addTag('AD' , '/Event/UserTag/Detector/AD')
00101         
00102         or alternative (pythonic) syntax:
00103         self.tagSet.AD  = Tag('/Event/UserTag/Detector/AD')
00104         
00105         example adding a Dummy Tag and UserData:
00106         self.addTag('Dummy', ''
00107             ).addData('ADReadoutData', '/Event/UserData/AD/ADReadoutData'
00108             ).addInt('nChannels', 'nHits'
00109             ).addFloat('chargeSum')
00110         
00111         or alternative (pythonic) syntax:
00112         self.tagSet.Dummy = Tag()
00113         self.tagSet.Dummy.dataSet.ADReadoutData = Data('/Event/UserData/AD/ADReadoutData')
00114         paraSet = self.tagSet.Dummy.dataSet.ADReadoutData.paraSet
00115         paraSet.nChannels = Int()
00116         paraSet.nHits = Int()
00117         paraSet.chargeSum = Float()
00118         '''
00119 
00120     # ---------------------------------------
00121     def addTag(self, name, path=''):
00122         """
00123         Add one UserTag by name, provide a path to cause the tag
00124         object to be sent through I/O.
00125 
00126         Can also do:
00127 
00128           self.tagSet.<name> = Models.Tag([path])
00129         """
00130         return self.tagSet.addTag(name, path)
00131 
00132     # ---------------------------------------
00133     def getTag(self, name):
00134         """
00135         Look the UserTagHeader a tag by name.
00136 
00137         Can also do:
00138 
00139           mytag = self.tagSet.<name>
00140         """
00141         return self.tagSet.get(name)
00142     
00143     # --------------------------------------
00144     def tagIt(self, name):
00145         """
00146         Tag the named UserTag.
00147 
00148         Can also do:
00149         
00150           self.tagSet.<name>.tagIt()
00151         """
00152         return self.tagSet.get(name).tagIt()
00153             
00154     # --------------------------------------
00155     def reset(self):
00156         '''Reset the Tag States'''
00157         
00158         for tag in self.tagSet.all():
00159             if tag.isTagged:
00160                 if tag.path:
00161                     tag.header = gbl.DayaBay.HeaderObject()
00162                     self.initializeHeader(tag.header)
00163                 for data in tag.dataSet.all():
00164                     data.header = gbl.DayaBay.UserDataHeader()
00165                     self.initializeHeader(data.header)
00166                 tag.isTagged = False
00167                     
00168     # --------------------------------------
00169     def check(self, evt):
00170         '''Child Class should overwrie this function'''
00171         # all the real logics/algorithms happen here:
00172         # - check the properties of the event headers, run some algorithms.
00173         # - decide if this event should be tagged.
00174         # - if shoud be tagged, call tagIt(tagName) function to trigger the I/O
00175         # - save some calculated variables to the UserData
00176         # - the tags and user data will then be automatically saved 
00177         #   to the output (root) file at the paths defined in the tags
00178         #   at this exec cycle.
00179         #
00180         # If GaudiKernel.FAILURE is returned the execute() method will
00181         # immediately return this value.
00182         pass
00183             
00184     # --------------------------------------
00185     def config(self, evt):
00186         '''
00187         Configure the UserTagHeader and UserDataHeader object
00188         Child class can overwrite depends on the situation.
00189 
00190         The "evt" is the transient event store object.
00191         '''
00192         
00193         for tag in self.tagSet.all():
00194             if tag.isTagged:
00195                 if tag.path:
00196                     self.finalizeHeader(tag.header, evt[tag.baseHeader])
00197                 for data in tag.dataSet.all():
00198                     if tag.path:
00199                         self.finalizeHeader(data.header, tag.header)
00200                     else:
00201                         self.finalizeHeader(data.header, evt[tag.baseHeader])
00202 
00203                     
00204     # --------------------------------------
00205     def save(self, evt):
00206         '''
00207         Save the UserTag and UserData objects
00208 
00209         Most subclass should not override.
00210         '''
00211         
00212         for tag in self.tagSet.all():
00213             if tag.isTagged:
00214                 tag.nTagged += 1
00215                 if tag.path:
00216                     evt[tag.path] = tag.header
00217                     del tag.header
00218                 for data in tag.dataSet.all():
00219                     data.save()
00220                     evt[data.path] = data.header
00221                     del data.header
00222 
00223     # --------------------------------------
00224     def post_save(self, evt):
00225         '''
00226         Hook to allow subclass to perform actions
00227         after UserTag and UserData being saved to the event store. 
00228         '''
00229         pass
00230                                 
00231         # --------------------------------------
00232     def execute(self):
00233         'Subclass should not override'
00234                     
00235         evt = self.evtSvc()
00236         
00237         status = self.reset()  
00238         if status == FAILURE: 
00239             self.error('error in reset() method')
00240             return FAILURE
00241         
00242         status = self.check(evt)
00243         if status == FAILURE: 
00244             self.error('error in check() method')
00245             return FAILURE
00246         
00247         status = self.config(evt)
00248         if status == FAILURE: 
00249             self.error('error in config() method')
00250             return FAILURE
00251         
00252         status = self.save(evt)
00253         if status == FAILURE: 
00254             self.error('error in save() method')
00255             return FAILURE
00256         
00257         status = self.post_save(evt)
00258         if status == FAILURE: 
00259             self.error('error in post_save() method')
00260             return FAILURE
00261                 
00262         self.execNumber += 1
00263         return SUCCESS
00264 
00265     # --------------------------------------
00266     def finalPrint(self):
00267         '''Print how many tagged events'''
00268         
00269         for name, tag in self.tagSet.all_dict().items():
00270             self.info( 'Summary: UserTag %s : %d events tagged at %s' % (name, tag.nTagged, tag.path) )
00271             for dataName, data in tag.dataSet.all_dict().items():
00272                 self.info( "\tUserData %s saved at %s" % (dataName, data.path) )
00273                 
00274     # -------------------------------------- 
00275     def finalize(self):     
00276         'Subclass may override, but it must called this method on the super-class'
00277         self.finalPrint()   
00278         status = DybPythonAlg.finalize(self)
00279         return status
00280 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 10:02:01 for UserTagging by doxygen 1.7.4