source: tools/ddd/Functions.cpp@ 1736

Last change on this file since 1736 was 198, checked in by ogrimm, 15 years ago
Added chip selection spin box
File size: 17.1 KB
Line 
1
2#include "GUI.h"
3
4//---------------------------------------------------------------------
5//************************ All functions ****************************
6//-------------------------------------------------------------------
7
8// +++ Open file dialog +++
9void ddd::FileDialog(void) {
10QString Filename = QFileDialog::getOpenFileName(this,
11 "Open raw file", INITIAL_DIRECTORY, "Raw data files (*.raw);; All files (*)");
12 if (Filename != NULL) {
13 FilenameBox->setText(Filename);
14 OpenDatafile();
15 }
16}
17
18// +++ Open selected file and read run header +++
19void ddd::OpenDatafile() {
20
21 if(Socket->state() == QAbstractSocket::ConnectedState) { // do not execute if socket is open
22 MakeConnection();
23 }
24
25 CloseDatafile(); // Close previous file if open
26
27 // Write run header to temporary file
28 ftruncate(fileno(Tmpfile),0);
29 rewind(Tmpfile);
30 switch (RD->OpenDataFile(FilenameBox->text().toAscii().data(), Tmpfile)) {
31 case CTX_FOPEN: QMessageBox::warning(this, "ddd Message","Could not open file.",QMessageBox::Ok);
32 return;
33 case CTX_RHEADER: QMessageBox::warning(this, "ddd Message","Could not read run header.",QMessageBox::Ok);
34 return;
35 case CTX_BSTRUCT: QMessageBox::warning(this, "ddd Message","Could not read board structures.",QMessageBox::Ok);
36 return;
37 default: break;
38 }
39 RunHeader *R = RD->RHeader;
40
41 if (R->MagicNum == MAGICNUM_OPEN) {
42 QMessageBox::warning(this, "ddd Message","Magic number in run header indicates that the file has not been closed properly.",QMessageBox::Ok);
43 }
44 if (R->MagicNum == MAGICNUM_ERROR) {
45 QMessageBox::warning(this, "ddd Message","Magic number in run header indicates that an error occurred while writing the file.",QMessageBox::Ok);
46 }
47
48 rewind(Tmpfile);
49 QTextStream in(Tmpfile);
50 QString text = in.readAll();
51 RunHeaderDisplay->setPlainText(text);
52
53 // Enable spin boxes, set ranges and display first event
54 EventNo->setEnabled(true);
55 ChannelNo->setEnabled(true);
56 ChipNo->setEnabled(true);
57 BoardNo->setEnabled(true);
58 PixelID->setEnabled(true);
59 M0Start->setEnabled(true);
60 M0Stop->setEnabled(true);
61 PhysPipeAction->setEnabled(true);
62 EventNo->setRange(1, R->Events);
63 M0Display->setEnabled(true);
64 M0Start->setRange(0, R->Samples-1);
65 M0Stop->setRange(0, R->Samples-1);
66 ChannelNo->setRange(0, R->NChannels-1);
67 ChipNo->setRange(0, R->NChips-1);
68 BoardNo->setRange(0, R->NBoards-1);
69 DisplayEvent();
70}
71
72// +++ Close data file file if open, delete event header and disable spin boxes and displays +++
73void ddd::CloseDatafile() {
74 if(RD->CloseDataFile()==CTX_OK) {
75 EventNo->setEnabled(false);
76 ChannelNo->setEnabled(false);
77 ChipNo->setEnabled(false);
78 BoardNo->setEnabled(false);
79 PixelID->setEnabled(false);
80 M0Start->setEnabled(false);
81 M0Stop->setEnabled(false);
82 PhysPipeAction->setEnabled(false);
83 RunHeaderDisplay->clear();
84 EventHeaderDisplay->clear();
85 Signal->hide();
86 }
87}
88
89// +++ Read event header and display event (only called if Datafile is open) +++
90void ddd::DisplayEvent(int) {
91
92 PixelID->setText(PixMap->DRS_to_Pixel(BoardNo->value(), ChipNo->value(), ChannelNo->value()).c_str()); // Translate to pixel ID
93 if(Socket->state() == QAbstractSocket::ConnectedState) return; // do not execute if socket is open
94
95 if (PhysPipeAction->isChecked()) {
96 Graph->setAxisTitle(QwtPlot::xBottom, "Time from start of physical pipeline (ns)");
97 }
98 else {
99 Graph->setAxisTitle(QwtPlot::xBottom, "Time from trigger (ns)");
100 }
101
102 // Read event
103 ftruncate(fileno(Tmpfile),0);
104 rewind(Tmpfile);
105 if (RD->ReadEvent(EventNo->value(), Tmpfile) != CTX_OK) {
106 QMessageBox::warning(this, "ddd Warning","Could not read event.",QMessageBox::Ok);
107 EventHeaderDisplay->clear();
108 return;
109 }
110 RunHeader *R = RD->RHeader;
111
112 // Print event header and trigger cell information from event data
113 rewind(Tmpfile);
114 QTextStream in(Tmpfile);
115 QString text = in.readAll();
116
117 text.append("\nTrigger cells: ");
118 for (unsigned int i=0; i<R->NBoards*R->NChips; i++) {
119 QString a;
120 text.append(a.sprintf("%d ", *((int *)RD->Data + i)));
121 }
122 EventHeaderDisplay->setPlainText(text);
123
124 // Case data in double format required by qwt library
125 double *x = new double [R->Samples];
126 double *y = new double [R->Samples];
127
128 for (unsigned int i=0; i<R->Samples; i++) {
129 x[i] = (double) (i/RD->BStruct[BoardNo->value()].NomFreq);
130 if (PhysPipeAction->isChecked()) {
131 y[(i + *((int *) RD->Data + BoardNo->value()*R->NChips+ChipNo->value()))%1024] =
132 (double) *((short *) (RD->Data + R->NBoards*R->NChips*sizeof(int)) +
133 BoardNo->value()*R->NChips*R->NChannels*R->Samples +
134 ChipNo->value()*R->NChannels*R->Samples +
135 ChannelNo->value()*RD->RHeader->Samples + i) * RD->BStruct[BoardNo->value()].ScaleFactor;
136 }
137 else {
138 y[i] = (double) *((short *) (RD->Data + R->NBoards*R->NChips*sizeof(int)) +
139 BoardNo->value()*R->NChips*R->NChannels*R->Samples +
140 ChipNo->value()*R->NChannels*R->Samples +
141 ChannelNo->value()*R->Samples + i) * RD->BStruct[BoardNo->value()].ScaleFactor;
142 }
143 }
144
145 Signal->setData(x, y, (int) R->Samples);
146 Signal->show();
147 Zoomer->setZoomBase(Signal->boundingRect());
148
149 delete[] x; delete[] y;
150
151 // ************************************
152 // Get data for M0 display (event based)
153 // ************************************
154
155 if (!(M0Display->isEnabled())) return;
156
157 double z[6][6] = {{0}};//36 pixels
158 bool IDerror = false;
159
160 //only interested in M0 data using DRS2
161 //if (RD->RHeader->NChannels == 9) IDerror = true;
162
163 //only interested in M0 data using DRS4
164 if (RD->RHeader->NChannels == 10) IDerror = true;
165
166 for(unsigned int i=0; i<RD->RHeader->NBoards; i++) {//board loop
167 for(unsigned int j=0; j<RD->RHeader->NChips; j++) {//chip loop
168 for(unsigned int k=0; k<RD->RHeader->NChannels; k++) {//channel loop
169
170 //for DRS2 data (requires the correct pixelmap!!!
171 //if( ( (i==0 || i==1) && (j<=1) && (k<=7) ) || ( (i==2) && (j==0) && (k<=3) ) ) {
172
173 //for DRS4 data (requires the correct pixelmap!!!
174 if( ( (i==0 || i==1) && (j<=3) && (k==0 || k==2 || k==4 || k==6) ) || ( (i==2) && (j==0) && (k==0 || k==2 || k==4 || k==6) ) ) {
175
176 //get module, superpixel and pixel number from pixel name
177 std::string pixelname = PixMap->DRS_to_Pixel(i,j,k);
178 if (pixelname == "") {
179 IDerror = true;
180 continue;
181 }
182 char pixelname_copy[256];
183 memset(pixelname_copy,'\0',256);
184 pixelname.copy(pixelname_copy, 256);
185
186 char delim[] = "-";
187 char *buffer = NULL;
188 int module = -1;
189 int superpixel = -1;
190 int pixel = -1;
191
192 buffer = strtok(pixelname_copy, delim);
193 module = atoi(buffer);
194 buffer = strtok(NULL, delim);
195 superpixel = atoi(buffer);
196 buffer = strtok(NULL, delim);
197 pixel = atoi(buffer);
198
199 //usual M0 mapping
200 //int binx = 5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2));
201 //int biny = 5-(((superpixel-1)%3)*2)-(int((pixel-1)/2));
202
203 //M0 upside down
204 int binx = 5-(5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2)));
205 int biny = 5-(5-(((superpixel-1)%3)*2)-(int((pixel-1)/2)));
206
207 //search maximum sample amplitude within user specified window
208 //start bin is always smaller than stop bin (taken care of by updated ranges)
209 int StartBin = (int)(M0Start->value());
210 int StopBin = (int)(M0Stop->value());
211
212 for(int l=StartBin; l<=StopBin; l++){
213
214 float sample = *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) +
215 i*RD->RHeader->NChips*RD->RHeader->NChannels*RD->RHeader->Samples+
216 j*RD->RHeader->NChannels*RD->RHeader->Samples+
217 k*RD->RHeader->Samples+
218 l)*RD->BStruct[i].ScaleFactor;
219
220 if (fabs(sample) > z[binx][biny]) {
221 z[binx][biny]=fabs(sample);
222 }
223
224 }//sample loop
225 }//only M0 data
226 }//channel loop
227 }//chip loop
228 }//board loop
229
230 if(IDerror){
231 QMessageBox::warning(this, "ddd Message","Sorry! The M0 display is not available for this data file because of a pixel ID mismatch.",QMessageBox::Ok);
232 if(M0Window->isVisible()) M0Window->hide();
233 M0Display->setEnabled(false);
234 return;
235 }
236
237 //fill data to M0 display (event based)
238 Signal2D->setData(SpectrogramDataM0(z));
239 Graph2D->axisWidget(QwtPlot::yRight)->setColorMap(Signal2D->data().range(),Signal2D->colorMap());
240 Graph2D->setAxisScale(QwtPlot::yRight,Signal2D->data().range().minValue(),Signal2D->data().range().maxValue() );
241 Graph2D->replot();
242
243 //update ranges for start and stop bin to avoid startbin > stopbin
244 M0Start->setRange(0, M0Stop->value());
245 M0Stop->setRange(M0Start->value(),(RD->RHeader->Samples)-1);
246}
247
248// +++ Open sub window handling the socket interface +++
249void ddd::OpenSocketWindow() {
250
251 if(SocketWindow->isVisible()) SocketWindow->hide();
252 else SocketWindow->show();
253}
254
255// +++ Open sub window for M0 Display +++
256void ddd::OpenM0Window() {
257
258 if(M0Window->isVisible()) M0Window->hide();
259 else M0Window->show();
260}
261
262
263// +++ Acquire data through socket (acquire botton only available if socket exists) +++
264void ddd::GetSignalFromSocket() {
265 char Command[MAX_COM_SIZE];
266
267 GetButton->setEnabled(false);
268 WaitForData = true;
269 sprintf(Command, "read %d %d %d restart", BoardNo->value(), ChipNo->value(), ChannelNo->value());
270 Socket->write(Command);
271}
272
273// Quit application when clicking close button on window
274void ddd::closeEvent(QCloseEvent *) {
275 qApp->quit();
276}
277
278// +++ Connecting or disconnecting from client +++
279void ddd::MakeConnection() {
280
281 if(Socket->state() == QAbstractSocket::ConnectedState) {
282 ManualDisconnect = true;
283 Socket->disconnectFromHost();
284 }
285 else {
286 if (RD->IsFileOpen() && QMessageBox::question(this, "ddd Request","Connecting will close current data file. Proceed?",
287 QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes) return;
288
289 PhysPipeAction->setChecked(false);
290
291 Socket->connectToHost(IPAddress->text(),Port->value());
292 Connect->setEnabled(false); // While waiting for connection, button not available
293 Socket->waitForConnected(SOCKET_TIMEOUT);
294 Connect->setEnabled(true);
295 if(Socket->state() != QAbstractSocket::ConnectedState)
296 QMessageBox::warning(this, "ddd Message","Could not connect to host.",QMessageBox::Ok);
297 else {
298 Connect->setText("Disconnect");
299 ConnectAction->setText("Disconnect");
300 Port->setEnabled(false);
301 IPAddress->setEnabled(false);
302 Command->setEnabled(true);
303 GetButton->setEnabled(true);
304 ManualDisconnect = false;
305
306 FilenameBox->clear();
307 CloseDatafile();
308
309 M0Window->hide();
310 M0Display->setEnabled(false);
311
312 ChannelNo->setEnabled(true);
313 ChipNo->setEnabled(true);
314 BoardNo->setEnabled(true);
315 PixelID->setEnabled(true);
316 ChannelNo->setRange(0, 65535);
317 ChipNo->setRange(0, 65535);
318 BoardNo->setRange(0, 65535);
319
320 TabWidget->setTabEnabled(1,false);
321 TabWidget->setTabEnabled(2,false);
322
323 RunHeaderDisplay->clear();
324 EventHeaderDisplay->clear();
325 Signal->hide();
326 }
327 }
328}
329
330// +++ Send command to socket (command button available only if socket existing) +++
331void ddd::SendToSocket() {
332 Socket->write(Command->text().toAscii());
333 Command->clear();
334}
335
336// +++ Read data from socket and display +++
337void ddd::ReadFromSocket() {
338 // Check if channel data expected and error message arrived
339 QByteArray Data = Socket->readAll();
340 if (WaitForData && Data.contains("Error")) {
341 WaitForData = false;
342 GetButton->setEnabled(true);
343 QMessageBox::warning(this, "ddd Message","Could not read waveform data from socket.",QMessageBox::Ok);
344 return;
345 }
346
347 // Check if channel data were transmitted, if yes and waiting for data, extract and plot them
348 SocketOutput->insertPlainText(Data);
349 QString Text = SocketOutput->document()->toPlainText().trimmed();
350 if (WaitForData && Text.endsWith(QLatin1String("==END=="))) {
351 // Extract text between ==START== and ==END==
352 QByteArray Data = Text.mid(Text.lastIndexOf("==START==")+9, Text.length() - Text.lastIndexOf("==START==")-16).toAscii();
353
354 char *NextNumber = strtok(Data.data()," "); // Number of entries that follow
355 int Count=0, NumberOfEntries = atoi(NextNumber);
356 double *x = new double [NumberOfEntries];
357 double *y = new double [NumberOfEntries];
358
359 // Convert all entries (separated by a whitespace) to numbers
360 while((NextNumber=strtok(NULL, " "))!=NULL && Count<NumberOfEntries)
361 *(y+Count++) = atof(NextNumber);
362 if (Count==NumberOfEntries && NextNumber!=0)
363 QMessageBox::warning(this, "ddd Message","Found too many numbers in data block, truncated.",QMessageBox::Ok);
364 // Apply sampling frequency and scaling factor
365 for(int i=2; i<Count; i++) {
366 x[i] = (i-2) / y[0];
367 y[i] = y[i] * y[1];
368 }
369 if(NumberOfEntries>2) {
370 Signal->setData(x+2, y+2, NumberOfEntries-2); // Copies data, arrays can be deleted afterwards
371 Signal->show();
372 Zoomer->setZoomBase(Signal->boundingRect());
373 Graph->setAxisTitle(QwtPlot::xBottom, "Time from trigger (ns)");
374 }
375 delete[] x; delete[] y;
376
377 if(ContinuousBox->isChecked()) {
378 usleep(100000); // Wait to limit maximum update rate
379 GetSignalFromSocket();
380 }
381 else {
382 WaitForData = false;
383 GetButton->setEnabled(true);
384 }
385 }
386}
387
388// +++ Reset graph axes to autoscale when fully unzoomed +++
389void ddd::HandleZoom(const QwtDoubleRect &) {
390 if(Zoomer->zoomRectIndex() == 0) {
391 Graph->setAxisAutoScale(QwtPlot::xBottom);
392 Graph->setAxisAutoScale(QwtPlot::yLeft);
393 }
394}
395
396// +++ Disconnect from socket +++
397void ddd::GotDisconnected() {
398 Connect->setText("Connect");
399 ConnectAction->setText("Connect");
400 Port->setEnabled(true);
401 IPAddress->setEnabled(true);
402 Command->setEnabled(false);
403
404 GetButton->setEnabled(false);
405 ChannelNo->setEnabled(false);
406 ChipNo->setEnabled(false);
407 BoardNo->setEnabled(false);
408 PixelID->setEnabled(false);
409 Signal->hide();
410 TabWidget->setTabEnabled(1, true);
411 TabWidget->setTabEnabled(2, true);
412
413 SocketOutput->clear();
414 if(!ManualDisconnect) QMessageBox::warning(this, "ddd Message","Socket disconnected, maybe host gone.",QMessageBox::Ok);
415}
416
417// +++ Translate pixel ID +++
418void ddd::TranslatePixelID() {
419
420 int Board = PixMap->Pixel_to_DRSboard(PixelID->text().toStdString());
421 int Chip = PixMap->Pixel_to_DRSchip(PixelID->text().toStdString());
422 int Channel = PixMap->Pixel_to_DRSchannel(PixelID->text().toStdString());
423
424 if(Board >= BoardNo->minimum() && Board <= BoardNo->maximum() &&
425 Chip >= ChipNo->minimum() && Chip <= ChipNo->maximum() &&
426 Channel>= ChannelNo->minimum() && Channel <= ChannelNo->maximum()) {
427 BoardNo->setValue(Board);
428 ChipNo->setValue(Chip);
429 ChannelNo->setValue(Channel);
430 }
431 else if(Board==999999999) QMessageBox::warning(this, "ddd Message","Pixel ID unknown.",QMessageBox::Ok);
432 else QMessageBox::warning(this, "ddd Message","Pixel ID out of current range.",QMessageBox::Ok);
433}
434
435
436//------------------------------------------------------------------
437//**************************** All menus ***************************
438//------------------------------------------------------------------
439
440void ddd::MenuSave() {
441 QString Filename = QFileDialog::getSaveFileName(this,
442 "Filename of image", "/home/ogrimm/ddd", "Image files (*.bmp *.jpg *.png *.ppm *.tiff *.xbm *.xpm);;All files (*)");
443 if (Filename.length()>0) {
444 QPixmap Pixmap = QPixmap::grabWidget(Graph);
445 if(!Pixmap.save(Filename)) {
446 QMessageBox::warning(this, "ddd Message","Could not write image file.",QMessageBox::Ok);
447 remove(Filename.toAscii().data());
448 }
449 }
450}
451
452void ddd::MenuPrint() {
453 QPrinter *Printer = new QPrinter;
454 QPrintDialog *PrintDialog = new QPrintDialog(Printer, this);
455 if (PrintDialog->exec() == QDialog::Accepted) {
456 QPainter Painter(Printer);
457 QPixmap Pixmap = QPixmap::grabWidget(Graph);
458 Painter.drawPixmap(0, 0, Pixmap);
459 }
460 delete Printer; delete PrintDialog;
461}
462
463void ddd::MenuSaveASCII() {
464 QString Filename = QFileDialog::getSaveFileName(this,
465 "Filename", ".", "Text files (*.txt *.ascii *.asc);;All files (*)");
466 if (Filename.length()>0) {
467 QFile File(Filename);
468 if (File.open(QFile::WriteOnly | QIODevice::Text | QFile::Truncate)) {
469 QTextStream Stream(&File);
470 for (int i=0; i<Signal->dataSize(); i++) {
471 Stream << Signal->y(i) << endl;
472 }
473 }
474 else {
475 QMessageBox::warning(this, "ddd Message","Could not write data to file.",QMessageBox::Ok);
476 }
477 }
478}
479
480void ddd::MenuHelp() {
481 QMessageBox Message;
482 Message.setText("The DRS Data Display program can be used for two purposes:\n\n"
483 "1. Reading and displaying the content of a raw data file written by the drsdaq program\n"
484 "2. Acquiring and displaying online data from a running drsdaq program via the socket interface\n\n"
485 "With an established socket connection, displaying of raw data files is disabled.\n\n"
486 "Navigation in signal display: Left mouse button to zoom, right to unzoom fully, middle to unzoom one level. Left mouse button plus shift key to pan.\n"
487 "When unzoomed, the axes are rescaled automatically.");
488
489 Message.setWindowTitle("ddd Help");
490 Message.exec();
491}
492
493void ddd::MenuAbout() {
494 QMessageBox::about(this, "ddd About","DRS Data Display\n\n"
495 "Written by Oliver Grimm, IPP, ETH Zurich\n"
496 "Event display by Quirin Weitzel.\n\n"
497 "This version compiled "__DATE__".\n\n"
498 "Graphical user interface implemented with Qt.\n"
499 "Bug reports to oliver.grimm@phys.ethz.ch.");
500}
Note: See TracBrowser for help on using the repository browser.