]> git.uio.no Git - u/mrichter/AliRoot.git/blob - SHUTTLE/AliShuttle.cxx
o) removed files that were moved to STEER
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttle.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 $Log$
18 Revision 1.2  2006/03/07 07:52:34  hristov
19 New version (B.Yordanov)
20
21 Revision 1.6  2005/11/19 17:19:14  byordano
22 RetrieveDATEEntries and RetrieveConditionsData added
23
24 Revision 1.5  2005/11/19 11:09:27  byordano
25 AliShuttle declaration added
26
27 Revision 1.4  2005/11/17 17:47:34  byordano
28 TList changed to TObjArray
29
30 Revision 1.3  2005/11/17 14:43:23  byordano
31 import to local CVS
32
33 Revision 1.1.1.1  2005/10/28 07:33:58  hristov
34 Initial import as subdirectory in AliRoot
35
36 Revision 1.2  2005/09/13 08:41:15  byordano
37 default startTime endTime added
38
39 Revision 1.4  2005/08/30 09:13:02  byordano
40 some docs added
41
42 Revision 1.3  2005/08/29 21:15:47  byordano
43 some docs added
44
45 */
46
47 //
48 // This class is the main manager for AliShuttle. 
49 // It organizes the data retrieval from DCS and call the 
50 // interface methods of AliPreprocessor.
51 // For every detector in AliShuttleConfgi (see AliShuttleConfig),
52 // data for its set of aliases is retrieved. If there is registered
53 // AliPreprocessor for this detector then it will be used
54 // accroding to the schema (see AliPreprocessor).
55 // If there isn't registered AliPreprocessor than the retrieved
56 // data is stored automatically to the undelying AliCDBStorage.
57 // For detSpec is used the alias name.
58 //
59
60 #include "AliShuttle.h"
61
62 #include "AliCDBManager.h"
63 #include "AliCDBStorage.h"
64 #include "AliCDBId.h"
65 #include "AliShuttleConfig.h"
66 #include "AliDCSClient.h"
67 #include "AliLog.h"
68 #include "AliPreprocessor.h"
69 #include "AliDefaultPreprocessor.h"
70
71 #include <TString.h>
72 #include <TObjString.h>
73
74 ClassImp(AliShuttle)
75
76 TString AliShuttle::fgkLocalUri("local://ShuttleCDB");
77
78 //______________________________________________________________________________________________
79 AliShuttle::AliShuttle(const AliShuttleConfig* config,
80                 UInt_t timeout, Int_t retries):
81         fConfig(config),
82         fTimeout(timeout),
83         fRetries(retries), fCurrentRun(-1), fCurrentStartTime(0),
84         fCurrentEndTime(0),
85         fLog("")
86 {
87         //
88         // config: AliShuttleConfig used
89         // mainStorage: underlying AliCDBStorage
90         // localStorage (local) CDB storage to be used if mainStorage is unavailable
91         // timeout: timeout used for AliDCSClient connection
92         // retries: the number of retries in case of connection error.
93         //
94
95         RegisterPreprocessor(new AliDefaultPreprocessor("DEFAULT", 0));
96
97 }
98
99 //______________________________________________________________________________________________
100 AliShuttle::~AliShuttle() {
101         fPreprocessorMap.DeleteAll();
102 }
103
104 //______________________________________________________________________________________________
105 void AliShuttle::RegisterPreprocessor(AliPreprocessor* preprocessor) {
106         //
107         // Registers new AliPreprocessor.
108         // It uses GetName() for indentificator of the pre processor.
109         // The pre processor is registered it there isn't any other
110         // with the same identificator (GetName()).
111         //
112
113         if (fPreprocessorMap.GetValue(preprocessor->GetName())) {
114                 AliWarning(Form("AliPreprocessor %s is already registered!",
115                         preprocessor->GetName()));
116                 return;
117         }
118
119         fPreprocessorMap.Add(new TObjString(preprocessor->GetName()), preprocessor);
120 }
121
122 //______________________________________________________________________________________________
123 UInt_t AliShuttle::Store(const char* detector,
124                 TObject* object, AliCDBMetaData* metaData)
125 {
126         // store data into CDB
127         // returns 0 if fail
128         //         1 if stored in main (Grid) storage
129         //         2 if stored in backup (Local) storage
130
131         if (!(AliCDBManager::Instance()->IsDefaultStorageSet())) {
132                 AliError("No CDB storage set!");
133                 return 0;
134         }
135
136         AliCDBId id(AliCDBPath(detector, "DCS", "Data"),
137                 GetCurrentRun(), GetCurrentRun());
138
139         UInt_t result = (UInt_t) AliCDBManager::Instance()->Put(object, id, metaData);
140         if(!result) {
141
142                 Log(detector, "Error while storing object in main storage!");
143                 AliError("local storage will be used!");
144
145 //              result = fLocalStorage->Put(object, id, metaData);
146                 result = AliCDBManager::Instance()->GetStorage(fgkLocalUri)
147                                         ->Put(object, id, metaData);
148
149                 if(result) {
150                         result = 2;
151                 }else{
152                         Log(detector, "Can't store data!");
153                 }
154         }
155         return result;
156
157 }
158
159 //______________________________________________________________________________________________
160 Bool_t AliShuttle::Process(Int_t run, UInt_t startTime, UInt_t endTime) {
161         //
162         // Makes data retrieval for all detectors in the configuration.
163         // run: is the run number used
164         // startTime: is the run start time
165         // endTime: is the run end time
166         // Returns kFALSE in case of error occured and kTRUE otherwise
167         //
168
169         Bool_t hasError = kFALSE;
170
171         TIter iter(fConfig->GetDetectors());
172         TObjString* aDetector;
173
174         ClearLog();
175
176         while ((aDetector = (TObjString*) iter.Next())) {
177                 if(!fConfig->HostProcessDetector(aDetector->GetName())) continue;
178                 if(!Process(run, startTime, endTime, aDetector->String())) {
179                         hasError = kTRUE;
180                 }
181         }
182
183         if(fLog != "") StoreLog(run);
184
185         return !hasError;
186 }
187
188 //______________________________________________________________________________________________
189 Bool_t AliShuttle::Process(Int_t run, UInt_t startTime, UInt_t endTime,
190                 const char* detector)
191 {
192         //
193         // Makes data retrieval just for one specific detector.
194         // Threre should be a configuration for this detector.
195         // run: is the run number used
196         // startTime: is the run start time
197         // endTime: is the run end time
198         // detector: detector for which the retrieval will be made
199         // Returns kFALSE in case of error occured and kTRUE otherwise
200         //
201
202         AliInfo(Form("Retrieving values for %s, run %d", detector, run));
203
204         if (!fConfig->HasDetector(detector)) {
205                 Log(detector, "There isn't any configuration for %s !");
206                 return kFALSE;
207         }
208
209         fCurrentRun = run;
210         fCurrentStartTime = startTime;
211         fCurrentEndTime = endTime;
212
213         TString host(fConfig->GetDCSHost(detector));
214         Int_t port = fConfig->GetDCSPort(detector);
215
216         TIter iter(fConfig->GetDCSAliases(detector));
217         TObjString* anAlias;
218         TMap aliasMap;
219
220         Bool_t hasError = kFALSE;
221         Bool_t result=kFALSE;
222
223         while ((anAlias = (TObjString*) iter.Next())) {
224                 TObjArray valueSet;
225                 result = GetValueSet(host, port, anAlias->String(), valueSet);
226                 if(result) {
227                         aliasMap.Add(anAlias->Clone(), valueSet.Clone());
228                 }else{
229                         TString message = Form("Error while retrieving alias %s !", 
230                                         anAlias->GetName());
231                         Log(detector, message.Data());
232                         hasError = kTRUE;
233                 }
234         }
235
236         AliPreprocessor* aPreprocessor =
237                 dynamic_cast<AliPreprocessor*> (fPreprocessorMap.GetValue(detector));
238         if(!aPreprocessor){
239                 AliInfo(Form("No Preprocessor for %s: Using default Preprocessor!",detector));
240                 aPreprocessor = dynamic_cast<AliPreprocessor*> (fPreprocessorMap.GetValue("DEFAULT"));
241         }
242
243         aPreprocessor->Initialize(run, startTime, endTime);
244         hasError = (Bool_t) !(aPreprocessor->Process(&aliasMap));
245
246   aliasMap.Delete();
247
248         fCurrentRun = -1;
249         fCurrentStartTime = 0;
250         fCurrentEndTime = 0;
251
252         return !hasError;
253 }
254
255 //______________________________________________________________________________________________
256 Bool_t AliShuttle::GetValueSet(const char* host, Int_t port, const char* alias,
257                                 TObjArray& valueSet)
258 {
259         AliDCSClient client(host, port, fTimeout, fRetries);
260         if (!client.IsConnected()) {
261                 return kFALSE;
262         }
263
264         Int_t result = client.GetAliasValues(alias, 
265                 GetCurrentStartTime(), GetCurrentEndTime(), valueSet);
266
267         if (result < 0) {
268                 AliError(Form("Can't get '%s'! Reason: %s",
269                         alias, AliDCSClient::GetErrorString(result)));
270
271                 if (result == AliDCSClient::fgkServerError) {
272                         AliError(Form("Server error: %s",
273                                 client.GetServerError().Data()));
274                 }
275
276                 return kFALSE;
277         }
278
279         return kTRUE;
280 }
281
282 //______________________________________________________________________________________________
283 const char* AliShuttle::GetFile(Int_t /*system*/, const char* /*detector*/,
284                 const char* /*id*/, const char* /*source*/)
285 {
286
287         AliInfo("You are in AliShuttle::GetFile!");
288         return 0;
289 }
290
291
292 //______________________________________________________________________________________________
293 TList* AliShuttle::GetFileSources(Int_t /*system*/, const char* /*detector*/, const char* /*id*/)
294 {
295
296         AliInfo("You are in AliShuttle::GetFileSources!");
297         return 0;
298 }
299
300 //______________________________________________________________________________________________
301 void AliShuttle::Log(const char* detector, const char* message)
302 {
303
304         TString toLog = Form("%s - %s", detector, message);
305         AliError(toLog.Data());
306
307         fLog += toLog;
308         fLog += "\n";
309
310 }
311
312 //______________________________________________________________________________________________
313 void AliShuttle::StoreLog(Int_t run){
314
315         AliInfo("Printing fLog...");
316         AliInfo(fLog.Data());
317         // Storing log string for runs with errors in "SHUTTLE/SYSTEM/ERRORLOGS"
318         TObjString *logString = new TObjString(fLog);
319         AliCDBId badRunId("SHUTTLE/SYSTEM/ERRORLOGS",run,run);
320         AliCDBMetaData metaData;
321         AliCDBManager::Instance()->GetStorage(fgkLocalUri)
322                                         ->Put(logString, badRunId,&metaData);
323         delete logString;
324
325
326 }