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