]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponent.cxx
- improvements in AliHLTFilePublisher/Writer
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
8  *          for The ALICE Off-line 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   AliHLTComponent.cxx
20     @author Matthias Richter, Timm Steinbeck
21     @date   
22     @brief  Base class implementation for HLT components. */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 #include "AliHLTStdIncludes.h"
29 #include "AliHLTComponent.h"
30 #include "AliHLTComponentHandler.h"
31 #include "AliHLTSystem.h"
32
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTComponent)
35
36 AliHLTComponent::AliHLTComponent()
37   :
38   fEnvironment(),
39   fCurrentEvent(0),
40   fEventCount(-1)
41
42   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
43   if (fpComponentHandler)
44     fpComponentHandler->ScheduleRegister(this);
45 }
46
47 AliHLTComponent::~AliHLTComponent()
48 {
49 }
50
51 AliHLTComponentHandler* AliHLTComponent::fpComponentHandler=NULL;
52
53 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite) 
54 {
55   int iResult=0;
56   if (fpComponentHandler==NULL || bOverwrite!=0)
57     fpComponentHandler=pCH;
58   else
59     iResult=-EPERM;
60   return iResult;
61 }
62
63 int AliHLTComponent::UnsetGlobalComponentHandler() {
64   return SetGlobalComponentHandler(NULL,1);
65 }
66
67 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environ_param, int argc, const char** argv )
68 {
69   int iResult=0;
70   if (environ) {
71     memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
72     fEnvironment.fParam=environ_param;
73   }
74   iResult=DoInit(argc, argv);
75   if (iResult>=0) fEventCount=0;
76   return iResult;
77 }
78
79 int AliHLTComponent::Deinit()
80 {
81   int iResult=0;
82   iResult=DoDeinit();
83   return iResult;
84 }
85
86 int AliHLTComponent::DoInit( int argc, const char** argv )
87 {
88   if (argc==0 && argv==NULL) {
89     // this is currently just to get rid of the warning "unused parameter"
90   }
91   return 0;
92 }
93
94 int AliHLTComponent::DoDeinit()
95 {
96   return 0;
97 }
98
99 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) {
100   memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
101   strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
102   strcat( output, ":" );
103   strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
104 }
105
106 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
107 {
108   string out("");
109   
110   if (type==kAliHLTVoidDataType) {
111     out="VOID:VOID";
112   } else {
113     // some gymnastics in order to avoid a '0' which is part of either or both
114     // ID and origin terminating the whole string. Unfortunately, string doesn't
115     // stop appending at the '0' if the number of elements to append was 
116     // explicitely specified
117     string tmp("");
118     tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
119     out.append(tmp.c_str());
120     out.append(":");
121     tmp="";
122     tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
123     out.append(tmp.c_str());
124   }
125   return out;
126 }
127
128
129 void* AliHLTComponent::AllocMemory( unsigned long size ) {
130   if (fEnvironment.fAllocMemoryFunc)
131     return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
132   HLTFatal("no memory allocation handler registered");
133   return NULL;
134 }
135
136 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
137                                               AliHLTComponentBlockData** outputBlocks ) {
138     if ( blockCount==NULL || outputBlocks==NULL )
139         return -EFAULT;
140     AliHLTUInt32_t count = blocks.size();
141     if ( !count )
142         {
143         *blockCount = 0;
144         *outputBlocks = NULL;
145         return 0;
146         }
147     *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
148     if ( !*outputBlocks )
149         return -ENOMEM;
150     for ( unsigned long i = 0; i < count; i++ ) {
151         (*outputBlocks)[i] = blocks[i];
152         if (blocks[i].fDataType==kAliHLTAnyDataType) {
153           memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
154           memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
155         }
156     }
157     *blockCount = count;
158     return 0;
159
160 }
161
162 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) {
163   if (fEnvironment.fGetEventDoneDataFunc)
164     return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
165   return -ENOSYS;
166 }
167
168 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList) 
169 {
170   int iResult=0;
171   if (pConsumer) {
172     vector<AliHLTComponentDataType> ctlist;
173     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
174     vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
175     while (type!=ctlist.end() && iResult==0) {
176       if ((*type)==GetOutputDataType() ||
177           (*type)==kAliHLTAnyDataType) {
178         if (tgtList) tgtList->push_back(*type);
179         iResult++;
180         // this loop has to be changed in case of multiple output types
181         break;
182       }
183       type++;
184     }
185   } else {
186     iResult=-EINVAL;
187   }
188   return iResult;
189 }
190
191 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) {
192   blockData.fStructSize = sizeof(blockData);
193   FillShmData( blockData.fShmKey );
194   blockData.fOffset = ~(AliHLTUInt32_t)0;
195   blockData.fPtr = NULL;
196   blockData.fSize = 0;
197   FillDataType( blockData.fDataType );
198   blockData.fSpecification = ~(AliHLTUInt32_t)0;
199 }
200
201 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) {
202   shmData.fStructSize = sizeof(shmData);
203   shmData.fShmType = gkAliHLTComponentInvalidShmType;
204   shmData.fShmID = gkAliHLTComponentInvalidShmID;
205 }
206
207 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) {
208   dataType=kAliHLTAnyDataType;
209 }
210
211 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt) {
212   memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
213   memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
214 }
215
216 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin) {
217   tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
218   memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
219   memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
220
221   if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
222     HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
223   }
224   strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
225
226   if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
227     HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
228   }
229   strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
230 }
231
232 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
233 {
234   memset(&evtData, 0, sizeof(AliHLTComponentEventData));
235   evtData.fStructSize=sizeof(AliHLTComponentEventData);
236 }
237
238 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) {
239   TString msg;
240   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
241   for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
242    if (dt.fID[i]!=0) msg+=dt.fID[i];
243    else msg+="\\0";
244   }
245   msg+="\" Origin=\"";
246   for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
247    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
248    else msg+="\\0";
249   }
250   msg+="\"";
251   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
252 }
253
254 int AliHLTComponent::GetEventCount()
255 {
256   return fEventCount;
257 }
258
259 int AliHLTComponent::IncrementEventCounter()
260 {
261   if (fEventCount>=0) fEventCount++;
262   return fEventCount;
263 }
264
265 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
266                                    const AliHLTComponentBlockData* blocks, 
267                                    AliHLTComponentTriggerData& trigData,
268                                    AliHLTUInt8_t* outputPtr, 
269                                    AliHLTUInt32_t& size,
270                                    AliHLTUInt32_t& outputBlockCnt, 
271                                    AliHLTComponentBlockData*& outputBlocks,
272                                    AliHLTComponentEventDoneData*& edd )
273 {
274   int iResult=0;
275   fCurrentEvent=evtData.fEventID;
276   iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, outputBlockCnt, outputBlocks, edd);
277   IncrementEventCounter();
278   return iResult;
279 }