#include "../include/sipmscan.h"
 
#include "../include/workstation.h"
 
#include "../include/substructure.h"
 
 
 
#include <stdio.h>
 
#include <stdlib.h>
 
 
 
// Main window constructor (+layout) ---------------------------------
 
 
 
TGAppMainFrame::TGAppMainFrame(const TGWindow *p, int w, int h)
 
{
 
   TGCompositeFrame *fT1;
 
   idtotal = 0;
 
 
 
   char *cTemp;
 
 
 
   // CAMAC and Scope objects
 
   gDaq = new daq();
 
   gScopeDaq = new daqscope();
 
 
 
   // Define main window and menubar
 
   fMain = new TGMainFrame(p, w, h, kVerticalFrame);
 
   
 
   // Initialize the menu
 
   fMenuBar = new TGMenuBar(fMain, 200, 30);
 
   InitMenu();
 
   fMain->AddFrame(fMenuBar, new TGLayoutHints(kLHintsExpandX | kLHintsTop));
 
 
 
   // Prepare the tabbed structure
 
   fTab = new TGTab(fMain, 500, 500);
 
 
 
   // Read the layout we wish to use
 
   int frmWidth[measwin+analysiswin], frmHeight[measwin+analysiswin];
 
   LayoutRead(measwin+analysiswin, frmWidth, frmHeight);
 
 
 
   // Structure the measurement layout window
 
   int *vert, *hor, *wPane, *hPane;
 
 
 
   vert = new int[2]; hor = new int;
 
   *hor = 1;
 
   vert[0] = 1; vert[1] = 0;
 
 
 
   const char *measFrmTit[] = {"Settings pane", "Display", "Main measurement window"};
 
   wPane = new int[measwin]; hPane = new int[measwin];
 
   for(int i = 0; i < measwin; i++)
 
   {
 
      wPane[i] = frmWidth[i];
 
      hPane[i] = frmHeight[i];
 
   }
 
 
 
   fT1 = fTab->AddTab("Measurement");
 
   TGSplitter(fT1, "horizontal", hor, vert, measFrmTit, measLayout, wPane, hPane);
 
   fT1->AddFrame(fLayout[idtotal], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
   idtotal++;
 
   delete[] vert; delete hor; delete[] wPane; delete[] hPane;
 
 
 
   // Structure the analysis layout window
 
   vert = new int[2]; hor = new int;
 
   *hor = 1;
 
   vert[0] = 1; vert[1] = 1;
 
 
 
   const char *analysisFrmTit[] = {"Histogram file selection", "Analysis", "Histogram", "Histogram controls"};
 
   wPane = new int[analysiswin]; hPane = new int[analysiswin];
 
   for(int i = 0; i < analysiswin; i++)
 
   {
 
      wPane[i] = frmWidth[i+measwin];
 
      hPane[i] = frmHeight[i+measwin];
 
   }
 
 
 
   fT1 = fTab->AddTab("Analysis");
 
   TGSplitter(fT1, "horizontal", hor, vert, analysisFrmTit, analysisLayout, wPane, hPane);
 
   fT1->AddFrame(fLayout[idtotal], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
   idtotal++;
 
   delete[] vert; delete hor; delete[] wPane; delete[] hPane;
 
 
 
   // Structure the monitoring layout window (Fieldpoint) //TODO
 
   fT1 = fTab->AddTab("Monitoring");
 
   fTab->SetEnabled(2,kFALSE); //TODO
 
 
 
   // Structure the help layout window
 
   fT1 = fTab->AddTab("Help");
 
   TGTextView *helpdesc;
 
   const TGFont *tfont = gClient->GetFont(HELPFONT);
 
   FontStruct_t helpFont = tfont->GetFontStruct();
 
   helpdesc = new TGTextView(fT1,100,100);
 
   helpdesc->SetFont(helpFont);
 
   helpdesc->SetForegroundColor(0x000000);
 
   fT1->AddFrame(helpdesc, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
   
 
   cTemp = new char[1024];
 
   sprintf(cTemp, "%s/doc/README", rootdir);
 
   if(helpdesc->LoadFile(cTemp))
 
   {
 
      if(DBGSIG) printf("TGAppMainFrame(): Help file correctly loaded.\n");
 
   }
 
   else
 
      printf("Error! Help file not loaded.\n");
 
   delete[] cTemp;
 
   helpdesc->AddLine("");
 
 
 
   fMain->AddFrame(fTab, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
 
 
   // Set the inner layout of each frame
 
   AppLayout();
 
 
 
   fMain->SetWindowName(WINDOW_NAME);
 
   fMain->MapSubwindows();
 
   fMain->MapWindow();
 
   fMain->Layout();
 
 
 
   // Prepare initial settings
 
   EnableScan(0);       //. 
 
   EnableScan(1);       // Grey out scan
 
   EnableScan(2);       // options by default
 
   EnableScan(3);       //.
 
   EnableLiveUpdate();  //. Disable the live histogram update at beginning
 
   VoltOut(1);          //. Get the output voltage save in file
 
   HistogramOptions(1); //. Enable clean plots by default
 
   plotType->widgetTB[0]->SetDown(kTRUE);                       //.
 
   fMenuHisttype->CheckEntry(M_ANALYSIS_HISTTYPE_1DADC);        // Set the ADC histogram
 
   fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_1DTDC);      // to show by default
 
   fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_2D);         //.
 
   acqStarted = false;  //. At program start, the acquisition is stopped
 
   ToolTipSet();        //. Turn on tooltips
 
   PositionSet(1);      //. Get starting table position
 
   RotationSet(1);      //. Get starting rotation
 
 
 
   if(DBGSIG > 1)
 
   {
 
      printf("TGAppMainFrame(): At end of constructor\n");
 
      gObjectTable->Print();
 
   }
 
}
 
 
 
TGAppMainFrame::~TGAppMainFrame()
 
{
 
   fMain->Cleanup();
 
   delete fMain;
 
}
 
 
 
// -------------------------------------------------------------------
 
 
 
// Event handler for menubar actions ---------------------------------
 
 
 
void TGAppMainFrame::HandleMenu(Int_t id)
 
{
 
//   int ret = 0;
 
   char cmd[256];
 
 
 
   switch(id)
 
   {
 
      case M_FILE_SET_LAYOUT:
 
         LayoutSet();
 
         break;
 
 
 
      case M_FILE_SAVE_LAYOUT:
 
         LayoutSave();
 
         break;
 
 
 
      case M_FILE_SAVE_MSETTINGS:
 
         // Here, we save the set values in the measurement layout (automatically done when we safely exit the application and after each start of measurement).
 
         break;
 
 
 
      case M_FILE_SAVE_ASETTINGS:
 
         // Here, we save the set values in the analysis layout (automatically done when we safely exit the application and after each start of analysis).
 
         break;
 
 
 
      case M_FILE_CHECK_WIDTH:
 
         printf("\nSettings window: %dx%d\n", measLayout[0]->GetWidth(), measLayout[0]->GetHeight());
 
         printf("Histogram window: %dx%d\n", measLayout[1]->GetWidth(), measLayout[1]->GetHeight());
 
         printf("Main measurement window: %dx%d\n", measLayout[2]->GetWidth(), measLayout[2]->GetHeight());
 
         printf("Histogram file window: %dx%d\n", analysisLayout[0]->GetWidth(), analysisLayout[0]->GetHeight());
 
         printf("Analysis window: %dx%d\n", analysisLayout[1]->GetWidth(), analysisLayout[1]->GetHeight());
 
         printf("Histogram window: %dx%d\n", analysisLayout[2]->GetWidth(), analysisLayout[2]->GetHeight());
 
         printf("Histogram controls window: %dx%d\n", analysisLayout[3]->GetWidth(), analysisLayout[3]->GetHeight());
 
 
 
         printf("Main window: %dx%d\n", fMain->GetWidth(), fMain->GetHeight());
 
         printf("Menu bar: %dx%d\n", fMenuBar->GetWidth(), fMenuBar->GetHeight());
 
         printf("Tab window: %dx%d\n", fTab->GetWidth(), fTab->GetHeight());
 
         break;
 
 
 
      case M_FILE_EXIT:
 
         CloseWindow();
 
         break;
 
 
 
      case M_ANALYSIS_HISTTYPE_1DADC:
 
         ChangeHisttype(0);
 
         break;
 
 
 
      case M_ANALYSIS_HISTTYPE_1DTDC:
 
         ChangeHisttype(1);
 
         break;
 
 
 
      case M_ANALYSIS_HISTTYPE_2D:
 
         ChangeHisttype(2);
 
         break;
 
 
 
      case M_ANALYSIS_INTEG:
 
         fTab->SetTab(1);
 
         analTab->SetTab(0);
 
         for(int i = 0; i < 3; i++)
 
         {
 
            if(intSpect->widgetChBox[i]->IsDown())
 
               intSpect->widgetChBox[i]->SetState(kButtonUp);
 
         }
 
         break;
 
 
 
      case M_ANALYSIS_INTEGX:
 
         fTab->SetTab(1);
 
         analTab->SetTab(0);
 
         for(int i = 0; i < 3; i++)
 
         {
 
            if(i == 0)
 
               intSpect->widgetChBox[i]->SetState(kButtonDown);
 
            else
 
            {
 
               if(intSpect->widgetChBox[i]->IsDown())
 
                  intSpect->widgetChBox[i]->SetState(kButtonUp);
 
            }
 
         }
 
         break;
 
 
 
      case M_ANALYSIS_INTEGY:
 
         fTab->SetTab(1);
 
         analTab->SetTab(0);
 
         for(int i = 0; i < 3; i++)
 
         {
 
            if(i == 1)
 
               intSpect->widgetChBox[i]->SetState(kButtonDown);
 
            else
 
            {
 
               if(intSpect->widgetChBox[i]->IsDown())
 
                  intSpect->widgetChBox[i]->SetState(kButtonUp);
 
            }
 
         }
 
         break;
 
 
 
      case M_ANALYSIS_PHOTMU:
 
         fTab->SetTab(1);
 
         analTab->SetTab(1);
 
         for(int i = 0; i < 3; i++)
 
         {
 
            if(i == 2)
 
               intSpect->widgetChBox[i]->SetState(kButtonDown);
 
            else
 
            {
 
               if(intSpect->widgetChBox[i]->IsDown())
 
                  intSpect->widgetChBox[i]->SetState(kButtonUp);
 
            }
 
         }
 
         break;
 
 
 
      case M_ANALYSIS_BREAKDOWN:
 
         fTab->SetTab(1);
 
         analTab->SetTab(2);
 
         break;
 
 
 
      case M_ANALYSIS_SURFSCAN:
 
         fTab->SetTab(1);
 
         analTab->SetTab(3);
 
         break;
 
 
 
      case M_ANALYSIS_TIMING:
 
         fTab->SetTab(1);
 
         analTab->SetTab(4);
 
         break;
 
 
 
      case M_HELP_WEBHELP:
 
         printf("TGAppMainFrame::HandleMenu(): Opening %s/doc/documentation.html in a web browser.\n", rootdir);
 
         sprintf(cmd, "xdg-open %s/doc/documentation.html &", rootdir);
 
         system(cmd);
 
         break;
 
 
 
      case M_HELP_ABOUT:
 
         About();
 
         break;
 
 
 
      default:
 
//         fMainFrame->SetCurrent(id);
 
         break;
 
   }
 
}
 
 
 
// -------------------------------------------------------------------
 
 
 
// Initialize the main window menu -----------------------------------
 
 
 
void TGAppMainFrame::InitMenu()
 
{
 
   fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
 
 
 
   // Popup menu in menubar for File controls
 
   fMenuFile = new TGPopupMenu(gClient->GetRoot());
 
   fMenuFile->AddEntry(new TGHotString("Set &user layout"), M_FILE_SET_LAYOUT);
 
   fMenuFile->AddEntry(new TGHotString("Save ¤t layout"), M_FILE_SAVE_LAYOUT);
 
   fMenuFile->AddSeparator();
 
   fMenuFile->AddEntry(new TGHotString("Save current &measurement settings"), M_FILE_SAVE_MSETTINGS);
 
   fMenuFile->AddEntry(new TGHotString("Save current &analysis settings"), M_FILE_SAVE_ASETTINGS);
 
   fMenuFile->AddEntry(new TGHotString("&Check frame width (Testing)"), M_FILE_CHECK_WIDTH);
 
   fMenuFile->AddSeparator();
 
   fMenuFile->AddEntry(new TGHotString("E&xit"), M_FILE_EXIT);
 
 
 
   // Popup menu in menubar for Analysis controls
 
   fMenuHisttype = new TGPopupMenu(gClient->GetRoot()); // adds a cascade menu that will be incorporated into analysis controls
 
   fMenuHisttype->AddEntry(new TGHotString("1D &ADC histogram"), M_ANALYSIS_HISTTYPE_1DADC);
 
   fMenuHisttype->AddEntry(new TGHotString("1D &TDC histogram"), M_ANALYSIS_HISTTYPE_1DTDC);
 
   fMenuHisttype->AddEntry(new TGHotString("&2D ADC vs. TDC histogram"), M_ANALYSIS_HISTTYPE_2D);
 
 
 
   fMenuAnalysis = new TGPopupMenu(gClient->GetRoot()); // adds a new popup menu to the menubar
 
   fMenuAnalysis->AddPopup(new TGHotString("&Histogram type"), fMenuHisttype);
 
   fMenuAnalysis->AddEntry(new TGHotString("&Integrate spectrum"), M_ANALYSIS_INTEG);
 
   fMenuAnalysis->AddEntry(new TGHotString("Integrate spectrum (&X direction)"), M_ANALYSIS_INTEGX);
 
   fMenuAnalysis->AddEntry(new TGHotString("Integrate spectrum (&Y direction)"), M_ANALYSIS_INTEGY);
 
   fMenuAnalysis->AddEntry(new TGHotString("&Relative PDE"), M_ANALYSIS_PHOTMU);
 
   fMenuAnalysis->AddEntry(new TGHotString("&Breakdown voltage"), M_ANALYSIS_BREAKDOWN);
 
   fMenuAnalysis->AddEntry(new TGHotString("Surface 2&D scan"), M_ANALYSIS_SURFSCAN);
 
   fMenuAnalysis->AddEntry(new TGHotString("&Timing analysis"), M_ANALYSIS_TIMING);
 
 
 
   // Popup menu in menubar for File controls
 
   fMenuHelp = new TGPopupMenu(gClient->GetRoot());
 
   fMenuHelp->AddEntry(new TGHotString("Open &help in web browser"), M_HELP_WEBHELP);
 
   fMenuHelp->AddEntry(new TGHotString("&About"), M_HELP_ABOUT);
 
 
 
   // Connect all menu items with actions
 
   fMenuFile->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
 
   fMenuAnalysis->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
 
   fMenuHelp->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
 
 
 
   // Draw the created popup menus on the menubar
 
   fMenuBar->AddPopup(new TGHotString("&File"), fMenuFile, fMenuBarItemLayout);
 
   fMenuBar->AddPopup(new TGHotString("&Analysis"),fMenuAnalysis,fMenuBarItemLayout);
 
   fMenuBar->AddPopup(new TGHotString("&Help"), fMenuHelp, fMenuBarItemLayout);
 
}
 
 
 
// -------------------------------------------------------------------
 
 
 
// Setting the application subwindow layout --------------------------
 
 
 
void TGAppMainFrame::AppLayout()
 
{
 
   double numform[6], numform2[6];
 
   int *checksel;
 
   char selected[256];
 
   int subgroup[2];
 
   TGCompositeFrame *fH1, *fV1, *fH2, *fT1;
 
   TGGroupFrame *fG1;
 
 
 
   TGLayoutHints *f0centerX = new TGLayoutHints(kLHintsCenterX,2,2,2,2);
 
   TGLayoutHints *f0leftX = new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2);
 
   TGLayoutHints *f0leftXnoleft = new TGLayoutHints(kLHintsLeft | kLHintsTop,0,2,2,2);
 
   TGLayoutHints *f0leftXpad = new TGLayoutHints(kLHintsLeft | kLHintsTop,12,12,2,2);
 
   TGLayoutHints *f0rightX = new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2);
 
   TGLayoutHints *f0rightXpad = new TGLayoutHints(kLHintsRight | kLHintsTop,12,12,2,2);
 
   TGLayoutHints *f0centerY = new TGLayoutHints(kLHintsCenterY,2,2,2,2);
 
   TGLayoutHints *f0center2d = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,2,2,2,2);
 
   TGLayoutHints *f1expandX = new TGLayoutHints(kLHintsExpandX,2,2,2,2);
 
   TGLayoutHints *f1expand2d = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,2,2,2,2);
 
   TGLayoutHints *f1expandXpad = new TGLayoutHints(kLHintsExpandX,12,12,2,2);
 
 
 
// Settings pane ---------------------------------------------------------------------------
 
   subgroup[0] = (measLayout[0]->GetWidth())-4;
 
 
 
   // Check buttons to toggle voltage, surface and Z axis scans
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *scansOn -> 4 check buttons (voltage, surface, Z axis, incidence angle scans)\n");
 
   scansOn = new TSubStructure();
 
   checksel = new int[4];
 
   checksel[0] = 0; checksel[1] = 0; checksel[2] = 0; checksel[3] = 0;
 
   const char *selnames[] = {"Voltage scan ON/OFF", "Surface scan ON/OFF", "Z-axis scan ON/OFF","Rotation scan ON/OFF","0","0","0","0","0","0","0","0","0","0","0","0"};
 
   if(scansOn->TGCheckList(measLayout[0], subgroup[0], 30, 4, selnames, checksel, "vertical", "center"))
 
      measLayout[0]->AddFrame(scansOn->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Hard limit for maximum voltage we can set
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vHardlimit -> Number entry for voltage limit\n");
 
   vHardlimit = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 2;
 
   if(vHardlimit->TGLabelNEntry(measLayout[0], subgroup[0], 30, "Voltage limit:", 70.00, numform, "center"))
 
      measLayout[0]->AddFrame(vHardlimit->outsidebox, f1expandXpad);
 
 
 
   // Number of used channels
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *NCH -> Number entry for number of channels to capture\n");
 
   NCH = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 3; numform[2] = 2; numform[3] = 2; numform[4] = 1; numform[5] = 8;
 
   if(NCH->TGLabelNEntry(measLayout[0], subgroup[0], 30, "Nr. of channels:", 1, numform, "center"))
 
      measLayout[0]->AddFrame(NCH->outsidebox, f1expandXpad);
 
 
 
   // Select the units to use for table positioning (micrometer, table position units)
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *posUnits -> Dropdown menu for selecting the units for table positioning\n");
 
   posUnits = new TSubStructure();
 
   selnames[0] = "table units"; selnames[1] = "micrometers";
 
   sprintf(selected, "table units");
 
   if(posUnits->TGLabelDrop(measLayout[0], 2*subgroup[0]/3, 30, "Position units:", 2, selnames, selected))
 
      measLayout[0]->AddFrame(posUnits->outsidebox, f1expandXpad);
 
 
 
   // Select the units to use for rotation platform (degrees, table rotation units)
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotUnits -> Dropdown menu for selecting the units for rotation\n");
 
   rotUnits = new TSubStructure();
 
   selnames[0] = "table units"; selnames[1] = "degrees";
 
   sprintf(selected, "degrees");
 
   if(rotUnits->TGLabelDrop(measLayout[0], 2*subgroup[0]/3, 30, "Rotation units:", 2, selnames, selected))
 
      measLayout[0]->AddFrame(rotUnits->outsidebox, f1expandXpad);
 
 
 
   // Button and textbox to enter the oscilloscope IP address
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *oscConnect -> Text Entry (oscilloscope IP address)\n");
 
   oscConnect = new TSubStructure();
 
   if(oscConnect->TGLabelTEntryButton(measLayout[0], subgroup[0], 30, "Scope IP:", "178.172.43.157", "Connect", "twoline"))
 
      measLayout[0]->AddFrame(oscConnect->outsidebox, f1expandXpad);
 
 
 
   // Laser settings (freq., tune, ND filter)
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *laserInfo -> Text Entry (Laser setting information)\n");
 
   laserInfo = new TSubStructure();
 
   if(laserInfo->TGLabelTEntry(measLayout[0], subgroup[0], 30, "Laser settings:", "kHz, tune, ND", "twoline"))
 
      measLayout[0]->AddFrame(laserInfo->outsidebox, f1expandXpad);
 
 
 
   // Chamber temperature (will only be manually set until we can get it directly from the chamber)
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *chtemp -> Number entry for chamber temperature\n");
 
   chtemp = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 5; numform[1] = 1; numform[3] = 2; numform[4] = -70.; numform[5] = 140.;
 
   if(chtemp->TGLabelNEntry(measLayout[0], subgroup[0], 30, "Chamber temp.:", 25.0, numform, "center"))
 
      measLayout[0]->AddFrame(chtemp->outsidebox, f1expandXpad);
 
 
 
   // Check button to toggle live update of histogram (in display canvas)
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *liveDisp -> 1 check button (live histogram update)\n");
 
   liveDisp = new TSubStructure();
 
   checksel = new int;
 
   *checksel = 0;
 
   selnames[0] = "Live histogram ON/OFF";
 
   if(liveDisp->TGCheckList(measLayout[0], subgroup[0], 30, 1, selnames, checksel, "vertical", "center"))
 
      measLayout[0]->AddFrame(liveDisp->outsidebox, f1expandXpad);
 
   delete checksel;
 
 
 
   // Actions for Settings pane //TODO
 
   scansOn->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EnableScan(=0)");
 
   scansOn->widgetChBox[1]->Connect("Clicked()", "TGAppMainFrame", this, "EnableScan(=1)");
 
   scansOn->widgetChBox[2]->Connect("Clicked()", "TGAppMainFrame", this, "EnableScan(=2)");
 
   scansOn->widgetChBox[3]->Connect("Clicked()", "TGAppMainFrame", this, "EnableScan(=3)");
 
   vHardlimit->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "VoltageLimit()");
 
   (vHardlimit->widgetNE[0]->GetNumberEntry())->Connect("ReturnPressed()", "TGAppMainFrame", this, "VoltageLimit()");
 
   posUnits->widgetCB->Connect("Selected(Int_t)", "TGAppMainFrame", this, "ChangeUnits(Int_t)");
 
   rotUnits->widgetCB->Connect("Selected(Int_t)", "TGAppMainFrame", this, "ChangeUnitsRot(Int_t)");
 
   liveDisp->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EnableLiveUpdate()");
 
// Settings pane ---------------------------------------------------------------------------
 
 
 
// Main window -----------------------------------------------------------------------------
 
   TGTab *setTab;
 
 
 
   // Voltage, position and incidence angle tab
 
   subgroup[0] = 2*(measLayout[2]->GetWidth())/7-14;
 
   subgroup[1] = (measLayout[2]->GetHeight())/2-4;
 
   setTab = new TGTab(measLayout[2], subgroup[0], subgroup[1]);
 
 
 
   fT1 = setTab->AddTab("Voltage, position and incidence angle");
 
   fH1 = new TGCompositeFrame(fT1, measLayout[2]->GetWidth(), subgroup[1], kFixedHeight | kHorizontalFrame);
 
 
 
   // Left pane (Bias voltage controls)
 
   fV1 = new TGCompositeFrame(fH1, subgroup[0], subgroup[1], kFixedWidth | kFixedHeight | kVerticalFrame);
 
   fG1 = new TGGroupFrame(fV1, "Bias voltage controls");
 
 
 
   // Output voltage supply channel
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutCh -> Dropdown menu for bias voltage channel\n");
 
   vOutCh = new TSubStructure();
 
   selnames[0] = "U0"; selnames[1] = "U1"; selnames[2] = "U2"; selnames[3] = "U3"; selnames[4] = "U4"; selnames[5] = "U5"; selnames[6] = "U6"; selnames[7] = "U7"; selnames[8] = "U100"; selnames[9] = "U101"; selnames[10] = "U102"; selnames[11] = "U103"; selnames[12] = "U104"; selnames[13] = "U105"; selnames[14] = "U106"; selnames[15] = "U107"; 
 
   sprintf(selected, "U0");
 
   if(vOutCh->TGLabelDrop(fG1, 2*subgroup[0]/3, 30, "Output channel:", 16, selnames, selected))
 
      fG1->AddFrame(vOutCh->outsidebox, f1expandXpad);
 
 
 
   // Output voltage setting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutOpt -> 2 check buttons (negative polarity, ON/OFF toggle switch)\n");
 
   vOutOpt = new TSubStructure();
 
   checksel = new int[2];
 
   checksel[0] = 0; checksel[1] = 0;
 
   selnames[1] = "Output ON/OFF"; selnames[0] = "Negative polarity";
 
   if(vOutOpt->TGCheckList(fG1, 3*subgroup[0]/4, 30, 2, selnames, checksel, "vertical", "center"))
 
      fG1->AddFrame(vOutOpt->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOut -> Number entry for bias voltage\n");
 
   vOut = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2; numform[3] = 2; numform[4] = 0.; numform[5] = vHardlimit->widgetNE[0]->GetNumber();
 
   if(vOut->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Output voltage:", 0.00, numform, "center"))
 
      fG1->AddFrame(vOut->outsidebox, f1expandXpad);
 
   
 
   // Set, get and reset voltage buttons
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutButtons -> 3 buttons (set bias voltage, read current bias voltage, reset voltage output when it gets interlocked)\n");
 
   vOutButtons = new TSubStructure();
 
   selnames[0] = "Set"; selnames[1] = "Get"; selnames[2] = "Reset";
 
   if(vOutButtons->TGMultiButton(fG1, 3*subgroup[0]/4, 30, 3, selnames, "center"))
 
      fG1->AddFrame(vOutButtons->outsidebox, f1expandXpad);
 
 
 
   // Voltage scan controls
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutStart -> Number entry for starting bias voltage\n");
 
   vOutStart = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2;
 
   if(vOutStart->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "V (min):", 0.00, numform, "center"))
 
      fG1->AddFrame(vOutStart->outsidebox, f1expandXpad);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutStop -> Number entry for starting bias voltage\n");
 
   vOutStop = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2;
 
   if(vOutStop->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "V (max):", 0.00, numform, "center"))
 
      fG1->AddFrame(vOutStop->outsidebox, f1expandXpad);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *vOutStep -> Number entry for starting bias voltage\n");
 
   vOutStep = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2;
 
   if(vOutStep->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "V (step):", 0.00, numform, "center"))
 
      fG1->AddFrame(vOutStep->outsidebox, f1expandXpad);
 
 
 
   fV1->AddFrame(fG1, f1expand2d);
 
   // Left pane (Bias voltage controls)
 
   fH1->AddFrame(fV1, f0leftX);
 
 
 
   // Center pane (Table position controls)
 
   subgroup[0] = 3*(measLayout[2]->GetWidth())/7-13;
 
   fV1 = new TGCompositeFrame(fH1, subgroup[0], subgroup[1], kFixedWidth | kFixedHeight | kVerticalFrame);
 
   fG1 = new TGGroupFrame(fV1, "Table position controls");
 
 
 
   // X, Y and Z positions
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *xPos, *yPos, *zPos, *zPosMin, *zPosMax, *zPosStep -> Settings for position and Z axis scan\n");
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   xPos = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 9; numform[3] = 2; numform[4] = -100; numform[5] = 215000;
 
   if(xPos->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "X:", 0, numform, "center"))
 
      fH2->AddFrame(xPos->outsidebox, f0centerX);
 
 
 
   zPosMin = new TSubStructure();
 
   numform[5] = 375000;
 
   if(zPosMin->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Z (min):", 0, numform, "center"))
 
      fH2->AddFrame(zPosMin->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
 
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   yPos = new TSubStructure();
 
   numform[5] = 215000;
 
   if(yPos->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Y:", 0, numform, "center"))
 
      fH2->AddFrame(yPos->outsidebox, f0centerX);
 
 
 
   zPosMax = new TSubStructure();
 
   numform[5] = 375000;
 
   if(zPosMax->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Z (max):", 0, numform, "center"))
 
      fH2->AddFrame(zPosMax->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
   
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   zPos = new TSubStructure();
 
   numform[5] = 375000;
 
   if(zPos->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Z:", 0, numform, "center"))
 
      fH2->AddFrame(zPos->outsidebox, f0centerX);
 
 
 
   zPosStep = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 9;
 
   if(zPosStep->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Z (step):", 0, numform, "center"))
 
      fH2->AddFrame(zPosStep->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
   
 
   // Set, get, home and reset position buttons
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *posButtons -> 4 buttons (set position, read current position, home the motor and reset all three controllers)\n");
 
   posButtons = new TSubStructure();
 
   selnames[0] = "Set"; selnames[1] = "Get"; selnames[2] = "Home"; selnames[3] = "Reset";
 
   if(posButtons->TGMultiButton(fG1, 3*subgroup[0]/4, 30, 4, selnames, "center"))
 
      fG1->AddFrame(posButtons->outsidebox, f1expandXpad);
 
 
 
   // Position scan controls
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *xPosMin, *xPosMax, *xPosStep, *yPosMin, *yPosMax, *yPosStep -> Settings for X and Y axis scans\n");
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   xPosMin = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 9; numform[3] = 2; numform[4] = -100; numform[5] = 215000;
 
   if(xPosMin->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "X (min):", 0, numform, "center"))
 
      fH2->AddFrame(xPosMin->outsidebox, f0centerX);
 
 
 
   yPosMin = new TSubStructure();
 
   if(yPosMin->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Y (min):", 0, numform, "center"))
 
      fH2->AddFrame(yPosMin->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
 
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   xPosMax = new TSubStructure();
 
   if(xPosMax->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "X (max):", 0, numform, "center"))
 
      fH2->AddFrame(xPosMax->outsidebox, f0centerX);
 
 
 
   yPosMax = new TSubStructure();
 
   if(yPosMax->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Y (max):", 0, numform, "center"))
 
      fH2->AddFrame(yPosMax->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
 
 
   fH2 = new TGCompositeFrame(fG1, 3*subgroup[0]/4, 30, kFixedWidth | kHorizontalFrame);
 
   xPosStep = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 9;
 
   if(xPosStep->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "X (step):", 0, numform, "center"))
 
      fH2->AddFrame(xPosStep->outsidebox, f0centerX);
 
 
 
   yPosStep = new TSubStructure();
 
   if(yPosStep->TGLabelNEntry(fH2, 8*subgroup[0]/16, 30, "Y (step):", 0, numform, "center"))
 
      fH2->AddFrame(yPosStep->outsidebox, f0centerX);
 
   fG1->AddFrame(fH2, f1expandXpad);
 
 
 
   fV1->AddFrame(fG1, f1expand2d);
 
   // Center pane (Table position controls)
 
   fH1->AddFrame(fV1, f0leftX);
 
 
 
   // Right pane (Incidence angle controls)
 
   subgroup[0] = 2*(measLayout[2]->GetWidth())/7-14;
 
   fV1 = new TGCompositeFrame(fH1, subgroup[0], subgroup[1], kFixedWidth | kFixedHeight | kVerticalFrame);
 
   fG1 = new TGGroupFrame(fV1, "Incidence angle controls");
 
 
 
   // Rotation positions
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotPos -> Setting for rotation position\n");
 
   rotPos = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2; numform[3] = 2; numform[4] = -180.; numform[5] = 180.;
 
   if(rotPos->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Incidence angle:", 0., numform, "center"))
 
      fG1->AddFrame(rotPos->outsidebox, f1expandXpad);
 
   
 
   // Set, get, home and reset rotation buttons
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotButtons -> 4 buttons (set rotation, read current rotation, home the motor and reset controller)\n");
 
   rotButtons = new TSubStructure();
 
   selnames[0] = "Set"; selnames[1] = "Get"; selnames[2] = "Home"; selnames[3] = "Reset";
 
   if(rotButtons->TGMultiButton(fG1, 3*subgroup[0]/4, 30, 4, selnames, "center"))
 
      fG1->AddFrame(rotButtons->outsidebox, f1expandXpad);
 
 
 
   // Rotation scan controls
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotPosMin -> Number entry for starting angle\n");
 
   rotPosMin = new TSubStructure();
 
   if(rotPosMin->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Angle (min):", 0.0, numform, "center"))
 
      fG1->AddFrame(rotPosMin->outsidebox, f1expandXpad);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotPosMax -> Number entry for finishing angle\n");
 
   rotPosMax = new TSubStructure();
 
   if(rotPosMax->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Angle (max):", 0.0, numform, "center"))
 
      fG1->AddFrame(rotPosMax->outsidebox, f1expandXpad);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *rotPosStep -> Number entry for angle step\n");
 
   rotPosStep = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 1;
 
   if(rotPosStep->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Angle (step):", 0.0, numform, "center"))
 
      fG1->AddFrame(rotPosStep->outsidebox, f1expandXpad);
 
 
 
   fV1->AddFrame(fG1, f1expand2d);
 
   // Right pane (Incidence angle controls)
 
   fH1->AddFrame(fV1, f0leftX);
 
   
 
   fT1->AddFrame(fH1, f1expand2d);
 
 
 
   // Waveform tab
 
   //TODO
 
   measLayout[2]->AddFrame(setTab, f0leftX);
 
 
 
   // Bottom pane (File controls)
 
   subgroup[0] = measLayout[2]->GetWidth()-19;
 
   subgroup[1] = (measLayout[2]->GetHeight())/4-4;
 
   fH1 = new TGCompositeFrame(measLayout[2], subgroup[0], subgroup[1], kFixedWidth | kFixedHeight | kHorizontalFrame);
 
   fG1 = new TGGroupFrame(fH1, "Event/Data file controls");
 
 
 
   // Number of events
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *evtNum -> Number entry for set number of events to acquire\n");
 
   evtNum = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 8; numform[2] = 1;
 
   if(evtNum->TGLabelNEntry(fG1, 3*subgroup[0]/4, 30, "Number of events:", 10000, numform, "left"))
 
      fG1->AddFrame(evtNum->outsidebox, f0leftXpad);
 
 
 
   // Time stamp display
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *timeStamp -> Text entry for timestamp\n");
 
   timeStamp = new TSubStructure();
 
   if(timeStamp->TGLabelTEntry(fG1, 3*subgroup[0]/4, 30, "Time stamp:", "", "oneline"))
 
   {
 
      timeStamp->widgetTE->SetState(kFALSE);
 
      fG1->AddFrame(timeStamp->outsidebox, f1expandXpad);
 
   }
 
 
 
   // Save to file
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fileName -> Text entry for timestamp\n");
 
   fileName = new TSubStructure();
 
   char *cTemp;
 
   cTemp = new char[256];
 
   sprintf(cTemp, "%s/results/test%s", rootdir, histext);
 
   if(fileName->TGLabelTEntryButton(fG1, 3*subgroup[0]/4, 30, "Save to file:", cTemp, "...", "oneline"))
 
   {
 
      fileName->widgetTE->SetState(kFALSE);
 
      fG1->AddFrame(fileName->outsidebox, f1expandXpad);
 
   }
 
 
 
   fH1->AddFrame(fG1, new TGLayoutHints(kLHintsExpandX,8,2,2,2));
 
   // Bottom pane (File controls)
 
   measLayout[2]->AddFrame(fH1, f0leftX);
 
 
 
   // Start acquisition and progress bar
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *measProgress -> Button to start acquiring data and horizontal progress bar\n");
 
   measProgress = new TSubStructure();
 
   if(measProgress->TGButtonProgressTEntry(measLayout[2], 3*subgroup[0]/4, 30, "Start acquisition", "Estimated end time: "))
 
   {
 
      measProgress->widgetTE->SetState(kFALSE);
 
      measLayout[2]->AddFrame(measProgress->outsidebox, f1expandXpad);
 
   }
 
 
 
   // Actions for Main window //TODO
 
   vOutOpt->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "NegativePolarity()");
 
   vOutButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "VoltOut(=0)");
 
   vOutButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "VoltOut(=1)");
 
   vOutButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "VoltOut(=2)");
 
   posButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "PositionSet(=0)");
 
   posButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "PositionSet(=1)");
 
   posButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "PositionSet(=2)");
 
   posButtons->widgetTB[3]->Connect("Clicked()", "TGAppMainFrame", this, "PositionSet(=3)");
 
   rotButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "RotationSet(=0)");
 
   rotButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "RotationSet(=1)");
 
   rotButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "RotationSet(=2)");
 
   rotButtons->widgetTB[3]->Connect("Clicked()", "TGAppMainFrame", this, "RotationSet(=3)");
 
   fileName->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "SaveFile()");
 
   measProgress->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "StartAcq()");
 
   // TODO - Save file
 
 
 
// Main window -----------------------------------------------------------------------------
 
 
 
// Display pane ----------------------------------------------------------------------------
 
   measCanvas = new TRootEmbeddedCanvas("measCanvas",measLayout[1],300,300);
 
   measLayout[1]->AddFrame(measCanvas, f1expand2d);
 
// Display pane ----------------------------------------------------------------------------
 
 
 
// Histogram file selection pane -----------------------------------------------------------
 
   subgroup[0] = (analysisLayout[0]->GetWidth())-4;
 
 
 
   // Open browser for file selection 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *selectDir -> Button to open histogram files\n");
 
   selectDir = new TSubStructure();
 
   if(selectDir->TGLabelButton(analysisLayout[0], 3*subgroup[0]/4, 30, "File selection:", "...", "left"))
 
      analysisLayout[0]->AddFrame(selectDir->outsidebox, f0leftXpad);
 
 
 
   // List view of the opened files
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TGListBox *fileList -> List box for opened histograms\n");
 
   fileList = new TGListBox(analysisLayout[0],1);
 
   fileList->GetVScrollbar();
 
   fileList->Resize(300, (3*analysisLayout[0]->GetHeight()/7)-10 );
 
   analysisLayout[0]->AddFrame(fileList, f1expandXpad);
 
 
 
   fH1 = new TGCompositeFrame(analysisLayout[0], subgroup[0], 30, kHorizontalFrame);
 
   // Multiple file selection toggle
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *multiSelect -> 2 check buttons (enable multiple select, select everything)\n");
 
   multiSelect = new TSubStructure();
 
   checksel = new int[2];
 
   checksel[0] = 0; checksel[1] = 0;
 
   selnames[0] = "Multiple file select"; selnames[1] = "Select all listed files";
 
   if(multiSelect->TGCheckList(fH1, subgroup[0]/2, 30, 2, selnames, checksel, "horizontal", "left"))
 
      fH1->AddFrame(multiSelect->outsidebox, f0leftX);
 
   delete[] checksel;
 
   
 
   // Previous/next controls, clear list and edit header
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fileListCtrl -> Multiple buttons for controlling and displaying the histograms in list box\n");
 
   fileListCtrl = new TSubStructure();
 
   selnames[0] = "<<"; selnames[1] = ">>"; selnames[2] = "Edit header"; selnames[3] = "Clear list";
 
   if(fileListCtrl->TGMultiButton(fH1, subgroup[0]/3, 30, 4, selnames, "right"))
 
      fH1->AddFrame(fileListCtrl->outsidebox, f0rightX);
 
 
 
   analysisLayout[0]->AddFrame(fH1, f1expandXpad);
 
 
 
   // Header information of opened file
 
   fG1 = new TGGroupFrame(analysisLayout[0], "Opened file header information");
 
 
 
   fH1 = new TGHorizontalFrame(fG1, subgroup[0], 30);
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispTime -> Display text Entry for opened histogram (time stamp)\n");
 
   dispTime = new TSubStructure();
 
   if(dispTime->TGLabelTEntry(fH1, 3*subgroup[0]/4-24, 30, "Time:", "", "oneline"))
 
      fH1->AddFrame(dispTime->outsidebox, f0leftXnoleft);
 
   dispTime->widgetTE->SetState(kFALSE);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispBias -> Number entry for opened histogram (bias voltage)\n");
 
   dispBias = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2;
 
   if(dispBias->TGLabelNEntry(fH1, subgroup[0]/4-24, 30, "Bias voltage:", 0.00, numform, "center"))
 
      fH1->AddFrame(dispBias->outsidebox, f0leftX);
 
   dispBias->widgetNE[0]->SetState(kFALSE);
 
   fG1->AddFrame(fH1, f0leftXpad);
 
 
 
   fH1 = new TGHorizontalFrame(fG1, subgroup[0], 30);
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispPos -> Display text Entry for opened histogram (table position)\n");
 
   dispPos = new TSubStructure();
 
   if(dispPos->TGLabelTEntry(fH1, subgroup[0]/2-12, 30, "Position:", "", "oneline"))
 
      fH1->AddFrame(dispPos->outsidebox, f0leftXnoleft);
 
   dispPos->widgetTE->SetState(kFALSE);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispTemp -> Number entry for opened histogram (temperature)\n");
 
   dispTemp = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 1;
 
   if(dispTemp->TGLabelNEntry(fH1, subgroup[0]/4-18, 30, "Temperature:", 0.0, numform, "center"))
 
      fH1->AddFrame(dispTemp->outsidebox, f0leftX);
 
   dispTemp->widgetNE[0]->SetState(kFALSE);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispAngle -> Number entry for opened histogram (incidence angle)\n");
 
   dispAngle = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 7; numform[1] = 2;
 
   if(dispAngle->TGLabelNEntry(fH1, subgroup[0]/4-18, 30, "Angle:", 0.00, numform, "center"))
 
      fH1->AddFrame(dispAngle->outsidebox, f0leftX);
 
   dispAngle->widgetNE[0]->SetState(kFALSE);
 
   fG1->AddFrame(fH1, f0leftXpad);
 
 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *dispLaser -> Display text Entry for opened histogram (laser and additional info)\n");
 
   dispLaser = new TSubStructure();
 
   if(dispLaser->TGLabelTEntry(fG1, 3*subgroup[0]/4, 30, "Laser settings:", "", "oneline"))
 
      fG1->AddFrame(dispLaser->outsidebox, f1expandXpad);
 
   dispLaser->widgetTE->SetState(kFALSE);
 
 
 
   analysisLayout[0]->AddFrame(fG1, f1expandX);
 
 
 
   // Actions for histogram file selection pane
 
   selectDir->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "SelectDirectory()");
 
   multiSelect->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "ListMultiSelect(=0)");
 
   multiSelect->widgetChBox[1]->Connect("Clicked()", "TGAppMainFrame", this, "ListMultiSelect(=1)");
 
   fileList->Connect("DoubleClicked(Int_t)", "TGAppMainFrame", this, "FileListNavigation(Int_t)");
 
   fileListCtrl->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "FileListNavigation(=-2)");
 
   fileListCtrl->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "FileListNavigation(=-3)");
 
   fileListCtrl->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "HeaderEdit()");
 
   fileListCtrl->widgetTB[3]->Connect("Clicked()", "TGAppMainFrame", this, "ClearHistogramList()");
 
 
 
// Histogram file selection pane -----------------------------------------------------------
 
 
 
// Analysis pane ---------------------------------------------------------------------------
 
   fH1 = new TGCompositeFrame(analysisLayout[1], analysisLayout[1]->GetWidth(), 30, kHorizontalFrame);
 
 
 
   subgroup[0] = (analysisLayout[1]->GetWidth())/2-4;
 
   subgroup[1] = (analysisLayout[1]->GetHeight())-4;
 
 
 
   analTab = new TGTab(fH1, subgroup[0], subgroup[1]);
 
 
 
   // Analysis tabs
 
   // Integrate spectrum tab
 
   fT1 = analTab->AddTab("Integ. spectrum");
 
   fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame | kFixedWidth | kFixedHeight);
 
 
 
   // Check buttons to toggle direction of integration and normalization
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *intSpect -> 3 check buttons (direction of integration, normalization)\n");
 
   intSpect = new TSubStructure();
 
   checksel = new int[3];
 
   checksel[0] = 0; checksel[1] = 0; checksel[2] = 1;
 
   selnames[0] = "X direction (for edge scans)";
 
   selnames[1] = "Y direction (for edge scans)";
 
   selnames[2] = "Normalize to number of events";
 
   if(intSpect->TGCheckList(fV1, subgroup[0], 30, 3, selnames, checksel, "vertical", "center"))
 
      fV1->AddFrame(intSpect->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Values for 2D plot pixel resolution
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *resol2d -> Pixel resolution for 2D plots\n");
 
   resol2d = new TSubStructure();
 
   for(int i = 0; i < 6; i++) { numform[i] = 0; numform2[i] = 0; }
 
   numform[0] = 5; numform2[0] = 5; numform[2] = 2; numform2[2] = 2;
 
   if(resol2d->TGLabelDoubleNEntry(fV1, subgroup[0], 30, "2D plot pixel resolution (X, Y):", 40, numform, 40, numform2, "center"))
 
      fV1->AddFrame(resol2d->outsidebox, f1expandX);
 
 
 
   // Start integrating or set its defaults
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *intSpectButtons -> 2 buttons for integrating spectra\n");
 
   intSpectButtons = new TSubStructure();
 
   selnames[0] = "Start"; selnames[1] = "Start and edit"; selnames[2] = "Default values";
 
   if(intSpectButtons->TGMultiButton(fV1, subgroup[0], 30, 3, selnames, "center"))
 
      fV1->AddFrame(intSpectButtons->outsidebox, f1expandX);
 
 
 
   fT1->AddFrame(fV1, f1expandX);
 
 
 
   // Relative photon detection efficiency tab (PDE)
 
   fT1 = analTab->AddTab("Relative PDE");
 
   fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame | kFixedWidth | kFixedHeight);
 
 
 
   // Check button to toggle normalization
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *relPde -> 2 check buttons (relative pde, normalization)\n");
 
   relPde = new TSubStructure();
 
   checksel = new int;
 
   *checksel = 1;
 
   selnames[0] = "Normalize to number of events";
 
   if(relPde->TGCheckList(fV1, subgroup[0], 30, 1, selnames, checksel, "vertical", "center"))
 
      fV1->AddFrame(relPde->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Calculate relative PDE using the middle of the pedestal peak
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *midPeak -> Calculate the relative PDE, by setting the middle of the pedestal peak.\n");
 
   midPeak = new TSubStructure();
 
   checksel = new int;
 
   *checksel = 0;
 
   selnames[0] = "Pedestal entries end at middle of ped. peak";
 
   if(midPeak->TGCheckList(fV1, subgroup[0], 30, 1, selnames, checksel, "vertical", "center"))
 
      fV1->AddFrame(midPeak->outsidebox, f1expandX);
 
   delete checksel;
 
 
 
   // Select the dark run 
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *darkRun -> Button to open histogram files of a dark run\n");
 
   darkRun = new TSubStructure();
 
   if(darkRun->TGLabelTEntryButton(fV1, 3*subgroup[0]/4, 30, "Select dark run histogram:", "", "...", "oneline"))
 
   {
 
      darkRun->widgetTE->SetState(kFALSE);
 
      fV1->AddFrame(darkRun->outsidebox, f1expandXpad);
 
   }
 
 
 
   // Select the offset of 0 angle
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *zeroAngle -> Set the offset for 0 angle\n");
 
   zeroAngle = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 2;
 
   if(zeroAngle->TGLabelNEntry(fV1, 3*subgroup[0]/4, 30, "Offset to zero angle:", 0., numform, "center"))
 
      fV1->AddFrame(zeroAngle->outsidebox, f0centerX);
 
 
 
   // Start calculating the PDE or set its defaults
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *relPdeButtons -> 2 buttons for calculating the relative PDE\n");
 
   relPdeButtons = new TSubStructure();
 
   selnames[0] = "Start"; selnames[1] = "Start and edit"; selnames[2] = "Default values";
 
   if(relPdeButtons->TGMultiButton(fV1, subgroup[0], 30, 3, selnames, "center"))
 
      fV1->AddFrame(relPdeButtons->outsidebox, f1expandX);
 
 
 
   fT1->AddFrame(fV1, f1expandX);
 
 
 
   // Breaktown voltage tab
 
   fT1 = analTab->AddTab("Breakdown voltage");
 
   fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame | kFixedWidth | kFixedHeight);
 
 
 
   // Select the minumum number of peaks for fitting to be initiated
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *minPeak -> Minimum number of peaks to fit for peak fitting\n");
 
   minPeak = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 4; numform[2] = 1; numform[3] = 2; numform[4] = 1; numform[5] = 20;
 
   if(minPeak->TGLabelNEntry(fV1, subgroup[0], 30, "Min. nr. of peaks:", 2, numform, "center"))
 
      fV1->AddFrame(minPeak->outsidebox, f0centerX);
 
 
 
   // Select which separation to use
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *peakSepCalc -> Make the separation between the selected peaks\n");
 
   peakSepCalc = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 4; numform[2] = 1; numform[3] = 2; numform[4] = 1; numform[5] = 3;
 
   if(peakSepCalc->TGLabelNEntry(fV1, subgroup[0], 30, "Calculate peak separation between N pe and N+1 pe peaks:", 1, numform, "center"))
 
      fV1->AddFrame(peakSepCalc->outsidebox, f0centerX);
 
 
 
   // Start calculating the breakdown voltage or set its defaults
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *brDownButtons -> 2 buttons for calculating the breakdown voltage\n");
 
   brDownButtons = new TSubStructure();
 
   selnames[0] = "Start"; selnames[1] = "Start and edit"; selnames[2] = "Default values";
 
   if(brDownButtons->TGMultiButton(fV1, subgroup[0], 30, 3, selnames, "center"))
 
      fV1->AddFrame(brDownButtons->outsidebox, f1expandX);
 
 
 
   fT1->AddFrame(fV1, f1expandX);
 
 
 
   // Surface scan tab
 
   fT1 = analTab->AddTab("Surface scan");
 
   fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame | kFixedWidth | kFixedHeight);
 
 
 
   // Check button to toggle normalization
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *surfScanOpt -> 2 check buttons (normalization, zero bottom-left corner)\n");
 
   surfScanOpt = new TSubStructure();
 
   checksel = new int[2];
 
   checksel[0] = 1; checksel[1] = 0;
 
   selnames[0] = "Normalize to number of events"; selnames[1] = "Zero the bottom left corner";
 
   if(surfScanOpt->TGCheckList(fV1, subgroup[0], 30, 2, selnames, checksel, "vertical", "center"))
 
      fV1->AddFrame(surfScanOpt->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Values for 2D plot pixel resolution
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *resolSurf -> Pixel resolution for surface plots\n");
 
   resolSurf = new TSubStructure();
 
   for(int i = 0; i < 6; i++) { numform[i] = 0; numform2[i] = 0; }
 
   numform[0] = 5; numform2[0] = 5; numform[2] = 2; numform2[2] = 2;
 
   if(resolSurf->TGLabelDoubleNEntry(fV1, subgroup[0], 30, "Surface plot pixel resolution (X, Y):", 40, numform, 40, numform2, "center"))
 
      fV1->AddFrame(resolSurf->outsidebox, f1expandX);
 
 
 
   // Start calculating the breakdown voltage or set its defaults
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *surfButtons -> 2 buttons for calculating the surface scan\n");
 
   surfButtons = new TSubStructure();
 
   selnames[0] = "Start"; selnames[1] = "Start and edit"; selnames[2] = "Default values";
 
   if(surfButtons->TGMultiButton(fV1, subgroup[0], 30, 3, selnames, "center"))
 
      fV1->AddFrame(surfButtons->outsidebox, f1expandX);
 
 
 
   fT1->AddFrame(fV1, f1expandX);
 
 
 
   // Timing tab
 
   fT1 = analTab->AddTab("Timing");
 
   fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame | kFixedWidth | kFixedHeight);
 
 
 
   fT1->AddFrame(fV1, f1expandX);
 
 
 
   // Analysis tabs
 
   fH1->AddFrame(analTab, /*f1expandX*/f0leftX);
 
 
 
   // Peak fitting settings
 
   fV1 = new TGCompositeFrame(fH1, subgroup[0], 30, kVerticalFrame);
 
 
 
   fH2 = new TGCompositeFrame(fV1, subgroup[0], 30, kHorizontalFrame);
 
   // Select the sigma for peak fitting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fitSigma -> Sigma for peak fitting\n");
 
   fitSigma = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 3; numform[2] = 2;
 
   if(fitSigma->TGLabelNEntry(fH2, subgroup[0]/2, 30, "Peak sigma:", 1.2, numform, "center"))
 
      fH2->AddFrame(fitSigma->outsidebox, f0centerX);
 
   // Select the signal to noise treshold for peak fitting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fitTresh -> S/N ratio for peak fitting\n");
 
   fitTresh = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 3; numform[2] = 2;
 
   if(fitTresh->TGLabelNEntry(fH2, subgroup[0]/2, 30, "S/N ratio:", 0.005, numform, "center"))
 
      fH2->AddFrame(fitTresh->outsidebox, f0centerX);
 
   fV1->AddFrame(fH2, f1expandXpad);
 
 
 
   // Select the background interpolation for peak fitting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fitInter -> Background interpolation for peak fitting\n");
 
   fitInter = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 5; numform[2] = 1;
 
   if(fitInter->TGLabelNEntry(fV1, subgroup[0], 30, "Back. interpolation:", 7, numform, "center"))
 
      fV1->AddFrame(fitInter->outsidebox, f0centerX);
 
 
 
   // Select the ADC offset
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *adcOffset -> Select the offset for all ADC spectra\n");
 
   adcOffset = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 2;
 
   if(adcOffset->TGLabelNEntry(fV1, subgroup[0], 30, "ADC spectrum offset:", 0.00, numform, "center"))
 
      fV1->AddFrame(adcOffset->outsidebox, f0centerX);
 
 
 
   // Select the acceptable error for peak fitting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *accError -> Acceptable peak fitting error for peak fitting\n");
 
   accError = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 2; numform[2] = 1;
 
   if(accError->TGLabelNEntry(fV1, subgroup[0], 30, "Max. peak fit error:", 0.15, numform, "center"))
 
      fV1->AddFrame(accError->outsidebox, f0centerX);
 
 
 
   // Select the pedestal lower limit for peak fitting
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *pedesLow -> Lower ADC limit of pedestal peak for peak fitting\n");
 
   pedesLow = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 6; numform[1] = 1; numform[2] = 2;
 
   if(pedesLow->TGLabelNEntry(fV1, subgroup[0], 30, "Pedestal low limit:", 0.0, numform, "center"))
 
      fV1->AddFrame(pedesLow->outsidebox, f0centerX);
 
 
 
   // Check buttons to toggle subtracting the background and exporting the fitted spectra
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *fitChecks -> 2 check buttons (substracting background, exporting fitted spectra)\n");
 
   fitChecks = new TSubStructure();
 
   checksel = new int[2];
 
   checksel[0] = 1; checksel[1] = 0;
 
   selnames[0] = "Backround subtraction ON/OFF";
 
   selnames[1] = "Export fitted spectra ON/OFF";
 
   if(fitChecks->TGCheckList(fV1, subgroup[0], 30, 2, selnames, checksel, "vertical", "center"))
 
      fV1->AddFrame(fitChecks->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Analysis progress bar
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *analysisProgress -> Horizontal progress bar for analysis\n");
 
   analysisProgress = new TSubStructure();
 
   if(analysisProgress->TGLabelProgress(fV1, subgroup[0], 30, "Current analysis:"))
 
      fV1->AddFrame(analysisProgress->outsidebox, f1expandXpad);
 
 
 
   fH1->AddFrame(fV1, f1expandX);
 
 
 
   analysisLayout[1]->AddFrame(fH1, f1expandXpad);
 
 
 
   // Actions for analysis pane
 
   intSpectButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=0)");
 
   intSpectButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=1)");
 
   intSpectButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisDefaults()");
 
 
 
   relPdeButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=0)");
 
   relPdeButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=1)");
 
   relPdeButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisDefaults()");
 
   darkRun->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "SelectDarkHist()");
 
 
 
   brDownButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=0)");
 
   brDownButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=1)");
 
   brDownButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisDefaults()");
 
 
 
   surfButtons->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=0)");
 
   surfButtons->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisHandle(=1)");
 
   surfButtons->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "AnalysisDefaults()");
 
// Analysis pane ---------------------------------------------------------------------------
 
 
 
// Histogram pane --------------------------------------------------------------------------
 
   analysisCanvas = new TRootEmbeddedCanvas("analysisCanvas",analysisLayout[2],300,300);
 
   analysisLayout[2]->AddFrame(analysisCanvas, f1expand2d);
 
   analysisCanvas->GetCanvas()->SetGrid();
 
// Histogram pane --------------------------------------------------------------------------
 
 
 
// Histogram controls pane -----------------------------------------------------------------
 
   subgroup[0] = (analysisLayout[3]->GetWidth())-4;
 
 
 
   // Control for histogram X range
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *adcRange -> Range for ADC histogram\n");
 
   adcRange = new TSubStructure();
 
   for(int i = 0; i < 6; i++) { numform[i] = 0; numform2[i] = 0; }
 
   numform[0] = 6; numform2[0] = 6;
 
   if(adcRange->TGLabelDoubleNEntry(analysisLayout[3], subgroup[0], 30, "ADC range (min, max):", 0, numform, 0, numform2, "center"))
 
      analysisLayout[3]->AddFrame(adcRange->outsidebox, f1expandXpad);
 
 
 
   // TDC window for getting data
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *tdcRange -> Range for TDC histogram\n");
 
   tdcRange = new TSubStructure();
 
   for(int i = 0; i < 6; i++) { numform[i] = 0; numform2[i] = 0; }
 
   numform[0] = 8; numform[1] = 2; numform2[0] = 8; numform2[1] = 2;
 
   if(tdcRange->TGLabelDoubleNEntry(analysisLayout[3], subgroup[0], 30, "TDC range (min, max):", -0.5, numform, 221.8, numform2, "center"))
 
      analysisLayout[3]->AddFrame(tdcRange->outsidebox, f1expandXpad);
 
 
 
   // Y axis range settings
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *yRange -> Y axis range\n");
 
   yRange = new TSubStructure();
 
   for(int i = 0; i < 6; i++) { numform[i] = 0; numform2[i] = 0; }
 
   numform[0] = 8; numform[1] = 1; numform2[0] = 8; numform2[1] = 1;
 
   if(yRange->TGLabelDoubleNEntry(analysisLayout[3], subgroup[0], 30, "Y range (min, max):", 0, numform, 0, numform2, "center"))
 
      analysisLayout[3]->AddFrame(yRange->outsidebox, f1expandXpad);
 
 
 
   fH1 = new TGHorizontalFrame(analysisLayout[3], subgroup[0], 30);
 
   // Select the channel to display
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *selectCh -> Channel to display\n");
 
   selectCh = new TSubStructure();
 
   for(int i = 0; i < 6; i++) numform[i] = 0;
 
   numform[0] = 3; numform[2] = 2; numform[3] = 2; numform[4] = 0; numform[5] = 7; // TODO - ko odprem file, nastavi zgornjo limito
 
   if(selectCh->TGLabelNEntry(fH1, subgroup[0]/2-24, 30, "Display channel:", 0, numform, "center"))
 
      fH1->AddFrame(selectCh->outsidebox, f0leftX);
 
   selChannel = -1;
 
 
 
   // Change plot type
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *plotType -> 3 buttons for selecting the plot type (ADC, TDC, ADC/TDC)\n");
 
   plotType = new TSubStructure();
 
   selnames[0] = "ADC"; selnames[1] = "TDC"; selnames[2] = "ADC/TDC";
 
   if(plotType->TGMultiButton(fH1, subgroup[0]/2-24, 30, 3, selnames, "center"))
 
      fH1->AddFrame(plotType->outsidebox, f1expandX);
 
   analysisLayout[3]->AddFrame(fH1, f1expandXpad);
 
 
 
   // Check button to toggle logarithmic scale
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *histOpt -> 2 check buttons (histogram logarithmic scale, clean plots)\n");
 
   histOpt = new TSubStructure();
 
   checksel = new int[2];
 
   checksel[0] = 0; checksel[1] = 1;
 
   selnames[0] = "Logarithmic scale ON/OFF";
 
   selnames[1] = "Clean plots ON/OFF";
 
   if(histOpt->TGCheckList(analysisLayout[3], subgroup[0], 30, 2, selnames, checksel, "vertical", "center"))
 
      analysisLayout[3]->AddFrame(histOpt->outsidebox, f1expandXpad);
 
   delete[] checksel;
 
 
 
   // Export the selected files
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *exportHist -> Button to export current histogram\n");
 
   exportHist = new TSubStructure();
 
   if(exportHist->TGLabelButton(analysisLayout[3], subgroup[0], 30, "Export selected histograms:", "Export", "center"))
 
      analysisLayout[3]->AddFrame(exportHist->outsidebox, f1expandXpad);
 
 
 
   // Edit the selected histograms
 
   if(DBGSIG > 1) printf("AppLayout(): Creating TSubStructure *editSelHist -> Button to additionally edit the selected histograms (make a canvas clone in a new tab)\n");
 
   editSelHist = new TSubStructure();
 
   if(editSelHist->TGLabelButton(analysisLayout[3], subgroup[0], 30, "Edit selected histograms:", "Edit", "center"))
 
      analysisLayout[3]->AddFrame(editSelHist->outsidebox, f1expandXpad);
 
 
 
   // Actions for histogram controls pane //TODO
 
   for(int i = 0; i < 2; i++)
 
   {
 
      adcRange->widgetNE[i]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
//      (adcRange->widgetNE[i]->GetNumberEntry())->Connect("ReturnPressed()", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
      tdcRange->widgetNE[i]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
//      (tdcRange->widgetNE[i]->GetNumberEntry())->Connect("ReturnPressed()", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
      yRange->widgetNE[i]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
//      (yRange->widgetNE[i]->GetNumberEntry())->Connect("ReturnPressed()", "TGAppMainFrame", this, "UpdateHistogram(=0)");
 
   }
 
   plotType->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "ChangeHisttype(=0)");
 
   plotType->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, "ChangeHisttype(=1)");
 
   plotType->widgetTB[2]->Connect("Clicked()", "TGAppMainFrame", this, "ChangeHisttype(=2)");
 
   selectCh->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "UpdateHistogram(=2)");
 
   selectCh->widgetNE[0]->Connect("ValueChanged(Long_t)", "TGAppMainFrame", this, "UpdateHistogram(=2)");
 
   (selectCh->widgetNE[0]->GetNumberEntry())->Connect("ReturnPressed()", "TGAppMainFrame", this, "UpdateHistogram(=2)");
 
   histOpt->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "HistogramOptions(=0)");
 
   histOpt->widgetChBox[1]->Connect("Clicked()", "TGAppMainFrame", this, "HistogramOptions(=1)");
 
   exportHist->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "UpdateHistogram(=1)");
 
 
 
// Histogram controls pane -----------------------------------------------------------------
 
}
 
 
 
// -------------------------------------------------------------------
 
 
 
// Closing main window and checking about information ----------------
 
 
 
void TGAppMainFrame::CloseWindow()
 
{
 
   gApplication->Terminate(0);
 
}
 
 
 
Bool_t TGAppMainFrame::About()
 
{
 
   int ret = 0;
 
 
 
   new TGMsgBox(gClient->GetRoot(), fMain, fMain->GetWindowName(), "Software for SiPM characterization with\nCAMAC, scope, bias voltage and table position support\n\nCreated by Gasper Kukec Mezek (gasper.kukec@ung.si),\nUpdated on July 17th 2015", kMBIconQuestion, kMBClose, &ret);
 
 
 
   return kFALSE;
 
}
 
 
 
// -------------------------------------------------------------------
 
 
 
// Subwindow constructor (+layout) and close subwindow ---------------
 
/*
 
TGMdiSubwindow::TGMdiSubwindow(TGMdiMainFrame *main, int w, int h)
 
{
 
   // Create a new subwindow
 
   fMdiFrame = new TGMdiFrame(main, w, h);
 
   fMdiFrame->Connect("CloseWindow()", "TGMdiSubwindow", this, "CloseWindow(=0)");
 
   fMdiFrame->DontCallClose();
 
}
 
 
 
Bool_t TGMdiSubwindow::CloseWindow(int layoutchange)
 
{
 
   int ret = 0;
 
 
 
   if(layoutchange == 0)
 
      new TGMsgBox(gClient->GetRoot(), fMdiFrame, fMdiFrame->GetWindowName(), "Really want to close the window?", kMBIconExclamation, kMBYes | kMBNo, &ret);
 
   else if(layoutchange == 1)
 
      ret = kMBYes;
 
   if(ret == kMBYes)
 
   {
 
      if(strcmp("Information window", fMdiFrame->GetWindowName()) == 0)
 
         id = -1;
 
      return fMdiFrame->CloseWindow();
 
   }
 
 
 
   return kFALSE;
 
}*/
 
 
 
// -------------------------------------------------------------------
 
 
 
// Main function -----------------------------------------------------
 
void root_app()
 
{
 
   if(DBGSIG > 1)
 
   {
 
      printf("root_app(): Starting objects\n");
 
      gObjectTable->Print();
 
   }
 
 
 
   int winWidth = 1240;
 
   int winHeight = 800;
 
   layoutMainWindow(&winWidth, &winHeight);
 
 
 
   new TGAppMainFrame(gClient->GetRoot(), winWidth, winHeight);
 
}
 
 
 
int main(int argc, char **argv)
 
{
 
   if(DBGSIG > 1)
 
   {
 
      printf("main(): Starting objects\n");
 
      gObjectTable->Print();
 
   }
 
 
 
   TApplication theApp("MdiTest", &argc, argv);
 
   root_app();
 
   theApp.Run();
 
 
 
   return 0;
 
}
 
 
 
// -------------------------------------------------------------------