source: branches/fscctrl_safety_limits/gui/RawEventsViewer/RawEventsViewer.cc@ 20115

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