source: trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.cc@ 19373

Last change on this file since 19373 was 19373, checked in by tbretz, 7 years ago
On the way to make it more maintanance friendly. Fixed a bug (some roi were roiTM)
File size: 77.0 KB
Line 
1/*
2 * QtGl.cpp
3 *
4 * Created on: Jul 19, 2011
5 * Author: lyard
6 *
7 ******
8 */
9#include <math.h>
10#include <fstream>
11
12#include <boost/date_time/local_time/local_time.hpp>
13
14#include "RawEventsViewer.h"
15
16#include <QFileDialog>
17#include <QMouseEvent>
18
19#include <qwt_symbol.h>
20#include <qwt_plot_grid.h>
21#pragma GCC diagnostic push
22#pragma GCC diagnostic ignored "-Woverloaded-virtual"
23#include <qwt_plot_zoomer.h>
24#pragma GCC diagnostic pop
25
26#include "src/Configuration.h"
27#include "externals/factfits.h"
28
29
30using namespace std;
31
32#undef ACTUAL_NUM_PIXELS
33#define ACTUAL_NUM_PIXELS 1440
34
35//bounding box for diplaying the impulse curve
36float bboxMin[2] = {-0.8,-0.9};
37float bboxMax[2] = {0.8,-0.3};
38/************************************************************
39 * CALC BLUR COLOR if in blur display mode, calculate the interpolated
40 * colour for a given vertex
41 ************************************************************/
42void RawDataViewer::calcBlurColor(int pixel, int vertex)
43{
44 GLfloat color[3];
45 int first, second;
46 first = vertex-1;
47 second = vertex;
48 if (first < 0)
49 first = 5;
50
51 first = neighbors[pixel][first];
52 second = neighbors[pixel][second];
53// cout << pixel << " " << vertex << " " << "first: " << first << " second: " << second << endl;
54 for (int i=0;i<3;i++)
55 color[i] = pixelsColor[pixel][i];
56 float divide = 1;
57 if (first != -1)
58 {
59 divide++;
60 for (int i=0;i<3;i++)
61 color[i] += pixelsColor[first][i];
62 }
63 if (second != -1)
64 {
65 divide++;
66 for (int i=0;i<3;i++)
67 color[i] += pixelsColor[second][i];
68 }
69 for (int i=0;i<3;i++)
70 color[i] /= divide;
71
72// cout << color[0] << " " << color[1] << " " << color[2] << endl;
73
74 glColor3fv(color);
75}
76void RawDataViewer::calcMidBlurColor(int pixel, int vertex)
77{
78 GLfloat color[3];
79 int first;
80 first = vertex-1;
81 if (first < 0)
82 first = 5;
83 first = neighbors[pixel][first];
84 for (int i=0;i<3;i++)
85 color[i] = pixelsColor[pixel][i];
86 float divide = 1;
87 if (first != -1)
88 {
89 divide++;
90 for (int i=0;i<3;i++)
91 color[i] += pixelsColor[first][i];
92 }
93 for (int i=0;i<3;i++)
94 color[i] /= divide;
95 glColor3fv(color);
96}
97/************************************************************
98 * DRAW BLURRY HEXAGON. draws a solid hexagon, with interpolated colours
99 ************************************************************/
100void RawDataViewer::drawBlurryHexagon(int index)
101{
102
103//per-pixel mesh
104 GLfloat color[3];
105 for (int i=0;i<3;i++)
106 color[i] = pixelsColor[index][i];
107 glBegin(GL_TRIANGLES);
108 calcBlurColor(index, 0);
109 glVertex2fv(verticesList[verticesIndices[index][0]]);
110 glColor3fv(color);
111 glVertex2fv(pixelsCoords[index]);
112
113 calcBlurColor(index, 1);
114 glVertex2fv(verticesList[verticesIndices[index][1]]);
115
116 glVertex2fv(verticesList[verticesIndices[index][1]]);
117 glColor3fv(color);
118 glVertex2fv(pixelsCoords[index]);
119
120 calcBlurColor(index, 2);
121 glVertex2fv(verticesList[verticesIndices[index][2]]);
122
123 glVertex2fv(verticesList[verticesIndices[index][2]]);
124 glColor3fv(color);
125 glVertex2fv(pixelsCoords[index]);
126
127 calcBlurColor(index, 3);
128 glVertex2fv(verticesList[verticesIndices[index][3]]);
129
130 glVertex2fv(verticesList[verticesIndices[index][3]]);
131 glColor3fv(color);
132 glVertex2fv(pixelsCoords[index]);
133
134 calcBlurColor(index, 4);
135 glVertex2fv(verticesList[verticesIndices[index][4]]);
136
137 glVertex2fv(verticesList[verticesIndices[index][4]]);
138 glColor3fv(color);
139 glVertex2fv(pixelsCoords[index]);
140
141 calcBlurColor(index, 5);
142 glVertex2fv(verticesList[verticesIndices[index][5]]);
143
144 glVertex2fv(verticesList[verticesIndices[index][5]]);
145 glColor3fv(color);
146 glVertex2fv(pixelsCoords[index]);
147
148 calcBlurColor(index, 0);
149 glVertex2fv(verticesList[verticesIndices[index][0]]);
150 glEnd();
151
152 return;
153}
154
155/************************************************************
156 * DRAW CAMERA draws all the camera pixels
157 ************************************************************/
158void RawDataViewer::drawCamera(bool alsoWire)
159{
160 glLoadIdentity();
161 if (!drawImpulse)
162 {
163 glTranslatef(0,-0.44,0);
164 glRotatef(cameraRotation, 0,0,-1);
165 if (cameraRotation == 90)
166 {
167 glTranslatef(-0.45,-0.45,0);
168 }
169 if (cameraRotation == -90)
170 {
171 glTranslatef(0.45,-0.45,0);
172 }
173 glScalef(1.5,1.5,1);
174 }
175 else
176 {
177 glRotatef(cameraRotation, 0,0,-1);
178 if (cameraRotation == 90)
179 {
180 glTranslatef(-0.45/1.5,-0.45/1.5,0);
181 }
182 if (cameraRotation == -90)
183 {
184 glTranslatef(0.45/1.5,-0.45/1.5,0);
185 }
186 }
187 glColor3f(0.5,0.5,0.5);
188 glLineWidth(1.0);
189 float color;
190
191 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
192 {
193 if (!nRoi)
194 color = (float)(i)/(float)(ACTUAL_NUM_PIXELS);
195 else
196
197// if (_softwareOrdering)
198// color = float(eventData[nRoi*i + whichSlice] + (VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
199// else
200 color = float(eventData[nRoi*hardwareMapping[i] + whichSlice]+(VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
201 if (logScale)
202 {
203 color *= 9;
204 color += 1;
205 color = log10(color);
206 }
207
208 if (color < ss[0])
209 {
210 pixelsColor[i][0] = tooLowValueCoulour[0];
211 pixelsColor[i][1] = tooLowValueCoulour[1];
212 pixelsColor[i][2] = tooLowValueCoulour[2];
213 continue;
214 }
215 if (color > ss[4])
216 {
217 pixelsColor[i][0] = tooHighValueCoulour[0];
218 pixelsColor[i][1] = tooHighValueCoulour[1];
219 pixelsColor[i][2] = tooHighValueCoulour[2];
220 continue;
221 }
222 int index = 0;
223 while (ss[index] < color && index < 4)
224 index++;
225 index--;
226 if (index < 0) index = 0;
227 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
228 if (weight0 > 1.0f) weight0 = 1.0f;
229 if (weight0 < 0.0f) weight0 = 0.0f;
230 float weight1 = 1.0f-weight0;
231 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
232 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
233 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
234 }
235
236 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
237 {
238
239 glColor3fv(pixelsColor[i]);
240 glLoadName(i);
241if (drawBlur)
242 drawBlurryHexagon(i);
243else
244 drawHexagon(i,true);
245
246 }
247 if (!alsoWire)
248 return;
249 glTranslatef(0,0,0.1f);
250 glColor3f(0.0f,0.0f,0.0f);
251 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
252 {
253
254 drawHexagon(i, false);
255 }
256
257}
258
259/************************************************************
260 * DRAW PIXEL CURVE. draws the raw impulse curve of the currently selected pixel
261 ************************************************************/
262void RawDataViewer::drawPixelCurve()
263{
264 float xRange = bboxMax[0] - bboxMin[0];
265 float yRange = bboxMax[1] - bboxMin[1];
266
267 glBegin(GL_LINES);
268 glLineWidth(1.0f);
269 glColor3f(0.0,0.0,0.0);
270 glVertex2f(bboxMin[0], bboxMin[1]);
271 glVertex2f(bboxMax[0], bboxMin[1]);
272 glVertex2f(bboxMin[0], bboxMin[1]);
273 glVertex2f(bboxMin[0], bboxMax[1]);
274 glVertex2f(bboxMin[0], (bboxMin[1]+bboxMax[1])/2.0f);
275 glVertex2f(bboxMax[0], (bboxMin[1]+bboxMax[1])/2.0f);
276 glVertex2f(bboxMin[0] + xRange*nRoi/(float)(nRoi+nRoiTM),
277 bboxMin[1]);
278 glVertex2f(bboxMin[0] + xRange*nRoi/(float)(nRoi+nRoiTM),
279 bboxMax[1]);
280 glEnd();
281 glTranslatef(0,0,0.1f);
282 if (!nRoi)
283 return;
284 glBegin(GL_LINES);
285 glColor3f(1.0f,1.0f,0.0f);
286 float divideMe = (float)(VALUES_SPAN-1);
287 float plusMe = (float)(VALUES_SPAN)/2;
288 if (divideMe <= 0)
289 divideMe = 1;
290
291 /*
292 if (drawCalibrationLoaded)
293 plusMe += 0;//VALUES_SPAN/2;
294 if (drawCalibrationLoaded && calibrationLoaded)
295 {
296 divideMe /=2;
297 plusMe = 0 ;///=2;
298 }*/
299
300// int mapping = _softwareOrdering ? selectedPixel : hardwareMapping[selectedPixel];
301 int mapping = hardwareMapping[selectedPixel];
302 const int hw = mapping;
303 const PixelMapEntry& mapEntry = fPixelMap.index(selectedPixel);
304 const int pixelIdInPatch = mapEntry.pixel();
305 const int patchId = mapEntry.patch();
306 const int boardId = mapEntry.board();
307 const int crateId = mapEntry.crate();
308
309 if (selectedPixel != -1)
310 {
311 for (int i=0;i<nRoi-1;i++)
312 {
313 float d1 = eventData[nRoi*hw + i]+plusMe;
314 float d2 = eventData[nRoi*hw + i+1]+plusMe;
315 if (!finite(d1)) d1 = 20000;
316 if (!finite(d2)) d2 = 20000;
317 glVertex2f(bboxMin[0] + xRange*i/(float)(nRoi+nRoiTM),
318 bboxMin[1] + yRange*(d1) /divideMe);
319 glVertex2f(bboxMin[0] + xRange*(i+1)/(float)(nRoi+nRoiTM),
320 bboxMin[1] + yRange*(d2) /divideMe);
321 }
322 glEnd();
323
324 glColor3f(0.0f, 1.0f, 1.0f);
325 glBegin(GL_LINES);
326 if (pixelIdInPatch == 8)//this channel has a time marker
327 {
328
329 for (int i=0;i<nRoiTM-1;i++)
330 {
331 float d1 = eventData[nRoi*1440 + nRoiTM*(40*crateId + 4*boardId + patchId) + i] + plusMe;
332 float d2 = eventData[nRoi*1440 + nRoiTM*(40*crateId + 4*boardId + patchId) + i+1] + plusMe;
333 if (!finite(d1)) d1 = 20000;
334 if (!finite(d2)) d2 = 20000;
335 glVertex2f(bboxMin[0] + xRange*(i+nRoi)/(float)(nRoi+nRoiTM),
336 bboxMin[1] + yRange*(d1)/divideMe);
337 glVertex2f(bboxMin[0] + xRange*(i+1+nRoi)/(float)(nRoi+nRoiTM),
338 bboxMin[1] + yRange*(d2) / divideMe);
339 }
340 }
341
342 }
343 glEnd();
344 glTranslatef(0,0,0.1f);
345 glBegin(GL_LINES);
346 glColor3f(1.0,0.0,0.0);
347 glVertex2f(bboxMin[0] + xRange*whichSlice/(float)(nRoi+nRoiTM),
348 bboxMin[1]);
349 glVertex2f(bboxMin[0] + xRange*whichSlice/(float)(nRoi+nRoiTM),
350 bboxMax[1]);
351
352 glEnd();
353
354}
355/************************************************************
356 * CONSTRUCTOR.
357 ************************************************************/
358RawDataViewer::RawDataViewer(QWidget *cParent) : BasicGlCamera(cParent), RMSvalues(1440), Meanvalues(1440), Maxvalues(1440), PosOfMaxvalues(1440), VALUES_SPAN(4096)
359
360{
361
362 whichSlice = 0;
363
364 nRoi = 0;
365 nRoiTM = 0;
366 offSetRoi = 0;
367 eventNum = 0;
368 rowNum = -1;
369 eventStep = 1;
370 selectedPixel = 393;
371 inputFile = NULL;
372 drawPatch = false;
373 drawImpulse = true;
374 drawBlur = false;
375 loopCurrentEvent = false;
376 fIsDrsCalibration = false;
377 SetAutoRefresh(true);
378 runType = "unkown";
379
380
381}
382
383void RawDataViewer::assignPixelMapFile(const string& map)
384{
385 PixelMap mypMap;
386 if (map.empty())
387 {
388 if (!mypMap.Read("FACTmap111030.txt"))
389 {
390 if (!mypMap.Read("/swdev_nfs/FACT++/FACTmap111030.txt"))
391 {
392 if (!mypMap.Read("./FACTmap111030.txt"))
393 {
394 cerr << "ERROR - Problems reading FACTmap111030.txt" << endl;
395 exit(-1);
396 }
397 }
398 }
399 }
400 else
401 {
402 if (!mypMap.Read(map))
403 {
404 cerr << "ERROR - Problems reading mapping file '" << map << "'" << endl;
405 exit(-1);
406 }
407 }
408
409 assignPixelMap(mypMap);
410
411 for (int i=0;i<160;i++)
412 {
413 const float color[3] = { 0.5, 0.5, 0.3 };
414
415 for (int j=0;j<3;j++)
416 patchesColor[i][j] = color[j];
417 }
418 fZeroArray = NULL;
419
420 _softwareOrdering = false;
421}
422/************************************************************
423 * DESTRUCTOR
424 ************************************************************/
425RawDataViewer::~RawDataViewer()
426{
427 if (inputFile != NULL)
428 {
429 inputFile->close();
430 delete inputFile;
431 }
432 if (fZeroArray != NULL)
433 delete[] fZeroArray;
434}
435void RawDataViewer::allocateZeroArray()
436{
437 if (fZeroArray == NULL)
438 {
439 fZeroArray = new char[8192];
440 }
441}
442/************************************************************
443 * PAINT GL. main drawing function.
444 ************************************************************/
445void RawDataViewer::paintGL()
446{
447 //Should not be required, but apparently it helps when piping it through X forwarding
448 glFinish();
449 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
450 glLoadIdentity();
451
452 glTranslatef(0,0,-0.5);
453
454 if (drawBlur)
455 {
456 glShadeModel(GL_SMOOTH);
457 drawCamera(false);
458 }
459 else
460 {
461 glShadeModel(GL_FLAT);
462 drawCamera(true);
463 }
464 glTranslatef(0,0,0.1f);
465 if (drawPatch)
466 drawPatches();
467 glTranslatef(0,0,0.1f);
468 if (!drawBlur && (selectedPixel != -1))
469 {
470 glLineWidth(1.0f);
471 glColor3f(1.0,1.0,1.0);
472 drawHexagon(selectedPixel, false);
473 }
474 glTranslatef(0,0,0.1f);
475 if (drawImpulse)
476 {
477 // glRotatef(cameraRotation, 0,0,1);
478 glLoadIdentity();
479 glLineWidth(2.0);
480 drawPixelCurve();
481 }
482 glTranslatef(0,0,0.1f);
483 DrawScale();
484}
485
486/************************************************************
487 * MOUSE PRESS EVENT. mouse click handler.
488 ************************************************************/
489void RawDataViewer::mousePressEvent(QMouseEvent *cEvent)
490{
491 if (cEvent->pos().x() > width()-(width()/50.f))
492 {
493 toggleInterfaceDisplay();
494 return;
495 }
496 lastPos = cEvent->pos();
497 if (setCorrectSlice(cEvent))
498 return;
499 int face = PixelAtPosition(cEvent->pos());
500
501 selectedPixel = face;
502 emit signalCurrentPixel(face);
503
504 updateGL();
505}
506
507/************************************************************
508 * SET CORRECT SLICE. if displayed, figures out if the graph was
509 * clicked, and if so, which slice should be displayed
510 ************************************************************/
511bool RawDataViewer::setCorrectSlice(QMouseEvent* cEvent)
512{
513 if (!drawImpulse)
514 return false;
515 float cx = (float)cEvent->x() * pixelSize - shownSizex/2;
516 float cy = ((float)height()-(float)cEvent->y())*pixelSize - shownSizey/2;
517 if (cx < bboxMin[0] ||
518 cx > bboxMax[0] ||
519 cy < bboxMin[1] ||
520 cy > bboxMax[1])
521 return false;
522 whichSlice = (cx - bboxMin[0])*(nRoi+nRoiTM)/(bboxMax[0] - bboxMin[0]);
523 if (whichSlice >= nRoi)
524 whichSlice = nRoi-1;
525 emit signalCurrentSlice(whichSlice);
526 return true;
527}
528
529/************************************************************
530 * MOUSE MOVE EVENT. used to track the dragging of slices display
531 ************************************************************/
532void RawDataViewer::mouseMoveEvent(QMouseEvent *cEvent)
533{
534 if (cEvent->buttons() & Qt::LeftButton) {
535 setCorrectSlice(cEvent);
536 updateGL();
537 } else if (cEvent->buttons() & Qt::RightButton) {
538 updateGL();
539 }
540 lastPos = cEvent->pos();
541}
542
543/************************************************************
544 * MOUSE DOUBLE CLICK EVENT. used to select pixels
545 ************************************************************/
546void RawDataViewer::mouseDoubleClickEvent(QMouseEvent *cEvent)
547{
548 int face = PixelAtPosition(cEvent->pos());
549 if (face != -1) {
550 selectedPixel = face;
551 emit signalCurrentPixel(face);
552 updateGL();
553 }
554}
555
556/************************************************************
557 * OPEN FILE. opens a new fits file
558 ************************************************************/
559void RawDataViewer::openFile(string file, bool reopen)
560{
561 if (inputFile)
562 {
563 inputFile->close();
564 delete inputFile;
565 }
566 try {
567 inputFile = reopen && fIsDrsCalibration ? new zfits(file, "Events") : new factfits(file, "Events");
568 }
569 catch (std::runtime_error e)
570 {
571 cout << "Something went wrong while loading fits. Aborting: " << e.what() << endl;
572 return;
573 }
574 if (!*inputFile)
575 {
576 delete inputFile;
577 inputFile = NULL;
578 return;
579 }
580 vector<string> entriesToCheck;
581 if (inputFile->IsCompressedFITS())
582 entriesToCheck.push_back("ZNAXIS2");
583 else
584 entriesToCheck.push_back("NAXIS2");
585 entriesToCheck.push_back("NROI");
586 entriesToCheck.push_back("REVISION");
587 entriesToCheck.push_back("RUNID");
588 entriesToCheck.push_back("NBOARD");
589 entriesToCheck.push_back("NPIX");
590 entriesToCheck.push_back("NROITM");
591 entriesToCheck.push_back("TIMESYS");
592 entriesToCheck.push_back("DATE");
593 entriesToCheck.push_back("NIGHT");
594 entriesToCheck.push_back("CAMERA");
595 entriesToCheck.push_back("DAQ");
596 entriesToCheck.push_back("TSTART");
597 entriesToCheck.push_back("TSTOP");
598
599
600 for (vector<string>::const_iterator it=entriesToCheck.begin(); it != entriesToCheck.end(); it++)
601 {
602 try {
603 if (!inputFile->HasKey(*it)){
604 cout << "Warning: header keyword " << *it << " missing." << endl;
605 }
606 }
607 catch (std::runtime_error e)
608 {
609 cout << e.what() << endl;
610 return;
611 }
612 }
613
614 nRows = 0;
615 if (inputFile->IsCompressedFITS())
616 nRows = inputFile->HasKey("ZNAXIS2") ? inputFile->GetInt("ZNAXIS2") : 0;
617 else
618 nRows = inputFile->HasKey("NAXIS2") ? inputFile->GetInt("NAXIS2") : 0;
619 nRoi = inputFile->HasKey("NROI") ? inputFile->GetInt("NROI") : 0;
620 runNumber = inputFile->HasKey("RUNID") ? inputFile->GetInt("RUNID") : -1;
621 nTM = inputFile->HasKey("NTMARK") ? inputFile->GetInt("NTMARK") : 0;
622
623 runType = "unkown";
624 if (inputFile->HasKey("RUNTYPE"))
625 {
626 runType = inputFile->GetStr("RUNTYPE");
627 if (runType == "")
628 runType = "unkown";
629 }
630 firstDataTime = inputFile->HasKey("TSTART") ? inputFile->GetInt("TSTART") : -1;
631 lastDataTime = inputFile->HasKey("TSTOP") ? inputFile->GetInt("TSTOP"): -1;
632 nRoiTM = inputFile->HasKey("NROITM") ? inputFile->GetInt("NROITM") : 0;
633 revision = inputFile->HasKey("REVISION") ? inputFile->GetInt("REVISION") : -1;
634 builderVersion = inputFile->HasKey("BLDVER") ? inputFile->GetInt("BLDVER") : -1;
635 nBoards = inputFile->HasKey("NBOARD") ? inputFile->GetInt("NBOARD") : 0;
636 nPixels = inputFile->HasKey("NPIX") ? inputFile->GetInt("NPIX") : 0;
637 timeSystem = inputFile->HasKey("TIMESYS") ? inputFile->GetStr("TIMESYS") : "";
638 creationDate = inputFile->HasKey("DATE") ? inputFile->GetStr("DATE") : "";
639 nightInt = inputFile->HasKey("NIGHT") ? inputFile->GetInt("NIGHT") : 0;
640 camera = inputFile->HasKey("CAMERA") ? inputFile->GetStr("CAMERA") : "";
641 daq = inputFile->HasKey("DAQ") ? inputFile->GetStr("DAQ") : "";
642 adcCount = inputFile->HasKey("ADCRANGE") ? inputFile->GetFloat("ADCRANGE") : 2000;
643 if (nPixels == 0)
644 {
645 cout << "could not read num pixels from fits header. Assuming 1440 (FACT)." << endl;
646 nPixels = 1440;
647 }
648 if (nRoi == 0 && !inputFile->HasKey("NROI"))
649 {//let's try to figure out the roi from the column's format
650 const fits::Table::Columns& cols = inputFile->GetColumns();
651 if (cols.find("Data") == cols.end())
652 {
653 cout << "ERROR: Column \"Data\" could not be found. abortin load." << endl;
654 return;
655 }
656 const fits::Table::Columns::const_iterator col = cols.find("Data");
657 if (col->second.type != 'I')
658 {
659 cout << "ERROR: Data Column has type " << col->second.type << " while viewer expects I" << endl;
660 return;
661 }
662 if (col->second.num % nPixels != 0)
663 {
664 cout << "ERROR: Num pixels (" << nPixels << ") does not match Data length (" << col->second.num << "). Aborting" << endl;
665 return;
666 }
667 nRoi = col->second.num/nPixels;
668 cout << "Estimate num samples per pixels to be " << nRoi;
669 _softwareOrdering = true;
670 }
671 else
672 _softwareOrdering = inputFile->Get<string>("ISMC", "F")=="T";
673
674 if (inputFile->HasKey("OFFSET"))
675 offSetRoi = inputFile->GetInt("OFFSET");
676
677 nbOk = 0;//inputFile->GetInt("NBEVTOK");
678 nbRej = 0;//inputFile->GetInt("NBEVTREJ");
679 nbBad = 0;//inputFile->GetInt("NBEVTBAD");
680
681 eventNum = 1;
682
683 eventData.resize(1440*nRoi + 160*nRoiTM);
684 rawEventData.resize(1440*nRoi + 160*nRoiTM);
685 waveLetArray.resize(1024*1440);
686 try
687 {
688 inputFile->SetPtrAddress("Data", rawEventData.data());
689 if (inputFile->HasColumn("EventNum"))
690 inputFile->SetPtrAddress("EventNum", &eventNum);
691 else
692 cout << "Warning: could not find column \"EventNum\"" << endl;
693 if (inputFile->HasColumn("TriggerType"))
694 inputFile->SetPtrAddress("TriggerType", &triggerType);
695 else
696 cout << "Warning: could not find column \"TriggerType\"" << endl;
697 if (inputFile->HasColumn("SoftTrig"))
698 inputFile->SetPtrAddress("SoftTrig", &softTrig);
699 else
700 cout << "Warning: could not find column \"SoftTrig\"" << endl;
701 if (inputFile->HasColumn("BoardTime"))
702 inputFile->SetPtrAddress("BoardTime", boardTime);
703 else
704 cout << "Warning: could not find column \"BoardTime\"" << endl;
705 if (inputFile->HasColumn("StartCellData"))
706 inputFile->SetPtrAddress("StartCellData", startPix);
707 else
708 cout << "Warning: could not find column \"StartCellData\"" << endl;
709 if (inputFile->HasColumn("StartCellTimeMarker"))
710 inputFile->SetPtrAddress("StartCellTimeMarker", startTM);
711 else
712 cout << "Warning: could not find column \"StartCellTimeMarker\"" << endl;
713 if (inputFile->HasColumn("TimeMarker"))
714 inputFile->SetPtrAddress("TimeMarker", rawEventData.data()+1440*nRoi);
715 else
716 cout << "Warning: could not find column \"TimeMarker\"" << endl;
717 }
718 catch (const runtime_error &e)
719 {
720 cout << e.what() << endl;
721 cout << "Loading aborted." << endl;
722
723 nRoi = nRows = 0;
724
725 return;
726 }
727
728 try
729 {
730 pcTime[0] = pcTime[1] = 0;
731 if (inputFile->HasColumn("UnixTimeUTC"))
732 inputFile->SetPtrAddress("UnixTimeUTC", pcTime);
733 }
734 catch (const runtime_error&)
735 {
736 try
737 {
738 if (inputFile->HasColumn("PCTime"))
739 inputFile->SetPtrAddress("PCTime", pcTime);
740 else
741 cout << "Warning: could not find column \"UnixTimeUTC\" nor \"PCTime\"" << endl;
742
743 }
744 catch (const runtime_error&)
745 {
746
747 }
748 }
749
750
751 if (!reopen)
752 {
753 int backupStep = eventStep;
754 rowNum = -1;
755 eventStep = 1;
756 plusEvent();
757 eventStep = backupStep;
758 }
759 else
760 readEvent();
761
762 emit newFileLoaded();
763 emit signalCurrentPixel(selectedPixel);
764}
765
766void RawDataViewer::openCalibFile(string& file)
767{
768 //calibrationLoaded = false;
769 string msg;
770 try
771 {
772 msg = fDrsCalib.ReadFitsImp(file);
773 if (msg.empty())
774 {
775 emit newFileLoaded();
776 updateGL();
777 return;
778 }
779 }
780 catch (const runtime_error &e)
781 {
782 msg = string("Something went wrong while loading Drs Calib: ") + e.what() + string(".. Aborting file loading");
783 }
784 cerr << msg << endl;
785 fDrsCalib.Clear();
786}
787
788template <typename T>
789void RawDataViewer::getCalibrationDataForDisplay(const CalibDataTypes/* calibTypes*/,
790 const vector<T>& inputData,
791 const int roi,
792 const int roiTM)
793{
794 eventData.assign(inputData.begin(), inputData.end());
795 nRoi=roi;
796 nRoiTM=roiTM;
797
798 long long min, max, mean;
799 min = max = inputData[0];
800 mean=0;
801 for (int i=0;i<1440*roi + 160*roiTM;i++) {
802 mean += inputData[i];
803 if (inputData[i] > max)
804 max = inputData[i];
805 if (inputData[i] < min)
806 min = inputData[i];
807 }
808 mean /= 1440*roi + 160*roiTM;
809 for (int i=0;i<1440*roi + 160*roiTM;i++)
810 eventData[i] -= (float)mean;
811 VALUES_SPAN = max - min;
812// cout << VALUES_SPAN << " " << min << " " << max << " " << mean << endl;
813// cout << 1440*roi + 160*roiTM << " " << roi << " " << roiTM << " " << inputData.size() << endl;
814}
815/************************************************************
816 * PLUS EVENT
817 ************************************************************/
818void RawDataViewer::plusEvent()
819{
820 eventStepping(true);
821}
822/************************************************************
823 * MINUS EVENT
824 ************************************************************/
825void RawDataViewer::minusEvent()
826{
827 eventStepping(false);
828}
829/************************************************************
830 * SET EVENT STEP
831 ************************************************************/
832void RawDataViewer::setEventStep(int step)
833{
834 eventStep = step;
835}
836/************************************************************
837 * EVENT STEPPING
838 ************************************************************/
839
840void RawDataViewer::ApplyCalibration()
841{
842 eventData.assign(rawEventData.begin(), rawEventData.end());
843
844 if (fIsDrsCalibration)
845 {
846 fDrsCalib.Apply(eventData.data(), rawEventData.data(), startPix, nRoi);
847 DrsCalibrate::RemoveSpikes(eventData.data(), nRoi);
848 //TODO apply calibration to the Time markers
849 }
850
851 //hide the time markers
852 int nSlicesToRemove = 60;
853 float* backupData = 0;
854 if (nRoiTM == 0) //they are written into the regular channel
855 {
856 backupData = new float[nSlicesToRemove*160];
857 for (int i=0;i<1440;i++)
858 {
859 const PixelMapEntry& mapEntry = fPixelMap.index(i);
860 const int pixelIdInPatch = mapEntry.pixel();
861 const int patchId = mapEntry.patch();
862 const int boardId = mapEntry.board();
863 const int crateId = mapEntry.crate();
864
865 const int hw = mapEntry.hw();
866 if (pixelIdInPatch == 8)
867 {
868 for (int j=0;j<nSlicesToRemove;j++)
869 {
870 backupData[(40*crateId + 4*boardId + patchId)*nSlicesToRemove+j] = eventData[(hw*nRoi) + (nRoi-nSlicesToRemove) + j];
871 eventData[(hw*nRoi) + (nRoi-nSlicesToRemove) + j] = eventData[hw*nRoi + (nRoi-nSlicesToRemove) - 1];
872 }
873 }
874 }
875 }
876
877 vector<float> pixelStatsData(1440*4);
878 DrsCalibrate::GetPixelStats(pixelStatsData.data(), eventData.data(), nRoi);
879
880 for (vector<PixelMapEntry>::const_iterator it=fPixelMap.begin(); it!=fPixelMap.end(); it++)
881 {
882 Meanvalues[it->index] = pixelStatsData[0*1440+it->hw()];
883 RMSvalues[it->index] = pixelStatsData[1*1440+it->hw()];
884 Maxvalues[it->index] = pixelStatsData[2*1440+it->hw()];
885 PosOfMaxvalues[it->index] = pixelStatsData[3*1440+it->hw()];
886 }
887 if (nRoiTM == 0)//move back the data back in place
888 {
889 for (int i=0;i<1440;i++)
890 {
891 const PixelMapEntry& mapEntry = fPixelMap.index(i);
892 const int pixelIdInPatch = mapEntry.pixel();
893 const int patchId = mapEntry.patch();
894 const int boardId = mapEntry.board();
895 const int crateId = mapEntry.crate();
896 if (patchId > 160)
897 cout << "Voila mon probleme: " << patchId << endl;
898 const int hw = mapEntry.hw();
899 if (pixelIdInPatch == 8)
900 {
901 // cout << "|" << crateId << " " << boardId << " " << patchId << " " << hw << "| ";
902 for (int j=0;j<nSlicesToRemove;j++)
903 {
904 eventData[(hw*nRoi) + (nRoi - nSlicesToRemove) + j] = backupData[(40*crateId + 4*boardId + patchId)*nSlicesToRemove+j];
905 }
906 }
907 }
908 delete[] backupData;
909 }
910 if (isVisible())
911 updateGL();
912}
913
914void RawDataViewer::eventStepping(bool plus)
915{
916 if (plus)
917 rowNum += eventStep;
918 else
919 rowNum -= eventStep;
920 if (rowNum >= nRows)
921 rowNum -= nRows;
922 if (rowNum < 0)
923 rowNum += nRows;
924
925 readEvent();
926}
927
928void RawDataViewer::readEvent()
929{
930 if (inputFile == NULL)
931 return;
932 inputFile->GetRow(rowNum);
933 if (_softwareOrdering)
934 {//remap pixels data according to hardware id
935 if (nRoiTM != 0)
936 cout << "Warning: did not expect Time Markers data from Monte-Carlo simulations. These will not be mapped properly." << endl;
937 //first copy the data
938 vector<int16_t> tempData(rawEventData.begin(), rawEventData.begin()+1440*nRoi);
939 //copy back the data and re-map it on the fly
940 for (int i=0;i<1440;i++)
941 for (int j=0;j<nRoi;j++)
942 rawEventData[i*nRoi + j] = tempData[softwareMapping[i]*nRoi + j];
943 }
944// cout << "Getting row " << rowNum << endl;
945
946
947 ApplyCalibration();
948
949 emit signalCurrentEvent(eventNum);
950 emit signalCurrentPixel(selectedPixel);
951}
952
953/************************************************************
954 * NEXT SLICE. deprec ?
955 ************************************************************/
956void RawDataViewer::nextSlice()
957{
958 whichSlice++;
959 if (whichSlice >= nRoi)
960 {
961 whichSlice = 0;
962 if (!loopCurrentEvent)
963 {
964 int backupStep = eventStep;
965 eventStep = 1;
966 eventStepping(true);
967 eventStep = backupStep;
968 }
969 }
970 emit signalCurrentSlice(whichSlice);
971 updateGL();
972}
973void RawDataViewer::previousSlice()
974{
975 whichSlice--;
976 if (whichSlice < 0)
977 {
978 whichSlice = nRoi-1;
979 if (!loopCurrentEvent)
980 {
981 int backupStep = eventStep;
982 eventStep = 1;
983 eventStepping(false);
984 eventStep = backupStep;
985 }
986 }
987 emit signalCurrentSlice(whichSlice);
988 updateGL();
989}
990void RawDataViewer::setCurrentPixel(int pix)
991{
992 // if (pix == -1)
993 // return;
994 selectedPixel = pix;
995 if (isVisible())
996 updateGL();
997 emit signalCurrentPixel(pix);
998}
999void RawDataViewer::computePulsesStatistics()
1000{
1001 if (!inputFile)
1002 {
1003 cout << "A FITS file must be open in order to complete this operation" << endl;
1004 return;
1005 }
1006
1007
1008// for (int i=0;i<nRows;i++)//for all events
1009// {
1010// inputFile->GetRow(rowNum);
1011// for (int i=0;i<(1440+160)*nRoi;i++)
1012// eventData[i] = (float)rawEventData[i];
1013
1014// for (int j=0;j<ACTUAL_NUM_PIXELS;j++)
1015/// {
1016 int j = selectedPixel;
1017 if (j == -1)
1018 return;
1019 for (int i=0;i<nRoi;i++)
1020 {
1021 aMeas[i] = eventData[j*nRoi+i];// * adcCount;
1022
1023 }
1024 for (int i=0;i<nRoi;i++)
1025 {
1026 if (i==0)
1027 n1mean[i] = aMeas[i+1];
1028 else
1029 {
1030 if (i==1023)
1031 n1mean[i] = aMeas[i-1];
1032 else
1033 n1mean[i] = (aMeas[i-1]+aMeas[i+1])/2.f;
1034 }
1035 }
1036 //find spike
1037 for (int i=0;i<nRoi-3;i++)
1038 {
1039 const float fract = 0.8f;
1040 float xx, xp, xpp;
1041 vCorr[i] = 0;//aMeas[i];
1042 xx = aMeas[i] - n1mean[i];
1043 if (xx < -8.f)
1044 {
1045 xp = aMeas[i+1] - n1mean[i+1];
1046 xpp = aMeas[i+2] - n1mean[i+2];
1047 if ((aMeas[i+2] - (aMeas[i] + aMeas[i+3])/2.f) > 10.f)
1048 {
1049 vCorr[i+1] = (aMeas[i] + aMeas[i+3])/2.f;
1050 vCorr[i+2] = (aMeas[i] + aMeas[i+3])/2.f;
1051 i = i+2;
1052 }
1053 else
1054 {
1055 if ((xp > -2.*xx*fract) && (xpp < -10.f))
1056 {
1057 vCorr[i+1] = n1mean[i+1];
1058 n1mean[i+2] = aMeas[i+1] - aMeas[i+3]/2.f;
1059 i++;
1060 }
1061 }
1062 }
1063 }
1064 for (int i=0;i<nRoi;i++)
1065 n1mean[i] = aMeas[i]-n1mean[i];
1066 // }
1067 // }
1068}
1069/************************************************************
1070 * UICONNECTOR CONSTRUCTOR
1071 ************************************************************/
1072UIConnector::UIConnector(QWidget *p)
1073{
1074 setupUi(this);
1075 initHistograms();
1076
1077 updateSpinnerDisplay = true;
1078 updating = false;
1079
1080 timer.setInterval(10.0);
1081 QObject::connect(&timer, SIGNAL(timeout()),
1082 this, SLOT(nextSlicePlease()));
1083
1084 QButtonGroup &scaleGroup = *new QButtonGroup(p);// = new QButtonGroup(canvas);
1085 QButtonGroup &animateGroup = *new QButtonGroup(p);// = new QButtonGroup(canvas);
1086
1087 scaleGroup.addButton(currentPixelScale);
1088 scaleGroup.addButton(entireCameraScale);
1089
1090 animateGroup.addButton(playEventsRadio);
1091 animateGroup.addButton(playSlicesRadio);
1092 animateGroup.addButton(playPixelsRadio);
1093
1094 entireCameraScale->setChecked(true);
1095
1096 RMS_window->enableText(false);
1097 Mean_window->enableText(false);
1098 PosOfMax_window->enableText(false);
1099 Max_window->enableText(false);
1100
1101 // RMS_window->ShowPatchCursor(true);
1102
1103 QObject::connect(GLWindow, SIGNAL(colorPaletteHasChanged()),
1104 this, SLOT(on_autoScaleColor_clicked()));
1105 QObject::connect(GLWindow, SIGNAL(signalCurrentSlice(int)),
1106 this, SLOT(currentSliceHasChanged(int)));
1107 QObject::connect(GLWindow, SIGNAL(signalCurrentEvent(int)),
1108 this, SLOT(currentEventHasChanged(int)));
1109 QObject::connect(GLWindow, SIGNAL(signalCurrentPixel(int)),
1110 this, SLOT(pixelChanged(int)));
1111 QObject::connect(GLWindow, SIGNAL(newFileLoaded()),
1112 this, SLOT(newFileLoaded()));
1113
1114 QObject::connect(RMS_window, SIGNAL(signalCurrentPixel(int)),
1115 GLWindow, SLOT(setCurrentPixel(int)));
1116 QObject::connect(Max_window, SIGNAL(signalCurrentPixel(int)),
1117 GLWindow, SLOT(setCurrentPixel(int)));
1118 QObject::connect(PosOfMax_window, SIGNAL(signalCurrentPixel(int)),
1119 GLWindow, SLOT(setCurrentPixel(int)));
1120 QObject::connect(Mean_window, SIGNAL(signalCurrentPixel(int)),
1121 GLWindow, SLOT(setCurrentPixel(int)));
1122
1123
1124
1125 show();
1126}
1127UIConnector::~UIConnector()
1128{
1129 grid1->detach();
1130 grid2->detach();
1131 grid3->detach();
1132 grid4->detach();
1133 grid5->detach();
1134 grid6->detach();
1135 boardsTimeHistoItem.detach();
1136 startCellHistoItem.detach();
1137 startTimeMarkHistoItem.detach();
1138 pixelValueCurveItem.detach();
1139 pixelAverageCurveItem.detach();
1140 aMeanCurveItem.detach();
1141 vCorrCurveItem.detach();
1142 meanCurveItem.detach();
1143}
1144void UIConnector::slicesPlusPlus()
1145{
1146 GLWindow->nextSlice();
1147}
1148void UIConnector::slicesMinusMinus()
1149{
1150 GLWindow->previousSlice();
1151}
1152void UIConnector::on_calibratedCheckBox_stateChanged(int state)
1153{
1154 GLWindow->fIsDrsCalibration = state;
1155
1156 if (currentCalibFile.empty())
1157 GLWindow->openFile(currentFile, true);
1158
1159 GLWindow->ApplyCalibration();
1160
1161 threeD_Window->setData(GLWindow->eventData.data());
1162
1163 on_autoScaleColor_clicked();
1164 pixelChanged(GLWindow->selectedPixel);
1165
1166}
1167/************************************************************
1168 * DRAW PATCHES CHECK CHANGE. checkbox handler
1169 ************************************************************/
1170void UIConnector::on_drawPatchCheckBox_stateChanged(int state)
1171{
1172 GLWindow->drawPatch = state;
1173 GLWindow->updateGL();
1174 RMS_window->fDrawPatch = state;
1175 RMS_window->updateGL();
1176 Mean_window->fDrawPatch = state;
1177 Mean_window->updateGL();
1178 Max_window->fDrawPatch = state;
1179 Max_window->updateGL();
1180 PosOfMax_window->fDrawPatch = state;
1181 PosOfMax_window->updateGL();
1182}
1183/************************************************************
1184 * DRAW IMPULSE CHECK CHANGE. checkbox handler
1185 ************************************************************/
1186void UIConnector::on_drawImpulseCheckBox_stateChanged(int state)
1187{
1188 GLWindow->drawImpulse = state;
1189 TriggerOffset_window->drawImpulse = state;
1190 Gain_window->drawImpulse = state;
1191 Baseline_window->drawImpulse = state;
1192 GLWindow->updateGL();
1193 TriggerOffset_window->updateGL();
1194 Gain_window->updateGL();
1195 Baseline_window->updateGL();
1196}
1197/************************************************************
1198 * DRAW BLUR CHECK CHANGE. checkbox handler
1199 ************************************************************/
1200void UIConnector::on_drawBlurCheckBox_stateChanged(int state)
1201{
1202 GLWindow->drawBlur = state;
1203 GLWindow->updateGL();
1204}
1205void UIConnector::on_loopOverCurrentEventBox_stateChanged(int state)
1206{
1207 GLWindow->loopCurrentEvent = state;
1208}
1209
1210/************************************************************
1211 * NEXT SLICE PLEASE
1212 ************************************************************/
1213void UIConnector::nextSlicePlease()
1214{
1215 if (playEventsRadio->isChecked ())
1216 GLWindow->eventStepping(true);
1217 else
1218 if (playPixelsRadio->isChecked())
1219 GLWindow->setCurrentPixel((GLWindow->getCurrentPixel()+1)%1440);
1220 else
1221 GLWindow->nextSlice();
1222}
1223
1224/************************************************************
1225 * SET VIEWER.
1226 ************************************************************/
1227//void UIConnector::setViewer(RawDataViewer* v)
1228//{
1229// viewer = v;
1230//}
1231/************************************************************
1232 * SLICES PER SECOND CHANGED. timing ui handler
1233 ************************************************************/
1234void UIConnector::slicesPerSecondChanged(double value)
1235{
1236 timer.setInterval(1000.0/value);
1237}
1238
1239void UIConnector::on_colorRange0_valueChanged(double value) { GLWindow->ss[0] = (float)value; GLWindow->updateGL(); }
1240void UIConnector::on_colorRange1_valueChanged(double value) { GLWindow->ss[1] = (float)value; GLWindow->updateGL(); }
1241void UIConnector::on_colorRange2_valueChanged(double value) { GLWindow->ss[2] = (float)value; GLWindow->updateGL(); }
1242void UIConnector::on_colorRange3_valueChanged(double value) { GLWindow->ss[3] = (float)value; GLWindow->updateGL(); }
1243void UIConnector::on_colorRange4_valueChanged(double value) { GLWindow->ss[4] = (float)value; GLWindow->updateGL(); }
1244
1245void UIConnector::on_redValue0_valueChanged(double value) { GLWindow->rr[0] = (float)value; GLWindow->updateGL(); }
1246void UIConnector::on_redValue1_valueChanged(double value) { GLWindow->rr[1] = (float)value; GLWindow->updateGL(); }
1247void UIConnector::on_redValue2_valueChanged(double value) { GLWindow->rr[2] = (float)value; GLWindow->updateGL(); }
1248void UIConnector::on_redValue3_valueChanged(double value) { GLWindow->rr[3] = (float)value; GLWindow->updateGL(); }
1249void UIConnector::on_redValue4_valueChanged(double value) { GLWindow->rr[4] = (float)value; GLWindow->updateGL(); }
1250
1251void UIConnector::on_greenValue0_valueChanged(double value) { GLWindow->gg[0] = (float)value; GLWindow->updateGL(); }
1252void UIConnector::on_greenValue1_valueChanged(double value) { GLWindow->gg[1] = (float)value; GLWindow->updateGL(); }
1253void UIConnector::on_greenValue2_valueChanged(double value) { GLWindow->gg[2] = (float)value; GLWindow->updateGL(); }
1254void UIConnector::on_greenValue3_valueChanged(double value) { GLWindow->gg[3] = (float)value; GLWindow->updateGL(); }
1255void UIConnector::on_greenValue4_valueChanged(double value) { GLWindow->gg[4] = (float)value; GLWindow->updateGL(); }
1256
1257void UIConnector::on_blueValue0_valueChanged(double value) { GLWindow->bb[0] = (float)value; GLWindow->updateGL(); }
1258void UIConnector::on_blueValue1_valueChanged(double value) { GLWindow->bb[1] = (float)value; GLWindow->updateGL(); }
1259void UIConnector::on_blueValue2_valueChanged(double value) { GLWindow->bb[2] = (float)value; GLWindow->updateGL(); }
1260void UIConnector::on_blueValue3_valueChanged(double value) { GLWindow->bb[3] = (float)value; GLWindow->updateGL(); }
1261void UIConnector::on_blueValue4_valueChanged(double value) { GLWindow->bb[4] = (float)value; GLWindow->updateGL(); }
1262
1263/************************************************************
1264 * LOAD NEW FILE CLICKED. button handler
1265 ************************************************************/
1266void UIConnector::on_loadNewFileButton_clicked()
1267{
1268 QFileDialog dialog;
1269 dialog.setFileMode(QFileDialog::ExistingFile);
1270 dialog.open(this, SLOT(fileSelected(QString)));
1271 dialog.setVisible(true);
1272 dialog.exec();
1273}
1274void UIConnector::on_loadDRSCalibButton_clicked()
1275{
1276 QFileDialog dialog;
1277 dialog.setFileMode(QFileDialog::ExistingFile);
1278 dialog.open(this, SLOT(calibFileSelected(QString)));
1279 dialog.setVisible(true);
1280 dialog.exec();
1281}
1282/************************************************************
1283 * FILE SELECTED. return of the file open dialog handler
1284 ************************************************************/
1285void UIConnector::fileSelected(QString file)
1286{
1287 const bool reopen = !currentFile.empty();
1288
1289 currentFile = file.toStdString();
1290 if (!currentFile.empty())
1291 GLWindow->openFile(currentFile, reopen && currentCalibFile.empty());
1292}
1293void UIConnector::calibFileSelected(QString file)
1294{
1295 const bool reopen = currentCalibFile.empty();
1296
1297 currentCalibFile = file.toStdString();
1298 if (reopen && !currentFile.empty())
1299 GLWindow->openFile(currentFile, true);
1300 if (currentCalibFile != "")
1301 GLWindow->openCalibFile(currentCalibFile);
1302
1303
1304 if (GLWindow->fDrsCalib.fRoi != 0)
1305 {//spread the calibration data to the displayers
1306 Baseline_window->getCalibrationDataForDisplay(RawDataViewer::CALIB_BASELINE,
1307 GLWindow->fDrsCalib.fOffset,
1308 GLWindow->fDrsCalib.fRoi,
1309 GLWindow->fDrsCalib.fNumTm);
1310
1311 Gain_window->getCalibrationDataForDisplay(RawDataViewer::CALIB_GAIN,
1312 GLWindow->fDrsCalib.fGain,
1313 GLWindow->fDrsCalib.fRoi,
1314 GLWindow->fDrsCalib.fNumTm);
1315
1316 TriggerOffset_window->getCalibrationDataForDisplay(RawDataViewer::CALIB_TRIG_OFFSET,
1317 GLWindow->fDrsCalib.fTrgOff,
1318 GLWindow->fDrsCalib.fRoi,
1319 GLWindow->fDrsCalib.fNumTm);
1320
1321 }
1322}
1323/************************************************************
1324 * NEW FILE LOADED. update of the UI after a new file has been loaded
1325 ************************************************************/
1326void UIConnector::newFileLoaded()
1327{
1328 ostringstream str;
1329
1330 //extract the file name only (no path) from the full name
1331 str << "File: ";
1332 if (currentFile.size() > 2)
1333 str << currentFile.substr(currentFile.find_last_of("//")+1, currentFile.size()) << "\n";
1334 else
1335 str << "--\n";
1336 str << "Calibration: ";
1337 if (currentCalibFile.size() > 2)
1338 str << currentCalibFile.substr(currentCalibFile.find_last_of("//")+1, currentCalibFile.size()) << "\n";
1339 else
1340 str << "--\n";
1341// fileLoadedLabel->setText(QString(str.str().c_str()));
1342// str.str("");
1343 str << "Run number: " << GLWindow->runNumber << "\n";
1344// runNumberLabel->setText(QString(str.str().c_str()));
1345// str.str("");
1346 str << "Number of Events: " << GLWindow->nRows << "\n";
1347
1348 displayingEventBox->setMaximum(GLWindow->nRows-1);
1349
1350 str << "Number of Slices: " << GLWindow->nRoi << "\n";// << "/1024";
1351// numberOfSlicesLabel->setText(QString(str.str().c_str()));
1352// str.str("");
1353 str << "Number of Time Marks: " << GLWindow->nTM << "\n";
1354// numberOfTimeMarksLabel->setText(QString(str.str().c_str()));
1355
1356// str.str("");
1357 str << "Run Type: " << GLWindow->runType << "\n";
1358// runTypeLabel->setText(QString(str.str().c_str()));
1359// str.str("");
1360 str << "Time of 1st data: " << GLWindow->firstDataTime << "\n";
1361// firstTimeLabel->setText(QString(str.str().c_str()));
1362// str.str("");
1363 str << "Time of last data: " << GLWindow->lastDataTime << "\n";
1364// lastTimeLabel->setText(QString(str.str().c_str()));
1365// str.str("");
1366 str << "SVN revision: " << GLWindow->revision << '\n';
1367 str << "Number of boards: " << GLWindow->nBoards << '\n';
1368 str << "Number of pixels: " << GLWindow->nPixels << '\n';
1369 str << "Number of Slices TM: " << GLWindow->nRoiTM << '\n';
1370 str << "Time system: " << GLWindow->timeSystem << '\n';
1371 str << "Date: " << GLWindow->creationDate << '\n';
1372 str << "Night: " << GLWindow->nightInt << '\n';
1373 str << "Camera: " << GLWindow->camera << '\n';
1374 str << "DAQ: " << GLWindow->daq << '\n';
1375 str << "ADC Count: " << GLWindow->adcCount << '\n';
1376 str << "NB Evts OK:" << GLWindow->nbOk << '\n';
1377 str << "NB Evts Rejected: " << GLWindow->nbRej << '\n';
1378 str << "NB Evts Bad: " << GLWindow->nbBad << '\n';
1379 extraInfoLabel->setText(QString(str.str().c_str()));
1380
1381 /*
1382 if (GLWindow->calibrationLoaded)
1383 {
1384 drawCalibrationCheckBox->setEnabled(true);
1385 }*/
1386
1387
1388}
1389/************************************************************
1390 * PLAY PAUSE CLICKED. ui handler
1391 ************************************************************/
1392void UIConnector::on_playPauseButton_clicked()
1393{
1394 if (timer.isActive())
1395 timer.stop();
1396 else
1397 timer.start();
1398}
1399
1400void UIConnector::displaySliceValue()
1401{
1402 if (!GLWindow->nRoi)
1403 return;
1404 if (GLWindow->selectedPixel == -1)
1405 {
1406 ostringstream str;
1407 str << " Current Pixel val.: --";
1408 currentPixelValue->setText(QString(str.str().c_str()));
1409 return;
1410 }
1411// int mapping = GLWindow->_softwareOrdering ? GLWindow->selectedPixel : GLWindow->hardwareMapping[GLWindow->selectedPixel];
1412 int mapping = GLWindow->hardwareMapping[GLWindow->selectedPixel];
1413 const int idx = GLWindow->nRoi*mapping + GLWindow->whichSlice;
1414
1415 ostringstream str;
1416 str << "Current Pixel val.: " << GLWindow->eventData[idx];
1417 currentPixelValue->setText(QString(str.str().c_str()));
1418}
1419
1420/************************************************************
1421 * CURRENT SLICE HAS CHANGE. ui handler
1422 ************************************************************/
1423void UIConnector::currentSliceHasChanged(int slice)
1424{
1425 if (!GLWindow->nRoi)
1426 return;
1427
1428 if (updateSpinnerDisplay)
1429 displayingSliceBox->setValue(slice);
1430
1431 displaySliceValue();
1432}
1433
1434/*****
1435 *******************************************************
1436 * CURRENT EVENT HAS CHANGED. ui handler
1437 ************************************************************/
1438
1439double xval[50000];
1440double yval[50000];
1441void UIConnector::on_displayingEventBox_valueChanged(int cEvent)
1442{
1443// cout << "Here " << updateSpinnerDisplay << endl;
1444 if (!updateSpinnerDisplay)
1445 return;
1446 updateSpinnerDisplay = false;
1447// currentEventHasChanged(cEvent);
1448 GLWindow->rowNum = cEvent - GLWindow->eventStep;
1449 GLWindow->eventStepping(true);
1450 updateSpinnerDisplay = true;
1451
1452// GLWindow->updateGL();
1453}
1454void UIConnector::on_slicesPerSecValue_valueChanged(double value)
1455{
1456 timer.setInterval(1000.0/value);
1457}
1458void UIConnector::on_displayingSliceBox_valueChanged(int cSlice)
1459{
1460 updateSpinnerDisplay = false;
1461 currentSliceHasChanged(cSlice);
1462 updateSpinnerDisplay = true;
1463 GLWindow->whichSlice = cSlice;
1464 GLWindow->updateGL();
1465}
1466void UIConnector::currentEventHasChanged(int )
1467{
1468
1469 RMS_window->SetData(GLWindow->RMSvalues);
1470 Mean_window->SetData(GLWindow->Meanvalues);
1471 PosOfMax_window->SetData(GLWindow->PosOfMaxvalues);
1472 Max_window->SetData(GLWindow->Maxvalues);
1473 threeD_Window->setData(GLWindow->eventData.data());//rawEventData);
1474
1475 if (RMS_window->isVisible())
1476 RMS_window->updateGL();
1477 if (Mean_window->isVisible())
1478 Mean_window->updateGL();
1479 if (PosOfMax_window->isVisible())
1480 PosOfMax_window->updateGL();
1481 if (Max_window->isVisible())
1482 Max_window->updateGL();
1483 ostringstream str;
1484// str << "Displaying Event " << cEvent;
1485// QString qstr(str.str().c_str());
1486// emit updateCurrentEventDisplay(qstr);
1487 if (updateSpinnerDisplay)
1488 {
1489 updateSpinnerDisplay = false;
1490 displayingEventBox->setValue(GLWindow->rowNum);
1491 updateSpinnerDisplay = true;
1492 }
1493
1494 // GLWindow->doWaveLetOnCurrentEventPlease();
1495
1496 //retrieve the data that we want to display
1497 boost::posix_time::ptime hrTime( boost::gregorian::date(1970, boost::gregorian::Jan, 1),
1498 boost::posix_time::seconds(GLWindow->pcTime[0]) + boost::posix_time::microsec(GLWindow->pcTime[1]));
1499
1500 str.str("");
1501 str << "PC Time: " << boost::posix_time::to_iso_extended_string(hrTime);
1502 PCTimeLabel->setText(QString(str.str().c_str()));
1503
1504 str.str("");
1505 str << "Software Trigger: " << GLWindow->softTrig;
1506 softwareTriggerLabel->setText(QString(str.str().c_str()));
1507
1508 str.str("");
1509 str << "Trigger Type: " << GLWindow->triggerType;
1510 triggerTypeLabel->setText(QString(str.str().c_str()));
1511
1512 displaySliceValue();
1513
1514 if (autoScaleColor->isChecked())
1515 emit GLWindow->colorPaletteHasChanged();//autoScalePressed();
1516
1517 boardsTimeList->clear();
1518 startPixelsList->clear();
1519 startTimeMarksList->clear();
1520 triggerDelayList->clear();
1521 std::map<int, int> boardsHistoMap;
1522 for (int i=0;i <NBOARDS; i++)
1523 {
1524 str.str("");
1525 str << i;
1526 if (i<10) str << " ";
1527 if (i<100) str << " ";
1528 if (i<1000) str << " ";
1529 str << ": " << GLWindow->boardTime[i];
1530 boardsTimeList->addItem(QString(str.str().c_str()));
1531 if (boardsHistoMap.find(GLWindow->boardTime[i]) != boardsHistoMap.end())
1532 boardsHistoMap[GLWindow->boardTime[i]]++;
1533 else
1534 boardsHistoMap[GLWindow->boardTime[i]] = 1;
1535 }
1536 std::map<int, int> pixelHistoMap;
1537 for (int i=0;i <NPIX; i++)
1538 {
1539 str.str("");
1540 str << i;
1541 if (i<10) str << " ";
1542 if (i<100) str << " ";
1543 if (i<1000) str << " ";
1544 str << ": " << GLWindow->startPix[i];
1545 startPixelsList->addItem(QString(str.str().c_str()));
1546 if (pixelHistoMap.find(GLWindow->startPix[i]) != pixelHistoMap.end())
1547 pixelHistoMap[GLWindow->startPix[i]]++;
1548 else
1549 pixelHistoMap[GLWindow->startPix[i]] = 1;
1550 }
1551
1552 std::map<int, int> timeMarksMap;
1553 for (int i=0;i <NTMARK; i++)
1554 {
1555 str.str("");
1556 str << i;
1557 if (i<10) str << " ";
1558 if (i<100) str << " ";
1559 if (i<1000) str << " ";
1560 str << ": " << GLWindow->startTM[i];
1561 startTimeMarksList->addItem(QString(str.str().c_str()));
1562 if (timeMarksMap.find(GLWindow->startTM[i]) != timeMarksMap.end())
1563 timeMarksMap[GLWindow->startTM[i]]++;
1564 else
1565 timeMarksMap[GLWindow->startTM[i]] = 1;
1566 }
1567 std::map<int,int> delayMap;
1568 triggerDelayList->addItem(QString("Patch | Slice:Delay Slice:Delay..."));
1569 for (int i=0;i<NTMARK; i++)
1570 {
1571 str.str("");
1572 str << i << " | ";
1573 for (int j=0;j<GLWindow->nRoiTM;j++)
1574 {
1575 int value = GLWindow->eventData[1440*GLWindow->nRoi + i*GLWindow->nRoiTM + j];
1576 if (delayMap.find(value) != delayMap.end())
1577 delayMap[value]++;
1578 else
1579 delayMap[value] = 1;
1580 str << j << ":" << value << " ";
1581 }
1582 triggerDelayList->addItem(QString(str.str().c_str()));
1583 }
1584
1585 std::map<int,int>::iterator it = boardsHistoMap.begin();
1586 int nsamples = 0;
1587 int previousValue = it->first-10;
1588 for (unsigned int i=0;i<boardsHistoMap.size();i++)
1589 {
1590 if (previousValue != it->first-1)
1591 {
1592 xval[nsamples] = previousValue+1;
1593 yval[nsamples] = 0;
1594 nsamples++;
1595 xval[nsamples] = it->first-1;
1596 yval[nsamples] = 0;
1597 nsamples++;
1598 }
1599 xval[nsamples] = it->first;
1600 yval[nsamples] = it->second;
1601 previousValue = it->first;
1602 it++;
1603 nsamples++;
1604 xval[nsamples] = previousValue;
1605 yval[nsamples] = 0;
1606 nsamples++;
1607 if (nsamples > 4090)
1608 {
1609 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1610 break;
1611 }
1612 }
1613 xval[nsamples] = it==boardsHistoMap.begin() ? 0 : (--it)->first+1;
1614 yval[nsamples] = 0;
1615 nsamples++;
1616 // if (nsamples > 5)
1617#if QWT_VERSION < 0x060000
1618 boardsTimeHistoItem.setData(xval, yval, nsamples);
1619#else
1620 boardsTimeHistoItem.setSamples(xval, yval, nsamples);
1621#endif
1622
1623 it = pixelHistoMap.begin();
1624 nsamples = 0;
1625 previousValue = it->first-10;
1626 for (unsigned int i=0;i<pixelHistoMap.size();i++)
1627 {
1628 if (previousValue != it->first-1)
1629 {
1630 xval[nsamples] = previousValue+1;
1631 yval[nsamples] = 0;
1632 nsamples++;
1633 xval[nsamples] = it->first-1;
1634 yval[nsamples] = 0;
1635 nsamples++;
1636 }
1637 xval[nsamples] = it->first;
1638 yval[nsamples] = it->second;
1639 previousValue = it->first;
1640 it++;
1641 nsamples++;
1642 xval[nsamples] = previousValue;
1643 yval[nsamples] = 0;
1644 nsamples++;
1645 if (nsamples > 4090)
1646 {
1647 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1648 break;
1649 }
1650 }
1651 xval[nsamples] = it==pixelHistoMap.begin() ? 0 : (--it)->first+1;
1652 yval[nsamples] = 0;
1653 nsamples++;
1654// if (nsamples > 5)
1655#if QWT_VERSION < 0x060000
1656 startCellHistoItem.setData(xval, yval, nsamples);
1657#else
1658 startCellHistoItem.setSamples(xval, yval, nsamples);
1659#endif
1660
1661 it = timeMarksMap.begin();
1662 nsamples = 0;
1663 previousValue = it->first-10;
1664 for (unsigned int i=0;i<timeMarksMap.size();i++)
1665 {
1666 if (previousValue != it->first-1)
1667 {
1668 xval[nsamples] = previousValue+1;
1669 yval[nsamples] = 0;
1670 nsamples++;
1671 xval[nsamples] = it->first-1;
1672 yval[nsamples] = 0;
1673 nsamples++;
1674 }
1675 xval[nsamples] = it->first;
1676 yval[nsamples] = it->second;
1677 previousValue = it->first;
1678 it++;
1679 nsamples++;
1680 xval[nsamples] = previousValue;
1681 yval[nsamples] = 0;
1682 nsamples++;
1683 if (nsamples > 4090)
1684 {
1685 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1686 break;
1687 }
1688 }
1689 xval[nsamples] = it==timeMarksMap.begin() ? 0 : (--it)->first+1;
1690 yval[nsamples] = 0;
1691 nsamples++;
1692 // if (nsamples > 5)
1693#if QWT_VERSION < 0x060000
1694 startTimeMarkHistoItem.setData(xval, yval, nsamples);
1695#else
1696 startTimeMarkHistoItem.setSamples(xval, yval, nsamples);
1697#endif
1698
1699 it = delayMap.begin();
1700 nsamples = 0;
1701 previousValue = it->first-10;
1702 for (unsigned int i=0;i<delayMap.size();i++)
1703 {
1704 if (previousValue != it->first-1)
1705 {
1706 xval[nsamples] = previousValue+1;
1707 yval[nsamples] = 0;
1708 nsamples++;
1709 xval[nsamples] = it->first-1;
1710 yval[nsamples] = 0;
1711 nsamples++;
1712 }
1713 xval[nsamples] = it->first;
1714 yval[nsamples] = it->second;
1715 previousValue = it->first;
1716 it++;
1717 nsamples++;
1718 xval[nsamples] = previousValue;
1719 yval[nsamples] = 0;
1720 nsamples++;
1721 if (nsamples > 4090)
1722 {
1723 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1724 break;
1725 }
1726 }
1727 xval[nsamples] = it==delayMap.begin() ? 0 : (--it)->first+1;
1728 yval[nsamples] = 0;
1729 nsamples++;
1730 // if (nsamples > 5)
1731#if QWT_VERSION < 0x060000
1732 triggerDelayHistoItem.setData(xval, yval, nsamples);
1733#else
1734 triggerDelayHistoItem.setSamples(xval, yval, nsamples);
1735#endif
1736 //WAVELETS HACK
1737/* std::map<int, int> valuesHistoMap;
1738 std::map<int, int> waveletHistoMap;
1739 for (int i=0;i<1024*1440;i++)
1740 {
1741 if (valuesHistoMap.find(GLWindow->rawEventData[i]) != valuesHistoMap.end())
1742 valuesHistoMap[GLWindow->rawEventData[i]]++;
1743 else
1744 valuesHistoMap[GLWindow->rawEventData[i]] = 1;
1745 if (waveletHistoMap.find(GLWindow->waveLetArray[i]) != waveletHistoMap.end())
1746 waveletHistoMap[GLWindow->waveLetArray[i]]++;
1747 else
1748 waveletHistoMap[GLWindow->waveLetArray[i]] = 1;
1749 }
1750
1751 it = valuesHistoMap.begin();
1752 nsamples = 0;
1753 previousValue = it->first-10;
1754 cout << "Num values Original: " << valuesHistoMap.size() << endl;
1755 for (unsigned int i=0;i<valuesHistoMap.size();i++)
1756 {
1757 if (previousValue != it->first-1)
1758 {
1759 xval[nsamples] = previousValue+1;
1760 yval[nsamples] = 0;
1761 nsamples++;
1762 xval[nsamples] = it->first-1;
1763 yval[nsamples] = 0;
1764 nsamples++;
1765 }
1766 xval[nsamples] = it->first;
1767 yval[nsamples] = it->second;
1768 previousValue = it->first;
1769 it++;
1770 nsamples++;
1771 xval[nsamples] = previousValue;
1772 yval[nsamples] = 0;
1773 nsamples++;
1774 if (nsamples > 50000)
1775 {
1776 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1777 break;
1778 }
1779 }
1780 xval[nsamples] = it==valuesHistoMap.begin() ? 0 : (--it)->first+1;
1781 yval[nsamples] = 0;
1782 nsamples++;
1783 // if (nsamples > 5)
1784 #if QWT_VERSION < 0x060000
1785 triggerDelayHistoItem.setData(xval, yval, nsamples);
1786 #else
1787 triggerDelayHistoItem.setSamples(xval, yval, nsamples);
1788 #endif
1789
1790 it = waveletHistoMap.begin();
1791 nsamples = 0;
1792 previousValue = it->first-10;
1793 cout << "Num values WaveLets: " << waveletHistoMap.size() << endl;
1794 for (unsigned int i=0;i<waveletHistoMap.size();i++)
1795 {
1796 if (previousValue != it->first-1)
1797 {
1798 xval[nsamples] = previousValue+1;
1799 yval[nsamples] = 0;
1800 nsamples++;
1801 xval[nsamples] = it->first-1;
1802 yval[nsamples] = 0;
1803 nsamples++;
1804 }
1805 xval[nsamples] = it->first;
1806 yval[nsamples] = it->second;
1807 previousValue = it->first;
1808 it++;
1809 nsamples++;
1810 xval[nsamples] = previousValue;
1811 yval[nsamples] = 0;
1812 nsamples++;
1813 if (nsamples > 50000)
1814 {
1815 cout << "Error: Maximum number of samples reached for histograms. skipping what's remaining" << endl;
1816 break;
1817 }
1818 }
1819 xval[nsamples] = it==waveletHistoMap.begin() ? 0 : (--it)->first+1;
1820 yval[nsamples] = 0;
1821 nsamples++;
1822 // if (nsamples > 5)
1823 #if QWT_VERSION < 0x060000
1824 startTimeMarkHistoItem.setData(xval, yval, nsamples);
1825 #else
1826 startTimeMarkHistoItem.setSamples(xval, yval, nsamples);
1827 #endif
1828*/
1829//END OF WAVELETS HACK
1830 // startCellHistoZoom->setZoomBase(startCellHistoItem.boundingRect());
1831 QStack< QRectF > stack;
1832// QRectF cRectangle = boardsTimeHistoItem.boundingRect();
1833 stack.push(scaleBoundingRectangle(boardsTimeHistoItem.boundingRect(), 1.05f));//cRectangle);//boardsTimeHistoItem.boundingRect());
1834 boardsTimeHistoZoom->setZoomStack(stack);
1835 stack.pop();
1836 stack.push(scaleBoundingRectangle(startCellHistoItem.boundingRect(), 1.05f));
1837 startCellHistoZoom->setZoomStack(stack);
1838 stack.pop();
1839 stack.push(scaleBoundingRectangle(startTimeMarkHistoItem.boundingRect(), 1.05f));
1840 startTimeMarkHistoZoom->setZoomStack(stack);
1841 stack.pop();
1842 stack.push(scaleBoundingRectangle(triggerDelayHistoItem.boundingRect(), 1.05f));
1843 triggerDelayHistoZoom->setZoomStack(stack);
1844 stack.pop();
1845
1846 pixelChanged(GLWindow->selectedPixel);
1847}
1848//can't use a ref to rectangle, as the type must be converted first
1849QRectF UIConnector::scaleBoundingRectangle(QRectF rectangle, float scale)
1850{
1851 QPointF bottomRight = rectangle.bottomRight();
1852 QPointF topLeft = rectangle.topLeft();
1853 QPointF center = rectangle.center();
1854 return QRectF(topLeft + (topLeft-center)*(scale-1.0f), //top left
1855 bottomRight + (bottomRight-center)*(scale-1.0f)); //bottom right
1856}
1857void UIConnector::initHistograms()
1858{
1859// QwtPlot* boardsTimeHisto;
1860// QwtPlotHistogram boardsTimeHistoItem;
1861 grid1 = new QwtPlotGrid;
1862 grid1->enableX(false);
1863 grid1->enableY(true);
1864 grid1->enableXMin(false);
1865 grid1->enableYMin(false);
1866 grid1->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1867 grid1->attach(boardsTimeHisto);
1868
1869 grid2 = new QwtPlotGrid;
1870 grid2->enableX(false);
1871 grid2->enableY(true);
1872 grid2->enableXMin(false);
1873 grid2->enableYMin(false);
1874 grid2->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1875 grid2->attach(startCellsHisto);
1876
1877 grid3 = new QwtPlotGrid;
1878 grid3->enableX(false);
1879 grid3->enableY(true);
1880 grid3->enableXMin(false);
1881 grid3->enableYMin(false);
1882 grid3->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1883 grid3->attach(startTimeMarkHisto);
1884
1885 grid4 = new QwtPlotGrid;
1886 grid4->enableX(false);
1887 grid4->enableY(true);
1888 grid4->enableXMin(false);
1889 grid4->enableYMin(false);
1890 grid4->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1891 grid4->attach(pixelValueCurve);
1892
1893 grid6 = new QwtPlotGrid;
1894 grid6->enableX(false);
1895 grid6->enableY(true);
1896 grid6->enableXMin(false);
1897 grid6->enableYMin(false);
1898 grid6->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1899 grid6->attach(pixelAverageCurve);
1900
1901 grid5 = new QwtPlotGrid;
1902 grid5->enableX(false);
1903 grid5->enableY(true);
1904 grid5->enableXMin(false);
1905 grid5->enableYMin(false);
1906 grid5->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
1907 grid5->attach(triggerDelayHisto);
1908
1909 boardsTimeHisto->setAutoReplot(true);
1910 startCellsHisto->setAutoReplot(true);
1911 startTimeMarkHisto->setAutoReplot(true);
1912 pixelValueCurve->setAutoReplot(true);
1913 pixelAverageCurve->setAutoReplot(true);
1914 triggerDelayHisto->setAutoReplot(true);
1915 boardsTimeHisto->setTitle("Boards time values");
1916 startCellsHisto->setTitle("Start Cell values");
1917 startTimeMarkHisto->setTitle("Start Time Marks values");
1918 pixelValueCurve->setTitle("Current pixel values");
1919 pixelAverageCurve->setTitle("Average pixels values");
1920 triggerDelayHisto->setTitle("Trigger Delays");
1921
1922 // boardsTimeHistoItem.setBrush(QBrush(Qt::red));
1923// startCellHistoItem.setBrush(QBrush(Qt::red));
1924// startTimeMarkHistoItem.setBrush(QBrush(Qt::red));
1925// triggerDelayHistoItem.setBrush(QBrush(Qt::red));
1926// pixelValueCurveItem.setBrush(QBrush(Qt::red));
1927
1928 boardsTimeHistoItem.setPen(QColor(Qt::darkGreen));
1929 boardsTimeHistoItem.setStyle(QwtPlotCurve::Steps);
1930 startCellHistoItem.setPen(QColor(Qt::darkGreen));
1931 startCellHistoItem.setStyle(QwtPlotCurve::Steps);
1932 startTimeMarkHistoItem.setPen(QColor(Qt::darkGreen));
1933 startTimeMarkHistoItem.setStyle(QwtPlotCurve::Steps);
1934 triggerDelayHistoItem.setPen(QColor(Qt::darkGreen));
1935 triggerDelayHistoItem.setStyle(QwtPlotCurve::Steps);
1936
1937 boardsTimeHistoItem.attach(boardsTimeHisto);
1938 startCellHistoItem.attach(startCellsHisto);
1939 startTimeMarkHistoItem.attach(startTimeMarkHisto);
1940 triggerDelayHistoItem.attach(triggerDelayHisto);
1941
1942 //curve
1943// pixelValueCurveItem.setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush, QPen(Qt::black), QSize(5,5)));
1944 pixelValueCurveItem.setPen(QColor(Qt::black));
1945 pixelAverageCurveItem.setPen(QColor(Qt::black));
1946 aMeanCurveItem.setPen(QColor(Qt::darkGreen));
1947 vCorrCurveItem.setPen(QColor(Qt::red));
1948 meanCurveItem.setPen(QColor(Qt::blue));
1949 pixelValueCurveItem.setStyle(QwtPlotCurve::Lines);
1950 pixelAverageCurveItem.setStyle(QwtPlotCurve::Lines);
1951 aMeanCurveItem.setStyle(QwtPlotCurve::Lines);
1952 vCorrCurveItem.setStyle(QwtPlotCurve::Lines);
1953 meanCurveItem.setStyle(QwtPlotCurve::Lines);
1954
1955// pixelValueCurveItem.setCurveAttribute(QwtPlotCurve::Fitted);
1956 pixelValueCurveItem.attach(pixelValueCurve);
1957 pixelAverageCurveItem.attach(pixelAverageCurve);
1958// aMeanCurveItem.attach(pixelValueCurve);
1959 // vCorrCurveItem.attach(pixelValueCurve);
1960// meanCurveItem.attach(pixelValueCurve);
1961
1962 //FIXME delete these pointers with the destructor
1963 curveZoom = new QwtPlotZoomer(pixelValueCurve->canvas());
1964 curveZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1965 curveZoom->setTrackerPen(QPen(Qt::gray));
1966 averageCurveZoom = new QwtPlotZoomer(pixelAverageCurve->canvas());
1967 averageCurveZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1968 averageCurveZoom->setTrackerPen(QPen(Qt::gray));
1969
1970 boardsTimeHistoZoom = new QwtPlotZoomer(boardsTimeHisto->canvas());
1971 boardsTimeHistoZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1972 boardsTimeHistoZoom->setTrackerPen(QPen(Qt::gray));
1973
1974 startCellHistoZoom = new QwtPlotZoomer(startCellsHisto->canvas());
1975 startCellHistoZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1976 startCellHistoZoom->setTrackerPen(QPen(Qt::gray));
1977
1978 startTimeMarkHistoZoom = new QwtPlotZoomer(startTimeMarkHisto->canvas());
1979 startTimeMarkHistoZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1980 startTimeMarkHistoZoom->setTrackerPen(QPen(Qt::gray));
1981
1982 triggerDelayHistoZoom = new QwtPlotZoomer(triggerDelayHisto->canvas());
1983 triggerDelayHistoZoom->setRubberBandPen(QPen(Qt::gray, 2, Qt::DotLine));
1984 triggerDelayHistoZoom->setTrackerPen(QPen(Qt::gray));
1985
1986
1987}
1988
1989void UIConnector::pixelChanged(int pixel)
1990{
1991 RMS_window->fWhite = pixel;
1992 Mean_window->fWhite = pixel;
1993 Max_window->fWhite = pixel;
1994 PosOfMax_window->fWhite = pixel;
1995 if (pixel != -1)
1996 {
1997 RMS_window->fWhitePatch = RMS_window->pixelsPatch[pixel];
1998 Mean_window->fWhitePatch = Mean_window->pixelsPatch[pixel];
1999 Max_window->fWhitePatch = Max_window->pixelsPatch[pixel];
2000 PosOfMax_window->fWhitePatch = PosOfMax_window->pixelsPatch[pixel];
2001 }
2002 else
2003 {
2004 RMS_window->fWhitePatch = -1;
2005 Mean_window->fWhitePatch = -1;
2006 Max_window->fWhitePatch = -1;
2007 PosOfMax_window->fWhitePatch = -1;
2008 }
2009 if (pixel == -1)
2010 return;
2011 int softwarePix = pixel;
2012// if (!GLWindow->_softwareOrdering)
2013 pixel = GLWindow->hardwareMapping[pixel];
2014
2015 HwIDBox->setValue(pixel);
2016
2017 if (!GLWindow->nRoi)
2018 return;
2019
2020int currentPixel = pixel;
2021
2022 for (int i=0;i<GLWindow->nRoi;i++)
2023 {
2024 xval[i] = i;
2025 yval[i] = GLWindow->eventData[GLWindow->nRoi*currentPixel + i];
2026 }
2027
2028int realNumSamples = GLWindow->nRoi;
2029 if (GLWindow->nRoiTM != 0)
2030 {
2031 const PixelMapEntry& mapEntry = GLWindow->fPixelMap.index(softwarePix);
2032 const int pixelIdInPatch = mapEntry.pixel();
2033 const int patchId = mapEntry.patch();
2034 const int boardId = mapEntry.board();
2035 const int crateId = mapEntry.crate();
2036 if (pixelIdInPatch == 8)
2037 {
2038 int TMIndex = 0;
2039 int xIndex = GLWindow->nRoi;
2040 int arrayIndex = GLWindow->nRoi;
2041 if (GLWindow->offSetRoi < 0)
2042 TMIndex -= GLWindow->offSetRoi;
2043 if (GLWindow->offSetRoi > 0)
2044 xIndex += GLWindow->offSetRoi;
2045 for (int i=TMIndex;i<GLWindow->nRoiTM;i++, xIndex++, arrayIndex++)
2046 {
2047 xval[arrayIndex] = xIndex;
2048 yval[arrayIndex] = GLWindow->eventData[GLWindow->nRoi*1440 + GLWindow->nRoiTM*(40*crateId + 4*boardId + patchId) + i];
2049 }
2050 realNumSamples += GLWindow->nRoiTM - TMIndex;
2051 }
2052 // cout << pixelIdInPatch << " " ;
2053 }
2054
2055#if QWT_VERSION < 0x060000
2056 pixelValueCurveItem.setData(xval, yval, realNumSamples);
2057#else
2058 pixelValueCurveItem.setSamples(xval, yval, realNumSamples);
2059#endif
2060
2061//now compute the average value of all pixels
2062 currentPixel = 0;
2063 for (int i=0;i<GLWindow->nRoi;i++)
2064 yval[i] = 0;
2065 for (int j=0;j<1440;j++) {
2066 currentPixel = j;
2067 for (int i=0;i<GLWindow->nRoi;i++)
2068 {
2069 xval[i] = i;
2070 yval[i] += GLWindow->eventData[GLWindow->nRoi*currentPixel + i];
2071 }
2072 }
2073 for (int i=0;i<GLWindow->nRoi;i++)
2074 yval[i] /= 1440;
2075#if QWT_VERSION < 0x060000
2076 pixelAverageCurveItem.setData(xval, yval, GLWindow->nRoi);
2077#else
2078 pixelAverageCurveItem.setSamples(xval, yval, realNumSamples);
2079#endif
2080
2081 QStack< QRectF > stack;
2082 stack.push(scaleBoundingRectangle(pixelValueCurveItem.boundingRect(), 1.5f));
2083 curveZoom->setZoomBase(scaleBoundingRectangle(pixelValueCurveItem.boundingRect(), 1.5f));
2084 curveZoom->setZoomStack(stack);
2085 stack.pop();
2086 stack.push(scaleBoundingRectangle(pixelAverageCurveItem.boundingRect(), 1.5f));
2087 averageCurveZoom->setZoomBase(scaleBoundingRectangle(pixelAverageCurveItem.boundingRect(), 1.5f));
2088 averageCurveZoom->setZoomStack(stack);
2089 stack.pop();
2090
2091 displaySliceValue();
2092 on_autoScaleColor_clicked();
2093}
2094
2095void UIConnector::on_HwIDBox_valueChanged(int)
2096{
2097 updating = true;
2098
2099 const int hwID = HwIDBox->value();
2100
2101 const int crateID = hwID/360;
2102 const int boardID = (hwID%360)/36;
2103 const int patchID = (hwID%36 )/9;
2104 const int pixelID = hwID%9;
2105
2106 SwIDBox->setValue(GLWindow->softwareMapping[hwID]);
2107
2108 crateIDBox->setValue(crateID);
2109 boardIDBox->setValue(boardID);
2110 patchIDBox->setValue(patchID);
2111 pixelIDBox->setValue(pixelID);
2112
2113 updating = false;
2114
2115 GLWindow->selectedPixel = GLWindow->softwareMapping[hwID];
2116 GLWindow->updateGL();
2117
2118 pixelChanged(GLWindow->selectedPixel);
2119}
2120
2121void UIConnector::cbpxChanged()
2122{
2123 if (updating)
2124 return;
2125
2126 const int hwid = crateIDBox->value()*360 + boardIDBox->value()*36 + patchIDBox->value()*9 + pixelIDBox->value();
2127 HwIDBox->setValue(hwid);
2128}
2129
2130void UIConnector::on_SwIDBox_valueChanged(int swid)
2131{
2132 if (updating)
2133 return;
2134
2135// if (GLWindow->_softwareOrdering)
2136// HwIDBox->setValue(swid);
2137// else
2138 HwIDBox->setValue(GLWindow->hardwareMapping[swid]);
2139}
2140
2141void UIConnector::on_autoScaleColor_clicked()
2142{
2143 if (!autoScaleColor->isChecked())
2144 {
2145 GLWindow->ss[0] = 0.496;
2146 GLWindow->ss[1] = 0.507;
2147 GLWindow->ss[2] = 0.518;
2148 GLWindow->ss[3] = 0.529;
2149 GLWindow->ss[4] = 0.540;;
2150 colorRange0->setValue(GLWindow->ss[0]);
2151 colorRange1->setValue(GLWindow->ss[1]);
2152 colorRange2->setValue(GLWindow->ss[2]);
2153 colorRange3->setValue(GLWindow->ss[3]);
2154 colorRange4->setValue(GLWindow->ss[4]);
2155 return;
2156 }
2157 if (!GLWindow->nRoi)
2158 return;
2159
2160 int start = 0;
2161 int end = 1440;
2162
2163 if (!entireCameraScale->isChecked())
2164 {
2165 start = GLWindow->selectedPixel;
2166 end = GLWindow->selectedPixel+1;
2167 if (end == 0)
2168 {
2169 start = 0;
2170 end = 1440;
2171 }
2172 }
2173
2174 int min = 100000; //real min = -2048, int_16 = -32768 to 32767
2175 int max = -100000; //real max = 2047
2176
2177 long average = 0;
2178 long numSamples = 0;
2179 int errorDetected = -1;
2180
2181 for (int i=start;i<end;i++)
2182 {
2183 if (i==863)//keep crazy pixel out of the autoscale
2184 continue;
2185 for (int j=10;j<GLWindow->nRoi-50;j++)
2186 {
2187 int cValue = GLWindow->eventData[i*GLWindow->nRoi+j];
2188 if (cValue > max && cValue < 32767)
2189 max = cValue;
2190 if (cValue < min && cValue > -32768)
2191 min = cValue;
2192 if (cValue < 32767 && cValue > -32768)
2193 {
2194 average+=cValue;
2195 numSamples++;
2196 }
2197 else
2198 {
2199 errorDetected = i;
2200 }
2201// numSamples++;
2202 }
2203 }
2204 average /= numSamples;
2205 if (errorDetected != -1)
2206 {
2207 cout << "Overflow detected at pixel " << errorDetected << " (at least)" << endl;
2208 }
2209// cout << "min: " << min << " max: " << max << " average: " << average << endl;
2210 float minRange = (float)(min+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2211 float maxRange = (float)(max+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2212 float midRange = (float)(average+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2213 if (GLWindow->logScale)
2214 {
2215 minRange *= 9;
2216 maxRange *= 9;
2217// midRange *= 9;
2218 minRange += 1;
2219 maxRange += 1;
2220// midRange += 1;
2221 minRange = log10(minRange);
2222 maxRange = log10(maxRange);
2223// midRange = (minRange + maxRange)/2.f;
2224 midRange = log10(midRange);
2225 }
2226
2227 GLWindow->ss[0] = minRange;
2228 colorRange0->setValue(GLWindow->ss[0]);
2229 GLWindow->ss[4] = maxRange;
2230 colorRange4->setValue(GLWindow->ss[4]);
2231// GLWindow->ss[2] = midRange;
2232// range2->setValue(GLWindow->ss[2]);
2233// GLWindow->ss[1] = (minRange+midRange)/2;
2234// range1->setValue(GLWindow->ss[1]);
2235// GLWindow->ss[3] = (maxRange+midRange)/2;
2236// range3->setValue(GLWindow->ss[3]);
2237
2238 GLWindow->ss[2] = (maxRange+minRange)/2;
2239 colorRange2->setValue(GLWindow->ss[2]);
2240
2241 GLWindow->ss[1] = minRange+(maxRange-minRange)/4;
2242 colorRange1->setValue(GLWindow->ss[1]);
2243
2244 GLWindow->ss[3] = minRange+3*(maxRange-minRange)/4;
2245 colorRange3->setValue(GLWindow->ss[3]);
2246}
2247
2248void PrintUsage()
2249{
2250 cout << "\n"
2251 "The FACT++ raw data viewer.\n"
2252 "\n"
2253 "Usage: viewer [OPTIONS] [datafile.fits[.gz|.fz] [calibration.drs.fits[.gz]]]\n"
2254 " or: viewer [OPTIONS]\n";
2255 cout << endl;
2256
2257}
2258
2259void PrintHelp()
2260{
2261 cout <<
2262 "\n"
2263 << endl;
2264}
2265
2266int UIConnector::SetupConfiguration(Configuration &conf)
2267{
2268 RawDataViewer *canvas = GLWindow;
2269
2270 if (conf.Has("mappingFile"))
2271 {
2272 canvas->assignPixelMapFile(conf.Get<string>("mappingFile"));
2273 }
2274 else
2275 canvas->assignPixelMapFile("");
2276
2277 if (!canvas->isFACT())
2278 {
2279 HwIDBox->setMaximum(71);
2280 SwIDBox->setMaximum(71);
2281 SwIDBox->setValue(0);
2282 HwIDBox->setValue(19);
2283 crateIDBox->setMaximum(0);
2284 crateIDBox->setEnabled(false);
2285 boardIDBox->setMaximum(1);
2286 }
2287
2288 if (conf.Has("color.range"))
2289 {
2290 vector<double> value = conf.Vec<double>("color.range");
2291 if (value.size() != 5)
2292 {
2293 cout << "Error, colorRange option should have exactly 5 double values" << endl;
2294 return -1;
2295 }
2296 for (int i=0;i<5;i++)
2297 canvas->ss[i] = value[i];
2298 }
2299
2300 if (conf.Has("color.red"))
2301 {
2302 vector<double> value = conf.Vec<double>("color.red");
2303 if (value.size() != 5)
2304 {
2305 cout << "Error, colorRed option should have exactly 5 double values" << endl;
2306 return -1;
2307 }
2308 for (int i=0;i<5;i++)
2309 canvas->rr[i] = value[i];
2310 }
2311
2312 if (conf.Has("color.green"))
2313 {
2314 vector<double> value = conf.Vec<double>("color.green");
2315 if (value.size() != 5)
2316 {
2317 cout << "Error, colorGreen option should have exactly 5 double values" << endl;
2318 return -1;
2319 }
2320 for (int i=0;i<5;i++)
2321 canvas->gg[i] = value[i];
2322 }
2323
2324 if (conf.Has("color.blue"))
2325 {
2326 vector<double> value = conf.Vec<double>("color.blue");
2327 if (value.size() != 5)
2328 {
2329 cout << "Error, colorBlue option should have exactly 5 double values" << endl;
2330 return -1;
2331 }
2332 for (int i=0;i<5;i++)
2333 canvas->bb[i] = value[i];
2334 }
2335
2336 colorRange0->setValue(canvas->ss[0]);
2337 colorRange1->setValue(canvas->ss[1]);
2338 colorRange2->setValue(canvas->ss[2]);
2339 colorRange3->setValue(canvas->ss[3]);
2340 colorRange4->setValue(canvas->ss[4]);
2341 redValue0->setValue(canvas->rr[0]);
2342 redValue1->setValue(canvas->rr[1]);
2343 redValue2->setValue(canvas->rr[2]);
2344 redValue3->setValue(canvas->rr[3]);
2345 redValue4->setValue(canvas->rr[4]);
2346 greenValue0->setValue(canvas->gg[0]);
2347 greenValue1->setValue(canvas->gg[1]);
2348 greenValue2->setValue(canvas->gg[2]);
2349 greenValue3->setValue(canvas->gg[3]);
2350 greenValue4->setValue(canvas->gg[4]);
2351 blueValue0->setValue(canvas->bb[0]);
2352 blueValue1->setValue(canvas->bb[1]);
2353 blueValue2->setValue(canvas->bb[2]);
2354 blueValue3->setValue(canvas->bb[3]);
2355 blueValue4->setValue(canvas->bb[4]);
2356
2357 if (conf.Has("drs"))
2358 {
2359 const QString qstr(conf.Get<string>("drs").c_str());
2360 calibFileSelected(qstr);
2361 }
2362
2363 if (conf.Has("file"))
2364 {
2365 const QString qstr(conf.Get<string>("file").c_str());
2366 fileSelected(qstr);
2367 }
2368
2369
2370 return 0;
2371}
2372
2373void SetupConfiguration(Configuration& conf)
2374{
2375 po::options_description configs("Raw Events Viewer Options");
2376 configs.add_options()
2377 ("color.range", vars<double>(), "Range of the display colours")
2378 ("color.red", vars<double>(), "Range of red values")
2379 ("color.green", vars<double>(), "Range of green values")
2380 ("color.blue", vars<double>(), "Range of blue values")
2381 ("file,f", var<string>(), "File to be loaded")
2382 ("drs,d", var<string>(), "DRS calibration file to be loaded")
2383 ("mappingFile", var<string>(), "Which pixels mapping file to use")
2384 ;
2385 conf.AddOptions(configs);
2386
2387 po::positional_options_description p;
2388 p.add("file", 1); // The first positional options
2389 p.add("drs", 2); // The first positional options
2390 conf.SetArgumentPositions(p);
2391
2392}
2393
2394/************************************************************
2395 * MAIN PROGRAM FUNCTION.
2396 ************************************************************/
2397int main(int argc, const char *argv[])
2398{
2399 QApplication app(argc, const_cast<char**>(argv));
2400
2401 if (!QGLFormat::hasOpenGL()) {
2402 std::cerr << "This system has no OpenGL support" << std::endl;
2403 return 1;
2404 }
2405
2406 Configuration conf(argv[0]);
2407 conf.SetPrintUsage(PrintUsage);
2408 SetupConfiguration(conf);
2409 if (!conf.DoParse(argc, argv, PrintHelp))
2410 return 2;
2411
2412 UIConnector myUi;
2413 if (myUi.SetupConfiguration(conf)<0)
2414 return 3;
2415
2416 return app.exec();
2417}
2418
Note: See TracBrowser for help on using the repository browser.