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