Line data Source code
1 : // $Id$
2 :
3 : //**************************************************************************
4 : //* This file is property of and copyright by the *
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 2009-07-07
22 : /// @brief Implementation of various glue functions implemented in dynamically
23 : /// loaded libraries
24 :
25 : #include "AliHLTMiscImplementation.h"
26 : #include "AliHLTLogging.h"
27 : #include "AliHLTErrorGuard.h"
28 : #include "AliLog.h"
29 : #include "AliCDBManager.h"
30 : #include "AliCDBStorage.h"
31 : #include "AliCDBEntry.h"
32 : #include "AliGRPManager.h"
33 : #include "AliRawReader.h"
34 : #include "AliRawEventHeaderBase.h"
35 : #include "AliTracker.h"
36 : #include "AliESDtrack.h"
37 : #include "AliESDHLTDecision.h"
38 : #include "TGeoGlobalMagField.h"
39 : #include "AliHLTGlobalTriggerDecision.h"
40 : #include "TSystem.h"
41 : #include "TClass.h"
42 : #include "TStreamerInfo.h"
43 : #include "TObjArray.h"
44 : #include <stdexcept>
45 :
46 : /** ROOT macro for the implementation of ROOT specific class methods */
47 6 : ClassImp(AliHLTMiscImplementation);
48 :
49 2 : AliHLTMiscImplementation::AliHLTMiscImplementation()
50 10 : {
51 4 : }
52 :
53 : AliHLTMiscImplementation::~AliHLTMiscImplementation()
54 0 : {
55 : // see header file for function documentation
56 0 : }
57 :
58 : int AliHLTMiscImplementation::InitCDB(const char* cdbpath, const char* cdbsnapshot)
59 : {
60 : // see header file for function documentation
61 : int iResult=0;
62 0 : AliCDBManager* pCDB = AliCDBManager::Instance();
63 0 : AliHLTLogging log;
64 0 : if (!pCDB) {
65 0 : log.Logging(kHLTLogError, "InitCDB", "CDB handling", "Could not get CDB instance");
66 : } else {
67 0 : if (cdbpath && cdbpath[0]!=0) {
68 0 : TString uri=cdbpath;
69 0 : if (!uri.BeginsWith("local://") &&
70 0 : !uri.Contains("://")) {
71 : // assume a local path if no uri specifier is found
72 0 : uri="local://";
73 0 : uri+=cdbpath;
74 : }
75 0 : pCDB->SetDefaultStorage(uri);
76 0 : log.Logging(kHLTLogDebug, "InitCDB", "CDB handling", "CDB instance 0x%x", pCDB);
77 0 : } else if (!pCDB->IsDefaultStorageSet()) {
78 : const char* cdbUri="local://$ALICE_ROOT/OCDB";
79 0 : pCDB->SetDefaultStorage(cdbUri);
80 0 : pCDB->SetRun(0);
81 0 : log.Logging(kHLTLogInfo, "InitCDB", "CDB handling", "set default URI: %s", cdbUri);
82 0 : }
83 : }
84 0 : if (cdbsnapshot != NULL) {
85 0 : gSystem->Load("libGeom");
86 0 : gSystem->Load("libGeomBuilder");
87 0 : log.Logging(kHLTLogInfo, "InitCDB", "CDB Snapshot", "Running in snapshot mode: %s", cdbsnapshot);
88 0 : pCDB->SetSnapshotMode(cdbsnapshot);
89 : }
90 : return iResult;
91 0 : }
92 :
93 : int AliHLTMiscImplementation::SetCDBRunNo(int runNo)
94 : {
95 : // see header file for function documentation
96 : int iResult=0;
97 0 : AliCDBManager* pCDB = AliCDBManager::Instance();
98 0 : AliHLTLogging log;
99 0 : if (!pCDB) {
100 0 : log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
101 : } else {
102 0 : pCDB->SetRun(runNo);
103 : }
104 : return iResult;
105 0 : }
106 :
107 : int AliHLTMiscImplementation::GetCDBRunNo() const
108 : {
109 : // see header file for function documentation
110 0 : AliCDBManager* pCDB = AliCDBManager::Instance();
111 0 : AliHLTLogging log;
112 0 : if (!pCDB) {
113 0 : log.Logging(kHLTLogError, "SetCDBRunNo", "CDB handling", "Could not get CDB instance");
114 : } else {
115 0 : return pCDB->GetRun();
116 : }
117 0 : return -1;
118 0 : }
119 :
120 : AliCDBEntry* AliHLTMiscImplementation::LoadOCDBEntry(const char* path, int runNo) const
121 : {
122 : // see header file for function documentation
123 0 : if (!path) return NULL;
124 0 : AliHLTLogging log;
125 :
126 0 : AliCDBManager* man = AliCDBManager::Instance();
127 0 : if (!man) {
128 0 : ALIHLTERRORGUARD(1, "failed to access CDB manager");
129 0 : return NULL;
130 : }
131 0 : if (runNo<0) runNo=man->GetRun();
132 :
133 : AliCDBEntry* entry=NULL;
134 : try {
135 : // exceptions for the loading of OCDB objects have been introduced in r61012 on
136 : // Feb 20 2013. This allows to reduce this function to try and catch of AliCDBManager::Get
137 0 : AliCDBStorage* storage = man->GetDefaultStorage();
138 0 : if (storage && storage->GetId(path, runNo) ){ // check the path before calling Get() in order to avoid call of AliFatal() in Grid CDB manager
139 0 : entry=man->Get(path, runNo);
140 0 : }
141 0 : }
142 : catch (...) {
143 : // ignore the exception, missing object can be a valid condition, and is handled
144 : // downstream
145 0 : }
146 : return entry;
147 0 : }
148 :
149 : TObject* AliHLTMiscImplementation::ExtractObject(AliCDBEntry* entry) const
150 : {
151 : // see header file for function documentation
152 0 : if (!entry) return NULL;
153 0 : return entry->GetObject();
154 0 : }
155 :
156 : int AliHLTMiscImplementation::CheckOCDBEntries(const TMap* const pMap) const
157 : {
158 : // check the availability of the OCDB entry descriptions in the TMap
159 : // key : complete OCDB path of the entry
160 : // value : auxiliary object - short description
161 : // check uses AliCDBStorage::GetLatestVersion, which is the only method
162 : // to check the existence without risking AliFatal
163 :
164 : int iResult=0;
165 0 : if (!pMap) return -EINVAL;
166 0 : AliHLTLogging log;
167 :
168 0 : AliCDBManager* man = AliCDBManager::Instance();
169 0 : if (!man) {
170 0 : ALIHLTERRORGUARD(1, "failed to access CDB manager");
171 0 : return -ENOSYS;
172 : }
173 0 : Int_t runNo = GetCDBRunNo();
174 :
175 0 : TIterator* next=pMap->MakeIterator();
176 0 : if (!next) return -EFAULT;
177 :
178 : TObject* pEntry=NULL;
179 0 : while ((pEntry=next->Next())) {
180 : try {
181 : // exceptions for the loading of OCDB objects have been introduced in r61012 on
182 : // Feb 20 2013. This allows to reduce this function to try and catch of AliCDBManager::Get
183 0 : man->Get(pEntry->GetName(), runNo);
184 0 : }
185 : catch (...) {
186 : // find out the storage for more details in the error message
187 : AliCDBStorage* pStorage=NULL;
188 0 : const char* uri=man->GetURI(pEntry->GetName());
189 0 : if (uri) {
190 0 : pStorage = AliCDBManager::Instance()->GetStorage(uri);
191 0 : }
192 :
193 0 : 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!=NULL?pStorage->GetURI().Data():"<unavailable>"));
194 : iResult=-ENOENT;
195 0 : }
196 : }
197 0 : delete next;
198 : next=NULL;
199 :
200 : return iResult;
201 0 : }
202 :
203 : int AliHLTMiscImplementation::InitMagneticField() const
204 : {
205 : // see header file for function documentation
206 :
207 : // BAD: unit test fails if I call TGeoGlobalMagField::Instance()
208 : // at this point. Something goes wrong in the cleaning of the global
209 : // ROOT onject
210 : /*
211 : if (TGeoGlobalMagField::Instance()->GetField()) {
212 : // everything set, but think about storing the currents for
213 : // a cross-check
214 : return 0;
215 : }
216 : */
217 :
218 : // The magnetic field is initialized once at the start
219 : // of run. The fields are supposed to be constant througout the
220 : // data taking of one run. The run is aborted if the changes
221 : // exceed a certain limit.
222 0 : AliGRPManager grpman;
223 0 : if (grpman.ReadGRPEntry() && grpman.SetMagField()) {
224 0 : return 0;
225 : }
226 :
227 0 : return -ENOENT;
228 0 : }
229 :
230 : AliHLTTriggerMask_t AliHLTMiscImplementation::GetTriggerMask(AliRawReader* rawReader) const
231 : {
232 : // see header file for function documentation
233 0 : if (!rawReader) return 0;
234 0 : AliHLTTriggerMask_t trgMask=0;
235 0 : if (rawReader) {
236 0 : const UInt_t* pattern=rawReader->GetTriggerPattern();
237 0 : if(rawReader->GetVersion()==3){
238 0 : trgMask=pattern[3];
239 0 : trgMask<<=32;
240 0 : trgMask|=pattern[2];
241 0 : trgMask<<=32;
242 0 : }
243 0 : trgMask|=pattern[1];
244 0 : trgMask<<=32;
245 0 : trgMask|=pattern[0]; // 32 lower bits of the mask
246 0 : }
247 0 : return trgMask;
248 0 : }
249 :
250 : AliHLTUInt32_t AliHLTMiscImplementation::GetTimeStamp(AliRawReader* rawReader) const
251 : {
252 : // extract time stamp of the event from the event header
253 0 : if (!rawReader) return kMaxUInt;
254 0 : const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
255 0 : if (!eventHeader) return kMaxUInt;
256 0 : return eventHeader->Get("Timestamp");
257 0 : }
258 :
259 : AliHLTUInt32_t AliHLTMiscImplementation::GetEventType(AliRawReader* rawReader) const
260 : {
261 : // extract event type from the event header
262 0 : if (!rawReader) return 0;
263 0 : const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
264 0 : if (!eventHeader) return 0;
265 0 : UInt_t daqType = eventHeader->Get("Type");
266 0 : switch(daqType){
267 : case AliRawEventHeaderBase::kStartOfRun:
268 : case AliRawEventHeaderBase::kStartOfData:
269 0 : return gkAliEventTypeStartOfRun;
270 :
271 : case AliRawEventHeaderBase::kEndOfRun:
272 : case AliRawEventHeaderBase::kEndOfData:
273 0 : return gkAliEventTypeEndOfRun;
274 :
275 : case AliRawEventHeaderBase::kPhysicsEvent:
276 0 : return gkAliEventTypeData;
277 :
278 : case AliRawEventHeaderBase::kCalibrationEvent:
279 0 : return gkAliEventTypeCalibration;
280 :
281 : case AliRawEventHeaderBase::kFormatError:
282 0 : return gkAliEventTypeCorruptID;
283 :
284 : case AliRawEventHeaderBase::kSystemSoftwareTriggerEvent:
285 : case AliRawEventHeaderBase::kDetectorSoftwareTriggerEvent:
286 0 : return gkAliEventTypeSoftware;
287 :
288 : // TODO: Sync Event Type not implemented!
289 : //case AliRawEventHeaderBase::kSyncEvent:
290 : }
291 0 : return gkAliEventTypeUnknown;
292 0 : }
293 :
294 : const char* AliHLTMiscImplementation::GetBeamTypeFromGRP() const
295 : {
296 : // get beam type from GRP
297 0 : return NULL;
298 : }
299 :
300 : Double_t AliHLTMiscImplementation::GetBz()
301 : {
302 : // Returns Bz.
303 0 : return AliTracker::GetBz();
304 : }
305 :
306 : Double_t AliHLTMiscImplementation::GetBz(const Double_t *r)
307 : {
308 : // Returns Bz (kG) at the point "r" .
309 0 : return AliTracker::GetBz(r);
310 : }
311 :
312 : void AliHLTMiscImplementation::GetBxByBz(const Double_t r[3], Double_t b[3])
313 : {
314 : // Returns Bx, By and Bz (kG) at the point "r" .
315 0 : return AliTracker::GetBxByBz(r, b);
316 : }
317 :
318 : const TClass* AliHLTMiscImplementation::IsAliESDHLTDecision() const
319 : {
320 : // Return the IsA of the AliESDHLTDecision class
321 20 : return AliESDHLTDecision::Class();
322 : }
323 :
324 : int AliHLTMiscImplementation::Copy(const AliHLTGlobalTriggerDecision* pDecision, TObject* object) const
325 : {
326 : // Copy HLT global trigger decision to AliESDHLTDecision container
327 16 : if (!pDecision || !object) return -EINVAL;
328 : AliESDHLTDecision* pESDHLTDecision=NULL;
329 16 : if (object->IsA()==NULL ||
330 8 : object->IsA() != AliESDHLTDecision::Class() ||
331 24 : (pESDHLTDecision=dynamic_cast<AliESDHLTDecision*>(object))==NULL) {
332 : // HLTError("can not copy HLT global decision to object of class \"%s\"",
333 : // object->IsA()?object->IsA()->GetName():"NULL");
334 0 : return -EINVAL;
335 : }
336 :
337 8 : pESDHLTDecision->~AliESDHLTDecision();
338 24 : new (pESDHLTDecision) AliESDHLTDecision(pDecision->Result(), pDecision->GetTitle());
339 :
340 8 : return 0;
341 8 : }
342 :
343 : int AliHLTMiscImplementation::InitStreamerInfos(const char* ocdbEntry) const
344 : {
345 : // init streamer infos for HLT reconstruction
346 : // Root schema evolution is not enabled for AliHLTMessage and all streamed objects.
347 : // Objects in the raw data payload rely on the availability of the correct stream info.
348 : // The relevant streamer info for a specific run is stored in the OCDB.
349 : int iResult=0;
350 :
351 0 : AliCDBEntry* pEntry=LoadOCDBEntry(ocdbEntry);
352 : TObject* pObject=NULL;
353 0 : if (pEntry && (pObject=ExtractObject(pEntry))!=NULL)
354 : {
355 0 : TObjArray* pSchemas=dynamic_cast<TObjArray*>(pObject);
356 0 : if (pSchemas) {
357 0 : iResult=InitStreamerInfos(pSchemas);
358 0 : } else {
359 0 : AliError(Form("internal mismatch in OCDB entry %s: wrong class type", ocdbEntry));
360 : }
361 0 : } else {
362 0 : AliWarning(Form("missing HLT reco data (%s), skipping initialization of streamer info for TObjects in HLT raw data payload", ocdbEntry));
363 : }
364 0 : return iResult;
365 : }
366 :
367 : int AliHLTMiscImplementation::InitStreamerInfos(TObjArray* pSchemas) const
368 : {
369 : // init streamer infos for HLT reconstruction from an array of TStreamerInfo objects
370 :
371 0 : for (int i=0; i<pSchemas->GetEntriesFast(); i++) {
372 0 : if (pSchemas->At(i)) {
373 0 : TStreamerInfo* pSchema=dynamic_cast<TStreamerInfo*>(pSchemas->At(i));
374 0 : if (pSchema) {
375 0 : int version=pSchema->GetClassVersion();
376 0 : TClass* pClass=TClass::GetClass(pSchema->GetName());
377 0 : if (pClass) {
378 0 : if (pClass->GetClassVersion()==version) {
379 0 : AliDebug(0,Form("skipping schema definition %d version %d to class %s as this is the native version", i, version, pSchema->GetName()));
380 0 : continue;
381 : }
382 0 : TObjArray* pInfos=const_cast<TObjArray*>(pClass->GetStreamerInfos());
383 0 : if (pInfos /*&& version<pInfos->GetEntriesFast()*/) {
384 0 : if (pInfos->At(version)==NULL) {
385 0 : TStreamerInfo* pClone=(TStreamerInfo*)pSchema->Clone();
386 0 : if (pClone) {
387 0 : pClone->SetClass(pClass);
388 0 : pClone->BuildOld();
389 0 : pInfos->AddAtAndExpand(pClone, version);
390 0 : AliDebug(0,Form("adding schema definition %d version %d to class %s", i, version, pSchema->GetName()));
391 : } else {
392 0 : AliError(Form("skipping schema definition %d (%s), unable to create clone object", i, pSchema->GetName()));
393 : }
394 0 : } else {
395 0 : TStreamerInfo* pInfo=dynamic_cast<TStreamerInfo*>(pInfos->At(version));
396 0 : if (pInfo && pInfo->GetClassVersion()==version) {
397 0 : AliDebug(0,Form("schema definition %d version %d already available in class %s, skipping ...", i, version, pSchema->GetName()));
398 : } else {
399 0 : 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));
400 : }
401 : }
402 : } else {
403 0 : 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));
404 : }
405 0 : } else {
406 0 : AliError(Form("skipping schema definition %d (%s), unable to find class", i, pSchema->GetName()));
407 : }
408 0 : } else {
409 0 : AliError(Form("skipping schema definition %d, not of TStreamerInfo", i));
410 : }
411 0 : }
412 : }
413 :
414 0 : return 0;
415 : }
416 :
417 : int AliHLTMiscImplementation::MergeStreamerInfo(TObjArray* tgt, const TObjArray* src, int iVerbosity) const
418 : {
419 : /// merge streamer info entries from source array to target array
420 : /// return 1 if target array has been changed
421 :
422 : // add all existing infos if not existing in the current one, or having
423 : // different class version
424 : int iResult=0;
425 0 : if (!tgt || !src) return -EINVAL;
426 :
427 : {
428 : // check if all infos from the existing entry are in the new entry and with
429 : // identical class version
430 0 : TIter next(src);
431 : TObject* nextobj=NULL;
432 0 : while ((nextobj=next())) {
433 0 : TStreamerInfo* srcInfo=dynamic_cast<TStreamerInfo*>(nextobj);
434 0 : if (!srcInfo) continue;
435 0 : TString srcInfoName=srcInfo->GetName();
436 :
437 : int i=0;
438 0 : for (; i<tgt->GetEntriesFast(); i++) {
439 0 : if (tgt->At(i)==NULL) continue;
440 0 : if (srcInfoName.CompareTo(tgt->At(i)->GetName())!=0) continue;
441 : // TODO: 2010-08-23 some more detailed investigation is needed.
442 : // Structures used for data exchange, e.g. AliHLTComponentDataType
443 : // or AliHLTEventDDLV1 do not have a class version, but need to be stored in the
444 : // streamer info. Strictly speaking not, because those structures are not supposed
445 : // to be changed at all, so they should be the same in all versions in the future.
446 : // There has been a problem with detecting whether the streamer info is already in
447 : // the target array if the srcInfo has class version -1. As it just concerns
448 : // structures not going to be changed we can safely skip checking the class version,
449 : // as long as the entry is already in the target streamer infos it does not need
450 : // to be copied again.
451 0 : if (srcInfo->GetClassVersion()<0) break;
452 0 : TStreamerInfo* tgtInfo=dynamic_cast<TStreamerInfo*>(tgt->At(i));
453 0 : if (tgtInfo && tgtInfo->GetClassVersion()==srcInfo->GetClassVersion()) break;
454 0 : }
455 0 : if (i<tgt->GetEntriesFast()) continue;
456 :
457 : iResult=1;
458 0 : if (iVerbosity>0) {
459 0 : AliInfo(Form("adding streamer info for class %s version %d", srcInfoName.Data(), srcInfo->GetClassVersion()));
460 : }
461 0 : tgt->Add(srcInfo);
462 0 : }
463 0 : }
464 :
465 0 : return iResult;
466 0 : }
467 :
468 : void AliHLTMiscImplementation::SetAliESDtrackOnlineModeFlag(bool mode) const
469 : {
470 : /// set the online mode flag of AliESDtrack
471 0 : AliESDtrack::OnlineMode(mode);
472 0 : }
473 :
474 : bool AliHLTMiscImplementation::GetAliESDtrackOnlineModeFlag() const
475 : {
476 : /// get status of the online mode flag of AliESDtrack
477 0 : return AliESDtrack::OnlineMode();
478 : }
|