]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/rec/AliHLTMiscImplementation.cxx
Updating MultiplicityCorrelations Component
[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 #include "AliESDtrack.h"
36 #include "AliESDHLTDecision.h"
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) const
112 {
113   // see header file for function documentation
114   if (!path) return NULL;
115   AliHLTLogging log;
116
117   AliCDBManager* man = AliCDBManager::Instance();
118   if (!man) {
119     ALIHLTERRORGUARD(1, "failed to access CDB manager");
120     return NULL;
121   }
122   if (runNo<0) runNo=man->GetRun();
123
124   // check the cache first if no specific version required
125   { //condition was deprecated, but keep for formatting
126     const TMap* pCache=man->GetEntryCache();
127     TObject* pEntryObj=NULL;
128     if (pCache && (pEntryObj=pCache->GetValue(path))!=NULL) {
129       AliCDBEntry* pEntry=dynamic_cast<AliCDBEntry*>(pEntryObj);
130       if (pEntry) {
131         log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "using OCDB object %s from cache", path);
132         return pEntry;
133       } else {
134         log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "invalid OCDB object %s found in cache, not of type AliCDBEntry", path);
135       }
136     }
137   }
138
139   const char* uri=man->GetURI(path);
140   if (!uri) {
141     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find URI for CDB object \"%s\"", path);
142     return NULL;
143   }
144
145   AliCDBStorage* store = AliCDBManager::Instance()->GetStorage(uri);
146   if (!store) {
147     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "can not find storage for URI \"%s\"", uri);
148     return NULL;
149   }
150
151   TString strUri=store->GetURI();
152
153   int latest = store->GetLatestVersion(path, runNo);
154   if (latest<0) {
155     log.Logging(kHLTLogError, "AliHLTMiscImplementation::LoadOCDBEntry", "CDB handling", "Could not find an entry in the CDB for \"%s\".", path);
156     return NULL;
157   }
158
159   AliCDBEntry* entry=man->Get(path, runNo);
160   return entry;
161 }
162
163 TObject* AliHLTMiscImplementation::ExtractObject(AliCDBEntry* entry) const
164 {
165   // see header file for function documentation
166   if (!entry) return NULL;
167   return entry->GetObject();
168 }
169
170 int AliHLTMiscImplementation::CheckOCDBEntries(const TMap* const pMap) const
171 {
172   // check the availability of the OCDB entry descriptions in the TMap
173   //  key : complete OCDB path of the entry
174   //  value : auxiliary object - short description
175   // check uses AliCDBStorage::GetLatestVersion, which is the only method
176   // to check the existence without risking AliFatal
177
178   int iResult=0;
179   if (!pMap) return -EINVAL;
180   AliHLTLogging log;
181
182   AliCDBManager* man = AliCDBManager::Instance();
183   if (!man) {
184     ALIHLTERRORGUARD(1, "failed to access CDB manager");
185     return -ENOSYS;
186   }
187   const TMap* pCache=man->GetEntryCache();
188
189   Int_t runNo = GetCDBRunNo();
190
191   TIterator* next=pMap->MakeIterator();
192   if (!next) return -EFAULT;
193
194   TObject* pEntry=NULL;
195   while ((pEntry=next->Next())) {
196     // check if already on cache
197     if (pCache && pCache->GetValue(pEntry->GetName())!=NULL) {
198       log.Logging(kHLTLogDebug, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "found OCDB object %s on cache", pEntry->GetName());      
199       continue;
200     }
201
202     // check if the entry has specific storage
203     const char* uri=man->GetURI(pEntry->GetName());
204     if (!uri) {
205       log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find URI for CDB object \"%s\"", pEntry->GetName());
206       return -ENODEV;
207     }
208
209     AliCDBStorage* pStorage = AliCDBManager::Instance()->GetStorage(uri);
210     if (!pStorage) {
211       log.Logging(kHLTLogError, "AliHLTMiscImplementation::CheckOCDBEntries", "CDB handling", "can not find storage for URI \"%s\"", uri);
212       return -EBADF;
213     }
214
215     // GetLatestVersion is the only method to check the existence without potential AliFatal
216     if (pStorage->GetLatestVersion(pEntry->GetName(), runNo)<0) {
217       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());
218       iResult=-ENOENT;
219     } else {
220       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());
221     }
222   }
223   delete next;
224   next=NULL;
225
226   return iResult;
227 }
228
229 int AliHLTMiscImplementation::InitMagneticField() const
230 {
231   // see header file for function documentation
232
233   // BAD: unit test fails if I call TGeoGlobalMagField::Instance()
234   // at this point. Something goes wrong in the cleaning of the global
235   // ROOT onject
236   /*
237   if (TGeoGlobalMagField::Instance()->GetField()) {
238     // everything set, but think about storing the currents for
239     // a cross-check
240     return 0;
241   }
242   */
243
244   // The magnetic field is initialized once at the start
245   // of run. The fields are supposed to be constant througout the
246   // data taking of one run. The run is aborted if the changes
247   // exceed a certain limit.
248   AliGRPManager grpman;
249   if (grpman.ReadGRPEntry() && grpman.SetMagField()) {
250     return 0;
251   }
252
253   return -ENOENT;
254 }
255
256 AliHLTUInt64_t AliHLTMiscImplementation::GetTriggerMask(AliRawReader* rawReader) const
257 {
258   // see header file for function documentation
259   if (!rawReader) return 0;
260   AliHLTUInt64_t trgMask=0;
261   if (rawReader) {
262     const UInt_t* pattern=rawReader->GetTriggerPattern();
263     trgMask=pattern[1]&0xfffffff; // 28 upper bits of the 50 bit mask
264     trgMask<<=32;
265     trgMask|=pattern[0]; // 32 lower bits of the mask
266   }
267   return trgMask;
268 }
269
270 AliHLTUInt32_t AliHLTMiscImplementation::GetTimeStamp(AliRawReader* rawReader) const
271 {
272   // extract time stamp of the event from the event header
273   if (!rawReader) return 0;
274   const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
275   if (!eventHeader) return 0;
276   return eventHeader->Get("Timestamp");
277 }
278
279 AliHLTUInt32_t AliHLTMiscImplementation::GetEventType(AliRawReader* rawReader) const
280 {
281   // extract event type from the event header
282   if (!rawReader) return 0;
283   const AliRawEventHeaderBase* eventHeader = rawReader->GetEventHeader();
284   if (!eventHeader) return 0;
285   return eventHeader->Get("Type");
286 }
287
288 const char* AliHLTMiscImplementation::GetBeamTypeFromGRP() const
289 {
290   // get beam type from GRP
291   return NULL;
292 }
293
294 Double_t AliHLTMiscImplementation::GetBz()
295 {
296   // Returns Bz.
297   return AliTracker::GetBz();
298 }
299
300 Double_t AliHLTMiscImplementation::GetBz(const Double_t *r)
301 {
302   // Returns Bz (kG) at the point "r" .
303   return AliTracker::GetBz(r);
304 }
305
306 void AliHLTMiscImplementation::GetBxByBz(const Double_t r[3], Double_t b[3])
307 {
308   // Returns Bx, By and Bz (kG) at the point "r" .
309   return AliTracker::GetBxByBz(r, b);
310 }
311
312 const TClass* AliHLTMiscImplementation::IsAliESDHLTDecision() const
313 {
314   // Return the IsA of the AliESDHLTDecision class
315   return AliESDHLTDecision::Class();
316 }
317
318 int AliHLTMiscImplementation::Copy(const AliHLTGlobalTriggerDecision* pDecision, TObject* object) const
319 {
320   // Copy HLT global trigger decision to AliESDHLTDecision container
321   if (!pDecision || !object) return -EINVAL;
322   AliESDHLTDecision* pESDHLTDecision=NULL;
323   if (object->IsA()==NULL ||
324       object->IsA() != AliESDHLTDecision::Class() ||
325       (pESDHLTDecision=dynamic_cast<AliESDHLTDecision*>(object))==NULL) {
326 //     HLTError("can not copy HLT global decision to object of class \"%s\"", 
327 //           object->IsA()?object->IsA()->GetName():"NULL");
328     return -EINVAL;
329   }
330
331   pESDHLTDecision->~AliESDHLTDecision();
332   new (pESDHLTDecision) AliESDHLTDecision(pDecision->Result(), pDecision->GetTitle());
333
334   return 0;
335 }
336
337 int AliHLTMiscImplementation::InitStreamerInfos(const char* ocdbEntry) const
338 {
339   // init streamer infos for HLT reconstruction
340   // Root schema evolution is not enabled for AliHLTMessage and all streamed objects.
341   // Objects in the raw data payload rely on the availability of the correct stream info.
342   // The relevant streamer info for a specific run is stored in the OCDB.
343   int iResult=0;
344
345   AliCDBEntry* pEntry=LoadOCDBEntry(ocdbEntry);
346   TObject* pObject=NULL;
347   if (pEntry && (pObject=ExtractObject(pEntry))!=NULL)
348     {
349     TObjArray* pSchemas=dynamic_cast<TObjArray*>(pObject);
350     if (pSchemas) {
351       iResult=InitStreamerInfos(pSchemas);
352     } else {
353       AliError(Form("internal mismatch in OCDB entry %s: wrong class type", ocdbEntry));
354     }
355   } else {
356     AliWarning(Form("missing HLT reco data (%s), skipping initialization of streamer info for TObjects in HLT raw data payload", ocdbEntry));
357   }
358   return iResult;
359 }
360
361 int AliHLTMiscImplementation::InitStreamerInfos(TObjArray* pSchemas) const
362 {
363   // init streamer infos for HLT reconstruction from an array of TStreamerInfo objects
364
365   for (int i=0; i<pSchemas->GetEntriesFast(); i++) {
366     if (pSchemas->At(i)) {
367       TStreamerInfo* pSchema=dynamic_cast<TStreamerInfo*>(pSchemas->At(i));
368       if (pSchema) {
369         int version=pSchema->GetClassVersion();
370         TClass* pClass=TClass::GetClass(pSchema->GetName());
371         if (pClass) {
372           if (pClass->GetClassVersion()==version) {
373             AliDebug(0,Form("skipping schema definition %d version %d to class %s as this is the native version", i, version, pSchema->GetName()));
374             continue;
375           }
376           TObjArray* pInfos=pClass->GetStreamerInfos();
377           if (pInfos /*&& version<pInfos->GetEntriesFast()*/) {
378             if (pInfos->At(version)==NULL) {
379               TStreamerInfo* pClone=(TStreamerInfo*)pSchema->Clone();
380               if (pClone) {
381                 pClone->SetClass(pClass);
382                 pClone->BuildOld();
383                 pInfos->AddAtAndExpand(pClone, version);
384                 AliDebug(0,Form("adding schema definition %d version %d to class %s", i, version, pSchema->GetName()));
385               } else {
386                 AliError(Form("skipping schema definition %d (%s), unable to create clone object", i, pSchema->GetName()));
387               }
388             } else {
389               TStreamerInfo* pInfo=dynamic_cast<TStreamerInfo*>(pInfos->At(version));
390               if (pInfo && pInfo->GetClassVersion()==version) {
391                 AliDebug(0,Form("schema definition %d version %d already available in class %s, skipping ...", i, version, pSchema->GetName()));
392               } else {
393                 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));
394               }
395             }
396           } else {
397             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));
398           }
399         } else {
400           AliError(Form("skipping schema definition %d (%s), unable to find class", i, pSchema->GetName()));
401         }
402       } else {
403         AliError(Form("skipping schema definition %d, not of TStreamerInfo", i));
404       }
405     }
406   }
407
408   return 0;
409 }
410
411 void AliHLTMiscImplementation::SetAliESDtrackOnlineModeFlag(bool mode) const
412 {
413   /// set the online mode flag of AliESDtrack
414   AliESDtrack::OnlineMode(mode);
415 }
416
417 bool AliHLTMiscImplementation::GetAliESDtrackOnlineModeFlag() const
418 {
419   /// get status of the online mode flag of AliESDtrack
420   return AliESDtrack::OnlineMode();
421 }