source: tools/ddd/Functions.cpp@ 169

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