Fixed SOR-EOR handling in reconstruction server.
[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 myMutex;
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                 cout<<"Socket created"<<endl;
51                 while(1)
52                 {
53                   cout<<"waiting for event..."<<flush;
54                         event = manager->GetEvent(socket);
55                         
56                         if(event)
57                         {
58                                 cout<<"Received event. Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
59
60                                 cout<<event->GetPeriodNumber()<<endl;
61                                 cout<<event->GetOrbitNumber()<<endl;
62                                 cout<<event->GetBunchCrossNumber()<<endl;
63                                 for(int i=0;i<100;i++)
64                                 {
65                                         if(strcmp(event->GetESDRun()->GetTriggerClass(i),"")){
66                                                 cout<<event->GetESDRun()->GetTriggerClass(i)<<endl;}
67                                 }
68                                 delete event;
69                         }
70                         else
71                         {
72                                 cout<<"NO EVENT"<<endl;
73                         }
74                 }
75         }
76         else if(atoi(argv[1])==1)
77         {
78                 socket = SERVER_COMMUNICATION_REQ;
79                 manager->CreateSocket(socket);
80                 while(1)
81                 {
82                         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
83                         requestMessage->messageType = REQUEST_GET_LAST_EVENT;
84
85                         manager->Send(requestMessage,socket);                   
86                         event = manager->GetEvent(socket);
87                         if(event)
88                         {
89                                 cout<<"Last event - Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
90                 
91                                 delete event;
92                         }
93                         else
94                         {
95                                 cout<<"NO EVENT"<<endl;
96                         }
97                         sleep(1);
98                 }
99         }
100         else if(atoi(argv[1])==2)
101         {
102           TThread *getEventThread = new TThread("getEventThread",GetNextEvent,nullptr);
103           getEventThread->Run();
104
105           int counter=0;
106           
107           while(1)
108           {
109             counter++;
110             if(isNewEventAvaliable)
111             {
112               cout<<"new event"<<endl;
113               myMutex.Lock();
114               if(writingToEventIndex == 0) eventInUse = 0;
115               else if(writingToEventIndex == 1) eventInUse = 1;
116               cout<<"Using:"<<eventInUse<<endl;
117
118               if(currentEvent[eventInUse])
119               {
120                 if(currentEvent[eventInUse]->GetRunNumber() >= 0)
121                 {
122                   cout<<"CURRENT EVENT:"<<currentEvent[eventInUse]->GetEventNumberInFile()<<endl;
123                 }
124               }
125               isNewEventAvaliable = false;
126               myMutex.UnLock();
127             }
128             else{cout<<"No new event is avaliable."<<endl;}
129
130             sleep(2);
131           }
132         }
133
134         return 0;
135 }
136
137 void* GetNextEvent(void*)
138 {
139   AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
140   eventManager->CreateSocket(EVENTS_SERVER_SUB);
141
142   currentEvent[0]=0;
143   currentEvent[1]=0;
144   currentTree[0]=0;
145   currentTree[1]=0;
146   AliESDEvent *tmpEvent;
147   
148   while(1)
149   {
150     //if(tmpEvent){delete tmpEvent;tmpEvent=0;}
151     tmpEvent = eventManager->GetEvent(EVENTS_SERVER_SUB);
152
153     if(tmpEvent)
154     {
155       if(tmpEvent->GetRunNumber()>=0)
156       {
157         myMutex.Lock();
158         if(eventInUse == 0){writingToEventIndex = 1;}
159         else if(eventInUse == 1){writingToEventIndex = 0;}
160
161         if(currentEvent[writingToEventIndex])
162         {
163           delete currentEvent[writingToEventIndex];
164           currentEvent[writingToEventIndex]=0;
165         }
166         currentEvent[writingToEventIndex] = tmpEvent;
167         isNewEventAvaliable = true;
168         myMutex.UnLock();
169       }
170     }   
171   }
172 }