Index: tools/ddd/Functions.cpp
===================================================================
--- tools/ddd/Functions.cpp	(revision 74)
+++ tools/ddd/Functions.cpp	(revision 81)
@@ -22,11 +22,5 @@
 
   // Write run header to temporary file  
-  FILE *Tmpfile = tmpfile();
-  if(Tmpfile==NULL) {
-    QMessageBox::warning(this, "ddd Message","Could not open temporary file.",QMessageBox::Ok);
-    CloseDatafile();
-    return;
-  }
-
+  rewind(Tmpfile);
   switch (RD->OpenDataFile(FilenameBox->text().toAscii().data(), Tmpfile)) {
     case CTX_FOPEN:   QMessageBox::warning(this, "ddd Message","Could not open file.",QMessageBox::Ok);
@@ -49,5 +43,4 @@
   QString text = in.readAll();
   RunHeaderDisplay->setPlainText(text);
-  fclose(Tmpfile);
 
   // Enable spin boxes, set ranges and display first event
@@ -88,119 +81,117 @@
 // +++ Read event header and display event (only called if Datafile is open) +++
 void ddd::DisplayEvent(int) {
+
+  PixelID->setText(PixMap->DRS_to_Pixel(BoardNo->value(), ChannelNo->value()/10, ChannelNo->value()%10).c_str());  // Translate to pixel ID
+  if(Socket->state() == QAbstractSocket::ConnectedState) return; // do not execute if socket is open
+  
+  // Read event
+  rewind(Tmpfile);
+  if (RD->ReadEvent(EventNo->value(), Tmpfile) != CTX_OK) {
+    QMessageBox::warning(this, "ddd Warning","Could not read event.",QMessageBox::Ok);
+    EventHeaderDisplay->clear();
+    return;
+  }
  
-  PixelID->setText(PixMap->DRS_to_Pixel(BoardNo->value(),ChannelNo->value()/10,ChannelNo->value()%10).c_str());
-  if(Socket->state() == QAbstractSocket::ConnectedState) return; // do not execute if socket is open
-  
-  // Read event
-  FILE *Tmpfile = tmpfile();
-
-   switch(RD->ReadEvent(EventNo->value(), Tmpfile)) {
-    case !CTX_OK:  
-      QMessageBox::warning(this, "ddd Warning","Could not read event.",QMessageBox::Ok);
-      EventHeaderDisplay->clear();
-      break;
-    default:  // Print event header and trigger cell information from event data
-      rewind(Tmpfile);
-      QTextStream in(Tmpfile);
-      QString text = in.readAll();
-      text.append("\nTrigger cells: ");
-      for (unsigned int i=0; i<RD->RHeader->NBoards*RD->RHeader->NChips; i++) {
-        QString a;
-        text.append(a.sprintf("%d ", *((int *)RD->Data + i)));
-      }
-      EventHeaderDisplay->setPlainText(text);
-      
-      // Case data in double format required by qwt library
-      double* x = new double [RD->RHeader->Samples];
-      double* y = new double [RD->RHeader->Samples];
-
-      for (unsigned int i=0; i<RD->RHeader->Samples; i++) {
-      	x[i] = (double) (i/RD->BStruct[BoardNo->value()].NomFreq);
-        y[i] = (double) *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) + BoardNo->value()*RD->RHeader->NChips*RD->RHeader->NChannels *
-		 RD->RHeader->Samples+ChannelNo->value()*RD->RHeader->Samples+i)*RD->BStruct[BoardNo->value()].ScaleFactor;
-      }
-      Signal->setData(x, y, RD->RHeader->Samples);
-      Signal->show();
-      Zoomer->setZoomBase(Signal->boundingRect());
-
-      //Get data for M0 display (event based)
-
-      double z[6][6];//36 pixels
-
-      for(unsigned int i=0; i<RD->RHeader->NBoards; i++) {//board loop
-	for(unsigned int j=0; j<RD->RHeader->NChips; j++) {//chip loop
-	  for(unsigned int k=0; k<RD->RHeader->NChannels; k++) {//channel loop
-
-	    //only interested in M0 data
-	    if( ( (i==0 || i==1) && (j<=1) && (k<=7) ) || ( (i==2) && (j==0) && (k<=3) ) ) {
-
-	      //get module, superpixel and pixel number from pixel name
-
-	      std::string pixelname = PixMap->DRS_to_Pixel(i,j,k);
-	      char pixelname_copy[256];
-	      memset(pixelname_copy,'\0',256);
-	      pixelname.copy(pixelname_copy, 256);
-	      
-	      char delim[] = "-";
-	      char *buffer = NULL;
-	      int module = -1;
-	      int superpixel = -1;
-	      int pixel = -1;
-	      
-	      buffer = strtok(pixelname_copy, delim);
-	      module = atoi(buffer);
-	      buffer = strtok(NULL, delim);
-	      superpixel = atoi(buffer);
-	      buffer = strtok(NULL, delim);
-	      pixel = atoi(buffer);
-	      	
-	      //usual M0 mapping
-	      //int binx = 5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2));
-	      //int biny = 5-(((superpixel-1)%3)*2)-(int((pixel-1)/2));
-	      	      
-	      //M0 upside down
-	      int binx = 5-(5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2)));
-	      int biny = 5-(5-(((superpixel-1)%3)*2)-(int((pixel-1)/2)));
-
-	      //search maximum sample amplitude within user specified window
-	      //start bin is always smaller than stop bin (taken care of by updated ranges)
-	      int StartBin = (int)(M0Start->value());
-	      int StopBin = (int)(M0Stop->value());
-	      
-	      for(int l=StartBin; l<=StopBin; l++){
-		  
-		float sample = *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) + 
-				 i*RD->RHeader->NChips*RD->RHeader->NChannels*RD->RHeader->Samples+
-				 j*RD->RHeader->NChannels*RD->RHeader->Samples+
-				 k*RD->RHeader->Samples+
-				 l)*RD->BStruct[i].ScaleFactor;
-		
-		if (sample > z[binx][biny]) {
-		  z[binx][biny]=sample;
-		}
-		
-	      }//sample loop
-
-	    }//only M0 data
-	    
-	  }//channel loop
-	}//chip loop
-      }//board loop
-      
-      //fill data to M0 display (event based)
-      Signal2D->setData(SpectrogramDataM0(z));
-      Graph2D->axisWidget(QwtPlot::yRight)->setColorMap(Signal2D->data().range(),Signal2D->colorMap());
-      Graph2D->setAxisScale(QwtPlot::yRight,Signal2D->data().range().minValue(),Signal2D->data().range().maxValue() );
-      Graph2D->replot();
-      //Signal2D->show();
-
-      //update ranges for start and stop bin to avoid startbin > stopbin
-      M0Start->setRange(0, M0Stop->value());  
-      M0Stop->setRange(M0Start->value(),(RD->RHeader->Samples)-1);
-
-      delete[] x;	delete[] y;
-
-  }
-  if(Tmpfile!=NULL) fclose(Tmpfile);
+  // Print event header and trigger cell information from event data
+  rewind(Tmpfile);
+  QTextStream in(Tmpfile);
+  QString text = in.readAll();
+  
+  text.append("\nTrigger cells: ");
+  for (unsigned int i=0; i<RD->RHeader->NBoards*RD->RHeader->NChips; i++) {
+    QString a;
+    text.append(a.sprintf("%d ", *((int *)RD->Data + i)));
+  }
+  EventHeaderDisplay->setPlainText(text);
+
+  // Case data in double format required by qwt library
+  double *x = new double [RD->RHeader->Samples];
+  double *y = new double [RD->RHeader->Samples];
+
+  for (unsigned int i=0; i<RD->RHeader->Samples; i++) {
+    x[i] = (double) (i/RD->BStruct[BoardNo->value()].NomFreq);
+    y[i] = (double) *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) + BoardNo->value()*RD->RHeader->NChips*RD->RHeader->NChannels *
+	     RD->RHeader->Samples+ChannelNo->value()*RD->RHeader->Samples+i)*RD->BStruct[BoardNo->value()].ScaleFactor;
+  }
+
+  Signal->setData(x, y, (int) RD->RHeader->Samples);
+  Signal->show();
+  Zoomer->setZoomBase(Signal->boundingRect());
+  
+  delete[] x;	delete[] y;
+
+  // ************************************   
+  // Get data for M0 display (event based)
+  // ************************************   
+
+  double z[6][6];//36 pixels
+
+  for(unsigned int i=0; i<RD->RHeader->NBoards; i++) {//board loop
+    for(unsigned int j=0; j<RD->RHeader->NChips; j++) {//chip loop
+      for(unsigned int k=0; k<RD->RHeader->NChannels; k++) {//channel loop
+
+	//only interested in M0 data
+	if( ( (i==0 || i==1) && (j<=1) && (k<=7) ) || ( (i==2) && (j==0) && (k<=3) ) ) {
+
+	  //get module, superpixel and pixel number from pixel name
+
+	  std::string pixelname = PixMap->DRS_to_Pixel(i,j,k);
+	  char pixelname_copy[256];
+	  memset(pixelname_copy,'\0',256);
+	  pixelname.copy(pixelname_copy, 256);
+
+	  char delim[] = "-";
+	  char *buffer = NULL;
+	  int module = -1;
+	  int superpixel = -1;
+	  int pixel = -1;
+
+	  buffer = strtok(pixelname_copy, delim);
+	  module = atoi(buffer);
+	  buffer = strtok(NULL, delim);
+	  superpixel = atoi(buffer);
+	  buffer = strtok(NULL, delim);
+	  pixel = atoi(buffer);
+
+	  //usual M0 mapping
+	  //int binx = 5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2));
+	  //int biny = 5-(((superpixel-1)%3)*2)-(int((pixel-1)/2));
+
+	  //M0 upside down
+	  int binx = 5-(5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2)));
+	  int biny = 5-(5-(((superpixel-1)%3)*2)-(int((pixel-1)/2)));
+
+	  //search maximum sample amplitude within user specified window
+	  //start bin is always smaller than stop bin (taken care of by updated ranges)
+	  int StartBin = (int)(M0Start->value());
+	  int StopBin = (int)(M0Stop->value());
+
+	  for(int l=StartBin; l<=StopBin; l++){
+
+	    float sample = *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) + 
+			     i*RD->RHeader->NChips*RD->RHeader->NChannels*RD->RHeader->Samples+
+			     j*RD->RHeader->NChannels*RD->RHeader->Samples+
+			     k*RD->RHeader->Samples+
+			     l)*RD->BStruct[i].ScaleFactor;
+
+	    if (sample > z[binx][biny]) {
+	      z[binx][biny]=sample;
+	    }
+
+	  }//sample loop
+	}//only M0 data
+      }//channel loop
+    }//chip loop
+  }//board loop
+
+  //fill data to M0 display (event based)
+  Signal2D->setData(SpectrogramDataM0(z));
+  Graph2D->axisWidget(QwtPlot::yRight)->setColorMap(Signal2D->data().range(),Signal2D->colorMap());
+  Graph2D->setAxisScale(QwtPlot::yRight,Signal2D->data().range().minValue(),Signal2D->data().range().maxValue() );
+  Graph2D->replot();
+
+  //update ranges for start and stop bin to avoid startbin > stopbin
+  M0Start->setRange(0, M0Stop->value());  
+  M0Stop->setRange(M0Start->value(),(RD->RHeader->Samples)-1);
 }
 
@@ -304,5 +295,5 @@
   if (WaitForData && Text.endsWith(QLatin1String("==END=="))) {
     // Extract text between ==START== and ==END==
-    QByteArray Data=Text.mid(Text.lastIndexOf("==START==")+9,Text.length()-Text.lastIndexOf("==START==")-16).toAscii();
+    QByteArray Data = Text.mid(Text.lastIndexOf("==START==")+9, Text.length() - Text.lastIndexOf("==START==")-16).toAscii();
 
     char *NextNumber = strtok(Data.data()," ");  // Number of entries that follow
Index: tools/ddd/GUI.cpp
===================================================================
--- tools/ddd/GUI.cpp	(revision 74)
+++ tools/ddd/GUI.cpp	(revision 81)
@@ -24,4 +24,9 @@
   PixMap = new PixelMap("../../config/PixelMap.txt", false);
    
+  Tmpfile = tmpfile();
+  if(Tmpfile==NULL) {
+    QMessageBox::warning(this, "ddd Message","Could not open temporary file.",QMessageBox::Ok);
+  }
+
   //---------------------------------------------------------------------
   //**************************** Main window ****************************
@@ -107,4 +112,12 @@
   Signal->attach(Graph);
   Signal->setStyle(QwtPlotCurve::Steps);
+
+  AutoscaleBox = new QCheckBox("Autoscale", Central);
+  AutoscaleBox->setFont(QFont("Times", 10, QFont::Bold));
+  AutoscaleBox->setToolTip("Scale axes automatically");
+
+  GraphLayout = new QVBoxLayout;
+  GraphLayout->addWidget(Graph);
+  GraphLayout->addWidget(AutoscaleBox);
 
   // Text boxes for run and event header
@@ -283,4 +296,6 @@
   delete M0Window;
   delete PixMap;		delete RD;
+  
+  fclose(Tmpfile);
 }
 
Index: tools/ddd/GUI.h
===================================================================
--- tools/ddd/GUI.h	(revision 74)
+++ tools/ddd/GUI.h	(revision 81)
@@ -29,5 +29,5 @@
 
     QPushButton *GetButton, *SocketButton, *Connect, *M0Display;
-    QCheckBox *ContinuousBox;
+    QCheckBox *ContinuousBox, *AutoscaleBox;
     QToolButton *LoadButton;
     QLineEdit *FilenameBox, *IPAddress, *Command, *PixelID;
@@ -39,4 +39,5 @@
     QAction *OpenAction, *ConnectAction;
     QGridLayout *SocketLayout, *MainLayout, *M0Layout;
+    QVBoxLayout *GraphLayout;
     QFormLayout *CommandLayout, *PortLayout, *AddressLayout, *FormLayout, *SpinLayout, *M0StartLayout, *M0StopLayout;
     
@@ -54,4 +55,5 @@
     RawDataCTX *RD;
     PixelMap *PixMap;
+    FILE *Tmpfile;
       
   public:
