]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTOUT.cxx
- bugfix: correct equipment ids for HLT ddl links
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTOUT.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   AliHLTOUT.cxx
20     @author Matthias Richter
21     @date   
22     @brief  The control class for HLTOUT data.                            */
23
24 // see header file for class documentation
25 // or
26 // refer to README to build package
27 // or
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
29
30 #include <cerrno>
31 #include <cassert>
32 #include "AliHLTOUT.h"
33
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTOUT)
36
37 AliHLTOUT::AliHLTOUT()
38   :
39   fSearchDataType(kAliHLTVoidDataType),
40   fSearchSpecification(kAliHLTVoidDataSpec),
41   fSearchHandlerType(AliHLTModuleAgent::kUnknownOutput),
42   fFlags(0),
43   fBlockDescList(),
44   fCurrent(fBlockDescList.begin()),
45   fpBuffer(NULL),
46   fDataHandlers(),
47   fbVerbose(true)
48 {
49   // see header file for class documentation
50   // or
51   // refer to README to build package
52   // or
53   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
54 }
55
56 AliHLTOUT::~AliHLTOUT()
57 {
58   // see header file for class documentation
59 }
60
61 int AliHLTOUT::Init()
62 {
63   // see header file for class documentation
64   int iResult=0;
65   SetStatusFlag(kCollecting);
66   if ((iResult=GenerateIndex())>=0) {
67     if ((iResult=InitHandlers())>=0) {
68     }
69   }
70   ClearStatusFlag(kCollecting);
71   return iResult;
72 }
73
74 int AliHLTOUT::GetNofDataBlocks()
75 {
76   // see header file for class documentation
77   return fBlockDescList.size();
78 }
79
80 int AliHLTOUT::SelectFirstDataBlock(AliHLTComponentDataType dt, AliHLTUInt32_t spec,
81                                     AliHLTModuleAgent::AliHLTOUTHandlerType handlerType)
82 {
83   // see header file for class documentation
84   if (CheckStatusFlag(kLocked)) return -EPERM;
85   fCurrent=fBlockDescList.begin();
86   fSearchDataType=dt;
87   fSearchSpecification=spec;
88   fSearchHandlerType=handlerType;
89   return FindAndSelectDataBlock();
90 }
91
92 int AliHLTOUT::SelectNextDataBlock()
93 {
94   // see header file for class documentation
95   if (CheckStatusFlag(kLocked)) return -EPERM;
96   if (fCurrent==fBlockDescList.end()) return -ENOENT;
97   fCurrent++;
98   return FindAndSelectDataBlock();
99 }
100
101 int AliHLTOUT::FindAndSelectDataBlock()
102 {
103   // see header file for class documentation
104   if (CheckStatusFlag(kLocked)) return -EPERM;
105   int iResult=-ENOENT;
106   while (fCurrent!=fBlockDescList.end() && iResult==-ENOENT) {
107     if ((*fCurrent)==fSearchDataType &&
108         (fSearchSpecification==kAliHLTVoidDataSpec || (*fCurrent)==fSearchSpecification) &&
109         (fSearchHandlerType==AliHLTModuleAgent::kUnknownOutput || FindHandlerDesc(fCurrent->GetIndex())==fSearchHandlerType)) {
110       iResult=fCurrent->GetIndex();
111       // TODO: check the byte order on the current system and the byte order of the
112       // data block, print warning when missmatch and user did not check
113       //AliHLTOUTByteOrder blockBO=CheckByteOrder();
114       CheckByteOrder();
115       /*
116         if (blockBO!=fByteOrder) {
117         SetStatusFlag(kByteOrderWarning);
118
119         }
120        */
121       ClearStatusFlag(kByteOrderChecked);
122
123       // TODO: check the alignment on the current system and the alignment of the
124       // data block, print warning when missmatch and user did not check
125       ClearStatusFlag(kAlignmentChecked);
126
127       break;
128     }
129     fCurrent++;
130   }
131   return iResult;
132 }
133
134 int AliHLTOUT::GetDataBlockDescription(AliHLTComponentDataType& dt, AliHLTUInt32_t& spec)
135 {
136   // see header file for class documentation
137   int iResult=-ENOENT;
138   if (fCurrent!=fBlockDescList.end()) {
139     iResult=0;
140     dt=(*fCurrent);
141     spec=(*fCurrent);
142   }
143   return iResult;
144 }
145
146 AliHLTUInt32_t AliHLTOUT::GetDataBlockIndex()
147 {
148   // see header file for class documentation
149   if (fCurrent==fBlockDescList.end()) return AliHLTOUTInvalidIndex;
150   return fCurrent->GetIndex();
151 }
152
153 int AliHLTOUT::GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size)
154 {
155   // see header file for class documentation
156   int iResult=-ENOENT;
157   pBuffer=NULL;
158   size=0;
159   if (fCurrent!=fBlockDescList.end()) {
160     if ((iResult=GetDataBuffer(fCurrent->GetIndex(), pBuffer, size))>=0) {
161       fpBuffer=pBuffer;
162     }
163   }
164   return iResult;  
165 }
166
167 int AliHLTOUT::ReleaseDataBuffer(const AliHLTUInt8_t* pBuffer)
168 {
169   // see header file for class documentation
170   int iResult=0;
171   if (pBuffer==fpBuffer) {
172     fpBuffer=NULL;
173   } else {
174     HLTWarning("buffer %p does not match the provided one %p", pBuffer, fpBuffer);
175   }
176   return iResult;  
177 }
178
179 AliHLTOUTHandler* AliHLTOUT::GetHandler()
180 {
181   // see header file for class documentation
182   AliHLTOUTHandler* pHandler=NULL;
183   pHandler=FindHandlerDesc(GetDataBlockIndex());
184   return pHandler;
185 }
186
187 int AliHLTOUT::AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc)
188 {
189   // see header file for class documentation
190   if (!CheckStatusFlag(kCollecting)) return -EPERM;
191   int iResult=0;
192   fBlockDescList.push_back(desc);
193   return iResult;  
194 }
195
196 AliHLTOUT::AliHLTOUTByteOrder AliHLTOUT::CheckByteOrder()
197 {
198   // see header file for class documentation
199   if (fCurrent!=fBlockDescList.end()) {
200     SetStatusFlag(kByteOrderChecked);
201     AliHLTOUT::AliHLTOUTByteOrder order=CheckBlockByteOrder((*fCurrent).GetIndex());
202     return order;
203   }
204   return kInvalidByteOrder;
205 }
206
207 int AliHLTOUT::CheckAlignment(AliHLTOUT::AliHLTOUTDataType type)
208 {
209   // see header file for class documentation
210   if (fCurrent!=fBlockDescList.end()) {
211     SetStatusFlag(kAlignmentChecked);
212     int alignment=CheckBlockAlignment((*fCurrent).GetIndex(), type);
213     return alignment;
214   }
215   return -ENOENT;
216 }
217
218 int AliHLTOUT::InitHandlers()
219 {
220   // see header file for class documentation
221   int iResult=0;
222   AliHLTOUTIndexList remnants;
223   int iCount=0;
224   for (int havedata=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec); havedata>=0; havedata=SelectNextDataBlock()) {
225     iCount++;
226     remnants.push_back(GetDataBlockIndex());
227     AliHLTComponentDataType dt=kAliHLTVoidDataType;
228     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
229     if (GetDataBlockDescription(dt, spec)<0) break;
230     for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent(); pAgent && iResult>=0; pAgent=AliHLTModuleAgent::GetNextAgent()) {
231       AliHLTModuleAgent::AliHLTOUTHandlerDesc handlerDesc;
232       if (pAgent->GetHandlerDescription(dt, spec, handlerDesc)>0) {
233         AliHLTOUTHandlerListEntry entry(pAgent->GetOutputHandler(dt, spec), handlerDesc, pAgent, GetDataBlockIndex());
234         InsertHandler(entry);
235         remnants.pop_back();
236         break;
237       }
238     }
239   }
240
241   // warning if some of the data blocks are not selected by the kAliHLTAnyDataType
242   // criterion
243   if (GetNofDataBlocks()>iCount) {
244     HLTWarning("incomplete data type in %d out of %d data block(s)", GetNofDataBlocks()-iCount, iCount);
245   }
246
247   // warning if handler not found
248   if (remnants.size()>0) {
249     HLTWarning("no handlers found for %d data blocks out of %d", remnants.size(), iCount);
250     AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
251     for (AliHLTOUTIndexList::iterator element=remnants.begin(); element!=remnants.end(); element++) {
252       for (int trials=0; trials<2; trials++) {
253         do {
254           // we start searching the index from the current position in the block list
255           if ((*block).GetIndex()==*element) break;
256         } while ((++block)!=fBlockDescList.end());
257         if (block==fBlockDescList.end()) {
258           // rewind and try again
259           block=fBlockDescList.begin();
260         }
261       }
262       assert(block!=fBlockDescList.end());
263       if (block!=fBlockDescList.end()) {
264         HLTDebug("   %s", AliHLTComponent::DataType2Text((AliHLTComponentDataType)*block).c_str());
265       }
266     }
267   }
268   return iResult;
269 }
270
271 int AliHLTOUT::InsertHandler(const AliHLTOUTHandlerListEntry &entry)
272 {
273   // see header file for class documentation
274   int iResult=0;
275   AliHLTOUTHandlerListEntryVector::iterator element=fDataHandlers.begin();
276   while (element!=fDataHandlers.end()) {
277     if (entry==(*element)) break;
278     element++;
279   }
280   if (element==fDataHandlers.end()) {
281     fDataHandlers.push_back(entry);
282   } else {
283     element->AddIndex(const_cast<AliHLTOUTHandlerListEntry&>(entry));
284   }
285   return iResult;
286 }
287
288 const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::FindHandlerDesc(AliHLTUInt32_t blockIndex)
289 {
290   // see header file for class documentation
291   AliHLTOUTHandlerListEntryVector::iterator element=fDataHandlers.begin();
292   while (element!=fDataHandlers.end()) {
293     if (element->HasIndex(blockIndex)) {
294       return *element;
295     }
296     element++;
297   }
298   return const_cast<AliHLTOUT::AliHLTOUTHandlerListEntry&>(AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry);
299 }
300
301 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry()
302   :
303   fpHandler(NULL),
304   fpHandlerDesc(NULL),
305   fpAgent(NULL),
306   fBlocks()
307 {
308   // see header file for class documentation
309 }
310
311 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(AliHLTOUTHandler* pHandler, 
312                                                                 AliHLTModuleAgent::AliHLTOUTHandlerDesc& handlerDesc,
313                                                                 AliHLTModuleAgent* pAgent,
314                                                                 AliHLTUInt32_t index)
315   :
316   fpHandler(pHandler),
317   fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
318   fpAgent(pAgent),
319   fBlocks()
320 {
321   // see header file for class documentation
322   *fpHandlerDesc=handlerDesc;
323   fBlocks.push_back(index);
324 }
325
326 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(const AliHLTOUTHandlerListEntry& src)
327   :
328   fpHandler(src.fpHandler),
329   fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
330   fpAgent(src.fpAgent),
331   fBlocks()
332 {
333   // see header file for class documentation
334   *fpHandlerDesc=*src.fpHandlerDesc;
335   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
336 }
337
338 AliHLTOUT::AliHLTOUTHandlerListEntry::~AliHLTOUTHandlerListEntry()
339 {
340   // see header file for class documentation
341   if (fpHandlerDesc) delete fpHandlerDesc;
342   fpHandlerDesc=NULL;
343 }
344
345 AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTHandlerListEntry::operator=(const AliHLTOUTHandlerListEntry& src)
346 {
347   // see header file for class documentation
348   fpHandler=src.fpHandler;
349   if (src.fpHandlerDesc)
350     *fpHandlerDesc=*src.fpHandlerDesc;
351   fpAgent=src.fpAgent;
352   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
353   return *this;
354 }
355
356 AliHLTUInt32_t AliHLTOUT::AliHLTOUTHandlerListEntry::operator[](int i) const
357 {
358   // see header file for class documentation
359   return (int)fBlocks.size()>i?fBlocks[i]:AliHLTOUTInvalidIndex;
360 }
361
362 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTOUTHandlerListEntry& entry) const
363 {
364   // see header file for class documentation
365   if (entry.fpHandler!=fpHandler || fpHandler==NULL) return false;
366   assert(entry.fpAgent==fpAgent);
367   if (entry.fpAgent!=fpAgent) return false;
368   return true;
369 }
370
371 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTModuleAgent::AliHLTOUTHandlerType handlerType) const
372 {
373   // see header file for class documentation
374   if (!fpHandlerDesc) return false;
375   return *fpHandlerDesc==handlerType;
376 }
377
378 void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTOUT::AliHLTOUTHandlerListEntry &desc)
379 {
380   // see header file for class documentation
381   AliHLTOUTIndexList::iterator element;
382   for (element=desc.fBlocks.begin(); element!=desc.fBlocks.end(); element++) {
383     AddIndex(*element);
384   }  
385 }
386
387 void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTUInt32_t index)
388 {
389   // see header file for class documentation
390   fBlocks.push_back(index);
391 }
392
393 bool AliHLTOUT::AliHLTOUTHandlerListEntry::HasIndex(AliHLTUInt32_t index)
394 {
395   // see header file for class documentation
396   AliHLTOUTIndexList::iterator element;
397   for (element=fBlocks.begin(); element!=fBlocks.end(); element++) {
398     if (*element==index) return true;
399   }
400   return false;
401 }
402
403 const AliHLTOUT::AliHLTOUTHandlerListEntry AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry;
404
405 AliHLTUInt64_t AliHLTOUT::Swap(AliHLTUInt64_t src)
406 {
407   // see header file for class documentation
408   return ((src & 0xFFULL) << 56) | 
409     ((src & 0xFF00ULL) << 40) | 
410     ((src & 0xFF0000ULL) << 24) | 
411     ((src & 0xFF000000ULL) << 8) | 
412     ((src & 0xFF00000000ULL) >> 8) | 
413     ((src & 0xFF0000000000ULL) >> 24) | 
414     ((src & 0xFF000000000000ULL) >>  40) | 
415     ((src & 0xFF00000000000000ULL) >> 56);
416 }
417
418 AliHLTUInt32_t AliHLTOUT::Swap(AliHLTUInt32_t src)
419 {
420   // see header file for class documentation
421   return ((src & 0xFFULL) << 24) | 
422     ((src & 0xFF00ULL) << 8) | 
423     ((src & 0xFF0000ULL) >> 8) | 
424     ((src & 0xFF000000ULL) >> 24);
425 }