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