Subversion Repositories f9daq

Rev

Rev 167 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
146 f9daq 1
#include "../include/sipmscan.h"
2
#include "../include/workstation.h"
3
 
4
#include <stdio.h>
5
#include <stdlib.h>
6
 
7
int retTemp;
8
 
9
// Additional functions -------------------------------------
10
 
11
// Display the currently selected histogram in the file list
12
void TGAppMainFrame::DisplayHistogram(char *histfile, int histtype, int opt)
13
{
14
   char histtime[256];
15
   char ctemp[512];
16
 
17
   if(DBGSIG)
18
      printf("DisplayHistogram(): Selected file: %s\n", histfile);
19
 
20
   TCanvas *gCanvas = analysisCanvas->GetCanvas();
21
 
22
   inroot = TFile::Open(histfile, "READ");
23
 
24
   TTree *header_data, *meas_data;
25
   inroot->GetObject("header_data", header_data);
26
   inroot->GetObject("meas_data", meas_data);
27
 
28
   // Reading the header
29
   header_data->SetBranchAddress("nrch", &evtheader.nrch);
30
   header_data->GetEntry(0);
31
   header_data->SetBranchAddress("timestamp", &evtheader.timestamp);
32
   header_data->GetEntry(0);
33
   header_data->SetBranchAddress("biasvolt", &evtheader.biasvolt);
34
   header_data->GetEntry(0);
35
   header_data->SetBranchAddress("xpos", &evtheader.xpos);
36
   header_data->GetEntry(0);
37
   header_data->SetBranchAddress("ypos", &evtheader.ypos);
38
   header_data->GetEntry(0);
39
   header_data->SetBranchAddress("zpos", &evtheader.zpos);
40
   header_data->GetEntry(0);
41
   header_data->SetBranchAddress("temperature", &evtheader.temperature);
42
   header_data->GetEntry(0);
43
   if( header_data->FindBranch("angle") )
44
   {
45
      header_data->SetBranchAddress("angle", &evtheader.angle);
46
      header_data->GetEntry(0);
47
   }
48
   header_data->SetBranchAddress("laserinfo", &evtheader.laserinfo);
49
   header_data->GetEntry(0);
50
 
51
   // Change timestamp to local time
52
   GetTime(evtheader.timestamp, histtime);
53
 
54
   // Displaying header information debug
55
   if(DBGSIG)
56
   {
57
      printf("DisplayHistogram(): Opened file header information:\n");
58
      printf("- Number of channels (ADC and TDC are considered as separate channels): %d\n", evtheader.nrch);
59
      printf("- Timestamp: %d (%s)\n", evtheader.timestamp, histtime);
60
      printf("- Bias voltage: %lf\n", evtheader.biasvolt);
61
      printf("- Table position (X,Y,Z): %d, %d, %d\n", evtheader.xpos, evtheader.ypos, evtheader.zpos);
62
      if(evtheader.temperature)
63
         printf("- Temperature: %lf\n", evtheader.temperature);
64
      if( header_data->FindBranch("angle") )
65
         printf("- Incidence angle: %lf\n", evtheader.angle);
66
      else
67
         printf("- Incidence angle: No angle information!\n");
68
      printf("- Laser and filter settings: %s\n", evtheader.laserinfo);
69
   }
70
 
71
   // Displaying header information on the GUI
72
   dispTime->widgetTE->SetText(histtime);
73
   dispBias->widgetNE[0]->SetNumber(evtheader.biasvolt);
74
   sprintf(ctemp, "%d, %d, %d", evtheader.xpos, evtheader.ypos, evtheader.zpos);
75
   dispPos->widgetTE->SetText(ctemp);
76
   if(evtheader.temperature)
77
      dispTemp->widgetNE[0]->SetNumber(evtheader.temperature);
78
   else
79
      dispTemp->widgetNE[0]->SetNumber(0.0);
80
   if( header_data->FindBranch("angle") )
81
      dispAngle->widgetNE[0]->SetNumber(evtheader.angle);
82
   else
83
      dispAngle->widgetNE[0]->SetNumber(0.0);
84
   dispLaser->widgetTE->SetText(evtheader.laserinfo);
85
 
86
   selectCh->widgetNE[0]->SetLimitValues(0, (evtheader.nrch/2)-1);
87
 
88
   // Redraw the histograms
89
   int j;
90
   char rdc[256];
91
   char rdcsel[256];
92
 
93
   j = selectCh->widgetNE[0]->GetNumber();
94
 
95
   printf("Found %d data points.\n", (int)meas_data->GetEntries());
96
 
97
   gCanvas->cd();
98
   double range[4];
99
   range[0] = adcRange->widgetNE[0]->GetNumber();
100
   range[1] = adcRange->widgetNE[1]->GetNumber();
101
   range[2] = tdcRange->widgetNE[0]->GetNumber();
102
   range[3] = tdcRange->widgetNE[1]->GetNumber();
103
 
104
   // ADC histogram
105
   if(histtype == 0)
106
   {
107
      if( range[0] == range[1] )
108
         sprintf(rdc, "ADC%d>>%s", j, histname);
109
      else
110
         sprintf(rdc, "ADC%d>>%s(%d,%lf,%lf)", j, histname, (int)(range[1]-range[0]), range[0]-0.5, range[1]-0.5);
111
 
112
      sprintf(rdcsel, "(TDC%d>%lf)&&(TDC%d<%lf)", j, range[2]*tdctimeconversion, j, range[3]*tdctimeconversion);
113
      meas_data->Draw(rdc, rdcsel);
114
 
115
      sprintf(rdc, "ADC%d, Vbias=%.3lf, TDC=(%.2lf,%.2lf);ADC;", j, evtheader.biasvolt, range[2], range[3]);
116
      TH1F *histtemp = (TH1F*)gCanvas->GetPrimitive(histname);
117
      if(!cleanPlots)
118
         histtemp->SetTitle(rdc);
119
      else
120
         histtemp->SetTitle(";ADC;");
121
      histtemp->GetXaxis()->SetLabelSize(0.025);
122
      histtemp->GetXaxis()->CenterTitle(kTRUE);
123
      histtemp->GetYaxis()->SetLabelSize(0.025);
124
      if(cleanPlots)
125
      {
126
         TGaxis *yax = (TGaxis*)histtemp->GetYaxis();
127
         yax->SetMaxDigits(4);
128
      }
129
 
130
      gCanvas->Modified();
131
      gCanvas->Update();
132
 
133
      if( yRange->widgetNE[0]->GetNumber() != yRange->widgetNE[1]->GetNumber() )
134
      {
135
         if( (histOpt->widgetChBox[0]->IsDown()) && (yRange->widgetNE[0]->GetNumber() <= 0) )
136
         {
137
            histtemp->GetYaxis()->SetRangeUser(0.5, yRange->widgetNE[1]->GetNumber());
138
            yRange->widgetNE[0]->SetNumber(0.5);
139
            logChange = 1;
140
         }
141
         else
142
         {
143
            gCanvas->SetLogy(kFALSE);
144
            if(logChange == 1)
145
            {
146
               yRange->widgetNE[0]->SetNumber(0.0);
147
               logChange = 0;
148
            }
149
            histtemp->GetYaxis()->SetRangeUser(yRange->widgetNE[0]->GetNumber(), yRange->widgetNE[1]->GetNumber());
150
         }
151
      }
152
 
153
      TPaveStats *stats = (TPaveStats*)histtemp->FindObject("stats");
154
      if(!cleanPlots)
155
      {
156
         stats->SetX1NDC(0.84); stats->SetX2NDC(0.97);
157
         stats->SetY1NDC(0.87); stats->SetY2NDC(0.97);
158
      }
159
      else
160
      {
161
         stats->SetX1NDC(1.1); stats->SetX2NDC(1.3);
162
         stats->SetY1NDC(1.1); stats->SetY2NDC(1.3);
163
      }
164
   }
165
   // TDC histogram
166
   else if(histtype == 1)
167
   {
168
      if( range[0] == range[1] )
169
         sprintf(rdc, "(TDC%d/%lf)>>%s", j, tdctimeconversion, histname);
170
      else
171
         sprintf(rdc, "(TDC%d/%lf)>>%s(%d,%lf,%lf)", j, tdctimeconversion, histname, (int)((range[3]-range[2])*tdctimeconversion), range[2], range[3]);
172
 
173
      sprintf(rdcsel, "(TDC%d>%lf)&&(TDC%d<%lf)", j, range[2]*tdctimeconversion, j, range[3]*tdctimeconversion);
174
      meas_data->Draw(rdc, rdcsel);
175
 
176
      sprintf(rdc, "TDC%d, Vbias=%.3lf, TDC=(%.2lf,%.2lf);Time (TDC channel) [ns];", j, evtheader.biasvolt, range[2], range[3]);
177
      TH1F *histtemp = (TH1F*)gCanvas->GetPrimitive(histname);
178
      if(!cleanPlots)
179
         histtemp->SetTitle(rdc);
180
      else
181
         histtemp->SetTitle(";Time (TDC channel) [ns];");
182
      histtemp->GetXaxis()->SetLabelSize(0.025);
183
      histtemp->GetXaxis()->CenterTitle(kTRUE);
184
      histtemp->GetYaxis()->SetLabelSize(0.025);
185
      if(cleanPlots)
186
      {
187
         TGaxis *yax = (TGaxis*)histtemp->GetYaxis();
188
         yax->SetMaxDigits(4);
189
      }
190
 
191
      gCanvas->Modified();
192
      gCanvas->Update();
193
 
194
      if( yRange->widgetNE[0]->GetNumber() != yRange->widgetNE[1]->GetNumber() )
195
      {
196
         if( (histOpt->widgetChBox[0]->IsDown()) && (yRange->widgetNE[0]->GetNumber() <= 0) )
197
         {
198
            histtemp->GetYaxis()->SetRangeUser(0.5, yRange->widgetNE[1]->GetNumber());
199
            yRange->widgetNE[0]->SetNumber(0.5);
200
            logChange = 1;
201
         }
202
         else
203
         {
204
            gCanvas->SetLogy(kFALSE);
205
            if(logChange == 1)
206
            {
207
               yRange->widgetNE[0]->SetNumber(0.0);
208
               logChange = 0;
209
            }
210
            histtemp->GetYaxis()->SetRangeUser(yRange->widgetNE[0]->GetNumber(), yRange->widgetNE[1]->GetNumber());
211
         }
212
      }
213
 
214
      TPaveStats *stats = (TPaveStats*)histtemp->FindObject("stats");
215
      if(!cleanPlots)
216
      {
217
         stats->SetX1NDC(0.84); stats->SetX2NDC(0.97);
218
         stats->SetY1NDC(0.87); stats->SetY2NDC(0.97);
219
      }
220
      else
221
      {
222
         stats->SetX1NDC(1.1); stats->SetX2NDC(1.3);
223
         stats->SetY1NDC(1.1); stats->SetY2NDC(1.3);
224
      }
225
   }
226
   // ADC vs. TDC histogram
227
   else if(histtype == 2)
228
   {
229
      if( ((range[0] == range[1]) && (range[2] == range[3])) || (range[2] == range[3]) || (range[0] == range[1]) )
230
         sprintf(rdc, "(TDC%d/%lf):ADC%d>>%s", j, tdctimeconversion, j, histname);
231
      else
232
         sprintf(rdc, "(TDC%d/%lf):ADC%d>>%s(%d,%lf,%lf,%d,%lf,%lf)", j, tdctimeconversion, j, histname, (int)(range[1]-range[0])/2, range[0]-0.5, range[1]-0.5, (int)((range[3]-range[2])*tdctimeconversion)/2, range[2], range[3]);
233
      meas_data->Draw(rdc,"","COLZ");
234
 
235
      sprintf(rdc, "ADC/TDC%d, Vbias=%.3lf, TDC=(%.2lf,%.2lf);ADC;TDC", j, evtheader.biasvolt, range[2], range[3]);
236
      TH2F *histtemp = (TH2F*)gCanvas->GetPrimitive(histname);
237
      if(!cleanPlots)
238
         histtemp->SetTitle(rdc);
239
      else
240
         histtemp->SetTitle(";ADC;Time (TDC channel) [ns]");
241
      histtemp->GetXaxis()->SetLabelSize(0.025);
242
      histtemp->GetXaxis()->CenterTitle(kTRUE);
243
      histtemp->GetYaxis()->SetLabelSize(0.025);
244
      histtemp->GetYaxis()->CenterTitle(kTRUE);
245
      histtemp->GetYaxis()->SetTitleOffset(1.35);
246
      if(cleanPlots)
247
      {
248
         TGaxis *yax = (TGaxis*)histtemp->GetYaxis();
249
         yax->SetMaxDigits(4);
250
      }
251
 
252
      gCanvas->Modified();
253
      gCanvas->Update();
254
 
255
      TPaveStats *stats = (TPaveStats*)histtemp->FindObject("stats");
256
      stats->SetX1NDC(1.1); stats->SetX2NDC(1.3);
257
      stats->SetY1NDC(1.1); stats->SetY2NDC(1.3);
258
 
259
      TPaletteAxis *gpalette = (TPaletteAxis*)histtemp->GetListOfFunctions()->FindObject("palette");
260
      gpalette->SetLabelSize(0.022);
261
   }
262
 
263
   if(histtype < 2)
264
   {
265
      if( histOpt->widgetChBox[0]->IsDown() )
266
         gCanvas->SetLogy(kTRUE);
267
      else if( !histOpt->widgetChBox[0]->IsDown() )
268
         gCanvas->SetLogy(kFALSE);
269
   }
270
   else
271
      gCanvas->SetLogy(kFALSE);
272
 
273
   gCanvas->Modified();
274
   gCanvas->Update();
275
 
276
   delete header_data;
277
   delete meas_data;
278
 
279
   // Delete the opened file when we just display it in the analysis canvas (otherwise wait for histogram save)
280
   if(opt != 1)
281
      delete inroot;
282
 
283
   // If you close the opened file (delete inroot), the data can not be accessed by other functions (any time we wish to use the data directly from histogram, we need to call the DisplayHistogram function -> using different opt to determine what we need to do)
284
}
285
 
286
// Start a measurement (acquisition from CAMAC)
287
void TGAppMainFrame::RunMeas(void *ptr, int runCase, int &scanon)
288
{
289
   int vscan = 0, pscan = 0, zscan = 0, ascan = 0;
290
   if(scansOn->widgetChBox[0]->IsDown()) vscan = 1;
291
   if(scansOn->widgetChBox[1]->IsDown()) pscan = 1;
292
   if(scansOn->widgetChBox[2]->IsDown()) zscan = 1;
293
   if(scansOn->widgetChBox[3]->IsDown()) ascan = 1;
294
 
295
   printf("Start of Run, run case %d\n", runCase);
296
 
297
   float progVal;
298
 
299
   char ctemp[256];
300
   char ctemp2[256];
301
   char fname[256];
302
   int itemp = 0;
303
   TH1F *liveHist;
304
 
305
   float minVoltage, maxVoltage, stepVoltage, diffVoltage;
306
   float minXpos, maxXpos, stepXpos, diffXpos;
307
   float minYpos, maxYpos, stepYpos, diffYpos;
308
   float minZpos, maxZpos, stepZpos, diffZpos;
309
   float minAlpha, maxAlpha, stepAlpha, diffAlpha;
310
 
311
   minVoltage = vOutStart->widgetNE[0]->GetNumber();
312
   maxVoltage = vOutStop->widgetNE[0]->GetNumber();
313
   diffVoltage = abs(maxVoltage - minVoltage);
314
   stepVoltage = abs(vOutStep->widgetNE[0]->GetNumber());
315
   minXpos = xPosMin->widgetNE[0]->GetNumber();
316
   maxXpos = xPosMax->widgetNE[0]->GetNumber();
317
   diffXpos = abs(maxXpos - minXpos);
318
   stepXpos = abs(xPosStep->widgetNE[0]->GetNumber());
319
   minYpos = yPosMin->widgetNE[0]->GetNumber();
320
   maxYpos = yPosMax->widgetNE[0]->GetNumber();
321
   diffYpos = abs(maxYpos - minYpos);
322
   stepYpos = abs(yPosStep->widgetNE[0]->GetNumber());
323
   minZpos = zPosMin->widgetNE[0]->GetNumber();
324
   maxZpos = zPosMax->widgetNE[0]->GetNumber();
325
   diffZpos = abs(maxZpos - minZpos);
326
   stepZpos = abs(zPosStep->widgetNE[0]->GetNumber());
327
   minAlpha = rotPosMin->widgetNE[0]->GetNumber();
328
   maxAlpha = rotPosMax->widgetNE[0]->GetNumber();
329
   diffAlpha = abs(maxAlpha - minAlpha);
330
   stepAlpha = abs(rotPosStep->widgetNE[0]->GetNumber());
331
 
332
   remove_ext((char*)fileName->widgetTE->GetText(), ctemp);
333
 
334
   // TODO - angle scan + voltage scan
335
   // Voltage or surface scan
336
   if( vscan || pscan || ascan )
337
   {
338
      // No Z scan, No angle scan
339
      if(!zscan && !ascan)
340
      {
341
         // When we have a voltage scan
342
         if( vscan && (stepVoltage > 0.) )
343
            SeqNumber(runCase, (int)diffVoltage/stepVoltage, ctemp2);
344
         // With only a surface scan
345
         else if(pscan)
346
         {
347
            if( stepXpos == 0 )
348
               itemp = 1;
349
            else
350
               itemp = (int)diffXpos/stepXpos;
351
 
352
            if( stepYpos == 0 )
353
               itemp *= 1;
354
            else
355
               itemp *= (int)diffYpos/stepYpos;
356
            SeqNumber(runCase, itemp, ctemp2);
357
         }
358
         sprintf(fname, "%s_%s%s", ctemp, ctemp2, histext);
359
      }
360
      // With Z scan, No angle scan
361
      else if(zscan && !ascan)
362
      {
363
         SeqNumber((int)zPos->widgetNE[0]->GetNumber(), maxZpos, ctemp2);
364
 
365
         // Voltage scan is on
366
         if( vscan && (stepVoltage > 0.) )
367
         {
368
            sprintf(fname, "%s_z%s_", ctemp, ctemp2);
369
            SeqNumber(runCase, (int)diffVoltage/stepVoltage+1, ctemp2);
370
            strcat(fname, ctemp2);
371
            strcat(fname, histext);
372
         }
373
         // Surface scan is on
374
         else if(pscan)
375
         {
376
            sprintf(fname, "%s_z%s_", ctemp, ctemp2);
377
 
378
            if( stepXpos == 0 )
379
               itemp = 1;
380
            else
381
               itemp = (int)diffXpos/stepXpos+1;
382
 
383
            if( stepYpos == 0 )
384
               itemp *= 1;
385
            else
386
               itemp *= (int)diffYpos/stepYpos+1;
387
            SeqNumber(runCase, itemp, ctemp2);
388
            strcat(fname, ctemp2);
389
            strcat(fname, histext);
390
         }
391
         // Just Z scan
392
         else
393
            sprintf(fname, "%s_z%s%s", ctemp, ctemp2, histext);
394
      }
395
      // No Z scan, With angle scan
396
      else if(!zscan && ascan)
397
      {
398
         SeqNumber(runCase, (int)diffAlpha/stepAlpha, ctemp2);
399
 
400
         // Voltage scan is on
401
         if( vscan && (stepVoltage > 0.) )
402
         {
403
            sprintf(fname, "%s_phi%s_", ctemp, ctemp2);
404
            SeqNumber(runCase, (int)diffVoltage/stepVoltage+1, ctemp2);
405
            strcat(fname, ctemp2);
406
            strcat(fname, histext);
407
         }
408
         // Just angle scan
409
         else
410
            sprintf(fname, "%s_phi%s%s", ctemp, ctemp2, histext);
411
      }
412
   }
413
   // All the rest
414
   else if(!vscan && !pscan)
415
      sprintf(fname, "%s%s", ctemp, histext);
416
 
417
   // Check if set voltage is below the hard limit
418
   if( vOut->widgetNE[0]->GetNumber() > vHardlimit->widgetNE[0]->GetNumber() )
419
   {
420
      printf("Voltage hard limit triggered (%lf > %lf)!\n", vOut->widgetNE[0]->GetNumber(), vHardlimit->widgetNE[0]->GetNumber() );
421
      vOut->widgetNE[0]->SetNumber( vHardlimit->widgetNE[0]->GetNumber() );
422
   }
423
 
424
   printf("Output file is (runCase = %d): %s\n", runCase, fname);
425
 
426
   // Writing to output file
427
   outroot = TFile::Open(fname, "RECREATE");
428
 
429
   TTree *header_data = new TTree("header_data", "Header information for the measurement.");
430
   TTree *meas_data = new TTree("meas_data", "Saved ADC and TDC measurement data.");
431
   TTree *scope_data = new TTree("scope_data", "Saved scope measurement data.");
432
 
433
   // Branches for the header
434
   header_data->Branch("nrch", &evtheader.nrch, "nrch/I");
435
   header_data->Branch("timestamp", &evtheader.timestamp, "timestamp/I");
436
   header_data->Branch("biasvolt", &evtheader.biasvolt, "biasvolt/D");
437
   header_data->Branch("xpos", &evtheader.xpos, "xpos/I");
438
   header_data->Branch("ypos", &evtheader.ypos, "ypos/I");
439
   header_data->Branch("zpos", &evtheader.zpos, "zpos/I");
440
   header_data->Branch("temperature", &evtheader.temperature, "temperature/D");
441
   header_data->Branch("angle", &evtheader.angle, "angle/D");
442
   header_data->Branch("laserinfo", &evtheader.laserinfo, "laserinfo/C");
443
 
444
   evtheader.nrch = (int)NCH->widgetNE[0]->GetNumber()*2;
445
   evtheader.timestamp = (int)time(NULL);
446
   evtheader.biasvolt = (double)vOut->widgetNE[0]->GetNumber();
447
   if(posUnits->widgetCB->GetSelected() == 0)
448
   {
449
      evtheader.xpos = (int)xPos->widgetNE[0]->GetNumber();
450
      evtheader.ypos = (int)yPos->widgetNE[0]->GetNumber();
451
      evtheader.zpos = (int)zPos->widgetNE[0]->GetNumber();
452
   }
453
   else if(posUnits->widgetCB->GetSelected() == 1)
454
   {
455
      evtheader.xpos = (int)xPos->widgetNE[0]->GetNumber()/lenconversion;
456
      evtheader.ypos = (int)yPos->widgetNE[0]->GetNumber()/lenconversion;
457
      evtheader.zpos = (int)zPos->widgetNE[0]->GetNumber()/lenconversion;
458
   }
459
   evtheader.temperature = (double)chtemp->widgetNE[0]->GetNumber();
460
   if(rotUnits->widgetCB->GetSelected() == 0)
461
      evtheader.angle = (double)rotPos->widgetNE[0]->GetNumber()*rotconversion;
462
   else if(rotUnits->widgetCB->GetSelected() == 1)
463
      evtheader.angle = (double)rotPos->widgetNE[0]->GetNumber();
464
   sprintf(evtheader.laserinfo, "%s", laserInfo->widgetTE->GetText());
465
 
466
   char histtime[256];
467
   GetTime(evtheader.timestamp, histtime);
468
 
469
   printf("Save file header information:\n");
470
   printf("- Number of channels: %d\n", evtheader.nrch);
471
   printf("- Timestamp: %d (%s)\n", evtheader.timestamp, histtime);
472
   printf("- Bias voltage: %lf\n", evtheader.biasvolt);
473
   printf("- Table position (X,Y,Z): %d, %d, %d\n", evtheader.xpos, evtheader.ypos, evtheader.zpos);
474
   printf("- Temperature: %lf\n", evtheader.temperature);
475
   printf("- Incidence angle: %lf\n", evtheader.angle);
476
   printf("- Laser and filter settings: %s\n", evtheader.laserinfo);
477
 
478
   header_data->Fill();
479
 
480
   // Branches for ADC and TDC data
481
   for(int i = 0; i < evtheader.nrch/2; i++)
482
   {
483
      sprintf(ctemp, "ADC%d", i);
484
      sprintf(fname, "ADC%d/I", i);
485
      meas_data->Branch(ctemp, &evtdata.adcdata[i], fname);
486
 
487
      sprintf(ctemp, "TDC%d", i);
488
      sprintf(fname, "TDC%d/I", i);
489
      meas_data->Branch(ctemp, &evtdata.tdcdata[i], fname);
490
   }
491
 
492
   //TODO
493
   // Initialize the scope before measurement
494
/*   if( sCamaclink->IsDown() )
495
      InitializeScope();*/
496
 
497
   // Branch for scope measurement data
498
/*   if(gScopeDaq->scopeUseType == 2) // only if we select waveform measurement
499
   {
500
      if(gScopeDaq->scopeMeasSel == 0)
501
         scope_data->Branch("amp", &evtmeas.measdata, "amp/D");
502
      else if(gScopeDaq->scopeMeasSel == 1)
503
         scope_data->Branch("area", &evtmeas.measdata, "area/D");
504
      else if(gScopeDaq->scopeMeasSel == 2)
505
         scope_data->Branch("delay", &evtmeas.measdata, "delay/D");
506
      else if(gScopeDaq->scopeMeasSel == 3)
507
         scope_data->Branch("fall", &evtmeas.measdata, "fall/D");
508
      else if(gScopeDaq->scopeMeasSel == 4)
509
         scope_data->Branch("freq", &evtmeas.measdata, "freq/D");
510
      else if(gScopeDaq->scopeMeasSel == 5)
511
         scope_data->Branch("max", &evtmeas.measdata, "max/D");
512
      else if(gScopeDaq->scopeMeasSel == 6)
513
         scope_data->Branch("mean", &evtmeas.measdata, "mean/D");
514
      else if(gScopeDaq->scopeMeasSel == 7)
515
         scope_data->Branch("min", &evtmeas.measdata, "min/D");
516
      else if(gScopeDaq->scopeMeasSel == 8)
517
         scope_data->Branch("pk2p", &evtmeas.measdata, "pk2p/D");
518
      else if(gScopeDaq->scopeMeasSel == 9)
519
         scope_data->Branch("pwidth", &evtmeas.measdata, "pwidth/D");
520
      else if(gScopeDaq->scopeMeasSel == 10)
521
         scope_data->Branch("rise", &evtmeas.measdata, "rise/D");
522
   }*/
523
 
524
   int neve  = (int) evtNum->widgetNE[0]->GetNumber();
525
   int allEvt, zProg;
526
   zProg = 1;
527
 
528
#if WORKSTAT == 'I'
529
#else
530
// ONLY FOR TESTING!
531
   TRandom *randNum = new TRandom();
532
   randNum->SetSeed(0);
533
// ONLY FOR TESTING!
534
#endif
535
 
536
   // Initialize the CAMAC
537
   if (gDaq)
538
   {
539
      if(scanon == 0)
540
      {
541
         gDaq->init(evtheader.nrch);
542
         scanon = 1;
543
      }
544
      gDaq->fStop=0;
545
 
546
      // Set the stopwatch
547
      clock_t clkt1;
548
 
549
      // Prepare histogram for live histogram update
550
      int liven;
551
      TCanvas *gCanvas;
552
      if(liveUpdate && (!vscan && !pscan && !zscan && !ascan))
553
      {
554
         gCanvas = measCanvas->GetCanvas();
555
         gCanvas->SetGrid();
556
         gCanvas->cd();
557
         liveHist = new TH1F(histname,"",(int)TMath::Sqrt(neve),0,0);
558
         liven = 1;
559
      }
560
 
561
      // Start gathering
562
      gDaq->start();
563
 
564
      for (int n=0;n<neve && !gDaq->fStop ;/*n++*/)
565
      {
566
         int nb = gDaq->event(gBuf,BSIZE);
567
 
568
#if WORKSTAT == 'I'
569
#else
570
// ONLY FOR TESTING!
571
         for(int i=0; i < evtheader.nrch; i++)
572
         {
573
            if(i == 1)
574
               gBuf[i] = randNum->Gaus(1500,300);
575
            else if(i == 0)
576
               gBuf[i] = randNum->Poisson(2500);
577
         }
578
// ONLY FOR TESTING!
579
#endif
580
         if (nb<=0) n--;
581
 
582
         int nc=0;
583
 
584
         while ( (nb>0) && (n<neve) )
585
         {
586
            for(int i = 0; i < evtheader.nrch; i++)
587
            {
588
               unsigned short adc = gBuf[i+nc]&0xFFFF;
589
               if(i % 2 == 0)           // TDC
590
                  evtdata.tdcdata[i/2] = (int)adc;
591
               else if(i % 2 == 1)      // ADC
592
                  evtdata.adcdata[i/2] = (int)adc;
593
 
594
               // Start plotting the scope waveform
595
/*             if( (gScopeDaq->scopeUseType == 1) && (sCamaclink->IsDown()) )
596
                  StartScopeAcq();*/ // TODO
597
            }
598
            meas_data->Fill();
599
            n++;
600
 
601
            // Start making a scope measurement
602
/*          if( (gScopeDaq->scopeUseType == 2) && (sCamaclink->IsDown()) )
603
            {
604
               StartScopeAcq();
605
               evtmeas.measdata = gScopeDaq->measubuf;
606
            }
607
            scope_data->Fill();*/ // TODO
608
 
609
            // Start filling the histogram (only in normal single scan)
610
            if(liveUpdate && (!vscan && !pscan && !zscan && !ascan))
611
            {
612
               liveHist->Fill(evtdata.adcdata[0]);
613
               if( n == (neve*liven)/10 )
614
               {
615
                  gCanvas->cd();
616
                  liveHist->Draw("");
617
                  gCanvas->Modified();
618
                  gCanvas->Update();
619
                  liven++;
620
               }
621
            }
622
 
623
            nc += evtheader.nrch;
624
            nb -= evtheader.nrch;
625
         }
626
 
627
         MyTimer();
628
         allEvt = n;
629
         if (gSystem->ProcessEvents()) printf("Run Interrupted\n");
630
 
631
         if( acqStarted && (n == (neve*zProg)/10) && (!vscan && !pscan && !zscan && !ascan) )
632
         {
633
            // Progress the progress bar
634
            progVal = (float)zProg*10;
635
            measProgress->widgetPB->SetPosition(progVal);
636
 
637
            // Calculate the remaining time
638
            TimeEstimate(clkt0, timet0, progVal, ctemp, 0);
639
            printf("End time: %s\n", ctemp);
640
            measProgress->widgetTE->SetText(ctemp);
641
 
642
            gVirtualX->Update(1);
643
            zProg++;
644
         }
645
      }
646
 
647
      printf("Number of gathered events: %d\n", allEvt);
648
      measProgress->widgetTB[0]->SetText("Start acquisition");
649
      acqStarted = false;
650
 
651
      gDaq->stop();
652
   }
653
 
654
   printf("End of Run neve=%d\n",neve);
655
 
656
   header_data->Write();
657
   meas_data->Write();
658
//   scope_data->Write(); // TODO
659
   delete header_data;
660
   delete meas_data;
661
   delete scope_data;
662
 
663
   // Remove the histogram
664
   if(liveUpdate && (!vscan && !pscan && !zscan && !ascan))
665
      delete liveHist;
666
 
667
 
668
   outroot->Close();
669
}
670
 
671
int TGAppMainFrame::MyTimer()
672
{
673
   char cmd[100];
674
   GetTime(-1, cmd);
675
   if (timeStamp) timeStamp->widgetTE->SetText(cmd);
676
   return 0;
677
}
678
 
679
// Additional functions -------------------------------------
680
 
681
// Settings pane connections --------------------------------
682
 
683
// Enable or disable scans
684
void TGAppMainFrame::EnableScan(int type)
685
{
686
   // Voltage scan
687
   if(type == 0)
688
   {
689
      if(scansOn->widgetChBox[type]->IsOn())
690
      {
691
         vOutStart->widgetNE[0]->SetState(kTRUE);
692
         vOutStop->widgetNE[0]->SetState(kTRUE);
693
         vOutStep->widgetNE[0]->SetState(kTRUE);
694
      }
695
      else
696
      {
697
         vOutStart->widgetNE[0]->SetState(kFALSE);
698
         vOutStop->widgetNE[0]->SetState(kFALSE);
699
         vOutStep->widgetNE[0]->SetState(kFALSE);
700
      }
701
   }
702
   // Surface (X, Y axis) scan
703
   else if(type == 1)
704
   {
705
      if(scansOn->widgetChBox[type]->IsOn())
706
      {
707
         xPosMin->widgetNE[0]->SetState(kTRUE);
708
         xPosMax->widgetNE[0]->SetState(kTRUE);
709
         xPosStep->widgetNE[0]->SetState(kTRUE);
710
         yPosMin->widgetNE[0]->SetState(kTRUE);
711
         yPosMax->widgetNE[0]->SetState(kTRUE);
712
         yPosStep->widgetNE[0]->SetState(kTRUE);
713
      }
714
      else
715
      {
716
         xPosMin->widgetNE[0]->SetState(kFALSE);
717
         xPosMax->widgetNE[0]->SetState(kFALSE);
718
         xPosStep->widgetNE[0]->SetState(kFALSE);
719
         yPosMin->widgetNE[0]->SetState(kFALSE);
720
         yPosMax->widgetNE[0]->SetState(kFALSE);
721
         yPosStep->widgetNE[0]->SetState(kFALSE);
722
      }
723
   }
724
   // Z axis scan
725
   else if(type == 2)
726
   {
727
      if(scansOn->widgetChBox[type]->IsOn())
728
      {
729
         zPosMin->widgetNE[0]->SetState(kTRUE);
730
         zPosMax->widgetNE[0]->SetState(kTRUE);
731
         zPosStep->widgetNE[0]->SetState(kTRUE);
732
      }
733
      else
734
      {
735
         zPosMin->widgetNE[0]->SetState(kFALSE);
736
         zPosMax->widgetNE[0]->SetState(kFALSE);
737
         zPosStep->widgetNE[0]->SetState(kFALSE);
738
      }
739
   }
740
   // Incidence angle scan
741
   else if(type == 3)
742
   {
743
      if(scansOn->widgetChBox[type]->IsOn())
744
      {
745
         rotPosMin->widgetNE[0]->SetState(kTRUE);
746
         rotPosMax->widgetNE[0]->SetState(kTRUE);
747
         rotPosStep->widgetNE[0]->SetState(kTRUE);
748
      }
749
      else
750
      {
751
         rotPosMin->widgetNE[0]->SetState(kFALSE);
752
         rotPosMax->widgetNE[0]->SetState(kFALSE);
753
         rotPosStep->widgetNE[0]->SetState(kFALSE);
754
      }
755
   }
756
}
757
 
758
// Apply the upper voltage limit from settings pane to main window
759
void TGAppMainFrame::VoltageLimit()
760
{
761
   vOut->widgetNE[0]->SetLimitValues(0, vHardlimit->widgetNE[0]->GetNumber() );
762
}
763
 
764
// Select the table position units to be used (1 = 0.3595 micron)
765
void TGAppMainFrame::ChangeUnits(int type)
766
{
767
   int pos[12], poslim[3], chng = 0;
768
   double micro[12], microlim[3];
769
 
770
   TGNumberEntry *posEntries[12];
771
   posEntries[0] = (TGNumberEntry*)xPos->widgetNE[0];
772
   posEntries[1] = (TGNumberEntry*)yPos->widgetNE[0];
773
   posEntries[2] = (TGNumberEntry*)zPos->widgetNE[0];
774
   posEntries[3] = (TGNumberEntry*)xPosMin->widgetNE[0];
775
   posEntries[4] = (TGNumberEntry*)xPosMax->widgetNE[0];
776
   posEntries[5] = (TGNumberEntry*)xPosStep->widgetNE[0];
777
   posEntries[6] = (TGNumberEntry*)yPosMin->widgetNE[0];
778
   posEntries[7] = (TGNumberEntry*)yPosMax->widgetNE[0];
779
   posEntries[8] = (TGNumberEntry*)yPosStep->widgetNE[0];
780
   posEntries[9] = (TGNumberEntry*)zPosMin->widgetNE[0];
781
   posEntries[10] = (TGNumberEntry*)zPosMax->widgetNE[0];
782
   posEntries[11] = (TGNumberEntry*)zPosStep->widgetNE[0];
783
 
784
   // Table position values
785
   if(type == 0)
786
   {
787
      // Check if we had microns before
788
      if(posEntries[0]->GetNumStyle() == TGNumberFormat::kNESRealTwo)
789
         chng = 1;
790
 
791
      // Change to table position values   
792
      if(chng == 1)
793
      {
794
         for(int i = 0; i < 12; i++)
795
         {
796
            if(posEntries[i]->GetNumber() == 0.0)
797
               pos[i] = 0;
798
            else
799
               pos[i] = (int)posEntries[i]->GetNumber()/lenconversion;
800
         }
801
 
802
         poslim[0] = -100;
803
         poslim[1] = 215000;
804
         poslim[2] = 375000;
805
 
806
         for(int i = 0; i < 12; i++)
807
         {
808
            posEntries[i]->SetNumStyle(TGNumberFormat::kNESInteger);
809
            if( (i > 8) || (i == 2) ) // limits for Z axis (longer table)
810
               posEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, poslim[0], poslim[2]);
811
            else
812
               posEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, poslim[0], poslim[1]);
813
 
814
            posEntries[i]->SetNumber(pos[i]);
815
         }
816
      }
817
   }
818
   // Microns
819
   else if(type == 1)
820
   {
821
      // Check if we had table position values before
822
      if(posEntries[0]->GetNumStyle() == TGNumberFormat::kNESInteger)
823
         chng = 1;
824
 
825
      // Change to microns   
826
      if(chng == 1)
827
      {
828
         for(int i = 0; i < 12; i++)
829
         {
830
            if(posEntries[i]->GetNumber() == 0.0)
831
               micro[i] = 0.;
832
            else
833
               micro[i] = (double)posEntries[i]->GetNumber()*lenconversion;
834
         }
835
 
836
         microlim[0] = (double)-100*lenconversion;
837
         microlim[1] = (double)215000*lenconversion;
838
         microlim[2] = (double)375000*lenconversion;
839
 
840
         for(int i = 0; i < 12; i++)
841
         {
842
            posEntries[i]->SetNumStyle(TGNumberFormat::kNESRealTwo);
843
            if( (i > 8) || (i == 2) ) // limits for Z axis (longer table)
844
               posEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, microlim[0], microlim[2]);
845
            else
846
               posEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, microlim[0], microlim[1]);
847
 
848
            posEntries[i]->SetNumber(micro[i]);
849
         }
850
      }
851
   }
852
}
853
 
854
// Select the rotation units to be used (1 = 6.3281/3600 degrees)
855
void TGAppMainFrame::ChangeUnitsRot(int type)
856
{
857
   int rot[4], rotlim[2], chng = 0;
858
   double deg[4], deglim[2];
859
 
860
   TGNumberEntry *rotEntries[4];
861
   rotEntries[0] = (TGNumberEntry*)rotPos->widgetNE[0];
862
   rotEntries[1] = (TGNumberEntry*)rotPosMin->widgetNE[0];
863
   rotEntries[2] = (TGNumberEntry*)rotPosMax->widgetNE[0];
864
   rotEntries[3] = (TGNumberEntry*)rotPosStep->widgetNE[0];
865
 
866
   // Rotation values
867
   if(type == 0)
868
   {
869
      // Check if we had degrees before
870
      if(rotEntries[0]->GetNumStyle() == TGNumberFormat::kNESRealTwo)
871
         chng = 1;
872
 
873
      // Change to rotation values   
874
      if(chng == 1)
875
      {
876
         for(int i = 0; i < 4; i++)
877
         {
878
            if(rotEntries[i]->GetNumber() == 0.0)
879
               rot[i] = 0;
880
            else
881
               rot[i] = (int)rotEntries[i]->GetNumber()/rotconversion;
882
         }
883
 
884
         rotlim[0] = (int)-180/rotconversion;
885
         rotlim[1] = (int)180/rotconversion;
886
 
887
         for(int i = 0; i < 4; i++)
888
         {
889
            rotEntries[i]->SetNumStyle(TGNumberFormat::kNESInteger);
890
            rotEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, rotlim[0], rotlim[1]);
891
 
892
            rotEntries[i]->SetNumber(rot[i]);
893
         }
894
      }
895
   }
896
   // Degree
897
   else if(type == 1)
898
   {
899
      // Check if we had table position values before
900
      if(rotEntries[0]->GetNumStyle() == TGNumberFormat::kNESInteger)
901
         chng = 1;
902
 
903
      // Change to degrees   
904
      if(chng == 1)
905
      {
906
         for(int i = 0; i < 4; i++)
907
         {
908
            if(rotEntries[i]->GetNumber() == 0)
909
               deg[i] = 0.;
910
            else
911
               deg[i] = (double)rotEntries[i]->GetNumber()*rotconversion;
912
         }
913
 
914
         deglim[0] = -180.;
915
         deglim[1] = 180.;
916
 
917
         for(int i = 0; i < 4; i++)
918
         {
919
            rotEntries[i]->SetNumStyle(TGNumberFormat::kNESRealTwo);
920
            rotEntries[i]->SetLimits(TGNumberFormat::kNELLimitMinMax, deglim[0], deglim[1]);
921
 
922
            rotEntries[i]->SetNumber(deg[i]);
923
         }
924
      }
925
   }
926
}
927
 
928
// Enable display canvas to have a live update of histogram
929
void TGAppMainFrame::EnableLiveUpdate()
930
{
931
   liveUpdate = liveDisp->widgetChBox[0]->IsDown();
932
}
933
 
934
// Settings pane connections --------------------------------
935
 
936
// Main measurement window connections ----------------------
937
 
938
// Get the currently selected channel
939
int TGAppMainFrame::GetChannel()
940
{
941
   int selectedOutput;
942
   if(vOutCh->widgetCB->GetSelected() < 8) selectedOutput = (vOutCh->widgetCB->GetSelected())+1;
943
   else if( (vOutCh->widgetCB->GetSelected() >= 8) && (vOutCh->widgetCB->GetSelected() < 16) ) selectedOutput = (vOutCh->widgetCB->GetSelected())+93;
944
   else selectedOutput = 1;
945
 
946
   return selectedOutput;
947
}
948
 
949
// Set, get or reset the output voltage
950
void TGAppMainFrame::VoltOut(int opt)
951
{
952
   char cmd[256];
953
 
954
   // Set the selected voltage
955
   if(opt == 0)
956
   {
957
      int outOn;
958
      float outputVoltage;
959
 
960
      outputVoltage = vOut->widgetNE[0]->GetNumber();
961
 
962
      if(vOutOpt->widgetChBox[1]->IsOn()) outOn = 1;
963
      else outOn = 0;
964
 
965
      fflush(stdout);
966
      sprintf(cmd, "%s/src/mpod/mpod_voltage.sh -o %d -v %f -s %d", rootdir, GetChannel(), outputVoltage, outOn);
967
#if WORKSTAT == 'I'
968
      retTemp = system(cmd);
969
#else
970
      printf("Cmd: %s\n",cmd);
971
#endif
972
      fflush(stdout);
973
   }
974
   // Get current voltage
975
   else if(opt == 1)
976
   {
977
      fflush(stdout);
978
      sprintf(cmd, "%s/src/mpod/mpod_voltage.sh -o %d -g > %s/settings/curvolt.txt", rootdir, GetChannel(), rootdir);
979
#if WORKSTAT == 'I'
980
      retTemp = system(cmd);
981
#else
982
      printf("Cmd: %s\n",cmd);
983
#endif
984
      fflush(stdout);
985
 
986
#if WORKSTAT == 'I'
987
      FILE* fvolt;
988
      double dtemp;
989
      char ctemp[24];
990
      sprintf(cmd, "%s/settings/curvolt.txt", rootdir);
991
      fvolt = fopen(cmd, "r");
992
 
993
      if(fvolt != NULL)
994
      {
995
         sprintf(cmd, "WIENER-CRATE-MIB::outputVoltage.u%d = Opaque: Float: %s V\n", GetChannel()-1, "%lf" );
996
         retTemp = fscanf(fvolt, cmd, &dtemp);
997
         vOut->widgetNE[0]->SetNumber(dtemp);
998
         sprintf(cmd, "WIENER-CRATE-MIB::outputSwitch.u%d = INTEGER: %s\n", GetChannel()-1, "%s" );
999
         retTemp = fscanf(fvolt, cmd, ctemp);
1000
         if( strcmp(ctemp, "On(1)") == 0 )
1001
            vOutOpt->widgetChBox[1]->SetState(kButtonDown);
1002
         else if( strcmp(ctemp, "Off(0)") == 0 )
1003
            vOutOpt->widgetChBox[1]->SetState(kButtonUp);
1004
      }
1005
 
1006
      fclose(fvolt);
1007
#endif
1008
   }
1009
   // Reset output voltage (if stuck in interlock)
1010
   else if(opt == 2)
1011
   {
1012
      vOut->widgetNE[0]->SetNumber(0.000);
1013
      vOutOpt->widgetChBox[1]->SetState(kButtonUp);
1014
 
1015
      fflush(stdout);
1016
      sprintf(cmd, "%s/src/mpod/mpod_voltage.sh -r %d", rootdir, GetChannel());
1017
#if WORKSTAT == 'I'
1018
      retTemp = system(cmd);
1019
#else
1020
      printf("Cmd: %s\n",cmd);
1021
#endif
1022
      fflush(stdout);
1023
   }
1024
}
1025
 
1026
// Set output voltage polarity to negative
1027
void TGAppMainFrame::NegativePolarity()
1028
{
1029
   double newHardlimit;
1030
   int polar = 0;  // 0 = positive, 1 = negative
1031
 
1032
   if(vOutOpt->widgetChBox[0]->IsOn())
1033
      polar = 1;
1034
   else
1035
      polar = 0;
1036
 
1037
   // Set hard limit to the negative version of what it was before
1038
   if( (vHardlimit->widgetNE[0]->GetNumber() > 0.) && (polar == 1) )
1039
      newHardlimit = -(vHardlimit->widgetNE[0]->GetNumber());
1040
   else if( (vHardlimit->widgetNE[0]->GetNumber() < 0.) && (polar == 0) )
1041
      newHardlimit = -(vHardlimit->widgetNE[0]->GetNumber());
1042
   else if(vHardlimit->widgetNE[0]->GetNumber() == 0.)
1043
      newHardlimit = 0.;
1044
   else
1045
      newHardlimit = vHardlimit->widgetNE[0]->GetNumber();
1046
 
1047
   // Apropriately set the limit to the output voltage number entry
1048
   vHardlimit->widgetNE[0]->SetNumber(newHardlimit);
1049
 
1050
   if(polar == 1)
1051
      vOut->widgetNE[0]->SetLimits(TGNumberFormat::kNELLimitMinMax, newHardlimit, 0.);
1052
   else if(polar == 0)
1053
      vOut->widgetNE[0]->SetLimits(TGNumberFormat::kNELLimitMinMax, 0., newHardlimit);
1054
}
1055
 
1056
// Set, get, home or reset the table position
1057
void TGAppMainFrame::PositionSet(int opt)
1058
{
1059
   char cmd[1024];
1060
 
1061
   // Set the selected table position
1062
   if(opt == 0)
1063
   {
1064
      int positX, positY, positZ;
1065
 
1066
      if(posUnits->widgetCB->GetSelected() == 0)
1067
      {
1068
         positX = xPos->widgetNE[0]->GetNumber();
1069
         positY = yPos->widgetNE[0]->GetNumber();
1070
         positZ = zPos->widgetNE[0]->GetNumber();
1071
      }
1072
      else if(posUnits->widgetCB->GetSelected() == 1)
1073
      {
1074
         positX = (int)xPos->widgetNE[0]->GetNumber()/lenconversion;
1075
         positY = (int)yPos->widgetNE[0]->GetNumber()/lenconversion;
1076
         positZ = (int)zPos->widgetNE[0]->GetNumber()/lenconversion;
1077
      }
1078
 
1079
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 1 -c m", rootdir, positX, rootdir);
1080
#if WORKSTAT == 'I'
1081
      retTemp = system(cmd);
1082
#else
1083
      printf("Cmd: %s\n",cmd);
1084
#endif
1085
 
1086
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 2 -c m", rootdir, positY, rootdir);
1087
#if WORKSTAT == 'I'
1088
      retTemp = system(cmd);
1089
#else
1090
      printf("Cmd: %s\n",cmd);
1091
#endif
1092
 
1093
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 3 -c m", rootdir, positZ, rootdir);
1094
#if WORKSTAT == 'I'
1095
      retTemp = system(cmd);
1096
#else
1097
      printf("Cmd: %s\n",cmd);
1098
#endif
1099
   }
1100
   // Get current table position
1101
   else if(opt == 1)
1102
   {
1103
      fflush(stdout);
1104
 
1105
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -p > %s/settings/curpos.txt", rootdir, rootdir);  // X-axis
1106
      fflush(stdout);
1107
#if WORKSTAT == 'I'
1108
      retTemp = system(cmd);
1109
#else
1110
      printf("Cmd: %s\n",cmd);
1111
#endif
1112
 
1113
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -p >> %s/settings/curpos.txt", rootdir, rootdir); // Y-axis
1114
      fflush(stdout);
1115
#if WORKSTAT == 'I'
1116
      retTemp = system(cmd);
1117
#else
1118
      printf("Cmd: %s\n",cmd);
1119
#endif
1120
 
1121
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -p >> %s/settings/curpos.txt", rootdir, rootdir); // Z-axis
1122
      fflush(stdout);
1123
#if WORKSTAT == 'I'
1124
      retTemp = system(cmd);
1125
#else
1126
      printf("Cmd: %s\n",cmd);
1127
#endif
1128
 
1129
#if WORKSTAT == 'I'
1130
      FILE* fpos;
1131
      int itemp;
1132
      sprintf(cmd, "%s/settings/curpos.txt", rootdir);
1133
      fpos = fopen(cmd, "r");
1134
 
1135
      if(fpos != NULL)
1136
      {
1137
         if(posUnits->widgetCB->GetSelected() == 0)
1138
         {
1139
            retTemp = fscanf(fpos, "%d\n", &itemp);
1140
            xPos->widgetNE[0]->SetNumber(itemp);
1141
            retTemp = fscanf(fpos, "%d\n", &itemp);
1142
            yPos->widgetNE[0]->SetNumber(itemp);
1143
            retTemp = fscanf(fpos, "%d\n", &itemp);
1144
            zPos->widgetNE[0]->SetNumber(itemp);
1145
         }
1146
         else if(posUnits->widgetCB->GetSelected() == 1)
1147
         {
1148
            retTemp = fscanf(fpos, "%d\n", &itemp);
1149
            xPos->widgetNE[0]->SetNumber((double)itemp*lenconversion);
1150
            retTemp = fscanf(fpos, "%d\n", &itemp);
1151
            yPos->widgetNE[0]->SetNumber((double)itemp*lenconversion);
1152
            retTemp = fscanf(fpos, "%d\n", &itemp);
1153
            zPos->widgetNE[0]->SetNumber((double)itemp*lenconversion);
1154
         }
1155
      }
1156
 
1157
      fclose(fpos);
1158
#endif
1159
   }
1160
   // Home the table position
1161
   else if(opt == 2)
1162
   {
1163
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -h", rootdir);    // X-axis
1164
#if WORKSTAT == 'I'
1165
      retTemp = system(cmd);
1166
#else
1167
      printf("Cmd: %s\n",cmd);
1168
#endif
1169
 
1170
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -h", rootdir);    // Y-axis
1171
#if WORKSTAT == 'I'
1172
      retTemp = system(cmd);
1173
#else
1174
      printf("Cmd: %s\n",cmd);
1175
#endif
1176
 
1177
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -h", rootdir);    // Z-axis
1178
#if WORKSTAT == 'I'
1179
      retTemp = system(cmd);
1180
#else
1181
      printf("Cmd: %s\n",cmd);
1182
#endif
1183
      PositionSet(1);
1184
   }
1185
   // Reset the table position
1186
   else if(opt == 3)
1187
   {
1188
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -r && sudo %s/src/MIKRO/mikro_ctrl -n 1 -i 3 && sudo %s/src/MIKRO/mikro_ctrl -n 1 -h", rootdir, rootdir, rootdir);        // X-axis
1189
#if WORKSTAT == 'I'
1190
      printf("Positioning table reset, initialization and homing in progress. Please wait...\n");
1191
      retTemp = system(cmd);
1192
#else
1193
      printf("Cmd: %s\n",cmd);
1194
#endif
1195
 
1196
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -r && sudo %s/src/MIKRO/mikro_ctrl -n 2 -i 3 && sudo %s/src/MIKRO/mikro_ctrl -n 2 -h", rootdir, rootdir, rootdir);        // Y-axis
1197
#if WORKSTAT == 'I'
1198
      retTemp = system(cmd);
1199
#else
1200
      printf("Cmd: %s\n",cmd);
1201
#endif
1202
 
1203
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -r && sudo %s/src/MIKRO/mikro_ctrl -n 3 -i 3 && sudo %s/src/MIKRO/mikro_ctrl -n 3 -h", rootdir, rootdir, rootdir);        // Z-axis
1204
#if WORKSTAT == 'I'
1205
      retTemp = system(cmd);
1206
      printf("Positioning table reset, initialization and homing complete.\n");
1207
#else
1208
      printf("Cmd: %s\n",cmd);
1209
#endif
1210
      PositionSet(1);
1211
   }
172 f9daq 1212
   // Abort any motion
1213
   else if(opt == 4)
1214
   {
1215
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -c ab", rootdir); // X-axis
1216
#if WORKSTAT == 'I'
1217
      printf("Emergency stop of the current movement of all linear tables.\n");
1218
      retTemp = system(cmd);
1219
#else
1220
      printf("Cmd: %s\n",cmd);
1221
#endif
1222
 
1223
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -c ab", rootdir); // Y-axis
1224
#if WORKSTAT == 'I'
1225
      retTemp = system(cmd);
1226
#else
1227
      printf("Cmd: %s\n",cmd);
1228
#endif
1229
 
1230
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -c ab", rootdir); // Z-axis
1231
#if WORKSTAT == 'I'
1232
      retTemp = system(cmd);
1233
#else
1234
      printf("Cmd: %s\n",cmd);
1235
#endif
1236
      PositionSet(1);
1237
   }
146 f9daq 1238
}
1239
 
1240
// Set, get, home or reset the rotation platform
1241
void TGAppMainFrame::RotationSet(int opt)
1242
{
1243
   char cmd[1024];
1244
 
1245
   // Set the selected rotation
1246
   if(opt == 0)
1247
   {
1248
      int positAlpha;
1249
 
1250
      if(rotUnits->widgetCB->GetSelected() == 0)
1251
         positAlpha = rotPos->widgetNE[0]->GetNumber();
1252
      else if(rotUnits->widgetCB->GetSelected() == 1)
1253
         positAlpha = rotPos->widgetNE[0]->GetNumber()/rotconversion;
1254
 
1255
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, positAlpha, rootdir);
1256
#if WORKSTAT == 'I'
1257
      retTemp = system(cmd);
1258
#else
1259
      printf("Cmd: %s\n",cmd);
1260
#endif
1261
   }
1262
   // Get current rotation
1263
   else if(opt == 1)
1264
   {
1265
      fflush(stdout);
1266
 
1267
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -p > %s/settings/currot.txt", rootdir, rootdir);
1268
      fflush(stdout);
1269
#if WORKSTAT == 'I'
1270
      retTemp = system(cmd);
1271
#else
1272
      printf("Cmd: %s\n",cmd);
1273
#endif
1274
 
1275
#if WORKSTAT == 'I'
1276
      FILE* frot;
1277
      int itemp;
1278
      sprintf(cmd, "%s/settings/currot.txt", rootdir);
1279
      frot = fopen(cmd, "r");
1280
 
1281
      if(frot != NULL)
1282
      {
1283
         retTemp = fscanf(frot, "%d\n", &itemp);
1284
         if(rotUnits->widgetCB->GetSelected() == 0)
1285
            rotPos->widgetNE[0]->SetNumber(itemp);
1286
         else if(rotUnits->widgetCB->GetSelected() == 1)
1287
            rotPos->widgetNE[0]->SetNumber((double)itemp*rotconversion);
1288
      }
1289
 
1290
      fclose(frot);
1291
#endif
1292
   }
1293
   // Home the rotation
1294
   else if(opt == 2)
1295
   {
1296
      // TODO: For now only set back to 0, not home!
1297
//      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -h", rootdir);
1298
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v 0 -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, rootdir);
1299
#if WORKSTAT == 'I'
1300
      retTemp = system(cmd);
1301
#else
1302
      printf("Cmd: %s\n",cmd);
1303
#endif
1304
      RotationSet(1);
1305
   }
1306
   // Reset the rotation
1307
   else if(opt == 3)
1308
   {
1309
      // TODO: For now only set back to 0, not home!
1310
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -r && sudo %s/src/MIKRO/mikro_ctrl -n 4 -i 2 && sudo %s/src/MIKRO/mikro_ctrl -n 4 -h", rootdir, rootdir, rootdir);
1311
#if WORKSTAT == 'I'
1312
      printf("Rotation platform reset, initalization and homing in progress. Please wait...\n");
1313
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v 0 -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, rootdir);
1314
      retTemp = system(cmd);
1315
      sleep(15);        // wait for the motor to change position from wherever to 0
1316
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -r && sudo %s/src/MIKRO/mikro_ctrl -n 4 -i 2", rootdir, rootdir);
1317
      retTemp = system(cmd);
1318
      printf("Rotation platform reset, initalization and homing complete.\n");
1319
#else
1320
      printf("Cmd: %s\n",cmd);
1321
#endif
1322
      RotationSet(1);
1323
   }
172 f9daq 1324
   // Abort any motion
1325
   else if(opt == 4)
1326
   {
1327
      sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -c ab", rootdir);
1328
#if WORKSTAT == 'I'
1329
      printf("Emergency stop of the current movement of the rotation platform.\n");
1330
      retTemp = system(cmd);
1331
#else
1332
      printf("Cmd: %s\n",cmd);
1333
#endif
1334
      RotationSet(1);
1335
   }
146 f9daq 1336
}
1337
 
1338
// File browser for selecting the save file
1339
void TGAppMainFrame::SaveFile()
1340
{
172 f9daq 1341
//   char *cTemp;
1342
 
146 f9daq 1343
   TGFileInfo file_info;
1344
   const char *filetypes[] = {"Histograms",histextall,0,0};
1345
   file_info.fFileTypes = filetypes;
172 f9daq 1346
//   cTemp = new char[1024];
1347
//   sprintf(cTemp, "%s/results", rootdir);
1348
//   file_info.fIniDir = StrDup(cTemp);
1349
   file_info.fIniDir = StrDup(currentMeasDir);
146 f9daq 1350
   new TGFileDialog(gClient->GetDefaultRoot(), fMain, kFDSave, &file_info);
172 f9daq 1351
//   delete[] cTemp;
146 f9daq 1352
 
1353
   if(file_info.fFilename != NULL)
172 f9daq 1354
   {
146 f9daq 1355
      fileName->widgetTE->SetText(file_info.fFilename);
172 f9daq 1356
      remove_from_last(file_info.fFilename, '/', currentMeasDir);
1357
   }
146 f9daq 1358
}
1359
 
1360
// Start the acquisition
1361
void TGAppMainFrame::StartAcq()
1362
{
1363
   // Variable that will initialize camac only once (for scans)
1364
   int scanon = 0;
1365
 
1366
   // Determine the type of measurement to perform
1367
   int vscan = 0, pscan = 0, zscan = 0, ascan = 0;
1368
   if(scansOn->widgetChBox[0]->IsDown()) vscan = 1;
1369
   if(scansOn->widgetChBox[1]->IsDown()) pscan = 1;
1370
   if(scansOn->widgetChBox[2]->IsDown()) zscan = 1;
1371
   if(scansOn->widgetChBox[3]->IsDown()) ascan = 1;
1372
 
1373
   char cmd[256];
1374
   int i, j, k;
1375
   float progVal;
1376
   FILE *pfin;
1377
 
1378
   // Variables for voltage scan
1379
   float currentVoltage, minVoltage, maxVoltage, stepVoltage;
1380
   int repetition;
1381
 
1382
   // Variables for surface scan and Z axis scan
1383
   float minXpos, maxXpos, stepXpos;
1384
   float minYpos, maxYpos, stepYpos;
1385
   float minZpos, maxZpos, stepZpos;
1386
   int repetX, repetY, repetZ;
1387
 
1388
   // Variables for angle scan
1389
   float currentAlpha, minAlpha, maxAlpha, stepAlpha;
1390
   int repetAlpha;
1391
 
1392
   // Only voltage scan
1393
   if( (vscan == 1) && (pscan == 0) && (ascan == 0) )
1394
   { // TODO - include possibility to make voltage and angle scan at same time
1395
      // If already started, stop the acquisition
1396
      if(acqStarted)
1397
      {
1398
         printf("Stopping current voltage scan...\n");
1399
         gROOT->SetInterrupt();
1400
         measProgress->widgetTB[0]->SetText("Start acquisition");
1401
         acqStarted = false;
1402
 
1403
         // Write information to the finish_sig.txt value
1404
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1405
         pfin = fopen(cmd,"w");
1406
         fprintf(pfin, "%s: Voltage scan stopped.", timeStamp->widgetTE->GetText());
1407
         fclose(pfin);
1408
      }
1409
      // If stopped, start the acquisition
1410
      else if(!acqStarted)
1411
      {
1412
         printf("Running a voltage scan...\n");
1413
 
1414
         // Check the steps
1415
         minVoltage = vOutStart->widgetNE[0]->GetNumber();
1416
         maxVoltage = vOutStop->widgetNE[0]->GetNumber();
1417
         stepVoltage = vOutStep->widgetNE[0]->GetNumber();
1418
 
1419
         if(stepVoltage == 0.)
1420
            repetition = 1;
1421
         else
1422
         {
1423
            // Example: min = 40, max = 70, step = 5 (in increasing steps)
1424
            if( (maxVoltage > minVoltage) && (stepVoltage > 0) )
1425
               repetition = ((maxVoltage - minVoltage)/stepVoltage)+1;
1426
            // Example: min = 70, max = 40, step = -5 (in decreasing steps)
1427
            else if( (maxVoltage < minVoltage) && (stepVoltage < 0) )
1428
               repetition = ((minVoltage - maxVoltage)/stepVoltage)-1;
1429
            // Example: min = 70, max = 70 (no scan)
1430
            else if( maxVoltage == minVoltage )
1431
               repetition = 1;
1432
            // If step is not correctly set, stop the acqusition
1433
            else
1434
            {
1435
               // TODO
1436
               printf("Stopping current voltage scan...\n");
1437
               gROOT->SetInterrupt();
1438
               measProgress->widgetTB[0]->SetText("Start acquisition");
1439
               acqStarted = false;
1440
               repetition = 0;
1441
            }
1442
         }
1443
 
1444
         if(DBGSIG) printf("StartAcq(): Voltage repetition (%lf,%lf,%lf) = %d\n", minVoltage, maxVoltage, stepVoltage, repetition);
1445
 
1446
         i = 0;
1447
 
1448
         // TODO - Setting button text and acqStarted do not work!
1449
         measProgress->widgetTB[0]->SetText("Stop acquisition");
1450
         acqStarted = true;
1451
         progVal = 0.00;
1452
         measProgress->widgetPB->SetPosition(progVal);
1453
         gVirtualX->Update(1);
1454
 
1455
         clkt0 = clock();
1456
         timet0 = time(NULL);
1457
 
1458
         while(1)
1459
         {
1460
            if( (repetition > 0) && (i == repetition) ) break;
1461
            else if( (repetition < 0) && (i == -repetition) ) break;
1462
            else if( repetition == 0 ) break;
1463
 
1464
            progVal = (float)(100.00/abs(repetition))*i;
1465
            measProgress->widgetPB->SetPosition(progVal);
1466
 
1467
            TimeEstimate(clkt0, timet0, progVal, cmd, singlewait*abs(repetition));
1468
            measProgress->widgetTE->SetText(cmd);
1469
 
1470
            gVirtualX->Update(1);
1471
 
1472
            fflush(stdout);
1473
            currentVoltage = minVoltage + stepVoltage*i;
1474
            sprintf(cmd, "%s/src/mpod/mpod_voltage.sh -o %d -v %f -s 1", rootdir, GetChannel(), currentVoltage);
1475
#if WORKSTAT == 'I'
1476
            retTemp = system(cmd);
1477
#else
1478
            printf("Cmd: %s\n",cmd);
1479
#endif
1480
            fflush(stdout);
1481
 
1482
            printf("Waiting for voltage change...\n");
1483
            sleep(singlewait);
1484
            vOut->widgetNE[0]->SetNumber(currentVoltage);
1485
            gVirtualX->Update(1);
1486
            printf("Continuing...\n");
1487
 
1488
            // Here comes function to start histogramming <<<<<<<<<<<<<<<<<<<<<<<<
1489
            RunMeas((void*)0, i, scanon); // TODO
1490
            fflush(stdout);
1491
 
1492
            i++;
1493
         }
1494
 
1495
         // Set output back to off
1496
         fflush(stdout);
1497
         printf("Measurement finished, returning to starting voltage...\n");
1498
         sprintf(cmd, "%s/src/mpod/mpod_voltage.sh -o %d -v %f -s 1", rootdir, GetChannel(), minVoltage);
1499
         vOut->widgetNE[0]->SetNumber(minVoltage);
1500
#if WORKSTAT == 'I'
1501
         retTemp = system(cmd);
1502
#else
1503
         printf("Cmd: %s\n",cmd);
1504
#endif
1505
         fflush(stdout);
1506
 
1507
         progVal = 100.00;
1508
         measProgress->widgetPB->SetPosition(progVal);
1509
         printf("\n");
1510
 
1511
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1512
         pfin = fopen(cmd,"w");
1513
         fprintf(pfin, "%s: Voltage scan finished.", timeStamp->widgetTE->GetText());
1514
         fclose(pfin);
1515
 
1516
         measProgress->widgetTB[0]->SetText("Start acquisition");
1517
         acqStarted = false;
1518
      }
1519
   }
1520
   // Surface scan
1521
   else if( (pscan == 1) && (vscan == 0) && (ascan == 0) )
1522
   {
1523
      // If already started, stop the acquisition
1524
      if(acqStarted)
1525
      {
1526
         printf("Stopping current surface scan...\n");
1527
         gROOT->SetInterrupt();
1528
         measProgress->widgetTB[0]->SetText("Start acquisition");
1529
         acqStarted = false;
1530
 
1531
         // Write information to the finish_sig.txt value
1532
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1533
         pfin = fopen(cmd,"w");
1534
         fprintf(pfin, "%s: Surface scan stopped.", timeStamp->widgetTE->GetText());
1535
         fclose(pfin);
1536
      }
1537
      // If stopped, start the acquisition
1538
      else if(!acqStarted)
1539
      {
1540
         printf("Running a surface scan...\n");
1541
 
1542
         minXpos = xPosMin->widgetNE[0]->GetNumber();
1543
         maxXpos = xPosMax->widgetNE[0]->GetNumber();
1544
         stepXpos = xPosStep->widgetNE[0]->GetNumber();
1545
         minYpos = yPosMin->widgetNE[0]->GetNumber();
1546
         maxYpos = yPosMax->widgetNE[0]->GetNumber();
1547
         stepYpos = yPosStep->widgetNE[0]->GetNumber();
1548
         minZpos = zPosMin->widgetNE[0]->GetNumber();
1549
         maxZpos = zPosMax->widgetNE[0]->GetNumber();
1550
         stepZpos = zPosStep->widgetNE[0]->GetNumber();
1551
 
1552
         // Setting repetition for Z axis scan
1553
         if(zscan == 1)
1554
         {
1555
            if(stepZpos == 0.)
1556
               repetZ = 1;
1557
            else
1558
            {
1559
               // Example: min = 40, max = 70, step = 5 (in increasing steps)
1560
               if( (maxZpos > minZpos) && (stepZpos > 0) )
1561
                  repetZ = ((maxZpos - minZpos)/stepZpos)+1;
1562
               // Example: min = 70, max = 40, step = -5 (in decreasing steps)
1563
               else if( (maxZpos < minZpos) && (stepZpos < 0) )
1564
                  repetZ = ((minZpos - maxZpos)/stepZpos)-1;
1565
               // Example: min = 70, max = 70 (no scan)
1566
               else if( maxZpos == minZpos )
1567
                  repetZ = 1;
1568
               // If step is not correctly set, stop the acqusition
1569
               else
1570
               {
1571
                  // TODO
1572
                  printf("Stopping current surface scan (Z step error)...\n");
1573
                  gROOT->SetInterrupt();
1574
                  measProgress->widgetTB[0]->SetText("Start acquisition");
1575
                  acqStarted = false;
1576
                  repetZ = 0;
1577
               }
1578
            }
1579
         }
1580
         else
1581
         {
1582
            minZpos = zPos->widgetNE[0]->GetNumber();
1583
            repetZ = 1;
1584
         }
1585
 
1586
         // Setting repetition for X axis
1587
         if(stepXpos == 0.)
1588
            repetX = 1;
1589
         else
1590
         {
1591
            // Example: min = 40, max = 70, step = 5 (in increasing steps)
1592
            if( (maxXpos > minXpos) && (stepXpos > 0) )
1593
               repetX = ((maxXpos - minXpos)/stepXpos)+1;
1594
            // Example: min = 70, max = 40, step = -5 (in decreasing steps)
1595
            else if( (maxXpos < minXpos) && (stepXpos < 0) )
1596
               repetX = ((minXpos - maxXpos)/stepXpos)-1;
1597
            // Example: min = 70, max = 70 (no scan)
1598
            else if( maxXpos == minXpos )
1599
               repetX = 1;
1600
            // If step is not correctly set, stop the acqusition
1601
            else
1602
            {
1603
               // TODO
1604
               printf("Stopping current surface scan (X step error)...\n");
1605
               gROOT->SetInterrupt();
1606
               measProgress->widgetTB[0]->SetText("Start acquisition");
1607
               acqStarted = false;
1608
               repetX = 0;
1609
            }
1610
         }
1611
         // Setting repetition for Y axis
1612
         if(stepYpos == 0.)
1613
            repetY = 1;
1614
         else
1615
         {
1616
            // Example: min = 40, max = 70, step = 5 (in increasing steps)
1617
            if( (maxYpos > minYpos) && (stepYpos > 0) )
1618
               repetY = ((maxYpos - minYpos)/stepYpos)+1;
1619
            // Example: min = 70, max = 40, step = -5 (in decreasing steps)
1620
            else if( (maxYpos < minYpos) && (stepYpos < 0) )
1621
               repetY = ((minYpos - maxYpos)/stepYpos)-1;
1622
            // Example: min = 70, max = 70 (no scan)
1623
            else if( maxYpos == minYpos )
1624
               repetY = 1;
1625
            // If step is not correctly set, stop the acqusition
1626
            else
1627
            {
1628
               // TODO
1629
               printf("Stopping current surface scan (Y step error)...\n");
1630
               gROOT->SetInterrupt();
1631
               measProgress->widgetTB[0]->SetText("Start acquisition");
1632
               acqStarted = false;
1633
               repetY = 0;
1634
            }
1635
         }
1636
 
1637
         if(DBGSIG) printf("StartAcq(): X axis repetition (%lf,%lf,%lf) = %d\n", minXpos, maxXpos, stepXpos, repetX);
1638
         if(DBGSIG) printf("StartAcq(): Y axis repetition (%lf,%lf,%lf) = %d\n", minYpos, maxYpos, stepYpos, repetY);
1639
         if(DBGSIG) printf("StartAcq(): Z axis repetition (%lf,%lf,%lf) = %d\n", minZpos, maxZpos, stepZpos, repetZ);
1640
 
1641
         i = 0; j = 0; k = 0;
1642
 
1643
         // TODO - Setting button text and acqStarted do not work!
1644
         measProgress->widgetTB[0]->SetText("Stop acquisition");
1645
         acqStarted = true;
1646
         progVal = 0.00;
1647
         measProgress->widgetPB->SetPosition(progVal);
1648
         gVirtualX->Update(1);
1649
 
172 f9daq 1650
         int nrAverMeas = -1;
1651
 
146 f9daq 1652
         clkt0 = clock();
1653
         timet0 = time(NULL);
1654
 
1655
         // Scan over Z axis
1656
         while(1)
1657
         {
1658
            if( (repetZ > 0) && (k == repetZ) ) break;
1659
            else if( (repetZ < 0) && (k == -repetZ) ) break;
1660
            else if( repetZ == 0 ) break;
1661
 
1662
            fflush(stdout);
1663
            // Z-axis change
1664
            if( posUnits->widgetCB->GetSelected() == 0)
1665
               sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 3 -c m", rootdir, (int)(minZpos + stepZpos*k), rootdir);
1666
            else if( posUnits->widgetCB->GetSelected() == 1)
1667
               sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 3 -c m", rootdir, (int)((minZpos + stepZpos*k)/lenconversion), rootdir);
1668
#if WORKSTAT == 'I'
1669
            retTemp = system(cmd);
1670
#else
1671
            printf("Cmd: %s\n",cmd);
1672
#endif
1673
            fflush(stdout);
1674
 
1675
            printf("Next Z position...\n");
1676
            sleep(2*doublewait);
1677
            zPos->widgetNE[0]->SetNumber(minZpos + stepZpos*k);
1678
            fflush(stdout);
1679
 
1680
            // Scan over Y axis
1681
            while(1)
1682
            {
1683
               if( (repetY > 0) && (j == repetY) ) break;
1684
               else if( (repetY < 0) && (j == -repetY) ) break;
1685
               else if( repetY == 0 ) break;
1686
 
1687
               fflush(stdout);
1688
               // Y-axis change
1689
               if( posUnits->widgetCB->GetSelected() == 0)
1690
                  sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 2 -c m", rootdir, (int)(minYpos + stepYpos*j), rootdir);
1691
               else if( posUnits->widgetCB->GetSelected() == 1)
1692
                  sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 2 -c m", rootdir, (int)((minYpos + stepYpos*j)/lenconversion), rootdir);
1693
#if WORKSTAT == 'I'
1694
               retTemp = system(cmd);
1695
#else
1696
               printf("Cmd: %s\n",cmd);
1697
#endif
1698
               fflush(stdout);
1699
 
1700
               printf("Next Y position...\n");
1701
               sleep(2*doublewait);
1702
               yPos->widgetNE[0]->SetNumber(minYpos + stepYpos*j);
1703
               fflush(stdout);
1704
 
1705
               // Scan over X axis
1706
               while(1)
1707
               {
1708
                  if( (repetX > 0) && (i == repetX) ) break;
1709
                  else if( (repetX < 0) && (i == -repetX) ) break;
1710
                  else if( repetX == 0 ) break;
1711
 
1712
                  progVal = (float)(100.00/(abs(repetX)*abs(repetY)*abs(repetZ)))*(k*abs(repetX)*abs(repetY) + j*abs(repetX) + i);
1713
                  measProgress->widgetPB->SetPosition(progVal);
1714
 
172 f9daq 1715
//                TimeEstimate(clkt0, timet0, progVal, cmd, doublewait*((abs(repetX)+2)*abs(repetY)+2)*abs(repetZ));
1716
//                measProgress->widgetTE->SetText(cmd);
146 f9daq 1717
 
1718
                  gVirtualX->Update(1);
1719
 
1720
                  // X-axis change
1721
                  if( posUnits->widgetCB->GetSelected() == 0)
1722
                     sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 1 -c m", rootdir, (int)(minXpos + stepXpos*i), rootdir);
1723
                  else if( posUnits->widgetCB->GetSelected() == 1)
1724
                     sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 1 -c m", rootdir, (int)((minXpos + stepXpos*i)/lenconversion), rootdir);
1725
#if WORKSTAT == 'I'
1726
                  retTemp = system(cmd);
1727
#else
1728
                  printf("Cmd: %s\n",cmd);
1729
#endif
1730
                  fflush(stdout);
1731
 
1732
                  printf("Next X position...\n");
1733
                  fflush(stdout);
172 f9daq 1734
 
1735
TimeEstimateNew(nrAverMeas, clkt0, timet0, repetX, repetY, repetZ, doublewait, 2*doublewait, 2*doublewait, cmd);
1736
measProgress->widgetTE->SetText(cmd);
146 f9daq 1737
 
1738
                  printf("Waiting for position change...\n");
1739
                  sleep(doublewait);
1740
                  xPos->widgetNE[0]->SetNumber(minXpos + stepXpos*i);
1741
                  printf("Continuing...\n");
1742
 
1743
               // Here comes function to start histogramming <<<<<<<<<<<<<<<<<<<<<<<<
172 f9daq 1744
clkt0 = clock();
146 f9daq 1745
                  RunMeas((void*)0, (j*repetX + i), scanon);
172 f9daq 1746
nrAverMeas++;
146 f9daq 1747
                  fflush(stdout);
1748
 
1749
                  i++;
1750
               }
1751
 
1752
               i = 0;
1753
               printf("\n");
1754
 
1755
               j++;
1756
            }
1757
 
1758
            j = 0;
1759
 
1760
            k++;
1761
         }
1762
printf("Time = %d\n", (int)time(NULL));
1763
 
1764
         fflush(stdout);
1765
         printf("Measurement finished, returning to starting position...\n");
1766
         // X-axis return
1767
         if( posUnits->widgetCB->GetSelected() == 0)
1768
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 1 -c m", rootdir, (int)minXpos, rootdir);
1769
         else if( posUnits->widgetCB->GetSelected() == 1)
1770
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 1 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 1 -c m", rootdir, (int)(minXpos/lenconversion), rootdir);
1771
#if WORKSTAT == 'I'
1772
         retTemp = system(cmd);
1773
#else
1774
         printf("Cmd: %s\n",cmd);
1775
#endif
1776
         fflush(stdout);
1777
 
1778
         // Y-axis return
1779
         if( posUnits->widgetCB->GetSelected() == 0)
1780
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 2 -c m", rootdir, (int)minYpos, rootdir);
1781
         else if( posUnits->widgetCB->GetSelected() == 1)
1782
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 2 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 2 -c m", rootdir, (int)(minYpos/lenconversion), rootdir);
1783
#if WORKSTAT == 'I'
1784
         retTemp = system(cmd);
1785
#else
1786
         printf("Cmd: %s\n",cmd);
1787
#endif
1788
 
1789
         // Z-axis return
1790
         if( posUnits->widgetCB->GetSelected() == 0)
1791
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 3 -c m", rootdir, (int)minZpos, rootdir);
1792
         else if( posUnits->widgetCB->GetSelected() == 1)
1793
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 3 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 3 -c m", rootdir, (int)(minZpos/lenconversion), rootdir);
1794
#if WORKSTAT == 'I'
1795
         retTemp = system(cmd);
1796
#else
1797
         printf("Cmd: %s\n",cmd);
1798
#endif
1799
         xPos->widgetNE[0]->SetNumber(minXpos);
1800
         yPos->widgetNE[0]->SetNumber(minYpos);
1801
         zPos->widgetNE[0]->SetNumber(minZpos);
1802
 
1803
         progVal = 100.00;
1804
         measProgress->widgetPB->SetPosition(progVal);
1805
         printf("\n");
1806
 
1807
         // Write information to the finish_sig.txt value
1808
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1809
         pfin = fopen(cmd,"w");
1810
         fprintf(pfin, "%s: Surface scan finished.", timeStamp->widgetTE->GetText());
1811
         fclose(pfin);
1812
 
1813
         measProgress->widgetTB[0]->SetText("Start acquisition");
1814
         acqStarted = false;
1815
      }
1816
   }
1817
   // Only angle scan
1818
   if( (ascan == 1) && (pscan == 0) && (vscan == 0) )
1819
   {
1820
      // If already started, stop the acquisition
1821
      if(acqStarted)
1822
      {
1823
         printf("Stopping current angle scan...\n");
1824
         gROOT->SetInterrupt();
1825
         measProgress->widgetTB[0]->SetText("Start acquisition");
1826
         acqStarted = false;
1827
 
1828
         // Write information to the finish_sig.txt value
1829
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1830
         pfin = fopen(cmd,"w");
1831
         fprintf(pfin, "%s: Angle scan stopped.", timeStamp->widgetTE->GetText());
1832
         fclose(pfin);
1833
      }
1834
      // If stopped, start the acquisition
1835
      else if(!acqStarted)
1836
      {
1837
         printf("Running an incidence angle scan...\n");
1838
 
1839
         // Check the steps
1840
         minAlpha = rotPosMin->widgetNE[0]->GetNumber();
1841
         maxAlpha = rotPosMax->widgetNE[0]->GetNumber();
1842
         stepAlpha = rotPosStep->widgetNE[0]->GetNumber();
1843
 
1844
         if(stepAlpha == 0.)
1845
            repetAlpha = 1;
1846
         else
1847
         {
1848
            // Example: min = 40, max = 70, step = 5 (in increasing steps)
1849
            if( (maxAlpha > minAlpha) && (stepAlpha > 0) )
1850
               repetAlpha = ((maxAlpha - minAlpha)/stepAlpha)+1;
1851
            // Example: min = 70, max = 40, step = -5 (in decreasing steps)
1852
            else if( (maxAlpha < minAlpha) && (stepAlpha < 0) )
1853
               repetAlpha = ((minAlpha - maxAlpha)/stepAlpha)-1;
1854
            // Example: min = 70, max = 70 (no scan)
1855
            else if( maxAlpha == minAlpha )
1856
               repetAlpha = 1;
1857
            // If step is not correctly set, stop the acqusition
1858
            else
1859
            {
1860
               // TODO
1861
               printf("Stopping current incidence angle scan...\n");
1862
               gROOT->SetInterrupt();
1863
               measProgress->widgetTB[0]->SetText("Start acquisition");
1864
               acqStarted = false;
1865
               repetAlpha = 0;
1866
            }
1867
         }
1868
 
1869
         if(DBGSIG) printf("StartAcq(): Angle repetition (%lf,%lf,%lf) = %d\n", minAlpha, maxAlpha, stepAlpha, repetAlpha);
1870
 
1871
         int angleWait = TMath::Ceil(abs(rotPos->widgetNE[0]->GetNumber()-minAlpha)*15/(rotPos->widgetNE[0]->GetNumMax()));
1872
         if(rotUnits->widgetCB->GetSelected() == 1)
1873
         {
1874
            minAlpha = minAlpha/rotconversion;
1875
            maxAlpha = maxAlpha/rotconversion;
1876
            stepAlpha = stepAlpha/rotconversion;
1877
         }
1878
 
1879
         i = 0;
1880
 
1881
         // TODO - Setting button text and acqStarted do not work!
1882
         measProgress->widgetTB[0]->SetText("Stop acquisition");
1883
         acqStarted = true;
1884
         progVal = 0.00;
1885
         measProgress->widgetPB->SetPosition(progVal);
1886
         gVirtualX->Update(1);
1887
 
1888
         clkt0 = clock();
1889
         timet0 = time(NULL);
1890
 
1891
         // Setting angle to initial position
1892
         sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, (int)minAlpha, rootdir);
1893
         if(rotUnits->widgetCB->GetSelected() == 0)
1894
            rotPos->widgetNE[0]->SetNumber(minAlpha);
1895
         else if(rotUnits->widgetCB->GetSelected() == 1)
1896
            rotPos->widgetNE[0]->SetNumber(minAlpha*rotconversion);
1897
#if WORKSTAT == 'I'
1898
            retTemp = system(cmd);
1899
#else
1900
            printf("Cmd: %s\n",cmd);
1901
#endif
1902
            fflush(stdout);
1903
 
1904
            printf("Waiting for %ds for rotation platform to move into starting position...\n", angleWait);
1905
            sleep(angleWait);
1906
 
1907
         while(1)
1908
         {
1909
            if( (repetAlpha > 0) && (i == repetAlpha) ) break;
1910
            else if( (repetAlpha < 0) && (i == -repetAlpha) ) break;
1911
            else if( repetAlpha == 0 ) break;
1912
 
1913
            progVal = (float)(100.00/abs(repetAlpha))*i;
1914
            measProgress->widgetPB->SetPosition(progVal);
1915
 
1916
            TimeEstimate(clkt0, timet0, progVal, cmd, singlewait*abs(repetAlpha));
1917
            measProgress->widgetTE->SetText(cmd);
1918
 
1919
            gVirtualX->Update(1);
1920
 
1921
            fflush(stdout);
1922
            currentAlpha = minAlpha + stepAlpha*i;
1923
            sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, (int)currentAlpha, rootdir);
1924
#if WORKSTAT == 'I'
1925
            retTemp = system(cmd);
1926
#else
1927
            printf("Cmd: %s\n",cmd);
1928
#endif
1929
            fflush(stdout);
1930
 
1931
            printf("Waiting for angle change...\n");
1932
            sleep(singlewait);
1933
            if(rotUnits->widgetCB->GetSelected() == 0)
1934
               rotPos->widgetNE[0]->SetNumber(currentAlpha);
1935
            else if(rotUnits->widgetCB->GetSelected() == 1)
1936
               rotPos->widgetNE[0]->SetNumber(currentAlpha*rotconversion);
1937
            gVirtualX->Update(1);
1938
            printf("Continuing...\n");
1939
 
1940
            // Here comes function to start histogramming <<<<<<<<<<<<<<<<<<<<<<<<
1941
            RunMeas((void*)0, i, scanon); // TODO
1942
            fflush(stdout);
1943
 
1944
            i++;
1945
         }
1946
 
1947
         // Set angle back to original position
1948
         fflush(stdout);
1949
         printf("Measurement finished, returning to starting incidence angle...\n");
1950
         sprintf(cmd, "sudo %s/src/MIKRO/mikro_ctrl -n 4 -v %d -s la && %s/src/MIKRO/mikro_ctrl -n 4 -c m", rootdir, (int)minAlpha, rootdir);
1951
         if(rotUnits->widgetCB->GetSelected() == 0)
1952
            rotPos->widgetNE[0]->SetNumber(minAlpha);
1953
         else if(rotUnits->widgetCB->GetSelected() == 1)
1954
            rotPos->widgetNE[0]->SetNumber(minAlpha*rotconversion);
1955
#if WORKSTAT == 'I'
1956
         retTemp = system(cmd);
1957
#else
1958
         printf("Cmd: %s\n",cmd);
1959
#endif
1960
         fflush(stdout);
1961
 
1962
         progVal = 100.00;
1963
         measProgress->widgetPB->SetPosition(progVal);
1964
         printf("\n");
1965
 
1966
         sprintf(cmd, "%s/dbg/finish_sig.txt", rootdir);
1967
         pfin = fopen(cmd,"w");
1968
         fprintf(pfin, "%s: Incidence angle scan finished.", timeStamp->widgetTE->GetText());
1969
         fclose(pfin);
1970
 
1971
         measProgress->widgetTB[0]->SetText("Start acquisition");
1972
         acqStarted = false;
1973
      }
1974
   }
1975
   // Normal single measurement
1976
   else if( (vscan == 0) && (pscan == 0) && (ascan == 0) )
1977
   {
1978
      // Set the start button to stop and enable stopping of measurement
1979
      if(acqStarted)
1980
      {
1981
         printf("Stopping current single scan...\n");
1982
         gROOT->SetInterrupt();
1983
         measProgress->widgetTB[0]->SetText("Start acquisition");
1984
         acqStarted = false;
1985
      }
1986
      else if(!acqStarted)
1987
      {
1988
         measProgress->widgetTB[0]->SetText("Stop acquisition");
1989
         acqStarted = true;
1990
 
1991
         printf("Running a single scan...\n");
1992
         clkt0 = clock();
1993
         timet0 = time(NULL);
1994
         RunMeas((void*)0, 0, scanon);
1995
         printf("Measurement finished...\n");
1996
         printf("\n");
1997
 
1998
         measProgress->widgetTB[0]->SetText("Start acquisition");
1999
         acqStarted = false;
2000
      }
2001
   }
2002
}
2003
 
2004
// Main measurement window connections ----------------------
2005
 
2006
// Histogram file selection pane connections ----------------
2007
 
2008
// File browser for opening histograms
2009
void TGAppMainFrame::SelectDirectory()
2010
{
2011
   int i = fileList->GetNumberOfEntries();
167 f9daq 2012
//   char *cTemp;
146 f9daq 2013
 
2014
   TGFileInfo file_info;
2015
   const char *filetypes[] = {"Histograms",histextall,0,0};
2016
   file_info.fFileTypes = filetypes;
167 f9daq 2017
//   cTemp = new char[1024];
2018
//   sprintf(cTemp, "%s/results", rootdir);
2019
//   file_info.fIniDir = StrDup(cTemp);
172 f9daq 2020
   file_info.fIniDir = StrDup(currentAnalDir);
146 f9daq 2021
   file_info.fMultipleSelection = kTRUE;
2022
   new TGFileDialog(gClient->GetDefaultRoot(), fMain, kFDOpen, &file_info);
167 f9daq 2023
//   delete[] cTemp;
146 f9daq 2024
 
2025
   TList *files = file_info.fFileNamesList;
2026
   if(files)
2027
   {
2028
      TSystemFile *file;
2029
      TString fname;
2030
      TIter next(files);
2031
      while(file=(TSystemFile*)next())
2032
      {
2033
         fname = file->GetName();
172 f9daq 2034
         remove_from_last((char*)fname.Data(), '/', currentAnalDir);
146 f9daq 2035
         fileList->AddEntry(fname.Data(), i);
2036
         i++;
2037
      }
2038
   }
2039
   fileList->Layout();
2040
}
2041
 
2042
// Toggle multiple selection in filelist or delete all entries
2043
void TGAppMainFrame::ListMultiSelect(int opt)
2044
{
2045
   // Enable multiselect
2046
   if(opt == 0)
2047
   {
2048
      fileList->SetMultipleSelections((multiSelect->widgetChBox[0]->IsOn()));
2049
 
2050
      if(multiSelect->widgetChBox[1]->IsDown())
2051
         multiSelect->widgetChBox[1]->SetState(kButtonUp);
2052
   }
2053
   else if(opt == 1)
2054
   {
2055
      if(multiSelect->widgetChBox[1]->IsDown())
2056
      {
2057
         multiSelect->widgetChBox[0]->SetState(kButtonDown);
2058
         fileList->SetMultipleSelections((multiSelect->widgetChBox[0]->IsOn()));
2059
         for(int i = 0; i < fileList->GetNumberOfEntries(); i++)
2060
            fileList->Select(i,kTRUE);
2061
      }
2062
      else if(!multiSelect->widgetChBox[1]->IsDown())
2063
      {
2064
         multiSelect->widgetChBox[0]->SetState(kButtonUp);
2065
         fileList->SetMultipleSelections((multiSelect->widgetChBox[0]->IsOn()));
2066
         for(int i = 0; i < fileList->GetNumberOfEntries(); i++)
2067
            fileList->Select(i,kFALSE);
2068
      }
2069
   }
2070
}
2071
 
2072
// Navigation buttons for the filelist (<<, >>) and double click
2073
void TGAppMainFrame::FileListNavigation(int opt)
2074
{
2075
   unsigned int nrfiles = fileList->GetNumberOfEntries();
2076
   int curSel;
2077
   TList *files;
2078
   if( nrfiles > 0 )
2079
   {
2080
      if(opt < -1)
2081
      {
2082
         // turn off multiple selection and select first file on list
2083
         if(multiSelect->widgetChBox[0]->IsOn())
2084
         {
2085
            fileList->SetMultipleSelections(kFALSE);
2086
            multiSelect->widgetChBox[0]->SetState(kButtonUp);
2087
            multiSelect->widgetChBox[1]->SetState(kButtonUp);
2088
 
2089
            fileList->Select(0,kTRUE);
2090
         }
2091
         else
2092
         {
2093
            // if nothing is selected, curSel will be -1
2094
            curSel = fileList->GetSelected();
2095
            // go to next file on list
2096
            if(opt == -3)
2097
            {
2098
               if( (curSel == (int)(nrfiles-1)) || (curSel == -1) )
2099
                  fileList->Select(0);
2100
               else
2101
                  fileList->Select(curSel+1);
2102
            }
2103
            // go to previous file on list
2104
            else if(opt == -2)
2105
            {
2106
               if( (curSel == 0) || (curSel == -1) )
2107
                  fileList->Select(nrfiles-1);
2108
               else
2109
                  fileList->Select(curSel-1);
2110
            }
2111
         }
2112
      }
2113
   }
2114
 
2115
   UpdateHistogram(0);
2116
}
2117
 
2118
// Open the header edit window when pressing on editHeader button
2119
void TGAppMainFrame::HeaderEdit()
2120
{
2121
   bool createTab = true;
2122
   int tabid = -1;
2123
 
2124
   for(int i = 0; i < fTab->GetNumberOfTabs(); i++)
2125
   {
2126
      if(strcmp("File header editor", fTab->GetTabTab(i)->GetString() ) == 0)
2127
      {
2128
         createTab = false;
2129
         tabid = i;
2130
      }
2131
 
2132
      if(DBGSIG > 1) printf("HeaderEdit(): Name of tab = %s\n", fTab->GetTabTab(i)->GetString() );
2133
   }
2134
 
2135
   unsigned int nrfiles = fileList->GetNumberOfEntries();
2136
   if(nrfiles > 0)
2137
      HeaderEditTab(fTab, createTab, &tabid);
2138
}
2139
 
2140
// Clear the histogram file selection list and dark run analysis selection
2141
void TGAppMainFrame::ClearHistogramList()
2142
{
2143
   fileList->RemoveAll();
2144
   darkRun->widgetTE->Clear();
2145
}
2146
 
2147
// Histogram file selection pane connections ----------------
2148
 
2149
// Histogram controls pane connections ----------------------
2150
 
2151
// Readjust the histogram range after changing ADC, TDC, Y range or logarithmic scale (opt: 0 = normal redraw, 1 = export, 2 = redraw when changing which channel to display) 
2152
void TGAppMainFrame::UpdateHistogram(int opt)
2153
{
2154
   if(DBGSIG > 1)
2155
   {
2156
      printf("UpdateHistogram(): Clearing the TList\n");
2157
      gDirectory->GetList()->Delete();
2158
      gObjectTable->Print();
2159
   }
2160
 
2161
   // Do not do normal histogram update if we have multiple files selected
2162
   if( (opt == 0) && (multiSelect->widgetChBox[0]->IsDown()) )
2163
   {
2164
      printf("UpdateHistogram(): To preview changes done to a histogram, please deselect the \"Multiple files select\" option.");
2165
      return;
2166
   }
2167
 
2168
   // Do not update histogram if we are on the same channel
2169
   if( ((opt == 2) && (selChannel != (int)selectCh->widgetNE[0]->GetNumber())) || (opt < 2) )
2170
   {
2171
      unsigned int nrfiles = fileList->GetNumberOfEntries();
2172
      TCanvas *gCanvas;
2173
      char exportname[512];
2174
      char cTemp[512];
2175
 
2176
      if(opt == 1)
2177
         gCanvas = analysisCanvas->GetCanvas();
2178
 
2179
      if(nrfiles > 0)
2180
      {
2181
         TList *files;
2182
         files = new TList();
2183
         fileList->GetSelectedEntries(files);
2184
 
2185
         if(files)
2186
         {
2187
            for(int i = 0; i < (int)nrfiles; i++)
2188
            {
2189
               if(files->At(i))
2190
               {
2191
                  if(DBGSIG)
2192
                     printf("UpdateHistogram(): Filename: %s\n", files->At(i)->GetTitle());
2193
                  if(opt == 1)
2194
                     remove_ext((char*)files->At(i)->GetTitle(), cTemp);
2195
                  if( fMenuHisttype->IsEntryChecked(M_ANALYSIS_HISTTYPE_1DADC) )
2196
                  {
2197
                     sprintf(exportname, "%s_adc%d.pdf", cTemp, (int)selectCh->widgetNE[0]->GetNumber());
2198
                     DisplayHistogram( (char*)(files->At(i)->GetTitle()), 0, opt);
2199
                  }
2200
                  else if( fMenuHisttype->IsEntryChecked(M_ANALYSIS_HISTTYPE_1DTDC) )
2201
                  {
2202
                     sprintf(exportname, "%s_tdc%d.pdf", cTemp, (int)selectCh->widgetNE[0]->GetNumber());
2203
                     DisplayHistogram( (char*)(files->At(i)->GetTitle()), 1, opt);
2204
                  }
2205
                  else if( fMenuHisttype->IsEntryChecked(M_ANALYSIS_HISTTYPE_2D) )
2206
                  {
2207
                     sprintf(exportname, "%s_adctdc%d.pdf", cTemp, (int)selectCh->widgetNE[0]->GetNumber());
2208
                     DisplayHistogram( (char*)(files->At(i)->GetTitle()), 2, opt);
2209
                  }
2210
 
2211
                  if(opt == 1)
2212
                  {
2213
                     gCanvas->SaveAs(exportname);
2214
                     delete inroot;
2215
                  }
2216
               }
2217
            }
2218
         }
2219
      }
2220
      selChannel = selectCh->widgetNE[0]->GetNumber();
2221
   }
2222
 
2223
   if(DBGSIG > 1)
2224
   {
2225
      printf("UpdateHistogram(): After drawing histograms (connections)\n");
2226
      gObjectTable->Print();
2227
   }
2228
}
2229
 
2230
// Options for histogram (logarithmic scale, clean plots)
2231
void TGAppMainFrame::HistogramOptions(int opt)
2232
{
2233
   // Logarithmic scale
2234
   if(opt == 0)
2235
      UpdateHistogram(0);
2236
   // Clean plots
2237
   else if(opt == 1)
2238
   {
2239
      cleanPlots = histOpt->widgetChBox[1]->IsDown();
2240
      UpdateHistogram(0);
2241
   }
2242
}
2243
 
2244
// Changing the histogram type to display
2245
void TGAppMainFrame::ChangeHisttype(int type)
2246
{
2247
   TGTextButton *pressedB = new TGTextButton();
2248
   int menuID = 0;
2249
   unsigned int nrfiles = fileList->GetNumberOfEntries();
2250
 
2251
   // ADC histogram
2252
   if(type == 0)
2253
   {
2254
      pressedB = plotType->widgetTB[0];
2255
      menuID = M_ANALYSIS_HISTTYPE_1DADC;
2256
 
2257
      plotType->widgetTB[1]->SetDown(kFALSE);
2258
      plotType->widgetTB[2]->SetDown(kFALSE);
2259
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_1DTDC);
2260
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_2D);
2261
   }
2262
   // TDC histogram
2263
   else if(type == 1)
2264
   {
2265
      pressedB = plotType->widgetTB[1];
2266
      menuID = M_ANALYSIS_HISTTYPE_1DTDC;
2267
 
2268
      plotType->widgetTB[0]->SetDown(kFALSE);
2269
      plotType->widgetTB[2]->SetDown(kFALSE);
2270
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_1DADC);
2271
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_2D);
2272
   }
2273
   // ADC vs. TDC histogram
2274
   else if(type == 2)
2275
   {
2276
      pressedB = plotType->widgetTB[2];
2277
      menuID = M_ANALYSIS_HISTTYPE_2D;
2278
 
2279
      plotType->widgetTB[0]->SetDown(kFALSE);
2280
      plotType->widgetTB[1]->SetDown(kFALSE);
2281
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_1DADC);
2282
      fMenuHisttype->UnCheckEntry(M_ANALYSIS_HISTTYPE_1DTDC);
2283
   }
2284
 
2285
   if( fMenuHisttype->IsEntryChecked(menuID) )
2286
   {
2287
      pressedB->SetDown(kFALSE);
2288
      fMenuHisttype->UnCheckEntry(menuID);
2289
   }
2290
   else if( !fMenuHisttype->IsEntryChecked(menuID) )
2291
   {
2292
      pressedB->SetDown(kTRUE);
2293
      fMenuHisttype->CheckEntry(menuID);
2294
   }
2295
 
2296
   UpdateHistogram(0);
2297
}
2298
 
2299
// Histogram controls pane connections ----------------------