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