- added new task for pi0 reconstruction using purely calorimeter clusters
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageAdministratorPanel.cxx
1 #include "AliStorageAdministratorPanel.h"
2 #include "AliStorageAdministratorPanelMarkEvent.h"
3 #include "AliStorageAdministratorPanelListEvents.h"
4 #include "AliStorageAdministratorPanelSetStorageParams.h"
5 #include "AliESDEvent.h"
6
7 #include <fstream>
8 #include <iostream>
9
10 #include <TGMenu.h>
11
12 using namespace std;
13
14 ClassImp(AliStorageAdministratorPanel);
15
16 #define WINDOWS_WIDTH 500
17 #define WINDOWS_HEIGHT 350
18
19 enum TOOLBUTTON{
20         TOOLBUTTON_START=1,
21         TOOLBUTTON_STOP,
22         TOOLBUTTON_PREFERENCES,
23         TOOLBUTTON_EXIT         
24 };
25
26 enum MENUBAR{
27         MENUBAR_CLIENT_SET_PARAMS=1,
28         MENUBAR_SERVER_LIST_EVENTS,
29         MENUBAR_SERVER_MARK_EVENT,
30         MENUBAR_SERVER_GET_EVENT,
31         MENUBAR_SERVER_GET_NEXT_EVENT,
32         MENUBAR_SERVER_GET_LAST_EVENT
33 };
34
35 enum FRAME{
36         CLIENT_FRAME=1,
37         SERVER_FRAME
38 };
39
40 AliStorageAdministratorPanel::AliStorageAdministratorPanel() :
41         TGMainFrame(gClient->GetRoot(), 400, 400),
42         fPanelQuited(false),
43         fConnectionLabel(0),
44         fDataLabel(0),
45         fSavingLabel(0),
46         fCurrentSizeLabel(0),
47         fMaxSizeLabel(0),
48         fMaxOccupationLabel(0),
49         fRemoveEventsLabel(0),
50         fEventsInChunkLabel(0),
51         fMaxStorageSize(0),
52         fMaxOccupation(0),
53         fRemoveEvents(0),
54         fEventsInChunk(0),
55         fCommunicationThread(0),
56         fCommunicationSocket(CLIENT_COMMUNICATION_REQ),
57         fServerSocket(SERVER_COMMUNICATION_REQ),
58         fEventManager(0)
59 {
60         InitWindow();
61     
62         //create event manager
63         fEventManager = AliStorageEventManager::GetEventManagerInstance();
64         fEventManager->CreateSocket(fServerSocket);
65         fEventManager->CreateSocket(fCommunicationSocket);
66
67         // start communication with client thread
68         fCommunicationThread = new TThread("fCommunicationThread",
69                                        Dispatch,(void*)this);
70         fCommunicationThread->Run();
71 }
72
73 AliStorageAdministratorPanel::~AliStorageAdministratorPanel()
74 {
75         cout<<"ADMIN -- AliStorageAdministratorPanel descructor called";
76         cout<<" --- OK"<<endl;
77 }
78
79 void AliStorageAdministratorPanel::CheckStateHandle()//ask client about its state
80 {
81         while(!fPanelQuited)
82         {
83                 CheckClientState(REQUEST_CONNECTION);
84                 sleep(1);
85                 CheckClientState(REQUEST_RECEIVING);
86                 sleep(1);
87                 CheckClientState(REQUEST_SAVING);
88                 sleep(1);
89                 CheckClientState(REQUEST_CURRENT_SIZE);
90                 sleep(1);
91                 CheckClientState(REQUEST_GET_PARAMS);
92                 sleep(1);
93         }
94 }
95
96 void AliStorageAdministratorPanel::CheckClientState(int option)
97 {
98         struct clientRequestStruct *request = new struct clientRequestStruct;
99         request->messageType = option;
100         if(!fEventManager->Send(request,fCommunicationSocket,10000))
101         {
102                 SetLabel(fConnectionLabel,STATUS_DOWN);
103                 SetLabel(fDataLabel,STATUS_DOWN);
104                 SetLabel(fSavingLabel,STATUS_DOWN);
105                 cout<<"ADMIN -- CLIENT IS DOWN"<<endl;
106                 return;
107         }
108
109         long response = -1;
110         struct clientRequestStruct *responseParams = NULL;
111         
112         if(option == REQUEST_GET_PARAMS)
113         {
114                 responseParams = fEventManager->GetClientStruct(fCommunicationSocket);
115         }
116         else
117         {
118                 response = fEventManager->GetLong(fCommunicationSocket);
119         }
120         switch(option)
121         {
122         case REQUEST_CONNECTION:
123                 SetLabel(fConnectionLabel,response);
124                 break;
125         case REQUEST_RECEIVING:
126                 SetLabel(fDataLabel,response);
127                 break;
128         case REQUEST_SAVING:
129                 SetLabel(fSavingLabel,response);
130                 break;
131         case REQUEST_CURRENT_SIZE:
132                 SetLabelValue(fCurrentSizeLabel,response,1);
133                 break;
134         case REQUEST_GET_PARAMS:
135                 fMaxStorageSize = responseParams->maxStorageSize;
136                 fMaxOccupation = responseParams->maxOccupation;
137                 fRemoveEvents = responseParams->removeEvents;
138                 fEventsInChunk = responseParams->eventsInChunk;
139                 
140                 SetLabelValue(fMaxSizeLabel,fMaxStorageSize,1);
141                 SetLabelValue(fMaxOccupationLabel,fMaxOccupation,2);
142                 SetLabelValue(fRemoveEventsLabel,fRemoveEvents,2);
143                 SetLabelValue(fEventsInChunkLabel,fEventsInChunk,3);
144                 break;
145         default:break;  
146         }
147 }
148
149 void AliStorageAdministratorPanel::InitWindow()
150 {
151         SetCleanup(kDeepCleanup);
152         // add main manubar on top of the window
153         SetupFixedMenuBar();
154         AddFrame(new TGHorizontal3DLine(this),
155                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
156         // add toolbar with pictured buttons
157         /*SetupToolbar();
158         AddFrame(new TGHorizontal3DLine(this),
159                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
160         // add dockable toolbar
161         SetupDockableMenuBar();
162         AddFrame(new TGHorizontal3DLine(this),
163                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
164         */
165         
166         // add frame with two segments for client and server
167         SetupThreadsFrame();
168         SetWindowName("ALICE Storage Manager");
169         MapSubwindows();
170         Resize(WINDOWS_WIDTH,WINDOWS_HEIGHT);
171         MapWindow();
172 }
173
174 void AliStorageAdministratorPanel::SetupThreadsFrame()
175 {
176         // create frame for both client and server
177         TGHorizontalFrame *threadsFrame = new TGHorizontalFrame(this);
178
179         //create client frame and add components to it
180         TGCompositeFrame *clientThreadFrame = new TGCompositeFrame(threadsFrame,CLIENT_FRAME);
181         fConnectionLabel = new TGLabel(clientThreadFrame,"======Waiting======");
182         fDataLabel = new TGLabel(clientThreadFrame,"======Waiting======");
183         fSavingLabel = new TGLabel(clientThreadFrame,"======Waiting======");
184         fCurrentSizeLabel = new TGLabel(clientThreadFrame,"=========Waiting=========");
185         fMaxSizeLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
186         fMaxOccupationLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
187         fRemoveEventsLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
188         fEventsInChunkLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
189         
190         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
191                                                 "ALICE Storage Client"),
192                                     new TGLayoutHints(kLHintsCenterX));
193
194         //connection label
195         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
196                                                 "Connection:"),
197                                     new TGLayoutHints(kLHintsCenterX));
198         clientThreadFrame->AddFrame(fConnectionLabel,
199                                     new TGLayoutHints(kLHintsCenterX));
200
201         //data receiving label
202         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
203                                                 "Receiving data:"),
204                                     new TGLayoutHints(kLHintsCenterX));
205         clientThreadFrame->AddFrame(fDataLabel,
206                                     new TGLayoutHints(kLHintsCenterX));
207
208         //saving label
209         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
210                                                 "Saving:"),
211                                     new TGLayoutHints(kLHintsCenterX));
212         clientThreadFrame->AddFrame(fSavingLabel,
213                                     new TGLayoutHints(kLHintsCenterX));
214
215         //current size label
216         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
217                                                 "Current storage size:"),
218                                     new TGLayoutHints(kLHintsCenterX));
219         clientThreadFrame->AddFrame(fCurrentSizeLabel,
220                                     new TGLayoutHints(kLHintsCenterX));
221
222         //max size label
223         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
224                                                 "Max storage size:"),
225                                     new TGLayoutHints(kLHintsCenterX));
226         clientThreadFrame->AddFrame(fMaxSizeLabel,
227                                     new TGLayoutHints(kLHintsCenterX));
228
229         //max occupation label
230         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
231                                                 "Max occupation percent:"),
232                                     new TGLayoutHints(kLHintsCenterX));
233         clientThreadFrame->AddFrame(fMaxOccupationLabel,
234                                     new TGLayoutHints(kLHintsCenterX));
235
236         //remove events label
237         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
238                                                 "Remove events percentage:"),
239                                     new TGLayoutHints(kLHintsCenterX));
240         clientThreadFrame->AddFrame(fRemoveEventsLabel,
241                                     new TGLayoutHints(kLHintsCenterX));
242
243         //events in chunk label
244         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
245                                                 "Number of events in chunk:"),
246                                     new TGLayoutHints(kLHintsCenterX));
247         clientThreadFrame->AddFrame(fEventsInChunkLabel,
248                                     new TGLayoutHints(kLHintsCenterX));
249
250         //create server frame and add components to it
251         TGCompositeFrame *serverThreadFrame = new TGCompositeFrame(threadsFrame,SERVER_FRAME);
252         serverThreadFrame->AddFrame(new TGLabel(serverThreadFrame,
253                                                  "ALICE Storage Server"),
254                                      new TGLayoutHints(kLHintsCenterX));
255
256         //add client and server frames to threads frame
257         threadsFrame->AddFrame(clientThreadFrame,
258                                 new TGLayoutHints(kLHintsLeft | kLHintsExpandX));
259         threadsFrame->AddFrame(new TGVertical3DLine(threadsFrame),
260                  new TGLayoutHints(kLHintsNormal | kLHintsExpandY));
261         threadsFrame->AddFrame(serverThreadFrame,
262                                 new TGLayoutHints(kLHintsRight | kLHintsExpandX));
263
264         //add threads frame to main window
265         AddFrame(threadsFrame,new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
266 }
267
268 void AliStorageAdministratorPanel::SetupFixedMenuBar()
269 {
270         //create popup menu for client
271         TGPopupMenu *clientPopup = new TGPopupMenu(fClient->GetRoot());
272         clientPopup->AddEntry("&Set storage params",MENUBAR_CLIENT_SET_PARAMS);
273         clientPopup->Associate(this);
274
275         //create popup menu for server
276         TGPopupMenu *serverPopup = new TGPopupMenu(fClient->GetRoot());
277         serverPopup->AddEntry("&Get Events List",MENUBAR_SERVER_LIST_EVENTS);
278         serverPopup->AddEntry("&Mark Event",MENUBAR_SERVER_MARK_EVENT);
279         serverPopup->AddEntry("&Get Event (test)",MENUBAR_SERVER_GET_EVENT);
280         serverPopup->AddEntry("&Get Next Event (test)",MENUBAR_SERVER_GET_NEXT_EVENT);
281         serverPopup->AddEntry("&Get Last Event (test)",MENUBAR_SERVER_GET_LAST_EVENT);
282         serverPopup->Associate(this);
283
284         //create menubar
285         TGMenuBar *menuBar = new TGMenuBar(this,1,1,kHorizontalFrame);
286         menuBar->AddPopup("&Client",clientPopup,
287                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
288         menuBar->AddPopup("&Server", serverPopup,
289                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
290
291         AddFrame(menuBar,new TGLayoutHints(kLHintsTop | kLHintsExpandX));
292 }
293
294 //handle GUI actions
295 Bool_t AliStorageAdministratorPanel::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
296 {
297         switch (GET_MSG(msg))
298         {
299
300         case kC_COMMAND:
301                 switch (GET_SUBMSG(msg))
302                 {
303                 case kCM_BUTTON://when button is pressed
304                         break;
305                 case kCM_MENUSELECT://when mouse is over menu entry
306                         break;
307                 case kCM_MENU://when menu entry was clicked
308                         switch (parm1)
309                         {
310                         case MENUBAR_CLIENT_SET_PARAMS:onClientSetParams();break;
311                         case MENUBAR_SERVER_LIST_EVENTS:onServerListEvents();break;
312                         case MENUBAR_SERVER_MARK_EVENT:onServerMarkEvent();break;
313                         case MENUBAR_SERVER_GET_EVENT:onServerGetEvent();break;
314                         case MENUBAR_SERVER_GET_NEXT_EVENT:onServerGetNextEvent();break;
315                         case MENUBAR_SERVER_GET_LAST_EVENT:onServerGetLastEvent();break;
316                         default:break;
317                         }
318                         break;
319                 default:break;
320                 }
321                 break;
322         default:break;
323         }
324
325         return false;
326 }
327
328 void AliStorageAdministratorPanel::onServerListEvents()
329 {
330         AliStorageAdministratorPanelListEvents *listEventsWindow =
331                 AliStorageAdministratorPanelListEvents::GetInstance();
332         if(listEventsWindow){cout<<"List events window created"<<endl;}
333 }
334
335 void AliStorageAdministratorPanel::onServerMarkEvent()
336 {
337         AliStorageAdministratorPanelMarkEvent *markEventWindow =
338                 AliStorageAdministratorPanelMarkEvent::GetInstance();
339         if(markEventWindow){cout<<"Mark event window created"<<endl;}
340 }
341
342 void AliStorageAdministratorPanel::onClientSetParams()
343 {
344         AliStorageAdministratorPanelSetStorageParams *setParamsWindow =
345                 AliStorageAdministratorPanelSetStorageParams::GetInstance();
346
347         setParamsWindow->Setup(fCommunicationSocket,
348                                fMaxStorageSize/1000000,
349                                fMaxOccupation,
350                                fRemoveEvents,
351                                fEventsInChunk);
352 }
353
354 void AliStorageAdministratorPanel::onServerGetEvent()
355 {
356         //this method is just for tests
357         int runNumber=197669;
358         int eventNumber=168;
359         
360         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
361         struct eventStruct mark;
362         mark.runNumber = runNumber;
363         mark.eventNumber = eventNumber;
364         requestMessage->messageType = REQUEST_GET_EVENT;
365         requestMessage->event = mark;
366
367         fEventManager->Send(requestMessage,fServerSocket);
368         AliESDEvent *resultEvent = fEventManager->GetEvent(fServerSocket);
369         
370         if(resultEvent)
371         {
372                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
373         }
374         else
375         {
376                 cout<<"ADMIN -- received no event"<<endl;
377         }
378 }
379
380
381 void AliStorageAdministratorPanel::onServerGetNextEvent()
382 {
383         //this method is just for tests
384         int runNumber=197669;
385         int eventNumber=33;
386         
387         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
388         struct eventStruct mark;
389         mark.runNumber = runNumber;
390         mark.eventNumber = eventNumber;
391         requestMessage->messageType = REQUEST_GET_NEXT_EVENT;
392         requestMessage->event = mark;
393
394         fEventManager->Send(requestMessage,fServerSocket);
395         AliESDEvent* resultEvent= fEventManager->GetEvent(fServerSocket);       
396         if(resultEvent)
397         {
398                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
399         }
400         else
401         {
402                 cout<<"ADMIN -- received no event"<<endl;
403         }
404 }
405
406
407 void AliStorageAdministratorPanel::onServerGetLastEvent()
408 {       
409         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
410         requestMessage->messageType = REQUEST_GET_LAST_EVENT;
411
412         fEventManager->Send(requestMessage,fServerSocket);
413         AliESDEvent* resultEvent= fEventManager->GetEvent(fServerSocket);
414
415         if(resultEvent)
416         {
417                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
418         }
419         else
420         {
421                 cout<<"ADMIN -- received no event"<<endl;
422         }
423 }
424
425 void AliStorageAdministratorPanel::onExit()
426 {
427         cout<<"ADMIN -- Quiting ALICE Storage Admin Panel"<<endl;
428         fPanelQuited=true;
429         gSystem->ExitLoop();
430 }
431
432 void AliStorageAdministratorPanel::CloseWindow(){onExit();}
433
434
435 //methods to change labels:
436 void AliStorageAdministratorPanel::SetLabel(TGLabel *label, int option)
437 {
438         switch(option)
439         {
440         case STATUS_WAITING:
441                 label->SetText("Waiting");
442                 label->SetTextColor(kBlack);
443                 break;
444         case STATUS_OK:
445                 label->SetText("OK");
446                 label->SetTextColor(kBlack);
447                 break;
448         case STATUS_ERROR:
449                 label->SetText("ERROR");
450                 label->SetTextColor(kBlack);
451                 break;
452         case STATUS_DOWN:
453                 label->SetText("CLIENT IS DOWN");
454                 label->SetTextColor(kRed);
455                 break;
456         default:break;
457         }
458         gClient->HandleInput();
459 }
460
461 void AliStorageAdministratorPanel::SetLabelValue(TGLabel *label, long value,int option)
462 {
463         // options:
464         // 1 - MB
465         // 2 - percentage
466         // 3 - number
467
468         switch(option)
469         {
470         case 1:
471                 label->SetText(Form("%lu B (%.2f MB)",value,(float)value/(1000.*1000.)));
472                 break;
473         case 2:
474                 label->SetText(Form("%d %%",(int)value));
475                 break;
476         case 3:
477                 label->SetText(Form("%d",(int)value));
478                 break;
479         default:break;
480         }
481         label->SetTextColor(kBlack);
482         gClient->HandleInput();
483 }
484 //other methods that can be useful later
485
486 /*
487 void AliStorageAdministratorPanel::SetupToolbar()
488 {
489         TGToolBar* toolBar = new TGToolBar(this);
490         toolBar->AddButton(this,new TGPictureButton(toolBar,
491                                                      Form("%s/MONITOR/icons/start.png",
492                                                      gSystem->Getenv("ALICE_ROOT")),
493                                                      TOOLBUTTON_START ) );
494         toolBar->AddButton(this, new TGPictureButton(toolBar,
495                                                       Form("%s/MONITOR/icons/stop.png",
496                                                       gSystem->Getenv("ALICE_ROOT")),
497                                                       TOOLBUTTON_STOP) ); 
498         
499         AddFrame(toolBar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
500 }
501
502 void AliStorageAdministratorPanel::SetupDockableMenuBar()
503 {
504         TGDockableFrame *menuDock = new TGDockableFrame(this);
505         AddFrame(menuDock,new TGLayoutHints(kLHintsExpandX,0,0,1,0));
506         menuDock->SetWindowName("ALICE Storage Dockable  MenuBar");
507         menuDock->EnableUndock(kTRUE);
508         menuDock->EnableHide(kTRUE);
509
510         TGPopupMenu *popup1 = new TGPopupMenu(fClient->GetRoot());
511         popup1->AddEntry("&Check client state",MENUBAR_1_OPTION_1);
512         popup1->AddSeparator();
513         popup1->AddEntry("&BBB",MENUBAR_1_OPTION_2);
514         popup1->AddEntry("&CCC",MENUBAR_1_OPTION_3);
515
516         popup1->DisableEntry(MENUBAR_1_OPTION_2);
517         popup1->Associate(this);
518
519         
520         TGMenuBar *menuBar = new TGMenuBar(menuDock,1,1,kHorizontalFrame);
521         menuBar->AddPopup("&File",
522                           popup1,
523                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
524
525         menuDock->AddFrame(menuBar,
526                            new TGLayoutHints(kLHintsTop | kLHintsExpandX));
527                            }*/