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