Index: fact/tools/pyscripts/tools/fbsl2.py
===================================================================
--- fact/tools/pyscripts/tools/fbsl2.py	(revision 12872)
+++ fact/tools/pyscripts/tools/fbsl2.py	(revision 12874)
@@ -13,23 +13,27 @@
     """
     """
-    def __init__( self, fnames = {}, step = 500, bsl_corr = True ):
+    def __init__( self, rfiles = {}, step = 500, bsl_corr = True,
+                  max_events = -1 ):
         """
-        fnames   : dictionary of strings : run file names
-        step     : integer : number of events to be used for the calculation of a baseline values
+        rfiles   : dictionary of strings : run file names
+        step     : integer : number of events to be used for
+                   the calculation of a baseline values
         bsl_corr : bool : apply baseline correction
-
+        max_events : maximum number of events to be processed
+        
         self:
             blsMeanValues : np.array() to store computed baseline values
         """
 
-        if fnames = {}:
+        if rfiles == {}:
             print "no file names given for analysis!!!"
             return (-1) 
         else:
-            print "analysing data: "
-            fnames.info()
+            print "baseline analysing data: "
+            rfiles.info()
 
-        self.step = step
-        self.bsl_corr = True
+        self.rfiles   = rfiles
+        self.step     = step
+        self.bsl_corr = bsl_corr
 
         if bsl_corr:
@@ -38,110 +42,136 @@
             print 'no baseline correction'
 
-        self.bslMeanValues = np.zeros( ( nsteps, rd.NPIX) )
 
         # create the object providing the rawdata
-        rd = pyfact.rawdata( fnames['data'], fnames['drscal'] )
-        self.nevents = rd.NEvents
+        self.rd = pyfact.rawdata( rfiles.names['data'], rfiles.names['drscal'] )
+        self.npix    = self.rd.NPIX
+        
+        if max_events == -1:
+            self.nevents = self.rd.NEvents
+        else:
+            self.nevents = max_events
+        
+        self.steps() # compute the number of steps
 
-        self.steps()
+        self.bslMeanValues = np.zeros( ( self.nsteps, self.npix ) )
+            
+        self.pixhist = hist_array( self.npix, 400, -99.5, 100.5,
+                              'bsl_pix', 'Pixel Baseline',
+                              'sample value in mV', 'Entries / 0.5 mV' )  
+        self.bslSummaryHistos = bslHistograms( 'bsl_sum' )
+
+        self.run()  # compute baseline
+        self.save() # save results
         
-        # rd.ReadBaseline( 'histo.root', 'bls_sum/hplt_mean' )
+    def steps( self ):
+        """ compute the ranges of events
+        
+            self:
+                nsteps   : integer : number of ranges of events
+                evranges : list of range boundaries of length nsteps + 1
+        """
+            
+        nsteps   = self.nevents / self.step + 1
+        evranges = range(0, nsteps * self.step, self.step)
+            
+        # add the events of the last incomplete step to the previous once
+        evranges[-1] = int( self.nevents )
 
-        def steps( self ):
-            """ compute the ranges of events
+        self.nsteps   = nsteps
+        self.evranges = evranges
 
-            self:
-                nsteps   : integer : number of ranges the events are devided into
-                evranges : list of range boundaries of length nsteps + 1
-            """
-            
-            nsteps   = self.nevents / self.step
-            evranges = range(0, (nsteps + 1) * self.step, self.step)
-            
-            # add the events of the last incomplete step to the previous once
-            evranges[-1] = int( rd.NEvents )
+        print 'nevents: ', self.nevents
+        print 'ranges:  ', self.evranges
 
-            self.nsteps   = nsteps
-            self.evranges = evranges
 
-            print 'nevents: ', self.nevents
-            print 'ranges:  ', self.evranges
-
-        def loopEvents( first, last, hist):
-            """
-            LOOP over all events: Do what you want, here!
-            """
-            for ev in range( first, last ):
-                print 'Event', ev
-                rd.next()
-                # rd.CorrectBaseline()
-                hist.fillall( rd.acalData )
+    def loopEvents( self, first, last, hist):
+        """ LOOP over all events: Do what you want, here!
+        first : integer : first event to process
+        last  : integer : last event to process
+        hist  : hist_array : fill all pixel data
+        """
+        for ev in range( first, last ):
+            if ev%100 == 0: print 'Event', ev
+            self.rd.next()
+            # rd.CorrectBaseline()
+            hist.fillall( self.rd.acalData )
                 
                 
-        def loopPixel( npix, phist ):
-            
-            vmaxprob = np.zeros( npix )
-            v_rms    = np.zeros( npix )
+    def loopPixel( self, npix, phist ):
+        """ loop over pixel and extract most probable values 
+        """
+        vmaxprob = np.zeros( npix )
+        v_rms    = np.zeros( npix )
 
-            for pix in range( npix ):
+        for pix in range( npix ):
     
-                hpix = phist.list[pix]
+            hpix = phist.list[pix]
     
-                vmaxprob[pix] = hpix.GetXaxis().GetBinCenter( hpix.GetMaximumBin() )
-                bslSummaryHistos.dict['hbsl_mean'].Fill( vmaxprob[pix] )
-                bslSummaryHistos.dict['hplt_mean'].SetBinContent(pix+1, vmaxprob[pix] )
+            vmaxprob[pix] = hpix.GetXaxis().GetBinCenter( hpix.GetMaximumBin() )
+            self.bslSummaryHistos.dict['hbsl_mean'].Fill( vmaxprob[pix] )
+            self.bslSummaryHistos.dict['hplt_mean'].SetBinContent(pix+1,
+                                                             vmaxprob[pix] )
                 
-                v_rms[pix] = hpix.GetRMS()
-                bslSummaryHistos.dict['hbsl_rms'].Fill( v_rms[pix] )
-                bslSummaryHistos.dict['hplt_rms'].SetBinContent( pix+1, v_rms[pix] );
-                
-            return vmaxprob
+            v_rms[pix] = hpix.GetRMS()
+            self.bslSummaryHistos.dict['hbsl_rms'].Fill( v_rms[pix] )
+            self.bslSummaryHistos.dict['hplt_rms'].SetBinContent( pix+1,
+                                                             v_rms[pix] );
+        
+        self.vmaxprob = vmaxprob
 
 
-        def run( self ):
-            """ run the analysis
-            """
+    def run( self ):
+        """ run the analysis
+        """
+        
+        for step in range( self.nsteps - 1 ):
+                
+            print step, 'processing events: ', self.evranges[ step ], 'to ',
+            print self.evranges[ step+1 ]
             
-            pixhist = hist_array( rd.NPIX, 400, -99.5, 100.5,
-                                  'bsl_pix', 'Pixel Baseline',
-                                  'sample value in mV', 'Entries / 0.5 mV' )  
+            pixstephist = hist_array( self.npix, 400, -99.5, 100.5,
+                                      'bsl_pix_step', 'Pixel Baseline',
+                                      'sample value in mV', 'Entries / 0.5 mV' )
+                
+            self.loopEvents( self.evranges[step], self.evranges[step+1],
+                             pixstephist )
 
-            for step in range( self.nsteps ):
-                
-                print self.step, 'processing events: ', self.evranges[ self.step], 'to ',
-                      self.evranges[self.step+1]
+            self.pixhist.y[:,:] += pixstephist.y[:,:] 
+            pixstephist.SetBinContent()
             
-                pixstephist = hist_array( rd.NPIX, 400, -99.5, 100.5,
-                                          'bsl_pix', 'Pixel Baseline',
-                                          'sample value in mV', 'Entries / 0.5 mV' )
-                
-                self.loopEvents( evranges[step], evranges[step+1], pixstephist )
+            bslSummaryHistos = bslHistograms( 'bsl_sum_step' )
+            self.bslMeanValues[step,:] = self.loopPixel( self.npix, pixstephist )
+            # print self.bslMeanValues
 
-                pixhist.y[:,:] += pixstephist.y[:,:] 
-                pixstephist.SetBinContent()
-            
-                bslSummaryHistos = bslHistograms( 'bsl_sum' )
-                self.bslMeanValues[step,:] = loopPixel( rd.NPIX, pixstephist )
-                # print self.bslMeanValues
-                
-                fname = 'h'+ str( evranges[step] ) + '_to_'+ str( evranges[step+1] ) + '.root'
-                pyfact.SaveHistograms( [pixstephist, bslSummaryHistos], fname )
+            fname = 'h'+ str( self.evranges[step] ) + '_to_'+ str( self.evranges[step+1] ) + '.root'
+            SaveHistograms( [pixstephist, bslSummaryHistos], fname )
     
-                del pixstephist
-                del bslSummaryHistos
+            del pixstephist
+            del bslSummaryHistos
 
-            pixhist.SetBinContent()
+        self.pixhist.SetBinContent()
 
-            bslSummaryHistos = bslHistograms( 'bsl_sum' )    
-            loopPixel( rd.NPIX, pixhist )
+        self.loopPixel( self.rd.NPIX, self.pixhist )
 
-            self.save()
-            # fname = 'hbsl.root'
 
-        def save( self ):
-            """ save data after completion of analysis
-            """
-            pyfact.SaveHistograms( [pixhist, bslSummaryHistos], resFileName + '_bsl.root' )
+    def save( self ):
+        """ save baseline values and histograms
+        """
+        SaveHistograms( [ self.pixhist, self.bslSummaryHistos],
+                               self.rfiles.names['results'] + '_bsl.root' )
+        
+        np.savetxt( self.rfiles.names['results'] + '_bls.txt', self.bslMeanValues,
+                    fmt = '%8.3f' )
+        np.save( self.rfiles.names['results'] + '_bsl.npy', self.bslMeanValues )
+        
 
-            np.savetxt( resFileName + '_bls.dat', self.bslMeanValues, fmt = '%8.3f' )
-            np.save( resFileName + '_bsl.npy', self.bslMeanValues )
+if __name__ == '__main__':
+    """ create an instance for test purposes
+
+    """
+    runfiles = pyfact.fnames()
+
+    runfiles.info()
+
+    bcal = baseline_ana( rfiles = runfiles, step = 14,
+                         bsl_corr = False, max_events = 60 ) 
