]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/interface/AliHLTExternalInterface.cxx
Adding common handling for the magnetic field settings tothe component interface
[u/mrichter/AliRoot.git] / HLT / BASE / interface / AliHLTExternalInterface.cxx
CommitLineData
d098ebd4 1// $Id$
2
a3c9b745 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//**************************************************************************
d098ebd4 18
02d01bbf 19/** @file AliHLTExternalInterface.cxx
d098ebd4 20 @author Matthias Richter, Timm Steinbeck
21 @date
a3c9b745 22 @brief Pure C interface to the AliRoot HLT analysis framework
d098ebd4 23*/
24
c043fa2c 25#include "AliHLTExternalInterface.h"
d098ebd4 26#include "AliHLTComponentHandler.h"
27#include "AliHLTComponent.h"
c043fa2c 28#include "AliHLTSystem.h"
cc484ed9 29#include "AliHLTMisc.h"
c043fa2c 30#include <cerrno>
ac7de438 31#include <cstring>
a3c9b745 32/////////////////////////////////////////////////////////////////////////////////////
33//
34// AliHLT external interface functions
35//
d098ebd4 36
a3c9b745 37static AliHLTComponentHandler *gComponentHandler = NULL;
38static AliHLTRunDesc gRunDesc=kAliHLTVoidRunDesc;
39static char* gRunType=NULL;
d098ebd4 40
a3c9b745 41int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
d098ebd4 42{
a3c9b745 43 if ( gComponentHandler ) {
44 return EINPROGRESS;
45 }
46
47 if (!externalEnv) {
48 return EINVAL;
49 }
50
51 if (!externalEnv->fStructSize) {
52 return EPERM;
53 }
54
55 // map the environment structure to the internal structure to
56 // support different versions of the interface
57 AliHLTAnalysisEnvironment mappedEnv;
58 memset(&mappedEnv, 0, sizeof(mappedEnv));
59 memcpy(&mappedEnv, externalEnv, sizeof(mappedEnv)<externalEnv->fStructSize?sizeof(mappedEnv):externalEnv->fStructSize);
60 mappedEnv.fStructSize=sizeof(mappedEnv);
61
62 gComponentHandler = new AliHLTComponentHandler(&mappedEnv);
63 if ( !gComponentHandler )
d098ebd4 64 return EFAULT;
a3c9b745 65 gComponentHandler->InitAliLogTrap(gComponentHandler);
66 gComponentHandler->AnnounceVersion();
67
68 if (version!=ALIHLT_DATA_TYPES_VERSION && externalEnv->fLoggingFunc) {
69 char message[100];
70 sprintf(message, "interface definition does not match: internal %d - external %lu", ALIHLT_DATA_TYPES_VERSION, version);
71 externalEnv->fLoggingFunc(externalEnv->fParam, kHLTLogWarning, "AliHLTAnalysisInitSystem", "interface version", message);
72 }
73
74 gRunDesc.fRunNo=runNo;
75 if (runType && strlen(runType)>0) {
76 gRunType=new char[strlen(runType)+1];
77 if (gRunType) {
78 strcpy(gRunType, runType);
79 }
80 }
cc484ed9 81
82 // the AliRoot dependent code is implemented by the
83 // AliHLTMiscImplementation class in libHLTrec
84 AliHLTMisc::Instance().InitCDB(getenv("ALIHLT_HCDBDIR"));
85 AliHLTMisc::Instance().SetCDBRunNo(gRunDesc.fRunNo);
86 AliHLTMisc::Instance().InitMagneticField();
87
d098ebd4 88 return 0;
89}
90
a3c9b745 91int AliHLTAnalysisDeinitSystem()
d098ebd4 92{
a3c9b745 93 if (gComponentHandler) delete gComponentHandler;
94 gComponentHandler = NULL;
95
96 if (gRunType) delete[] gRunType;
97 gRunType=NULL;
98
99 gRunDesc=kAliHLTVoidRunDesc;
100
d098ebd4 101 return 0;
102}
103
a3c9b745 104int AliHLTAnalysisLoadLibrary( const char* libraryPath )
d098ebd4 105{
a3c9b745 106 if ( !gComponentHandler )
d098ebd4 107 return ENXIO;
a3c9b745 108 return gComponentHandler->LoadLibrary( libraryPath );
d098ebd4 109}
110
a3c9b745 111int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
d098ebd4 112{
a3c9b745 113 if ( !gComponentHandler )
d098ebd4 114 return ENXIO;
a3c9b745 115 // Matthias 26.10.2007
116 // Unloading of libraries has to be re-worked. It has been commented out here
117 // since the libraries will be unloaded at the destruction of the component
118 // handler instance anyway. So it has no effect to the operation in PubSub.
119 // With the introduction of the dynamic component registration via module
120 // agents we run into trouble when cleaning up the samples managed by the
121 // component handler. Destruction of the sample objects is done AFTER
122 // unloading of the library and thus the destructor is not present any
123 // more.
124 //return gComponentHandler->UnloadLibrary( libraryPath );
125 return 0;
d098ebd4 126}
127
a3c9b745 128int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
d098ebd4 129{
a3c9b745 130 if ( !gComponentHandler ) return ENXIO;
131 if (!handle) return EINVAL;
132
133 AliHLTComponent* comp=NULL;
cc484ed9 134
a3c9b745 135 int ret = gComponentHandler->CreateComponent( componentType, comp);
136 if (ret>=0 && comp) {
a3c9b745 137 const AliHLTAnalysisEnvironment* comenv=gComponentHandler->GetEnvironment();
c528fdc6 138 comp->SetComponentEnvironment(comenv, environParam);
a3c9b745 139 if (comenv) {
140 if (description) {
141 comp->SetComponentDescription(description);
142 }
143 comp->SetRunDescription(&gRunDesc, gRunType);
144 }
145 ret=comp->Init(comenv, environParam, argc, argv);
146 }
d098ebd4 147 *handle = reinterpret_cast<AliHLTComponentHandle>( comp );
a3c9b745 148
d098ebd4 149 return ret;
150}
151
a3c9b745 152int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
d098ebd4 153{
154 if ( !handle )
a3c9b745 155 return ENOENT;
d098ebd4 156
157 AliHLTComponent* pComp=reinterpret_cast<AliHLTComponent*>( handle );
158 pComp->Deinit();
159 delete pComp;
a3c9b745 160 return 0;
d098ebd4 161}
162
a3c9b745 163int AliHLTAnalysisProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks,
f7561f8d 164 AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr,
d098ebd4 165 AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt,
f7561f8d 166 AliHLTComponentBlockData** outputBlocks,
167 AliHLTComponentEventDoneData** edd )
d098ebd4 168{
a3c9b745 169 if ( !handle ) return EINVAL;
d098ebd4 170 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
a3c9b745 171 if (!comp) return ENXIO;
172 int ret=comp->ProcessEvent( *evtData, blocks, *trigData, outputPtr, *size, *outputBlockCnt, *outputBlocks, *edd );
173
174 // internally, return values <0 are errors, >=0 are success with some
175 // optional return vaue.
176 // externally everthing !=0 is error. This is also according to the
177 // common habit
178 if (ret>0) ret=0;
179 else if (ret<0) ret*=-1;
180 return ret;
d098ebd4 181}
182
a3c9b745 183int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
d098ebd4 184{
a3c9b745 185 if ( !handle ) return EINVAL;
d098ebd4 186 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
a3c9b745 187 if (!comp) return ENXIO;
d098ebd4 188 *dataType = comp->GetOutputDataType();
189 return 0;
190}
191
a3c9b745 192int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
d098ebd4 193{
a3c9b745 194 if ( !handle ) return EINVAL;
d098ebd4 195 AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
a3c9b745 196 if (!comp) return ENXIO;
197 // TODO: extend component interface
e35ac21a 198 if (constBlockBase) *constBlockBase=0;
199 comp->GetOutputDataSize( *constEventBase, *inputBlockMultiplier );
d098ebd4 200 return 0;
201}
c043fa2c 202
a3c9b745 203struct AliHLTAnalysisInterfaceCall {
204 const char* fSignature;
205 void* fCall;
206};
207
208AliHLTAnalysisInterfaceCall gSignatures[]={
209 //int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
210 {"int AliHLTAnalysisInitSystem(unsigned long,AliHLTAnalysisEnvironment*,unsigned long,const char*)", (void*)AliHLTAnalysisInitSystem},
211
212 //int AliHLTAnalysisDeinitSystem()
213 {"int AliHLTAnalysisDeinitSystem()", (void*)AliHLTAnalysisDeinitSystem},
214
215 //int AliHLTAnalysisLoadLibrary( const char* libraryPath )
216 {"int AliHLTAnalysisLoadLibrary(const char*)", (void*)AliHLTAnalysisLoadLibrary},
217
218 //int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
219 {"int AliHLTAnalysisUnloadLibrary(const char*)", (void*)AliHLTAnalysisUnloadLibrary},
220
221 //int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
222 {"int AliHLTAnalysisCreateComponent(const char*,void*,int,const char**,AliHLTComponentHandle*,const char*)", (void*)AliHLTAnalysisCreateComponent},
223
224 //int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
225 {"int AliHLTAnalysisDestroyComponent(AliHLTComponentHandle)", (void*)AliHLTAnalysisDestroyComponent},
226
227 //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 )
228 {"int AliHLTAnalysisProcessEvent(AliHLTComponentHandle,const AliHLTComponentEventData*,const AliHLTComponentBlockData*,AliHLTComponentTriggerData*,AliHLTUInt8_t*,AliHLTUInt32_t*,AliHLTUInt32_t*,AliHLTComponentBlockData**,AliHLTComponentEventDoneData**)", (void*)AliHLTAnalysisProcessEvent},
229
230 //int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
231 {"int AliHLTAnalysisGetOutputDataType(AliHLTComponentHandle,AliHLTComponentDataType*)", (void*)AliHLTAnalysisGetOutputDataType},
232
233 //int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
234 {"int AliHLTAnalysisGetOutputSize(AliHLTComponentHandle,unsigned long*,unsigned long*,double*)", (void*)AliHLTAnalysisGetOutputSize},
235
236 {NULL, NULL}
237};
238
239void* AliHLTAnalysisGetInterfaceCall(const char* signature)
240{
241 if (!signature) return NULL;
242 for (int i=0; gSignatures[i].fSignature!=NULL; i++) {
243 if (strcmp(gSignatures[i].fSignature, signature)==0) {
244 return gSignatures[i].fCall;
245 }
246 }
247 return NULL;
248}
249
250/////////////////////////////////////////////////////////////////////////////////////
251//
252// AliHLTSystem interface functions
253//
c043fa2c 254
255int AliHLTSystemSetOptions(AliHLTSystem* pInstance, const char* options)
256{
257 int iResult=0;
258 if (pInstance) {
259 AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
260 if (pSystem) {
261 iResult=pSystem->ScanOptions(options);
262 } else {
263 iResult=-EFAULT;
264 }
265 } else {
266 iResult=-EINVAL;
267 }
268 return iResult;
269}
c5123824 270
271int AliHLTSystemProcessHLTOUT(AliHLTSystem* pInstance, AliHLTOUT* pHLTOUT, AliESDEvent* esd)
272{
273 int iResult=0;
274 if (pInstance) {
275 AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
276 if (pSystem) {
277 iResult=pSystem->ProcessHLTOUT(pHLTOUT, esd);
278 } else {
279 iResult=-EFAULT;
280 }
281 } else {
282 iResult=-EINVAL;
283 }
284 return iResult;
285}