source: tools/ddd/Functions.cpp@ 59

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