Index: hvcontrol/src/HVCalib.cc
===================================================================
--- hvcontrol/src/HVCalib.cc	(revision 90)
+++ hvcontrol/src/HVCalib.cc	(revision 100)
@@ -7,5 +7,5 @@
                 sabrina.stark@phys.ethz.ch
 
-  Contents:     Class reading the calibration tabel for DAC to HV values
+  Contents:     Class reading the calibration table for DAC to HV values
 
 \*************************************************************************/
@@ -24,41 +24,33 @@
 HVCalib::HVCalib(HVConfig* hvConfig) {
 
-  char calibfile[80];
-  int idac, idacOld;
-  int istep, ihv1000, ihvOld;
-  char dLine[6000];
+  char calibfile[80], dLine[6000];
+  int idac, idacOld, istep, ihv1000, ihvOld;
   bool first = true;
   float hv1000,dac;
   double intpart;
 
-  iDACMin = hvConfig->DACMin;
-  fHVCalibSlope = hvConfig->fHVCalibSlope;
-  NDACValues = hvConfig->DACMax - hvConfig->DACMin;
+  Config = hvConfig;
+  
+  iDACMin = Config->DACMin;
+  fHVCalibSlope = Config->fHVCalibSlope;
+  NDACValues = Config->DACMax - Config->DACMin;
   NHVValues = 10*NDACValues;
 
-
-  HVArray = new double***[hvConfig->NumHVBoards];
-  for(int i=0; i<hvConfig->NumHVBoards; i++) {
-    HVArray[i] = new double**[NUM_CHAINS];
+  HVArray = new double*** [Config->NumHVBoards];
+  DACArray = new int*** [Config->NumHVBoards];
+  for(int i=0; i<Config->NumHVBoards; i++) {
+    HVArray[i] = new double** [NUM_CHAINS];
+    DACArray[i] = new int** [NUM_CHAINS];
     for(int j=0; j<NUM_CHAINS; j++) {
-      HVArray[i][j] = new double*[NUM_CHANNELS];
+      HVArray[i][j] = new double* [NUM_CHANNELS];
+      DACArray[i][j] = new int* [NUM_CHANNELS];
       for(int k=0; k<NUM_CHANNELS; k++) {
-	HVArray[i][j][k] = new double[NDACValues];
+	HVArray[i][j][k] = new double [NDACValues];
+	DACArray[i][j][k] = new int [NHVValues];
       }
     }
   }
 
-  DACArray = new int***[hvConfig->NumHVBoards];
-  for(int i=0; i<hvConfig->NumHVBoards; i++) {
-    DACArray[i] = new int**[NUM_CHAINS];
-    for(int j=0; j<NUM_CHAINS; j++) {
-      DACArray[i][j] = new int*[NUM_CHANNELS];
-      for(int k=0; k<NUM_CHANNELS; k++) {
-	DACArray[i][j][k] = new int[NHVValues];
-      }
-    }
-  }
-
-  for(int i=0; i<hvConfig->NumHVBoards; i++){
+  for(int i=0; i<Config->NumHVBoards; i++){
     for(int j=0; j<NUM_CHAINS; j++){
       sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fUSBDevice[i],65+j,1); 
@@ -68,5 +60,5 @@
 	fin >> dac;
 	idacOld = 0;
-	if ((int) dac >= hvConfig->DACMin) {
+	if ((int) dac >= Config->DACMin) {
 	  if (first){
 	    iDACMin = (int) dac;
@@ -76,5 +68,8 @@
 	  istep = idac - idacOld;
 	  for(int k=0; k<NUM_CHANNELS; k++){
+ 	    if (idac>=NDACValues) break;
+
 	    fin>>HVArray[i][j][k][idac];
+
 	    for (int l=1; l<istep;l++){
 	      HVArray[i][j][k][idac-istep+l] = HVArray[i][j][k][idac-istep] + l*(HVArray[i][j][k][idac]-HVArray[i][j][k][idac-istep])/istep;
@@ -93,10 +88,10 @@
   }
 
-  for(int i=0; i<hvConfig->NumHVBoards; i++){
-    for(int j=0;j<NUM_CHAINS;j++){
-      for(int k=0;k<NUM_CHANNELS;k++){
+  for(int i=0; i<Config->NumHVBoards; i++){
+    for(int j=0; j<NUM_CHAINS; j++){
+      for(int k=0; k<NUM_CHANNELS; k++){
 	ihvOld = 0;
 	first = true;
-	for (int l=0; l<NDACValues;l++){
+	for (int l=0; l<NDACValues; l++){
 	  if (l == 0){
 	    ihvOld = 0;
@@ -129,22 +124,18 @@
 HVCalib::~HVCalib() {
 
-
-  for (int i=0; i<MAX_NUM_HVBOARDS; i++){
-    for (int j=0; j<NUM_CHAINS;j++){
-      for (int k=0; k<NUM_CHANNELS;k++){
-	delete [] HVArray[i][j][k];
-	delete [] HVArray;
+  for(int i=0; i<Config->NumHVBoards; i++) {
+    for(int j=0; j<NUM_CHAINS; j++) {
+      for(int k=0; k<NUM_CHANNELS; k++) {
+	delete[] HVArray[i][j][k];
+	delete[] DACArray[i][j][k];
       }
+      delete[] HVArray[i][j];
+      delete[] DACArray[i][j];
     }
+    delete[] HVArray[i];
+    delete[] DACArray[i];
   }
-
-  for (int i=0; i<MAX_NUM_HVBOARDS; i++){
-    for (int j=0; j<NUM_CHAINS;j++){
-      for (int k=0; k<NUM_CHANNELS;k++){
-	delete [] DACArray[i][j][k];
-	delete [] DACArray;
-      }
-    }
-  }
+  delete[] HVArray;
+  delete[] DACArray;
 }
 
@@ -175,6 +166,6 @@
       ihv = (int) (hv1000<0 ? ceil(hv1000) : floor(hv1000));
     }
+    if (ihv >= NHVValues) ihv = NHVValues;
     return DACArray[board][chain][channel][ihv];
   }
 }
-
