00001
00002
00003
00004
00005
00006
00007
00008
00009 from DybPython.DybPythonAlg import DybPythonAlg
00010 from GaudiPython import SUCCESS, FAILURE
00011 from GaudiPython import gbl
00012 from DybPython.Util import irange
00013 import GaudiKernel.SystemOfUnits as units
00014
00015
00016 TH1F = gbl.TH1F
00017 TimeStamp = gbl.TimeStamp
00018 FeeChannelId = gbl.DayaBay.FeeChannelId
00019 Detector = gbl.DayaBay.Detector
00020
00021
00022 gbl.gStyle.SetHistLineColor(4)
00023 gbl.gStyle.SetHistLineWidth(2)
00024 gbl.gStyle.SetMarkerColor(4)
00025 gbl.gStyle.SetMarkerStyle(8)
00026 gbl.gStyle.SetPalette(1)
00027
00028
00029 class OnlineOfflineAlg(DybPythonAlg):
00030 "Online/Offline Test Algorithm"
00031 def __init__(self,name):
00032 DybPythonAlg.__init__(self,name)
00033 return
00034
00035 def initialize(self):
00036 status = DybPythonAlg.initialize(self)
00037 if status.isFailure(): return status
00038 self.info("initializing")
00039
00040 self.lastReadoutTime = None
00041 self.activeChannelIds = []
00042 self.activeDetectors = []
00043
00044 return SUCCESS
00045
00046 def execute(self):
00047 self.info("executing")
00048
00049
00050 evt = self.evtSvc()
00051 readoutHdr = evt["/Event/Readout/ReadoutHeader"]
00052 if readoutHdr == None:
00053 self.error("Failed to get current readout header")
00054 return FAILURE
00055
00056 readout = readoutHdr.readout()
00057 if readout == None:
00058 self.error("Failed to get readout from header")
00059 return FAILURE
00060
00061 detector = readout.detector()
00062 if self.isNewDetector( detector ):
00063 self.prepareDetectorHistograms( detector )
00064 detectorName = readout.detector().detName()
00065 detectorPath = "/file1/" + detectorName
00066
00067 nChannels = 0
00068 for channelPair in readout.channelReadout():
00069 channel = channelPair.second
00070 channelId = channel.channelId()
00071 if self.isNewChannel( channelId ):
00072 self.prepareChannelHistograms( channelId )
00073
00074
00075 path = "%s/board_%d_connector_%d" % (detectorPath,
00076 channelId.board(),
00077 channelId.connector())
00078 self.debug("Processing channel with path: "+path)
00079
00080 for tdc in channel.tdc():
00081 self.debug("Type of tdc hist: "+str(type( self.stats[path + "/tdc"] )))
00082 self.stats[path + "/tdc"].Fill( tdc )
00083 self.debug("Filled path with tdc: "+str(tdc))
00084
00085 self.debug("Processing adc data with path: "+path)
00086 for adcPair in channel.adc():
00087 adcClock = adcPair.first
00088 adc = adcPair.second
00089 self.stats[path + "/adc"].Fill( adc )
00090
00091 channelIndex = channelId.board()*16 + channelId.connector()
00092 self.stats[detectorPath + "/hitRate"].Fill( channelIndex )
00093 nChannels += 1
00094
00095
00096 self.debug("Processing readout with path: "+detectorPath)
00097
00098 self.stats[detectorPath + "/nChannels"].Fill( nChannels )
00099
00100 self.stats[detectorPath + "/triggerType"].Fill( readout.triggerType() )
00101
00102 if self.lastReadoutTime != None:
00103
00104 dT_trigger = TimeStamp( readout.triggerTime() )
00105 dT_trigger.Subtract( self.lastReadoutTime )
00106 self.debug("Dt Readout: "+str(dT_trigger.GetSeconds()))
00107 self.stats[detectorPath + "/dT_trigger"].Fill(
00108 dT_trigger.GetSeconds() )
00109
00110
00111 self.lastReadoutTime = TimeStamp(readout.triggerTime())
00112 self.debug("done execute")
00113 return SUCCESS
00114
00115 def finalize(self):
00116 self.info("finalizing")
00117
00118 for channelInt in self.activeChannelIds:
00119 channelId = FeeChannelId( channelInt )
00120 detectorPath = "/file1/" + channelId.detName()
00121 path = "%s/board_%d_connector_%d" % (detectorPath,
00122 channelId.board(),
00123 channelId.connector())
00124 channelIndex = channelId.board()*16 + channelId.connector()
00125
00126 meanAdc = self.stats[path + "/adc"].GetMean()
00127 rmsAdc = self.stats[path + "/adc"].GetRMS()
00128 self.stats[detectorPath + "/meanAdc"].Fill(channelIndex, meanAdc)
00129 self.stats[detectorPath + "/rmsAdc"].Fill(channelIndex, rmsAdc)
00130 for detectorInt in self.activeDetectors:
00131
00132 detector = Detector( detectorInt )
00133 detectorPath = "/file1/" + detector.detName()
00134 nReadouts = self.stats[detectorPath + "/nChannels"].GetEntries()
00135 self.stats[detectorPath + "/hitRate"].Scale( 1 / nReadouts )
00136
00137 status = DybPythonAlg.finalize(self)
00138 return status
00139
00140 def isNewChannel(self, channelId):
00141
00142 if channelId.fullPackedData() in self.activeChannelIds:
00143 return False
00144 return True
00145
00146 def isNewDetector(self, detector):
00147
00148 if detector.fullPackedData() in self.activeDetectors:
00149 return False
00150 return True
00151
00152 def prepareDetectorHistograms(self, detector):
00153
00154 path = "/file1/" + detector.detName()
00155 self.debug("Making histograms at path: "+path)
00156
00157 self.stats[path + "/nChannels"] = TH1F("nChannels",
00158 "Number of Hit Channels",
00159 250,0,250)
00160 hist = self.stats[path + "/nChannels"]
00161 hist.GetXaxis().SetTitle("Number of Hit Channels")
00162 hist.GetYaxis().SetTitle("Number of Triggered Readouts")
00163
00164 self.stats[path + "/triggerType"] = TH1F("triggerType",
00165 "Trigger Type",
00166 4096,0,4096)
00167 hist = self.stats[path + "/triggerType"]
00168 hist.GetXaxis().SetTitle("Trigger Type")
00169 hist.GetYaxis().SetTitle("Number of Triggered Readouts")
00170
00171 self.stats[path + "/dT_trigger"] = TH1F("dT_trigger",
00172 "Time between triggers [s]",
00173 1000,0,1)
00174 hist = self.stats[path + "/dT_trigger"]
00175 hist.GetXaxis().SetTitle("Time Between Triggered Readouts [s]")
00176 hist.GetYaxis().SetTitle("Number of Triggered Readouts / 1 ms")
00177
00178
00179 self.stats[path + "/meanAdc"] = TH1F("meanAdc",
00180 "Mean ADC by channel",
00181 300,0,300)
00182 hist = self.stats[path + "/meanAdc"]
00183 hist.GetXaxis().SetTitle("Channel Index (Board*16 + Connector)")
00184 hist.GetYaxis().SetTitle("Mean ADC value")
00185
00186 self.stats[path + "/rmsAdc"] = TH1F("rmsAdc",
00187 "RMS ADC by channel (board*16 + connector)",
00188 300,0,300)
00189 hist = self.stats[path + "/rmsAdc"]
00190 hist.GetXaxis().SetTitle("Channel Index (Board*16 + Connector)")
00191 hist.GetYaxis().SetTitle("RMS of ADC values")
00192
00193 self.stats[path + "/hitRate"] = TH1F("hitRate",
00194 "Hit Rate by channel",
00195 300,0,300)
00196 hist = self.stats[path + "/hitRate"]
00197 hist.GetXaxis().SetTitle("Channel Index (Board*16 + Connector)")
00198 hist.GetYaxis().SetTitle("Number of Hits / Number of Triggered Readouts")
00199 self.activeDetectors.append( detector.fullPackedData() )
00200 return
00201
00202 def prepareChannelHistograms(self, channelId):
00203
00204 path = "/file1/%s/board_%d_connector_%d" % (channelId.detName(),
00205 channelId.board(),
00206 channelId.connector())
00207 self.debug("Making histograms at path: "+path)
00208
00209 self.stats[path + "/tdc"] = TH1F("tdc",
00210 "TDC Values",
00211 4096,0,4096)
00212 hist = self.stats[path + "/tdc"]
00213 hist.GetXaxis().SetTitle("TDC value")
00214 hist.GetYaxis().SetTitle("Number of TDCs")
00215
00216 self.stats[path + "/adc"] = TH1F("adc",
00217 "ADC Values",
00218 4096,0,4096)
00219 hist = self.stats[path + "/adc"]
00220 hist.GetXaxis().SetTitle("ADC value")
00221 hist.GetYaxis().SetTitle("Number of ADCs")
00222 self.activeChannelIds.append( channelId.fullPackedData() )
00223 return
00224
00225
00226
00227 def configure():
00228 from StatisticsSvc.StatisticsSvcConf import StatisticsSvc
00229 statsSvc = StatisticsSvc()
00230 statsSvc.Output ={"file1":"onlineOfflineResult.root"}
00231 return
00232
00233 def run(app):
00234 '''
00235 Configure and add an algorithm to job
00236 '''
00237 app.ExtSvc += ["StaticCableSvc", "StatisticsSvc"]
00238 myAlg = OnlineOfflineAlg("MyOnlineOfflineAlg")
00239 app.addAlgorithm(myAlg)
00240 pass
00241