AliRoot Core  v5-06-30 (35d6c57)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliStorageDatabase.cxx
Go to the documentation of this file.
1 #include "AliStorageDatabase.h"
2 
3 #include "AliCDBManager.h"
4 #include "AliCDBEntry.h"
5 #include "AliCDBPath.h"
6 #include "AliOnlineReconstructionUtil.h"
7 #include "AliTriggerConfiguration.h"
8 #include "AliTriggerClass.h"
9 
10 #include <iostream>
11 #include <fstream>
12 
13 #include <TSQLRow.h>
14 #include <TSQLResult.h>
15 #include <TThread.h>
16 #include <TSystem.h>
17 #include <TFile.h>
18 #include <TEnv.h>
19 
20 using namespace std;
21 
23 fHost(""),
24 fPort(""),
25 fDatabase(""),
26 fUID(""),
27 fPassword(""),
28 fTable(""),
29 fServer(0),
30 fStoragePath("")
31 {
32  TThread::Lock();
33  ifstream configFile (GetConfigFilePath());
34  if (configFile.is_open())
35  {
36  string line;
37  int from,to;
38  while(configFile.good())
39  {
40  getline(configFile,line);
41  from = line.find("\"")+1;
42  to = line.find_last_of("\"");
43  if(line.find("HOST=")==0){
44  fHost=line.substr(from,to-from);
45  }
46  else if(line.find("PORT=")==0){
47  fPort=line.substr(from,to-from);
48  }
49  else if(line.find("DATABASE=")==0){
50  fDatabase=line.substr(from,to-from);
51  }
52  else if(line.find("USER=")==0){
53  fUID=line.substr(from,to-from);
54  }
55  else if(line.find("PASS=")==0){
56  fPassword=line.substr(from,to-from);
57  }
58  else if(line.find("TABLE=")==0){
59  fTable=line.substr(from,to-from);
60  }
61  else if(line.find("STORAGE_PATH=")==0){
62  fStoragePath=line.substr(from,to-from);
63  }
64 
65  }
66  if(configFile.eof()){configFile.clear();}
67  configFile.close();
68  }
69  else{cout << "DATABASE -- Unable to open file" <<endl;}
70 
71  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;
72  fServer = TSQLServer::Connect(Form("mysql://%s:%s/%s",fHost.c_str(),fPort.c_str(),fDatabase.c_str()),fUID.c_str(),fPassword.c_str());
73 
74  TThread::UnLock();
75  cout<<"Connected"<<endl;
76 }
77 
79  if (fServer) {delete fServer;}
80 }
81 
83  int eventNumber,
84  char *system,
85  int multiplicity,
86  char *filePath,
87  ULong64_t triggerMask,
88  ULong64_t triggerMaskNext50)
89 {
90  TSQLResult *res = fServer->Query(Form("select * FROM %s WHERE run_number = %d AND event_number = %d AND permanent = 1;",fTable.c_str(),runNumber,eventNumber));
91  TSQLRow *row = res->Next();
92 
93  cout<<"DATABASE -- insterting:"<<Form("REPLACE INTO %s (run_number,event_number,system,multiplicity,permanent,file_path,trigger_mask,trigger_mask_next) VALUES (%d,%d,'%s',%d,0,'%s',%llu,%llu);",fTable.c_str(),runNumber,eventNumber,system,multiplicity,filePath,triggerMask,triggerMaskNext50)<<endl;
94 
95  if(!row)
96  {
97  res = fServer->Query(Form("REPLACE INTO %s (run_number,event_number,system,multiplicity,permanent,file_path,trigger_mask,trigger_mask_next) VALUES (%d,%d,'%s',%d,0,'%s',%llu,%llu);",fTable.c_str(),runNumber,eventNumber,system,multiplicity,filePath,triggerMask,triggerMaskNext50));
98  }
99 
100  delete row;
101  delete res;
102 }
103 
104 bool AliStorageDatabase::MarkEvent(struct eventStruct event)
105 {
106  TSQLResult *res;
107  res = fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
108  if(!res)
109  {
110  cout<<"DATABASE -- couldn't update permanent flag"<<endl;
111  delete res;
112  return 0;
113  }
114  else
115  {
116  cout<<"DATABASE -- permanent flag updated"<<endl;
117 
118  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));
119  if(!res)
120  {
121  cout<<"DATABASE -- couldn't update file's path. Unsetting permanent flag"<<endl;
122  res = fServer->Query(Form("UPDATE %s SET permanent = 0 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
123  delete res;
124  return 0;
125  }
126  else
127  {
128  cout<<"DATABASE -- event marked"<<endl;
129  delete res;
130  return 1;
131  }
132  }
133 }
134 
135 bool AliStorageDatabase::UpdateEventPath(struct eventStruct event,const char *newPath)
136 {
137  TSQLResult* res;
138  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));
139  if(!res)
140  {
141  cout<<"DATABASE -- couldn't update file's path"<<endl;
142  delete res;
143  return 0;
144  }
145  else
146  {
147  cout<<"DATABASE -- path updated for event:"<<event.eventNumber<<endl;
148  delete res;
149  return 1;
150  }
151 }
152 
153 
154 vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list)
155 {
156  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]<<"\t"<<list.triggerClass<<endl;
157 
158  ULong64_t triggerMask;
159  ULong64_t triggerMaskNext50;
160 
161  vector<serverListStruct> eventsVector;
162 
163  if(strcmp(list.triggerClass,"No trigger selection")!=0 && strcmp(list.triggerClass,"")!=0)
164  {
165 
166  // craete CDB manager:
167  TEnv settings;
168  settings.ReadFile(AliOnlineReconstructionUtil::GetPathToServerConf(), kEnvUser);
169  const char *cdbPath = settings.GetValue("cdb.defaultStorage", "");
170  AliCDBManager *man = AliCDBManager::Instance();
171  man->SetDefaultStorage(cdbPath);
172 
173  // get list of runs stored in Storage's database:
174  vector<int> runs = GetListOfRuns();
175 
176  // get trigger classes for all runs:
177  AliCDBEntry *cdbEntry;
178  AliTriggerConfiguration *cfg;
179  TObjArray trarr;
180  AliCDBPath path("GRP/CTP/Config");
181 
182  AliTriggerClass* trgclass;
183 
184  for(int i=0;i<runs.size();i++)
185  {
186  if(runs[i] > list.runNumber[0] && runs[i] < list.runNumber[1])
187  {
188  man->SetRun(runs[i]);
189  cdbEntry = man->Get(path);
190  cfg = (AliTriggerConfiguration*)cdbEntry->GetObject();
191  trarr = cfg->GetClasses();
192 
193  triggerMask = 0;
194  triggerMaskNext50 = 0;
195 
196  for (int j=0;j<trarr.GetEntriesFast();j++)
197  {
198  trgclass = (AliTriggerClass*)trarr.At(j);
199  if(strcmp(trgclass->GetName(),list.triggerClass)==0)
200  {
201  triggerMask = trgclass->GetMask();
202  triggerMaskNext50 = trgclass->GetMaskNext50();
203  }
204  }
205 
206  TThread::Lock();
207  TSQLResult *result = NULL;
208 
209  result = fServer->Query(Form("SELECT * FROM %s WHERE 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') AND ((trigger_mask & %llu) > 0 OR (trigger_mask_next & %llu) > 0) ORDER BY run_number,event_number;",
210  fTable.c_str(),
211  runs[i],
212  list.eventNumber[0],
213  list.eventNumber[1],
214  list.multiplicity[0],
215  list.multiplicity[1],
216  list.marked[0],
217  list.marked[1],
218  list.system[0],
219  list.system[1],
220  triggerMask,
221  triggerMaskNext50));
222 
223  cout<<"Query:"<<Form("SELECT * FROM %s WHERE 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') AND ((trigger_mask & %llu) > 0 OR (trigger_mask_next & %llu) > 0) ORDER BY run_number,event_number;",
224  fTable.c_str(),
225  runs[i],
226  list.eventNumber[0],
227  list.eventNumber[1],
228  list.multiplicity[0],
229  list.multiplicity[1],
230  list.marked[0],
231  list.marked[1],
232  list.system[0],
233  list.system[1],
234  triggerMask,
235  triggerMaskNext50)<<endl;
236 
237  TThread::UnLock();
238  TSQLRow *row;
239 
240  while((row = result->Next()))
241  {
242  serverListStruct resultList;
243 
244  resultList.runNumber = atoi(row->GetField(0));
245  resultList.eventNumber = atoi(row->GetField(1));
246  strcpy(resultList.system, row->GetField(2));
247  resultList.multiplicity = atoi(row->GetField(3));
248  resultList.marked = atoi(row->GetField(4));
249  strcpy(resultList.triggerClass,list.triggerClass);
250 
251  eventsVector.push_back(resultList);
252  delete row;
253  }
254  delete result;
255 
256  }
257  }
258  }
259  else
260  {
261  TThread::Lock();
262  TSQLResult *result = NULL;
263 
264  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;",
265  fTable.c_str(),
266  list.runNumber[0],
267  list.runNumber[1],
268  list.eventNumber[0],
269  list.eventNumber[1],
270  list.multiplicity[0],
271  list.multiplicity[1],
272  list.marked[0],
273  list.marked[1],
274  list.system[0],
275  list.system[1]));
276 
277  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;",
278  fTable.c_str(),
279  list.runNumber[0],
280  list.runNumber[1],
281  list.eventNumber[0],
282  list.eventNumber[1],
283  list.multiplicity[0],
284  list.multiplicity[1],
285  list.marked[0],
286  list.marked[1],
287  list.system[0],
288  list.system[1])<<endl;
289 
290  TThread::UnLock();
291  TSQLRow *row;
292 
293  while((row = result->Next()))
294  {
295  serverListStruct resultList;
296 
297  resultList.runNumber = atoi(row->GetField(0));
298  resultList.eventNumber = atoi(row->GetField(1));
299  strcpy(resultList.system, row->GetField(2));
300  resultList.multiplicity = atoi(row->GetField(3));
301  resultList.marked = atoi(row->GetField(4));
302  strcpy(resultList.triggerClass,list.triggerClass);
303 
304  eventsVector.push_back(resultList);
305  delete row;
306  }
307  delete result;
308 
309  }
310 
311  return eventsVector;
312 }
313 
314 AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event)
315 {
316  cout<<"database - get event:"<<event.runNumber<<"\t"<<event.eventNumber<<endl;
317  string pathToFile = GetFilePath(event);
318 
319  cout<<"DATABASE -- path to file:"<<pathToFile<<endl;
320 
321  if(!strcmp(pathToFile.c_str(),""))
322  {
323  cout<<"DATABASE -- no such file in database"<<endl;
324  return NULL;
325  }
326 
327  TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
328  if(!tmpFile)
329  {
330  cout<<"DATABASE -- couldn't open temp file"<<endl;
331  return NULL;
332  }
333  tmpFile->cd(Form("run%d",event.runNumber));
334  AliESDEvent *data = (AliESDEvent*)gDirectory->Get(Form("event%d;1",event.eventNumber));
335  tmpFile->Close();
336  delete tmpFile;
337 
338  if(data)
339  {
340  cout<<"DATABASE -- read event:"<<data->GetEventNumberInFile()<<endl;
341  }
342  else
343  {
344  cout<<"DATABASE -- event is corrupted"<<endl;
345  }
346 
347  return data;
348 }
349 
350 void AliStorageDatabase::RemoveEvent(struct eventStruct event)
351 {
352  TSQLResult* res;
353  res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber));
354  delete res;
355 }
356 
358 {
359  TSQLResult *res = fServer->Query(Form("DELETE FROM %s WHERE file_path = \"%s\";",fTable.c_str(),path.c_str()));
360  delete res;
361 }
362 
363 string AliStorageDatabase::GetFilePath(struct eventStruct event)
364 {
365  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
366  TSQLRow *row;
367  row = result->Next();
368  if(row)
369  {
370  string path(row->GetField(5));
371  delete row;
372  return path;
373  }
374  else
375  {
376  return "";
377  }
378 }
379 
381 {
382  vector<int> resultingVector;
383 
384  TSQLResult *result = fServer->Query(Form("SELECT run_number FROM %s ORDER BY run_number;",fTable.c_str()));
385  TSQLRow *row;
386 
387  int currentRun,prevRun=-1;
388 
389  while((row=result->Next()))
390  {
391  currentRun = atoi(row->GetField(0));
392  if(currentRun!=prevRun)
393  {
394  resultingVector.push_back(currentRun);
395  prevRun=currentRun;
396  }
397  }
398  delete row;
399  return resultingVector;
400 }
401 
402 AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event)
403 {
404  cout<<"Database:"<<event.runNumber<<"\t"<<event.eventNumber<<endl;
405 
406  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
407 
408  TSQLRow *row;
409  bool isCurrentEvent=false;
410  struct eventStruct nextEvent;
411 
412  while((row = result->Next()))
413  {
414  if(isCurrentEvent)
415  {
416  nextEvent.runNumber = atoi(row->GetField(0));
417  nextEvent.eventNumber = atoi(row->GetField(1));
418  return GetEvent(nextEvent);
419  }
420 
421  //if current event found
422  if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
423  {
424  isCurrentEvent=true;
425  }
426 
427  delete row;
428  }
429 
430  return NULL;
431 }
432 
433 AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
434 {
435  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
436 
437  TSQLRow *row;
438  bool isCurrentEvent=false;
439  struct eventStruct nextEvent;
440 
441  while((row = result->Next()))
442  {
443  if(isCurrentEvent)
444  {
445  nextEvent.runNumber = atoi(row->GetField(0));
446  nextEvent.eventNumber = atoi(row->GetField(1));
447  return GetEvent(nextEvent);
448  }
449 
450  //if current event found
451  if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
452  {
453  isCurrentEvent=true;
454  }
455 
456  delete row;
457  }
458  delete result;
459  return NULL;
460 }
461 
462 struct eventStruct AliStorageDatabase::GetOldestEvent()
463 {
464  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE permanent = 0 ORDER BY run_number,event_number;",fTable.c_str()));
465 
466  TSQLRow *row;
467  struct eventStruct oldestEvent = {0,0};
468 
469  if((row = result->Next()))
470  {
471  oldestEvent.runNumber = atoi(row->GetField(0));
472  oldestEvent.eventNumber = atoi(row->GetField(1));
473  delete row;
474  }
475  else
476  {
477  cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl;
478  }
479  return oldestEvent;
480 }
481 
483 {
484  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
485 
486  TSQLRow *row;
487  struct eventStruct lastEvent = {0,0};
488 
489  while((row = result->Next()))
490  {
491  lastEvent.runNumber = atoi(row->GetField(0));
492  lastEvent.eventNumber = atoi(row->GetField(1));
493  delete row;
494  }
495  cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
496  return GetEvent(lastEvent);
497 
498 }
499 
501 {
502  cout<<"Database - first"<<endl;
503  TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
504 
505  TSQLRow *row;
506  struct eventStruct firstEvent = {0,0};
507 
508  while((row = result->Next()))
509  {
510  firstEvent.runNumber = atoi(row->GetField(0));
511  firstEvent.eventNumber = atoi(row->GetField(1));
512  delete row;
513  }
514  cout<<"First event is:"<<firstEvent.eventNumber<<endl;
515  return GetEvent(firstEvent);
516 
517 }
#define TObjArray
bool MarkEvent(struct eventStruct event)
void InsertEvent(int runNumber, int eventNumber, char *system, int multiplicity, char *filePath, ULong64_t triggerMask, ULong64_t triggerMaskNext50)
AliESDEvent * GetFirstEvent()
AliESDEvent * GetLastEvent()
void RemoveEvent(struct eventStruct event)
std::vector< RunInfo > runs
std::vector< int > GetListOfRuns()
AliESDEvent * GetNextEvent(struct eventStruct event)
bool UpdateEventPath(struct eventStruct event, const char *newPath)
std::vector< serverListStruct > GetList(struct listRequestStruct listStruct)
AliESDEvent * GetEvent(struct eventStruct event)
std::string GetFilePath(struct eventStruct event)
void RemoveEventsWithPath(std::string path)
AliESDEvent * GetPrevEvent(struct eventStruct event)