5ef9795168080350d87924773ae22fc9498e7618
[u/mrichter/AliRoot.git] / SHUTTLE / test / TestShuttleLogbook.C
1
2 TSQLServer *fServer=0;
3
4 //______________________________________________________________________________________________
5 Bool_t Connect(){
6 // Connect to MySQL Server of the DAQ logbook
7
8         // check connection: if already connected return
9         if(fServer && fServer->IsConnected()) return kTRUE;
10
11         fServer = TSQLServer::Connect("mysql://pcald30.cern.ch","offline","alice");
12
13         if (!fServer || !fServer->IsConnected()) {
14                 printf("Can't establish connection to DAQ log book DB!\n");
15                 if(fServer) delete fServer;
16                 return kFALSE;
17         }
18
19         // Get table
20         TSQLResult* aResult=0;
21         aResult = fServer->GetTables("REFSYSLOG");
22         delete aResult;
23         return kTRUE;
24 }
25
26 //______________________________________________________________________________________________
27 Bool_t QueryShuttleLogbook(AliShuttleLogbookEntry& entry, Int_t runNumber=-1)
28 {
29 // Query DAQ's Shuttle logbook and fills detector status array
30
31         if(runNumber<=0 && entry.GetRun()<=0) {
32                 printf("Use a valid Run number!\n");
33                 return kFALSE;
34         }
35         if(!Connect()) return kFALSE;
36         if(runNumber<=0) runNumber= entry.GetRun();
37         entry.SetRun(runNumber); 
38
39         // check connection, in case connect
40         if(!Connect()) return kFALSE;
41
42         TString sqlQuery;
43         sqlQuery = Form("select * from logbook_shuttle where run = %d", runNumber);
44
45         TSQLResult* aResult = fServer->Query(sqlQuery);
46         if (!aResult) {
47                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
48                 return kFALSE;
49         }
50
51         // TODO Check field count!
52         if (aResult->GetFieldCount() != 24) {
53                 printf("Invalid SQL result field number!\n");
54                 delete aResult;
55                 return kFALSE;
56         }
57
58         TSQLRow* aRow;
59         while ((aRow = aResult->Next())) {
60                 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
61                 Int_t run = runString.Atoi();
62
63                 // loop on detectors
64                 for(UInt_t ii = 0; ii < 24; ii++){
65                         entry.SetDetectorStatus(aResult->GetFieldName(ii), aRow->GetField(ii));
66                 }
67
68                 delete aRow;
69         }
70
71         delete aResult; aResult=0;
72
73         // Query run parameters from logbook!
74
75         sqlQuery = Form("select * from logbook where run=%d", runNumber);
76
77         aResult = fServer->Query(sqlQuery);
78         if (!aResult) {
79                 printf("Can't execute query <%s>!", sqlQuery.Data());
80                 return kFALSE;
81         }
82
83         if(aResult->GetRowCount() == 0) {
84                 printf("QueryRunParameters - No entry in DAQ Logbook for run %d!", runNumber);
85                 delete aResult;
86                 return kFALSE;
87         }
88
89         if(aResult->GetRowCount() > 1) {
90                 printf("More than one entry in DAQ Logbook for run %d!", runNumber);
91                 delete aResult;
92                 return kFALSE;
93         }
94
95         while ((aRow = aResult->Next())) {
96
97                 for(UInt_t ii = 0; ii < aResult->GetFieldCount(); ii++)
98                         entry.SetRunParameter(aResult->GetFieldName(ii), aRow->GetField(ii));
99
100                 UInt_t startTime, endTime;
101                 TString startTimeString = entry.GetRunParameter("time_start");
102                 UInt_t startTime = startTimeString.Atoi();
103                 TString endTimeString = entry.GetRunParameter("time_end");
104                 UInt_t endTime = endTimeString.Atoi();
105
106                 if (!startTime || !endTime || startTime > endTime) {
107                         printf("QueryRunParameters - Invalid parameters for Run %d: startTime = %d, endTime = %d",
108                                         runNumber, startTime, endTime);
109                         delete aRow;
110                         delete aResult;
111                         return kFALSE;
112                 }
113
114                 entry.SetStartTime(startTime);
115                 entry.SetEndTime(endTime);
116
117                 delete aRow;
118         }
119
120         entry.Print("all");
121
122         delete aResult;
123         return kTRUE;
124 }
125 //______________________________________________________________________________________________
126 Bool_t UpdateShuttleLogbook(AliShuttleLogbookEntry& entry, Int_t runNumber=-1)
127 {
128   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
129
130
131         if(runNumber<=0 && entry.GetRun()<=0) {
132                 printf("Use a valid Run number!\n");
133                 return kFALSE;
134         }
135         if(!Connect()) return kFALSE;
136         if(runNumber<=0) runNumber= entry.GetRun();
137         entry.SetRun(runNumber); 
138
139         TString sqlQuery("update logbook_shuttle set ");
140
141         for(UInt_t i=0; i < AliShuttleInterface::NDetectors(); i++){
142                 sqlQuery += Form("%s=\"%s\"", AliShuttleInterface::GetDetName(i), entry.GetDetectorStatusName(entry.GetDetectorStatus(i)));
143                 if(i < AliShuttleInterface::NDetectors()-1) sqlQuery += ", ";
144         }
145
146         sqlQuery += Form(" where run=%d;",entry.GetRun());
147
148         printf("sqlQuery: %s\n", sqlQuery.Data());
149
150         TSQLResult* aResult;
151         aResult = fServer->Query(sqlQuery);
152         if (!aResult) {
153                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
154                 return kFALSE;
155         }
156
157         delete aResult;
158
159         return kTRUE;
160 }
161
162 //______________________________________________________________________________________________
163 Bool_t UpdateShuttleLogbook(Int_t runNumber, const char* detCode, AliShuttleLogbookEntry::Status status)
164 {
165   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
166
167
168         if(AliShuttleInterface::GetDetPos(detCode) < 0) return kFALSE;
169         if(!Connect()) return kFALSE;
170
171         TString sqlQuery("update logbook_shuttle set ");
172
173
174         sqlQuery += Form("%s=\"%s\" ", detCode, AliShuttleLogbookEntry::GetDetectorStatusName(status));
175
176         sqlQuery += Form("where run=%d;",runNumber);
177
178         printf("sqlQuery: %s\n", sqlQuery.Data());
179
180         TSQLResult* aResult;
181         aResult = fServer->Query(sqlQuery);
182         if (!aResult) {
183                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
184                 return kFALSE;
185         }
186
187         delete aResult;
188
189         return kTRUE;
190 }
191
192 //______________________________________________________________________________________________
193 Bool_t UpdateShuttleLogbook(Int_t runNumber, const char* detCode, const char* status)
194 {
195   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
196
197
198         if(AliShuttleInterface::GetDetPos(detCode) < 0) return kFALSE;
199         if(!Connect()) return kFALSE;
200
201         TString sqlQuery("update logbook_shuttle set ");
202
203
204         sqlQuery += Form("%s=\"%s\" ", detCode, status);
205
206         sqlQuery += Form("where run=%d;",runNumber);
207
208         printf("sqlQuery: %s\n", sqlQuery.Data());
209
210         TSQLResult* aResult;
211         aResult = fServer->Query(sqlQuery);
212         if (!aResult) {
213                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
214                 return kFALSE;
215         }
216
217         delete aResult;
218
219         return kTRUE;
220 }
221
222 //______________________________________________________________________________________________
223 Bool_t InsertNewRun(AliShuttleLogbookEntry& entry, Int_t runNumber=-1)
224 {
225   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
226
227         if(runNumber<=0 && entry.GetRun()<=0) {
228                 printf("Use a valid Run number!\n");
229                 return kFALSE;
230         }
231         if(!Connect()) return kFALSE;
232         if(runNumber<=0) runNumber= entry.GetRun();
233         entry.SetRun(runNumber); 
234
235         TString sqlQuery = Form("insert into logbook_shuttle (run) values (%d);", runNumber);
236
237         printf("sqlQuery: %s\n", sqlQuery.Data());
238
239         TSQLResult* aResult;
240         aResult = fServer->Query(sqlQuery);
241         if (!aResult) {
242                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
243                 return kFALSE;
244         }
245
246         delete aResult;
247
248         UpdateShuttleLogbook(entry);
249
250         return kTRUE;
251 }
252
253 //______________________________________________________________________________________________
254 Bool_t DeleteEntries(Int_t runNumber=-1)
255 {
256   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
257
258         if(!Connect()) return kFALSE;
259         
260         TString runStr;
261         if(runNumber>0) runStr=Form("where run=%d",runNumber);
262         TString sqlQuery = Form("delete from logbook_shuttle %s;", runStr.Data());
263
264         printf("sqlQuery: %s\n", sqlQuery.Data());
265
266         TSQLResult* aResult;
267         aResult = fServer->Query(sqlQuery);
268         if (!aResult) {
269                 printf("Can't execute query <%s>!\n", sqlQuery.Data());
270                 return kFALSE;
271         }
272
273         delete aResult;
274
275         return kTRUE;
276 }
277
278 //______________________________________________________________________________________________
279 void TestShuttleLogbook(){
280
281         gSystem->Load("libSHUTTLE.so");
282         
283         DeleteEntries();
284
285         AliShuttleLogbookEntry::Status y[17]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
286         AliShuttleLogbookEntry lb(21242, y);
287         lb.SetDetectorStatus("HMP","Unprocessed"); // HMPID
288         lb.SetDetectorStatus("ZDC","Unprocessed"); // ZDC
289         lb.SetDetectorStatus("TPC","Unprocessed"); // TPC
290         lb.SetDetectorStatus("SPD","Unprocessed"); // SPD
291         lb.Print();
292
293         InsertNewRun(lb);
294         InsertNewRun(lb,21243);
295         InsertNewRun(lb,21244);
296         InsertNewRun(lb,21245);
297         InsertNewRun(lb,21246);
298
299 }