Subversion Repositories f9daq

Rev

Rev 197 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
195 f9daq 1
// By ROOT version 5.17/02 on 2008-03-13 06:46:41
2
 
3
#ifndef ROOT_TGDockableFrame
4
#include "TGDockableFrame.h"
5
#endif
6
#ifndef ROOT_TGMenu
7
#include "TGMenu.h"
8
#endif
9
#ifndef ROOT_TGMdiDecorFrame
10
#include "TGMdiDecorFrame.h"
11
#endif
12
#ifndef ROOT_TG3DLine
13
#include "TG3DLine.h"
14
#endif
15
#ifndef ROOT_TGMdiFrame
16
#include "TGMdiFrame.h"
17
#endif
18
#ifndef ROOT_TGMdiMainFrame
19
#include "TGMdiMainFrame.h"
20
#endif
21
//#ifndef ROOT_TGuiBldHintsButton
22
//#include "TGuiBldHintsButton.h"
23
//#endif
24
#ifndef ROOT_TGMdiMenu
25
#include "TGMdiMenu.h"
26
#endif
27
#ifndef ROOT_TGListBox
28
#include "TGListBox.h"
29
#endif
30
#ifndef ROOT_TGNumberEntry
31
#include "TGNumberEntry.h"
32
#endif
33
#ifndef ROOT_TGScrollBar
34
#include "TGScrollBar.h"
35
#endif
36
//#ifndef ROOT_TGuiBldHintsEditor
37
//#include "TGuiBldHintsEditor.h"
38
//#endif
39
#ifndef ROOT_TRootBrowser
40
#include "TRootBrowser.h"
41
#endif
42
#ifndef ROOT_TGFrame
43
#include "TGFrame.h"
44
#endif
45
#ifndef ROOT_TGFileDialog
46
#include "TGFileDialog.h"
47
#endif
48
#ifndef ROOT_TGShutter
49
#include "TGShutter.h"
50
#endif
51
#ifndef ROOT_TGButtonGroup
52
#include "TGButtonGroup.h"
53
#endif
54
#ifndef ROOT_TGCanvas
55
#include "TGCanvas.h"
56
#endif
57
#ifndef ROOT_TGFSContainer
58
#include "TGFSContainer.h"
59
#endif
60
#ifndef ROOT_TGButton
61
#include "TGButton.h"
62
#endif
63
//#ifndef ROOT_TGuiBldEditor
64
//#include "TGuiBldEditor.h"
65
//#endif
66
#ifndef ROOT_TGTextEdit
67
#include "TGTextEdit.h"
68
#endif
69
#ifndef ROOT_TGFSComboBox
70
#include "TGFSComboBox.h"
71
#endif
72
#ifndef ROOT_TGLabel
73
#include "TGLabel.h"
74
#endif
75
#ifndef ROOT_TGView
76
#include "TGView.h"
77
#endif
78
//#ifndef ROOT_TRootGuiBuilder
79
//#include "TRootGuiBuilder.h"
80
//#endif
81
#ifndef ROOT_TGTab
82
#include "TGTab.h"
83
#endif
84
#ifndef ROOT_TGListView
85
#include "TGListView.h"
86
#endif
87
#ifndef ROOT_TGSplitter
88
#include "TGSplitter.h"
89
#endif
90
#ifndef ROOT_TGStatusBar
91
#include "TGStatusBar.h"
92
#endif
93
#ifndef ROOT_TGListTree
94
#include "TGListTree.h"
95
#endif
96
#ifndef ROOT_TGToolTip
97
#include "TGToolTip.h"
98
#endif
99
#ifndef ROOT_TGToolBar
100
#include "TGToolBar.h"
101
#endif
102
#ifndef ROOT_TRootEmbeddedCanvas
103
#include "TRootEmbeddedCanvas.h"
104
#endif
105
#ifndef ROOT_TCanvas
106
#include "TCanvas.h"
107
#endif
108
//#ifndef ROOT_TGuiBldDragManager
109
//#include "TGuiBldDragManager.h"
110
//#endif
111
 
112
#include "Riostream.h"
113
#include "TThread.h"
114
#include "TApplication.h"
115
#include "TROOT.h"
116
#include "TGraph.h"
117
#include "TH1F.h"
118
//#include "daq.h"
119
 
120
TGTextButton   *gTextButton[10];
121
TCanvas        *fCanvas;
122
TGMainFrame    *fMain;
123
TGTextEntry    *gFilename;
124
TGNumberEntry  *gCh;
125
 
126
TGTextEntry    *gTimeDisplay;
127
TGCheckButton  *gDebugButton;
128
 
129
 
130
 
131
    TGNumberEntry  *gMaxEve;
132
TGNumberEntry  *gNeve;
133
TGNumberEntry *gMask;
134
TGNumberEntry *gRange;
135
TGNumberEntry *gSoftwareTrigger;
136
TGNumberEntry *gTriggerChannel;
137
TGNumberEntry *gSamplingFrequency;
138
TGNumberEntry *gTriggerDelay;
139
 
140
 
141
 
142
 
143
 
144
const char gParNames[30][30]={"Events:","EventNo:","Channel Mask:","Range:", "SwTrg", "TriggerChannel:","Sampling Freq.:","TRG Delay:" };
145
 
146
TGNumberFormat::EStyle gParStyle[10] = {
147
TGNumberFormat::kNESInteger ,
148
TGNumberFormat::kNESInteger,
149
TGNumberFormat::kNESInteger ,
150
TGNumberFormat::kNESReal,
151
TGNumberFormat::kNESInteger,
152
TGNumberFormat::kNESInteger,
153
TGNumberFormat::kNESInteger,
154
TGNumberFormat::kNESReal
155
};
156
TGNumberEntry *gParameters[10];
157
 
158
 
159
 
160
TRootEmbeddedCanvas *gCanvas;
161
#define MAXCH 32
162
TH1F* gHisto[MAXCH];
163
//daq * gDaq;
164
#define WINDOW_NAME "Praktikum IV MAPMT PET"
165
//----------------------------------------------------
166
int UIRDimming(int state){
167
  switch (state) {
168
  case 0:
169
    gTextButton[0]->SetEnabled(0);
170
    gTextButton[1]->SetEnabled(1);
171
    gTextButton[2]->SetEnabled(0);
172
 
173
    break;
174
 
175
  case 1:
176
    gTextButton[0]->SetEnabled(0);
177
    gTextButton[1]->SetEnabled(0);
178
    gTextButton[2]->SetEnabled(1);
179
 
180
    break;
181
 
182
  case 2:
183
    gTextButton[0]->SetEnabled(1);
184
    gTextButton[1]->SetEnabled(1);
185
    gTextButton[2]->SetEnabled(0);  
186
    break;
187
 
188
  default:
189
    break;
190
  }
191
  return 0;
192
}
193
 
194
int fDebug;
195
void GetDebug(){
196
  if ( gDebugButton->IsOn() ) fDebug=1;
197
  else fDebug=0;
198
}
199
 
200
int GetTime(char *x){
201
   time_t rawtime;
202
    struct tm * timeinfo;
203
    time ( &rawtime );
204
    timeinfo = localtime ( &rawtime );    
205
    sprintf(x,"%s",asctime (timeinfo));
206
    int len=strlen(x);
207
    if (len) x[len-1]=0;
208
    return 0;
209
}
210
void MyTimer(){
211
    char cmd[100];
212
    GetTime(cmd);
213
    if (gTimeDisplay) gTimeDisplay->SetText(cmd);
214
    /* Canvas ni thread safe
215
    if (gCanvas){
216
       gCanvas->GetCanvas()->Modified();
217
       gCanvas->GetCanvas()->Update();
218
    }
219
    */
220
}
221
 
222
//----------------------------------------------------
223
// thread function 
224
int gStop=0;
225
#define BSIZE 10000
226
unsigned int gBuf[BSIZE];
227
void *MyThread(void *ptr)
228
{
229
  TThread::Printf("Start of MyThread %x \n" ,(int *)ptr);
230
 
231
  // odpremo datoteko za pisanje
232
  char fname[128];
233
  sprintf(fname,"%s.dat",gFilename->GetText());
234
  FILE *fp=fopen(fname,"w");
235
  int neve  = (int) gMaxEve->GetNumber();
236
  int hdr[4]={1};
237
 
238
  //if (gDaq) gDaq->fStop=0;
239
  // zajem zeljenega kolicine podatkov
240
  for (int n=0;n<neve;n++){
241
        int nb=0;
242
    /*
243
        if (!gDaq) break;
244
    nb = gDaq->event(gBuf,BSIZE);
245
    if (gDaq->fStop) break;
246
        */
247
    if (nb<0){
248
      n--;
249
      continue;
250
    }
251
 
252
    // zapis v datoteko   
253
    hdr[1]=nb+4*sizeof(int);
254
    hdr[2]=time(NULL);
255
    hdr[3]=n;
256
 
257
    fwrite(hdr,   sizeof(int),4 , fp);
258
    fwrite(gBuf,   sizeof(int),nb, fp);
259
    // napolni histograme
260
    //*****************
261
    unsigned int *data= gBuf;
262
    int evsize=0;
263
    int events=0;  
264
    int ib=1,count=0;
265
    events = data[0];
266
    evsize = data[1]&0xffff;
267
    if (evsize<2){
268
      n--;
269
      continue;
270
    }
271
    const unsigned int  END_MARKER=0xFAF5;
272
    if (fDebug) printf("nb=%d Event:%d events=%d EvSize:%d\n",nb, n,  events, evsize);
273
    for (int i=0;i<evsize;i++)  {
274
       //if (fDebug) printf("%d\t%08x\n", ib, data[ib]);
275
 
276
       if (data[ib]== END_MARKER) break;
277
       if (ib%2==0) {
278
          unsigned short word1 =data[ib  ]&0xFFFF;
279
          unsigned short word2 =data[ib+1]&0xFFFF;
280
          unsigned short tdc =  word1;                 
281
          unsigned short ch  =  (word2 >> 1 ) &0x1F;
282
          unsigned short edge =  word2   & 0x1;
283
          unsigned short q    = (word2 >> 8)  &0x1;
284
          unsigned short x    = (word2 >> 9)  &0x1;
285
          TThread::Lock();
286
          if (edge && ch < MAXCH) gHisto[ch]->Fill(tdc);
287
          TThread::UnLock();
288
          if (fDebug) TThread::Printf("%d. [ch=%2d] edge=%d data=%d q=%d x=%d\n",count,ch,edge,tdc, q, x);
289
 
290
          count++;
291
       }
292
       ib++;
293
    }
294
    if (data[evsize+1]!=END_MARKER) printf("Error! END_MARKER not found\n");
295
//***************** 
296
 
297
 
298
 
299
 
300
 
301
   gNeve->SetNumber(n);
302
  }  
303
  fclose(fp);
304
  UIRDimming(2);
305
  TThread::Printf("End of MyThread neve=%d\n",neve);
306
 return 0;
307
}
308
 
309
 
310
int save2ascii(){
311
         if (!gHisto[0]) return 0;
312
         char fname[128];
313
         sprintf(fname,"%s.txt",gFilename->GetText());
314
         FILE *fp= fopen(fname, "w");    
315
         fprintf(fp, "%s\n",WINDOW_NAME);
316
         char cmd[128];
317
         GetTime(cmd);
318
         fprintf(fp, "Shranjeno: %s\n\n", cmd );
319
         fprintf(fp, "Kanal hid=")      ;
320
         for (int j=0;j<MAXCH;j++)  fprintf(fp, "%d\t",j);
321
         fprintf(fp, "\n-------------------------------------------------\n");                                   
322
         for (int i=0;i<gHisto[0]->GetNbinsX();i++){
323
           fprintf(fp, "%d\t",i);
324
           for (int j=0;j<MAXCH;j++) fprintf(fp, "%d\t",(int) gHisto[j]->GetBinContent(i+1));
325
           fprintf(fp, "\n");  
326
         }
327
         fclose(fp);
328
         printf("Rezultati meritev so zapisani v datoteki %s\n",fname);
329
         return 0;
330
 }
331
 
332
void MyEventHandler(int i){
333
  static TTimer * tmr = new TTimer(1000, kFALSE);
334
  UIRDimming(i);
335
  TThread *fThread;
336
  switch (i) {
337
  case 0: // Init
338
    //gDaq->init();
339
    break;
340
  case 1: // Start
341
    fThread = new TThread(MyThread,(void*)0);
342
    fThread->Run();
343
    tmr->SetCommand("MyTimer()");
344
    tmr->TurnOn();
345
    tmr->Start(1000, kFALSE);   // 1 second single-shot
346
    break;
347
  case 2: // Stop 
348
    //gDaq->fStop=1;
349
    tmr->Stop();
350
    tmr->TurnOff();
351
    break;
352
  case 3: // ReDraw
353
    gCanvas->GetCanvas()->Modified();
354
    gCanvas->GetCanvas()->Update();
355
    break;
356
  case 4: // Clear
357
    for (int j=0;j<MAXCH;j++) if (gHisto[j]) gHisto[j]->Reset();
358
    break;
359
  case 5: // Save
360
    save2ascii();
361
    break;
362
  case 6: // Print
363
    gCanvas->GetCanvas()->SaveAs("zivljenjski_cas_mionov.pdf");
364
    break;
365
  case 7: // exit
366
    gApplication->Terminate(0);
367
    break;
368
  }
369
 
370
}
371
 
372
int Redraw(long val=0){
373
  unsigned int ch= (unsigned int)(gCh->GetNumber());
374
  if (ch<MAXCH && gHisto[ch]) {
375
        gCanvas->GetCanvas()->cd();
376
        gHisto[ch]->Draw();
377
        gCanvas->GetCanvas()->Modified();
378
        gCanvas->GetCanvas()->Update();
379
  } else {
380
    if (gCh->GetNumber()>=MAXCH) gCh->SetNumber(MAXCH-1);
381
    if (gCh->GetNumber()< 0)     gCh->SetNumber(0);
382
  }
383
  return 0;
384
}
385
//----------------------------------------------------
386
 
387
int gui(){
388
 
389
  for (int i=0;i<MAXCH;i++){
390
    char hname[50];
391
    sprintf(hname,"TDC Ch. %d;TDC;N",i);
392
    char hn[50];
393
    sprintf(hn,"ch%d",i);
394
    gHisto[i]   = new TH1F(hn,hname,128,-0.5,1024*8-0.5);
395
  }  
396
  //gDaq= new daq();
397
  fMain = new TGMainFrame(0,800,800);
398
  TGHorizontalFrame *fH=new TGHorizontalFrame(fMain,800,400);
399
  //------------------------------------------------------------
400
  TGLayoutHints *f0= new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2);
401
  TGLayoutHints *layout2= new TGLayoutHints(kLHintsLeft | kLHintsTop,20,20,20,20);
402
  // gumbi
403
 
404
  int nbut=8;
405
  const char *names[10]={"Init","Start","Stop","Refresh","Clear","Export to ASCII", "Print" , "Exit"};
406
  for (int i=0;i<nbut;i++){
407
 
408
    gTextButton[i]= new TGTextButton(fH, names[i]);
409
    gTextButton[i]->SetTextJustify(36);
410
    gTextButton[i]->SetMargins(0,0,0,0);
411
    gTextButton[i]->SetWrapLength(-1);
412
    gTextButton[i]->ChangeOptions(gTextButton[i]->GetOptions() | kFixedWidth); // | kFixedSize
413
    gTextButton[i]->Resize(100,gTextButton[i]->GetDefaultHeight());
414
 
415
    fH->AddFrame(gTextButton[i], f0);
416
    char cmd[50];
417
    sprintf(cmd,"MyEventHandler(=%d)",i);
418
    TQObject::Connect(gTextButton[i],"Clicked()",0,0,cmd);
419
  }
420
 
421
  gDebugButton =  new TGCheckButton( fH,"Debug");
422
  gDebugButton->Resize(50,22);
423
  TQObject::Connect(gDebugButton,"Clicked()", 0, 0 , "GetDebug()");
424
  gDebugButton->SetState(kButtonDown);
425
  fH->AddFrame(gDebugButton, f0);
426
 
427
  fMain->AddFrame(fH    , f0);
428
 
429
  TGHorizontalFrame *fH1=new TGHorizontalFrame(fMain,800,400);
430
  //---------------------------------------------------------
431
  // ura
432
  TGLabel *lab1;
433
  fH=new TGHorizontalFrame(fH1,800,200);
434
  lab1 = new TGLabel( fH ,"Ura:");
435
  fH->AddFrame(lab1, f0);
436
  gTimeDisplay =  new TGTextEntry( fH,"");
437
  gTimeDisplay->Resize(200,22);
438
  fH->AddFrame(gTimeDisplay, f0);
439
  fH1->AddFrame(fH    , f0);
440
  //---------------------------------------------------------
441
  // inputi
442
  fH=new TGHorizontalFrame(fH1,800,200);
443
  lab1 = new TGLabel( fH ,"Filename:");
444
  fH->AddFrame(lab1, f0);
445
  gFilename =  new TGTextEntry( fH,"tmp");
446
  gFilename->Resize(200,22);
447
  fH->AddFrame(gFilename, f0);
448
  fH1->AddFrame(fH    , f0);
449
  //---------------------------------------------------------
450
  fH=new TGHorizontalFrame(fH1,800,200);
451
  lab1 = new TGLabel( fH ,"Events:");
452
  fH->AddFrame(lab1, f0);
453
  gMaxEve =  new TGNumberEntry( fH,10000);
454
  gMaxEve->Resize(100,22);
455
  fH->AddFrame(gMaxEve, f0);
456
  fH1->AddFrame(fH    , f0);
457
  //---------------------------------------------------------
458
  fH=new TGHorizontalFrame(fH1,800,200);
459
  lab1 = new TGLabel( fH ,"Event no:");
460
  fH->AddFrame(lab1, f0);
461
  gNeve =  new TGNumberEntry( fH,0);
462
  gNeve->Resize(100,22);
463
  fH->AddFrame(gNeve, f0);
464
  fH1->AddFrame(fH    , f0);
465
  //---------------------------------------------------------
466
  fMain->AddFrame(fH1    , f0);
467
  TGVerticalFrame *fV=new TGVerticalFrame(fMain,800,200);
468
  nbut = 8;
469
  for (int i=0;i<nbut;i++){
470
          fH=new TGHorizontalFrame(fV,800,200);
471
      lab1 = new TGLabel( fH ,gParNames[i]);
472
          lab1->Resize(100,22);
473
      fH->AddFrame(lab1, f0);
474
      gParameters[i] =  new TGNumberEntry( fH,0,5,i, gParStyle[i] );
475
      gParameters[i]->Resize(100,22);
476
      fH->AddFrame(gParameters[i] , f0);
477
      fV->AddFrame(fH    , f0);
478
  }      
479
  fMain->AddFrame(fV    , f0);
480
  //---------------------------------------------------------
481
  // canvas
482
  fH=new TGHorizontalFrame(fMain,800,200);
483
  gCanvas = new TRootEmbeddedCanvas ("gCanvas",fH,800,400);
484
  fH->AddFrame(gCanvas, f0);
485
  fMain->AddFrame(fH   , f0);
486
  //------------------------------------------------------------ 
487
  fH=new TGHorizontalFrame(fMain,800,200);
488
  lab1 = new TGLabel( fH ,"Ch. Number(0..3):");
489
  fH->AddFrame(lab1, f0);
490
  gCh =  new TGNumberEntry( fH,0);
491
  fH->AddFrame(gCh, f0);
492
  TQObject::Connect(gCh,"ValueSet(Long_t)",0,0,"Redraw(Long_t )");
493
 
494
  fMain->AddFrame(fH    , f0);
495
  //------------------------------------------------------------
496
  fMain->SetWindowName(WINDOW_NAME);
497
  fMain->MapSubwindows();
498
  fMain->Resize(fMain->GetDefaultSize());
499
  fMain->MapWindow();
500
  Redraw();
501
  GetDebug();
502
  return 0;
503
}