Index: /fact/FADctrl/FAD.cc
===================================================================
--- /fact/FADctrl/FAD.cc	(revision 10225)
+++ /fact/FADctrl/FAD.cc	(revision 10226)
@@ -542,5 +542,5 @@
 	while (fread(&Data, sizeof(Data), 1, File) == 1) {
 	  for (unsigned int i=0; i<Boards.size(); i++) if (Data.DNA == Boards[i]->GetStatus().DNA) {
-	    PrintMessage("Found calibration data for board %d (%s)\n", i, ctime(&Data.Time));
+	    PrintMessage("Found calibration for board %d - %s", i, ctime(&Data.Time));
 	    Boards[i]->ACalib = Data;
 	  }
@@ -566,4 +566,7 @@
 //
 void FAD::cmd_status() {
+
+  int MinCount = std::numeric_limits<int>::max();
+  unsigned int SlowestBoard = 0;
 
   // ==== Print board overview ====
@@ -575,13 +578,16 @@
 	  if (Boards[i]->Active) Count++;
 	  if (!Boards[i]->CommOK) Error++;
+	  if (Boards[i]->Active && Boards[i]->Count < MinCount) {
+		MinCount = Boards[i]->Count;
+		SlowestBoard = i;
+	  }
 	}	  
 
-	PrintMessage("\rTotal boards: %d    (%d communication errors)\n", Boards.size(), Error);
+	PrintMessage("\rTotal boards: %d    (%d with communication errors)\n", Boards.size(), Error);
 
 	// Print list of active boards
 	PrintMessage("Active are %d boards(s)   ", Count);
 	for (unsigned int i=0; i<Boards.size(); i++) {
-	  if (Boards[i]->Active) PrintMessage(" %d", i);
-	  if (!Boards[i]->Active) PrintMessage("!");
+	  if (Boards[i]->Active) PrintMessage(" %d%s", i, Boards[i]->CommOK ? "":"!");
 	}
 	PrintMessage("\n");
@@ -589,5 +595,5 @@
 	// Current mode
 	if (Mode == idle) PrintMessage("Current mode is IDLE\n");
-	else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events)\n", NumEventsRequested);
+	else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events, slowest board %d has %d events)\n", NumEventsRequested, SlowestBoard, MinCount);
 	else if (Mode == datarun) PrintMessage("Current mode is DATARUN (%d events requested, %d events taken)\n", NumEventsRequested, NumEvents);
 
@@ -623,9 +629,9 @@
 	// Board identification
 	PrintMessage("Board ID %.4x      Firmware revision %.4x      Serial %llx\n", S.BoardID, S.FirmwareRevision, S.DNA);
-	PrintMessage("Board time %gs       Event counter %d\n", S.BoardTime/1.0e4 , S.EventCounter);
-
-	// Slow data
+	PrintMessage("Board time %g s       Event counter %d\n", S.BoardTime/1.0e4 , S.EventCounter);
+
+	// Other data
 	PrintMessage("Frequency %.2f GHz    Phase shift %d    PLL lock %d %d %d %d\n", S.Frequency, S.PhaseShift, S.Lock[0], S.Lock[1], S.Lock[2], S.Lock[3]);
-	PrintMessage("DENABLE %d  DWRITE %d  SPI_clk %d DCM_lock: %d DCM_ready %d\n", S.denable, S.dwrite, S.spi_clk, S.DCM_lock, S.DCM_ready);	
+	PrintMessage("DENABLE %d   DWRITE %d   SPI_clk %d   DCM_lock %d   DCM_ready %d\n", S.denable, S.dwrite, S.spi_clk, S.DCM_lock, S.DCM_ready);	
 	PrintMessage("DAC %d %d %d %d   %d %d %d %d\n", S.DAC[0], S.DAC[1], S.DAC[2], S.DAC[3], S.DAC[4], S.DAC[5], S.DAC[6], S.DAC[7] );
 	PrintMessage("Temperature %.2f %.2f %.2f %.2f", S.Temp[0], S.Temp[1], S.Temp[2], S.Temp[3]);
@@ -636,14 +642,4 @@
 	}
 	PrintMessage("\n");
-	/*
-	if (GetBoard(i)->GetStatusReg() & BIT_RUNNING)
-	  PrintMessage("   Domino wave running\n");
-
-	if (GetBoard(i)->GetCtrlReg() & BIT_DMODE)
-	  PrintMessage("   DMODE circular\n");
-	else
-	  PrintMessage("   DMODE single shot\n");
- 
-    }*/
   } // for()
 }
@@ -811,6 +807,4 @@
 void FAD::SaveAmplitudeCalibration() {
 
-  PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
-  
   // Open calibration data file
   string Filename = string(getenv("HOME"))+"/FAD_ACal";
@@ -822,6 +816,8 @@
   }
   
-  // Write calibration information
-  for (unsigned int i=0; i<Boards.size(); i++) {
+  // Write valid calibration information for active boards
+  for (unsigned int i=0; i<Boards.size(); i++) {
+	if (!Boards[i]->Active || Boards[i]->ACalib.Time == -1) continue;
+	
 	if (fwrite(&Boards[i]->ACalib, sizeof(Boards[i]->ACalib), 1, File) != 1) {
 	  PrintMessage("Could not write to calibration file '%s'\n", Filename.c_str());
@@ -833,5 +829,5 @@
   if (fclose(File) != 0) PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
   
-  PrintMessage("Appended amplitude calibration information to file '%s'\n", Filename.c_str());
+  PrintMessage("Appended amplitude calibration to file '%s'\n", Filename.c_str());
 }
 
@@ -985,6 +981,6 @@
 
 	  // Write channel data (12 bit signed two's complement with out-of-range-bit and leading zeroes)
-	  int Count = 0;
-	  memset(Data, 0, Boards.size()*NChips*NChannels*NBins*sizeof(short));
+	  int Count = Brd*NChips*NChannels*NBins;
+	  memset(Data+Count, 0, NChips*NChannels*NBins*sizeof(short));
 
 	  Boards[Brd]->Lock();
@@ -1022,5 +1018,5 @@
 	if (Mode != datarun) continue;
 	
-	// Check if all event numbers are the same
+	// Check if all event numbers of active boards are the same
 	bool Same = true;
 	for (unsigned int i=0; i<Boards.size(); i++) {
Index: /fact/FADctrl/FADBoard.cc
===================================================================
--- /fact/FADctrl/FADBoard.cc	(revision 10225)
+++ /fact/FADctrl/FADBoard.cc	(revision 10226)
@@ -146,25 +146,19 @@
 // Perform amplitude calibration in steps
 //
+// The steps are intended to assure that up to date data is available
 void FADBoard::AmplitudeCalibration() {
 
-  enum StateType {wait, init, baseline, gain, secondary};
-
-  static struct BoardStatus InitialStatus;
-  static vector<unsigned short> ROICmd;
-  static unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
-  static StateType State = wait;
-  static int Count = 0;
-
-  // Check if mode is amplitude calibration
-  if (m->Mode != m->acalib) {
-	State = init;
-	return;
-  }
+  vector<unsigned short> ROICmd;
+  unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
+  string Message = string("ACALIBDONE")+Name;
 
   switch (State) {
-  // ====== Part A: Initialization =====
-  case init:
+  // ====== Part A: Check if amplitude calibration should start and initialise =====
+  case standbye:
+	if (m->Mode != m->acalib) break;
+
 	// Invalidate current calibration
 	ACalib.Time = -1;
+	Count = 0;
 
 	// Save initial board status, set all ROIs to 1024 and set DAC values
@@ -189,4 +183,11 @@
   // ====== Part B: Baseline calibration =====
   case baseline:
+	// Check for stopping
+	if (m->Mode != m->acalib) {
+	  State = cleanup;
+	  break;
+	}
+
+	// Average
 	for (unsigned int Chip=0; Chip<NChips; Chip++) {
 	  for (unsigned int Chan=0; Chan<NChannels; Chan++) {
@@ -199,28 +200,35 @@
 	
 	// Determine baseline if integration finished
-	if (Count == m->NumEventsRequested) {
-	  for (unsigned int i=0; i<NChips; i++) {
-		for (unsigned int j=0; j<NChannels; j++) {
-		  for (unsigned int k=0; k<NBins; k++) {
-			ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
-		  }
-		}
-	  }
-	  
-	  // Set new DAC values and start accumulation
-	  DACCmd[1] = htons(50000);
-	  DACCmd[3] = htons(50000);
-	  DACCmd[5] = htons(50000);
-	  Send(DACCmd, sizeof(DACCmd));
-
-	  // Clear sum vector and set state to accumulate
-	  memset(Sum, 0, sizeof(Sum));
-	  Count = 0;
-	  State = gain;
-	}
+	if (Count < m->NumEventsRequested) break;
+
+	for (unsigned int i=0; i<NChips; i++) {
+	  for (unsigned int j=0; j<NChannels; j++) {
+		for (unsigned int k=0; k<NBins; k++) {
+		  ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
+		}
+	  }
+	}
+
+	// Set new DAC values and start accumulation
+	DACCmd[1] = htons(50000);
+	DACCmd[3] = htons(50000);
+	DACCmd[5] = htons(50000);
+	Send(DACCmd, sizeof(DACCmd));
+
+	// Clear sum vector and set state to accumulate
+	memset(Sum, 0, sizeof(Sum));
+	Count = 0;
+	State = gain;
 	break;
 
   // ====== Part C: Gain calibration =====
   case gain:
+	// Check for stopping
+	if (m->Mode != m->acalib) {
+	  State = cleanup;
+	  break;
+	}
+
+	// Average
 	for (unsigned int Chip=0; Chip<NChips; Chip++) {
 	  for (unsigned int Chan=0; Chan<NChannels; Chan++) {
@@ -233,28 +241,35 @@
 	
 	// Determine gain if integration finished
-	if (Count == m->NumEventsRequested) {
-	  for (unsigned int i=0; i<NChips; i++) {
-		for (unsigned int j=0; j<NChannels; j++) {
-		  for (unsigned int k=0; k<NBins; k++) {
-			ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
-		  }
-		}
-	  }
-	  
-	  // Set new DAC values and start accumulation
-	  DACCmd[1] = htons(0);
-	  DACCmd[3] = htons(0);
-	  DACCmd[5] = htons(0);
-	  Send(DACCmd, sizeof(DACCmd));
-
-	  // Clear sum vector and set state to accumulate
-	  memset(Sum, 0, sizeof(Sum));
-	  Count = 0;
-	  State = secondary;
-	}
+	if (Count < m->NumEventsRequested) break;
+	
+	for (unsigned int i=0; i<NChips; i++) {
+	  for (unsigned int j=0; j<NChannels; j++) {
+		for (unsigned int k=0; k<NBins; k++) {
+		  ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
+		}
+	  }
+	}
+
+	// Set new DAC values and start accumulation
+	DACCmd[1] = htons(0);
+	DACCmd[3] = htons(0);
+	DACCmd[5] = htons(0);
+	Send(DACCmd, sizeof(DACCmd));
+
+	// Clear sum vector and set state to accumulate
+	memset(Sum, 0, sizeof(Sum));
+	Count = 0;
+	State = secondary;
 	break;
 
   // ====== Part D: Secondary calibration =====
   case secondary:
+	// Check for stopping
+	if (m->Mode != m->acalib) {
+	  State = cleanup;
+	  break;
+	}
+
+	// Average
 	for (unsigned int Chip=0; Chip<NChips; Chip++) {
 	  for (unsigned int Chan=0; Chan<NChannels; Chan++) {
@@ -267,46 +282,51 @@
 	
 	// Determine secondary baseline if integration finished
-	if (Count == m->NumEventsRequested) {
-	  for (unsigned int i=0; i<NChips; i++) {
-		for (unsigned int j=0; j<NChannels; j++) {
-		  for (unsigned int k=0; k<NBins; k++) {
-			ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
-		  }
-		}
-	  }
-	  
-	  // Write back original ROI and DAC settings
-	  ROICmd.clear();
-	  for (unsigned int i=0; i<NChips*NChannels; i++) {
-		ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
-		ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
-	  }
-	  Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
-
-      DACCmd[1] = htons(InitialStatus.DAC[1]);
-      DACCmd[3] = htons(InitialStatus.DAC[2]);
-      DACCmd[5] = htons(InitialStatus.DAC[3]);
-  	  Send(DACCmd, sizeof(DACCmd));
-
-	  // Store calibration time and temperature
-	  ACalib.DNA = Status.DNA;
-	  ACalib.Frequency = Status.Frequency;
-	  ACalib.Time = time(NULL);
-	  ACalib.Temp = 0;
-	  for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
-	  
-	  // Inform event thread that calibration is finished
-	  string Message = string("ACALIBDONE")+Name;
-	  if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
-		m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
-	  }
-
-	  State = wait;
-	}
+	if (Count < m->NumEventsRequested) break;
+
+	for (unsigned int i=0; i<NChips; i++) {
+	  for (unsigned int j=0; j<NChannels; j++) {
+		for (unsigned int k=0; k<NBins; k++) {
+		  ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
+		}
+	  }
+	}
+
+	// Store calibration time and temperature
+	ACalib.DNA = Status.DNA;
+	ACalib.Frequency = Status.Frequency;
+	ACalib.Time = time(NULL);
+	ACalib.Temp = 0;
+	for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
+
+	// Inform event thread that calibration is finished for this board
+	if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
+	  m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
+	}
+
+	State = cleanup;
 	break;
 
-  // ====== Wait for Mode not being acalib =====
+  // ====== Part E: Write back original ROI and DAC settings =====
+  case cleanup:
+    // ROI values
+	ROICmd.clear();
+	for (unsigned int i=0; i<NChips*NChannels; i++) {
+	  ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
+	  ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
+	}
+	Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
+
+	// DAC values
+    DACCmd[1] = htons(InitialStatus.DAC[1]);
+    DACCmd[3] = htons(InitialStatus.DAC[2]);
+    DACCmd[5] = htons(InitialStatus.DAC[3]);
+  	Send(DACCmd, sizeof(DACCmd));
+	
+	State = wait;
+	break;
+
+  // ====== Wait for Mode not being idle =====
   case wait:
-	if (m->Mode != m->acalib) State = init;
+	if (m->Mode == m->idle) State = standbye;
 	break;
   }
@@ -435,11 +455,10 @@
 	  }
 
-		// Extract Firmware status info
-		Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
-		Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
-		Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
-		Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
-		Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
-
+	  // Extract Firmware status info
+	  Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
+	  Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
+	  Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
+	  Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
+	  Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
 
 	  // Extract temperatures (MSB indicates if temperature is positive or negative)
Index: /fact/FADctrl/FADBoard.h
===================================================================
--- /fact/FADctrl/FADBoard.h	(revision 10225)
+++ /fact/FADctrl/FADBoard.h	(revision 10226)
@@ -43,20 +43,24 @@
 	  uint32_t BoardTime;
 	  uint32_t EventCounter;
+	  struct timeval Update;
+
 	  uint32_t TriggerID;
 	  uint16_t TriggerType;
 	  uint16_t TriggerCRC;
 	  uint16_t TriggerCell[NChips];
+
 	  float Frequency;
 	  bool Lock[NChips];
-		bool denable;
-		bool dwrite;
-		bool DCM_lock;
-		bool DCM_ready;
-		bool spi_clk;
 	  int8_t PhaseShift;
+
+	  bool denable;
+	  bool dwrite;
+	  bool DCM_lock;
+	  bool DCM_ready;
+	  bool spi_clk;
+
 	  uint16_t ROI[NChips][NChannels];
 	  uint16_t DAC[NDAC];
 	  float Temp[NTemp];
-	  struct timeval Update;
 	} Status;
 
@@ -88,4 +92,12 @@
 	pthread_t Thread;
 	pthread_cond_t CondVar;
+	
+  // Amplitude calibration
+   	int Count;  
+
+  private:
+	enum StateType {standbye, baseline, gain, secondary, cleanup, wait};
+	struct BoardStatus InitialStatus;
+	StateType State;
 };
 
