Changeset 12874
- Timestamp:
- 02/08/12 14:22:47 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/tools/fbsl2.py
r12813 r12874 13 13 """ 14 14 """ 15 def __init__( self, fnames = {}, step = 500, bsl_corr = True ): 15 def __init__( self, rfiles = {}, step = 500, bsl_corr = True, 16 max_events = -1 ): 16 17 """ 17 fnames : dictionary of strings : run file names 18 step : integer : number of events to be used for the calculation of a baseline values 18 rfiles : dictionary of strings : run file names 19 step : integer : number of events to be used for 20 the calculation of a baseline values 19 21 bsl_corr : bool : apply baseline correction 20 22 max_events : maximum number of events to be processed 23 21 24 self: 22 25 blsMeanValues : np.array() to store computed baseline values 23 26 """ 24 27 25 if fnames= {}:28 if rfiles == {}: 26 29 print "no file names given for analysis!!!" 27 30 return (-1) 28 31 else: 29 print " analysing data: "30 fnames.info()32 print "baseline analysing data: " 33 rfiles.info() 31 34 32 self.step = step 33 self.bsl_corr = True 35 self.rfiles = rfiles 36 self.step = step 37 self.bsl_corr = bsl_corr 34 38 35 39 if bsl_corr: … … 38 42 print 'no baseline correction' 39 43 40 self.bslMeanValues = np.zeros( ( nsteps, rd.NPIX) )41 44 42 45 # create the object providing the rawdata 43 rd = pyfact.rawdata( fnames['data'], fnames['drscal'] ) 44 self.nevents = rd.NEvents 46 self.rd = pyfact.rawdata( rfiles.names['data'], rfiles.names['drscal'] ) 47 self.npix = self.rd.NPIX 48 49 if max_events == -1: 50 self.nevents = self.rd.NEvents 51 else: 52 self.nevents = max_events 53 54 self.steps() # compute the number of steps 45 55 46 self.steps() 56 self.bslMeanValues = np.zeros( ( self.nsteps, self.npix ) ) 57 58 self.pixhist = hist_array( self.npix, 400, -99.5, 100.5, 59 'bsl_pix', 'Pixel Baseline', 60 'sample value in mV', 'Entries / 0.5 mV' ) 61 self.bslSummaryHistos = bslHistograms( 'bsl_sum' ) 62 63 self.run() # compute baseline 64 self.save() # save results 47 65 48 # rd.ReadBaseline( 'histo.root', 'bls_sum/hplt_mean' ) 66 def steps( self ): 67 """ compute the ranges of events 68 69 self: 70 nsteps : integer : number of ranges of events 71 evranges : list of range boundaries of length nsteps + 1 72 """ 73 74 nsteps = self.nevents / self.step + 1 75 evranges = range(0, nsteps * self.step, self.step) 76 77 # add the events of the last incomplete step to the previous once 78 evranges[-1] = int( self.nevents ) 49 79 50 def steps( self ):51 """ compute the ranges of events80 self.nsteps = nsteps 81 self.evranges = evranges 52 82 53 self: 54 nsteps : integer : number of ranges the events are devided into 55 evranges : list of range boundaries of length nsteps + 1 56 """ 57 58 nsteps = self.nevents / self.step 59 evranges = range(0, (nsteps + 1) * self.step, self.step) 60 61 # add the events of the last incomplete step to the previous once 62 evranges[-1] = int( rd.NEvents ) 83 print 'nevents: ', self.nevents 84 print 'ranges: ', self.evranges 63 85 64 self.nsteps = nsteps65 self.evranges = evranges66 86 67 print 'nevents: ', self.nevents 68 print 'ranges: ', self.evranges 69 70 def loopEvents( first, last, hist): 71 """ 72 LOOP over all events: Do what you want, here! 73 """ 74 for ev in range( first, last ): 75 print 'Event', ev 76 rd.next() 77 # rd.CorrectBaseline() 78 hist.fillall( rd.acalData ) 87 def loopEvents( self, first, last, hist): 88 """ LOOP over all events: Do what you want, here! 89 first : integer : first event to process 90 last : integer : last event to process 91 hist : hist_array : fill all pixel data 92 """ 93 for ev in range( first, last ): 94 if ev%100 == 0: print 'Event', ev 95 self.rd.next() 96 # rd.CorrectBaseline() 97 hist.fillall( self.rd.acalData ) 79 98 80 99 81 def loopPixel( npix, phist ): 82 83 vmaxprob = np.zeros( npix ) 84 v_rms = np.zeros( npix ) 100 def loopPixel( self, npix, phist ): 101 """ loop over pixel and extract most probable values 102 """ 103 vmaxprob = np.zeros( npix ) 104 v_rms = np.zeros( npix ) 85 105 86 106 for pix in range( npix ): 87 107 88 108 hpix = phist.list[pix] 89 109 90 vmaxprob[pix] = hpix.GetXaxis().GetBinCenter( hpix.GetMaximumBin() ) 91 bslSummaryHistos.dict['hbsl_mean'].Fill( vmaxprob[pix] ) 92 bslSummaryHistos.dict['hplt_mean'].SetBinContent(pix+1, vmaxprob[pix] ) 110 vmaxprob[pix] = hpix.GetXaxis().GetBinCenter( hpix.GetMaximumBin() ) 111 self.bslSummaryHistos.dict['hbsl_mean'].Fill( vmaxprob[pix] ) 112 self.bslSummaryHistos.dict['hplt_mean'].SetBinContent(pix+1, 113 vmaxprob[pix] ) 93 114 94 v_rms[pix] = hpix.GetRMS() 95 bslSummaryHistos.dict['hbsl_rms'].Fill( v_rms[pix] ) 96 bslSummaryHistos.dict['hplt_rms'].SetBinContent( pix+1, v_rms[pix] ); 97 98 return vmaxprob 115 v_rms[pix] = hpix.GetRMS() 116 self.bslSummaryHistos.dict['hbsl_rms'].Fill( v_rms[pix] ) 117 self.bslSummaryHistos.dict['hplt_rms'].SetBinContent( pix+1, 118 v_rms[pix] ); 119 120 self.vmaxprob = vmaxprob 99 121 100 122 101 def run( self ): 102 """ run the analysis 103 """ 123 def run( self ): 124 """ run the analysis 125 """ 126 127 for step in range( self.nsteps - 1 ): 128 129 print step, 'processing events: ', self.evranges[ step ], 'to ', 130 print self.evranges[ step+1 ] 104 131 105 pixhist = hist_array( rd.NPIX, 400, -99.5, 100.5, 106 'bsl_pix', 'Pixel Baseline', 107 'sample value in mV', 'Entries / 0.5 mV' ) 132 pixstephist = hist_array( self.npix, 400, -99.5, 100.5, 133 'bsl_pix_step', 'Pixel Baseline', 134 'sample value in mV', 'Entries / 0.5 mV' ) 135 136 self.loopEvents( self.evranges[step], self.evranges[step+1], 137 pixstephist ) 108 138 109 for step in range( self.nsteps ): 110 111 print self.step, 'processing events: ', self.evranges[ self.step], 'to ', 112 self.evranges[self.step+1] 139 self.pixhist.y[:,:] += pixstephist.y[:,:] 140 pixstephist.SetBinContent() 113 141 114 pixstephist = hist_array( rd.NPIX, 400, -99.5, 100.5, 115 'bsl_pix', 'Pixel Baseline', 116 'sample value in mV', 'Entries / 0.5 mV' ) 117 118 self.loopEvents( evranges[step], evranges[step+1], pixstephist ) 142 bslSummaryHistos = bslHistograms( 'bsl_sum_step' ) 143 self.bslMeanValues[step,:] = self.loopPixel( self.npix, pixstephist ) 144 # print self.bslMeanValues 119 145 120 pixhist.y[:,:] += pixstephist.y[:,:] 121 pixstephist.SetBinContent() 122 123 bslSummaryHistos = bslHistograms( 'bsl_sum' ) 124 self.bslMeanValues[step,:] = loopPixel( rd.NPIX, pixstephist ) 125 # print self.bslMeanValues 126 127 fname = 'h'+ str( evranges[step] ) + '_to_'+ str( evranges[step+1] ) + '.root' 128 pyfact.SaveHistograms( [pixstephist, bslSummaryHistos], fname ) 146 fname = 'h'+ str( self.evranges[step] ) + '_to_'+ str( self.evranges[step+1] ) + '.root' 147 SaveHistograms( [pixstephist, bslSummaryHistos], fname ) 129 148 130 131 149 del pixstephist 150 del bslSummaryHistos 132 151 133 152 self.pixhist.SetBinContent() 134 153 135 bslSummaryHistos = bslHistograms( 'bsl_sum' ) 136 loopPixel( rd.NPIX, pixhist ) 154 self.loopPixel( self.rd.NPIX, self.pixhist ) 137 155 138 self.save()139 # fname = 'hbsl.root'140 156 141 def save( self ): 142 """ save data after completion of analysis 143 """ 144 pyfact.SaveHistograms( [pixhist, bslSummaryHistos], resFileName + '_bsl.root' ) 157 def save( self ): 158 """ save baseline values and histograms 159 """ 160 SaveHistograms( [ self.pixhist, self.bslSummaryHistos], 161 self.rfiles.names['results'] + '_bsl.root' ) 162 163 np.savetxt( self.rfiles.names['results'] + '_bls.txt', self.bslMeanValues, 164 fmt = '%8.3f' ) 165 np.save( self.rfiles.names['results'] + '_bsl.npy', self.bslMeanValues ) 166 145 167 146 np.savetxt( resFileName + '_bls.dat', self.bslMeanValues, fmt = '%8.3f' ) 147 np.save( resFileName + '_bsl.npy', self.bslMeanValues ) 168 if __name__ == '__main__': 169 """ create an instance for test purposes 170 171 """ 172 runfiles = pyfact.fnames() 173 174 runfiles.info() 175 176 bcal = baseline_ana( rfiles = runfiles, step = 14, 177 bsl_corr = False, max_events = 60 )
Note:
See TracChangeset
for help on using the changeset viewer.