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