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