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