Index: drsdaq/HVFeedback.cc
===================================================================
--- drsdaq/HVFeedback.cc	(revision 92)
+++ drsdaq/HVFeedback.cc	(revision 99)
@@ -16,5 +16,4 @@
 #include <signal.h>
 
-#define MAX_RETRY 5
 #define PIXMAP_LOCATION "../config/PixelMap.txt"
 
@@ -64,4 +63,5 @@
     ReadCard("MaxCmdAckDelay",      &fMaxCmdAckDelay,    'I', File);
     ReadCard("DefaultResponse",      Response,      	 'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels);
+    ReadCard("DefaultTarget",        Target,      	 'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels);
     fclose(File);
   }
@@ -115,5 +115,5 @@
   
   // Check for LED trigger channel on given channel and if feedback running
-  if (FBMode==FB_Off || m->WaveForm[fLedTrigBoard][fLedTrigChip][fLedTrigChannel][fLedTrigSample] < fLedTrigThreshold)
+  if (FBMode==FB_Off || m->WaveForm[fLedTrigBoard][fLedTrigChip][fLedTrigChannel][(fLedTrigSample+m->TriggerCell[fLedTrigBoard][fLedTrigChip])%kNumberOfBins] < fLedTrigThreshold)
     return false;
   
@@ -123,5 +123,5 @@
       for (k=0; k<kNumberOfChannels; k++) {
 	for (Integral=0, q=-fIntHalfWidth; q<=(int) fIntHalfWidth; q++) 
-          Integral += (m->WaveForm[i][j][k][fLedSignalSample+q] - m->WaveForm[i][j][k][fLedBaselineSample+q])*m->BStruct[i].ScaleFactor;
+          Integral += (m->WaveForm[i][j][k][(fLedSignalSample+q+m->TriggerCell[i][j])%kNumberOfBins] - m->WaveForm[i][j][k][(fLedBaselineSample+q+m->TriggerCell[i][j])%kNumberOfBins])*m->BStruct[i].ScaleFactor;
         Integral /= 2*fIntHalfWidth+1;
 	Average[i][j][k] += Integral;
@@ -129,7 +129,8 @@
       }
 
+  // Check if acquired number of event requires action
   if (++Count<NumAverages) return false;
 
-  // Acquired number of event requires action
+  // Make entry in slow data file
   switch (FBMode) {
     case FB_Active: SlowDataClass->NewEntry("Average");
@@ -143,4 +144,5 @@
   }			
 
+  // Feedback action
   for (i=m->FirstBoard; i<=m->LastBoard; i++) {
     for (j=0; j<kNumberOfChips; j++) {
@@ -153,4 +155,5 @@
 	    EffectiveGain = Gain*pow(0.5*(1+(Difference/Sigma[i][j][k]-1)/(Difference/Sigma[i][j][k]+1)),2);
 	    Correction = -Difference*Response[i][j][k]*EffectiveGain;
+    	    if (fabs(Correction) > 0.1) Correction = fabs(Correction)/Correction*0.1;   // Limit changes to 100 mV
             if(Correction!=0 && Target[i][j][k]!=0 && !PixMap->DRS_to_Pixel(i,j,k).empty()) {
               printf("Average of board %d, chip %d, channel %d is %.2f +/- %.2f    Correction %.3f\n",i,j,k,Average[i][j][k],Sigma[i][j][k],Correction);
@@ -261,5 +264,5 @@
 
 //
-// Set feedback mode and clear averages
+// Get feedback mode
 //
 FBState HVFeedback::GetFBMode() {
@@ -295,7 +298,9 @@
 void HVFeedback::GetTargets() {
   for (int i=m->FirstBoard; i<=m->LastBoard; i++)
-    for (int j=0; j<kNumberOfChips; j++)
-      for (int k=0; k<kNumberOfChannels; k++)
-         m->PrintMessage("Board %d, chip %d, channel %d: %.2f\n",i,j,k,Target[i][j][k]);
+    for (int j=0; j<kNumberOfChips; j++) {
+      m->PrintMessage("Board %d, chip %d:",i,j);
+      for (int k=0; k<kNumberOfChannels; k++) m->PrintMessage(" %.2f",Target[i][j][k]);
+      m->PrintMessage("\n\r");
+    }
 }
 
@@ -305,17 +310,20 @@
 void HVFeedback::MeasureResponse(float U) {
 
-  if (U==0) m->PrintMessage("HV Feedback: Error, voltage difference must not non-zero.\n");
-  else {
-    for (int i=m->FirstBoard; i<=m->LastBoard; i++) 
-      for (int j=0; j<kNumberOfChips; j++) 
-        for (int k=0; k<kNumberOfChannels-2; k++) {
-	  if(PixMap->DRS_to_Pixel(i,j,k).empty()) m->PrintMessage("Could not find pixel ID of board %d, chip %d, channel %d\n",i,j,k);
-          else WriteHVCommand("hvdiff %s %f\n",PixMap->DRS_to_Pixel(i,j,k).c_str(), -U/2);
-        }
-    DiffVoltage = U;
-    FBMode = FB_ResponseFirst;
-    ClearAverages();  
-    m->PrintMessage("HV Feedback: Decreasing voltages by %f for response measurement, acquiring data.\n",DiffVoltage/2);
-  }
+  if (U==0) {
+    m->PrintMessage("HV Feedback: Error, voltage difference must not non-zero.\n");
+    return;
+  }
+
+  for (int i=m->FirstBoard; i<=m->LastBoard; i++) 
+    for (int j=0; j<kNumberOfChips; j++) 
+      for (int k=0; k<kNumberOfChannels; k++) {
+	if(!PixMap->DRS_to_Pixel(i,j,k).empty()) {
+          WriteHVCommand("hvdiff %s %f\n",PixMap->DRS_to_Pixel(i,j,k).c_str(), -U/2);
+    	}
+      }
+  DiffVoltage = U;
+  FBMode = FB_ResponseFirst;
+  ClearAverages();  
+  m->PrintMessage("HV Feedback: Decreasing voltages by %f for response measurement, acquiring data.\n",DiffVoltage/2);
 }
 
@@ -324,8 +332,11 @@
 //
 void HVFeedback::GetResponse() {
-  for (int i=m->FirstBoard; i<=m->LastBoard; i++)
-    for (int j=0; j<kNumberOfChips; j++)
-      for (int k=0; k<kNumberOfChannels; k++)
-         m->PrintMessage("Board %d, chip %d, channel %d: %.3f\n",i,j,k,Response[i][j][k]);
+  for (int i=m->FirstBoard; i<=m->LastBoard; i++) {
+    for (int j=0; j<kNumberOfChips; j++) {
+      m->PrintMessage("Board %d, chip %d:",i,j);
+      for (int k=0; k<kNumberOfChannels; k++) m->PrintMessage(" %.3f",Response[i][j][k]);
+      m->PrintMessage("\n\r");
+    }
+  }
 }
 
@@ -336,34 +347,33 @@
   char Textbuffer[MAX_COM_SIZE];
   fd_set SelectDescriptor;
-  int RetryCount=0;
-  
-  do {
-    va_list ArgumentPointer;  va_start(ArgumentPointer, Format); 
-    vsnprintf(Textbuffer, sizeof(Textbuffer), Format, ArgumentPointer);
-
-    // Write command to socket
-    if(write(SocketDescriptor, Textbuffer, strlen(Textbuffer)+1)!=(int) strlen(Textbuffer)+1) {
-      m->PrintMessage("Error: Could not write (entire) command to HV socket (%s)\n", strerror(errno));
-      return false;
-    }
-
-    // Wait for command acknowledge from hvcontrol program
-    FD_ZERO(&SelectDescriptor);   FD_SET(SocketDescriptor, &SelectDescriptor);
-    struct timeval WaitTime = {fMaxCmdAckDelay, 0};
-    if (select(((int) SocketDescriptor)+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
-      m->PrintMessage("Error with select() in command acknowledge (%s)\n", strerror(errno));
-      return false;
-    }
-    if (!FD_ISSET(SocketDescriptor, &SelectDescriptor)) { // Time-out expired
-      m->PrintMessage("Time-out of %d seconds expired before receiving acknowledge from HV socket.\n", fMaxCmdAckDelay);
-      return false;
-    }
-    if (read(SocketDescriptor, Textbuffer, MAX_COM_SIZE) == -1) {
-      m->PrintMessage("Error reading acknowledge from HV socket (%s)\n", strerror(errno));
-      return false;
-    }
-  } while (strstr(Textbuffer,"WC ok")==NULL && ++RetryCount<=MAX_RETRY);
-  if(RetryCount==MAX_RETRY) {
-    m->PrintMessage("Could not set high voltage after %d tries.\n", MAX_RETRY);
+  
+  va_list ArgumentPointer;  va_start(ArgumentPointer, Format); 
+  vsnprintf(Textbuffer, sizeof(Textbuffer), Format, ArgumentPointer);
+
+  // Write command to socket
+  if(write(SocketDescriptor, Textbuffer, strlen(Textbuffer)+1)!=(int) strlen(Textbuffer)+1) {
+    m->PrintMessage("Error: Could not write (entire) command to HV socket (%s)\n", strerror(errno));
+    return false;
+  }
+
+  // Wait for command acknowledge from hvcontrol program
+  FD_ZERO(&SelectDescriptor);   FD_SET(SocketDescriptor, &SelectDescriptor);
+  struct timeval WaitTime = {fMaxCmdAckDelay, 0};
+  if (select(((int) SocketDescriptor)+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
+    m->PrintMessage("Error with select() in command acknowledge (%s)\n", strerror(errno));
+    return false;
+  }
+  
+  // Evaluate response
+  if (!FD_ISSET(SocketDescriptor, &SelectDescriptor)) { // Time-out
+    m->PrintMessage("Time-out of %d seconds expired before receiving acknowledge from HV socket.\n", fMaxCmdAckDelay);
+    return false;
+  }
+  if (read(SocketDescriptor, Textbuffer, MAX_COM_SIZE) == -1) { // Could not read
+    m->PrintMessage("Error reading acknowledge from HV socket (%s)\n", strerror(errno));
+    return false;
+  }
+  if (strncmp(Textbuffer, "OK", 2) != 0) { // ERROR response
+    m->PrintMessage("Did not received OK from hvcontrol.\n");
     return false;
   }
