Merging STORAGE-dev to master
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / alifakedisplay.cxx
1 #include "AliStorageEventManager.h"
2 #include "AliStorageTypes.h"
3 #include "AliESDEvent.h"
4 #include "AliESDRun.h"
5
6 #include <iostream>
7
8 #include <TThread.h>
9
10 using namespace std;
11
12 /* parameters:
13
14 0 - connect directly to reconstruction socket
15 1 - connect to Storage Manager and ask for last event
16 2 - connect to alieventserver, receive in thread and use in main thread
17
18 */
19
20 // global variables:
21 AliESDEvent *currentEvent[2];
22 TTree *currentTree[2];
23 TMutex mutex;
24 int eventInUse=1;
25 int writingToEventIndex=0;
26 bool isNewEventAvaliable=false;
27 //-----------------
28
29 static void* GetNextEvent(void*);
30
31 int main(int argc, char **argv)
32 {
33         if(argc<2)
34         {
35                 cout<<"Usage: alifakedisplay <mode>"<<endl;
36                 cout<<"mode:"<<endl;
37                 cout<<"0 - connect directly to reconstruction socket"<<endl;
38                 cout<<"1 - connect to Storage Manager and ask for last event"<<endl;
39                 return 0;
40         }
41
42         storageSockets socket; 
43         AliStorageEventManager *manager = AliStorageEventManager::GetEventManagerInstance();
44         AliESDEvent *event;
45         
46         if(atoi(argv[1])==0)
47         {
48                 socket = EVENTS_SERVER_SUB;
49                 manager->CreateSocket(socket);
50                 while(1)
51                 {
52                         event = manager->GetEvent(socket);
53                         if(event)
54                         {
55                                 cout<<"Received event. Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
56
57                                 cout<<event->GetPeriodNumber()<<endl;
58                                 cout<<event->GetOrbitNumber()<<endl;
59                                 cout<<event->GetBunchCrossNumber()<<endl;
60                                 for(int i=0;i<100;i++)
61                                 {
62                                         if(strcmp(event->GetESDRun()->GetTriggerClass(i),"")){
63                                                 cout<<event->GetESDRun()->GetTriggerClass(i)<<endl;}
64                                 }
65                                 delete event;
66                         }
67                         else
68                         {
69                                 cout<<"NO EVENT"<<endl;
70                         }
71                 }
72         }
73         else if(atoi(argv[1])==1)
74         {
75                 socket = SERVER_COMMUNICATION_REQ;
76                 manager->CreateSocket(socket);
77                 while(1)
78                 {
79                         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
80                         requestMessage->messageType = REQUEST_GET_LAST_EVENT;
81
82                         manager->Send(requestMessage,socket);                   
83                         event = manager->GetEvent(socket);
84                         if(event)
85                         {
86                                 cout<<"Last event - Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
87                 
88                                 delete event;
89                         }
90                         else
91                         {
92                                 cout<<"NO EVENT"<<endl;
93                         }
94                         sleep(1);
95                 }
96         }
97         else if(atoi(argv[1])==2)
98         {
99           TThread *getEventThread = new TThread("getEventThread",GetNextEvent,nullptr);
100           getEventThread->Run();
101
102           int counter=0;
103           
104           while(1)
105           {
106             counter++;
107             if(isNewEventAvaliable)
108             {
109               cout<<"new event"<<endl;
110               mutex.Lock();
111               if(writingToEventIndex == 0) eventInUse = 0;
112               else if(writingToEventIndex == 1) eventInUse = 1;
113               cout<<"Using:"<<eventInUse<<endl;
114
115               if(currentEvent[eventInUse])
116               {
117                 if(currentEvent[eventInUse]->GetRunNumber() >= 0)
118                 {
119                   cout<<"CURRENT EVENT:"<<currentEvent[eventInUse]->GetEventNumberInFile()<<endl;
120                 }
121               }
122               isNewEventAvaliable = false;
123               mutex.UnLock();
124             }
125             else{cout<<"No new event is avaliable."<<endl;}
126
127             sleep(2);
128           }
129         }
130
131         return 0;
132 }
133
134 void* GetNextEvent(void*)
135 {
136   AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
137   eventManager->CreateSocket(EVENTS_SERVER_SUB);
138
139   currentEvent[0]=0;
140   currentEvent[1]=0;
141   currentTree[0]=0;
142   currentTree[1]=0;
143   AliESDEvent *tmpEvent;
144   TTree *tmpTree = NULL;
145   
146   while(1)
147   {
148     //if(tmpEvent){delete tmpEvent;tmpEvent=0;}
149     tmpEvent = eventManager->GetEvent(EVENTS_SERVER_SUB,-1,&tmpTree);
150
151     if(tmpEvent)
152     {
153       if(tmpEvent->GetRunNumber()>=0)
154       {
155         mutex.Lock();
156         if(eventInUse == 0){writingToEventIndex = 1;}
157         else if(eventInUse == 1){writingToEventIndex = 0;}
158         cout<<"Writing to:"<<writingToEventIndex<<endl;
159         if(currentEvent[writingToEventIndex])
160         {
161           cout<<"DELETING:"<<currentEvent[writingToEventIndex]<<endl;
162           delete currentEvent[writingToEventIndex];
163           currentEvent[writingToEventIndex]=0;
164           delete currentTree[writingToEventIndex];
165         }
166         currentEvent[writingToEventIndex] = tmpEvent;
167         currentTree[writingToEventIndex] = tmpTree;
168         isNewEventAvaliable = true;
169         mutex.UnLock();
170       }
171     }   
172   }
173 }