Fixes in online reconstruction, Event Display and Storage Manager
[u/mrichter/AliRoot.git] / MONITOR / MONITORzmq / AliStorageDatabase.cxx
1 #include "AliStorageDatabase.h"
2
3 #include <iostream>
4 #include <fstream>
5
6 #include <TSQLRow.h>
7 #include <TSQLResult.h>
8 #include <TThread.h>
9 #include <TSystem.h>
10 #include <TFile.h>
11
12 using namespace std;
13
14 AliStorageDatabase::AliStorageDatabase() :
15         fHost(""),
16         fPort(""),
17         fDatabase(""),
18         fUID(""),
19         fPassword(""),
20         fTable(""),
21         fServer(0),
22         fStoragePath("")
23 {
24     TThread::Lock();
25         ifstream configFile (GetConfigFilePath());
26         if (configFile.is_open())
27         {
28                 string line;
29                 int from,to;
30                 while(configFile.good())
31                 {
32                         getline(configFile,line);
33                         from = line.find("\"")+1;
34                         to = line.find_last_of("\"");
35                         if(line.find("HOST=")==0){
36                                 fHost=line.substr(from,to-from);
37                         }
38                         else if(line.find("PORT=")==0){
39                                 fPort=line.substr(from,to-from);
40                         }
41                         else if(line.find("DATABASE=")==0){
42                                 fDatabase=line.substr(from,to-from);
43                         }
44                         else if(line.find("USER=")==0){
45                                 fUID=line.substr(from,to-from);
46                         }
47                         else if(line.find("PASS=")==0){
48                                 fPassword=line.substr(from,to-from);
49                         }
50                         else if(line.find("TABLE=")==0){
51                                 fTable=line.substr(from,to-from);
52                         }
53                         else if(line.find("STORAGE_PATH=")==0){
54                                 fStoragePath=line.substr(from,to-from);
55                         }
56
57                 }
58                 if(configFile.eof()){configFile.clear();}
59                 configFile.close();
60         }
61         else{cout << "DATABASE -- Unable to open file" <<endl;}
62         TThread::UnLock();
63
64     cout<<"DATABASE -- connecting to server:"<<Form("mysql://%s:%s/%s",fHost.c_str(),fPort.c_str(),fDatabase.c_str())<<fUID.c_str()<<fPassword.c_str()<<endl;
65         fServer = TSQLServer::Connect(Form("mysql://%s:%s/%s",fHost.c_str(),fPort.c_str(),fDatabase.c_str()),fUID.c_str(),fPassword.c_str());
66     cout<<"Connected"<<endl;
67 }
68
69 AliStorageDatabase::~AliStorageDatabase(){
70   if (fServer) {delete fServer;}
71 }
72
73 void AliStorageDatabase::InsertEvent(int runNumber,
74                                      int eventNumber,
75                                      char *system,
76                                      int multiplicity,
77                                      char *filePath)
78 {
79     TSQLResult *res = fServer->Query(Form("select * FROM %s WHERE run_number = %d AND event_number = %d AND permanent = 1;",fTable.c_str(),runNumber,eventNumber));
80     TSQLRow *row = res->Next();
81
82     if(!row)
83     {
84         res = fServer->Query(Form("REPLACE INTO %s (run_number,event_number,system,multiplicity,permanent,file_path) VALUES (%d,%d,'%s',%d,0,'%s');",fTable.c_str(),runNumber,eventNumber,system,multiplicity,filePath));
85     }
86
87     delete row;
88     delete res;
89 }
90
91 bool AliStorageDatabase::MarkEvent(struct eventStruct event)
92 {  
93   TSQLResult* res;
94   res = fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
95   if(!res) 
96     {
97       cout<<"DATABASE -- couldn't update permanent flag"<<endl;
98       delete res;
99       return 0;
100     }
101   else 
102     {  
103       cout<<"DATABASE -- permanent flag updated"<<endl;
104       
105       res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),Form("%s/permEvents.root",fStoragePath.c_str()), event.runNumber,event.eventNumber));
106       if(!res) 
107         {
108           cout<<"DATABASE -- couldn't update file's path. Unsetting permanent flag"<<endl;
109           res = fServer->Query(Form("UPDATE %s SET permanent = 0 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
110           delete res;
111           return 0;
112         }
113       else
114         {
115           cout<<"DATABASE -- event marked"<<endl;
116           delete res;
117           return 1;
118         }
119     }
120 }
121
122 bool AliStorageDatabase::UpdateEventPath(struct eventStruct event,const char *newPath)
123 {  
124   TSQLResult* res;
125   res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),newPath,event.runNumber,event.eventNumber));
126   if(!res) 
127     {
128       cout<<"DATABASE -- couldn't update file's path"<<endl;
129       delete res;
130       return 0;
131     }
132   else 
133     {
134       cout<<"DATABASE -- path updated for event:"<<event.eventNumber<<endl;
135       delete res;
136       return 1;
137     }
138 }
139
140
141 vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list)
142 {
143   cout<<"LIST:"<< list.runNumber[0]<<"\t"<<list.runNumber[1]<<"\t"<<list.eventNumber[0]<<"\t"<<list.eventNumber[1]<<"\t"<< list.multiplicity[0]<<"\t"<< list.multiplicity[1]<<"\t"<<list.marked[0]<<"\t"<< list.marked[1]<<"\t"<<list.system[0]<<"\t"<<list.system[1]<<endl;
144
145
146   cout<<"QUERY:"<<Form("SELECT * FROM %s WHERE run_number >= %d AND run_number <= %d AND event_number >= %d AND event_number <= %d AND multiplicity >= %d AND multiplicity <= %d AND (permanent = %d OR permanent = %d) AND (system = '%s' OR system = '%s') ORDER BY run_number,event_number;",
147                                                  fTable.c_str(),
148                                                  list.runNumber[0],
149                                                  list.runNumber[1],
150                                                  list.eventNumber[0],
151                                                  list.eventNumber[1],
152                                                  list.multiplicity[0],
153                                                  list.multiplicity[1],
154                                                  list.marked[0],
155                                                  list.marked[1],
156                                                  list.system[0],
157                        list.system[1])<<endl;
158
159   TThread::Lock();
160   TSQLResult *result = NULL;
161
162   result =  fServer->Query(Form("SELECT * FROM %s WHERE run_number >= %d AND run_number <= %d AND event_number >= %d AND event_number <= %d AND multiplicity >= %d AND multiplicity <= %d AND (permanent = %d OR permanent = %d) AND (system = '%s' OR system = '%s') ORDER BY run_number,event_number;",
163                                                  fTable.c_str(),
164                                                  list.runNumber[0],
165                                                  list.runNumber[1],
166                                                  list.eventNumber[0],
167                                                  list.eventNumber[1],
168                                                  list.multiplicity[0],
169                                                  list.multiplicity[1],
170                                                  list.marked[0],
171                                                  list.marked[1],
172                                                  list.system[0],
173                                                  list.system[1]));
174
175         TThread::UnLock();
176         TSQLRow *row;
177         vector<serverListStruct> eventsVector;
178         
179         while((row = result->Next()))
180         {
181                 serverListStruct resultList;
182
183                 resultList.runNumber = atoi(row->GetField(0));
184                 resultList.eventNumber = atoi(row->GetField(1));
185                 strcpy(resultList.system, row->GetField(2));
186                 resultList.multiplicity = atoi(row->GetField(3));
187                 resultList.marked = atoi(row->GetField(4));
188
189                 eventsVector.push_back(resultList);
190                 delete row;
191         }
192         delete result;
193         return eventsVector;
194 }
195
196 AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event)
197 {
198         cout<<"database - get event"<<endl;
199         string pathToFile = GetFilePath(event);
200
201         if(!strcmp(pathToFile.c_str(),""))
202         {
203                 cout<<"DATABASE -- no such file in database"<<endl;
204                 return NULL;
205         }
206         
207         TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
208         if(!tmpFile)
209         {
210                 cout<<"DATABASE -- couldn't open temp file"<<endl;
211                 return NULL;
212         }
213         AliESDEvent *data;
214         tmpFile->cd(Form("run%d",event.runNumber));
215         data = (AliESDEvent*)gDirectory->Get(Form("event%d;1",event.eventNumber));
216
217
218     tmpFile->Close();
219     delete tmpFile;
220     
221         return data;
222 }
223
224 void AliStorageDatabase::RemoveEvent(struct eventStruct event)
225 {
226   TSQLResult* res;
227   res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber));
228   delete res;
229 }
230
231 void AliStorageDatabase::RemoveEventsWithPath(string path)
232 {
233     TSQLResult *res = fServer->Query(Form("DELETE FROM %s WHERE file_path = \"%s\";",fTable.c_str(),path.c_str()));
234     delete res;
235 }
236
237 string AliStorageDatabase::GetFilePath(struct eventStruct event)
238 {
239         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
240         TSQLRow *row;
241         row = result->Next();
242         if(row)
243         {
244                 string path(row->GetField(5));
245                 delete row;
246                 return path;
247         }
248         else
249         {
250                 return "";
251         }
252 }
253
254 AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event)
255 {
256     cout<<"Database:"<<event.runNumber<<"\t"<<event.eventNumber<<endl;
257     
258         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
259
260         TSQLRow *row;
261         bool isCurrentEvent=false;
262         struct eventStruct nextEvent;
263         
264         while((row = result->Next()))
265         {
266                 if(isCurrentEvent)
267                 {
268                         nextEvent.runNumber = atoi(row->GetField(0));
269                         nextEvent.eventNumber = atoi(row->GetField(1));
270                         return GetEvent(nextEvent);
271                 }
272
273                 //if current event found
274                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
275                 {
276                         isCurrentEvent=true;
277                 }
278                 
279                 delete row;
280         }
281
282         return NULL;
283 }
284
285 AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
286 {
287         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
288
289         TSQLRow *row;
290         bool isCurrentEvent=false;
291         struct eventStruct nextEvent;
292         
293         while((row = result->Next()))
294         {
295                 if(isCurrentEvent)
296                 {
297                         nextEvent.runNumber = atoi(row->GetField(0));
298                         nextEvent.eventNumber = atoi(row->GetField(1));
299                         return GetEvent(nextEvent);
300                 }
301
302                 //if current event found
303                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
304                 {
305                         isCurrentEvent=true;
306                 }
307                 
308                 delete row;
309         }
310         delete result;
311         return NULL;
312 }
313
314 struct eventStruct AliStorageDatabase::GetOldestEvent()
315 {
316     TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE permanent = 0 ORDER BY run_number,event_number;",fTable.c_str()));
317     
318     TSQLRow *row;
319     struct eventStruct oldestEvent = {0,0};
320     
321     if((row = result->Next()))
322     {
323         oldestEvent.runNumber = atoi(row->GetField(0));
324         oldestEvent.eventNumber = atoi(row->GetField(1));
325         delete row;
326     }
327     else
328     {
329         cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl;
330     }
331     return oldestEvent;
332 }
333
334 AliESDEvent* AliStorageDatabase::GetLastEvent()
335 {
336         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
337
338         TSQLRow *row;
339         struct eventStruct lastEvent = {0,0};
340
341         while((row = result->Next()))
342         {
343                 lastEvent.runNumber = atoi(row->GetField(0));
344                 lastEvent.eventNumber = atoi(row->GetField(1));
345                 delete row;
346         }
347         cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
348         return GetEvent(lastEvent);
349
350 }
351
352 AliESDEvent* AliStorageDatabase::GetFirstEvent()
353 {
354     cout<<"Database - first"<<endl;
355         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
356
357         TSQLRow *row;
358         struct eventStruct firstEvent = {0,0};
359
360         while((row = result->Next()))
361         {
362                 firstEvent.runNumber = atoi(row->GetField(0));
363                 firstEvent.eventNumber = atoi(row->GetField(1));
364                 delete row;
365         }
366         cout<<"First event is:"<<firstEvent.eventNumber<<endl;
367         return GetEvent(firstEvent);
368
369 }