// By ROOT version 5.17/02 on 2008-03-13 06:46:41
 
 
 
#ifndef ROOT_TGDockableFrame
 
#include "TGDockableFrame.h"
 
#endif
 
#ifndef ROOT_TGMenu
 
#include "TGMenu.h"
 
#endif
 
#ifndef ROOT_TGMdiDecorFrame
 
#include "TGMdiDecorFrame.h"
 
#endif
 
#ifndef ROOT_TG3DLine
 
#include "TG3DLine.h"
 
#endif
 
#ifndef ROOT_TGMdiFrame
 
#include "TGMdiFrame.h"
 
#endif
 
#ifndef ROOT_TGMdiMainFrame
 
#include "TGMdiMainFrame.h"
 
#endif
 
#ifndef ROOT_TGuiBldHintsButton
 
#include "TGuiBldHintsButton.h"
 
#endif
 
#ifndef ROOT_TGMdiMenu
 
#include "TGMdiMenu.h"
 
#endif
 
#ifndef ROOT_TGListBox
 
#include "TGListBox.h"
 
#endif
 
#ifndef ROOT_TGNumberEntry
 
#include "TGNumberEntry.h"
 
#endif
 
#ifndef ROOT_TGScrollBar
 
#include "TGScrollBar.h"
 
#endif
 
#ifndef ROOT_TGuiBldHintsEditor
 
#include "TGuiBldHintsEditor.h"
 
#endif
 
#ifndef ROOT_TRootBrowser
 
#include "TRootBrowser.h"
 
#endif
 
#ifndef ROOT_TGFrame
 
#include "TGFrame.h"
 
#endif
 
#ifndef ROOT_TGFileDialog
 
#include "TGFileDialog.h"
 
#endif
 
#ifndef ROOT_TGShutter
 
#include "TGShutter.h"
 
#endif
 
#ifndef ROOT_TGButtonGroup
 
#include "TGButtonGroup.h"
 
#endif
 
#ifndef ROOT_TGCanvas
 
#include "TGCanvas.h"
 
#endif
 
#ifndef ROOT_TGFSContainer
 
#include "TGFSContainer.h"
 
#endif
 
#ifndef ROOT_TGButton
 
#include "TGButton.h"
 
#endif
 
#ifndef ROOT_TGuiBldEditor
 
#include "TGuiBldEditor.h"
 
#endif
 
#ifndef ROOT_TGTextEdit
 
#include "TGTextEdit.h"
 
#endif
 
#ifndef ROOT_TGFSComboBox
 
#include "TGFSComboBox.h"
 
#endif
 
#ifndef ROOT_TGLabel
 
#include "TGLabel.h"
 
#endif
 
#ifndef ROOT_TGView
 
#include "TGView.h"
 
#endif
 
#ifndef ROOT_TRootGuiBuilder
 
#include "TRootGuiBuilder.h"
 
#endif
 
#ifndef ROOT_TGTab
 
#include "TGTab.h"
 
#endif
 
#ifndef ROOT_TGListView
 
#include "TGListView.h"
 
#endif
 
#ifndef ROOT_TGSplitter
 
#include "TGSplitter.h"
 
#endif
 
#ifndef ROOT_TGStatusBar
 
#include "TGStatusBar.h"
 
#endif
 
#ifndef ROOT_TGListTree
 
#include "TGListTree.h"
 
#endif
 
#ifndef ROOT_TGToolTip
 
#include "TGToolTip.h"
 
#endif
 
#ifndef ROOT_TGToolBar
 
#include "TGToolBar.h"
 
#endif
 
#ifndef ROOT_TRootEmbeddedCanvas
 
#include "TRootEmbeddedCanvas.h"
 
#endif
 
#ifndef ROOT_TCanvas
 
#include "TCanvas.h"
 
#endif
 
#ifndef ROOT_TGuiBldDragManager
 
#include "TGuiBldDragManager.h"
 
#endif
 
 
 
#include "Riostream.h"
 
#include "TApplication.h"
 
#include "TROOT.h"
 
#include "TSystem.h"
 
#include "TGraph.h"
 
#include "TH1F.h"
 
#include "daq.h"
 
 
 
TGTextButton   *gTextButton[10];
 
TCanvas        *fCanvas;
 
TGMainFrame    *fMain;
 
TGTextEntry    *gFilename;
 
TGNumberEntry  *gPedestal;
 
TGNumberEntry  *gCh;
 
TGNumberEntry  *gMaxEve;
 
TGNumberEntry  *gNeve;
 
TGTextEntry    *gTimeDisplay;
 
TGCheckButton  *gCheckButton;
 
 
 
 
 
TRootEmbeddedCanvas *gCanvas;
 
#define MAXCH 32
 
TH1F* gHisto[MAXCH];
 
daq * gDaq;
 
#define WINDOW_NAME "ADC CAEN V965"
 
//----------------------------------------------------
 
int UIRDimming(int state){
 
  switch (state) {
 
  case 0:
 
    gTextButton[0]->SetEnabled(0);
 
    gTextButton[1]->SetEnabled(1);
 
    gTextButton[2]->SetEnabled(0);
 
    
 
    break;
 
 
 
  case 1:
 
    gTextButton[0]->SetEnabled(0);
 
    gTextButton[1]->SetEnabled(0);
 
    gTextButton[2]->SetEnabled(1);
 
      
 
    break;
 
 
 
  case 2:
 
    gTextButton[0]->SetEnabled(1);
 
    gTextButton[1]->SetEnabled(1);
 
    gTextButton[2]->SetEnabled(0);   
 
    break;
 
 
 
  default:
 
    break;
 
  }
 
  return 0;
 
}
 
 
 
int fDebug;
 
void GetDebug(){
 
  if ( gCheckButton->IsOn() ) fDebug=1;
 
  else fDebug=0;
 
}
 
 
 
int GetTime(char *x){
 
   int ret;
 
   time_t rawtime;
 
   static time_t oldtime=-1;
 
    struct tm * timeinfo;
 
    ret =  (oldtime==rawtime);
 
    if (len) x[len-1]=0;
 
    return ret;
 
}
 
void MyTimer(){
 
    char cmd[100];
 
    if (!GetTime(cmd)){
 
      if (gTimeDisplay) gTimeDisplay->SetText(cmd);
 
      if (gCanvas){
 
        gCanvas->GetCanvas()->Modified();
 
        gCanvas->GetCanvas()->Update();
 
      }
 
    }
 
}
 
 
 
//----------------------------------------------------
 
// thread function 
 
int gStop=0;
 
#define BSIZE 10000
 
unsigned long gBuf[BSIZE];
 
void Run(){
 
 
 
  // odpremo datoteko za pisanje
 
  int counters[10]={0,0,0,0,0, 0,0,0,0,0};
 
  char names[20][20]={"CAENV 1290","CAEN V965","LC2277","LC2277","LC2277"};
 
 
 
  char fname[128];
 
  sprintf(fname
,"%s.dat",gFilename
->GetText
());  
  FILE 
*fp
=fopen(fname
,"w"); 
  int neve  = (int) gMaxEve->GetNumber();
 
  int hdr[4]={1};
 
  
 
  if (gDaq) gDaq->fStop=0;
 
  // zajem zeljenega kolicine podatkov
 
  for (int n=0;n<neve;n++){
 
    if (!gDaq) break; 
 
    int nb = gDaq->event(gBuf,BSIZE,counters,1);
 
    if (gDaq->fStop) break;
 
    // zapis v datoteko   
 
    hdr[1]=nb+4*sizeof(int);
 
    hdr[3]=n;
 
   
 
    fwrite(hdr
,   sizeof(int),4 , fp
);   
    if (nb
) fwrite(gBuf
,   1,nb
, fp
);  
    // napolni histograme
 
   int nint= nb/4;
 
 
 
   int ii=0;
 
     
 
   while (ii<nint){
 
  
 
   int recid=gBuf[ii++];
 
   int len  =gBuf[ii++];
 
   unsigned int *dbuf=       (unsigned int *)&gBuf[ii];
 
   if (n
%1000==0) printf("%d 0x%03x Len=%d\n",n
,recid
,len
);  
   unsigned short edge;
 
 
 
    if (recid==0x130){     
 
        for (int i=0; i<len; i++) {
 
          int dtype=(dbuf[i]>>25)&0x3;
 
          if (dtype==0) {
 
            int ch=(dbuf[i]>>17)&0xf;
 
            if (2*ch<MAXCH) {
 
              int rg=(dbuf[i]>>16)&0x1;
 
              int adc=dbuf[i]&0xfff;
 
              gHisto[2*ch+rg]->Fill(adc);
 
              //  printf("V965 0x%08x nev=%4d %d. [ch=%2d] range=%d data=%d \n",dbuf[i],n,i,ch,rg,adc);
 
            }
 
          }
 
        }
 
     } // if (recid==0x130)
 
      ii+=len;
 
   }
 
    /*
 
    // decode
 
    
 
    int nint=nb/sizeof(int);
 
   
 
    for (int i=0;i<nint;i++){
 
       unsigned short tdc =   gBuf[i]&0xFFFF;                  
 
       unsigned short ch  =  (gBuf[i] >> 17 ) &0x1F;                            
 
       unsigned short edge = (gBuf[i] >> 16 ) &0x1;
 
       if (edge && ch < MAXCH) gHisto[ch]->Fill(tdc);
 
       if (fDebug) printf("nev=%4d %d. [ch=%2d] egde=%d data=%d",n,i,ch,edge,tdc);
 
    }
 
    */
 
   gSystem->ProcessEvents();
 
   MyTimer();
 
   gNeve->SetNumber(n);
 
  }  
 
  UIRDimming(2);
 
  printf("End of MyThread neve=%d\n",neve
);  
 
 
}
 
 
 
 
 
int save2ascii(){
 
         if (!gHisto[0]) return 0; 
 
         char fname[128];
 
         sprintf(fname
,"%s.txt",gFilename
->GetText
());  
         FILE 
*fp
= fopen(fname
, "w");     
         char cmd[128];
 
         GetTime(cmd);
 
         fprintf(fp
, "Shranjeno: %s\n\n", cmd 
);  
         for (int j
=0;j
<MAXCH
;j
++)  fprintf(fp
, "%d\t",j
);  
         fprintf(fp
, "\n-------------------------------------------------\n");                                     
         for (int i=0;i<gHisto[0]->GetNbinsX();i++){
 
           for (int j
=0;j
<MAXCH
;j
++) fprintf(fp
, "%d\t",(int) gHisto
[j
]->GetBinContent
(i
+1));  
         }
 
         printf("Histogram content is dumped to the file %s\n",fname
);  
         return 0;
 
 }
 
 
 
void MyEventHandler(int i){
 
 
 
  UIRDimming(i);
 
 
 
  switch (i) {
 
  case 0: // Init
 
    gDaq->fPedestal = (int)(gPedestal->GetNumber());
 
    gDaq->init();
 
    break;
 
  case 1: // Start
 
    Run();
 
  case 2: // Stop 
 
    gDaq->fStop=1;
 
  case 3: // ReDraw
 
    gCanvas->GetCanvas()->Modified();
 
    gCanvas->GetCanvas()->Update();
 
    break;
 
  case 4: // Clear
 
    for (int j=0;j<MAXCH;j++) if (gHisto[j]) gHisto[j]->Reset();
 
    break;
 
  case 5: // Save
 
    save2ascii();
 
    break;
 
  case 6: // Print
 
    gCanvas->GetCanvas()->SaveAs("zivljenjski_cas_mionov.pdf");
 
    break;
 
  case 7: // exit
 
    gApplication->Terminate(0);
 
    break;
 
  }
 
 
 
}
 
 
 
int Redraw(long val=0){
 
  unsigned int ch= (unsigned int)(gCh->GetNumber());
 
  if (ch<MAXCH && gHisto[ch]) {
 
        gCanvas->GetCanvas()->cd();
 
              gHisto[ch]->Draw();
 
        gCanvas->GetCanvas()->Modified();
 
        gCanvas->GetCanvas()->Update();
 
  } else {
 
    if (gCh->GetNumber()>=MAXCH) gCh->SetNumber(MAXCH-1);
 
    if (gCh->GetNumber()< 0)     gCh->SetNumber(0);
 
  }
 
  return 0;
 
}
 
//----------------------------------------------------
 
 
 
int gui(){
 
 
 
  for (int i=0;i<MAXCH;i++){
 
    char hname[50];
 
    sprintf(hname
,"Ch. %d Rg.%d;ADC;N",i
/2,i
%2);  
    char hn[50];
 
    gHisto[i]   = new TH1F(hn,hname,4096,-0.5,4095.5);
 
  }  
 
  gDaq= new daq();
 
  fMain = new TGMainFrame(0,800,800);
 
  TGHorizontalFrame *fH=new TGHorizontalFrame(fMain,800,400);
 
  //------------------------------------------------------------
 
  TGLayoutHints *f0= new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2);
 
  TGLayoutHints *layout2= new TGLayoutHints(kLHintsLeft | kLHintsTop,20,20,20,20);
 
  // gumbi
 
  
 
  int nbut=8;
 
  const char *names[10]={"Init","Start","Stop","Refresh","Clear","Save", "Print" , "Exit"};
 
  for (int i=0;i<nbut;i++){
 
    
 
    gTextButton[i]= new TGTextButton(fH, names[i]);
 
    gTextButton[i]->SetTextJustify(36);
 
    gTextButton[i]->SetMargins(0,0,0,0);
 
    gTextButton[i]->SetWrapLength(-1);
 
    gTextButton[i]->Resize(50,22);
 
    fH->AddFrame(gTextButton[i], layout2);
 
    char cmd[50];
 
    sprintf(cmd
,"MyEventHandler(=%d)",i
);  
    TQObject::Connect(gTextButton[i],"Clicked()",0,0,cmd);
 
  }
 
 
 
  gCheckButton =  new TGCheckButton( fH,"Debug");
 
  gCheckButton->Resize(50,22);
 
  TQObject::Connect(gCheckButton,"Clicked()", 0, 0 , "GetDebug()");
 
  gCheckButton->SetState(kButtonDown);
 
  fH->AddFrame(gCheckButton, f0);
 
 
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  // ura
 
  TGLabel *lab1;
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"Time:");
 
  fH->AddFrame(lab1, f0);
 
  gTimeDisplay =  new TGTextEntry( fH,"");
 
  gTimeDisplay->Resize(200,22);
 
  fH->AddFrame(gTimeDisplay, f0);
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  // inputi
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"Filename:");
 
  fH->AddFrame(lab1, f0);
 
  gFilename =  new TGTextEntry( fH,"filename");
 
  gFilename->Resize(200,22);
 
  fH->AddFrame(gFilename, f0);
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"ADC Pedestal:");
 
  fH->AddFrame(lab1, f0);
 
  gPedestal =  new TGNumberEntry( fH,100);
 
  gPedestal->Resize(100,22);
 
  fH->AddFrame(gPedestal, f0);
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"N of events to acquire:");
 
  fH->AddFrame(lab1, f0);
 
  gMaxEve =  new TGNumberEntry( fH,10000);
 
  gMaxEve->Resize(100,22);
 
  fH->AddFrame(gMaxEve, f0);
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"Number of events:");
 
  fH->AddFrame(lab1, f0);
 
  gNeve =  new TGNumberEntry( fH,0);
 
  gNeve->Resize(100,22);
 
  fH->AddFrame(gNeve, f0);
 
  fMain->AddFrame(fH    , f0);
 
  //---------------------------------------------------------
 
  // canvas
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  gCanvas = new TRootEmbeddedCanvas ("gCanvas",fH,800,400);
 
  fH->AddFrame(gCanvas, f0);
 
  fMain->AddFrame(fH   , f0);
 
  //------------------------------------------------------------ 
 
  fH=new TGHorizontalFrame(fMain,800,200);
 
  lab1 = new TGLabel( fH ,"Channel number (0..31):");
 
  fH->AddFrame(lab1, f0);
 
  gCh =  new TGNumberEntry( fH,0);
 
  fH->AddFrame(gCh, f0);
 
  TQObject::Connect(gCh,"ValueSet(Long_t)",0,0,"Redraw(Long_t )"); 
 
  
 
  fMain->AddFrame(fH    , f0);
 
  //------------------------------------------------------------
 
  fMain->SetWindowName(WINDOW_NAME);
 
  fMain->MapSubwindows();
 
  fMain->Resize(fMain->GetDefaultSize());
 
  fMain->MapWindow();
 
  Redraw();
 
  GetDebug();
 
  return 0;
 
}