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

Last change on this file since 19311 was 19300, checked in by tbretz, 6 years ago
Disable some compiler warning for a QWT header.
File size: 76.7 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#include "viewer.h"
16
17#include <QFileDialog>
18#include <QMouseEvent>
19
20#include <qwt_symbol.h>
21#include <qwt_plot_grid.h>
22#pragma GCC diagnostic push
23#pragma GCC diagnostic ignored "-Woverloaded-virtual"
24#include <qwt_plot_zoomer.h>
25#pragma GCC diagnostic pop
26
27#include "src/Configuration.h"
28#include "externals/factfits.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
52 first = neighbors[pixel][first];
53 second = neighbors[pixel][second];
54// cout << pixel << " " << vertex << " " << "first: " << first << " second: " << second << endl;
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
73// cout << color[0] << " " << color[1] << " " << color[2] << endl;
74
75 glColor3fv(color);
76}
77void RawDataViewer::calcMidBlurColor(int pixel, int vertex)
78{
79 GLfloat color[3];
80 int first;
81 first = vertex-1;
82 if (first < 0)
83 first = 5;
84 first = neighbors[pixel][first];
85 for (int i=0;i<3;i++)
86 color[i] = pixelsColor[pixel][i];
87 float divide = 1;
88 if (first != -1)
89 {
90 divide++;
91 for (int i=0;i<3;i++)
92 color[i] += pixelsColor[first][i];
93 }
94 for (int i=0;i<3;i++)
95 color[i] /= divide;
96 glColor3fv(color);
97}
98/************************************************************
99 * DRAW BLURRY HEXAGON. draws a solid hexagon, with interpolated colours
100 ************************************************************/
101void RawDataViewer::drawBlurryHexagon(int index)
102{
103
104//per-pixel mesh
105 GLfloat color[3];
106 for (int i=0;i<3;i++)
107 color[i] = pixelsColor[index][i];
108 glBegin(GL_TRIANGLES);
109 calcBlurColor(index, 0);
110 glVertex2fv(verticesList[verticesIndices[index][0]]);
111 glColor3fv(color);
112 glVertex2fv(pixelsCoords[index]);
113
114 calcBlurColor(index, 1);
115 glVertex2fv(verticesList[verticesIndices[index][1]]);
116
117 glVertex2fv(verticesList[verticesIndices[index][1]]);
118 glColor3fv(color);
119 glVertex2fv(pixelsCoords[index]);
120
121 calcBlurColor(index, 2);
122 glVertex2fv(verticesList[verticesIndices[index][2]]);
123
124 glVertex2fv(verticesList[verticesIndices[index][2]]);
125 glColor3fv(color);
126 glVertex2fv(pixelsCoords[index]);
127
128 calcBlurColor(index, 3);
129 glVertex2fv(verticesList[verticesIndices[index][3]]);
130
131 glVertex2fv(verticesList[verticesIndices[index][3]]);
132 glColor3fv(color);
133 glVertex2fv(pixelsCoords[index]);
134
135 calcBlurColor(index, 4);
136 glVertex2fv(verticesList[verticesIndices[index][4]]);
137
138 glVertex2fv(verticesList[verticesIndices[index][4]]);
139 glColor3fv(color);
140 glVertex2fv(pixelsCoords[index]);
141
142 calcBlurColor(index, 5);
143 glVertex2fv(verticesList[verticesIndices[index][5]]);
144
145 glVertex2fv(verticesList[verticesIndices[index][5]]);
146 glColor3fv(color);
147 glVertex2fv(pixelsCoords[index]);
148
149 calcBlurColor(index, 0);
150 glVertex2fv(verticesList[verticesIndices[index][0]]);
151 glEnd();
152
153 return;
154}
155
156/************************************************************
157 * DRAW CAMERA draws all the camera pixels
158 ************************************************************/
159void RawDataViewer::drawCamera(bool alsoWire)
160{
161 glLoadIdentity();
162 if (!drawImpulse)
163 {
164 glTranslatef(0,-0.44,0);
165 glRotatef(cameraRotation, 0,0,-1);
166 if (cameraRotation == 90)
167 {
168 glTranslatef(-0.45,-0.45,0);
169 }
170 if (cameraRotation == -90)
171 {
172 glTranslatef(0.45,-0.45,0);
173 }
174 glScalef(1.5,1.5,1);
175 }
176 else
177 {
178 glRotatef(cameraRotation, 0,0,-1);
179 if (cameraRotation == 90)
180 {
181 glTranslatef(-0.45/1.5,-0.45/1.5,0);
182 }
183 if (cameraRotation == -90)
184 {
185 glTranslatef(0.45/1.5,-0.45/1.5,0);
186 }
187 }
188 glColor3f(0.5,0.5,0.5);
189 glLineWidth(1.0);
190 float color;
191
192 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
193 {
194 if (!nRoi)
195 color = (float)(i)/(float)(ACTUAL_NUM_PIXELS);
196 else
197
198// if (_softwareOrdering)
199// color = float(eventData[nRoi*i + whichSlice] + (VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
200// else
201 color = float(eventData[nRoi*hardwareMapping[i] + whichSlice]+(VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
202 if (logScale)
203 {
204 color *= 9;
205 color += 1;
206 color = log10(color);
207 }
208
209 if (color < ss[0])
210 {
211 pixelsColor[i][0] = tooLowValueCoulour[0];
212 pixelsColor[i][1] = tooLowValueCoulour[1];
213 pixelsColor[i][2] = tooLowValueCoulour[2];
214 continue;
215 }
216 if (color > ss[4])
217 {
218 pixelsColor[i][0] = tooHighValueCoulour[0];
219 pixelsColor[i][1] = tooHighValueCoulour[1];
220 pixelsColor[i][2] = tooHighValueCoulour[2];
221 continue;
222 }
223 int index = 0;
224 while (ss[index] < color && index < 4)
225 index++;
226 index--;
227 if (index < 0) index = 0;
228 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
229 if (weight0 > 1.0f) weight0 = 1.0f;
230 if (weight0 < 0.0f) weight0 = 0.0f;
231 float weight1 = 1.0f-weight0;
232 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
233 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
234 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
235 }
236
237 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
238 {
239
240 glColor3fv(pixelsColor[i]);
241 glLoadName(i);
242if (drawBlur)
243 drawBlurryHexagon(i);
244else
245 drawHexagon(i,true);
246
247 }
248 if (!alsoWire)
249 return;
250 glTranslatef(0,0,0.1f);
251 glColor3f(0.0f,0.0f,0.0f);
252 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
253 {
254
255 drawHexagon(i, false);
256 }
257
258}
259
260/************************************************************
261 * DRAW PIXEL CURVE. draws the raw impulse curve of the currently selected pixel
262 ************************************************************/
263void RawDataViewer::drawPixelCurve()
264{
265 float xRange = bboxMax[0] - bboxMin[0];
266 float yRange = bboxMax[1] - bboxMin[1];
267
268 glBegin(GL_LINES);
269 glLineWidth(1.0f);
270 glColor3f(0.0,0.0,0.0);
271 glVertex2f(bboxMin[0], bboxMin[1]);
272 glVertex2f(bboxMax[0], bboxMin[1]);
273 glVertex2f(bboxMin[0], bboxMin[1]);
274 glVertex2f(bboxMin[0], bboxMax[1]);
275 glVertex2f(bboxMin[0], (bboxMin[1]+bboxMax[1])/2.0f);
276 glVertex2f(bboxMax[0], (bboxMin[1]+bboxMax[1])/2.0f);
277 glVertex2f(bboxMin[0] + xRange*nRoi/(float)(nRoi+nRoiTM),
278 bboxMin[1]);
279 glVertex2f(bboxMin[0] + xRange*nRoi/(float)(nRoi+nRoiTM),
280 bboxMax[1]);
281 glEnd();
282 glTranslatef(0,0,0.1f);
283 if (!nRoi)
284 return;
285 glBegin(GL_LINES);
286 glColor3f(1.0f,1.0f,0.0f);
287 float divideMe = (float)(VALUES_SPAN-1);
288 float plusMe = (float)(VALUES_SPAN)/2;
289 if (divideMe <= 0)
290 divideMe = 1;
291
292 /*
293 if (drawCalibrationLoaded)
294 plusMe += 0;//VALUES_SPAN/2;
295 if (drawCalibrationLoaded && calibrationLoaded)
296 {
297 divideMe /=2;
298 plusMe = 0 ;///=2;
299 }*/
300
301// int mapping = _softwareOrdering ? selectedPixel : hardwareMapping[selectedPixel];
302 int mapping = hardwareMapping[selectedPixel];
303 const int hw = mapping;
304 const PixelMapEntry& mapEntry = fPixelMap.index(selectedPixel);
305 const int pixelIdInPatch = mapEntry.pixel();
306 const int patchId = mapEntry.patch();
307 const int boardId = mapEntry.board();
308 const int crateId = mapEntry.crate();
309
310 if (selectedPixel != -1)
311 {
312 for (int i=0;i<nRoi-1;i++)
313 {
314 float d1 = eventData[nRoi*hw + i]+plusMe;
315 float d2 = eventData[nRoi*hw + i+1]+plusMe;
316 if (!finite(d1)) d1 = 20000;
317 if (!finite(d2)) d2 = 20000;
318 glVertex2f(bboxMin[0] + xRange*i/(float)(nRoi+nRoiTM),
319 bboxMin[1] + yRange*(d1) /divideMe);
320 glVertex2f(bboxMin[0] + xRange*(i+1)/(float)(nRoi+nRoiTM),
321 bboxMin[1] + yRange*(d2) /divideMe);
322 }
323 glEnd();
324
325 glColor3f(0.0f, 1.0f, 1.0f);
326 glBegin(GL_LINES);
327 if (pixelIdInPatch == 8)//this channel has a time marker
328 {
329
330 for (int i=0;i<nRoiTM-1;i++)
331 {
332 float d1 = eventData[nRoi*1440 + nRoiTM*(40*crateId + 4*boardId + patchId) + i] + plusMe;
333 float d2 = eventData[nRoi*1440 + nRoiTM*(40*crateId + 4*boardId + patchId) + i+1] + plusMe;
334 if (!finite(d1)) d1 = 20000;
335 if (!finite(d2)) d2 = 20000;
336 glVertex2f(bboxMin[0] + xRange*(i+nRoi)/(float)(nRoi+nRoiTM),
337 bboxMin[1] + yRange*(d1)/divideMe);
338 glVertex2f(bboxMin[0] + xRange*(i+1+nRoi)/(float)(nRoi+nRoiTM),
339 bboxMin[1] + yRange*(d2) / divideMe);
340 }
341 }
342
343 }
344 glEnd();
345 glTranslatef(0,0,0.1f);
346 glBegin(GL_LINES);
347 glColor3f(1.0,0.0,0.0);
348 glVertex2f(bboxMin[0] + xRange*whichSlice/(float)(nRoi+nRoiTM),
349 bboxMin[1]);
350 glVertex2f(bboxMin[0] + xRange*whichSlice/(float)(nRoi+nRoiTM),
351 bboxMax[1]);
352
353 glEnd();
354
355}
356/************************************************************
357 * CONSTRUCTOR.
358 ************************************************************/
359RawDataViewer::RawDataViewer(QWidget *cParent) : BasicGlCamera(cParent), RMSvalues(1440), Meanvalues(1440), Maxvalues(1440), PosOfMaxvalues(1440), VALUES_SPAN(4096)
360
361{
362
363 whichSlice = 0;
364
365 nRoi = 0;
366 nRoiTM = 0;
367 offSetRoi = 0;
368 eventNum = 0;
369 rowNum = -1;
370 eventStep = 1;
371 selectedPixel = 393;
372 inputFile = NULL;
373 eventData = NULL;
374 drawPatch = false;
375 drawImpulse = true;
376 drawBlur = false;
377 loopCurrentEvent = false;
378 fIsDrsCalibration = false;
379 SetAutoRefresh(true);
380 runType = "unkown";
381
382
383}
384
385void RawDataViewer::assignPixelMapFile(const string& map)
386{
387 PixelMap mypMap;
388 if (map.empty())
389 {
390 if (!mypMap.Read("FACTmap111030.txt"))
391 {
392 if (!mypMap.Read("/swdev_nfs/FACT++/FACTmap111030.txt"))
393 {
394 if (!mypMap.Read("./FACTmap111030.txt"))
395 {
396 cerr << "ERROR - Problems reading FACTmap111030.txt" << endl;
397 exit(-1);
398 }
399 }
400 }
401 }
402 else
403 {
404 if (!mypMap.Read(map))
405 {
406 cerr << "ERROR - Problems reading mapping file '" << map << "'" << endl;
407 exit(-1);
408 }
409 }
410
411 assignPixelMap(mypMap);
412
413 for (int i=0;i<160;i++)
414 {
415 const float color[3] = { 0.5, 0.5, 0.3 };
416
417 for (int j=0;j<3;j++)
418 patchesColor[i][j] = color[j];
419 }
420 fZeroArray = NULL;
421
422 _softwareOrdering = false;
423}
424/************************************************************
425 * DESTRUCTOR
426 ************************************************************/
427RawDataViewer::~RawDataViewer()
428{
429 if (inputFile != NULL)
430 {
431 inputFile->close();
432 delete inputFile;
433 }
434 if (eventData != NULL) {
435 delete[] eventData;
436 delete[] rawEventData;
437 delete[] waveLetArray;
438 }
439 if (fZeroArray != NULL)
440 delete[] fZeroArray;
441}
442void RawDataViewer::allocateZeroArray()
443{
444 if (fZeroArray == NULL)
445 {
446 fZeroArray = new char[8192];
447 }
448}
449/************************************************************
450 * PAINT GL. main drawing function.
451 ************************************************************/
452void RawDataViewer::paintGL()
453{
454 //Should not be required, but apparently it helps when piping it through X forwarding
455 glFinish();
456 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
457 glLoadIdentity();
458
459 glTranslatef(0,0,-0.5);
460
461 if (drawBlur)
462 {
463 glShadeModel(GL_SMOOTH);
464 drawCamera(false);
465 }
466 else
467 {
468 glShadeModel(GL_FLAT);
469 drawCamera(true);
470 }
471 glTranslatef(0,0,0.1f);
472 if (drawPatch)
473 drawPatches();
474 glTranslatef(0,0,0.1f);
475 if (!drawBlur && (selectedPixel != -1))
476 {
477 glLineWidth(1.0f);
478 glColor3f(1.0,1.0,1.0);
479 drawHexagon(selectedPixel, false);
480 }
481 glTranslatef(0,0,0.1f);
482 if (drawImpulse)
483 {
484 // glRotatef(cameraRotation, 0,0,1);
485 glLoadIdentity();
486 glLineWidth(2.0);
487 drawPixelCurve();
488 }
489 glTranslatef(0,0,0.1f);
490 DrawScale();
491}
492
493/************************************************************
494 * MOUSE PRESS EVENT. mouse click handler.
495 ************************************************************/
496void RawDataViewer::mousePressEvent(QMouseEvent *cEvent)
497{
498 if (cEvent->pos().x() > width()-(width()/50.f))
499 {
500 toggleInterfaceDisplay();
501 return;
502 }
503 lastPos = cEvent->pos();
504 if (setCorrectSlice(cEvent))
505 return;
506 int face = PixelAtPosition(cEvent->pos());
507
508 selectedPixel = face;
509 emit signalCurrentPixel(face);
510
511 updateGL();
512}
513
514/************************************************************
515 * SET CORRECT SLICE. if displayed, figures out if the graph was
516 * clicked, and if so, which slice should be displayed
517 ************************************************************/
518bool RawDataViewer::setCorrectSlice(QMouseEvent* cEvent)
519{
520 if (!drawImpulse)
521 return false;
522 float cx = (float)cEvent->x() * pixelSize - shownSizex/2;
523 float cy = ((float)height()-(float)cEvent->y())*pixelSize - shownSizey/2;
524 if (cx < bboxMin[0] ||
525 cx > bboxMax[0] ||
526 cy < bboxMin[1] ||
527 cy > bboxMax[1])
528 return false;
529 whichSlice = (cx - bboxMin[0])*(nRoi+nRoiTM)/(bboxMax[0] - bboxMin[0]);
530 if (whichSlice >= nRoi)
531 whichSlice = nRoi-1;
532 emit signalCurrentSlice(whichSlice);
533 return true;
534}
535
536/************************************************************
537 * MOUSE MOVE EVENT. used to track the dragging of slices display
538 ************************************************************/
539void RawDataViewer::mouseMoveEvent(QMouseEvent *cEvent)
540{
541 if (cEvent->buttons() & Qt::LeftButton) {
542 setCorrectSlice(cEvent);
543 updateGL();
544 } else if (cEvent->buttons() & Qt::RightButton) {
545 updateGL();
546 }
547 lastPos = cEvent->pos();
548}
549
550/************************************************************
551 * MOUSE DOUBLE CLICK EVENT. used to select pixels
552 ************************************************************/
553void RawDataViewer::mouseDoubleClickEvent(QMouseEvent *cEvent)
554{
555 int face = PixelAtPosition(cEvent->pos());
556 if (face != -1) {
557 selectedPixel = face;
558 emit signalCurrentPixel(face);
559 updateGL();
560 }
561}
562
563/************************************************************
564 * OPEN FILE. opens a new fits file
565 ************************************************************/
566void RawDataViewer::openFile(string& file)
567{
568 if (inputFile)
569 {
570 inputFile->close();
571 delete inputFile;
572 }
573 try {
574 inputFile = new factfits(file, "Events");
575 }
576 catch (std::runtime_error e)
577 {
578 cout << "Something went wrong while loading fits. Aborting: " << e.what() << endl;
579 return;
580 }
581 if (!*inputFile)
582 {
583 delete inputFile;
584 inputFile = NULL;
585 return;
586 }
587 vector<string> entriesToCheck;
588 if (inputFile->IsCompressedFITS())
589 entriesToCheck.push_back("ZNAXIS2");
590 else
591 entriesToCheck.push_back("NAXIS2");
592 entriesToCheck.push_back("NROI");
593 entriesToCheck.push_back("REVISION");
594 entriesToCheck.push_back("RUNID");
595 entriesToCheck.push_back("NBOARD");
596 entriesToCheck.push_back("NPIX");
597 entriesToCheck.push_back("NROITM");
598 entriesToCheck.push_back("TIMESYS");
599 entriesToCheck.push_back("DATE");
600 entriesToCheck.push_back("NIGHT");
601 entriesToCheck.push_back("CAMERA");
602 entriesToCheck.push_back("DAQ");
603 entriesToCheck.push_back("TSTART");
604 entriesToCheck.push_back("TSTOP");
605
606
607 for (vector<string>::const_iterator it=entriesToCheck.begin(); it != entriesToCheck.end(); it++)
608 {
609 try {
610 if (!inputFile->HasKey(*it)){
611 cout << "Warning: header keyword " << *it << " missing." << endl;
612 }
613 }
614 catch (std::runtime_error e)
615 {
616 cout << e.what() << endl;
617 return;
618 }
619 }
620
621 nRows = 0;
622 if (inputFile->IsCompressedFITS())
623 nRows = inputFile->HasKey("ZNAXIS2") ? inputFile->GetInt("ZNAXIS2") : 0;
624 else
625 nRows = inputFile->HasKey("NAXIS2") ? inputFile->GetInt("NAXIS2") : 0;
626 nRoi = inputFile->HasKey("NROI") ? inputFile->GetInt("NROI") : 0;
627 runNumber = inputFile->HasKey("RUNID") ? inputFile->GetInt("RUNID") : -1;
628 nTM = inputFile->HasKey("NTMARK") ? inputFile->GetInt("NTMARK") : 0;
629
630 runType = "unkown";
631 if (inputFile->HasKey("RUNTYPE"))
632 {
633 runType = inputFile->GetStr("RUNTYPE");
634 if (runType == "")
635 runType = "unkown";
636 }
637 firstDataTime = inputFile->HasKey("TSTART") ? inputFile->GetInt("TSTART") : -1;
638 lastDataTime = inputFile->HasKey("TSTOP") ? inputFile->GetInt("TSTOP"): -1;
639 nRoiTM = inputFile->HasKey("NROITM") ? inputFile->GetInt("NROITM") : 0;
640 revision = inputFile->HasKey("REVISION") ? inputFile->GetInt("REVISION") : -1;
641 builderVersion = inputFile->HasKey("BLDVER") ? inputFile->GetInt("BLDVER") : -1;
642 nBoards = inputFile->HasKey("NBOARD") ? inputFile->GetInt("NBOARD") : 0;
643 nPixels = inputFile->HasKey("NPIX") ? inputFile->GetInt("NPIX") : 0;
644 timeSystem = inputFile->HasKey("TIMESYS") ? inputFile->GetStr("TIMESYS") : "";
645 creationDate = inputFile->HasKey("DATE") ? inputFile->GetStr("DATE") : "";
646 nightInt = inputFile->HasKey("NIGHT") ? inputFile->GetInt("NIGHT") : 0;
647 camera = inputFile->HasKey("CAMERA") ? inputFile->GetStr("CAMERA") : "";
648 daq = inputFile->HasKey("DAQ") ? inputFile->GetStr("DAQ") : "";
649 adcCount = inputFile->HasKey("ADCRANGE") ? inputFile->GetFloat("ADCRANGE") : 2000;
650 if (nPixels == 0)
651 {
652 cout << "could not read num pixels from fits header. Assuming 1440 (FACT)." << endl;
653 nPixels = 1440;
654 }
655 if (nRoi == 0 && !inputFile->HasKey("NROI"))
656 {//let's try to figure out the roi from the column's format
657 const fits::Table::Columns& cols = inputFile->GetColumns();
658 if (cols.find("Data") == cols.end())
659 {
660 cout << "ERROR: Column \"Data\" could not be found. abortin load." << endl;
661 return;
662 }
663 const fits::Table::Columns::const_iterator col = cols.find("Data");
664 if (col->second.type != 'I')
665 {
666 cout << "ERROR: Data Column has type " << col->second.type << " while viewer expects I" << endl;
667 return;
668 }
669 if (col->second.num % nPixels != 0)
670 {
671 cout << "ERROR: Num pixels (" << nPixels << ") does not match Data length (" << col->second.num << "). Aborting" << endl;
672 return;
673 }
674 nRoi = col->second.num/nPixels;
675 cout << "Estimate num samples per pixels to be " << nRoi;
676 _softwareOrdering = true;
677 }
678 else
679 _softwareOrdering = inputFile->Get<string>("ISMC", "F")=="T";
680
681 if (inputFile->HasKey("OFFSET"))
682 offSetRoi = inputFile->GetInt("OFFSET");
683
684 nbOk = 0;//inputFile->GetInt("NBEVTOK");
685 nbRej = 0;//inputFile->GetInt("NBEVTREJ");
686 nbBad = 0;//inputFile->GetInt("NBEVTBAD");
687
688 eventNum = 1;
689
690 if (eventData != NULL) {
691 delete[] eventData;
692 delete[] rawEventData;
693 delete[] waveLetArray;
694 }
695 eventData = new float[1440*nRoi + 160*nRoiTM];//(1440+160)*nRoi];
696
697 rawEventData = new int16_t[1440*nRoi + 160*nRoiTM];//(1440+160)*nRoi];
698 waveLetArray = new int16_t[1024*1440];
699 try
700 {
701 inputFile->SetPtrAddress("Data", rawEventData);
702 if (inputFile->HasColumn("EventNum"))
703 inputFile->SetPtrAddress("EventNum", &eventNum);
704 else
705 cout << "Warning: could not find column \"EventNum\"" << endl;
706 if (inputFile->HasColumn("TriggerType"))
707 inputFile->SetPtrAddress("TriggerType", &triggerType);
708 else
709 cout << "Warning: could not find column \"TriggerType\"" << endl;
710 if (inputFile->HasColumn("SoftTrig"))
711 inputFile->SetPtrAddress("SoftTrig", &softTrig);
712 else
713 cout << "Warning: could not find column \"SoftTrig\"" << endl;
714 if (inputFile->HasColumn("BoardTime"))
715 inputFile->SetPtrAddress("BoardTime", boardTime);
716 else
717 cout << "Warning: could not find column \"BoardTime\"" << endl;
718 if (inputFile->HasColumn("StartCellData"))
719 inputFile->SetPtrAddress("StartCellData", startPix);
720 else
721 cout << "Warning: could not find column \"StartCellData\"" << endl;
722 if (inputFile->HasColumn("StartCellTimeMarker"))
723 inputFile->SetPtrAddress("StartCellTimeMarker", startTM);
724 else
725 cout << "Warning: could not find column \"StartCellTimeMarker\"" << endl;
726 if (inputFile->HasColumn("TimeMarker"))
727 inputFile->SetPtrAddress("TimeMarker", &rawEventData[1440*nRoi]);
728 else
729 cout << "Warning: could not find column \"TimeMarker\"" << endl;
730 }
731 catch (const runtime_error &e)
732 {
733 cout << e.what() << endl;
734 cout << "Loading aborted." << endl;
735
736 nRoi = nRows = 0;
737
738 return;
739 }
740
741 try
742 {
743 pcTime[0] = pcTime[1] = 0;
744 if (inputFile->HasColumn("UnixTimeUTC"))
745 inputFile->SetPtrAddress("UnixTimeUTC", pcTime);
746 }
747 catch (const runtime_error&)
748 {
749 try
750 {
751 if (inputFile->HasColumn("PCTime"))
752 inputFile->SetPtrAddress("PCTime", pcTime);
753 else
754 cout << "Warning: could not find column \"UnixTimeUTC\" nor \"PCTime\"" << endl;
755
756 }
757 catch (const runtime_error&)
758 {
759
760 }
761 }
762
763
764 int backupStep = eventStep;
765 rowNum = -1;
766 eventStep = 1;
767
768 plusEvent();
769 eventStep = backupStep;
770 emit newFileLoaded();
771 emit signalCurrentPixel(selectedPixel);
772}
773
774void RawDataViewer::openCalibFile(string& file)
775{
776 //calibrationLoaded = false;
777 string msg;
778 try
779 {
780 msg = fDrsCalib.ReadFitsImp(file);
781 if (msg.empty())
782 {
783 emit newFileLoaded();
784 updateGL();
785 return;
786 }
787 }
788 catch (const runtime_error &e)
789 {
790 msg = string("Something went wrong while loading Drs Calib: ") + e.what() + string(".. Aborting file loading");
791 }
792 cerr << msg << endl;
793 fDrsCalib.Clear();
794}
795
796template <typename T>
797void RawDataViewer::getCalibrationDataForDisplay(const CalibDataTypes/* calibTypes*/,
798 const vector<T>& inputData,
799 const int roi,
800 const int roiTM)
801{
802
803 eventData = new float[1440*roi + 160*roiTM];//(1440+160)*nRoi];
804 nRoi=roi;
805 nRoiTM=roiTM;
806
807 long long min, max, mean;
808 min = max = inputData[0];
809 mean=0;
810 for (int i=0;i<1440*roi + 160*roiTM;i++) {
811 eventData[i] = (float)inputData[i];
812 mean += inputData[i];
813 if (inputData[i] > max)
814 max = inputData[i];
815 if (inputData[i] < min)
816 min = inputData[i];
817 }
818 mean /= 1440*roi + 160*roiTM;
819 for (int i=0;i<1440*roi + 160*roiTM;i++)
820 eventData[i] -= (float)mean;
821 VALUES_SPAN = max - min;
822// cout << VALUES_SPAN << " " << min << " " << max << " " << mean << endl;
823// cout << 1440*roi + 160*roiTM << " " << roi << " " << roiTM << " " << inputData.size() << endl;
824}
825/************************************************************
826 * PLUS EVENT
827 ************************************************************/
828void RawDataViewer::plusEvent()
829{
830 eventStepping(true);
831}
832/************************************************************
833 * MINUS EVENT
834 ************************************************************/
835void RawDataViewer::minusEvent()
836{
837 eventStepping(false);
838}
839/************************************************************
840 * SET EVENT STEP
841 ************************************************************/
842void RawDataViewer::setEventStep(int step)
843{
844 eventStep = step;
845}
846/************************************************************
847 * EVENT STEPPING
848 ************************************************************/
849
850void RawDataViewer::ApplyCalibration()
851{
852 for (int i=0;i<1440*nRoi + 160*nRoiTM;i++)//(1440+160)*nRoi;i++)
853 eventData[i] = (float)rawEventData[i];
854
855 if (fIsDrsCalibration)
856 {
857 fDrsCalib.Apply(eventData, rawEventData, startPix, nRoi);
858 DrsCalibrate::RemoveSpikes(eventData, nRoi);
859 //TODO apply calibration to the Time markers
860 }
861
862 //hide the time markers
863 int nSlicesToRemove = 60;
864 float* backupData = 0;
865 if (nRoiTM == 0) //they are written into the regular channel
866 {
867 backupData = new float[nSlicesToRemove*160];
868 for (int i=0;i<1440;i++)
869 {
870 const PixelMapEntry& mapEntry = fPixelMap.index(i);
871 const int pixelIdInPatch = mapEntry.pixel();
872 const int patchId = mapEntry.patch();
873 const int boardId = mapEntry.board();
874 const int crateId = mapEntry.crate();
875
876 const int hw = mapEntry.hw();
877 if (pixelIdInPatch == 8)
878 {
879 for (int j=0;j<nSlicesToRemove;j++)
880 {
881 backupData[(40*crateId + 4*boardId + patchId)*nSlicesToRemove+j] = eventData[(hw*nRoi) + (nRoi-nSlicesToRemove) + j];
882 eventData[(hw*nRoi) + (nRoi-nSlicesToRemove) + j] = eventData[hw*nRoi + (nRoi-nSlicesToRemove) - 1];
883 }
884 }
885 }
886 }
887
888 vector<float> pixelStatsData(1440*4);
889 DrsCalibrate::GetPixelStats(pixelStatsData.data(), eventData, nRoi);
890
891 for (vector<PixelMapEntry>::const_iterator it=fPixelMap.begin(); it!=fPixelMap.end(); it++)
892 {
893 Meanvalues[it->index] = pixelStatsData[0*1440+it->hw()];
894 RMSvalues[it->index] = pixelStatsData[1*1440+it->hw()];
895 Maxvalues[it->index] = pixelStatsData[2*1440+it->hw()];
896 PosOfMaxvalues[it->index] = pixelStatsData[3*1440+it->hw()];
897 }
898 if (nRoiTM == 0)//move back the data back in place
899 {
900 for (int i=0;i<1440;i++)
901 {
902 const PixelMapEntry& mapEntry = fPixelMap.index(i);
903 const int pixelIdInPatch = mapEntry.pixel();
904 const int patchId = mapEntry.patch();
905 const int boardId = mapEntry.board();
906 const int crateId = mapEntry.crate();
907 if (patchId > 160)
908 cout << "Voila mon probleme: " << patchId << endl;
909 const int hw = mapEntry.hw();
910 if (pixelIdInPatch == 8)
911 {
912 // cout << "|" << crateId << " " << boardId << " " << patchId << " " << hw << "| ";
913 for (int j=0;j<nSlicesToRemove;j++)
914 {
915 eventData[(hw*nRoi) + (nRoi - nSlicesToRemove) + j] = backupData[(40*crateId + 4*boardId + patchId)*nSlicesToRemove+j];
916 }
917 }
918 }
919 delete[] backupData;
920 }
921 if (isVisible())
922 updateGL();
923}
924
925void RawDataViewer::eventStepping(bool plus)
926{
927 if (plus)
928 rowNum += eventStep;
929 else
930 rowNum -= eventStep;
931 if (rowNum >= nRows)
932 rowNum -= nRows;
933 if (rowNum < 0)
934 rowNum += nRows;
935
936 if (inputFile == NULL)
937 return;
938 inputFile->GetRow(rowNum);
939 if (_softwareOrdering)
940 {//remap pixels data according to hardware id
941 if (nRoiTM != 0)
942 cout << "Warning: did not expect Time Markers data from Monte-Carlo simulations. These will not be mapped properly." << endl;
943 //first copy the data
944 int16_t* tempData = new int16_t[1440*nRoi];
945 for (int i=0;i<1440*nRoi;i++)
946 tempData[i] = rawEventData[i];
947 //copy back the data and re-map it on the fly
948 for (int i=0;i<1440;i++)
949 for (int j=0;j<nRoi;j++)
950 rawEventData[i*nRoi + j] = tempData[softwareMapping[i]*nRoi + j];
951
952 delete[] tempData;
953 }
954// cout << "Getting row " << rowNum << endl;
955
956
957 ApplyCalibration();
958
959 emit signalCurrentEvent(eventNum);
960 emit signalCurrentPixel(selectedPixel);
961}
962
963/************************************************************
964 * NEXT SLICE. deprec ?
965 ************************************************************/
966void RawDataViewer::nextSlice()
967{
968 whichSlice++;
969 if (whichSlice >= nRoi)
970 {
971 whichSlice = 0;
972 if (!loopCurrentEvent)
973 {
974 int backupStep = eventStep;
975 eventStep = 1;
976 eventStepping(true);
977 eventStep = backupStep;
978 }
979 }
980 emit signalCurrentSlice(whichSlice);
981 updateGL();
982}
983void RawDataViewer::previousSlice()
984{
985 whichSlice--;
986 if (whichSlice < 0)
987 {
988 whichSlice = nRoi-1;
989 if (!loopCurrentEvent)
990 {
991 int backupStep = eventStep;
992 eventStep = 1;
993 eventStepping(false);
994 eventStep = backupStep;
995 }
996 }
997 emit signalCurrentSlice(whichSlice);
998 updateGL();
999}
1000void RawDataViewer::setCurrentPixel(int pix)
1001{
1002 // if (pix == -1)
1003 // return;
1004 selectedPixel = pix;
1005 if (isVisible())
1006 updateGL();
1007 emit signalCurrentPixel(pix);
1008}
1009void RawDataViewer::computePulsesStatistics()
1010{
1011 if (!inputFile)
1012 {
1013 cout << "A FITS file must be open in order to complete this operation" << endl;
1014 return;
1015 }
1016
1017
1018// for (int i=0;i<nRows;i++)//for all events
1019// {
1020// inputFile->GetRow(rowNum);
1021// for (int i=0;i<(1440+160)*nRoi;i++)
1022// eventData[i] = (float)rawEventData[i];
1023
1024// for (int j=0;j<ACTUAL_NUM_PIXELS;j++)
1025/// {
1026 int j = selectedPixel;
1027 if (j == -1)
1028 return;
1029 for (int i=0;i<nRoi;i++)
1030 {
1031 aMeas[i] = eventData[j*nRoi+i];// * adcCount;
1032
1033 }
1034 for (int i=0;i<nRoi;i++)
1035 {
1036 if (i==0)
1037 n1mean[i] = aMeas[i+1];
1038 else
1039 {
1040 if (i==1023)
1041 n1mean[i] = aMeas[i-1];
1042 else
1043 n1mean[i] = (aMeas[i-1]+aMeas[i+1])/2.f;
1044 }
1045 }
1046 //find spike
1047 for (int i=0;i<nRoi-3;i++)
1048 {
1049 const float fract = 0.8f;
1050 float xx, xp, xpp;
1051 vCorr[i] = 0;//aMeas[i];
1052 xx = aMeas[i] - n1mean[i];
1053 if (xx < -8.f)
1054 {
1055 xp = aMeas[i+1] - n1mean[i+1];
1056 xpp = aMeas[i+2] - n1mean[i+2];
1057 if ((aMeas[i+2] - (aMeas[i] + aMeas[i+3])/2.f) > 10.f)
1058 {
1059 vCorr[i+1] = (aMeas[i] + aMeas[i+3])/2.f;
1060 vCorr[i+2] = (aMeas[i] + aMeas[i+3])/2.f;
1061 i = i+2;
1062 }
1063 else
1064 {
1065 if ((xp > -2.*xx*fract) && (xpp < -10.f))
1066 {
1067 vCorr[i+1] = n1mean[i+1];
1068 n1mean[i+2] = aMeas[i+1] - aMeas[i+3]/2.f;
1069 i++;
1070 }
1071 }
1072 }
1073 }
1074 for (int i=0;i<nRoi;i++)
1075 n1mean[i] = aMeas[i]-n1mean[i];
1076 // }
1077 // }
1078}
1079/************************************************************
1080 * UICONNECTOR CONSTRUCTOR
1081 ************************************************************/
1082UIConnector::UIConnector(QWidget *p)
1083{
1084 setupUi(this);
1085 initHistograms();
1086
1087 currentFile = "none";
1088 currentCalibFile = "none";
1089
1090 updateSpinnerDisplay = true;
1091 updating = false;
1092
1093 timer.setInterval(10.0);
1094 QObject::connect(&timer, SIGNAL(timeout()),
1095 this, SLOT(nextSlicePlease()));
1096
1097 QButtonGroup &scaleGroup = *new QButtonGroup(p);// = new QButtonGroup(canvas);
1098 QButtonGroup &animateGroup = *new QButtonGroup(p);// = new QButtonGroup(canvas);
1099
1100 scaleGroup.addButton(currentPixelScale);
1101 scaleGroup.addButton(entireCameraScale);
1102
1103 animateGroup.addButton(playEventsRadio);
1104 animateGroup.addButton(playSlicesRadio);
1105 animateGroup.addButton(playPixelsRadio);
1106
1107 entireCameraScale->setChecked(true);
1108
1109 RMS_window->enableText(false);
1110 Mean_window->enableText(false);
1111 PosOfMax_window->enableText(false);
1112 Max_window->enableText(false);
1113
1114 // RMS_window->ShowPatchCursor(true);
1115
1116 QObject::connect(GLWindow, SIGNAL(colorPaletteHasChanged()),
1117 this, SLOT(on_autoScaleColor_clicked()));
1118 QObject::connect(GLWindow, SIGNAL(signalCurrentSlice(int)),
1119 this, SLOT(currentSliceHasChanged(int)));
1120 QObject::connect(GLWindow, SIGNAL(signalCurrentEvent(int)),
1121 this, SLOT(currentEventHasChanged(int)));
1122 QObject::connect(GLWindow, SIGNAL(signalCurrentPixel(int)),
1123 this, SLOT(pixelChanged(int)));
1124 QObject::connect(GLWindow, SIGNAL(newFileLoaded()),
1125 this, SLOT(newFileLoaded()));
1126
1127 QObject::connect(RMS_window, SIGNAL(signalCurrentPixel(int)),
1128 GLWindow, SLOT(setCurrentPixel(int)));
1129 QObject::connect(Max_window, SIGNAL(signalCurrentPixel(int)),
1130 GLWindow, SLOT(setCurrentPixel(int)));
1131 QObject::connect(PosOfMax_window, SIGNAL(signalCurrentPixel(int)),
1132 GLWindow, SLOT(setCurrentPixel(int)));
1133 QObject::connect(Mean_window, SIGNAL(signalCurrentPixel(int)),
1134 GLWindow, SLOT(setCurrentPixel(int)));
1135
1136
1137
1138 show();
1139}
1140UIConnector::~UIConnector()
1141{
1142 grid1->detach();
1143 grid2->detach();
1144 grid3->detach();
1145 grid4->detach();
1146 grid5->detach();
1147 grid6->detach();
1148 boardsTimeHistoItem.detach();
1149 startCellHistoItem.detach();
1150 startTimeMarkHistoItem.detach();
1151 pixelValueCurveItem.detach();
1152 pixelAverageCurveItem.detach();
1153 aMeanCurveItem.detach();
1154 vCorrCurveItem.detach();
1155 meanCurveItem.detach();
1156}
1157void UIConnector::slicesPlusPlus()
1158{
1159 GLWindow->nextSlice();
1160}
1161void UIConnector::slicesMinusMinus()
1162{
1163 GLWindow->previousSlice();
1164}
1165void UIConnector::on_calibratedCheckBox_stateChanged(int state)
1166{
1167 GLWindow->fIsDrsCalibration = state;
1168 GLWindow->ApplyCalibration();
1169 threeD_Window->setData(GLWindow->eventData);
1170
1171 on_autoScaleColor_clicked();
1172 pixelChanged(GLWindow->selectedPixel);
1173
1174}
1175/************************************************************
1176 * DRAW PATCHES CHECK CHANGE. checkbox handler
1177 ************************************************************/
1178void UIConnector::on_drawPatchCheckBox_stateChanged(int state)
1179{
1180 GLWindow->drawPatch = state;
1181 GLWindow->updateGL();
1182 RMS_window->fDrawPatch = state;
1183 RMS_window->updateGL();
1184 Mean_window->fDrawPatch = state;
1185 Mean_window->updateGL();
1186 Max_window->fDrawPatch = state;
1187 Max_window->updateGL();
1188 PosOfMax_window->fDrawPatch = state;
1189 PosOfMax_window->updateGL();
1190}
1191/************************************************************
1192 * DRAW IMPULSE CHECK CHANGE. checkbox handler
1193 ************************************************************/
1194void UIConnector::on_drawImpulseCheckBox_stateChanged(int state)
1195{
1196 GLWindow->drawImpulse = state;
1197 TriggerOffset_window->drawImpulse = state;
1198 Gain_window->drawImpulse = state;
1199 Baseline_window->drawImpulse = state;
1200 GLWindow->updateGL();
1201 TriggerOffset_window->updateGL();
1202 Gain_window->updateGL();
1203 Baseline_window->updateGL();
1204}
1205/************************************************************
1206 * DRAW BLUR CHECK CHANGE. checkbox handler
1207 ************************************************************/
1208void UIConnector::on_drawBlurCheckBox_stateChanged(int state)
1209{
1210 GLWindow->drawBlur = state;
1211 GLWindow->updateGL();
1212}
1213void UIConnector::on_loopOverCurrentEventBox_stateChanged(int state)
1214{
1215 GLWindow->loopCurrentEvent = state;
1216}
1217
1218/************************************************************
1219 * NEXT SLICE PLEASE
1220 ************************************************************/
1221void UIConnector::nextSlicePlease()
1222{
1223 if (playEventsRadio->isChecked ())
1224 GLWindow->eventStepping(true);
1225 else
1226 if (playPixelsRadio->isChecked())
1227 GLWindow->setCurrentPixel((GLWindow->getCurrentPixel()+1)%1440);
1228 else
1229 GLWindow->nextSlice();
1230}
1231
1232/************************************************************
1233 * SET VIEWER.
1234 ************************************************************/
1235//void UIConnector::setViewer(RawDataViewer* v)
1236//{
1237// viewer = v;
1238//}
1239/************************************************************
1240 * SLICES PER SECOND CHANGED. timing ui handler
1241 ************************************************************/
1242void UIConnector::slicesPerSecondChanged(double value)
1243{
1244 timer.setInterval(1000.0/value);
1245}
1246
1247void UIConnector::on_colorRange0_valueChanged(double value) { GLWindow->ss[0] = (float)value; GLWindow->updateGL(); }
1248void UIConnector::on_colorRange1_valueChanged(double value) { GLWindow->ss[1] = (float)value; GLWindow->updateGL(); }
1249void UIConnector::on_colorRange2_valueChanged(double value) { GLWindow->ss[2] = (float)value; GLWindow->updateGL(); }
1250void UIConnector::on_colorRange3_valueChanged(double value) { GLWindow->ss[3] = (float)value; GLWindow->updateGL(); }
1251void UIConnector::on_colorRange4_valueChanged(double value) { GLWindow->ss[4] = (float)value; GLWindow->updateGL(); }
1252
1253void UIConnector::on_redValue0_valueChanged(double value) { GLWindow->rr[0] = (float)value; GLWindow->updateGL(); }
1254void UIConnector::on_redValue1_valueChanged(double value) { GLWindow->rr[1] = (float)value; GLWindow->updateGL(); }
1255void UIConnector::on_redValue2_valueChanged(double value) { GLWindow->rr[2] = (float)value; GLWindow->updateGL(); }
1256void UIConnector::on_redValue3_valueChanged(double value) { GLWindow->rr[3] = (float)value; GLWindow->updateGL(); }
1257void UIConnector::on_redValue4_valueChanged(double value) { GLWindow->rr[4] = (float)value; GLWindow->updateGL(); }
1258
1259void UIConnector::on_greenValue0_valueChanged(double value) { GLWindow->gg[0] = (float)value; GLWindow->updateGL(); }
1260void UIConnector::on_greenValue1_valueChanged(double value) { GLWindow->gg[1] = (float)value; GLWindow->updateGL(); }
1261void UIConnector::on_greenValue2_valueChanged(double value) { GLWindow->gg[2] = (float)value; GLWindow->updateGL(); }
1262void UIConnector::on_greenValue3_valueChanged(double value) { GLWindow->gg[3] = (float)value; GLWindow->updateGL(); }
1263void UIConnector::on_greenValue4_valueChanged(double value) { GLWindow->gg[4] = (float)value; GLWindow->updateGL(); }
1264
1265void UIConnector::on_blueValue0_valueChanged(double value) { GLWindow->bb[0] = (float)value; GLWindow->updateGL(); }
1266void UIConnector::on_blueValue1_valueChanged(double value) { GLWindow->bb[1] = (float)value; GLWindow->updateGL(); }
1267void UIConnector::on_blueValue2_valueChanged(double value) { GLWindow->bb[2] = (float)value; GLWindow->updateGL(); }
1268void UIConnector::on_blueValue3_valueChanged(double value) { GLWindow->bb[3] = (float)value; GLWindow->updateGL(); }
1269void UIConnector::on_blueValue4_valueChanged(double value) { GLWindow->bb[4] = (float)value; GLWindow->updateGL(); }
1270
1271/************************************************************
1272 * LOAD NEW FILE CLICKED. button handler
1273 ************************************************************/
1274void UIConnector::on_loadNewFileButton_clicked()
1275{
1276 QFileDialog dialog;
1277 dialog.setFileMode(QFileDialog::ExistingFile);
1278 dialog.open(this, SLOT(fileSelected(QString)));
1279 dialog.setVisible(true);
1280 dialog.exec();
1281}
1282void UIConnector::on_loadDRSCalibButton_clicked()
1283{
1284 QFileDialog dialog;
1285 dialog.setFileMode(QFileDialog::ExistingFile);
1286 dialog.open(this, SLOT(calibFileSelected(QString)));
1287 dialog.setVisible(true);
1288 dialog.exec();
1289}
1290/************************************************************
1291 * FILE SELECTED. return of the file open dialog handler
1292 ************************************************************/
1293void UIConnector::fileSelected(QString file)
1294{
1295 currentFile = file.toStdString();
1296 if (currentFile != "")
1297 GLWindow->openFile(currentFile);
1298}
1299void UIConnector::calibFileSelected(QString file)
1300{
1301 currentCalibFile = file.toStdString();
1302 if (currentCalibFile != "")
1303 GLWindow->openCalibFile(currentCalibFile);
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);//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->nRoi;j++)
1574 {
1575 int value = GLWindow->eventData[1440*GLWindow->nRoi + i*GLWindow->nRoi + 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
2028
2029int realNumSamples = GLWindow->nRoi;
2030 if (GLWindow->nRoiTM != 0)
2031 {
2032 const PixelMapEntry& mapEntry = GLWindow->fPixelMap.index(softwarePix);
2033 const int pixelIdInPatch = mapEntry.pixel();
2034 const int patchId = mapEntry.patch();
2035 const int boardId = mapEntry.board();
2036 const int crateId = mapEntry.crate();
2037 if (pixelIdInPatch == 8)
2038 {
2039 int TMIndex = 0;
2040 int xIndex = GLWindow->nRoi;
2041 int arrayIndex = GLWindow->nRoi;
2042 if (GLWindow->offSetRoi < 0)
2043 TMIndex -= GLWindow->offSetRoi;
2044 if (GLWindow->offSetRoi > 0)
2045 xIndex += GLWindow->offSetRoi;
2046 for (int i=TMIndex;i<GLWindow->nRoiTM;i++, xIndex++, arrayIndex++)
2047 {
2048 xval[arrayIndex] = xIndex;
2049 yval[arrayIndex] = GLWindow->eventData[GLWindow->nRoi*1440 + GLWindow->nRoiTM*(40*crateId + 4*boardId + patchId) + i];
2050 }
2051 realNumSamples += GLWindow->nRoiTM - TMIndex;
2052 }
2053 // cout << pixelIdInPatch << " " ;
2054 }
2055
2056#if QWT_VERSION < 0x060000
2057 pixelValueCurveItem.setData(xval, yval, realNumSamples);
2058#else
2059 pixelValueCurveItem.setSamples(xval, yval, realNumSamples);
2060#endif
2061
2062//now compute the average value of all pixels
2063 currentPixel = 0;
2064 for (int i=0;i<GLWindow->nRoi;i++)
2065 yval[i] = 0;
2066 for (int j=0;j<1440;j++) {
2067 currentPixel = j;
2068 for (int i=0;i<GLWindow->nRoi;i++)
2069 {
2070 xval[i] = i;
2071 yval[i] += GLWindow->eventData[GLWindow->nRoi*currentPixel + i];
2072 }
2073 }
2074 for (int i=0;i<GLWindow->nRoi;i++)
2075 yval[i] /= 1440;
2076#if QWT_VERSION < 0x060000
2077 pixelAverageCurveItem.setData(xval, yval, GLWindow->nRoi);
2078#else
2079 pixelAverageCurveItem.setSamples(xval, yval, realNumSamples);
2080#endif
2081
2082 QStack< QRectF > stack;
2083 stack.push(scaleBoundingRectangle(pixelValueCurveItem.boundingRect(), 1.5f));
2084 curveZoom->setZoomBase(scaleBoundingRectangle(pixelValueCurveItem.boundingRect(), 1.5f));
2085 curveZoom->setZoomStack(stack);
2086 stack.pop();
2087 stack.push(scaleBoundingRectangle(pixelAverageCurveItem.boundingRect(), 1.5f));
2088 averageCurveZoom->setZoomBase(scaleBoundingRectangle(pixelAverageCurveItem.boundingRect(), 1.5f));
2089 averageCurveZoom->setZoomStack(stack);
2090 stack.pop();
2091
2092 displaySliceValue();
2093 on_autoScaleColor_clicked();
2094}
2095
2096void UIConnector::on_HwIDBox_valueChanged(int)
2097{
2098 updating = true;
2099
2100 const int hwID = HwIDBox->value();
2101
2102 const int crateID = hwID/360;
2103 const int boardID = (hwID%360)/36;
2104 const int patchID = (hwID%36 )/9;
2105 const int pixelID = hwID%9;
2106
2107 SwIDBox->setValue(GLWindow->softwareMapping[hwID]);
2108
2109 crateIDBox->setValue(crateID);
2110 boardIDBox->setValue(boardID);
2111 patchIDBox->setValue(patchID);
2112 pixelIDBox->setValue(pixelID);
2113
2114 updating = false;
2115
2116 GLWindow->selectedPixel = GLWindow->softwareMapping[hwID];
2117 GLWindow->updateGL();
2118
2119 pixelChanged(GLWindow->selectedPixel);
2120}
2121
2122void UIConnector::cbpxChanged()
2123{
2124 if (updating)
2125 return;
2126
2127 const int hwid = crateIDBox->value()*360 + boardIDBox->value()*36 + patchIDBox->value()*9 + pixelIDBox->value();
2128 HwIDBox->setValue(hwid);
2129}
2130
2131void UIConnector::on_SwIDBox_valueChanged(int swid)
2132{
2133 if (updating)
2134 return;
2135
2136// if (GLWindow->_softwareOrdering)
2137// HwIDBox->setValue(swid);
2138// else
2139 HwIDBox->setValue(GLWindow->hardwareMapping[swid]);
2140}
2141
2142void UIConnector::on_autoScaleColor_clicked()
2143{
2144 if (!autoScaleColor->isChecked())
2145 {
2146 GLWindow->ss[0] = 0.496;
2147 GLWindow->ss[1] = 0.507;
2148 GLWindow->ss[2] = 0.518;
2149 GLWindow->ss[3] = 0.529;
2150 GLWindow->ss[4] = 0.540;;
2151 colorRange0->setValue(GLWindow->ss[0]);
2152 colorRange1->setValue(GLWindow->ss[1]);
2153 colorRange2->setValue(GLWindow->ss[2]);
2154 colorRange3->setValue(GLWindow->ss[3]);
2155 colorRange4->setValue(GLWindow->ss[4]);
2156 return;
2157 }
2158 if (!GLWindow->nRoi)
2159 return;
2160
2161 int start = 0;
2162 int end = 1440;
2163
2164 if (!entireCameraScale->isChecked())
2165 {
2166 start = GLWindow->selectedPixel;
2167 end = GLWindow->selectedPixel+1;
2168 if (end == 0)
2169 {
2170 start = 0;
2171 end = 1440;
2172 }
2173 }
2174
2175 int min = 100000; //real min = -2048, int_16 = -32768 to 32767
2176 int max = -100000; //real max = 2047
2177
2178 long average = 0;
2179 long numSamples = 0;
2180 int errorDetected = -1;
2181
2182 for (int i=start;i<end;i++)
2183 {
2184 if (i==863)//keep crazy pixel out of the autoscale
2185 continue;
2186 for (int j=10;j<GLWindow->nRoi-50;j++)
2187 {
2188 int cValue = GLWindow->eventData[i*GLWindow->nRoi+j];
2189 if (cValue > max && cValue < 32767)
2190 max = cValue;
2191 if (cValue < min && cValue > -32768)
2192 min = cValue;
2193 if (cValue < 32767 && cValue > -32768)
2194 {
2195 average+=cValue;
2196 numSamples++;
2197 }
2198 else
2199 {
2200 errorDetected = i;
2201 }
2202// numSamples++;
2203 }
2204 }
2205 average /= numSamples;
2206 if (errorDetected != -1)
2207 {
2208 cout << "Overflow detected at pixel " << errorDetected << " (at least)" << endl;
2209 }
2210// cout << "min: " << min << " max: " << max << " average: " << average << endl;
2211 float minRange = (float)(min+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2212 float maxRange = (float)(max+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2213 float midRange = (float)(average+(GLWindow->VALUES_SPAN/2))/(float)(GLWindow->VALUES_SPAN-1);
2214 if (GLWindow->logScale)
2215 {
2216 minRange *= 9;
2217 maxRange *= 9;
2218// midRange *= 9;
2219 minRange += 1;
2220 maxRange += 1;
2221// midRange += 1;
2222 minRange = log10(minRange);
2223 maxRange = log10(maxRange);
2224// midRange = (minRange + maxRange)/2.f;
2225 midRange = log10(midRange);
2226 }
2227
2228 GLWindow->ss[0] = minRange;
2229 colorRange0->setValue(GLWindow->ss[0]);
2230 GLWindow->ss[4] = maxRange;
2231 colorRange4->setValue(GLWindow->ss[4]);
2232// GLWindow->ss[2] = midRange;
2233// range2->setValue(GLWindow->ss[2]);
2234// GLWindow->ss[1] = (minRange+midRange)/2;
2235// range1->setValue(GLWindow->ss[1]);
2236// GLWindow->ss[3] = (maxRange+midRange)/2;
2237// range3->setValue(GLWindow->ss[3]);
2238
2239 GLWindow->ss[2] = (maxRange+minRange)/2;
2240 colorRange2->setValue(GLWindow->ss[2]);
2241
2242 GLWindow->ss[1] = minRange+(maxRange-minRange)/4;
2243 colorRange1->setValue(GLWindow->ss[1]);
2244
2245 GLWindow->ss[3] = minRange+3*(maxRange-minRange)/4;
2246 colorRange3->setValue(GLWindow->ss[3]);
2247}
2248
2249void PrintUsage()
2250{
2251 cout << "\n"
2252 "The FACT++ raw data viewer.\n"
2253 "\n"
2254 "Usage: viewer [OPTIONS] [datafile.fits[.gz|.fz] [calibration.drs.fits[.gz]]]\n"
2255 " or: viewer [OPTIONS]\n";
2256 cout << endl;
2257
2258}
2259
2260void PrintHelp()
2261{
2262 cout <<
2263 "\n"
2264 << endl;
2265}
2266
2267int UIConnector::SetupConfiguration(Configuration &conf)
2268{
2269 RawDataViewer *canvas = GLWindow;
2270
2271 if (conf.Has("mappingFile"))
2272 {
2273 canvas->assignPixelMapFile(conf.Get<string>("mappingFile"));
2274 }
2275 else
2276 canvas->assignPixelMapFile("");
2277
2278 if (conf.Has("color.range"))
2279 {
2280 vector<double> value = conf.Vec<double>("color.range");
2281 if (value.size() != 5)
2282 {
2283 cout << "Error, colorRange option should have exactly 5 double values" << endl;
2284 return -1;
2285 }
2286 for (int i=0;i<5;i++)
2287 canvas->ss[i] = value[i];
2288 }
2289
2290 if (conf.Has("color.red"))
2291 {
2292 vector<double> value = conf.Vec<double>("color.red");
2293 if (value.size() != 5)
2294 {
2295 cout << "Error, colorRed option should have exactly 5 double values" << endl;
2296 return -1;
2297 }
2298 for (int i=0;i<5;i++)
2299 canvas->rr[i] = value[i];
2300 }
2301
2302 if (conf.Has("color.green"))
2303 {
2304 vector<double> value = conf.Vec<double>("color.green");
2305 if (value.size() != 5)
2306 {
2307 cout << "Error, colorGreen option should have exactly 5 double values" << endl;
2308 return -1;
2309 }
2310 for (int i=0;i<5;i++)
2311 canvas->gg[i] = value[i];
2312 }
2313
2314 if (conf.Has("color.blue"))
2315 {
2316 vector<double> value = conf.Vec<double>("color.blue");
2317 if (value.size() != 5)
2318 {
2319 cout << "Error, colorBlue option should have exactly 5 double values" << endl;
2320 return -1;
2321 }
2322 for (int i=0;i<5;i++)
2323 canvas->bb[i] = value[i];
2324 }
2325
2326 colorRange0->setValue(canvas->ss[0]);
2327 colorRange1->setValue(canvas->ss[1]);
2328 colorRange2->setValue(canvas->ss[2]);
2329 colorRange3->setValue(canvas->ss[3]);
2330 colorRange4->setValue(canvas->ss[4]);
2331 redValue0->setValue(canvas->rr[0]);
2332 redValue1->setValue(canvas->rr[1]);
2333 redValue2->setValue(canvas->rr[2]);
2334 redValue3->setValue(canvas->rr[3]);
2335 redValue4->setValue(canvas->rr[4]);
2336 greenValue0->setValue(canvas->gg[0]);
2337 greenValue1->setValue(canvas->gg[1]);
2338 greenValue2->setValue(canvas->gg[2]);
2339 greenValue3->setValue(canvas->gg[3]);
2340 greenValue4->setValue(canvas->gg[4]);
2341 blueValue0->setValue(canvas->bb[0]);
2342 blueValue1->setValue(canvas->bb[1]);
2343 blueValue2->setValue(canvas->bb[2]);
2344 blueValue3->setValue(canvas->bb[3]);
2345 blueValue4->setValue(canvas->bb[4]);
2346
2347 if (conf.Has("drs"))
2348 {
2349 const QString qstr(conf.Get<string>("drs").c_str());
2350 calibFileSelected(qstr);
2351 }
2352
2353 if (conf.Has("file"))
2354 {
2355 const QString qstr(conf.Get<string>("file").c_str());
2356 fileSelected(qstr);
2357 }
2358
2359
2360 return 0;
2361}
2362
2363void SetupConfiguration(Configuration& conf)
2364{
2365 po::options_description configs("Raw Events Viewer Options");
2366 configs.add_options()
2367 ("color.range", vars<double>(), "Range of the display colours")
2368 ("color.red", vars<double>(), "Range of red values")
2369 ("color.green", vars<double>(), "Range of green values")
2370 ("color.blue", vars<double>(), "Range of blue values")
2371 ("file,f", var<string>(), "File to be loaded")
2372 ("drs,d", var<string>(), "DRS calibration file to be loaded")
2373 ("mappingFile", var<string>(), "Which pixels mapping file to use")
2374 ;
2375 conf.AddOptions(configs);
2376
2377 po::positional_options_description p;
2378 p.add("file", 1); // The first positional options
2379 p.add("drs", 2); // The first positional options
2380 conf.SetArgumentPositions(p);
2381
2382}
2383
2384/************************************************************
2385 * MAIN PROGRAM FUNCTION.
2386 ************************************************************/
2387int main(int argc, const char *argv[])
2388{
2389 QApplication app(argc, const_cast<char**>(argv));
2390
2391 if (!QGLFormat::hasOpenGL()) {
2392 std::cerr << "This system has no OpenGL support" << std::endl;
2393 return 1;
2394 }
2395
2396 Configuration conf(argv[0]);
2397 conf.SetPrintUsage(PrintUsage);
2398 SetupConfiguration(conf);
2399 if (!conf.DoParse(argc, argv, PrintHelp))
2400 return 2;
2401
2402 UIConnector myUi;
2403 if (myUi.SetupConfiguration(conf)<0)
2404 return 3;
2405
2406 return app.exec();
2407}
2408
Note: See TracBrowser for help on using the repository browser.