update (alberto):
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttleLogbookEntry.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //
17 //  This class is a container for the data queried from DAQ's logbook and logbook_shuttle tables.
18 //  It holds the run number, the start time and end time of the run,
19 //  and the array of the detectors' status (Unprocessed, Inactive, Failed, Done)
20
21 #include "AliShuttleLogbookEntry.h"
22 #include "AliLog.h"
23 #include "TTimeStamp.h"
24
25 // TODO test only!
26 #include <TSQLServer.h>
27 #include <TSQLResult.h>
28 #include <TSQLRow.h>
29 #include <TObjArray.h>
30
31 ClassImp(AliShuttleLogbookEntry)
32
33 //______________________________________________________________________________________________
34 AliShuttleLogbookEntry::AliShuttleLogbookEntry() :
35 TObject(),
36 fRun(-1),
37 fStartTime(0),
38 fEndTime(0),
39 //fDetectorStatus(0),
40 fServer(0)
41 {
42   // default constructor
43
44         const UInt_t nDet = AliShuttle::NDetectors();
45 //      fDetectorStatus = new Status[nDet];
46         memset(fDetectorStatus, kUnprocessed, nDet*sizeof(Status));
47 }
48
49 //______________________________________________________________________________________________
50 AliShuttleLogbookEntry::AliShuttleLogbookEntry(Int_t run, UInt_t startTime, UInt_t endTime, Status* status) :
51 TObject(),
52 fRun(run),
53 fStartTime(startTime),
54 fEndTime(endTime),
55 //fDetectorStatus(0),
56 fServer(0)
57 {
58   // default constructor
59
60         const UInt_t nDet = AliShuttle::NDetectors();
61 //      fDetectorStatus = new Status[nDet];
62         memset(fDetectorStatus, kUnprocessed, nDet*sizeof(Status));
63         if(status) SetDetectorStatus(status);
64 }
65
66 //______________________________________________________________________________________________
67 AliShuttleLogbookEntry::~AliShuttleLogbookEntry() {
68 // destructor
69
70         if(fServer){
71                 if(fServer->IsConnected()) fServer->Close();
72                 delete fServer;
73         }
74 //      if(fDetectorStatus) delete[] fDetectorStatus; fDetectorStatus=0;
75 }
76
77 //______________________________________________________________________________________________
78 AliShuttleLogbookEntry::AliShuttleLogbookEntry(const AliShuttleLogbookEntry &c) :
79 TObject(),
80 fRun(c.fRun),
81 fStartTime(c.fStartTime),
82 fEndTime(c.fEndTime),
83 fServer(0)
84 {
85   // copy constructor
86
87   SetDetectorStatus(c.GetDetectorStatus());
88 }
89
90 //______________________________________________________________________________________________
91 AliShuttleLogbookEntry &AliShuttleLogbookEntry::operator=(const AliShuttleLogbookEntry &c)
92 {
93   // assigment operator
94
95         if (this != &c)
96                 ((AliShuttleLogbookEntry &) c).Copy(*this);
97         return *this;
98 }
99
100 //______________________________________________________________________________________________
101 void AliShuttleLogbookEntry::Copy(TObject& c) const
102 {
103   // copy function
104
105         AliShuttleLogbookEntry& target = (AliShuttleLogbookEntry &) c;
106
107         target.fRun = fRun;
108         target.fStartTime = fStartTime;
109         target.fEndTime = fEndTime;
110
111         target.SetDetectorStatus(GetDetectorStatus());
112 }
113
114 //______________________________________________________________________________________________
115 AliShuttleLogbookEntry::Status AliShuttleLogbookEntry::GetDetectorStatus(const char* detCode) const
116 {
117 // get detector status from detector code
118
119         return GetDetectorStatus(AliShuttle::GetDetPos(detCode));
120 }
121
122 //______________________________________________________________________________________________
123 AliShuttleLogbookEntry::Status AliShuttleLogbookEntry::GetDetectorStatus(Int_t detPos) const
124 {
125 // get detector status from detector code
126
127         if(detPos < 0 || detPos >= (Int_t) AliShuttle::NDetectors()) {
128                 AliError(Form("Invalid parameter: %d", detPos));
129                 return kUnprocessed;
130         }
131         return fDetectorStatus[detPos];
132 }
133
134 //______________________________________________________________________________________________
135 void AliShuttleLogbookEntry::SetDetectorStatus(const char* detCode, Status status)
136 {
137 // set detector status from detector code
138
139         Int_t detPos = AliShuttle::GetDetPos(detCode);
140         if(detPos<0) return;
141         SetDetectorStatus(detPos, status);
142 }
143
144 //______________________________________________________________________________________________
145 void AliShuttleLogbookEntry::SetDetectorStatus(Status* status)
146 {
147 // set detector status from detector code
148
149         for(UInt_t i=0; i < AliShuttle::NDetectors(); i++){
150                 fDetectorStatus[i] = status[i];
151         }
152 }
153
154 //______________________________________________________________________________________________
155 void AliShuttleLogbookEntry::SetDetectorStatus(UInt_t detPos, Status status)
156 {
157 // set detector status from detector code
158
159         if(detPos >= AliShuttle::NDetectors()) {
160                 AliError(Form("Shuttle has only %d subdetectors!", AliShuttle::NDetectors()));
161                 return;
162         }
163         fDetectorStatus[detPos] = status;
164 }
165
166 //______________________________________________________________________________________________
167 Bool_t AliShuttleLogbookEntry::IsDone() const{
168 // return TRUE if all subdetectors are in status DONE, FAILED or INACTIVE
169
170         for(UInt_t i=0; i < AliShuttle::NDetectors(); i++){
171                 if(fDetectorStatus[i] == kUnprocessed) return kFALSE;
172         }
173         return kTRUE;
174 }
175
176 //______________________________________________________________________________________________
177 const char* AliShuttleLogbookEntry::GetDetectorStatusName(Status status)
178 {
179   // returns a name (string) of the detector status
180
181         switch (status){
182                 case kUnprocessed: return "UNPROCESSED";
183                 case kInactive: return "INACTIVE";
184                 case kFailed: return "FAILED";
185                 case kDone: return "DONE";
186         }
187         return 0;
188 }
189
190 //______________________________________________________________________________________________
191 void AliShuttleLogbookEntry::Print(Option_t* /*option*/) const
192 {
193   // print current shuttle logbook entry
194
195         TString message = "\n*** Run parameters ***\n";
196         TTimeStamp startTimeStamp(fStartTime);
197         TTimeStamp endTimeStamp(fEndTime);
198         message += Form("\tRun \t\t%d\n", fRun);
199         message += Form("\tStarted \t%s\n", startTimeStamp.AsString("s"));
200         message += Form("\tFinished \t%s\n", endTimeStamp.AsString("s"));
201         message += "\n*** Detector status ***\n";
202
203         for(UInt_t i=0; i < AliShuttle::NDetectors(); i++)
204                 message += Form("\t%2d - %s: %s\n", i, AliShuttle::GetDetCode(i),
205                                         GetDetectorStatusName(fDetectorStatus[i]));
206
207         AliInfo(Form("%s",message.Data()));
208 }
209
210 //______________________________________________________________________________________________
211 Bool_t AliShuttleLogbookEntry::Connect(){
212 // Connect to MySQL Server of the DAQ logbook
213
214         // check connection: if already connected return
215         if(fServer && fServer->IsConnected()) return kTRUE;
216
217         fServer = TSQLServer::Connect("mysql://pcald30.cern.ch","offline","alice");
218
219         if (!fServer || !fServer->IsConnected()) {
220                 AliError("Can't establish connection to DAQ log book DB!");
221                 if(fServer) delete fServer;
222                 return kFALSE;
223         }
224
225         // Get table
226         TSQLResult* aResult=0;
227         aResult = fServer->GetTables("REFSYSLOG");
228         delete aResult;
229         return kTRUE;
230 }
231
232 //______________________________________________________________________________________________
233 Bool_t AliShuttleLogbookEntry::QueryShuttleLogbook(Int_t runNumber)
234 {
235 // Query DAQ's Shuttle logbook and fills detector status array
236
237         Int_t run;
238         if(runNumber < 0) {
239                 run = GetRun();
240         } else{
241                 run = runNumber;
242         }
243
244         // check connection, in case connect
245         if(!Connect()) return kFALSE;
246
247         TString sqlQuery;
248         sqlQuery = Form("select * from logbook_shuttle where run = %d", run);
249
250         TSQLResult* aResult = fServer->Query(sqlQuery);
251         if (!aResult) {
252                 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
253                 return kFALSE;
254         }
255
256         // TODO Check field count!
257         if (aResult->GetFieldCount() != 24) {
258                 AliError("Invalid SQL result field number!");
259                 delete aResult;
260                 return kFALSE;
261         }
262
263         TSQLRow* aRow;
264         while ((aRow = aResult->Next())) {
265                 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
266                 Int_t run = runString.Atoi();
267
268                 Status detStatus[24];
269
270                 // loop on detectors
271                 for(UInt_t ii = 0; ii < 24; ii++){
272                         TString detCode(aResult->GetFieldName(ii));
273                         Int_t detPos = AliShuttle::GetDetPos(detCode.Data());
274                         if(detPos < 0) continue;
275                         TString statusString(aRow->GetField(ii), aRow->GetFieldLength(ii));
276                         if(statusString == "UNPROCESSED"){
277                                 detStatus[detPos] = AliShuttleLogbookEntry::kUnprocessed;
278                         } else if (statusString == "INACTIVE") {
279                                 detStatus[detPos] = AliShuttleLogbookEntry::kInactive;
280                         } else if (statusString == "FAILED") {
281                                 detStatus[detPos] = AliShuttleLogbookEntry::kFailed;
282                         } else if (statusString == "DONE") {
283                                 detStatus[detPos] = AliShuttleLogbookEntry::kDone;
284                         }
285                 }
286
287                 SetRun(run);
288                 SetDetectorStatus(detStatus);
289                 delete aRow;
290         }
291
292         Print("");
293
294         delete aResult;
295         return kTRUE;
296 }
297 //______________________________________________________________________________________________
298 Bool_t AliShuttleLogbookEntry::UpdateShuttleLogbook()
299 {
300   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
301
302
303         if(!Connect()) return kFALSE;
304
305         TString sqlQuery("update logbook_shuttle set ");
306
307         for(UInt_t i=0; i < AliShuttle::NDetectors(); i++){
308                 sqlQuery += Form("%s=\"%s\"", AliShuttle::GetDetCode(i), GetDetectorStatusName(fDetectorStatus[i]));
309                 if(i < AliShuttle::NDetectors()-1) sqlQuery += ", ";
310         }
311
312         sqlQuery += Form(" where run=%d;",GetRun());
313
314         AliInfo(Form("sqlQuery: %s", sqlQuery.Data()));
315
316         TSQLResult* aResult;
317         aResult = fServer->Query(sqlQuery);
318         if (!aResult) {
319                 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
320                 return kFALSE;
321         }
322
323         delete aResult;
324
325         return kTRUE;
326 }
327
328 //______________________________________________________________________________________________
329 Bool_t AliShuttleLogbookEntry::UpdateShuttleLogbook(const char* detCode, Status status)
330 {
331   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
332
333
334         if(AliShuttle::GetDetPos(detCode) < 0) return kFALSE;
335         SetDetectorStatus(detCode, status);
336         if(!Connect()) return kFALSE;
337
338         TString sqlQuery("update logbook_shuttle set ");
339
340
341         sqlQuery += Form("%s=\"%s\" ", detCode, GetDetectorStatusName(status));
342
343         sqlQuery += Form("where run=%d;",GetRun());
344
345         AliInfo(Form("sqlQuery: %s", sqlQuery.Data()));
346
347         TSQLResult* aResult;
348         aResult = fServer->Query(sqlQuery);
349         if (!aResult) {
350                 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
351                 return kFALSE;
352         }
353
354         delete aResult;
355
356         return kTRUE;
357 }
358
359 //______________________________________________________________________________________________
360 Bool_t AliShuttleLogbookEntry::InsertNewRun(Int_t runNumber)
361 {
362   // Update Shuttle logbook table - TEST ONLY, USE WITH CARE!
363
364         if(runNumber<=0 && GetRun()<=0) return kFALSE;
365         if(runNumber>0) SetRun(runNumber);
366         if(!Connect()) return kFALSE;
367
368         TString sqlQuery = Form("insert into logbook_shuttle (run) values (%d);", GetRun());
369
370         AliInfo(Form("sqlQuery: %s", sqlQuery.Data()));
371
372         TSQLResult* aResult;
373         aResult = fServer->Query(sqlQuery);
374         if (!aResult) {
375                 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
376                 return kFALSE;
377         }
378
379         delete aResult;
380
381         UpdateShuttleLogbook();
382
383         return kTRUE;
384 }