The trigger reconstructor component now generates the correct data format.
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONTriggerReconstructorComponent.cxx
CommitLineData
6efe69e7 1/**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
4 * *
5 * Primary Authors: *
6 * Indranil Das <indra.das@saha.ac.in> *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
16
17/* $Id$ */
18
19/** @file AliHLTMUONTriggerReconstructorComponent.cxx
20 @author Indranil Das
21 @date
960d54ad 22 @brief Implementation of the trigger DDL reconstructor component. */
6efe69e7 23
6efe69e7 24#include "AliHLTMUONTriggerReconstructorComponent.h"
960d54ad 25#include "AliHLTMUONTriggerReconstructor.h"
26#include "AliHLTMUONHitReconstructor.h"
27#include "AliHLTMUONConstants.h"
227e7192 28#include "AliHLTMUONDataBlockWriter.h"
29#include <cstdlib>
30#include <cerrno>
6efe69e7 31
960d54ad 32namespace
33{
34 // This is a global object used for automatic component registration,
35 // do not use this for calculation.
36 AliHLTMUONTriggerReconstructorComponent gAliHLTMUONTriggerReconstructorComponent;
227e7192 37
960d54ad 38} // end of namespace
39
6efe69e7 40
41ClassImp(AliHLTMUONTriggerReconstructorComponent)
42
960d54ad 43
227e7192 44AliHLTMUONTriggerReconstructorComponent::AliHLTMUONTriggerReconstructorComponent() :
45 fTrigRec(NULL),
46 fDDLDir(""),
47 fDDL(0)
960d54ad 48{
49}
50
6efe69e7 51
52AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
960d54ad 53{
54}
55
6efe69e7 56
57const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
960d54ad 58{
227e7192 59 return AliHLTMUONConstants::TriggerReconstructorId();
960d54ad 60}
61
62
63void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
64{
227e7192 65 list.clear();
66 list.push_back( AliHLTMUONConstants::TriggerDDLRawDataType() );
960d54ad 67}
6efe69e7 68
6efe69e7 69
70AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
960d54ad 71{
227e7192 72 return AliHLTMUONConstants::TriggerRecordsBlockDataType();
960d54ad 73}
6efe69e7 74
6efe69e7 75
227e7192 76void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
77 unsigned long& constBase, double& inputMultiplier
78 )
960d54ad 79{
227e7192 80 constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
81 inputMultiplier = 1;
960d54ad 82}
6efe69e7 83
84
6efe69e7 85AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
960d54ad 86{
227e7192 87 return new AliHLTMUONTriggerReconstructorComponent;
960d54ad 88}
89
6efe69e7 90
960d54ad 91int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
6efe69e7 92{
227e7192 93 // perform initialization. We check whether our relative output size is
94 // specified in the arguments.
960d54ad 95
96 HLTInfo("Initialising DHLT Trigger Record Component");
97
98 fTrigRec = new AliHLTMUONTriggerReconstructor();
6efe69e7 99
960d54ad 100 // this is just to get rid of the warning "unused parameter"
101 if (argc==0 && argv==NULL) {
102 HLTError("Arguments missing, no arguments" );
103 }
104
960d54ad 105 char lutFileName[500],reglocFileName[500];
6efe69e7 106
960d54ad 107 int i = 0;
108 char* cpErr;
109 while ( i < argc )
110 {
111 HLTDebug("argv[%d] == %s", i, argv[i] );
112
113 if ( !strcmp( argv[i], "lut" ) ) {
114 if ( argc <= i+1 ) {
115 HLTError("LookupTable filename not specified" );
116 return EINVAL; /* Invalid argument */
117 }
6efe69e7 118
960d54ad 119 sprintf(lutFileName,"%s",argv[i+1]);
120
121 i += 2;
122 continue;
123 }// lut argument
124
125 if ( !strcmp( argv[i], "ddl" ) ) {
126 if ( argc <= i+1 ) {
127 HLTError("DDL number not specified" );
128 return EINVAL; /* Invalid argument */
129 }
6efe69e7 130
960d54ad 131 fDDL = strtoul( argv[i+1], &cpErr, 0 );
132 if ( *cpErr )
133 {
134 HLTError("Cannot convert '%s' to DDL Number ", argv[i+1] );
135 return EINVAL;
136 }
137 //fDDL = atoi(argv[i+1]);
138
139 i += 2;
140 continue;
141 }// ddl argument
6efe69e7 142
960d54ad 143 if ( !strcmp( argv[i], "rawdir" ) ) {
144 if ( argc <= i+1 ) {
145 HLTError("DDL directory not specified" );
146 return EINVAL; /* Invalid argument */
147 }
148
149 fDDLDir = argv[i+1] ;
150 i += 2;
151 continue;
152 }// ddl directory argument
153
154 if ( !strcmp( argv[i], "reglocmap" ) ) {
155 if ( argc <= i+1 ) {
156 HLTError("Regional to Local Card mapping filename not specified" );
157 return EINVAL; /* Invalid argument */
158 }
6efe69e7 159
960d54ad 160 sprintf(reglocFileName,"%s",argv[i+1]);
161
162 i += 2;
163 continue;
164 }// regtolocalmap argument
165
166 HLTError("Unknown option '%s'", argv[i] );
167 return EINVAL;
6efe69e7 168
960d54ad 169 }//while loop
6efe69e7 170
171 int lutline = fTrigRec->GetLutLine();
172 AliHLTMUONHitReconstructor::DHLTLut* lookupTable = new AliHLTMUONHitReconstructor::DHLTLut[lutline];
173 if(!ReadLookUpTable(lookupTable,lutFileName)){
960d54ad 174 HLTError("Failed to read lut, lut cannot be read");
6efe69e7 175 return ENOENT ; /* No such file or directory */
176 }else{
177
960d54ad 178 fTrigRec->LoadLookUpTable(lookupTable,fDDL+AliHLTMUONTriggerReconstructor::GetkDDLOffSet());
179
180 AliHLTMUONTriggerReconstructor::RegToLoc regToLocMap[128]; // 16(locCard)*8(regCard)
181 if(!ReadRegToLocMap(regToLocMap,reglocFileName)){
182 HLTError("Failed to read RegToLocMap file");
183 return ENOENT ; /* No such file or directory */
184 }
185
186 if(!(fTrigRec->SetRegToLocCardMap(regToLocMap))){
187 HLTError("Failed to assign RegToLocMap to TrigRec Class due to memory problem");
188 return ENOMEM ; /*cannot allocate memory*/
189 }
6efe69e7 190
191 }// reading lut
192
193 delete []lookupTable;
194
960d54ad 195 HLTInfo("Initialisation of DHLT Trigger Record Component is done");
196
6efe69e7 197 return 0;
198}
199
960d54ad 200
6efe69e7 201int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
960d54ad 202{
227e7192 203 HLTInfo("Deinitialising DHLT Trigger Record Component");
204
205 if(fTrigRec)
206 {
207 delete fTrigRec;
208 fTrigRec = NULL;
209 }
210 return 0;
960d54ad 211}
6efe69e7 212
227e7192 213
960d54ad 214int AliHLTMUONTriggerReconstructorComponent::DoEvent(
215 const AliHLTComponentEventData& evtData,
216 const AliHLTComponentBlockData* blocks,
217 AliHLTComponentTriggerData& trigData,
218 AliHLTUInt8_t* outputPtr,
219 AliHLTUInt32_t& size,
220 std::vector<AliHLTComponentBlockData>& outputBlocks
221 )
222{
227e7192 223 // Process an event
224 unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
6efe69e7 225
227e7192 226 HLTDebug("Processing event %llu with %u input data blocks.",
227 evtData.fEventID, evtData.fBlockCnt
228 );
6efe69e7 229
227e7192 230 // Loop over all input blocks in the event and run the trigger DDL
231 // reconstruction algorithm on the raw data.
232 for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
233 {
234 char id[kAliHLTComponentDataTypefIDsize+1];
235 for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
236 id[i] = blocks[n].fDataType.fID[i];
237 id[kAliHLTComponentDataTypefIDsize] = '\0';
238 char origin[kAliHLTComponentDataTypefOriginSize+1];
239 for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
240 origin[i] = blocks[n].fDataType.fOrigin[i];
241 origin[kAliHLTComponentDataTypefOriginSize] = '\0';
242
243 HLTDebug("Handling block: %u, with fDataType.fID = '%s',"
244 " fDataType.fID = '%s', fPtr = %p and fSize = %u bytes.",
245 n, static_cast<char*>(id), static_cast<char*>(origin)
246 blocks[n].fPtr, blocks[n].fSize
247 );
248
249 if (blocks[n].fDataType != AliHLTMUONConstants::TriggerDDLRawDataType())
250 {
251 // Log a message indicating that we got a data block that we
252 // do not know how to handle.
253 char id[kAliHLTComponentDataTypefIDsize+1];
254 for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
255 id[i] = blocks[n].fDataType.fID[i];
256 id[kAliHLTComponentDataTypefIDsize] = '\0';
257 char origin[kAliHLTComponentDataTypefOriginSize+1];
258 for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
259 origin[i] = blocks[n].fDataType.fOrigin[i];
260 origin[kAliHLTComponentDataTypefOriginSize] = '\0';
261
262 HLTError("Received a data block of an unexpected type: %s origin %s",
263 static_cast<char*>(id), static_cast<char*>(origin)
264 );
265 continue;
266 }
267
268 // Create a new output data block and initialise the header.
269 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
270 if (not block.InitCommonHeader())
271 {
272 HLTError("There is not enough space in the output buffer for the new data block.",
273 " We require at least %u bytes, but have %u bytes left.",
274 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
275 block.BufferSize()
276 );
277 break;
278 }
279
280 AliHLTUInt32_t totalDDLSize = blocks[n].fSize / sizeof(AliHLTUInt32_t);
281 AliHLTUInt32_t ddlRawDataSize = totalDDLSize - fTrigRec->GetkDDLHeaderSize();
282 AliHLTUInt32_t* buffer = reinterpret_cast<AliHLTUInt32_t*>(blocks[n].fPtr)
283 + fTrigRec->GetkDDLHeaderSize();
284 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
285
286 if (not fTrigRec->Run(buffer, ddlRawDataSize, block.GetArray(), nofTrigRec))
287 {
288 HLTError("Error while processing of trigger DDL reconstruction algorithm.");
289 size = totalSize; // Must tell the framework how much buffer space was used.
290 return EIO;
291 }
292
293 // nofTrigRec should now contain the number of triggers actually found
294 // and filled into the output data block, so we can set this number.
295 assert( nofTrigRec <= block.MaxNumberOfEntries() );
296 block.SetNumberOfEntries(nofTrigRec);
297
298 HLTDebug("Number of trigger records found is %d", nofTrigRec);
299
300 // Fill a block data structure for our output block.
301 AliHLTComponentBlockData bd;
302 FillBlockData(bd);
303 bd.fPtr = outputPtr;
304 // This block's start (offset) is after all other blocks written so far.
305 bd.fOffset = totalSize;
306 bd.fSize = block.BytesUsed();
307 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
308 bd.fSpecification = blocks[n].fSpecification;
309 outputBlocks.push_back(bd);
310
311 HLTDebug("Created a new output data block at fPtr = %p,"
312 " with fOffset = %u (0x%.X) and fSize = %u bytes.",
313 bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
314 );
315
316 // Increase the total amount of data written so far to our output memory.
317 totalSize += block.BytesUsed();
318 }
960d54ad 319
227e7192 320 // Finally we set the total size of output memory we consumed.
321 size = totalSize;
322 return 0;
960d54ad 323}
324
6efe69e7 325
326bool AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(AliHLTMUONHitReconstructor::DHLTLut* lookupTable, const char* lutpath)
327{
960d54ad 328 if (fDDL < 0 || fDDL >= 2){
6efe69e7 329 HLTError("DDL number is out of range");
330 return false;
331 }
332
333 int lutLine = fTrigRec->GetLutLine();
334
335 FILE* fin = fopen(lutpath, "r");
336 if (fin == NULL){
337 HLTError("Failed to open file: %s",lutpath);
338 return false;
339 }
340
341 for(int i=0;i<lutLine;i++){
342 fscanf(
343 fin,
344 "%d\t%d\t%d\t%f\t%f\t%f\t%d\t%d\n",
345 &lookupTable[i].fIdManuChannel,
346 &lookupTable[i].fIX,
347 &lookupTable[i].fIY,
348 &lookupTable[i].fRealX,
349 &lookupTable[i].fRealY,
350 &lookupTable[i].fRealZ,
960d54ad 351 &lookupTable[i].fPcbZone,
352 &lookupTable[i].fPlane
6efe69e7 353 );
354 }
355
356 fclose(fin);
357 return true;
358}
359
6efe69e7 360
960d54ad 361bool AliHLTMUONTriggerReconstructorComponent::ReadRegToLocMap(AliHLTMUONTriggerReconstructor::RegToLoc* regToLocMap,const char* reglocFileName)
362{
363 int iTrigDDL,iReg,iLoc,locId,switchWord,detElemId[4];
364 int index;
365
366 memset(regToLocMap,-1,128*sizeof(AliHLTMUONTriggerReconstructor::RegToLoc));
367
368 char s[100];
369 ifstream fin(reglocFileName);
370
371 if(!fin){
372 HLTError("Failed to open file %s",reglocFileName);
373 return false;
374 }
375
376 while(fin.getline(s,100)){
377 sscanf(s,"%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",
378 &iTrigDDL,&iReg,&iLoc,&locId,&switchWord,&detElemId[0],&detElemId[1],&detElemId[2],&detElemId[3]);
379 if(iTrigDDL==fDDL){
380 index = iReg*16 + iLoc;
381 regToLocMap[index].fTrigDDL = iTrigDDL ;
382 regToLocMap[index].fRegId = iReg ;
383 regToLocMap[index].fLoc = iLoc ;
384 regToLocMap[index].fLocId = locId ;
385 regToLocMap[index].fSwitch = switchWord ;
386 for(int idet = 0; idet<4; idet++)
387 regToLocMap[index].fDetElemId[idet] = detElemId[idet] ;
388 }// if matches with fDDL
389 }//file loop
390
391 fin.close();
392 return true;
393}