Subversion Repositories f9daq

Rev

Rev 146 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "../include/sipmscan.h"
#include "../include/workstation.h"

#include <stdio.h>
#include <stdlib.h>

// Layout hints
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 *f0leftXnopad = new TGLayoutHints(kLHintsLeft | kLHintsTop,0,0,0,0);
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);

// Edit file window ---------------------------------------------------

// Open a new tab for editing datafile headers
void TGAppMainFrame::HeaderEditTab(TGTab *mainTab, bool create, int *tabid)
{
   unsigned int nrfiles;
   ULong_t rcolor, bcolor;
   gClient->GetColorByName("red", rcolor);
   gClient->GetColorByName("black", bcolor);

   if(create)
   {
      TGCompositeFrame *fH1, *fV1;
      TGHorizontalFrame *fTitle;
      TGGroupFrame *fG1;
      TGLabel *lab;
   
      int startTab = mainTab->GetCurrent();
      int newTab = mainTab->GetNumberOfTabs();
      if(DBGSIG > 1) printf("HeaderEditTab(): Current tab = %d, Nr. of tabs = %d\n", startTab, newTab );

      double numform[6];
     
      int subgroup[2];
      subgroup[0] = mainTab->GetWidth()-10;
      subgroup[1] = mainTab->GetHeight()-10;
   
      TGCompositeFrame *fT1;
      fT1 = fTab->AddTab("File header editor");

      // Title label
      fTitle = new TGHorizontalFrame(fT1, 100, 25, kFixedHeight | kSunkenFrame);
      TGTitleLabel(fT1, fTitle, "File header editor", (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
      fT1->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop));
   
      // List view of files that we will edit
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TGListBox *editList -> List box for editing files\n");
      editList = new TGListBox(fT1,1);
      editList->GetVScrollbar();
      editList->Resize(300, (3*subgroup[1]/7)-10 );
      fT1->AddFrame(editList, f1expandXpad);

      editList->SetMultipleSelections((multiSelect->widgetChBox[0]->IsOn()));
   
      // Copy the file list from the analysis tab for clearer view
      nrfiles = fileList->GetNumberOfEntries();
      printf("Nr. files = %d\n", nrfiles);
      for(int i = 0; i < nrfiles; i++)
         editList->AddEntry(fileList->GetEntry(i)->GetTitle(), i);

      fH1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kHorizontalFrame);
      fV1 = new TGCompositeFrame(fH1, subgroup[0]/2, subgroup[1], kVerticalFrame);
      // Time stamp display
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *timeEditDisplay -> Display text Entry (time stamp)\n");
      timeEditDisplay = new TSubStructure();
      if(timeEditDisplay->TGLabelTEntry(fV1, subgroup[0]/2-4, 30, "Time of measurement:", "", "oneline"))
         fV1->AddFrame(timeEditDisplay->outsidebox, f0leftXpad);
      timeEditDisplay->widgetTE->SetState(kFALSE);

      // Bias voltage edit
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *biasEdit -> Number entry for bias voltage edit\n");
      biasEdit = new TSubStructure();
      for(int i = 0; i < 6; i++) numform[i] = 0;
      numform[0] = 7; numform[1] = 2;
      if(biasEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Bias voltage edit:", 0, 0.00, numform, "left"))
         fV1->AddFrame(biasEdit->outsidebox, f0leftXpad);

      // Position edits
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *xPosEdit, *yPosEdit, *zPosEdit -> Number entries for position edit\n");
      xPosEdit = 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(xPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "X position edit:", 0, 0, numform, "left"))
         fV1->AddFrame(xPosEdit->outsidebox, f0leftXpad);

      yPosEdit = new TSubStructure();
      if(yPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Y position edit:", 0, 0, numform, "left"))
         fV1->AddFrame(yPosEdit->outsidebox, f0leftXpad);

      zPosEdit = new TSubStructure();
      numform[5] = 375000;
      if(zPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Z position edit:", 0, 0, numform, "left"))
         fV1->AddFrame(zPosEdit->outsidebox, f0leftXpad);

      // Temperature edit
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *tempEdit -> Number entry for temperature edit\n");
      tempEdit = new TSubStructure();
      for(int i = 0; i < 6; i++) numform[i] = 0;
      numform[0] = 6; numform[1] = 1;
      if(tempEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Temperature edit:", 0, 0.0, numform, "left"))
         fV1->AddFrame(tempEdit->outsidebox, f0leftXpad);

      // Angle edit
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *angleEdit -> Number entry for angle edit\n");
      angleEdit = new TSubStructure();
      for(int i = 0; i < 6; i++) numform[i] = 0;
      numform[0] = 7; numform[1] = 2;
      if(angleEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Incidence angle edit:", 0, 0.00, numform, "left"))
         fV1->AddFrame(angleEdit->outsidebox, f0leftXpad);

      // Laser settings edit
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *laserEdit -> Display text Entry for laser edit\n");
      laserEdit = new TSubStructure();
      if(laserEdit->TGCheckTEntry(fV1, subgroup[0]/2, 30, "Laser settings edit:", 0, "", "oneline"))
         fV1->AddFrame(laserEdit->outsidebox, f0leftXpad);

      // Edit and close buttons
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *editHead -> 2 buttons for either editing the head or closing the tab\n");
      editHead = new TSubStructure();
      const char *selnames[512] = {"Edit header","Close"};
      if(editHead->TGMultiButton(fV1, subgroup[0]/2, 30, 2, selnames, "center"))
         fV1->AddFrame(editHead->outsidebox, f0leftXpad);
      fH1->AddFrame(fV1, f0leftXnopad);

      fV1 = new TGCompositeFrame(fH1, subgroup[0]/2, subgroup[1], kVerticalFrame);
      // Multiple file select
      if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *editMulti -> 1 Check button to set multi select or not\n");
      editMulti = new TSubStructure();
      int *checksel;
      checksel = new int;
      *checksel = multiSelect->widgetChBox[0]->IsDown();
      selnames[0] = "Select multiple files";
      if(editMulti->TGCheckList(fV1, subgroup[0]/2, 30, 1, selnames, checksel, "vertical", "center"))
         fV1->AddFrame(editMulti->outsidebox, f0centerX);

      // Warning information
      fG1 = new TGGroupFrame(fV1, "Warnings");
      lab = new TGLabel(fG1, "Note: Tick checkbox in front of each header information you wish to change\n(to avoid any unwanted changes, they are unticked by default).");
      fG1->AddFrame(lab, f0leftXpad);
      lab = new TGLabel(fG1, "Note: When selecting files in the list, the entry fields will update accordingly\nwith information from the selected file (only for those where check\nboxes are not turned on).");
      fG1->AddFrame(lab, f0leftXpad);
      lab = new TGLabel(fG1, "Warning: Using button \"Edit header\" will edit headers in all files currently\nselected in the above selection list.");
      lab->SetTextColor(rcolor);
      fG1->AddFrame(lab, f0leftXpad);
      if((editMulti->widgetChBox[0]->IsOn()))
      {
         selectWarn = new TGLabel(fG1, "Warning: Multiple files selected!");
         selectWarn->SetTextColor(rcolor);
         fG1->AddFrame(selectWarn, f0leftXpad);
      }
      else
      {
         selectWarn = new TGLabel(fG1, "Note: Single file selected.      ");
         selectWarn->SetTextColor(bcolor);
         fG1->AddFrame(selectWarn, f0leftXpad);
      }
      fV1->AddFrame(fG1, f0centerX);
      fH1->AddFrame(fV1, f0centerX);

      // Actions for header editor
      editList->Connect("Selected(Int_t)", "TGAppMainFrame", this, "ShowHeaderEdit(Int_t)");
      biasEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=1)");
      xPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=2)");
      yPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=3)");
      zPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=4)");
      tempEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=5)");
      angleEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=6)");
      laserEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=7)");
      editMulti->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "SetWarnings()");
      editHead->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "StartHeaderEdit()");
      char cTemp[512];
      sprintf(cTemp, "CloseEditTab(=%d)", newTab*100+startTab);
      editHead->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, cTemp);

      fT1->AddFrame(fH1, f1expand2d);
      fMain->MapSubwindows();
      fMain->MapWindow();
      fMain->Layout();

      // Initialize the values
      for(int i = 0; i < 8; i++)
         EditTickToggle(i);

      // Switch to new tab
      fTab->SetTab(newTab);

      if(DBGSIG > 1)
      {
         printf("HeaderEditTab(): New tab objects (Edit Header)\n");
         gObjectTable->Print();
      }
   }
   else
   {
      if(multiSelect->widgetChBox[0]->IsDown())
         editMulti->widgetChBox[0]->SetState(kButtonDown);
      else
         editMulti->widgetChBox[0]->SetState(kButtonUp);

      editList->SetMultipleSelections((editMulti->widgetChBox[0]->IsDown()));

      // Recopy the file list from the analysis tab
      nrfiles = fileList->GetNumberOfEntries();
      printf("Nr. files = %d\n", nrfiles);
      for(int i = 0; i < nrfiles; i++)
         editList->AddEntry(fileList->GetEntry(i)->GetTitle(), i);

      SetWarnings();

      // Switch to new tab
      fTab->SetTab(*tabid);
   }
}

// Change the warning when selecting multiple files
void TGAppMainFrame::SetWarnings()
{
   ULong_t rcolor, bcolor;
   gClient->GetColorByName("red", rcolor);
   gClient->GetColorByName("black", bcolor);

   editList->SetMultipleSelections((editMulti->widgetChBox[0]->IsDown()));

   // Set the warnings
   if(editMulti->widgetChBox[0]->IsDown())
   {
      selectWarn->SetText("Warning: Multiple files selected!");
      selectWarn->SetTextColor(rcolor);
      selectWarn->SetWrapLength(-1);
   }
   else
   {
      selectWarn->SetText("Note: Single file selected.      ");
      selectWarn->SetTextColor(bcolor);
      selectWarn->SetWrapLength(-1);
   }
}

// Actions for editing the header
void TGAppMainFrame::EditTickToggle(int type)
{
   // Toggle the edit possibility for header entries

   // Bias voltage
   if(type == 1)
   {
      if(biasEdit->widgetChBox[0]->IsDown()) biasEdit->widgetNE[0]->SetState(kTRUE);
      else biasEdit->widgetNE[0]->SetState(kFALSE);
   }
   // X position
   else if(type == 2)
   {
      if(xPosEdit->widgetChBox[0]->IsDown()) xPosEdit->widgetNE[0]->SetState(kTRUE);
      else xPosEdit->widgetNE[0]->SetState(kFALSE);
   }
   // Y position
   else if(type == 3)
   {
      if(yPosEdit->widgetChBox[0]->IsDown()) yPosEdit->widgetNE[0]->SetState(kTRUE);
      else yPosEdit->widgetNE[0]->SetState(kFALSE);
   }
   // Z position
   else if(type == 4)
   {
      if(zPosEdit->widgetChBox[0]->IsDown()) zPosEdit->widgetNE[0]->SetState(kTRUE);
      else zPosEdit->widgetNE[0]->SetState(kFALSE);
   }
   // Temperature
   else if(type == 5)
   {
      if(tempEdit->widgetChBox[0]->IsDown()) tempEdit->widgetNE[0]->SetState(kTRUE);
      else tempEdit->widgetNE[0]->SetState(kFALSE);
   }
   // Angle
   else if(type == 6)
   {
      if(angleEdit->widgetChBox[0]->IsDown()) angleEdit->widgetNE[0]->SetState(kTRUE);
      else angleEdit->widgetNE[0]->SetState(kFALSE);
   }
   // Laser info
   else if(type == 7)
   {
      if(laserEdit->widgetChBox[0]->IsDown()) laserEdit->widgetTE->SetState(kTRUE);
      else laserEdit->widgetTE->SetState(kFALSE);
   }
}

void TGAppMainFrame::StartHeaderEdit()
{
   unsigned int nrfiles = editList->GetNumberOfEntries();
   TList *files;
   // Changelist: Bias, X, Y, Z Positions, Temperature, Angle, Laser info
   bool changelist[] = { biasEdit->widgetChBox[0]->IsDown(), xPosEdit->widgetChBox[0]->IsDown(), yPosEdit->widgetChBox[0]->IsDown(), zPosEdit->widgetChBox[0]->IsDown(), tempEdit->widgetChBox[0]->IsDown(), angleEdit->widgetChBox[0]->IsDown(), laserEdit->widgetChBox[0]->IsDown() };

   if( nrfiles > 0 )
   {
      // check the selected file/files and return its name/their names
      files = new TList();
      editList->GetSelectedEntries(files);
      if(files)
      {
         for(int i = 0; i < (int)nrfiles; i++)
         {
            if(files->At(i))
            {
               if(DBGSIG)
                  printf("StartHeaderEdit(): Filename: %s\n", files->At(i)->GetTitle());

               HeaderChange( (char*)(files->At(i)->GetTitle()), changelist );
            }
         }
      }
   }
}

void TGAppMainFrame::ShowHeaderEdit(int id)
{
   char cTemp[512];

   // Preparing input file
   inroot = TFile::Open(editList->GetEntry(id)->GetTitle(), "READ");

   // Header tree
   TTree *header_data;
   inroot->GetObject("header_data", header_data);

   // Display branches from header in the entry fields
   header_data->SetBranchAddress("timestamp", &evtheader.timestamp);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("biasvolt", &evtheader.biasvolt);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("xpos", &evtheader.xpos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("ypos", &evtheader.ypos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("zpos", &evtheader.zpos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("temperature", &evtheader.temperature);
   header_data->GetEntry(0);
   if( header_data->FindBranch("angle") )
   {
      header_data->SetBranchAddress("angle", &evtheader.angle);
      header_data->GetEntry(0);
   }
   header_data->SetBranchAddress("laserinfo", &evtheader.laserinfo);
   header_data->GetEntry(0);

   GetTime(evtheader.timestamp, cTemp);

   timeEditDisplay->widgetTE->SetText(cTemp);
   if(!biasEdit->widgetChBox[0]->IsDown())
      biasEdit->widgetNE[0]->SetNumber(evtheader.biasvolt);
   if(!xPosEdit->widgetChBox[0]->IsDown())
      xPosEdit->widgetNE[0]->SetNumber(evtheader.xpos);
   if(!yPosEdit->widgetChBox[0]->IsDown())
      yPosEdit->widgetNE[0]->SetNumber(evtheader.ypos);
   if(!zPosEdit->widgetChBox[0]->IsDown())
      zPosEdit->widgetNE[0]->SetNumber(evtheader.zpos);
   if(!tempEdit->widgetChBox[0]->IsDown())
      tempEdit->widgetNE[0]->SetNumber(evtheader.temperature);
   if(!angleEdit->widgetChBox[0]->IsDown())
   {
      if( header_data->FindBranch("angle") )
         tempEdit->widgetNE[0]->SetNumber(evtheader.angle);
      else
         tempEdit->widgetNE[0]->SetNumber(0.);
   }
   if(!laserEdit->widgetChBox[0]->IsDown())
      laserEdit->widgetTE->SetText(evtheader.laserinfo);

   delete header_data;
   delete inroot;
}

void TGAppMainFrame::HeaderChange(char *histfile, bool *changetype)
{
   int scopeTemp;

   if(DBGSIG)
      printf("HeaderChange(): Selected file: %s\n", histfile);

   // Preparing input file and the temporary output file
   inroot = TFile::Open(histfile, "READ");

   scopeTemp = inroot->GetListOfKeys()->Contains("scope_data");

   char outname[256];
   sprintf(outname, "%s/results/temp.root", rootdir);
   outroot = TFile::Open(outname, "RECREATE");

   // Tree structure of input file and output file
   TTree *header_data, *meas_data, *scope_data;

   printf("%d\n", inroot->GetListOfKeys()->Contains("header_data"));
   printf("%d\n", inroot->GetListOfKeys()->Contains("meas_data"));
   printf("%d\n", scopeTemp);

   TTree *new_meas_data, *new_scope_data;
   inroot->GetObject("header_data", header_data);
   inroot->GetObject("meas_data", meas_data);
   new_meas_data = meas_data->CloneTree();
   //TTree *new_scope_data;
   if(scopeTemp)
   {
      inroot->GetObject("scope_data", scope_data);
      new_scope_data = scope_data->CloneTree();
   }
   else
      printf("No scope_data header found.\n");

   // Save branches from the old header to temporary variables
   header_data->SetBranchAddress("nrch", &evtheader.nrch);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("timestamp", &evtheader.timestamp);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("biasvolt", &evtheader.biasvolt);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("xpos", &evtheader.xpos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("ypos", &evtheader.ypos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("zpos", &evtheader.zpos);
   header_data->GetEntry(0);
   header_data->SetBranchAddress("temperature", &evtheader.temperature);
   header_data->GetEntry(0);
   if( header_data->FindBranch("angle") )
   {
      header_data->SetBranchAddress("angle", &evtheader.angle);
      header_data->GetEntry(0);
   }
   header_data->SetBranchAddress("laserinfo", &evtheader.laserinfo);
   header_data->GetEntry(0);

   int itemp[5] = {0,0,0,0,0};
   double dtemp[3] = {0.,0.,0.};
   char ctemp[256];

   itemp[0] = evtheader.nrch;
   itemp[1] = evtheader.timestamp;
   itemp[2] = evtheader.xpos;
   itemp[3] = evtheader.ypos;
   itemp[4] = evtheader.zpos;
   dtemp[0] = evtheader.biasvolt;
   dtemp[1] = evtheader.temperature;
   if( header_data->FindBranch("angle") )
      dtemp[2] = evtheader.angle;
   else
      dtemp[2] = 0.;
   sprintf(ctemp, "%s", evtheader.laserinfo);

   delete header_data;
   delete meas_data;
   if(scopeTemp)
      delete scope_data;
   delete inroot;
 
//printf("HeaderChange(): 6\n");
   // Prepare branches for the new header
   TTree *new_header_data = new TTree("header_data", "Header information for the measurement.");
   new_header_data->Branch("nrch", &evtheader.nrch, "nrch/I");
   new_header_data->Branch("timestamp", &evtheader.timestamp, "timestamp/I");
   new_header_data->Branch("biasvolt", &evtheader.biasvolt, "biasvolt/D");
   new_header_data->Branch("xpos", &evtheader.xpos, "xpos/I");
   new_header_data->Branch("ypos", &evtheader.ypos, "ypos/I");
   new_header_data->Branch("zpos", &evtheader.zpos, "zpos/I");
   new_header_data->Branch("temperature", &evtheader.temperature, "temperature/D");
   new_header_data->Branch("angle", &evtheader.angle, "temperature/D");
   new_header_data->Branch("laserinfo", &evtheader.laserinfo, "laserinfo/C");

//printf("HeaderChange(): 7\n");
   // Save new values (and old ones where we don't want to edit anything)
   evtheader.nrch = itemp[0];
   evtheader.timestamp = itemp[1];
   // Bias voltage
   if(changetype[0])
      evtheader.biasvolt = (double)biasEdit->widgetNE[0]->GetNumber();
   else
      evtheader.biasvolt = dtemp[0];
   // X pos
   if(changetype[1])
      evtheader.xpos = (int)xPosEdit->widgetNE[0]->GetNumber();
   else
      evtheader.xpos = itemp[2];
   // Y pos
   if(changetype[2])
      evtheader.ypos = (int)yPosEdit->widgetNE[0]->GetNumber();
   else
      evtheader.ypos = itemp[3];
   // Z pos
   if(changetype[3])
      evtheader.zpos = (int)zPosEdit->widgetNE[0]->GetNumber();
   else
      evtheader.zpos = itemp[4];
   // Temperature
   if(changetype[4])
      evtheader.temperature = (double)tempEdit->widgetNE[0]->GetNumber();
   else
      evtheader.temperature = dtemp[1];
   // Angle
   if(changetype[5])
      evtheader.angle = (double)angleEdit->widgetNE[0]->GetNumber();
   else
      evtheader.angle = dtemp[2];
   // Laser info
   if(changetype[6])
      sprintf(evtheader.laserinfo, "%s", laserEdit->widgetTE->GetText());
   else
      sprintf(evtheader.laserinfo, "%s", ctemp);

//printf("HeaderChange(): 8\n");
   new_header_data->Fill();

   // Write down the temporary output file
   new_header_data->Write();
   new_meas_data->Write();
   if(scopeTemp)
      new_scope_data->Write();

//printf("HeaderChange(): 9\n");
   delete new_header_data;
   delete new_meas_data;
   if(scopeTemp)
      delete new_scope_data;
   delete outroot;

   // Replace the original file with temporary output file (and delete temporary file)
   sprintf(outname, "cp -f %s/results/temp.root %s", rootdir, histfile);
   system(outname);
   sprintf(outname, "rm -f %s/results/temp.root", rootdir);
   system(outname);

   printf("Edited header in file: %s\n", histfile);
}

void TGAppMainFrame::CloseEditTab(int tabval)
{
   int curtab = (int)TMath::Floor(tabval/100.);
   int oldtab = tabval - curtab*100;

   if(DBGSIG > 1) printf("CloseEditTab(): New tab = %d, old tab = %d\n", curtab, oldtab);

   fTab->RemoveTab(curtab);

   delete editList;
   delete timeEditDisplay;
   delete biasEdit;
   delete xPosEdit;
   delete yPosEdit;
   delete zPosEdit;
   delete tempEdit;
   delete angleEdit;
   delete laserEdit;
   delete editHead;
   delete selectWarn;

   for(int i = 0; i < fTab->GetNumberOfTabs(); i++)
      if(DBGSIG > 1) printf("CloseEditTab(): Name of tab (%d) = %s\n", i, fTab->GetTabTab(i)->GetString() );

   fTab->SetTab(oldtab);
}

// Edit file window ---------------------------------------------------

// Temporary analysis window ------------------------------------------

// Open a new tab for editing datafile headers
void TGAppMainFrame::TempAnalysisTab(TGTab *mainTab, bool create, int *tabid, int analtype)
{
   if(create)
   {
      TGCompositeFrame *fH1, *fV1;
      TGHorizontalFrame *fTitle;
      TGGroupFrame *fG1;
      TGLabel *lab;
   
      int startTab = mainTab->GetCurrent();
      int newTab = mainTab->GetNumberOfTabs();
      if(DBGSIG > 1) printf("TempAnalysisTab(): Current tab = %d, Nr. of tabs = %d\n", startTab, newTab );

      double numform[6];
     
      int subgroup[2];
      subgroup[0] = mainTab->GetWidth()-10;
      subgroup[1] = mainTab->GetHeight()-10;

      TGCompositeFrame *fT1;
      fT1 = fTab->AddTab("Analysis edit");

      // Title label
      fTitle = new TGHorizontalFrame(fT1, 100, 25, kFixedHeight | kSunkenFrame);
      TGTitleLabel(fT1, fTitle, "Analysis edit", (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
      fT1->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop));

      fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame);
      // Temporary analysis canvas
      if( (analTab->GetCurrent() == 3) || ((analTab->GetCurrent() == 0) && (intSpect->widgetChBox[0]->IsDown() || intSpect->widgetChBox[1]->IsDown())) )
      {
         tempAnalysisCanvas = new TRootEmbeddedCanvas("tempAnalysisCanvas",fV1,subgroup[1],5*subgroup[1]/6);
         fV1->AddFrame(tempAnalysisCanvas, f0centerX);
      }
      else
      {
         tempAnalysisCanvas = new TRootEmbeddedCanvas("tempAnalysisCanvas",fV1,3*subgroup[0]/4,3*subgroup[1]/4);
         fV1->AddFrame(tempAnalysisCanvas, f1expandXpad);
      }
      tempAnalysisCanvas->GetCanvas()->SetGrid();

      // Specific options for plotting (analtype: 0 = Normal integration, 1 = Edge scans, 2 = Relative PDE,...)
      // Normal integration
      if(analtype == 0)
      {
      }
      // Edge scans
      else if(analtype == 1)
      {
      }
      // Relative PDE
      else if(analtype == 2)
      {
         // Running average offset
         if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *runningOff -> Set running average offset.\n");
         runningOff = new TSubStructure();
         for(int i = 0; i < 6; i++) numform[i] = 0;
         numform[0] = 5; numform[2] = 2;
         if(runningOff->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Running average offset:", 0, numform, "center"))
            fV1->AddFrame(runningOff->outsidebox, f1expandXpad);

         // Running average setting for plot
         if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *runningAver -> Produce running average of a graph.\n");
         runningAver = new TSubStructure();
         for(int i = 0; i < 6; i++) numform[i] = 0;
         numform[0] = 5; numform[2] = 2;
         if(runningAver->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Running average type (0 to disable):", 0, numform, "center"))
            fV1->AddFrame(runningAver->outsidebox, f1expandXpad);

         // Putting a second y-axis to the plot for mean number of photons histogram
         if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *secondAxis -> Create second y-axis for mean number of photons.\n");
         secondAxis = new TSubStructure();
         for(int i = 0; i < 6; i++) numform[i] = 0;
         numform[0] = 5; numform[1] = 1; numform[2] = 2;
         if(secondAxis->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Scale second axis:", 0, numform, "center"))
            fV1->AddFrame(secondAxis->outsidebox, f1expandXpad);

         runningAver->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "ApplyRunningAver()");
         runningOff->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "ApplyRunningAver()");
      }

      // Export and close buttons
      if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *exportExitAnalysis -> 2 buttons for either exporting the plot or closing the tab\n");
      exportExitAnalysis = new TSubStructure();
      const char *selnames[512] = {"Export","Close"};
      if(exportExitAnalysis->TGMultiButton(fV1, subgroup[0]/2, 30, 2, selnames, "center"))
         fV1->AddFrame(exportExitAnalysis->outsidebox, f1expandXpad);

      // Actions for header editor
      char cTemp[512];
      exportExitAnalysis->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "ExportTempAnalysisPlot()");
      sprintf(cTemp, "CloseTempAnalysisTab(=%d)", newTab*100+startTab);
      exportExitAnalysis->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, cTemp);

      fT1->AddFrame(fV1, f1expand2d);

      fMain->MapSubwindows();
      fMain->MapWindow();
      fMain->Layout();

      // Set tab ID
      *tabid = newTab;

      if(DBGSIG > 1)
      {
         printf("TempAnalysisTab(): New tab objects (Temporary Analysis Header)\n");
         gObjectTable->Print();
      }
   }
   else
   {
      // Switch to new tab
      fTab->SetTab(*tabid);
   }
}

void TGAppMainFrame::CloseTempAnalysisTab(int tabval)
{
   int curtab = (int)TMath::Floor(tabval/100.);
   int oldtab = tabval - curtab*100;

   if(DBGSIG > 1) printf("CloseTempAnalysisTab(): New tab = %d, old tab = %d\n", curtab, oldtab);

   fTab->RemoveTab(curtab);

   delete tempAnalysisCanvas;
   delete exportExitAnalysis;

   for(int i = 0; i < fTab->GetNumberOfTabs(); i++)
      if(DBGSIG > 1) printf("CloseTempAnalysisTab(): Name of tab (%d) = %s\n", i, fTab->GetTabTab(i)->GetString() );

   fTab->SetTab(oldtab);
}

void TGAppMainFrame::ApplyRunningAver()
{
   TCanvas *gCanvas = tempAnalysisCanvas->GetCanvas();
   TList *funcList = (TList*)gCanvas->GetListOfPrimitives();
   unsigned int nrfunc = funcList->GetSize();
   TGraph *baseGr;
   char funcname[512];
   int runav = runningAver->widgetNE[0]->GetNumber();
   int offx = runningOff->widgetNE[0]->GetNumber();

   if(runav == 0)       // If running average is disabled, don't update the plot
      return;

   for(int i = 0; i < nrfunc; i++)
   {
      sprintf(funcname, "%s", funcList->At(i)->GetName());
      if(DBGSIG) printf("ApplyRunningAver(): Function is: %s\n", funcname);

      if(strcmp(funcname,"runaver") == 0)
      {
         gCanvas->GetPrimitive(funcname)->Delete();
         gCanvas->Modified();
         gCanvas->Update();
      }
      else if(strcmp(funcname,"pde") == 0)
      {
         baseGr = (TGraph*)gCanvas->GetPrimitive(funcname);
         int nrpoints = baseGr->GetN();
         TGraph *runaver = new TGraph((int)(nrpoints-2*offx)/runav);
         runaver->SetName("runaver");
         runaver->SetFillColor(1);
         runaver->SetLineColor(kBlack);
         runaver->SetLineWidth(2);
         runaver->SetMarkerColor(kBlack);
         int nr = 0, j = 0;
         double averx = 0, avery = 0;
         double *xval, *yval;
         xval = new double[runav];
         yval = new double[runav];
         while(1)
         {
            if((nr == (int)nrpoints/runav) || (runav*nr+j+offx > nrpoints-offx)) break;
            baseGr->GetPoint(runav*nr+j+offx,xval[j],yval[j]);
            if(DBGSIG) printf("ApplyRunningAver(): j = %d: X = %lf, Y = %lf\n", j, xval[j], yval[j]);
            averx += xval[j];
            avery += yval[j];
            j++;
            if((j == runav) && (runav*nr+j+offx <= nrpoints-offx))
            {
               runaver->SetPoint(nr,averx/runav,avery/runav);
               if(DBGSIG) printf("ApplyRunningAver(): \t%d: averX = %lf, averY = %lf\n", nr, averx/runav, avery/runav);
               nr++;
               averx = 0;
               avery = 0;
               j = 0;
            }
         }
         gCanvas->cd();
         runaver->Draw("l same");
         gCanvas->Modified();
         gCanvas->Update();
         delete[] xval;
         delete[] yval;
      }
   }
}

void TGAppMainFrame::ExportTempAnalysisPlot()
{
   TCanvas *gCanvas = tempAnalysisCanvas->GetCanvas();

   TGFileInfo file_info;
   const char *filetypes[] = {"PDF","*.pdf","Encapsulated PostScript (.eps)","*.eps", "JPG/JPEG", "*.jpg", "PNG", "*.png",0,0};
   char *cTemp;
   file_info.fFileTypes = filetypes;
   cTemp = new char[1024];
//   sprintf(cTemp, "%s/results", rootdir);
//   file_info.fIniDir = StrDup(cTemp);
   file_info.fIniDir = StrDup(currentOpenDir);
   new TGFileDialog(gClient->GetDefaultRoot(), fMain, kFDSave, &file_info);
   delete[] cTemp;

   if(file_info.fFilename != NULL)
      gCanvas->SaveAs(file_info.fFilename);
}

// Temporary analysis window ------------------------------------------