source: tools/ddd/Functions.cpp@ 80

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