]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/rec/AliHLTMiscImplementation.cxx
correcting error return codes
[u/mrichter/AliRoot.git] / HLT / rec / AliHLTMiscImplementation.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 /// @file   AliHLTMisc.cxx
20 /// @author Matthias Richter
21 /// @date   
22 /// @brief  Miscellaneous methods for the HLT AliRoot integration
23
24 #include "AliHLTMiscImplementation.h"
25 #include "AliHLTLogging.h"
26 #include "AliHLTErrorGuard.h"
27 #include "AliLog.h"
28 #include "AliCDBManager.h"
29 #include "AliCDBStorage.h"
30 #include "AliCDBEntry.h"
31 #include "AliGRPManager.h"
32 #include "AliRawReader.h"
33 #include "AliRawEventHeaderBase.h"
34 #include "AliTracker.h"
35 #ifndef HAVE_NOT_ALIESDHLTDECISION
36 #include "AliESDHLTDecision.h"
37 #endif //HAVE_NOT_ALIESDHLTDECISION
38 #include "TGeoGlobalMagField.h"
39 #include "AliHLTGlobalTriggerDecision.h"
40 #include "TClass.h"
41 #include "TStreamerInfo.h"
42 #include "TObjArray.h"
43
44 /** ROOT macro for the implementation of ROOT specific class methods */
45 ClassImp(AliHLTMiscImplementation);
46
47 AliHLTMiscImplementation::AliHLTMiscImplementation()
48 {
49 }
50
51 AliHLTMiscImplementation::~AliHLTMiscImplementation()
52 {
53   // see header file for function documentation
54 }
55
56 int AliHLTMiscImplementation::InitCDB(const char* cdbpath)
57 {
58   // see header file for function documentation
59   int iResult=0;
60   AliCDBManager* pCDB = AliCDBManager::Instance();
61   AliHLTLogging log;
62   if (!pCDB) {
63     log.Logging(kHLTLogError, "InitCDB", "CDB handling", "Could not get CDB instance");
64   } else {
65     if (cdbpath && cdbpath[0]!=0) {
66       TString uri=cdbpath;
67       if (!uri.BeginsWith("local://") &&
68           !uri.Contains("://")) {
69         // assume a local path if no uri specifier is found
70         uri="local://";
71         uri+=cdbpath;
72       }
73       pCDB->SetDefaultStorage(uri);
74       log.Logging(kHLTLogDebug, "InitCDB", "CDB handling", "CDB instance 0x%x", pCDB);
75     } else if (!pCDB->IsDefaultStorageSet()) {
76       const char* cdbUri="local://$ALICE_ROOT/OCDB";
77       pCDB->SetDefaultStorage(cdbUri);
78       pCDB->SetRun(0);
79       log.Logging(kHLTLogInfo, "InitCDB", "CDB handling", "set default URI: %s", cdbUri);
80     }
81   }
82   return iResult;
83 }
84
85 int AliHLTMiscImplementation::SetCDBRunNo(int runNo)
86 {
87   // see header file for function documentation
88   int iResult=0;
89   AliCDBManager* pCDB = AliCDBManager::Instance();
90   AliHLTLogging log;
91   if (!pCDB) {
92     log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
93   } else {
94     pCDB->SetRun(runNo);
95   }
96   return iResult;
97 }
98
99 int AliHLTMiscImplementation::GetCDBRunNo() const
100 {
101   // see header file for function documentation
102   AliCDBManager* pCDB = AliCDBManager::Instance();
103   AliHLTLogging log;
104   if (!pCDB) {
105     log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
106   } else {
107     return pCDB->GetRun();
108   }
109   return -1;
110 }
111
112 AliCDBEntry* AliHLTMiscImplementation::LoadOCDBEntry(const char* path, int runNo, int version, int subVersion) const
113 {
114   // see header file for function documentation
115   if (!path) return NULL;
116   AliHLTLogging log;
117
118   AliCDBManager* man = AliCDBManager::Instance();
119   if (!man) {
120     ALIHLTERRORGUARD(1, "failed to access CDB manager");
121     return NULL;
122   }
123   if (runNo<0) runNo=man->GetRun();
124
125   // check the cache first if no specific version required
126   if (version<0) {
127     const TMap* pCache=man->GetEntryCache();
128     TObject* pEntryObj=NULL;
129     if (pCache && (pEntryObj=pCache->GetValue(path))!=NULL) {
130       AliCDBEntry* pEntry=dynamic_cast<AliCDBEntry*>(pEntryObj);
131       if (pEntry) {
132         log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "using OCDB object %s from cache", path);
133         return pEntry;
134       } else {
135         log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "invalid OCDB object %s found in cache, not of type AliCDBEntry", path);
136       }
137     }
138   }
139
140   const char* uri=man->GetURI(path);
141   if (!uri) {
142     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find URI for CDB object \"%s\"", path);
143     return NULL;
144   }
145
146   AliCDBStorage* store = AliCDBManager::Instance()->GetStorage(uri);
147   if (!store) {
148     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find storage for URI \"%s\"", uri);
149     return NULL;
150   }
151
152   TString strUri=store->GetURI();
153   bool bIsGrid=strUri.BeginsWith("alien://");
154
155   int latest = store->GetLatestVersion(path, runNo);
156   if (latest<0) {
157     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "Could not find an entry in the CDB for \"%s\".", path);
158     return NULL;
159   }
160   if (version<0) version=latest;
161
162   // OCDB objects on GRID have no sub version
163   if (subVersion<0 && !bIsGrid) subVersion = store->GetLatestSubVersion(path, runNo, version);
164   AliCDBEntry* entry=man->Get(path, runNo, version, subVersion);
165   if (entry) {
166     // there seems to be a problem with the caching of objects in the CDBManager
167     // regardless what version is specified it returns the object from the cache
168     AliCDBId id=entry->GetId();
169     if ((version<0 || id.GetVersion()==version) &&
170         (subVersion<0 || id.GetSubVersion()==subVersion)) {
171       // entry in the cache has the correct version
172       return entry;
173     }
174   }
175   return store->Get(path, runNo, version, subVersion);
176 }
177
178 TObject* AliHLTMiscImplementation::ExtractObject(AliCDBEntry* entry) const
179 {
180   // see header file for function documentation
181   if (!entry) return NULL;
182   return entry->GetObject();
183 }
184
185 int AliHLTMiscImplementation::CheckOCDBEntries(const TMap* const pMap) const
186 {
187   // check the availability of the OCDB entry descriptions in the TMap
188   //  key : complete OCDB path of the entry
189   //  value : auxiliary object - short description
190   // check uses AliCDBStorage::GetLatestVersion, which is the only method
191   // to check the existence without risking AliFatal
192
193   int iResult=0;
194   if (!pMap) return -EINVAL;
195   AliHLTLogging log;
196
197   AliCDBManager* man = AliCDBManager::Instance();
198   if (!man) {
199     ALIHLTERRORGUARD(1, "failed to access CDB manager");
200     return -ENOSYS;
201   }
202   const TMap* pCache=man->GetEntryCache();
203
204   Int_t runNo = GetCDBRunNo();
205
206   TIterator* next=pMap->MakeIterator();
207   if (!next) return -EFAULT;
208
209   TObject* pEntry=NULL;
210   while ((pEntry=next->Next())) {
211     // check if already on cache
212     if (pCache && pCache->GetValue(pEntry->GetName())!=NULL) {
213       log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "found OCDB object %s on cache", pEntry->GetName());      
214       continue;
215     }
216
217     // check if the entry has specific storage
218     const char* uri=man->GetURI(pEntry->GetName());
219     if (!uri) {
220       log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find URI for CDB object \"%s\"", pEntry->GetName());
221       return -ENODEV;
222     }
223
224     AliCDBStorage* pStorage = AliCDBManager::Instance()->GetStorage(uri);
225     if (!pStorage) {
226       log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find storage for URI \"%s\"", uri);
227       return -EBADF;
228     }
229
230     // GetLatestVersion is the only method to check the existence without potential AliFatal
231     if (pStorage->GetLatestVersion(pEntry->GetName(), runNo)<0) {
232       log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find required OCDB object %s for run number %d in storage %s", pEntry->GetName(), runNo, pStorage->GetURI().Data());
233       iResult=-ENOENT;
234     } else {
235       log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "found required OCDB object %s for run number %d in storage %s", pEntry->GetName(), runNo, pStorage->GetURI().Data());
236     }
237   }
238   delete next;
239   next=NULL;
240
241   return iResult;
242 }
243
244 int AliHLTMiscImplementation::InitMagneticField() const
245 {
246   // see header file for function documentation
247
248   // BAD: unit test fails if I call TGeoGlobalMagField::Instance()
249   // at this point. Something goes wrong in the cleaning of the global
250   // ROOT onject
251   /*
252   if (TGeoGlobalMagField::Instance()->GetField()) {
253     // everything set, but think about storing the currents for
254     // a cross-check
255     return 0;
256   }
257   */
258
259   // The magnetic field is initialized once at the start
260   // of run. The fields are supposed to be constant througout the
261   // data taking of one run. The run is aborted if the changes
262   // exceed a certain limit.
263   AliGRPManager grpman;
264   if (grpman.ReadGRPEntry() && grpman.SetMagField()) {
265     return 0;
266   }
267
268   return -ENOENT;
269 }
270
271 AliHLTUInt64_t AliHLTMiscImplementation::GetTriggerMask(AliRawReader* rawReader) const
272 {
273   // see header file for function documentation
274   if (!rawReader) return 0;
275   AliHLTUInt64_t trgMask=0;
276   if (rawReader) {
277     const UInt_t* pattern=rawReader->GetTriggerPattern();
278     trgMask=pattern[1]&0xfffffff; // 28 upper bits of the 50 bit mask
279     trgMask<<=32;
280     trgMask|=pattern[0]; // 32 lower bits of the mask
281   }
282   return trgMask;
283 }
284
285 AliHLTUInt32_t AliHLTMiscImplementation::GetTimeStamp(AliRawReader* rawReader) const
286 {
287   // extract time stamp of the event from the event header
288   if (!rawReader) return 0;
289   const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
290   if (!eventHeader) return 0;
291   return eventHeader->Get("Timestamp");
292 }
293
294 AliHLTUInt32_t AliHLTMiscImplementation::GetEventType(AliRawReader* rawReader) const
295 {
296   // extract event type from the event header
297   if (!rawReader) return 0;
298   const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
299   if (!eventHeader) return 0;
300   return eventHeader->Get("Type");
301 }
302
303 const char* AliHLTMiscImplementation::GetBeamTypeFromGRP() const
304 {
305   // get beam type from GRP
306   return NULL;
307 }
308
309 Double_t AliHLTMiscImplementation::GetBz()
310 {
311   // Returns Bz.
312   return AliTracker::GetBz();
313 }
314
315 Double_t AliHLTMiscImplementation::GetBz(const Double_t *r)
316 {
317   // Returns Bz (kG) at the point "r" .
318   return AliTracker::GetBz(r);
319 }
320
321 void AliHLTMiscImplementation::GetBxByBz(const Double_t r[3], Double_t b[3])
322 {
323   // Returns Bx, By and Bz (kG) at the point "r" .
324   return AliTracker::GetBxByBz(r, b);
325 }
326
327 const TClass* AliHLTMiscImplementation::IsAliESDHLTDecision() const
328 {
329   // Return the IsA of the AliESDHLTDecision class
330 #ifndef HAVE_NOT_ALIESDHLTDECISION
331   return AliESDHLTDecision::Class();
332 #else // HAVE_NOT_ALIESDHLTDECISION
333   return NULL;
334 #endif // HAVE_NOT_ALIESDHLTDECISION
335 }
336
337 int AliHLTMiscImplementation::Copy(const AliHLTGlobalTriggerDecision* pDecision, TObject* object) const
338 {
339   // Copy HLT global trigger decision to AliESDHLTDecision container
340   if (!pDecision || !object) return -EINVAL;
341 #ifndef HAVE_NOT_ALIESDHLTDECISION
342   AliESDHLTDecision* pESDHLTDecision=NULL;
343   if (object->IsA()==NULL ||
344       object->IsA() != AliESDHLTDecision::Class() ||
345       (pESDHLTDecision=dynamic_cast<AliESDHLTDecision*>(object))==NULL) {
346 //     HLTError("can not copy HLT global decision to object of class \"%s\"", 
347 //           object->IsA()?object->IsA()->GetName():"NULL");
348     return -EINVAL;
349   }
350
351   pESDHLTDecision->~AliESDHLTDecision();
352   new (pESDHLTDecision) AliESDHLTDecision(pDecision->Result(), pDecision->GetTitle());
353
354 #endif // HAVE_NOT_ALIESDHLTDECISION
355   return 0;
356 }
357
358 int AliHLTMiscImplementation::InitStreamerInfos(const char* ocdbEntry) const
359 {
360   // init streamer infos for HLT reconstruction
361   // Root schema evolution is not enabled for AliHLTMessage and all streamed objects.
362   // Objects in the raw data payload rely on the availability of the correct stream info.
363   // The relevant streamer info for a specific run is stored in the OCDB.
364   int iResult=0;
365
366   AliCDBEntry* pEntry=LoadOCDBEntry(ocdbEntry);
367   TObject* pObject=NULL;
368   if (pEntry && (pObject=ExtractObject(pEntry))!=NULL)
369     {
370     TObjArray* pSchemas=dynamic_cast<TObjArray*>(pObject);
371     if (pSchemas) {
372       iResult=InitStreamerInfos(pSchemas);
373     } else {
374       AliError(Form("internal mismatch in OCDB entry %s: wrong class type", ocdbEntry));
375     }
376   } else {
377     AliWarning(Form("missing HLT reco data (%s), skipping initialization of streamer info for TObjects in HLT raw data payload", ocdbEntry));
378   }
379   return iResult;
380 }
381
382 int AliHLTMiscImplementation::InitStreamerInfos(TObjArray* pSchemas) const
383 {
384   // init streamer infos for HLT reconstruction from an array of TStreamerInfo objects
385
386   for (int i=0; i<pSchemas->GetEntriesFast(); i++) {
387     if (pSchemas->At(i)) {
388       TStreamerInfo* pSchema=dynamic_cast<TStreamerInfo*>(pSchemas->At(i));
389       if (pSchema) {
390         int version=pSchema->GetClassVersion();
391         TClass* pClass=TClass::GetClass(pSchema->GetName());
392         if (pClass) {
393           if (pClass->GetClassVersion()==version) {
394             AliDebug(0,Form("skipping schema definition %d version %d to class %s as this is the native version", i, version, pSchema->GetName()));
395             continue;
396           }
397           TObjArray* pInfos=pClass->GetStreamerInfos();
398           if (pInfos /*&& version<pInfos->GetEntriesFast()*/) {
399             if (pInfos->At(version)==NULL) {
400               TStreamerInfo* pClone=(TStreamerInfo*)pSchema->Clone();
401               if (pClone) {
402                 pClone->SetClass(pClass);
403                 pClone->BuildOld();
404                 pInfos->AddAtAndExpand(pClone, version);
405                 AliDebug(0,Form("adding schema definition %d version %d to class %s", i, version, pSchema->GetName()));
406               } else {
407                 AliError(Form("skipping schema definition %d (%s), unable to create clone object", i, pSchema->GetName()));
408               }
409             } else {
410               TStreamerInfo* pInfo=dynamic_cast<TStreamerInfo*>(pInfos->At(version));
411               if (pInfo && pInfo->GetClassVersion()==version) {
412                 AliDebug(0,Form("schema definition %d version %d already available in class %s, skipping ...", i, version, pSchema->GetName()));
413               } else {
414                 AliError(Form("can not verify version for already existing schema definition %d (%s) version %d: version of existing definition is %d", i, pSchema->GetName(), version, pInfo?pInfo->GetClassVersion():-1));
415               }
416             }
417           } else {
418             AliError(Form("skipping schema definition %d (%s), unable to set version %d in info array of size %d", i, pSchema->GetName(), version, pInfos?pInfos->GetEntriesFast():-1));
419           }
420         } else {
421           AliError(Form("skipping schema definition %d (%s), unable to find class", i, pSchema->GetName()));
422         }
423       } else {
424         AliError(Form("skipping schema definition %d, not of TStreamerInfo", i));
425       }
426     }
427   }
428
429   return 0;
430 }