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