Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

// 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 "TThread.h"
#include "TApplication.h"
#include "TROOT.h"
#include "TGraph.h"
#include "TH1F.h"
#include "daq.h"

TGTextButton   *gTextButton[10];
TCanvas        *fCanvas;
TGMainFrame    *fMain;
TGTextEntry    *gFilename;
TGNumberEntry  *gCh;
TGNumberEntry  *gMaxEve;
TGNumberEntry  *gNeve;
TGTextEntry    *gTimeDisplay;
TGCheckButton  *gCheckButton;

 
TRootEmbeddedCanvas *gCanvas;
#define MAXCH 32
TH1F* gHisto[MAXCH];
daq * gDaq;
#define WINDOW_NAME "Zivljenski cas mionov"
//----------------------------------------------------
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){
   time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );    
    sprintf(x,"%s",asctime (timeinfo));
    int len=strlen(x);
    if (len) x[len-1]=0;
    return 0;
}
void MyTimer(){
    char cmd[100];
    GetTime(cmd);
    if (gTimeDisplay) gTimeDisplay->SetText(cmd);
    /* Canvas ni thread safe
    if (gCanvas){
       gCanvas->GetCanvas()->Modified();
       gCanvas->GetCanvas()->Update();
    }
    */
}

//----------------------------------------------------
// thread function 
int gStop=0;
#define BSIZE 10000
unsigned int gBuf[BSIZE];
void *MyThread(void *ptr)
{
  TThread::Printf("Start of MyThread %x \n" ,(int *)ptr);

  // odpremo datoteko za pisanje
  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);
    if (gDaq->fStop) break;
    if (nb<0){
      n--; 
      continue;
    }
    
    // zapis v datoteko   
    hdr[1]=nb+4*sizeof(int);
    hdr[2]=time(NULL);
    hdr[3]=n;
   
    fwrite(hdr,   sizeof(int),4 , fp); 
    fwrite(gBuf,   sizeof(int),nb, fp);
    // napolni histograme
    //*****************
    unsigned int *data= gBuf;
    int evsize=0;
    int events=0;  
    int ib=1,count=0;
    events = data[0];
    evsize = data[1]&0xffff;
    if (evsize<2){
      n--; 
      continue;
    }
    const unsigned int  END_MARKER=0xFAF5;
    if (fDebug) printf("nb=%d Event:%d events=%d EvSize:%d\n",nb, n,  events, evsize);
    for (int i=0;i<evsize;i++)  {
       //if (fDebug) printf("%d\t%08x\n", ib, data[ib]);
      
       if (data[ib]== END_MARKER) break; 
       if (ib%2==0) {
          unsigned short word1 =data[ib  ]&0xFFFF;
          unsigned short word2 =data[ib+1]&0xFFFF;
          unsigned short tdc =  word1;                 
          unsigned short ch  =  (word2 >> 1 ) &0x1F;
          unsigned short edge =  word2   & 0x1;
          unsigned short q    = (word2 >> 8)  &0x1;
          unsigned short x    = (word2 >> 9)  &0x1;
          TThread::Lock();
          if (edge && ch < MAXCH) gHisto[ch]->Fill(tdc);
          TThread::UnLock();
          if (fDebug) TThread::Printf("%d. [ch=%2d] edge=%d data=%d q=%d x=%d\n",count,ch,edge,tdc, q, x);
         
          count++;
       }
       ib++;
    }
    if (data[evsize+1]!=END_MARKER) printf("Error! END_MARKER not found\n");
//***************** 




    
   gNeve->SetNumber(n);
  }  
  fclose(fp);
  UIRDimming(2);
  TThread::Printf("End of MyThread neve=%d\n",neve);
 return 0;
}


int save2ascii(){
         if (!gHisto[0]) return 0; 
         char fname[128];
         sprintf(fname,"%s.txt",gFilename->GetText());
         FILE *fp= fopen(fname, "w");    
         fprintf(fp, "%s\n",WINDOW_NAME); 
         char cmd[128];
         GetTime(cmd);
         fprintf(fp, "Shranjeno: %s\n\n", cmd );
         fprintf(fp, "Kanal hid=")      ;
         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++){
           fprintf(fp, "%f\t",gHisto[0]->GetBinCenter(i+1));
           for (int j=0;j<MAXCH;j++) fprintf(fp, "%d\t",(int) gHisto[j]->GetBinContent(i+1));
           fprintf(fp, "\n");   
         }
         fclose(fp);
         printf("Rezultati meritev so zapisani v datoteki %s\n",fname);
         return 0;
 }

void MyEventHandler(int i){
  static TTimer * tmr = new TTimer(1000, kFALSE);
  UIRDimming(i);
  TThread *fThread;
  switch (i) {
  case 0: // Init
    gDaq->init();
    break;
  case 1: // Start
    fThread = new TThread(MyThread,(void*)0);
    fThread->Run(); 
    tmr->SetCommand("MyTimer()");
    tmr->TurnOn();
    tmr->Start(1000, kFALSE);   // 1 second single-shot
    break;
  case 2: // Stop 
    gDaq->fStop=1;
    tmr->Stop();
    tmr->TurnOff();
    break;
  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,"TDC Ch. %d;TDC;N",i);
    char hn[50];
    sprintf(hn,"ch%d",i);
    gHisto[i]   = new TH1F(hn,hname,128,-0.5,1024*8-0.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]={"Inicializacija","Start","Stop","Osvezi","Zbrisi","Shrani v datoteko", "Natisni" , "Izhod"};
  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]->ChangeOptions(gTextButton[i]->GetOptions() | kFixedWidth); // | kFixedSize
    gTextButton[i]->Resize(100,gTextButton[i]->GetDefaultHeight());
  
    fH->AddFrame(gTextButton[i], f0);
    char cmd[50];
    sprintf(cmd,"MyEventHandler(=%d)",i);
    TQObject::Connect(gTextButton[i],"Clicked()",0,0,cmd);
  }

  gCheckButton =  new TGCheckButton( fH,"Razhroscevanje");
  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 ,"Ura:");
  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 ,"Ime datoteke:");
  fH->AddFrame(lab1, f0);
  gFilename =  new TGTextEntry( fH,"datoteka");
  gFilename->Resize(200,22);
  fH->AddFrame(gFilename, f0);
  fMain->AddFrame(fH    , f0);
  //---------------------------------------------------------
  fH=new TGHorizontalFrame(fMain,800,200);
  lab1 = new TGLabel( fH ,"Zeljeno stevilo dogodkov:");
  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 ,"Zajetih dogodkov:");
  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 ,"St. kanala (0..31):");
  fH->AddFrame(lab1, f0);
  gCh =  new TGNumberEntry( fH,2);
  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;
}