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