source: tools/ddd/Functions.cpp@ 71

Last change on this file since 71 was 66, checked in by qweitzel, 15 years ago
MO event display improved: start- and stopbin spinbox added
File size: 14.7 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 int binx = 5-(int((superpixel-1)/3)*2)-(int((pixel%4)/2));
158 int biny = 5-(((superpixel-1)%3)*2)-(int((pixel-1)/2));
159
160 //search maximum sample amplitude within user specified window
161 //start bin is always smaller than stop bin (taken care of by updated ranges)
162 int StartBin = (int)(M0Start->value());
163 int StopBin = (int)(M0Stop->value());
164
165 for(int l=StartBin; l<=StopBin; l++){
166
167 float sample = *((short *) (RD->Data + RD->RHeader->NBoards*RD->RHeader->NChips*sizeof(int)) +
168 i*RD->RHeader->NChips*RD->RHeader->NChannels*RD->RHeader->Samples+
169 j*RD->RHeader->NChannels*RD->RHeader->Samples+
170 k*RD->RHeader->Samples+
171 l)*RD->BStruct[i].ScaleFactor;
172
173 if (sample > z[binx][biny]) {
174 z[binx][biny]=sample;
175 }
176
177 }//sample loop
178
179 }//only M0 data
180
181 }//channel loop
182 }//chip loop
183 }//board loop
184
185 //fill data to M0 display (event based)
186 Signal2D->setData(SpectrogramDataM0(z));
187 Graph2D->axisWidget(QwtPlot::yRight)->setColorMap(Signal2D->data().range(),Signal2D->colorMap());
188 Graph2D->setAxisScale(QwtPlot::yRight,Signal2D->data().range().minValue(),Signal2D->data().range().maxValue() );
189 Graph2D->replot();
190 //Signal2D->show();
191
192 //update ranges for start and stop bin to avoid startbin > stopbin
193 M0Start->setRange(0, M0Stop->value());
194 M0Stop->setRange(M0Start->value(),(RD->RHeader->Samples)-1);
195
196 delete[] x; delete[] y;
197
198 }
199 if(Tmpfile!=NULL) fclose(Tmpfile);
200}
201
202// +++ Open sub window handling the socket interface +++
203void ddd::OpenSocketWindow() {
204
205 if(SocketWindow->isVisible()) SocketWindow->hide();
206 else SocketWindow->show();
207}
208
209// +++ Open sub window for M0 Display +++
210void ddd::OpenM0Window() {
211
212 if(M0Window->isVisible()) M0Window->hide();
213 else M0Window->show();
214}
215
216
217// +++ Acquire data through socket (acquire botton only available if socket exists) +++
218void ddd::GetSignalFromSocket() {
219 char Command[MAX_COM_SIZE];
220
221 GetButton->setEnabled(false);
222 WaitForData = true;
223 sprintf(Command, "read %d %d %d restart", BoardNo->value(), ChannelNo->value()/10, ChannelNo->value()%10);
224 Socket->write(Command);
225}
226
227// Quit application when clicking close button on window
228void ddd::closeEvent(QCloseEvent *) {
229 qApp->quit();
230}
231
232// +++ Connecting or disconnecting from client +++
233void ddd::MakeConnection() {
234
235 if(Socket->state() == QAbstractSocket::ConnectedState) {
236 ManualDisconnect = true;
237 Socket->disconnectFromHost();
238 }
239 else {
240 if (RD->IsFileOpen() && QMessageBox::question(this, "ddd Request","Connecting will close current data file. Proceed?",
241 QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes) return;
242 Socket->connectToHost(IPAddress->text(),Port->value());
243 Connect->setEnabled(false); // While waiting for connection, button not available
244 Socket->waitForConnected(SOCKET_TIMEOUT);
245 Connect->setEnabled(true);
246 if(Socket->state() != QAbstractSocket::ConnectedState)
247 QMessageBox::warning(this, "ddd Message","Could not connect to host.",QMessageBox::Ok);
248 else {
249 Connect->setText("Disconnect");
250 ConnectAction->setText("Disconnect");
251 Port->setEnabled(false);
252 IPAddress->setEnabled(false);
253 Command->setEnabled(true);
254 GetButton->setEnabled(true);
255 ManualDisconnect = false;
256
257 OpenAction->setEnabled(false);
258 FilenameBox->setEnabled(false);
259 LoadButton->setEnabled(false);
260 FilenameBox->clear();
261 CloseDatafile();
262
263 ChannelNo->setEnabled(true);
264 BoardNo->setEnabled(true);
265 PixelID->setEnabled(true);
266 ChannelNo->setRange(0, 65535);
267 BoardNo->setRange(0, 65535);
268
269 TabWidget->setTabEnabled(1,false);
270 TabWidget->setTabEnabled(2,false);
271
272 RunHeaderDisplay->clear();
273 EventHeaderDisplay->clear();
274 Signal->hide();
275 }
276 }
277}
278
279// +++ Send command to socket (command button available only if socket existing) +++
280void ddd::SendToSocket() {
281 Socket->write(Command->text().toAscii());
282 Command->clear();
283}
284
285// +++ Read data from socket and display +++
286void ddd::ReadFromSocket() {
287 // Check if channel data expected and error message arrived
288 QByteArray Data = Socket->readAll();
289 if (WaitForData && Data.contains("Error")) {
290 WaitForData = false;
291 GetButton->setEnabled(true);
292 QMessageBox::warning(this, "ddd Message","Could not read waveform data from socket.",QMessageBox::Ok);
293 return;
294 }
295
296 // Check if channel data were transmitted, if yes and waiting for data, extract and plot them
297 SocketOutput->insertPlainText(Data);
298 QString Text = SocketOutput->document()->toPlainText().trimmed();
299 if (WaitForData && Text.endsWith(QLatin1String("==END=="))) {
300 // Extract text between ==START== and ==END==
301 QByteArray Data=Text.mid(Text.lastIndexOf("==START==")+9,Text.length()-Text.lastIndexOf("==START==")-16).toAscii();
302
303 char *NextNumber = strtok(Data.data()," "); // Number of entries that follow
304 int Count=0, NumberOfEntries = atoi(NextNumber);
305 double *x = new double [NumberOfEntries];
306 double *y = new double [NumberOfEntries];
307
308 // Convert all entries (separated by a whitespace) to numbers
309 while((NextNumber=strtok(NULL, " "))!=NULL && Count<NumberOfEntries)
310 *(y+Count++) = atof(NextNumber);
311 if (Count==NumberOfEntries && NextNumber!=0)
312 QMessageBox::warning(this, "ddd Message","Found too many numbers in data block, truncated.",QMessageBox::Ok);
313 // Apply sampling frequency and scaling factor
314 for(int i=2; i<Count; i++) {
315 x[i] = (i-2) / y[0];
316 y[i] = y[i] * y[1];
317 }
318 if(NumberOfEntries>2) {
319 Signal->setData(x+2, y+2, NumberOfEntries-2); // Copies data, arrays can be deleted afterwards
320 Signal->show();
321 Zoomer->setZoomBase(Signal->boundingRect());
322 }
323 delete[] x; delete[] y;
324
325 if(ContinuousBox->isChecked()) {
326 usleep(100000); // Wait to limit maximum update rate
327 GetSignalFromSocket();
328 }
329 else {
330 WaitForData = false;
331 GetButton->setEnabled(true);
332 }
333 }
334}
335
336// +++ Disconnect from socket +++
337void ddd::GotDisconnected() {
338 Connect->setText("Connect");
339 ConnectAction->setText("Connect");
340 Port->setEnabled(true);
341 IPAddress->setEnabled(true);
342 Command->setEnabled(false);
343
344 GetButton->setEnabled(false);
345 FilenameBox->setEnabled(true);
346 OpenAction->setEnabled(true);
347 LoadButton->setEnabled(true);
348 ChannelNo->setEnabled(false);
349 BoardNo->setEnabled(false);
350 PixelID->setEnabled(false);
351 Signal->hide();
352 TabWidget->setTabEnabled(1, true);
353 TabWidget->setTabEnabled(2, true);
354
355 SocketOutput->clear();
356 if(!ManualDisconnect) QMessageBox::warning(this, "ddd Message","Socket disconnected, maybe host gone.",QMessageBox::Ok);
357}
358
359// +++ Translate pixel ID +++
360void ddd::TranslatePixelID() {
361
362 int Board = PixMap->Pixel_to_DRSboard(PixelID->text().toStdString());
363 int Chip = PixMap->Pixel_to_DRSchip(PixelID->text().toStdString());
364 int Channel = PixMap->Pixel_to_DRSchannel(PixelID->text().toStdString());
365
366 if(Board>=BoardNo->minimum() && Board<=BoardNo->maximum() &&
367 (Chip*10+Channel)>=ChannelNo->minimum() && (Chip*10+Channel)<=ChannelNo->maximum()) {
368 BoardNo->setValue(Board);
369 ChannelNo->setValue(Chip*10+Channel);
370 }
371 else if(Board==999999999) QMessageBox::warning(this, "ddd Message","Pixel ID unknown.",QMessageBox::Ok);
372 else QMessageBox::warning(this, "ddd Message","Pixel ID out of current range.",QMessageBox::Ok);
373}
374
375
376//------------------------------------------------------------------
377//**************************** All menus ***************************
378//------------------------------------------------------------------
379
380void ddd::MenuSave() {
381 QString Filename = QFileDialog::getSaveFileName(this,
382 "Filename of image", "/home/ogrimm/ddd", "Image files (*.bmp *.jpg *.png *.ppm *.tiff *.xbm *.xpm);;All files (*)");
383 if (Filename.length()>0) {
384 QPixmap Pixmap = QPixmap::grabWidget(Graph);
385 if(!Pixmap.save(Filename)) {
386 QMessageBox::warning(this, "ddd Message","Could not write image file.",QMessageBox::Ok);
387 remove(Filename.toAscii().data());
388 }
389 }
390}
391
392void ddd::MenuPrint() {
393 QPrinter *Printer = new QPrinter;
394 QPrintDialog *PrintDialog = new QPrintDialog(Printer, this);
395 if (PrintDialog->exec() == QDialog::Accepted) {
396 QPainter Painter(Printer);
397 QPixmap Pixmap = QPixmap::grabWidget(Graph);
398 Painter.drawPixmap(0, 0, Pixmap);
399 }
400 delete Printer; delete PrintDialog;
401}
402
403void ddd::MenuHelp() {
404 QMessageBox Message;
405 Message.setText("The DRS Data Display program can be used for two purposes:\n\n"
406 "1. Reading and displaying the content of a raw data file written by the drsdaq program\n"
407 "2. Acquiring and displaying online data from a running drsdaq program via the socket interface\n\n"
408 "With an established socket connection, displaying of raw data files is disabled.");
409 Message.setWindowTitle("ddd Help");
410 Message.exec();
411}
412
413void ddd::MenuAbout() {
414 QMessageBox::about(this, "ddd About","DRS Data Display\n\n"
415 "Written by Oliver Grimm, IPP, ETH Zurich\n"
416 "This version compiled "__DATE__".\n\n"
417 "Graphical user interface implemented with Qt.\n"
418 "Bug reports to oliver.grimm@phys.ethz.ch.");
419}
Note: See TracBrowser for help on using the repository browser.