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

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