Index: /drsdaq/DAQReadout.cc
===================================================================
--- /drsdaq/DAQReadout.cc	(revision 98)
+++ /drsdaq/DAQReadout.cc	(revision 99)
@@ -25,6 +25,6 @@
   {{"board", &DAQReadout::cmd_board, true, "<i> [j] | <all>" ,"Address board i, boards i-j, all boards"},
    {"status", &DAQReadout::cmd_status, false, "[daq|drs]", "Show DAQ/DRS status information"},
-   {"freq", &DAQReadout::cmd_freq, true, "<GHz> [reg]", "Set DRS sampling frequency (regulated)"},
-   {"calib", &DAQReadout::cmd_calib, true, "<t_f> <c_f>", "Response calibration"},
+   {"frequency", &DAQReadout::cmd_freq, true, "<GHz> [reg]", "Set DRS sampling frequency (regulated)"},
+   {"calib", &DAQReadout::cmd_calib, true, "<trig rate> ", "Response calibration"},
    {"trigger", &DAQReadout::cmd_trigger, true, "<on|off>", "Hardware trigger on or off"},
    {"delayed", &DAQReadout::cmd_delayed, true, "<on|off>", "Switch delayed start on or off"},
@@ -220,5 +220,5 @@
 // Print current number of events
 void DAQReadout::cmd_events() {
-  if(daq_state != active) PrintMessage("DAQ not active.\n");
+  if(daq_state != active) PrintMessage("DAQ not active\n");
   else PrintMessage("Current number of events: %d (of %d requested)\n", NumEvents, NumEventsRequested);
 } 
@@ -234,5 +234,5 @@
   if(!Match(Param[1],"test")) {
     if (daq_state==active || NumBoards==0) {
-      PrintMessage("DAQ is busy or no boards available.\n");
+      PrintMessage("DAQ is busy or no boards available\n");
       return;
     }
@@ -244,5 +244,5 @@
     
     if (!CalibrationRead && !ReadCalibration()) {
-      PrintMessage("Cannot start run if response calibration not read.\n");
+      PrintMessage("Cannot start run if response calibration not read\n");
       return;
     }
@@ -333,5 +333,5 @@
   else if (Match(Param[1], "blt64")) Type = 1;
   else {
-    PrintMessage("Unknown type for testing.\n");
+    PrintMessage("Unknown type for testing\n");
     return;
   }
@@ -448,6 +448,10 @@
   char str[MAX_COM_SIZE];
 
-  if (NParam!=3 || !atof(Param[1]) || !atof(Param[2])) {
+  if (NParam!=2 || !atof(Param[1])) {
     PrintUsage();
+    return;
+  }
+  if (!IsDRSFreqSet()) {
+    PrintMessage("Set sampling frequency first\n");
     return;
   }
@@ -455,28 +459,22 @@
   getcwd(str, sizeof(str));
   strcat(str,"/calib");
-  PrintMessage("Writing calibration data to directory '%s'\n",str);
 
   for (int i=FirstBoard; i<=LastBoard; i++) {
-    drs->GetBoard(i)->Init();
-    drs->GetBoard(i)->SetFrequency(atof(Param[2]));
-    drs->GetBoard(i)->SoftTrigger();
-
-    PrintMessage("Creating calibration of board %d (%d)\n", i, drs->GetBoard(i)->GetCMCSerialNumber());
+    PrintMessage("Creating calibration of board %d (serial number %d)\n", i, drs->GetBoard(i)->GetCMCSerialNumber());
 
     drs->GetBoard(i)->EnableTcal(1);
-    PrintMessage("Tcal enabled\n");
-
     if (drs->GetBoard(i)->GetChipVersion() == 3) drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,21,0,20,0,0,0,0,0);
     else drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,36,110,20,19,40,15,atof(Param[1]),0);
     drs->GetBoard(i)->SetCalibrationDirectory(str);
 
-    for (int j=0; j<2; j++) {
+    for (int j=0; j<kNumberOfChips; j++) {
       drs->GetBoard(i)->GetResponseCalibration()->ResetCalibration();
-      PrintMessage("Calibration reset done.\n");
 
       while (!drs->GetBoard(i)->GetResponseCalibration()->RecordCalibrationPoints(j)) {}
-      PrintMessage("Record calibration points done.\n");
+      PrintMessage("Calibration points recorded.\n");
+
       while (!drs->GetBoard(i)->GetResponseCalibration()->FitCalibrationPoints(j)) {}
       PrintMessage("Calibration points fitted.\n");
+
       while (!drs->GetBoard(i)->GetResponseCalibration()->OffsetCalibration(j)) {}
       PrintMessage("Offset calibration done.\n");
@@ -484,6 +482,7 @@
       if (!drs->GetBoard(i)->GetResponseCalibration()->WriteCalibration(j)) break;
     }        
-    drs->GetBoard(i)->Init(); // Reset linear range -0.2 ... 0.8 V
   } // Loop over boards
+  PrintMessage("End of calibration\n");
+  CalibrationRead = false;
 }
 
@@ -737,17 +736,4 @@
 }
 
-// Check if calibration file has been read
-bool DAQReadout::IsCalibrationRead() {
-
-  for (int i=FirstBoard; i<=LastBoard; i++) {
-    for (int Chip=0; Chip<kNumberOfChips; Chip++) 
-      if (!(drs->GetBoard(i)->GetResponseCalibration()->IsRead(Chip))) {
-	PrintMessage("Warning: Response calibration of board %d chip %d not yet read!\n",i,Chip);
-	return false;
-      }
-  }
-  return true;
-}
-
 // Stop DAQ
 void DAQReadout::StopRun() {
Index: /drsdaq/DAQReadout.h
===================================================================
--- /drsdaq/DAQReadout.h	(revision 98)
+++ /drsdaq/DAQReadout.h	(revision 99)
@@ -123,5 +123,4 @@
     void HWTrigger(int); 
     bool ReadCalibration();
-    bool IsCalibrationRead();
     void ReadCalibratedDRSData();
     void PrintConfig(int);
@@ -136,4 +135,5 @@
 
 void DAQ(DAQReadout *);
+
 bool Match(const char*, const char*);
 int ParseInput(char*, const char *Param[]);
Index: /drsdaq/HVFeedback.cc
===================================================================
--- /drsdaq/HVFeedback.cc	(revision 98)
+++ /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;
   }
Index: /drsdaq/History.txt
===================================================================
--- /drsdaq/History.txt	(revision 98)
+++ /drsdaq/History.txt	(revision 99)
@@ -48,2 +48,6 @@
     	    configuration parameters are now mandatory: if one is not found, the
 	    program terminates.
+5/8/2009    Streamlined DRS response calibration (removing unnecessary
+    	    initialisation steps)
+10/8/2009   Socket interface to hvcontrol only checks, if first reponse starts with
+    	    'OK', otherwise assumes command to hvcontrol failed. 
