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

In This Package:

Models.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from UserTagging.Manager import ObjectSet
00004 from GaudiPython import gbl
00005 import ROOT
00006 
00007 class Data(object):
00008     '''Model for UserData'''
00009     
00010     def __init__(self, path=''):
00011         self.path = path
00012         self.header = gbl.DayaBay.UserDataHeader()
00013         ROOT.SetOwnership(self.header, False)
00014         self.paraSet = ParameterSet()
00015 
00016     # ---------------------------------------
00017     def save(self):
00018         """Save Parameters to the UserDataHeader"""   
00019         
00020         for paraName, paraClass in self.paraSet.description.items():
00021             if paraClass == Int:
00022                 self.header.setInt(paraName, self.paraSet.__dict__[paraName])
00023                 self.paraSet.__setattr__(paraName, Int())
00024             elif paraClass == Float:
00025                 self.header.setFloat(paraName, self.paraSet.__dict__[paraName])
00026                 self.paraSet.__setattr__(paraName, Float())
00027             elif paraClass == IntArray:
00028                 self.header.setIntArray(paraName, gbl.std.vector('int')())
00029                 for value in self.paraSet.__dict__[paraName]:
00030                     self.header.get(paraName).value().push_back(value)
00031                 self.paraSet.__setattr__(paraName, IntArray())
00032             elif paraClass == FloatArray:
00033                 self.header.setFloatArray(paraName, gbl.std.vector('float')())
00034                 for value in self.paraSet.__dict__[paraName]:
00035                     self.header.get(paraName).value().push_back(value)
00036                 self.paraSet.__setattr__(paraName, FloatArray())
00037             else:
00038                 raise AttributeError(paraName + ' should not be in the description. ' + str(paraClass))
00039                                                      
00040     # ---------------------------------------
00041     def addInt(self, *paraList):
00042         self.paraSet.addInt(*paraList)
00043         return self
00044     
00045     def addFloat(self, *paraList):
00046         self.paraSet.addFloat(*paraList)
00047         return self
00048     
00049     def addIntArray(self, *paraList):
00050         self.paraSet.addIntArray(*paraList)
00051         return self
00052     
00053     def addFloatArray(self, *paraList):
00054         self.paraSet.addFloatArray(*paraList)
00055         return self
00056                 
00057     # ---------------------------------------
00058     def set(self, name, value):
00059         """set Int or Float"""
00060         try:
00061             paraClass = self.paraSet.description[name]
00062         except KeyError:
00063             raise NameError('Parameter ' + name + ' is not defined yet')
00064         if paraClass in [Int, Float]:
00065             self.paraSet.__setattr__(name, value)
00066         else:
00067             raise AttributeError('Parameter ' + name + ' is an array. Use append() instead.')        
00068         
00069         return self
00070             
00071     # ---------------------------------------
00072     def append(self, name, value):
00073         """append to IntArray or FloatArray"""
00074         try:
00075             paraClass = self.paraSet.description[name]
00076         except KeyError:
00077             raise NameError('Parameter ' + name + ' is not defined yet')
00078         try:
00079             self.paraSet.__dict__[name].append(value)
00080         except AttributeError:
00081             raise AttributeError('Parameter ' + name + ' is not array. Use set() instead.')
00082         return self
00083 
00084     def get(self, paraName):
00085         '''wrapper of self.paraSet.get()'''             
00086         return self.paraSet.get(paraName)
00087                
00088     # ---------------------------------------
00089     def dump(self):
00090         '''wrapper of self.paraSet.dump()'''    
00091         print "\n----- dumping ", self.path, ' ------'         
00092         self.paraSet.dump()
00093         return self
00094 
00095     # ---------------------------------------
00096     def setInputHeaders(self, inputHeaders):
00097         '''
00098         a workaround on PyROOT's poor verctor support 
00099         on the original HeaderObject.setInputHeaders() 
00100         
00101         inputHeaders: a python list of HeaderObject's
00102         '''
00103         self.header.inputHeaders().clear()
00104         for hdr in inputHeaders:
00105             self.header.addInputHeader(hdr)
00106         return self
00107 
00108     # ---------------------------------------
00109     def addInputHeader(self, hdr):
00110         self.header.addInputHeader(hdr)
00111         return self
00112 
00113 # =========================================== 
00114 class Tag(object):
00115     '''Model for UserTag'''
00116     
00117     def __init__(self, path=''):
00118         # the path where the tagHeader saved to, e.g. '/Event/Tag/Detector/AD'
00119         # path can be empty string, in which case only the data will be saved
00120         self.path = path
00121         
00122         # the 'tagHeader' is saved as a generic HeaderObject
00123         # this is instantiated at every execution cycle
00124         self.header = gbl.DayaBay.HeaderObject()
00125         ROOT.SetOwnership(self.header, False)
00126         
00127         # the context, execNumber, earliest, latest properties
00128         # of the tagHeader will be set the same as the 'baseHeader' HeaderObject
00129         self.baseHeader = '/Event/Readout/ReadoutHeader'    # default
00130         
00131         # at the end of each exec cycle, the algorithm checks 'isTagged'
00132         # flag, and decide whether of not to save the tag and the user data
00133         self.isTagged = False
00134         
00135         # automatically increased by one for each tagged event
00136         self.nTagged = 0
00137         
00138         # you can save a set of Data's together with the tag        
00139         self.dataSet = DataSet()
00140         
00141     # ---------------------------------------
00142     def tagIt(self):
00143         self.isTagged = True
00144 
00145     # ---------------------------------------
00146     def addData(self, name, path):
00147         """Add one UserTag"""
00148         return self.dataSet.addData(name, path)
00149 
00150     # ---------------------------------------
00151     def getData(self, name):
00152         """Get the UserTagHeader"""
00153         return self.dataSet.getData(name)
00154 
00155     # ---------------------------------------
00156     def setBaseHeader(self, baseHeader):
00157         self.baseHeader = baseHeader
00158         return self
00159         
00160     # ---------------------------------------
00161     def setInputHeaders(self, inputHeaders):
00162         '''
00163         a workaround on PyROOT's poor verctor support 
00164         on the original HeaderObject.setInputHeaders() 
00165         
00166         inputHeaders: a python list of HeaderObject's
00167         '''
00168         self.header.inputHeaders().clear()
00169         for hdr in inputHeaders:
00170             self.header.addInputHeader(hdr)
00171         return self
00172 
00173     # ---------------------------------------
00174     def addInputHeader(self, hdr):
00175         self.header.addInputHeader(hdr)
00176         return self
00177 
00178 
00179 # =========================================== 
00180 class Int(int):
00181     pass
00182 
00183 # =========================================== 
00184 class Float(float):
00185     pass
00186 
00187 # =========================================== 
00188 class IntArray(list):
00189     pass
00190         
00191 # =========================================== 
00192 class FloatArray(list):
00193     pass
00194     
00195 # =========================================== 
00196 class ParameterSet(ObjectSet):
00197     '''Manager for colection of Parameter's'''
00198     
00199     def __init__(self):    
00200         self.classForManaging = (Int, Float, IntArray, FloatArray)
00201         # description = {name: class} for bookkeeping the attribute
00202         self.description = {}
00203         self.getParameter = self.get    # make alias
00204 
00205     # ---------------------------------------
00206     def addInt(self, *paraList):
00207         for para in paraList:
00208             self.__setattr__(para, Int())
00209         return self
00210     
00211     def addFloat(self, *paraList):
00212         for para in paraList:
00213             self.__setattr__(para, Float())
00214         return self
00215     
00216     def addIntArray(self, *paraList):
00217         for para in paraList:
00218             self.__setattr__(para, IntArray())
00219         return self
00220     
00221     def addFloatArray(self, *paraList):
00222         for para in paraList:
00223             self.__setattr__(para, FloatArray())
00224         return self
00225                     
00226     # ---------------------------------------    
00227     def dump(self):
00228         '''print all values in the parameter set'''
00229         for paraName, paraClass in self.description.items():
00230             print paraName, "\t", self.__dict__[paraName]
00231                     
00232 # =========================================== 
00233 class DataSet(ObjectSet):
00234     '''Manager for colection of Data's'''
00235     
00236     def __init__(self):  
00237         self.classForManaging = Data
00238         # description = {name: class} for bookkeeping the attribute
00239         self.description = {} 
00240         self.getData = self.get    # make alias
00241     
00242     def addData(self, name, path):
00243         return ObjectSet.add(self, name, Data(path))    
00244         
00245 # =========================================== 
00246 class TagSet(ObjectSet):
00247     '''Manager for colection of Tag's'''
00248     
00249     def __init__(self):      
00250         self.classForManaging = Tag
00251         # description = {name: class} for bookkeeping the attribute
00252         self.description = {}
00253         self.getTag = self.get    # make alias
00254     
00255     def addTag(self, name, path=''):
00256         return ObjectSet.add(self, name, Tag(path))        
00257         
| 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