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

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