3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /// @file AliHLTMisc.cxx
20 /// @author Matthias Richter
22 /// @brief Miscellaneous methods for the HLT AliRoot integration
24 #include "AliHLTMiscImplementation.h"
25 #include "AliHLTLogging.h"
26 #include "AliHLTErrorGuard.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"
41 #include "TStreamerInfo.h"
42 #include "TObjArray.h"
44 /** ROOT macro for the implementation of ROOT specific class methods */
45 ClassImp(AliHLTMiscImplementation);
47 AliHLTMiscImplementation::AliHLTMiscImplementation()
51 AliHLTMiscImplementation::~AliHLTMiscImplementation()
53 // see header file for function documentation
56 int AliHLTMiscImplementation::InitCDB(const char* cdbpath)
58 // see header file for function documentation
60 AliCDBManager* pCDB = AliCDBManager::Instance();
63 log.Logging(kHLTLogError, "InitCDB", "CDB handling", "Could not get CDB instance");
65 if (cdbpath && cdbpath[0]!=0) {
67 if (!uri.BeginsWith("local://") &&
68 !uri.Contains("://")) {
69 // assume a local path if no uri specifier is found
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);
79 log.Logging(kHLTLogInfo, "InitCDB", "CDB handling", "set default URI: %s", cdbUri);
85 int AliHLTMiscImplementation::SetCDBRunNo(int runNo)
87 // see header file for function documentation
89 AliCDBManager* pCDB = AliCDBManager::Instance();
92 log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
99 int AliHLTMiscImplementation::GetCDBRunNo() const
101 // see header file for function documentation
102 AliCDBManager* pCDB = AliCDBManager::Instance();
105 log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
107 return pCDB->GetRun();
112 AliCDBEntry* AliHLTMiscImplementation::LoadOCDBEntry(const char* path, int runNo, int version, int subVersion) const
114 // see header file for function documentation
115 if (!path) return NULL;
118 AliCDBManager* man = AliCDBManager::Instance();
120 ALIHLTERRORGUARD(1, "failed to access CDB manager");
123 if (runNo<0) runNo=man->GetRun();
125 // check the cache first if no specific version required
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);
132 log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "using OCDB object %s from cache", path);
135 log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "invalid OCDB object %s found in cache, not of type AliCDBEntry", path);
140 const char* uri=man->GetURI(path);
142 log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find URI for CDB object \"%s\"", path);
146 AliCDBStorage* store = AliCDBManager::Instance()->GetStorage(uri);
148 log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find storage for URI \"%s\"", uri);
152 TString strUri=store->GetURI();
153 bool bIsGrid=strUri.BeginsWith("alien://");
155 int latest = store->GetLatestVersion(path, runNo);
157 log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "Could not find an entry in the CDB for \"%s\".", path);
160 if (version<0) version=latest;
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);
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
175 return store->Get(path, runNo, version, subVersion);
178 TObject* AliHLTMiscImplementation::ExtractObject(AliCDBEntry* entry) const
180 // see header file for function documentation
181 if (!entry) return NULL;
182 return entry->GetObject();
185 int AliHLTMiscImplementation::CheckOCDBEntries(const TMap* const pMap) const
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
194 if (!pMap) return -EINVAL;
197 AliCDBManager* man = AliCDBManager::Instance();
199 ALIHLTERRORGUARD(1, "failed to access CDB manager");
202 const TMap* pCache=man->GetEntryCache();
204 Int_t runNo = GetCDBRunNo();
206 TIterator* next=pMap->MakeIterator();
207 if (!next) return -EFAULT;
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());
217 // check if the entry has specific storage
218 const char* uri=man->GetURI(pEntry->GetName());
220 log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find URI for CDB object \"%s\"", pEntry->GetName());
224 AliCDBStorage* pStorage = AliCDBManager::Instance()->GetStorage(uri);
226 log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find storage for URI \"%s\"", uri);
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());
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());
244 int AliHLTMiscImplementation::InitMagneticField() const
246 // see header file for function documentation
248 // BAD: unit test fails if I call TGeoGlobalMagField::Instance()
249 // at this point. Something goes wrong in the cleaning of the global
252 if (TGeoGlobalMagField::Instance()->GetField()) {
253 // everything set, but think about storing the currents for
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()) {
271 AliHLTUInt64_t AliHLTMiscImplementation::GetTriggerMask(AliRawReader* rawReader) const
273 // see header file for function documentation
274 if (!rawReader) return 0;
275 AliHLTUInt64_t trgMask=0;
277 const UInt_t* pattern=rawReader->GetTriggerPattern();
278 trgMask=pattern[1]&0xfffffff; // 28 upper bits of the 50 bit mask
280 trgMask|=pattern[0]; // 32 lower bits of the mask
285 AliHLTUInt32_t AliHLTMiscImplementation::GetTimeStamp(AliRawReader* rawReader) const
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");
294 AliHLTUInt32_t AliHLTMiscImplementation::GetEventType(AliRawReader* rawReader) const
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");
303 const char* AliHLTMiscImplementation::GetBeamTypeFromGRP() const
305 // get beam type from GRP
309 Double_t AliHLTMiscImplementation::GetBz()
312 return AliTracker::GetBz();
315 Double_t AliHLTMiscImplementation::GetBz(const Double_t *r)
317 // Returns Bz (kG) at the point "r" .
318 return AliTracker::GetBz(r);
321 void AliHLTMiscImplementation::GetBxByBz(const Double_t r[3], Double_t b[3])
323 // Returns Bx, By and Bz (kG) at the point "r" .
324 return AliTracker::GetBxByBz(r, b);
327 const TClass* AliHLTMiscImplementation::IsAliESDHLTDecision() const
329 // Return the IsA of the AliESDHLTDecision class
330 #ifndef HAVE_NOT_ALIESDHLTDECISION
331 return AliESDHLTDecision::Class();
332 #else // HAVE_NOT_ALIESDHLTDECISION
334 #endif // HAVE_NOT_ALIESDHLTDECISION
337 int AliHLTMiscImplementation::Copy(const AliHLTGlobalTriggerDecision* pDecision, TObject* object) const
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");
351 pESDHLTDecision->~AliESDHLTDecision();
352 new (pESDHLTDecision) AliESDHLTDecision(pDecision->Result(), pDecision->GetTitle());
354 #endif // HAVE_NOT_ALIESDHLTDECISION
358 int AliHLTMiscImplementation::InitStreamerInfos(const char* ocdbEntry) const
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.
366 AliCDBEntry* pEntry=LoadOCDBEntry(ocdbEntry);
367 TObject* pObject=NULL;
368 if (pEntry && (pObject=ExtractObject(pEntry))!=NULL)
370 TObjArray* pSchemas=dynamic_cast<TObjArray*>(pObject);
372 iResult=InitStreamerInfos(pSchemas);
374 AliError(Form("internal mismatch in OCDB entry %s: wrong class type", ocdbEntry));
377 AliWarning(Form("missing HLT reco data (%s), skipping initialization of streamer info for TObjects in HLT raw data payload", ocdbEntry));
382 int AliHLTMiscImplementation::InitStreamerInfos(TObjArray* pSchemas) const
384 // init streamer infos for HLT reconstruction from an array of TStreamerInfo objects
386 for (int i=0; i<pSchemas->GetEntriesFast(); i++) {
387 if (pSchemas->At(i)) {
388 TStreamerInfo* pSchema=dynamic_cast<TStreamerInfo*>(pSchemas->At(i));
390 int version=pSchema->GetClassVersion();
391 TClass* pClass=TClass::GetClass(pSchema->GetName());
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()));
397 TObjArray* pInfos=pClass->GetStreamerInfos();
398 if (pInfos /*&& version<pInfos->GetEntriesFast()*/) {
399 if (pInfos->At(version)==NULL) {
400 TStreamerInfo* pClone=(TStreamerInfo*)pSchema->Clone();
402 pClone->SetClass(pClass);
404 pInfos->AddAtAndExpand(pClone, version);
405 AliDebug(0,Form("adding schema definition %d version %d to class %s", i, version, pSchema->GetName()));
407 AliError(Form("skipping schema definition %d (%s), unable to create clone object", i, pSchema->GetName()));
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()));
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));
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));
421 AliError(Form("skipping schema definition %d (%s), unable to find class", i, pSchema->GetName()));
424 AliError(Form("skipping schema definition %d, not of TStreamerInfo", i));