Index: fact/tools/pyscripts/sandbox/dneise/gain/gain.py
===================================================================
--- fact/tools/pyscripts/sandbox/dneise/gain/gain.py	(revision 13440)
+++ fact/tools/pyscripts/sandbox/dneise/gain/gain.py	(revision 13442)
@@ -26,8 +26,12 @@
 thr = 3
 filter_delay = 3
+search_window_size = 12
+# shortcut
+sws = search_window_size
 
 plt.ion()
 fig = plt.figure()
 fig.hold(True)
+
 
 
@@ -41,25 +45,31 @@
     filtered = smooth(filtered)
 
+    # this is a loop over all pixel of this event
+    
     for dat, fil, orig in zip(data, filtered, data_orig):
         plt.cla()
         prod = fil[:-1] * fil[1:]
         cand = np.where( prod <= 0)[0]
+        if len(cand) == 0:
+            continue
         # zero crossing with rising edge
         cross = cand[np.where(fil[cand] < 0)[0]]
-        
+        if len(cross) == 0:
+            continue
         over_thr = cross[np.where(dat[cross-4] > thr)[0]]
 
-        # Now we have these values, we will throw away all those,
+        # Now since we have these values, we will throw away all those,
         # which are probably on a falling edge of its predecessor
-        
-        
-        over = []
         dover = np.diff(over_thr)
-        
-        for i in range(len(over_thr)):
-            if dover[i] > 100:
-                
-                
-        over_thr = np.array(over)
+        if len(dover) == 0:
+            good = over_thr
+        else:
+            good = []
+            good.append(over_thr[0])
+            for i in range(len(dover)):
+                if dover[-i-1] > 100:
+                    good.append(over_thr[-i-1])
+            
+            good = np.array(sorted(good))
         # these positions, we just found, do not exactly point to the maximum
         # of a peak, but the peak will be on the left side of it.
@@ -68,15 +78,15 @@
         # the smoothed data and the original data.
         
-        max_pos      = np.zeros( over_thr.shape )
-        max_smoothed = np.zeros( over_thr.shape )
-        max_orig     = np.zeros( over_thr.shape )
+        max_pos      = np.zeros( good.shape )
+        max_smoothed = np.zeros( good.shape )
+        max_orig     = np.zeros( good.shape )
     
-        for i in range(len(over_thr)):
+        for i in range(len(good)):
             # We search for a local maximum in a window of size 12
-            if len(dat[over_thr[i]-12:over_thr[i]]) > 0:
+            if len(dat[good[i]-sws:good[i]]) > 0:
             
-                max_pos[i]       = over_thr[i]-12 + np.argmax(dat[over_thr[i]-12:over_thr[i]])
+                max_pos[i]       = good[i]-sws + np.argmax(dat[good[i]-sws:good[i]])
                 max_smoothed[i]  = dat[max_pos[i]]
-                max_orig[i]       = orig[max_pos[i]-filter_delay]
+                max_orig[i]      = orig[max_pos[i]-filter_delay]
 
         plt.plot(max_pos, max_smoothed, 'ro')
