corrections to obey coding conventions
[u/mrichter/AliRoot.git] / MONITOR / AliMonitorControl.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  This class provides a graphical user interface for the monitor process   //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24
25 #include "AliMonitorControl.h"
26 #include "AliMonitorHisto.h"
27 #include <TGNumberEntry.h>
28 #include <TGTextView.h>
29 #include <TGMsgBox.h>
30 #include <TSystem.h>
31 #include <TSocket.h>
32 #include <TGFrame.h>
33 #include <TGMenu.h>
34 #include <TGButton.h>
35 #include <TGLabel.h>
36 #include <TGTextEntry.h>
37 #include <TTimer.h>
38 #include "AliMonitorProcess.h"
39
40
41 ClassImp(AliMonitorControl) 
42
43
44
45 //_____________________________________________________________________________
46 AliMonitorControl::AliMonitorBufferDlg::AliMonitorBufferDlg(Int_t& size, 
47                                                             TGFrame* main) :
48   AliMonitorDialog(main, 250, 80), fSize(size)
49 {
50 // create a dialog for setting the size of the buffer for monitor histos
51
52   fBufferLayout = new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 2, 5, 2, 2);
53   fBufferLabel = new TGLabel(fFrame, "size of histogram buffer:");
54   fFrame->AddFrame(fBufferLabel, fBufferLayout);
55   fBufferEntry = new TGNumberEntry(fFrame, size, 2, -1,
56                                    TGNumberFormat::kNESInteger);
57   fBufferEntry->SetLimits(TGNumberFormat::kNELLimitMinMax, 1, 99);
58   fFrame->AddFrame(fBufferEntry, fBufferLayout);
59
60   fSize = -1;
61
62   fMain->SetWindowName("Buffer Size");
63   fMain->MapSubwindows();
64   fMain->Layout();
65   gClient->WaitFor(fMain);
66 }
67
68 //_____________________________________________________________________________
69 AliMonitorControl::AliMonitorBufferDlg::~AliMonitorBufferDlg()
70 {
71 // clean up
72
73   delete fBufferLabel;
74   delete fBufferLayout;
75   delete fBufferEntry;
76 }
77
78 //_____________________________________________________________________________
79 void AliMonitorControl::AliMonitorBufferDlg::OnOkClicked()
80 {
81   fSize = fBufferEntry->GetIntNumber();
82 }
83
84
85 //_____________________________________________________________________________
86 AliMonitorControl::AliMonitorClientsDlg::AliMonitorClientsDlg(TObjArray* clients, 
87                                                               TGFrame* main) :
88   AliMonitorDialog(main, 450, 300, kFALSE)
89 {
90 // create a dialog to display the list of clients
91
92   delete fFrameLayout;
93   fFrameLayout = new TGLayoutHints(kLHintsCenterX | kLHintsTop, 
94                                    10, 10, 15, 15);
95   ((TGFrameElement*)(fMain->GetList()->First()))->fLayout = fFrameLayout;
96   fClientsLayout = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
97                                      0, 0, 0, 0);
98   fClients = new TGTextView(fFrame, 420, 230);
99   fFrame->AddFrame(fClients, fClientsLayout);
100
101   char line[256];
102   const char* format = "%-30s%-20s%-8d";
103   sprintf(line, format, "name", "address", 0);
104   strncpy(&line[50], "port", 4);
105   fClients->AddLine(line);
106   for (Int_t i = 0; i < (Int_t) strlen(line); i++) line[i] = '-';
107   fClients->AddLine(line);
108
109   for (Int_t iClient = 0; iClient < clients->GetEntriesFast(); iClient++) {
110     TSocket* socket = (TSocket*) clients->At(iClient);
111     if (!socket) continue;
112     TInetAddress adr = socket->GetInetAddress();
113     sprintf(line, format,
114             adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
115     fClients->AddLine(line);
116   }
117
118   fMain->SetWindowName("List of Clients");
119   fMain->MapSubwindows();
120   fMain->Layout();
121   gClient->WaitFor(fMain);
122 }
123
124 //_____________________________________________________________________________
125 AliMonitorControl::AliMonitorClientsDlg::~AliMonitorClientsDlg()
126 {
127 // clean up
128
129   delete fClientsLayout;
130   delete fClients;
131 }
132
133
134 // constants for menu entries
135 enum {kMenuFileExit, kMenuFileAbort,
136       kMenuOptBuffer, kMenuOptClients,
137       kMenuHelpDoc, kMenuHelpAbout
138 };
139
140
141 //_____________________________________________________________________________
142 AliMonitorControl::AliMonitorControl(AliMonitorProcess* process)
143 {
144 // initialize the monitoring control window
145
146   fMonitorProcess = process;
147
148
149   // colors
150   gClient->GetColorByName("lightblue", fColorStatus);
151   gClient->GetColorByName("green", fColorStart);
152   gClient->GetColorByName("red", fColorStop);
153
154   // main window
155   fMain = new TGMainFrame(gClient->GetRoot(), 380, 200);
156
157   // menu bar
158   fMenuBarLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,
159                                       0, 0, 1, 1);
160   fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
161   fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop | kLHintsRight);
162
163   fMenuFile = new TGPopupMenu(gClient->GetRoot());
164   fMenuFile->AddEntry("E&xit", kMenuFileExit);
165   fMenuFile->AddEntry("&Abort", kMenuFileAbort);
166   fMenuFile->DisableEntry(kMenuFileAbort);
167   fMenuFile->Connect("Activated(Int_t)", "AliMonitorControl", this,
168                      "HandleMenu(Int_t)");
169
170   fMenuOptions = new TGPopupMenu(gClient->GetRoot());
171   fMenuOptions->AddEntry("&Histogram buffer...", kMenuOptBuffer);
172   fMenuOptions->AddEntry("List of &Clients...", kMenuOptClients);
173   fMenuOptions->Connect("Activated(Int_t)", "AliMonitorControl", this,
174                         "HandleMenu(Int_t)");
175
176   fMenuHelp = new TGPopupMenu(gClient->GetRoot());
177   fMenuHelp->AddEntry("&Documentation...", kMenuHelpDoc);
178   fMenuHelp->AddEntry("A&bout...", kMenuHelpAbout);
179   fMenuHelp->Connect("Activated(Int_t)", "AliMonitorControl", this,
180                      "HandleMenu(Int_t)");
181
182   fMenuBar = new TGMenuBar(fMain, 1, 1, kHorizontalFrame);
183   fMenuBar->AddPopup("&File", fMenuFile, fMenuBarItemLayout);
184   fMenuBar->AddPopup("&Options", fMenuOptions, fMenuBarItemLayout);
185   fMenuBar->AddPopup("&Help", fMenuHelp, fMenuBarHelpLayout);
186
187   fMain->AddFrame(fMenuBar, fMenuBarLayout);
188
189
190   // status frame
191   fFrameLayout = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 
192                                    2, 2, 2, 2);
193   fFrame = new TGVerticalFrame(fMain, 0, 0, kChildFrame | kSunkenFrame);
194   fMain->AddFrame(fFrame, fFrameLayout);
195
196   fStatusLayout = new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 10, 2, 2, 2);
197   fStatusFrameLayout = new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 2, 2, 2, 2);
198
199
200   // run and event number
201   fStatus1Frame = new TGHorizontalFrame(fFrame, 0, 0);
202   fFrame->AddFrame(fStatus1Frame, fStatusFrameLayout);
203
204   fRunNumberLabel = new TGLabel(fStatus1Frame, "current run:");
205   fStatus1Frame->AddFrame(fRunNumberLabel, fStatusLayout);
206   fRunNumber = new TGTextEntry(fStatus1Frame, "-");
207   fRunNumber->Resize(60, fRunNumber->GetDefaultHeight());
208   fRunNumber->SetAlignment(kTextRight);
209   fRunNumber->SetEnabled(kFALSE);
210   fRunNumber->SetBackgroundColor(fColorStatus);
211   fStatus1Frame->AddFrame(fRunNumber, fStatusLayout);
212
213   fEventNumberLabel = new TGLabel(fStatus1Frame, 
214                                   "  event:");
215   fStatus1Frame->AddFrame(fEventNumberLabel, fStatusLayout);
216   fEventNumber = new TGTextEntry(fStatus1Frame, "-/-/-");
217   fEventNumber->Resize(100, fEventNumber->GetDefaultHeight());
218   fEventNumber->SetAlignment(kTextRight);
219   fEventNumber->SetEnabled(kFALSE);
220   fEventNumber->SetBackgroundColor(fColorStatus);
221   fStatus1Frame->AddFrame(fEventNumber, fStatusLayout);
222
223
224   // process status
225   fStatus2Frame = new TGHorizontalFrame(fFrame, 0, 0);
226   fFrame->AddFrame(fStatus2Frame, fStatusFrameLayout);
227
228   fStatusLabel = new TGLabel(fStatus2Frame, "current status:");
229   fStatus2Frame->AddFrame(fStatusLabel, fStatusLayout);
230   fStatus = new TGTextEntry(fStatus2Frame, "stopped");
231   fStatus->Resize(250, fStatus->GetDefaultHeight());
232   fStatus->SetAlignment(kTextLeft);
233   fStatus->SetEnabled(kFALSE);
234   fStatus->SetBackgroundColor(fColorStatus);
235   fStatus2Frame->AddFrame(fStatus, fStatusLayout);
236
237
238   // process status
239   fStatus3Frame = new TGHorizontalFrame(fFrame, 0, 0);
240   fFrame->AddFrame(fStatus3Frame, fStatusFrameLayout);
241
242   fEventsLabel = new TGLabel(fStatus3Frame, "monitored events:");
243   fStatus3Frame->AddFrame(fEventsLabel, fStatusLayout);
244   fEvents = new TGTextEntry(fStatus3Frame, "-");
245   fEvents->Resize(60, fEvents->GetDefaultHeight());
246   fEvents->SetAlignment(kTextRight);
247   fEvents->SetEnabled(kFALSE);
248   fEvents->SetBackgroundColor(fColorStatus);
249   fStatus3Frame->AddFrame(fEvents, fStatusLayout);
250
251   fClientsLabel = new TGLabel(fStatus3Frame, " number of clients:");
252   fStatus3Frame->AddFrame(fClientsLabel, fStatusLayout);
253   fClients = new TGTextEntry(fStatus3Frame, "-");
254   fClients->Resize(40, fClients->GetDefaultHeight());
255   fClients->SetAlignment(kTextRight);
256   fClients->SetEnabled(kFALSE);
257   fClients->SetBackgroundColor(fColorStatus);
258   fStatus3Frame->AddFrame(fClients, fStatusLayout);
259
260
261   // buttons
262   fButtonFrameLayout = new TGLayoutHints(kLHintsExpandX | kLHintsBottom, 
263                                          50, 50, 10, 10);
264   fButtonFrame = new TGHorizontalFrame(fMain, 0, 0);
265   fMain->AddFrame(fButtonFrame, fButtonFrameLayout);
266
267   fButtonLayout = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 
268                                     2, 2, 2, 2);
269   fResetButton = new TGTextButton(fButtonFrame, " &Reset ", 1);
270   fResetButton->Connect("Clicked()", "AliMonitorControl", this, "DoReset()");
271   fButtonFrame->AddFrame(fResetButton, fButtonLayout);
272
273   fStartStopButton = new TGTextButton(fButtonFrame, " &Start ", 2);
274   fStartStopButton->SetBackgroundColor(0x00FF00);
275   fStartStopButton->Connect("Clicked()", "AliMonitorControl", this, 
276                             "DoStartStop()");
277   fButtonFrame->AddFrame(fStartStopButton, fButtonLayout);
278   fStartButtonStatus = kTRUE;
279
280
281   // main window
282   fMain->SetWindowName("Monitor Process Control");
283   fMain->DontCallClose();
284   fMain->SetWMSize(fMain->GetWidth(), fMain->GetHeight());
285   fMain->SetWMSizeHints(fMain->GetWidth(), fMain->GetHeight(), 
286                         fMain->GetWidth(), fMain->GetHeight(), 0, 0);
287   fMain->SetWMPosition(100, 100);
288   fMain->MapSubwindows();
289   fMain->Layout();
290   fMain->MapWindow();
291
292
293   fTerminating = kFALSE;
294
295   fTimer = new TTimer(this, 10, kTRUE);
296   fTimer->TurnOn();
297 }
298
299 //_____________________________________________________________________________
300 AliMonitorControl::AliMonitorControl(const AliMonitorControl& control) :
301   TObject(control)
302 {
303   Fatal("AliMonitorControl", "copy constructor not implemented");
304 }
305
306 //_____________________________________________________________________________
307 AliMonitorControl& AliMonitorControl::operator = (const AliMonitorControl& 
308                                                   /*control*/)
309 {
310   Fatal("operator =", "assignment operator not implemented");
311   return *this;
312 }
313
314 //_____________________________________________________________________________
315 AliMonitorControl::~AliMonitorControl()
316 {
317 // clean up
318
319   delete fMenuBarLayout;
320   delete fMenuBarItemLayout;
321   delete fMenuBarHelpLayout;
322   delete fMenuFile;
323   delete fMenuOptions;
324   delete fMenuHelp;
325   delete fMenuBar;
326
327   delete fFrameLayout;
328   delete fFrame;
329   delete fStatusLayout;
330   delete fStatusFrameLayout;
331
332   delete fStatus1Frame;
333   delete fRunNumberLabel;
334   delete fRunNumber;
335   delete fEventNumberLabel;
336   delete fEventNumber;
337
338   delete fStatus2Frame;
339   delete fStatusLabel;
340   delete fStatus;
341
342   delete fStatus3Frame;
343   delete fEventsLabel;
344   delete fEvents;
345   delete fClientsLabel;
346   delete fClients;
347
348   delete fButtonFrameLayout;
349   delete fButtonFrame;
350   delete fButtonLayout;
351   delete fResetButton;
352   delete fStartStopButton;
353
354   delete fTimer;
355 }
356
357
358 //_____________________________________________________________________________
359 void AliMonitorControl::HandleMenu(Int_t id)
360 {
361 // called when a menu item was selected
362
363   switch (id) {
364
365   case kMenuFileExit: {
366     Int_t result;
367     new TGMsgBox(gClient->GetRoot(), fMain, "Exit", 
368                  "Do you really want to terminate the monitor process?", 
369                  kMBIconQuestion, kMBYes | kMBNo, &result);
370     if (result == kMBYes) {
371       fMenuFile->EnableEntry(kMenuFileAbort);
372       if (fMonitorProcess->IsStopped()) exit(0);
373       fMonitorProcess->Stop();
374       fTerminating = kTRUE;
375     }
376     break;
377   }
378
379   case kMenuFileAbort:
380     exit(1);
381
382   case kMenuOptBuffer: {
383     Int_t size = AliMonitorHisto::GetNHistosMax();
384     new AliMonitorBufferDlg(size, fMain);
385     if (size < 0) break;
386     AliMonitorHisto::SetNHistosMax(size);
387     break;
388   }
389
390   case kMenuOptClients: {
391     new AliMonitorClientsDlg(fMonitorProcess->GetListOfClients(), fMain);
392     break;
393   }
394
395   case kMenuHelpAbout: {
396     Int_t result;
397     char text[256];
398     sprintf(text, "AliMonitorControl $Revision$\nrunning\n"
399             "AliMonitorProcess %s", AliMonitorProcess::GetRevision());
400     new TGMsgBox(gClient->GetRoot(), fMain, 
401                  "About", text, kMBIconAsterisk, kMBOk, &result);
402     break;
403   }
404
405   default: {
406     Int_t result;
407     new TGMsgBox(gClient->GetRoot(), fMain, 
408                  "AliMonitorControl", "not yet implemented", 
409                  kMBIconExclamation, kMBOk, &result);
410   }
411   }
412 }
413
414 //_____________________________________________________________________________
415 void AliMonitorControl::DoReset()
416 {
417 // called when the reset button was clicked
418
419   Int_t result;
420   new TGMsgBox(gClient->GetRoot(), fMain, "Reset", 
421                "Do you really want to reset the monitor histograms?", 
422                kMBIconQuestion, kMBYes | kMBNo, &result);
423   if (result == kMBYes) {
424     fMonitorProcess->Reset();
425   }
426 }
427
428 //_____________________________________________________________________________
429 void AliMonitorControl::DoStartStop()
430 {
431 // called when the start/stop button was clicked
432
433   if (fStartButtonStatus) {
434     if (fMonitorProcess->GetStatus() == AliMonitorProcess::kStopped) {
435       fStartStopButton->SetBackgroundColor(fColorStop);
436       fStartStopButton->SetText(" &Stop ");
437       fStartButtonStatus = kFALSE;
438       fMonitorProcess->Run();
439       fStartStopButton->SetBackgroundColor(fColorStart);
440       fStartStopButton->SetText(" &Start ");
441       fStartButtonStatus = kTRUE;
442     }
443
444   } else {
445     if (fMonitorProcess->GetStatus() != AliMonitorProcess::kStopped) {
446       fMonitorProcess->Stop();
447     }
448   }
449 }
450
451
452
453 //_____________________________________________________________________________
454 Bool_t AliMonitorControl::HandleTimer(TTimer* timer)
455 {
456 // update the displayed information
457
458   timer->TurnOff();
459   if (fTerminating && fMonitorProcess->IsStopped()) exit(0); 
460   UpdateStatus();
461   gSystem->ProcessEvents();
462   timer->TurnOn();
463
464   return kFALSE;
465 }
466
467
468 //_____________________________________________________________________________
469 void AliMonitorControl::UpdateStatus()
470 {
471 // update the displayed status information
472
473   char text[256];
474
475   if (fMonitorProcess->IsStopped()) {
476     fRunNumber->SetText("-");
477     fEventNumber->SetText("-/-/-");
478     fEvents->SetText("-");
479     fClients->SetText("-");
480
481   } else {
482     sprintf(text, "%d", fMonitorProcess->GetRunNumber());
483     fRunNumber->SetText(text);
484     sprintf(text, "%d/%d/%d", fMonitorProcess->GetEventPeriodNumber(),
485             fMonitorProcess->GetEventOrbitNumber(),
486             fMonitorProcess->GetEventBunchNumber());
487     fEventNumber->SetText(text);
488     sprintf(text, "%d", fMonitorProcess->GetNumberOfEvents());
489     fEvents->SetText(text);
490     sprintf(text, "%d", fMonitorProcess->GetNumberOfClients());
491     fClients->SetText(text);
492   }
493
494   const char* status = NULL;
495   switch (fMonitorProcess->GetStatus()) {
496   case AliMonitorProcess::kStopped: 
497     status = "stopped"; break;
498   case AliMonitorProcess::kWaiting: 
499     status = "waiting for new data"; break;
500   case AliMonitorProcess::kReading: 
501     status = "reading raw data"; break;
502   case AliMonitorProcess::kRecTPC: 
503     status = "running TPC reconstruction"; break;
504   case AliMonitorProcess::kRecITS: 
505     status = "running ITS reconstruction"; break;
506   case AliMonitorProcess::kRecV0s: 
507     status = "running V0 reconstruction"; break;
508   case AliMonitorProcess::kRecHLT: 
509     status = "running HLT reconstruction"; break;
510   case AliMonitorProcess::kFilling: 
511     status = "filling monitor histograms"; break;
512   case AliMonitorProcess::kUpdating: 
513     status = "updating monitor histograms"; break;
514   case AliMonitorProcess::kWriting: 
515     status = "writing monitor histograms"; break;
516   case AliMonitorProcess::kResetting: 
517     status = "resetting monitor histograms"; break;
518   case AliMonitorProcess::kConnecting: 
519     status = "checking for new clients"; break;
520   case AliMonitorProcess::kBroadcasting: 
521     status = "broadcasting monitor histograms"; break;
522   }
523
524   if (fMonitorProcess->WillStop()) {
525     sprintf(text, "stopping... (%s)", status);
526   } else {
527     sprintf(text, "%s", status);
528   }
529   if (strcmp(text, fStatus->GetText()) != 0) {
530     fStatus->SetText(text);
531   }
532
533   /*
534   if (fStartButtonStatus != fMonitorProcess->IsStopped()) {
535     fStartButtonStatus = fMonitorProcess->IsStopped();
536     if (fStartButtonStatus) {
537       fStartStopButton->SetBackgroundColor(fColorStart);
538       fStartStopButton->SetText(" &Start ");
539     } else {
540       fStartStopButton->SetBackgroundColor(fColorStop);
541       fStartStopButton->SetText(" &Stop ");
542     }
543   }
544   */
545 }
546