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