Subversion Repositories f9daq

Rev

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

  1. #include "../include/sipmscan.h"
  2. #include "../include/workstation.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. // Layout hints
  8. TGLayoutHints *f0centerX = new TGLayoutHints(kLHintsCenterX,2,2,2,2);
  9. TGLayoutHints *f0leftX = new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2);
  10. TGLayoutHints *f0leftXnoleft = new TGLayoutHints(kLHintsLeft | kLHintsTop,0,2,2,2);
  11. TGLayoutHints *f0leftXnopad = new TGLayoutHints(kLHintsLeft | kLHintsTop,0,0,0,0);
  12. TGLayoutHints *f0leftXpad = new TGLayoutHints(kLHintsLeft | kLHintsTop,12,12,2,2);
  13. TGLayoutHints *f0rightX = new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2);
  14. TGLayoutHints *f0rightXpad = new TGLayoutHints(kLHintsRight | kLHintsTop,12,12,2,2);
  15. TGLayoutHints *f0centerY = new TGLayoutHints(kLHintsCenterY,2,2,2,2);
  16. TGLayoutHints *f0center2d = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,2,2,2,2);
  17. TGLayoutHints *f1expandX = new TGLayoutHints(kLHintsExpandX,2,2,2,2);
  18. TGLayoutHints *f1expand2d = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,2,2,2,2);
  19. TGLayoutHints *f1expandXpad = new TGLayoutHints(kLHintsExpandX,12,12,2,2);
  20.  
  21. // Edit file window ---------------------------------------------------
  22.  
  23. // Open a new tab for editing datafile headers
  24. void TGAppMainFrame::HeaderEditTab(TGTab *mainTab, bool create, int *tabid)
  25. {
  26.    unsigned int nrfiles;
  27.    ULong_t rcolor, bcolor;
  28.    gClient->GetColorByName("red", rcolor);
  29.    gClient->GetColorByName("black", bcolor);
  30.  
  31.    if(create)
  32.    {
  33.       TGCompositeFrame *fH1, *fV1;
  34.       TGHorizontalFrame *fTitle;
  35.       TGGroupFrame *fG1;
  36.       TGLabel *lab;
  37.    
  38.       int startTab = mainTab->GetCurrent();
  39.       int newTab = mainTab->GetNumberOfTabs();
  40.       if(DBGSIG > 1) printf("HeaderEditTab(): Current tab = %d, Nr. of tabs = %d\n", startTab, newTab );
  41.  
  42.       double numform[6];
  43.      
  44.       int subgroup[2];
  45.       subgroup[0] = mainTab->GetWidth()-10;
  46.       subgroup[1] = mainTab->GetHeight()-10;
  47.    
  48.       TGCompositeFrame *fT1;
  49.       fT1 = fTab->AddTab("File header editor");
  50.  
  51.       // Title label
  52.       fTitle = new TGHorizontalFrame(fT1, 100, 25, kFixedHeight | kSunkenFrame);
  53.       TGTitleLabel(fT1, fTitle, "File header editor", (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
  54.       fT1->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop));
  55.    
  56.       // List view of files that we will edit
  57.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TGListBox *editList -> List box for editing files\n");
  58.       editList = new TGListBox(fT1,1);
  59.       editList->GetVScrollbar();
  60.       editList->Resize(300, (3*subgroup[1]/7)-10 );
  61.       fT1->AddFrame(editList, f1expandXpad);
  62.  
  63.       editList->SetMultipleSelections((multiSelect->widgetChBox[0]->IsOn()));
  64.    
  65.       // Copy the file list from the analysis tab for clearer view
  66.       nrfiles = fileList->GetNumberOfEntries();
  67.       printf("Nr. files = %d\n", nrfiles);
  68.       for(int i = 0; i < nrfiles; i++)
  69.          editList->AddEntry(fileList->GetEntry(i)->GetTitle(), i);
  70.  
  71.       fH1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kHorizontalFrame);
  72.       fV1 = new TGCompositeFrame(fH1, subgroup[0]/2, subgroup[1], kVerticalFrame);
  73.       // Time stamp display
  74.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *timeEditDisplay -> Display text Entry (time stamp)\n");
  75.       timeEditDisplay = new TSubStructure();
  76.       if(timeEditDisplay->TGLabelTEntry(fV1, subgroup[0]/2-4, 30, "Time of measurement:", "", "oneline"))
  77.          fV1->AddFrame(timeEditDisplay->outsidebox, f0leftXpad);
  78.       timeEditDisplay->widgetTE->SetState(kFALSE);
  79.  
  80.       // Bias voltage edit
  81.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *biasEdit -> Number entry for bias voltage edit\n");
  82.       biasEdit = new TSubStructure();
  83.       for(int i = 0; i < 6; i++) numform[i] = 0;
  84.       numform[0] = 7; numform[1] = 2;
  85.       if(biasEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Bias voltage edit:", 0, 0.00, numform, "left"))
  86.          fV1->AddFrame(biasEdit->outsidebox, f0leftXpad);
  87.  
  88.       // Position edits
  89.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *xPosEdit, *yPosEdit, *zPosEdit -> Number entries for position edit\n");
  90.       xPosEdit = new TSubStructure();
  91.       for(int i = 0; i < 6; i++) numform[i] = 0;
  92.       numform[0] = 9; numform[3] = 2; numform[4] = -100; numform[5] = 215000;
  93.       if(xPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "X position edit:", 0, 0, numform, "left"))
  94.          fV1->AddFrame(xPosEdit->outsidebox, f0leftXpad);
  95.  
  96.       yPosEdit = new TSubStructure();
  97.       if(yPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Y position edit:", 0, 0, numform, "left"))
  98.          fV1->AddFrame(yPosEdit->outsidebox, f0leftXpad);
  99.  
  100.       zPosEdit = new TSubStructure();
  101.       numform[5] = 375000;
  102.       if(zPosEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Z position edit:", 0, 0, numform, "left"))
  103.          fV1->AddFrame(zPosEdit->outsidebox, f0leftXpad);
  104.  
  105.       // Temperature edit
  106.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *tempEdit -> Number entry for temperature edit\n");
  107.       tempEdit = new TSubStructure();
  108.       for(int i = 0; i < 6; i++) numform[i] = 0;
  109.       numform[0] = 6; numform[1] = 1;
  110.       if(tempEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Temperature edit:", 0, 0.0, numform, "left"))
  111.          fV1->AddFrame(tempEdit->outsidebox, f0leftXpad);
  112.  
  113.       // Angle edit
  114.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *angleEdit -> Number entry for angle edit\n");
  115.       angleEdit = new TSubStructure();
  116.       for(int i = 0; i < 6; i++) numform[i] = 0;
  117.       numform[0] = 7; numform[1] = 2;
  118.       if(angleEdit->TGCheckNEntry(fV1, subgroup[0]/2, 30, "Incidence angle edit:", 0, 0.00, numform, "left"))
  119.          fV1->AddFrame(angleEdit->outsidebox, f0leftXpad);
  120.  
  121.       // Laser settings edit
  122.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *laserEdit -> Display text Entry for laser edit\n");
  123.       laserEdit = new TSubStructure();
  124.       if(laserEdit->TGCheckTEntry(fV1, subgroup[0]/2, 30, "Laser settings edit:", 0, "", "oneline"))
  125.          fV1->AddFrame(laserEdit->outsidebox, f0leftXpad);
  126.  
  127.       // Edit and close buttons
  128.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *editHead -> 2 buttons for either editing the head or closing the tab\n");
  129.       editHead = new TSubStructure();
  130.       const char *selnames[512] = {"Edit header","Close"};
  131.       if(editHead->TGMultiButton(fV1, subgroup[0]/2, 30, 2, selnames, "center"))
  132.          fV1->AddFrame(editHead->outsidebox, f0leftXpad);
  133.       fH1->AddFrame(fV1, f0leftXnopad);
  134.  
  135.       fV1 = new TGCompositeFrame(fH1, subgroup[0]/2, subgroup[1], kVerticalFrame);
  136.       // Multiple file select
  137.       if(DBGSIG > 1) printf("HeaderEditTab(): Creating TSubStructure *editMulti -> 1 Check button to set multi select or not\n");
  138.       editMulti = new TSubStructure();
  139.       int *checksel;
  140.       checksel = new int;
  141.       *checksel = multiSelect->widgetChBox[0]->IsDown();
  142.       selnames[0] = "Select multiple files";
  143.       if(editMulti->TGCheckList(fV1, subgroup[0]/2, 30, 1, selnames, checksel, "vertical", "center"))
  144.          fV1->AddFrame(editMulti->outsidebox, f0centerX);
  145.  
  146.       // Warning information
  147.       fG1 = new TGGroupFrame(fV1, "Warnings");
  148.       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).");
  149.       fG1->AddFrame(lab, f0leftXpad);
  150.       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).");
  151.       fG1->AddFrame(lab, f0leftXpad);
  152.       lab = new TGLabel(fG1, "Warning: Using button \"Edit header\" will edit headers in all files currently\nselected in the above selection list.");
  153.       lab->SetTextColor(rcolor);
  154.       fG1->AddFrame(lab, f0leftXpad);
  155.       if((editMulti->widgetChBox[0]->IsOn()))
  156.       {
  157.          selectWarn = new TGLabel(fG1, "Warning: Multiple files selected!");
  158.          selectWarn->SetTextColor(rcolor);
  159.          fG1->AddFrame(selectWarn, f0leftXpad);
  160.       }
  161.       else
  162.       {
  163.          selectWarn = new TGLabel(fG1, "Note: Single file selected.      ");
  164.          selectWarn->SetTextColor(bcolor);
  165.          fG1->AddFrame(selectWarn, f0leftXpad);
  166.       }
  167.       fV1->AddFrame(fG1, f0centerX);
  168.       fH1->AddFrame(fV1, f0centerX);
  169.  
  170.       // Actions for header editor
  171.       editList->Connect("Selected(Int_t)", "TGAppMainFrame", this, "ShowHeaderEdit(Int_t)");
  172.       biasEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=1)");
  173.       xPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=2)");
  174.       yPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=3)");
  175.       zPosEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=4)");
  176.       tempEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=5)");
  177.       angleEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=6)");
  178.       laserEdit->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "EditTickToggle(=7)");
  179.       editMulti->widgetChBox[0]->Connect("Clicked()", "TGAppMainFrame", this, "SetWarnings()");
  180.       editHead->widgetTB[0]->Connect("Clicked()", "TGAppMainFrame", this, "StartHeaderEdit()");
  181.       char cTemp[512];
  182.       sprintf(cTemp, "CloseEditTab(=%d)", newTab*100+startTab);
  183.       editHead->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, cTemp);
  184.  
  185.       fT1->AddFrame(fH1, f1expand2d);
  186.       fMain->MapSubwindows();
  187.       fMain->MapWindow();
  188.       fMain->Layout();
  189.  
  190.       // Initialize the values
  191.       for(int i = 0; i < 8; i++)
  192.          EditTickToggle(i);
  193.  
  194.       // Switch to new tab
  195.       fTab->SetTab(newTab);
  196.  
  197.       if(DBGSIG > 1)
  198.       {
  199.          printf("HeaderEditTab(): New tab objects (Edit Header)\n");
  200.          gObjectTable->Print();
  201.       }
  202.    }
  203.    else
  204.    {
  205.       if(multiSelect->widgetChBox[0]->IsDown())
  206.          editMulti->widgetChBox[0]->SetState(kButtonDown);
  207.       else
  208.          editMulti->widgetChBox[0]->SetState(kButtonUp);
  209.  
  210.       editList->SetMultipleSelections((editMulti->widgetChBox[0]->IsDown()));
  211.  
  212.       // Recopy the file list from the analysis tab
  213.       nrfiles = fileList->GetNumberOfEntries();
  214.       printf("Nr. files = %d\n", nrfiles);
  215.       for(int i = 0; i < nrfiles; i++)
  216.          editList->AddEntry(fileList->GetEntry(i)->GetTitle(), i);
  217.  
  218.       SetWarnings();
  219.  
  220.       // Switch to new tab
  221.       fTab->SetTab(*tabid);
  222.    }
  223. }
  224.  
  225. // Change the warning when selecting multiple files
  226. void TGAppMainFrame::SetWarnings()
  227. {
  228.    ULong_t rcolor, bcolor;
  229.    gClient->GetColorByName("red", rcolor);
  230.    gClient->GetColorByName("black", bcolor);
  231.  
  232.    editList->SetMultipleSelections((editMulti->widgetChBox[0]->IsDown()));
  233.  
  234.    // Set the warnings
  235.    if(editMulti->widgetChBox[0]->IsDown())
  236.    {
  237.       selectWarn->SetText("Warning: Multiple files selected!");
  238.       selectWarn->SetTextColor(rcolor);
  239.       selectWarn->SetWrapLength(-1);
  240.    }
  241.    else
  242.    {
  243.       selectWarn->SetText("Note: Single file selected.      ");
  244.       selectWarn->SetTextColor(bcolor);
  245.       selectWarn->SetWrapLength(-1);
  246.    }
  247. }
  248.  
  249. // Actions for editing the header
  250. void TGAppMainFrame::EditTickToggle(int type)
  251. {
  252.    // Toggle the edit possibility for header entries
  253.  
  254.    // Bias voltage
  255.    if(type == 1)
  256.    {
  257.       if(biasEdit->widgetChBox[0]->IsDown()) biasEdit->widgetNE[0]->SetState(kTRUE);
  258.       else biasEdit->widgetNE[0]->SetState(kFALSE);
  259.    }
  260.    // X position
  261.    else if(type == 2)
  262.    {
  263.       if(xPosEdit->widgetChBox[0]->IsDown()) xPosEdit->widgetNE[0]->SetState(kTRUE);
  264.       else xPosEdit->widgetNE[0]->SetState(kFALSE);
  265.    }
  266.    // Y position
  267.    else if(type == 3)
  268.    {
  269.       if(yPosEdit->widgetChBox[0]->IsDown()) yPosEdit->widgetNE[0]->SetState(kTRUE);
  270.       else yPosEdit->widgetNE[0]->SetState(kFALSE);
  271.    }
  272.    // Z position
  273.    else if(type == 4)
  274.    {
  275.       if(zPosEdit->widgetChBox[0]->IsDown()) zPosEdit->widgetNE[0]->SetState(kTRUE);
  276.       else zPosEdit->widgetNE[0]->SetState(kFALSE);
  277.    }
  278.    // Temperature
  279.    else if(type == 5)
  280.    {
  281.       if(tempEdit->widgetChBox[0]->IsDown()) tempEdit->widgetNE[0]->SetState(kTRUE);
  282.       else tempEdit->widgetNE[0]->SetState(kFALSE);
  283.    }
  284.    // Angle
  285.    else if(type == 6)
  286.    {
  287.       if(angleEdit->widgetChBox[0]->IsDown()) angleEdit->widgetNE[0]->SetState(kTRUE);
  288.       else angleEdit->widgetNE[0]->SetState(kFALSE);
  289.    }
  290.    // Laser info
  291.    else if(type == 7)
  292.    {
  293.       if(laserEdit->widgetChBox[0]->IsDown()) laserEdit->widgetTE->SetState(kTRUE);
  294.       else laserEdit->widgetTE->SetState(kFALSE);
  295.    }
  296. }
  297.  
  298. void TGAppMainFrame::StartHeaderEdit()
  299. {
  300.    unsigned int nrfiles = editList->GetNumberOfEntries();
  301.    TList *files;
  302.    // Changelist: Bias, X, Y, Z Positions, Temperature, Angle, Laser info
  303.    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() };
  304.  
  305.    if( nrfiles > 0 )
  306.    {
  307.       // check the selected file/files and return its name/their names
  308.       files = new TList();
  309.       editList->GetSelectedEntries(files);
  310.       if(files)
  311.       {
  312.          for(int i = 0; i < (int)nrfiles; i++)
  313.          {
  314.             if(files->At(i))
  315.             {
  316.                if(DBGSIG)
  317.                   printf("StartHeaderEdit(): Filename: %s\n", files->At(i)->GetTitle());
  318.  
  319.                HeaderChange( (char*)(files->At(i)->GetTitle()), changelist );
  320.             }
  321.          }
  322.       }
  323.    }
  324. }
  325.  
  326. void TGAppMainFrame::ShowHeaderEdit(int id)
  327. {
  328.    char cTemp[512];
  329.  
  330.    // Preparing input file
  331.    inroot = TFile::Open(editList->GetEntry(id)->GetTitle(), "READ");
  332.  
  333.    // Header tree
  334.    TTree *header_data;
  335.    inroot->GetObject("header_data", header_data);
  336.  
  337.    // Display branches from header in the entry fields
  338.    header_data->SetBranchAddress("timestamp", &evtheader.timestamp);
  339.    header_data->GetEntry(0);
  340.    header_data->SetBranchAddress("biasvolt", &evtheader.biasvolt);
  341.    header_data->GetEntry(0);
  342.    header_data->SetBranchAddress("xpos", &evtheader.xpos);
  343.    header_data->GetEntry(0);
  344.    header_data->SetBranchAddress("ypos", &evtheader.ypos);
  345.    header_data->GetEntry(0);
  346.    header_data->SetBranchAddress("zpos", &evtheader.zpos);
  347.    header_data->GetEntry(0);
  348.    header_data->SetBranchAddress("temperature", &evtheader.temperature);
  349.    header_data->GetEntry(0);
  350.    if( header_data->FindBranch("angle") )
  351.    {
  352.       header_data->SetBranchAddress("angle", &evtheader.angle);
  353.       header_data->GetEntry(0);
  354.    }
  355.    header_data->SetBranchAddress("laserinfo", &evtheader.laserinfo);
  356.    header_data->GetEntry(0);
  357.  
  358.    GetTime(evtheader.timestamp, cTemp);
  359.  
  360.    timeEditDisplay->widgetTE->SetText(cTemp);
  361.    if(!biasEdit->widgetChBox[0]->IsDown())
  362.       biasEdit->widgetNE[0]->SetNumber(evtheader.biasvolt);
  363.    if(!xPosEdit->widgetChBox[0]->IsDown())
  364.       xPosEdit->widgetNE[0]->SetNumber(evtheader.xpos);
  365.    if(!yPosEdit->widgetChBox[0]->IsDown())
  366.       yPosEdit->widgetNE[0]->SetNumber(evtheader.ypos);
  367.    if(!zPosEdit->widgetChBox[0]->IsDown())
  368.       zPosEdit->widgetNE[0]->SetNumber(evtheader.zpos);
  369.    if(!tempEdit->widgetChBox[0]->IsDown())
  370.       tempEdit->widgetNE[0]->SetNumber(evtheader.temperature);
  371.    if(!angleEdit->widgetChBox[0]->IsDown())
  372.    {
  373.       if( header_data->FindBranch("angle") )
  374.          tempEdit->widgetNE[0]->SetNumber(evtheader.angle);
  375.       else
  376.          tempEdit->widgetNE[0]->SetNumber(0.);
  377.    }
  378.    if(!laserEdit->widgetChBox[0]->IsDown())
  379.       laserEdit->widgetTE->SetText(evtheader.laserinfo);
  380.  
  381.    delete header_data;
  382.    delete inroot;
  383. }
  384.  
  385. void TGAppMainFrame::HeaderChange(char *histfile, bool *changetype)
  386. {
  387.    int scopeTemp;
  388.  
  389.    if(DBGSIG)
  390.       printf("HeaderChange(): Selected file: %s\n", histfile);
  391.  
  392.    // Preparing input file and the temporary output file
  393.    inroot = TFile::Open(histfile, "READ");
  394.  
  395.    scopeTemp = inroot->GetListOfKeys()->Contains("scope_data");
  396.  
  397.    char outname[256];
  398.    sprintf(outname, "%s/results/temp.root", rootdir);
  399.    outroot = TFile::Open(outname, "RECREATE");
  400.  
  401.    // Tree structure of input file and output file
  402.    TTree *header_data, *meas_data, *scope_data;
  403.  
  404.    printf("%d\n", inroot->GetListOfKeys()->Contains("header_data"));
  405.    printf("%d\n", inroot->GetListOfKeys()->Contains("meas_data"));
  406.    printf("%d\n", scopeTemp);
  407.  
  408.    TTree *new_meas_data, *new_scope_data;
  409.    inroot->GetObject("header_data", header_data);
  410.    inroot->GetObject("meas_data", meas_data);
  411.    new_meas_data = meas_data->CloneTree();
  412.    //TTree *new_scope_data;
  413.    if(scopeTemp)
  414.    {
  415.       inroot->GetObject("scope_data", scope_data);
  416.       new_scope_data = scope_data->CloneTree();
  417.    }
  418.    else
  419.       printf("No scope_data header found.\n");
  420.  
  421.    // Save branches from the old header to temporary variables
  422.    header_data->SetBranchAddress("nrch", &evtheader.nrch);
  423.    header_data->GetEntry(0);
  424.    header_data->SetBranchAddress("timestamp", &evtheader.timestamp);
  425.    header_data->GetEntry(0);
  426.    header_data->SetBranchAddress("biasvolt", &evtheader.biasvolt);
  427.    header_data->GetEntry(0);
  428.    header_data->SetBranchAddress("xpos", &evtheader.xpos);
  429.    header_data->GetEntry(0);
  430.    header_data->SetBranchAddress("ypos", &evtheader.ypos);
  431.    header_data->GetEntry(0);
  432.    header_data->SetBranchAddress("zpos", &evtheader.zpos);
  433.    header_data->GetEntry(0);
  434.    header_data->SetBranchAddress("temperature", &evtheader.temperature);
  435.    header_data->GetEntry(0);
  436.    if( header_data->FindBranch("angle") )
  437.    {
  438.       header_data->SetBranchAddress("angle", &evtheader.angle);
  439.       header_data->GetEntry(0);
  440.    }
  441.    header_data->SetBranchAddress("laserinfo", &evtheader.laserinfo);
  442.    header_data->GetEntry(0);
  443.  
  444.    int itemp[5] = {0,0,0,0,0};
  445.    double dtemp[3] = {0.,0.,0.};
  446.    char ctemp[256];
  447.  
  448.    itemp[0] = evtheader.nrch;
  449.    itemp[1] = evtheader.timestamp;
  450.    itemp[2] = evtheader.xpos;
  451.    itemp[3] = evtheader.ypos;
  452.    itemp[4] = evtheader.zpos;
  453.    dtemp[0] = evtheader.biasvolt;
  454.    dtemp[1] = evtheader.temperature;
  455.    if( header_data->FindBranch("angle") )
  456.       dtemp[2] = evtheader.angle;
  457.    else
  458.       dtemp[2] = 0.;
  459.    sprintf(ctemp, "%s", evtheader.laserinfo);
  460.  
  461.    delete header_data;
  462.    delete meas_data;
  463.    if(scopeTemp)
  464.       delete scope_data;
  465.    delete inroot;
  466.  
  467. printf("HeaderChange(): 6\n");
  468.    // Prepare branches for the new header
  469.    TTree *new_header_data = new TTree("header_data", "Header information for the measurement.");
  470.    new_header_data->Branch("nrch", &evtheader.nrch, "nrch/I");
  471.    new_header_data->Branch("timestamp", &evtheader.timestamp, "timestamp/I");
  472.    new_header_data->Branch("biasvolt", &evtheader.biasvolt, "biasvolt/D");
  473.    new_header_data->Branch("xpos", &evtheader.xpos, "xpos/I");
  474.    new_header_data->Branch("ypos", &evtheader.ypos, "ypos/I");
  475.    new_header_data->Branch("zpos", &evtheader.zpos, "zpos/I");
  476.    new_header_data->Branch("temperature", &evtheader.temperature, "temperature/D");
  477.    new_header_data->Branch("angle", &evtheader.angle, "temperature/D");
  478.    new_header_data->Branch("laserinfo", &evtheader.laserinfo, "laserinfo/C");
  479.  
  480. printf("HeaderChange(): 7\n");
  481.    // Save new values (and old ones where we don't want to edit anything)
  482.    evtheader.nrch = itemp[0];
  483.    evtheader.timestamp = itemp[1];
  484.    // Bias voltage
  485.    if(changetype[0])
  486.       evtheader.biasvolt = (double)biasEdit->widgetNE[0]->GetNumber();
  487.    else
  488.       evtheader.biasvolt = dtemp[0];
  489.    // X pos
  490.    if(changetype[1])
  491.       evtheader.xpos = (int)xPosEdit->widgetNE[0]->GetNumber();
  492.    else
  493.       evtheader.xpos = itemp[2];
  494.    // Y pos
  495.    if(changetype[2])
  496.       evtheader.ypos = (int)yPosEdit->widgetNE[0]->GetNumber();
  497.    else
  498.       evtheader.ypos = itemp[3];
  499.    // Z pos
  500.    if(changetype[3])
  501.       evtheader.zpos = (int)zPosEdit->widgetNE[0]->GetNumber();
  502.    else
  503.       evtheader.zpos = itemp[4];
  504.    // Temperature
  505.    if(changetype[4])
  506.       evtheader.temperature = (double)tempEdit->widgetNE[0]->GetNumber();
  507.    else
  508.       evtheader.temperature = dtemp[1];
  509.    // Angle
  510.    if(changetype[5])
  511.       evtheader.angle = (double)angleEdit->widgetNE[0]->GetNumber();
  512.    else
  513.       evtheader.angle = dtemp[2];
  514.    // Laser info
  515.    if(changetype[6])
  516.       sprintf(evtheader.laserinfo, "%s", laserEdit->widgetTE->GetText());
  517.    else
  518.       sprintf(evtheader.laserinfo, "%s", ctemp);
  519.  
  520. printf("HeaderChange(): 8\n");
  521.    new_header_data->Fill();
  522.  
  523.    // Write down the temporary output file
  524.    new_header_data->Write();
  525.    new_meas_data->Write();
  526.    if(scopeTemp)
  527.       new_scope_data->Write();
  528.  
  529. printf("HeaderChange(): 9\n");
  530.    delete new_header_data;
  531.    delete new_meas_data;
  532.    if(scopeTemp)
  533.       delete new_scope_data;
  534.    delete outroot;
  535.  
  536.    // Replace the original file with temporary output file (and delete temporary file)
  537.    sprintf(outname, "cp -f %s/results/temp.root %s", rootdir, histfile);
  538.    system(outname);
  539.    sprintf(outname, "rm -f %s/results/temp.root", rootdir);
  540.    system(outname);
  541.  
  542.    printf("Edited header in file: %s\n", histfile);
  543. }
  544.  
  545. void TGAppMainFrame::CloseEditTab(int tabval)
  546. {
  547.    int curtab = (int)TMath::Floor(tabval/100.);
  548.    int oldtab = tabval - curtab*100;
  549.  
  550.    if(DBGSIG > 1) printf("CloseEditTab(): New tab = %d, old tab = %d\n", curtab, oldtab);
  551.  
  552.    fTab->RemoveTab(curtab);
  553.  
  554.    delete editList;
  555.    delete timeEditDisplay;
  556.    delete biasEdit;
  557.    delete xPosEdit;
  558.    delete yPosEdit;
  559.    delete zPosEdit;
  560.    delete tempEdit;
  561.    delete angleEdit;
  562.    delete laserEdit;
  563.    delete editHead;
  564.    delete selectWarn;
  565.  
  566.    for(int i = 0; i < fTab->GetNumberOfTabs(); i++)
  567.       if(DBGSIG > 1) printf("CloseEditTab(): Name of tab (%d) = %s\n", i, fTab->GetTabTab(i)->GetString() );
  568.  
  569.    fTab->SetTab(oldtab);
  570. }
  571.  
  572. // Edit file window ---------------------------------------------------
  573.  
  574. // Temporary analysis window ------------------------------------------
  575.  
  576. // Open a new tab for editing datafile headers
  577. void TGAppMainFrame::TempAnalysisTab(TGTab *mainTab, bool create, int *tabid, int analtype)
  578. {
  579.    if(create)
  580.    {
  581.       TGCompositeFrame *fH1, *fV1;
  582.       TGHorizontalFrame *fTitle;
  583.       TGGroupFrame *fG1;
  584.       TGLabel *lab;
  585.    
  586.       int startTab = mainTab->GetCurrent();
  587.       int newTab = mainTab->GetNumberOfTabs();
  588.       if(DBGSIG > 1) printf("TempAnalysisTab(): Current tab = %d, Nr. of tabs = %d\n", startTab, newTab );
  589.  
  590.       double numform[6];
  591.      
  592.       int subgroup[2];
  593.       subgroup[0] = mainTab->GetWidth()-10;
  594.       subgroup[1] = mainTab->GetHeight()-10;
  595.  
  596.       TGCompositeFrame *fT1;
  597.       fT1 = fTab->AddTab("Analysis edit");
  598.  
  599.       // Title label
  600.       fTitle = new TGHorizontalFrame(fT1, 100, 25, kFixedHeight | kSunkenFrame);
  601.       TGTitleLabel(fT1, fTitle, "Analysis edit", (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
  602.       fT1->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop));
  603.  
  604.       fV1 = new TGCompositeFrame(fT1, subgroup[0], subgroup[1], kVerticalFrame);
  605.       // Temporary analysis canvas
  606.       if( (analTab->GetCurrent() == 3) || ((analTab->GetCurrent() == 0) && (intSpect->widgetChBox[0]->IsDown() || intSpect->widgetChBox[1]->IsDown())) )
  607.       {
  608.          tempAnalysisCanvas = new TRootEmbeddedCanvas("tempAnalysisCanvas",fV1,subgroup[1],5*subgroup[1]/6);
  609.          fV1->AddFrame(tempAnalysisCanvas, f0centerX);
  610.       }
  611.       else
  612.       {
  613.          tempAnalysisCanvas = new TRootEmbeddedCanvas("tempAnalysisCanvas",fV1,3*subgroup[0]/4,3*subgroup[1]/4);
  614.          fV1->AddFrame(tempAnalysisCanvas, f1expandXpad);
  615.       }
  616.       tempAnalysisCanvas->GetCanvas()->SetGrid();
  617.  
  618.       // Specific options for plotting (analtype: 0 = Normal integration, 1 = Edge scans, 2 = Relative PDE,...)
  619.       // Normal integration
  620.       if(analtype == 0)
  621.       {
  622.       }
  623.       // Edge scans
  624.       else if(analtype == 1)
  625.       {
  626.       }
  627.       // Relative PDE
  628.       else if(analtype == 2)
  629.       {
  630.          // Running average offset
  631.          if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *runningOff -> Set running average offset.\n");
  632.          runningOff = new TSubStructure();
  633.          for(int i = 0; i < 6; i++) numform[i] = 0;
  634.          numform[0] = 5; numform[2] = 2;
  635.          if(runningOff->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Running average offset:", 0, numform, "center"))
  636.             fV1->AddFrame(runningOff->outsidebox, f1expandXpad);
  637.  
  638.          // Running average setting for plot
  639.          if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *runningAver -> Produce running average of a graph.\n");
  640.          runningAver = new TSubStructure();
  641.          for(int i = 0; i < 6; i++) numform[i] = 0;
  642.          numform[0] = 5; numform[2] = 2;
  643.          if(runningAver->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Running average type (0 to disable):", 0, numform, "center"))
  644.             fV1->AddFrame(runningAver->outsidebox, f1expandXpad);
  645.  
  646.          // Putting a second y-axis to the plot for mean number of photons histogram
  647.          if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *secondAxis -> Create second y-axis for mean number of photons.\n");
  648.          secondAxis = new TSubStructure();
  649.          for(int i = 0; i < 6; i++) numform[i] = 0;
  650.          numform[0] = 5; numform[1] = 1; numform[2] = 2;
  651.          if(secondAxis->TGLabelNEntry(fV1, subgroup[0]/2-24, 30, "Scale second axis:", 0, numform, "center"))
  652.             fV1->AddFrame(secondAxis->outsidebox, f1expandXpad);
  653.  
  654.          runningAver->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "ApplyRunningAver()");
  655.          runningOff->widgetNE[0]->Connect("ValueSet(Long_t)", "TGAppMainFrame", this, "ApplyRunningAver()");
  656.       }
  657.  
  658.       // Export and close buttons
  659.       if(DBGSIG > 1) printf("TempAnalysisTab(): Creating TSubStructure *exportExitAnalysis -> 2 buttons for either exporting the plot or closing the tab\n");
  660.       exportExitAnalysis = new TSubStructure();
  661.       const char *selnames[512] = {"Export","Close"};
  662.       if(exportExitAnalysis->TGMultiButton(fV1, subgroup[0]/2, 30, 2, selnames, "center"))
  663.          fV1->AddFrame(exportExitAnalysis->outsidebox, f1expandXpad);
  664.  
  665.       // Actions for header editor
  666.       char cTemp[512];
  667.       sprintf(cTemp, "CloseTempAnalysisTab(=%d)", newTab*100+startTab);
  668.       exportExitAnalysis->widgetTB[1]->Connect("Clicked()", "TGAppMainFrame", this, cTemp);
  669.  
  670.       fT1->AddFrame(fV1, f1expand2d);
  671.  
  672.       fMain->MapSubwindows();
  673.       fMain->MapWindow();
  674.       fMain->Layout();
  675.  
  676.       // Set tab ID
  677.       *tabid = newTab;
  678.  
  679.       if(DBGSIG > 1)
  680.       {
  681.          printf("TempAnalysisTab(): New tab objects (Temporary Analysis Header)\n");
  682.          gObjectTable->Print();
  683.       }
  684.    }
  685.    else
  686.    {
  687.       // Switch to new tab
  688.       fTab->SetTab(*tabid);
  689.    }
  690. }
  691.  
  692. void TGAppMainFrame::CloseTempAnalysisTab(int tabval)
  693. {
  694.    int curtab = (int)TMath::Floor(tabval/100.);
  695.    int oldtab = tabval - curtab*100;
  696.  
  697.    if(DBGSIG > 1) printf("CloseTempAnalysisTab(): New tab = %d, old tab = %d\n", curtab, oldtab);
  698.  
  699.    fTab->RemoveTab(curtab);
  700.  
  701.    delete tempAnalysisCanvas;
  702.    delete exportExitAnalysis;
  703.  
  704.    for(int i = 0; i < fTab->GetNumberOfTabs(); i++)
  705.       if(DBGSIG > 1) printf("CloseTempAnalysisTab(): Name of tab (%d) = %s\n", i, fTab->GetTabTab(i)->GetString() );
  706.  
  707.    fTab->SetTab(oldtab);
  708. }
  709.  
  710. void TGAppMainFrame::ApplyRunningAver()
  711. {
  712.    TCanvas *gCanvas = tempAnalysisCanvas->GetCanvas();
  713.    TList *funcList = (TList*)gCanvas->GetListOfPrimitives();
  714.    unsigned int nrfunc = funcList->GetSize();
  715.    TGraph *baseGr;
  716.    char funcname[512];
  717.    int runav = runningAver->widgetNE[0]->GetNumber();
  718.    int offx = runningOff->widgetNE[0]->GetNumber();
  719.  
  720.    if(runav == 0)       // If running average is disabled, don't update the plot
  721.       return;
  722.  
  723.    for(int i = 0; i < nrfunc; i++)
  724.    {
  725.       sprintf(funcname, "%s", funcList->At(i)->GetName());
  726.       if(DBGSIG) printf("ApplyRunningAver(): Function is: %s\n", funcname);
  727.  
  728.       if(strcmp(funcname,"runaver") == 0)
  729.       {
  730.          gCanvas->GetPrimitive(funcname)->Delete();
  731.          gCanvas->Modified();
  732.          gCanvas->Update();
  733.       }
  734.       else if(strcmp(funcname,"pde") == 0)
  735.       {
  736.          baseGr = (TGraph*)gCanvas->GetPrimitive(funcname);
  737.          int nrpoints = baseGr->GetN();
  738.          TGraph *runaver = new TGraph((int)(nrpoints-2*offx)/runav);
  739.          runaver->SetName("runaver");
  740.          runaver->SetFillColor(1);
  741.          runaver->SetLineColor(kBlack);
  742.          runaver->SetLineWidth(2);
  743.          runaver->SetMarkerColor(kBlack);
  744.          int nr = 0, j = 0;
  745.          double averx = 0, avery = 0;
  746.          double *xval, *yval;
  747.          xval = new double[runav];
  748.          yval = new double[runav];
  749.          while(1)
  750.          {
  751.             if((nr == (int)nrpoints/runav) || (runav*nr+j+offx > nrpoints-offx)) break;
  752.             baseGr->GetPoint(runav*nr+j+offx,xval[j],yval[j]);
  753.             if(DBGSIG) printf("ApplyRunningAver(): j = %d: X = %lf, Y = %lf\n", j, xval[j], yval[j]);
  754.             averx += xval[j];
  755.             avery += yval[j];
  756.             j++;
  757.             if((j == runav) && (runav*nr+j+offx <= nrpoints-offx))
  758.             {
  759.                runaver->SetPoint(nr,averx/runav,avery/runav);
  760.                if(DBGSIG) printf("ApplyRunningAver(): \t%d: averX = %lf, averY = %lf\n", nr, averx/runav, avery/runav);
  761.                nr++;
  762.                averx = 0;
  763.                avery = 0;
  764.                j = 0;
  765.             }
  766.          }
  767.          gCanvas->cd();
  768.          runaver->Draw("l same");
  769.          gCanvas->Modified();
  770.          gCanvas->Update();
  771.          delete[] xval;
  772.          delete[] yval;
  773.       }
  774.    }
  775. }
  776.  
  777. // Temporary analysis window ------------------------------------------
  778.