source: tools/ddd/Functions.cpp@ 104

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