coding conventions (Alberto)
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttle.cxx
CommitLineData
73abe331 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$
58bc3020 18Revision 1.3 2006/06/06 14:26:40 jgrosseo
19o) removed files that were moved to STEER
20o) shuttle updated to follow the new interface (Alberto)
21
b948db8d 22Revision 1.2 2006/03/07 07:52:34 hristov
23New version (B.Yordanov)
24
d477ad88 25Revision 1.6 2005/11/19 17:19:14 byordano
26RetrieveDATEEntries and RetrieveConditionsData added
27
28Revision 1.5 2005/11/19 11:09:27 byordano
29AliShuttle declaration added
30
31Revision 1.4 2005/11/17 17:47:34 byordano
32TList changed to TObjArray
33
34Revision 1.3 2005/11/17 14:43:23 byordano
35import to local CVS
36
37Revision 1.1.1.1 2005/10/28 07:33:58 hristov
38Initial import as subdirectory in AliRoot
39
73abe331 40Revision 1.2 2005/09/13 08:41:15 byordano
41default startTime endTime added
42
43Revision 1.4 2005/08/30 09:13:02 byordano
44some docs added
45
46Revision 1.3 2005/08/29 21:15:47 byordano
47some 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
b948db8d 54// interface methods of AliPreprocessor.
73abe331 55// For every detector in AliShuttleConfgi (see AliShuttleConfig),
56// data for its set of aliases is retrieved. If there is registered
b948db8d 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
73abe331 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"
73abe331 69#include "AliShuttleConfig.h"
70#include "AliDCSClient.h"
71#include "AliLog.h"
b948db8d 72#include "AliPreprocessor.h"
73#include "AliDefaultPreprocessor.h"
73abe331 74
58bc3020 75#include <TObject.h>
b948db8d 76#include <TString.h>
73abe331 77#include <TObjString.h>
78
79ClassImp(AliShuttle)
80
b948db8d 81TString AliShuttle::fgkLocalUri("local://ShuttleCDB");
82
83//______________________________________________________________________________________________
84AliShuttle::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("")
73abe331 91{
92 //
93 // config: AliShuttleConfig used
b948db8d 94 // mainStorage: underlying AliCDBStorage
95 // localStorage (local) CDB storage to be used if mainStorage is unavailable
73abe331 96 // timeout: timeout used for AliDCSClient connection
97 // retries: the number of retries in case of connection error.
98 //
99
b948db8d 100 RegisterPreprocessor(new AliDefaultPreprocessor("DEFAULT", 0));
101
73abe331 102}
103
58bc3020 104//______________________________________________________________________
105AliShuttle::AliShuttle(const AliShuttle& /*other*/):
106AliShuttleInterface()
107{
108// copy constructor (not implemented)
109
110}
111
112//______________________________________________________________________
113AliShuttle &AliShuttle::operator=(const AliShuttle& /*other*/)
114{
115// assignment operator (not implemented)
116
117return *this;
118}
119
b948db8d 120//______________________________________________________________________________________________
58bc3020 121AliShuttle::~AliShuttle()
122{
123// destructor
124
b948db8d 125 fPreprocessorMap.DeleteAll();
73abe331 126}
127
b948db8d 128//______________________________________________________________________________________________
58bc3020 129void AliShuttle::RegisterPreprocessor(AliPreprocessor* preprocessor)
130{
73abe331 131 //
b948db8d 132 // Registers new AliPreprocessor.
73abe331 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
b948db8d 138 if (fPreprocessorMap.GetValue(preprocessor->GetName())) {
139 AliWarning(Form("AliPreprocessor %s is already registered!",
140 preprocessor->GetName()));
73abe331 141 return;
142 }
143
b948db8d 144 fPreprocessorMap.Add(new TObjString(preprocessor->GetName()), preprocessor);
73abe331 145}
146
b948db8d 147//______________________________________________________________________________________________
148UInt_t AliShuttle::Store(const char* detector,
73abe331 149 TObject* object, AliCDBMetaData* metaData)
150{
b948db8d 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;
73abe331 159 }
160
b948db8d 161 AliCDBId id(AliCDBPath(detector, "DCS", "Data"),
73abe331 162 GetCurrentRun(), GetCurrentRun());
b948db8d 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
73abe331 182}
183
b948db8d 184//______________________________________________________________________________________________
58bc3020 185Bool_t AliShuttle::Process(Int_t run, UInt_t startTime, UInt_t endTime)
186{
73abe331 187 //
b948db8d 188 // Makes data retrieval for all detectors in the configuration.
73abe331 189 // run: is the run number used
190 // startTime: is the run start time
191 // endTime: is the run end time
d477ad88 192 // Returns kFALSE in case of error occured and kTRUE otherwise
73abe331 193 //
194
d477ad88 195 Bool_t hasError = kFALSE;
196
b948db8d 197 TIter iter(fConfig->GetDetectors());
73abe331 198 TObjString* aDetector;
b948db8d 199
200 ClearLog();
201
73abe331 202 while ((aDetector = (TObjString*) iter.Next())) {
b948db8d 203 if(!fConfig->HostProcessDetector(aDetector->GetName())) continue;
d477ad88 204 if(!Process(run, startTime, endTime, aDetector->String())) {
205 hasError = kTRUE;
206 }
73abe331 207 }
d477ad88 208
b948db8d 209 if(fLog != "") StoreLog(run);
210
d477ad88 211 return !hasError;
73abe331 212}
213
b948db8d 214//______________________________________________________________________________________________
d477ad88 215Bool_t AliShuttle::Process(Int_t run, UInt_t startTime, UInt_t endTime,
73abe331 216 const char* detector)
217{
218 //
b948db8d 219 // Makes data retrieval just for one specific detector.
73abe331 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
d477ad88 225 // Returns kFALSE in case of error occured and kTRUE otherwise
73abe331 226 //
227
228 AliInfo(Form("Retrieving values for %s, run %d", detector, run));
229
230 if (!fConfig->HasDetector(detector)) {
b948db8d 231 Log(detector, "There isn't any configuration for %s !");
d477ad88 232 return kFALSE;
73abe331 233 }
234
235 fCurrentRun = run;
236 fCurrentStartTime = startTime;
237 fCurrentEndTime = endTime;
238
b948db8d 239 TString host(fConfig->GetDCSHost(detector));
240 Int_t port = fConfig->GetDCSPort(detector);
73abe331 241
b948db8d 242 TIter iter(fConfig->GetDCSAliases(detector));
73abe331 243 TObjString* anAlias;
b948db8d 244 TMap aliasMap;
73abe331 245
d477ad88 246 Bool_t hasError = kFALSE;
b948db8d 247 Bool_t result=kFALSE;
d477ad88 248
b948db8d 249 while ((anAlias = (TObjString*) iter.Next())) {
d477ad88 250 TObjArray valueSet;
b948db8d 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;
73abe331 259 }
260 }
b948db8d 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
73abe331 274 fCurrentRun = -1;
275 fCurrentStartTime = 0;
276 fCurrentEndTime = 0;
d477ad88 277
278 return !hasError;
73abe331 279}
280
b948db8d 281//______________________________________________________________________________________________
73abe331 282Bool_t AliShuttle::GetValueSet(const char* host, Int_t port, const char* alias,
d477ad88 283 TObjArray& valueSet)
73abe331 284{
58bc3020 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
73abe331 290 AliDCSClient client(host, port, fTimeout, fRetries);
291 if (!client.IsConnected()) {
b948db8d 292 return kFALSE;
73abe331 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}
b948db8d 312
313//______________________________________________________________________________________________
314const char* AliShuttle::GetFile(Int_t /*system*/, const char* /*detector*/,
315 const char* /*id*/, const char* /*source*/)
316{
58bc3020 317// Get calibration file from DAQ transient file system
b948db8d 318
319 AliInfo("You are in AliShuttle::GetFile!");
320 return 0;
321}
322
323
324//______________________________________________________________________________________________
325TList* AliShuttle::GetFileSources(Int_t /*system*/, const char* /*detector*/, const char* /*id*/)
326{
58bc3020 327// Get list of sources that provided the files to be retrieved from DAQ
b948db8d 328
329 AliInfo("You are in AliShuttle::GetFileSources!");
330 return 0;
331}
332
333//______________________________________________________________________________________________
334void AliShuttle::Log(const char* detector, const char* message)
335{
58bc3020 336// Fill log string with a message
b948db8d 337
338 TString toLog = Form("%s - %s", detector, message);
339 AliError(toLog.Data());
340
341 fLog += toLog;
342 fLog += "\n";
343
344}
345
346//______________________________________________________________________________________________
58bc3020 347void AliShuttle::StoreLog(Int_t run)
348{
349// store error log string to SHUTTLE/SYSTEM/ERROR (on local storage)
b948db8d 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}