source: tools/ddd/Functions.cpp@ 63

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