3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 // @file AliHLTExternalInterface.cxx
20 // @author Matthias Richter, Timm Steinbeck
22 // @brief Pure C interface to the AliRoot HLT analysis framework
23 // The C interface is used from external applications like the HLT
26 #include "AliHLTExternalInterface.h"
27 #include "AliHLTComponentHandler.h"
28 #include "AliHLTComponent.h"
29 #include "AliHLTSystem.h"
30 #include "AliHLTMisc.h"
33 /////////////////////////////////////////////////////////////////////////////////////
35 // AliHLT external interface functions
38 static AliHLTComponentHandler *gComponentHandler = NULL;
39 static AliHLTRunDesc gRunDesc=kAliHLTVoidRunDesc;
40 static char* gRunType=NULL;
42 int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
44 // init the HLT system
45 if ( gComponentHandler ) {
53 if (!externalEnv->fStructSize) {
57 // map the environment structure to the internal structure to
58 // support different versions of the interface
59 AliHLTAnalysisEnvironment mappedEnv;
60 memset(&mappedEnv, 0, sizeof(mappedEnv));
61 memcpy(&mappedEnv, externalEnv, sizeof(mappedEnv)<externalEnv->fStructSize?sizeof(mappedEnv):externalEnv->fStructSize);
62 mappedEnv.fStructSize=sizeof(mappedEnv);
64 gComponentHandler = new AliHLTComponentHandler(&mappedEnv);
65 if ( !gComponentHandler )
67 gComponentHandler->InitAliLogTrap(gComponentHandler);
68 gComponentHandler->AnnounceVersion();
70 if (version!=ALIHLT_DATA_TYPES_VERSION && externalEnv->fLoggingFunc) {
71 const int messagelength=100;
72 char message[messagelength+1];
73 message[messagelength]=0;
74 snprintf(message, messagelength, "interface definition does not match: internal %d - external %lu", ALIHLT_DATA_TYPES_VERSION, version);
75 externalEnv->fLoggingFunc(externalEnv->fParam, kHLTLogWarning, "AliHLTAnalysisInitSystem", "interface version", message);
78 gRunDesc.fRunNo=runNo;
79 if (runType && strlen(runType)>0) {
80 int runtypelength=strlen(runType);
81 gRunType=new char[runtypelength+1];
83 gRunType[runtypelength]=0;
84 strncpy(gRunType, runType, runtypelength);
88 // the AliRoot dependent code is implemented by the
89 // AliHLTMiscImplementation class in libHLTrec
90 AliHLTMisc::Instance().InitCDB(getenv("ALIHLT_HCDBDIR"));
91 AliHLTMisc::Instance().SetCDBRunNo(gRunDesc.fRunNo);
92 AliHLTMisc::Instance().InitMagneticField();
97 int AliHLTAnalysisDeinitSystem()
99 // De-init the HLT system and clean-up internal memory
100 if (gComponentHandler) delete gComponentHandler;
101 gComponentHandler = NULL;
103 if (gRunType) delete[] gRunType;
106 gRunDesc=kAliHLTVoidRunDesc;
111 int AliHLTAnalysisLoadLibrary( const char* libraryPath )
113 // load a component library
114 if ( !gComponentHandler )
116 return gComponentHandler->LoadLibrary( libraryPath );
119 int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
121 // unload a component library
122 if ( !gComponentHandler )
124 // Matthias 26.10.2007
125 // Unloading of libraries has to be re-worked. It has been commented out here
126 // since the libraries will be unloaded at the destruction of the component
127 // handler instance anyway. So it has no effect to the operation in PubSub.
128 // With the introduction of the dynamic component registration via module
129 // agents we run into trouble when cleaning up the samples managed by the
130 // component handler. Destruction of the sample objects is done AFTER
131 // unloading of the library and thus the destructor is not present any
133 //return gComponentHandler->UnloadLibrary( libraryPath );
137 int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
139 // create a component
140 if ( !gComponentHandler ) return ENXIO;
141 if (!handle) return EINVAL;
143 AliHLTComponent* comp=NULL;
145 int ret = gComponentHandler->CreateComponent( componentType, comp);
146 if (ret>=0 && comp) {
147 const AliHLTAnalysisEnvironment* comenv=gComponentHandler->GetEnvironment();
148 comp->SetComponentEnvironment(comenv, environParam);
151 comp->SetComponentDescription(description);
153 comp->SetRunDescription(&gRunDesc, gRunType);
155 ret=comp->Init(comenv, environParam, argc, argv);
157 *handle = reinterpret_cast<AliHLTComponentHandle>( comp );
162 int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
164 // destroy a component
168 AliHLTComponent* pComp=reinterpret_cast<AliHLTComponent*>( handle );
174 int AliHLTAnalysisProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks,
175 AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr,
176 AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt,
177 AliHLTComponentBlockData** outputBlocks,
178 AliHLTComponentEventDoneData** edd )
181 if ( !handle ) return EINVAL;
182 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
183 if (!comp) return ENXIO;
184 int ret=comp->ProcessEvent( *evtData, blocks, *trigData, outputPtr, *size, *outputBlockCnt, *outputBlocks, *edd );
186 // internally, return values <0 are errors, >=0 are success with some
187 // optional return vaue.
188 // externally everthing !=0 is error. This is also according to the
191 else if (ret<0) ret*=-1;
195 int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
197 // get output data type of a component
198 if ( !handle ) return EINVAL;
199 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
200 if (!comp) return ENXIO;
201 *dataType = comp->GetOutputDataType();
205 int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
207 // get output data size of a component
208 if ( !handle ) return EINVAL;
209 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
210 if (!comp) return ENXIO;
211 // TODO: extend component interface
212 if (constBlockBase) *constBlockBase=0;
213 comp->GetOutputDataSize( *constEventBase, *inputBlockMultiplier );
217 struct AliHLTAnalysisInterfaceCall {
218 const char* fSignature;
222 const AliHLTAnalysisInterfaceCall gAliHLTInterfaceCallSignatures[]={
223 //int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
224 {"int AliHLTAnalysisInitSystem(unsigned long,AliHLTAnalysisEnvironment*,unsigned long,const char*)", (void*)AliHLTAnalysisInitSystem},
226 //int AliHLTAnalysisDeinitSystem()
227 {"int AliHLTAnalysisDeinitSystem()", (void*)AliHLTAnalysisDeinitSystem},
229 //int AliHLTAnalysisLoadLibrary( const char* libraryPath )
230 {"int AliHLTAnalysisLoadLibrary(const char*)", (void*)AliHLTAnalysisLoadLibrary},
232 //int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
233 {"int AliHLTAnalysisUnloadLibrary(const char*)", (void*)AliHLTAnalysisUnloadLibrary},
235 //int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
236 {"int AliHLTAnalysisCreateComponent(const char*,void*,int,const char**,AliHLTComponentHandle*,const char*)", (void*)AliHLTAnalysisCreateComponent},
238 //int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
239 {"int AliHLTAnalysisDestroyComponent(AliHLTComponentHandle)", (void*)AliHLTAnalysisDestroyComponent},
241 //int AliHLTAnalysisProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks, AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr, AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt, AliHLTComponentBlockData** outputBlocks, AliHLTComponentEventDoneData** edd )
242 {"int AliHLTAnalysisProcessEvent(AliHLTComponentHandle,const AliHLTComponentEventData*,const AliHLTComponentBlockData*,AliHLTComponentTriggerData*,AliHLTUInt8_t*,AliHLTUInt32_t*,AliHLTUInt32_t*,AliHLTComponentBlockData**,AliHLTComponentEventDoneData**)", (void*)AliHLTAnalysisProcessEvent},
244 //int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
245 {"int AliHLTAnalysisGetOutputDataType(AliHLTComponentHandle,AliHLTComponentDataType*)", (void*)AliHLTAnalysisGetOutputDataType},
247 //int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
248 {"int AliHLTAnalysisGetOutputSize(AliHLTComponentHandle,unsigned long*,unsigned long*,double*)", (void*)AliHLTAnalysisGetOutputSize},
253 void* AliHLTAnalysisGetInterfaceCall(const char* signature)
255 // get function pointer of an interface call
256 // the function pointer is selected according to the requested function signature
257 if (!signature) return NULL;
258 for (int i=0; gAliHLTInterfaceCallSignatures[i].fSignature!=NULL; i++) {
259 if (strcmp(gAliHLTInterfaceCallSignatures[i].fSignature, signature)==0) {
260 return gAliHLTInterfaceCallSignatures[i].fCall;
266 /////////////////////////////////////////////////////////////////////////////////////
268 // AliHLTSystem interface functions
271 int AliHLTSystemSetOptions(AliHLTSystem* pInstance, const char* options)
273 // deprecated helper function
276 AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
278 iResult=pSystem->ScanOptions(options);
288 int AliHLTSystemProcessHLTOUT(AliHLTSystem* pInstance, AliHLTOUT* pHLTOUT, AliESDEvent* esd)
290 // deprecated helper function
293 AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
295 iResult=pSystem->ProcessHLTOUT(pHLTOUT, esd);