source: branches/FACT++_part_filenames/gui/RawEventsViewer/RawEventsViewer.cc@ 18752

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