643286b5615d01d39f65d0754c8c9c57bec25521
[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 #include <TFile.h>
10
11 using namespace std;
12
13 /* parameters:
14  
15  0 - connect directly to reconstruction socket
16  1 - connect to Storage Manager and ask for last event
17  2 - connect to alieventserver, receive in thread and use in main thread
18  
19  */
20
21 // global variables:
22 AliESDEvent *currentEvent[2];
23 TTree *currentTree[2];
24 TMutex myMutex;
25 int eventInUse=1;
26 int writingToEventIndex=0;
27 bool isNewEventAvaliable=false;
28 //-----------------
29
30 static void* GetNextEvent(void*);
31
32 int main(int argc, char **argv)
33 {
34     if(argc<2)
35     {
36         cout<<"Usage: alifakedisplay <mode>"<<endl;
37         cout<<"mode:"<<endl;
38         cout<<"0 - connect directly to reconstruction socket"<<endl;
39         cout<<"1 - connect to Storage Manager and ask for last event"<<endl;
40         cout<<"3 - connect to Storage Manager, download list of perm events and ask for them in loop"<<endl;
41         return 0;
42     }
43     
44     storageSockets socket;
45     AliStorageEventManager *manager = AliStorageEventManager::GetEventManagerInstance();
46     AliESDEvent *event;
47     struct recPointsStruct *files;
48     
49     if(atoi(argv[1])==0)
50     {
51         manager->CreateSocket(EVENTS_SERVER_SUB);
52         manager->CreateSocket(ITS_POINTS_SUB);
53         cout<<"Socket created"<<endl;
54         while(1)
55         {
56             cout<<"waiting for event..."<<flush;
57             event = manager->GetEvent(EVENTS_SERVER_SUB);
58             
59             if(event)
60             {
61                 cout<<"Received event. Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
62                 
63                 cout<<event->GetPeriodNumber()<<endl;
64                 cout<<event->GetOrbitNumber()<<endl;
65                 cout<<event->GetBunchCrossNumber()<<endl;
66                 for(int i=0;i<100;i++)
67                 {
68                     if(strcmp(event->GetESDRun()->GetTriggerClass(i),"")){
69                         cout<<event->GetESDRun()->GetTriggerClass(i)<<endl;}
70                 }
71                 delete event;event=0;
72             }
73             else
74             {
75                 cout<<"NO EVENT"<<endl;
76             }
77             
78             // Receiving RecPoints:
79             /*
80
81             files = manager->GetFiles(ITS_POINTS_SUB);
82
83         
84             cout<<"Received file."<<endl;
85
86             //file->Write("ITS.RecPoints.root",kOverwrite);
87             if(files->files[0]){files->files[0]->SaveAs("ITS.RecPoints.root","recreate");}
88             if(files->files[1]){files->files[1]->SaveAs("TOF.RecPoints.root","recreate");}
89             if(files->files[2]){files->files[2]->SaveAs("galice.root","recreate");}
90
91             for(int i=0;i<10;i++)
92             {
93             if(files->files[i])
94             {
95             files->files[i]->Close();
96             delete files->files[i];files->files[i]=0;
97             }
98             }   
99             sleep(2);
100             */
101         }
102     }
103     else if(atoi(argv[1])==1)
104     {
105         socket = SERVER_COMMUNICATION_REQ;
106         manager->CreateSocket(socket);
107         while(1)
108         {
109             struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
110             requestMessage->messageType = REQUEST_GET_LAST_EVENT;
111             
112             manager->Send(requestMessage,socket);
113             event = manager->GetEvent(socket);
114             if(event)
115             {
116                 cout<<"Last event - Run:"<<event->GetRunNumber()<<"\t event:"<<event->GetEventNumberInFile()<<endl;
117                 
118                 delete event;
119             }
120             else
121             {
122                 cout<<"NO EVENT"<<endl;
123             }
124             sleep(1);
125         }
126     }
127     else if(atoi(argv[1])==2)
128     {
129         TThread *getEventThread = new TThread("getEventThread",GetNextEvent,nullptr);
130         getEventThread->Run();
131         
132         int counter=0;
133         
134         while(1)
135         {
136             counter++;
137             if(isNewEventAvaliable)
138             {
139                 cout<<"new event"<<endl;
140                 myMutex.Lock();
141                 if(writingToEventIndex == 0) eventInUse = 0;
142                 else if(writingToEventIndex == 1) eventInUse = 1;
143                 cout<<"Using:"<<eventInUse<<endl;
144                 
145                 if(currentEvent[eventInUse])
146                 {
147                     if(currentEvent[eventInUse]->GetRunNumber() >= 0)
148                     {
149                         cout<<"CURRENT EVENT:"<<currentEvent[eventInUse]->GetEventNumberInFile()<<endl;
150                     }
151                 }
152                 isNewEventAvaliable = false;
153                 myMutex.UnLock();
154             }
155             else{cout<<"No new event is avaliable."<<endl;}
156             
157             sleep(2);
158         }
159     }
160     else if(atoi(argv[1])==3)
161     {
162         socket = SERVER_COMMUNICATION_REQ;
163         manager->CreateSocket(socket);
164         
165         
166         struct listRequestStruct list;
167         list.runNumber[0]=0;
168         list.runNumber[1]=999999;
169         list.eventNumber[0]=0;
170         list.eventNumber[1]=999999;
171         list.marked[0]=1;
172         list.marked[1]=1;
173         list.multiplicity[0]=1;
174         list.multiplicity[1]=999999;
175         strcpy(list.system[0],"p-p");
176         strcpy(list.system[1],"A-A");
177         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
178         requestMessage->messageType = REQUEST_LIST_EVENTS;
179         requestMessage->list = list;
180         
181         manager->Send(requestMessage,socket);
182         vector<serverListStruct> receivedList = manager->GetServerListVector(socket,3000);
183         
184         cout<<"FAKE DISPLAY -- received list of marked events"<<endl;
185         
186         for(int i=0;i<receivedList.size();i++){
187             cout<<"ev:"<<receivedList[i].eventNumber<<endl;
188         }
189
190         int iter=0;
191         while(1)
192         {
193             if(iter<receivedList.size())
194             {
195                 struct eventStruct mark;
196                 mark.runNumber = receivedList[iter].runNumber;
197                 mark.eventNumber = receivedList[iter].eventNumber;
198                 
199                 requestMessage->messageType = REQUEST_GET_EVENT;
200                 requestMessage->event = mark;
201                 
202                 manager->Send(requestMessage,socket);
203                 event = manager->GetEvent(socket);
204              
205                 if(event)
206                 {
207                     cout<<"i:"<<iter<<"\tevent:"<<event->GetEventNumberInFile()<<endl;
208                 }
209                 else
210                 {
211                     cout<<"Received NO event"<<endl;
212                 }
213                 iter++;
214                 sleep(1);
215             }
216             else{iter=0;}
217         }
218     }
219     
220     return 0;
221 }
222
223 void* GetNextEvent(void*)
224 {
225     AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
226     eventManager->CreateSocket(EVENTS_SERVER_SUB);
227     
228     currentEvent[0]=0;
229     currentEvent[1]=0;
230     currentTree[0]=0;
231     currentTree[1]=0;
232     AliESDEvent *tmpEvent;
233     
234     while(1)
235     {
236         //if(tmpEvent){delete tmpEvent;tmpEvent=0;}
237         tmpEvent = eventManager->GetEvent(EVENTS_SERVER_SUB);
238         
239         if(tmpEvent)
240         {
241             if(tmpEvent->GetRunNumber()>=0)
242             {
243                 myMutex.Lock();
244                 if(eventInUse == 0){writingToEventIndex = 1;}
245                 else if(eventInUse == 1){writingToEventIndex = 0;}
246                 
247                 if(currentEvent[writingToEventIndex])
248                 {
249                     delete currentEvent[writingToEventIndex];
250                     currentEvent[writingToEventIndex]=0;
251                 }
252                 currentEvent[writingToEventIndex] = tmpEvent;
253                 isNewEventAvaliable = true;
254                 myMutex.UnLock();
255             }
256         }       
257     }
258 }