Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
109 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
TGNumberEntry  *gMaxEve;
126
TGNumberEntry  *gNeve;
127
TGTextEntry    *gTimeDisplay;
128
TGCheckButton  *gCheckButton;
129
 
130
 
131
TRootEmbeddedCanvas *gCanvas;
132
#define MAXCH 32
133
TH1F* gHisto[MAXCH];
134
daq * gDaq;
135
#define WINDOW_NAME "Zivljenski cas mionov"
136
//----------------------------------------------------
137
int UIRDimming(int state){
138
  switch (state) {
139
  case 0:
140
    gTextButton[0]->SetEnabled(0);
141
    gTextButton[1]->SetEnabled(1);
142
    gTextButton[2]->SetEnabled(0);
143
 
144
    break;
145
 
146
  case 1:
147
    gTextButton[0]->SetEnabled(0);
148
    gTextButton[1]->SetEnabled(0);
149
    gTextButton[2]->SetEnabled(1);
150
 
151
    break;
152
 
153
  case 2:
154
    gTextButton[0]->SetEnabled(1);
155
    gTextButton[1]->SetEnabled(1);
156
    gTextButton[2]->SetEnabled(0);  
157
    break;
158
 
159
  default:
160
    break;
161
  }
162
  return 0;
163
}
164
 
165
int fDebug;
166
void GetDebug(){
167
  if ( gCheckButton->IsOn() ) fDebug=1;
168
  else fDebug=0;
169
}
170
 
171
int GetTime(char *x){
172
   time_t rawtime;
173
    struct tm * timeinfo;
174
    time ( &rawtime );
175
    timeinfo = localtime ( &rawtime );    
176
    sprintf(x,"%s",asctime (timeinfo));
177
    int len=strlen(x);
178
    if (len) x[len-1]=0;
179
    return 0;
180
}
181
void MyTimer(){
182
    char cmd[100];
183
    GetTime(cmd);
184
    if (gTimeDisplay) gTimeDisplay->SetText(cmd);
185
    /* Canvas ni thread safe
186
    if (gCanvas){
187
       gCanvas->GetCanvas()->Modified();
188
       gCanvas->GetCanvas()->Update();
189
    }
190
    */
191
}
192
 
193
//----------------------------------------------------
194
// thread function 
195
int gStop=0;
196
#define BSIZE 10000
197
unsigned int gBuf[BSIZE];
198
void *MyThread(void *ptr)
199
{
200
  TThread::Printf("Start of MyThread %x \n" ,(int *)ptr);
201
 
202
  // odpremo datoteko za pisanje
203
  char fname[128];
204
  sprintf(fname,"%s.dat",gFilename->GetText());
205
  FILE *fp=fopen(fname,"w");
206
  int neve  = (int) gMaxEve->GetNumber();
207
  int hdr[4]={1};
208
 
209
  if (gDaq) gDaq->fStop=0;
210
  // zajem zeljenega kolicine podatkov
211
  for (int n=0;n<neve;n++){
212
    if (!gDaq) break;
213
    int nb = gDaq->event(gBuf,BSIZE);
214
    if (gDaq->fStop) break;
215
    if (nb<0){
216
      n--;
217
      continue;
218
    }
219
 
220
    // zapis v datoteko   
221
    hdr[1]=nb+4*sizeof(int);
222
    hdr[2]=time(NULL);
223
    hdr[3]=n;
224
 
225
    fwrite(hdr,   sizeof(int),4 , fp);
226
    fwrite(gBuf,   sizeof(int),nb, fp);
227
    // napolni histograme
228
    //*****************
229
    unsigned int *data= gBuf;
230
    int evsize=0;
231
    int events=0;  
232
    int ib=1,count=0;
233
    events = data[0];
234
    evsize = data[1]&0xffff;
235
    if (evsize<2){
236
      n--;
237
      continue;
238
    }
239
    const unsigned int  END_MARKER=0xFAF5;
240
    if (fDebug) printf("nb=%d Event:%d events=%d EvSize:%d\n",nb, n,  events, evsize);
241
    for (int i=0;i<evsize;i++)  {
242
       //if (fDebug) printf("%d\t%08x\n", ib, data[ib]);
243
 
244
       if (data[ib]== END_MARKER) break;
245
       if (ib%2==0) {
246
          unsigned short word1 =data[ib  ]&0xFFFF;
247
          unsigned short word2 =data[ib+1]&0xFFFF;
248
          unsigned short tdc =  word1;                 
249
          unsigned short ch  =  (word2 >> 1 ) &0x1F;
250
          unsigned short edge =  word2   & 0x1;
251
          unsigned short q    = (word2 >> 8)  &0x1;
252
          unsigned short x    = (word2 >> 9)  &0x1;
253
          TThread::Lock();
254
          if (edge && ch < MAXCH) gHisto[ch]->Fill(tdc);
255
          TThread::UnLock();
256
          if (fDebug) TThread::Printf("%d. [ch=%2d] edge=%d data=%d q=%d x=%d\n",count,ch,edge,tdc, q, x);
257
 
258
          count++;
259
       }
260
       ib++;
261
    }
262
    if (data[evsize+1]!=END_MARKER) printf("Error! END_MARKER not found\n");
263
//***************** 
264
 
265
 
266
 
267
 
268
 
269
   gNeve->SetNumber(n);
270
  }  
271
  fclose(fp);
272
  UIRDimming(2);
273
  TThread::Printf("End of MyThread neve=%d\n",neve);
274
 return 0;
275
}
276
 
277
 
278
int save2ascii(){
279
         if (!gHisto[0]) return 0;
280
         char fname[128];
281
         sprintf(fname,"%s.txt",gFilename->GetText());
282
         FILE *fp= fopen(fname, "w");    
283
         fprintf(fp, "%s\n",WINDOW_NAME);
284
         char cmd[128];
285
         GetTime(cmd);
286
         fprintf(fp, "Shranjeno: %s\n\n", cmd );
287
         fprintf(fp, "Kanal hid=")      ;
288
         for (int j=0;j<MAXCH;j++)  fprintf(fp, "%d\t",j);
289
         fprintf(fp, "\n-------------------------------------------------\n");                                   
290
         for (int i=0;i<gHisto[0]->GetNbinsX();i++){
291
           fprintf(fp, "%f\t",gHisto[0]->GetBinCenter(i+1));
292
           for (int j=0;j<MAXCH;j++) fprintf(fp, "%d\t",(int) gHisto[j]->GetBinContent(i+1));
293
           fprintf(fp, "\n");  
294
         }
295
         fclose(fp);
296
         printf("Rezultati meritev so zapisani v datoteki %s\n",fname);
297
         return 0;
298
 }
299
 
300
void MyEventHandler(int i){
301
  static TTimer * tmr = new TTimer(1000, kFALSE);
302
  UIRDimming(i);
303
  TThread *fThread;
304
  switch (i) {
305
  case 0: // Init
306
    gDaq->init();
307
    break;
308
  case 1: // Start
309
    fThread = new TThread(MyThread,(void*)0);
310
    fThread->Run();
311
    tmr->SetCommand("MyTimer()");
312
    tmr->TurnOn();
313
    tmr->Start(1000, kFALSE);   // 1 second single-shot
314
    break;
315
  case 2: // Stop 
316
    gDaq->fStop=1;
317
    tmr->Stop();
318
    tmr->TurnOff();
319
    break;
320
  case 3: // ReDraw
321
    gCanvas->GetCanvas()->Modified();
322
    gCanvas->GetCanvas()->Update();
323
    break;
324
  case 4: // Clear
325
    for (int j=0;j<MAXCH;j++) if (gHisto[j]) gHisto[j]->Reset();
326
    break;
327
  case 5: // Save
328
    save2ascii();
329
    break;
330
  case 6: // Print
331
    gCanvas->GetCanvas()->SaveAs("zivljenjski_cas_mionov.pdf");
332
    break;
333
  case 7: // exit
334
    gApplication->Terminate(0);
335
    break;
336
  }
337
 
338
}
339
 
340
int Redraw(long val=0){
341
  unsigned int ch= (unsigned int)(gCh->GetNumber());
342
  if (ch<MAXCH && gHisto[ch]) {
343
        gCanvas->GetCanvas()->cd();
344
        gHisto[ch]->Draw();
345
        gCanvas->GetCanvas()->Modified();
346
        gCanvas->GetCanvas()->Update();
347
  } else {
348
    if (gCh->GetNumber()>=MAXCH) gCh->SetNumber(MAXCH-1);
349
    if (gCh->GetNumber()< 0)     gCh->SetNumber(0);
350
  }
351
  return 0;
352
}
353
//----------------------------------------------------
354
 
355
int gui(){
356
 
357
  for (int i=0;i<MAXCH;i++){
358
    char hname[50];
359
    sprintf(hname,"TDC Ch. %d;TDC;N",i);
360
    char hn[50];
361
    sprintf(hn,"ch%d",i);
362
    gHisto[i]   = new TH1F(hn,hname,128,-0.5,1024*8-0.5);
363
  }  
364
  gDaq= new daq();
365
  fMain = new TGMainFrame(0,800,800);
366
  TGHorizontalFrame *fH=new TGHorizontalFrame(fMain,800,400);
367
  //------------------------------------------------------------
368
  TGLayoutHints *f0= new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2);
369
  TGLayoutHints *layout2= new TGLayoutHints(kLHintsLeft | kLHintsTop,20,20,20,20);
370
  // gumbi
371
 
372
  int nbut=8;
373
  const char *names[10]={"Inicializacija","Start","Stop","Osvezi","Zbrisi","Shrani v datoteko", "Natisni" , "Izhod"};
374
  for (int i=0;i<nbut;i++){
375
 
376
    gTextButton[i]= new TGTextButton(fH, names[i]);
377
    gTextButton[i]->SetTextJustify(36);
378
    gTextButton[i]->SetMargins(0,0,0,0);
379
    gTextButton[i]->SetWrapLength(-1);
380
    gTextButton[i]->ChangeOptions(gTextButton[i]->GetOptions() | kFixedWidth); // | kFixedSize
381
    gTextButton[i]->Resize(100,gTextButton[i]->GetDefaultHeight());
382
 
383
    fH->AddFrame(gTextButton[i], f0);
384
    char cmd[50];
385
    sprintf(cmd,"MyEventHandler(=%d)",i);
386
    TQObject::Connect(gTextButton[i],"Clicked()",0,0,cmd);
387
  }
388
 
389
  gCheckButton =  new TGCheckButton( fH,"Razhroscevanje");
390
  gCheckButton->Resize(50,22);
391
  TQObject::Connect(gCheckButton,"Clicked()", 0, 0 , "GetDebug()");
392
  gCheckButton->SetState(kButtonDown);
393
  fH->AddFrame(gCheckButton, f0);
394
 
395
  fMain->AddFrame(fH    , f0);
396
  //---------------------------------------------------------
397
  // ura
398
  TGLabel *lab1;
399
  fH=new TGHorizontalFrame(fMain,800,200);
400
  lab1 = new TGLabel( fH ,"Ura:");
401
  fH->AddFrame(lab1, f0);
402
  gTimeDisplay =  new TGTextEntry( fH,"");
403
  gTimeDisplay->Resize(200,22);
404
  fH->AddFrame(gTimeDisplay, f0);
405
  fMain->AddFrame(fH    , f0);
406
  //---------------------------------------------------------
407
  // inputi
408
  fH=new TGHorizontalFrame(fMain,800,200);
409
  lab1 = new TGLabel( fH ,"Ime datoteke:");
410
  fH->AddFrame(lab1, f0);
411
  gFilename =  new TGTextEntry( fH,"datoteka");
412
  gFilename->Resize(200,22);
413
  fH->AddFrame(gFilename, f0);
414
  fMain->AddFrame(fH    , f0);
415
  //---------------------------------------------------------
416
  fH=new TGHorizontalFrame(fMain,800,200);
417
  lab1 = new TGLabel( fH ,"Zeljeno stevilo dogodkov:");
418
  fH->AddFrame(lab1, f0);
419
  gMaxEve =  new TGNumberEntry( fH,10000);
420
  gMaxEve->Resize(100,22);
421
  fH->AddFrame(gMaxEve, f0);
422
  fMain->AddFrame(fH    , f0);
423
  //---------------------------------------------------------
424
  fH=new TGHorizontalFrame(fMain,800,200);
425
  lab1 = new TGLabel( fH ,"Zajetih dogodkov:");
426
  fH->AddFrame(lab1, f0);
427
  gNeve =  new TGNumberEntry( fH,0);
428
  gNeve->Resize(100,22);
429
  fH->AddFrame(gNeve, f0);
430
  fMain->AddFrame(fH    , f0);
431
  //---------------------------------------------------------
432
  // canvas
433
  fH=new TGHorizontalFrame(fMain,800,200);
434
  gCanvas = new TRootEmbeddedCanvas ("gCanvas",fH,800,400);
435
  fH->AddFrame(gCanvas, f0);
436
  fMain->AddFrame(fH   , f0);
437
  //------------------------------------------------------------ 
438
  fH=new TGHorizontalFrame(fMain,800,200);
439
  lab1 = new TGLabel( fH ,"St. kanala (0..31):");
440
  fH->AddFrame(lab1, f0);
441
  gCh =  new TGNumberEntry( fH,2);
442
  fH->AddFrame(gCh, f0);
443
  TQObject::Connect(gCh,"ValueSet(Long_t)",0,0,"Redraw(Long_t )");
444
 
445
  fMain->AddFrame(fH    , f0);
446
  //------------------------------------------------------------
447
  fMain->SetWindowName(WINDOW_NAME);
448
  fMain->MapSubwindows();
449
  fMain->Resize(fMain->GetDefaultSize());
450
  fMain->MapWindow();
451
  Redraw();
452
  GetDebug();
453
  return 0;
454
}