Index: /trunk/FACT++/src/feedback.cc
===================================================================
--- /trunk/FACT++/src/feedback.cc	(revision 17646)
+++ /trunk/FACT++/src/feedback.cc	(revision 17647)
@@ -10,5 +10,4 @@
 #include "Console.h"
 #include "externals/PixelMap.h"
-#include "externals/Interpolator2D.h"
 
 #include "tools.h"
@@ -491,4 +490,7 @@
         */
 
+        double UdrpAvg = 0;
+        double UdrpRms = 0;
+
         for (int i=0; i<320/*BIAS::kNumChannels*/; i++)
         {
@@ -526,11 +528,13 @@
 
             // This is assuming that the broken pixels have a 390 Ohm instead of 3900 Ohm serial resistor
-            if (i==66)                 // Pixel 830(66)
-                R5 = 300;              // 2400 = 1/(3/3900 + 1/390)
-            if (i==191 || i==193)      // Pixel 583(191) / Pixel 1401(193)
-                R5 = 390/1.4;          // 379 = 1/(4/3900 + 1/390)
+            if (i==66 || i==193)               // Pixel 830(66) / Pixel 583(191)
+                R5 = 1./((N-1)/3900.+1/1000.);
+            if (i==191)                        // Pixel 1399(193)
+                R5 = 1./((N-1)/3900.+1/390.);
+            if (i==17 || i==206)               // dead pixel 923(80) / dead pixel 424(927)
+                R5 = 3900./(N-1);              // cannot identify third dead pixel in light-pulser data
 
             // The measurement resistor
-            const double R8 = 100;
+            const double R8 = 0;
 
             // Total resistance of branch with diodes (R4+R5)
@@ -543,6 +547,6 @@
             // we get:
             //
-            // I[R=3900] =  Iout *      1/(10+(N-1))   = Iout        /(N+9)
-            // I[R= 390] =  Iout * (1 - 1/ (10+(N-1))) = Iout * (N+8)/(N+9)
+            // I[R=3900] =  Iout *      1/(10+(N-1))  = Iout        /(N+9)
+            // I[R= 390] =  Iout * (1 - 1/(10+(N-1))) = Iout * (N+8)/(N+9)
             //
             // I[R=390] / I[R=3900] = N+8
@@ -551,5 +555,7 @@
 
             // Voltage drop in R4/R5 branch (for the G-APDs with correct resistor)
-            const double Udrp = R*Iout;
+            // The voltage drop should not be <0, otherwise an unphysical value
+            // would be amplified when Uset is calculated.
+            const double Udrp = Iout<0 ? 0 : R*Iout;
 
             // Nominal breakdown voltage with correction for temperature dependence
@@ -558,5 +564,5 @@
             // Current overvoltage (at a G-APD with the correct 3900 Ohm resistor)
             //const double Uov = U9-Udrp-Ubd>0 ? U9-Udrp-Ubd : 0;
-            const double Uov = U9-Udrp-Ubd>-0.34 ? U9-Udrp-Ubd : -0.34;
+            const double Uov = U9-Udrp-Ubd>-0.44/*-0.34*/ ? U9-Udrp-Ubd : -0.44/*-0.34*/;
 
             // Iout linear with U9 above Ubd
@@ -581,4 +587,22 @@
             double Iapd = Iout/N;
 
+            // Rtot = Uapd/Iout
+            // Ich  = Uapd/Rch = (Rtot*Iout) / Rch = Rtot/Rch * Iout
+            //
+            // Rtot = 3900/N
+            // Rch  = 3900
+            //
+            // Rtot = 1./((N-1)/3900 + 1/X)       X=390 or X=1000
+            // Rch  = 3900
+            //
+            // Rtot/Rch =   1/((N-1)/3900 + 1/X)/3900
+            // Rtot/Rch =   1/( [ X*(N-1) + 3900 ] / [ 3900 * X ])/3900
+            // Rtot/Rch =   X/( [ X*(N-1)/3900 + 1 ] )/3900
+            // Rtot/Rch =   X/( [ X*(N-1) + 3900 ] )
+            // Rtot/Rch =   1/( [ (N-1) + 3900/X ] )
+            //
+            // Rtot/Rch[390Ohm]  =  1/( [ N + 9.0 ] )
+            // Rtot/Rch[1000Ohm] =  1/( [ N + 2.9 ] )
+            //
             // In this and the previosu case we neglect the resistance of the G-APDs, but we can make an
             // assumption: The differential resistance depends more on the NSB than on the PDE,
@@ -588,6 +612,18 @@
             // resistor is large enough that the increase of the overvoltage does not dramatically
             // increase the current flow as compared to the total current flow.
-            if (i==66 || i==191 || i==193)
-                Iapd = Iout/(N+9); // Iapd = R5*Iout/3900;
+            if (i==66 || i==193)           // Iout/13 15.8   / Iout/14  16.8
+                Iapd = Iout/(N+2.9);
+            if (i==191)                    // Iout/7.9  38.3
+                Iapd = Iout/(N+9);
+            if (i==17 || i==206)
+                Iapd = Iout/(N-1);
+
+
+            // 3900   +   Rapd  = I0   ->  Uapd = Utot - 3900*I0
+            // 3900   +   Rapd  = I0   ->  Uapd = Utot - 3900*I0
+            // 3900   +   Rapd  = I0   ->  Uapd = Utot - 3900*I0
+            // 390    +   Rx    = Ix   ->  Uapd = Utot -  390*Ix
+
+            // Iout = N*I0 + Ix
 
             // The differential resistance of the G-APD, i.e. the dependence of the
@@ -600,7 +636,26 @@
             //const double Uset = Ubd + overvoltage + R*Iov*N;
             //const double Uset = Uov<0.3 ? Ubd + overvoltage + Udrp : Ubd + overvoltage + Udrp*pow(overvoltage/Uov, 1.66);
-            const double Uset = Uov<0 ?
-                Ubd + overvoltage + Udrp*pow(overvoltage/0.34+1, 1.66) :
-                Ubd + overvoltage + Udrp*pow((overvoltage+0.34)/(Uov+0.34), 1.66);
+            const double Uset = 
+
+                // Uov<0 ?
+                //   Ubd + overvoltage + Udrp*pow(overvoltage/0.44/*0.34*/+1, 1.85/*1.66*/) :
+                //   Ubd + overvoltage + Udrp*pow((overvoltage+0.44/*0.34*/)/(Uov+0.44/*0.34*/), 1.85/*1.66*/);
+
+                // Uov<0 ?
+                //   Ubd + overvoltage + Udrp*pow(overvoltage+0.44, 1.3213+0.2475*(overvoltage+0.44)) :
+                //   Ubd + overvoltage + Udrp*pow(overvoltage+0.44, 1.3213+0.2475*(overvoltage+0.44))/pow(Uov+0.44, 1.3213+0.2475*(Uov+0.44));
+
+                // This estimation is based on the linear increase of the
+                // gain with voltage and the increase of the crosstalk with
+                // voltage, as measured with the overvoltage-tests (OVTEST)
+
+                Uov+0.44<0.022 ?
+            Ubd + overvoltage + Udrp*
+                exp(0.6*(overvoltage-Uov))*pow((overvoltage+0.44), 0.6) :
+            Ubd + overvoltage + Udrp*
+                exp(0.6*(overvoltage-Uov))*pow((overvoltage+0.44)/(Uov+0.44), 0.6);
+
+
+
 
             if (fabs(overvoltage-Uov)>0.033)
@@ -643,4 +698,7 @@
 
                 med[2][num[2]++] = iapd;
+
+                UdrpAvg += Udrp;
+                UdrpRms += Udrp*Udrp;
             }
         }
@@ -655,6 +713,16 @@
                                          vec.data(), BIAS::kNumChannels*sizeof(float));
 
+                UdrpAvg /= 320;
+                UdrpRms /= 320;
+                UdrpRms -= UdrpAvg*UdrpAvg;
+                UdrpRms  = UdrpRms<0 ? 0 : sqrt(UdrpRms);
+
                 ostringstream msg;
-                msg << setprecision(4) << "Sending new absolute offset: dU(" << fTemp << "degC)=" << fTempOffsetAvg << "V+-" << fTempOffsetRms << ", Unom=" << overvoltage << "V, Uov=" << (num[0]+num[1]>0?(avg[0]+avg[1])/(num[0]+num[1]):0) << " [N=" << Ndev[0] << "/" << Ndev[1] << "/" << Ndev[2] << "]";
+                msg << fixed;
+                msg << setprecision(2) << "Sending U: dU(" << fTemp << "degC)="
+                    << setprecision(3) << fTempOffsetAvg << "V+-" << fTempOffsetRms << "  Udrp="
+                    << UdrpAvg << "V+-" << UdrpRms;
+                msg.unsetf(ios_base::floatfield);
+                msg << " Unom=" << overvoltage << "V Uov=" << (num[0]+num[1]>0?(avg[0]+avg[1])/(num[0]+num[1]):0) << " [N=" << Ndev[0] << "/" << Ndev[1] << "/" << Ndev[2] << "]";
                 Info(msg);
             }
