Fixing code so that components use the new standard DDL_RAW data type.
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONTriggerReconstructorComponent.cxx
CommitLineData
6efe69e7 1/**************************************************************************
b39b98c8 2 * This file is property of and copyright by the ALICE HLT Project *
6efe69e7 3 * All rights reserved. *
4 * *
5 * Primary Authors: *
6 * Indranil Das <indra.das@saha.ac.in> *
b39b98c8 7 * Artur Szostak <artursz@iafrica.com> *
6efe69e7 8 * *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
b39b98c8 14 * about the suitability of this software for any purpose. It is *
6efe69e7 15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18/* $Id$ */
19
6253e09b 20///
21/// @file AliHLTMUONTriggerReconstructorComponent.cxx
22/// @author Indranil Das <indra.das@saha.ac.in>, Artur Szostak <artursz@iafrica.com>
23/// @date
24/// @brief Implementation of the trigger DDL reconstructor component.
25///
6efe69e7 26
6efe69e7 27#include "AliHLTMUONTriggerReconstructorComponent.h"
960d54ad 28#include "AliHLTMUONTriggerReconstructor.h"
29#include "AliHLTMUONHitReconstructor.h"
30#include "AliHLTMUONConstants.h"
b39b98c8 31#include "AliHLTMUONUtils.h"
227e7192 32#include "AliHLTMUONDataBlockWriter.h"
33#include <cstdlib>
34#include <cerrno>
5d1682b9 35#include <cassert>
b39b98c8 36#include <fstream>
6efe69e7 37
6efe69e7 38ClassImp(AliHLTMUONTriggerReconstructorComponent)
b39b98c8 39
40
227e7192 41AliHLTMUONTriggerReconstructorComponent::AliHLTMUONTriggerReconstructorComponent() :
6253e09b 42 AliHLTProcessor(),
227e7192 43 fTrigRec(NULL),
b39b98c8 44 fDDL(-1),
80590aa1 45 fWarnForUnexpecedBlock(false),
46 fSuppressPartialTrigs(false)
960d54ad 47{
6253e09b 48 ///
49 /// Default constructor.
50 ///
960d54ad 51}
52
6efe69e7 53
54AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
960d54ad 55{
6253e09b 56 ///
57 /// Default destructor.
58 ///
960d54ad 59}
60
6efe69e7 61
62const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
960d54ad 63{
6253e09b 64 ///
65 /// Inherited from AliHLTComponent. Returns the component ID.
66 ///
67
227e7192 68 return AliHLTMUONConstants::TriggerReconstructorId();
960d54ad 69}
70
71
72void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
73{
6253e09b 74 ///
75 /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
76 ///
77
227e7192 78 list.clear();
668eee9f 79 list.push_back( AliHLTMUONConstants::DDLRawDataType() );
960d54ad 80}
6efe69e7 81
6efe69e7 82
83AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
960d54ad 84{
6253e09b 85 ///
86 /// Inherited from AliHLTComponent. Returns the output data type.
87 ///
88
227e7192 89 return AliHLTMUONConstants::TriggerRecordsBlockDataType();
960d54ad 90}
6efe69e7 91
6efe69e7 92
227e7192 93void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
94 unsigned long& constBase, double& inputMultiplier
95 )
960d54ad 96{
6253e09b 97 ///
98 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
99 ///
100
227e7192 101 constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
c2e03d6e 102 inputMultiplier = 4;
960d54ad 103}
6efe69e7 104
105
6efe69e7 106AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
960d54ad 107{
6253e09b 108 ///
109 /// Inherited from AliHLTComponent. Creates a new object instance.
110 ///
111
227e7192 112 return new AliHLTMUONTriggerReconstructorComponent;
960d54ad 113}
114
6efe69e7 115
960d54ad 116int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
6efe69e7 117{
6253e09b 118 ///
119 /// Inherited from AliHLTComponent.
120 /// Parses the command line parameters and initialises the component.
121 ///
122
b39b98c8 123 // perform initialization.
124
125 HLTInfo("Initialising dHLT trigger reconstructor component.");
6efe69e7 126
b39b98c8 127 fWarnForUnexpecedBlock = false;
128 fSuppressPartialTrigs = false;
129 assert(fTrigRec == NULL);
130 fTrigRec = new AliHLTMUONTriggerReconstructor();
960d54ad 131
b39b98c8 132 const char* lutFileName = NULL;
133
134 for (int i = 0; i < argc; i++)
135 {
136 if ( !strcmp( argv[i], "-lut" ) )
137 {
138 if ( argc <= i+1 )
139 {
140 HLTError("LookupTable filename not specified." );
141 return EINVAL; /* Invalid argument */
142 }
143
144 lutFileName = argv[i+1];
145
146 i++;
147 continue;
148 }
149
150 if ( !strcmp( argv[i], "-ddl" ) )
151 {
152 if ( argc <= i+1 )
153 {
154 HLTError("DDL number not specified." );
155 return EINVAL; /* Invalid argument */
156 }
157
158 char* cpErr = NULL;
159 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
160 if (cpErr == NULL or *cpErr != '\0')
161 {
162 HLTError("Cannot convert '%s' to a DDL Number.", argv[i+1] );
163 return EINVAL;
164 }
165 if (num < 21 or 22 < num)
166 {
167 HLTError("The DDL number must be in the range [21..22].");
168 return EINVAL;
169 }
170 fDDL = num - 1; // Convert to DDL number in the range 0..21
171
172 i++;
173 continue;
174 }
175
176 if (not strcmp( argv[i], "-warn_on_unexpected_block" ))
177 {
178 fWarnForUnexpecedBlock = true;
179 continue;
180 }
181
182 if (not strcmp( argv[i], "-suppress_partial_triggers" ))
183 {
184 fSuppressPartialTrigs = true;
185 continue;
186 }
187
188 HLTError("Unknown option '%s'.", argv[i] );
189 return EINVAL;
190
191 }//while loop
192
193 if (fDDL == -1)
194 {
195 HLTWarning("DDL number not specified. Cannot check if incomming data is valid.");
960d54ad 196 }
960d54ad 197
b39b98c8 198 if (lutFileName != NULL)
199 {
200 if (not ReadLookUpTable(lutFileName))
201 {
202 HLTError("Failed to read lut, lut cannot be read");
203 return ENOENT ; /* No such file or directory */
204 }
960d54ad 205 }
b39b98c8 206 else
207 {
208 HLTWarning("The lookup table has not been specified. Output results will be invalid.");
960d54ad 209 }
b39b98c8 210
211 return 0;
6efe69e7 212}
213
960d54ad 214
6efe69e7 215int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
960d54ad 216{
6253e09b 217 ///
218 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
219 ///
220
b39b98c8 221 HLTInfo("Deinitialising dHLT trigger reconstructor component.");
227e7192 222
b39b98c8 223 if (fTrigRec != NULL)
227e7192 224 {
225 delete fTrigRec;
226 fTrigRec = NULL;
227 }
228 return 0;
960d54ad 229}
6efe69e7 230
227e7192 231
960d54ad 232int AliHLTMUONTriggerReconstructorComponent::DoEvent(
233 const AliHLTComponentEventData& evtData,
b39b98c8 234 const AliHLTComponentBlockData* blocks,
235 AliHLTComponentTriggerData& /*trigData*/,
236 AliHLTUInt8_t* outputPtr,
960d54ad 237 AliHLTUInt32_t& size,
238 std::vector<AliHLTComponentBlockData>& outputBlocks
239 )
240{
6253e09b 241 ///
242 /// Inherited from AliHLTProcessor. Processes the new event data.
243 ///
244
227e7192 245 // Process an event
246 unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
6efe69e7 247
227e7192 248 HLTDebug("Processing event %llu with %u input data blocks.",
249 evtData.fEventID, evtData.fBlockCnt
250 );
6efe69e7 251
227e7192 252 // Loop over all input blocks in the event and run the trigger DDL
253 // reconstruction algorithm on the raw data.
254 for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
255 {
5d1682b9 256#ifdef __DEBUG
227e7192 257 char id[kAliHLTComponentDataTypefIDsize+1];
258 for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
259 id[i] = blocks[n].fDataType.fID[i];
260 id[kAliHLTComponentDataTypefIDsize] = '\0';
261 char origin[kAliHLTComponentDataTypefOriginSize+1];
262 for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
263 origin[i] = blocks[n].fDataType.fOrigin[i];
264 origin[kAliHLTComponentDataTypefOriginSize] = '\0';
5d1682b9 265#endif // __DEBUG
227e7192 266 HLTDebug("Handling block: %u, with fDataType.fID = '%s',"
267 " fDataType.fID = '%s', fPtr = %p and fSize = %u bytes.",
a31f86fa 268 n, static_cast<char*>(id), static_cast<char*>(origin),
227e7192 269 blocks[n].fPtr, blocks[n].fSize
270 );
271
668eee9f 272 if (blocks[n].fDataType != AliHLTMUONConstants::DDLRawDataType()
273 or not AliHLTMUONUtils::IsTriggerDDL(blocks[n].fSpecification)
274 )
227e7192 275 {
276 // Log a message indicating that we got a data block that we
277 // do not know how to handle.
278 char id[kAliHLTComponentDataTypefIDsize+1];
279 for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
280 id[i] = blocks[n].fDataType.fID[i];
281 id[kAliHLTComponentDataTypefIDsize] = '\0';
282 char origin[kAliHLTComponentDataTypefOriginSize+1];
283 for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
284 origin[i] = blocks[n].fDataType.fOrigin[i];
285 origin[kAliHLTComponentDataTypefOriginSize] = '\0';
286
5d1682b9 287 if (fWarnForUnexpecedBlock)
668eee9f 288 HLTWarning("Received a data block of a type we cannot handle: '%s' origin: '%s' spec: 0x%X",
289 static_cast<char*>(id), static_cast<char*>(origin), blocks[n].fSpecification
5d1682b9 290 );
291 else
668eee9f 292 HLTDebug("Received a data block of a type we cannot handle: '%s' origin: '%s' spec: 0x%X",
293 static_cast<char*>(id), static_cast<char*>(origin), blocks[n].fSpecification
5d1682b9 294 );
295
227e7192 296 continue;
297 }
298
b39b98c8 299 bool ddl[22];
300 AliHLTMUONUtils::UnpackSpecBits(blocks[n].fSpecification, ddl);
301 if (not ddl[fDDL])
302 {
303 HLTWarning("Received trigger DDL raw data from a DDL which we did not expect.");
304 }
305
227e7192 306 // Create a new output data block and initialise the header.
307 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
308 if (not block.InitCommonHeader())
309 {
310 HLTError("There is not enough space in the output buffer for the new data block.",
b39b98c8 311 " We require at least %ufTrigRec->GetkDDLHeaderSize() bytes, but have %u bytes left.",
227e7192 312 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
313 block.BufferSize()
314 );
315 break;
316 }
317
318 AliHLTUInt32_t totalDDLSize = blocks[n].fSize / sizeof(AliHLTUInt32_t);
b39b98c8 319 AliHLTUInt32_t ddlRawDataSize = totalDDLSize - 8;
320 AliHLTUInt32_t* buffer = reinterpret_cast<AliHLTUInt32_t*>(blocks[n].fPtr) + 8;
227e7192 321 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
322
80590aa1 323 bool runOk = fTrigRec->Run(
324 buffer, ddlRawDataSize,
325 block.GetArray(), nofTrigRec,
326 fSuppressPartialTrigs
327 );
328 if (not runOk)
227e7192 329 {
330 HLTError("Error while processing of trigger DDL reconstruction algorithm.");
331 size = totalSize; // Must tell the framework how much buffer space was used.
332 return EIO;
333 }
334
335 // nofTrigRec should now contain the number of triggers actually found
336 // and filled into the output data block, so we can set this number.
337 assert( nofTrigRec <= block.MaxNumberOfEntries() );
338 block.SetNumberOfEntries(nofTrigRec);
339
340 HLTDebug("Number of trigger records found is %d", nofTrigRec);
341
342 // Fill a block data structure for our output block.
343 AliHLTComponentBlockData bd;
344 FillBlockData(bd);
345 bd.fPtr = outputPtr;
346 // This block's start (offset) is after all other blocks written so far.
347 bd.fOffset = totalSize;
348 bd.fSize = block.BytesUsed();
349 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
350 bd.fSpecification = blocks[n].fSpecification;
351 outputBlocks.push_back(bd);
352
353 HLTDebug("Created a new output data block at fPtr = %p,"
354 " with fOffset = %u (0x%.X) and fSize = %u bytes.",
355 bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
356 );
357
358 // Increase the total amount of data written so far to our output memory.
359 totalSize += block.BytesUsed();
360 }
960d54ad 361
227e7192 362 // Finally we set the total size of output memory we consumed.
363 size = totalSize;
364 return 0;
960d54ad 365}
366
6efe69e7 367
b39b98c8 368bool AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(const char* lutpath)
6efe69e7 369{
6253e09b 370 ///
371 /// Read in the lookup table from file.
372 ///
373
b39b98c8 374 assert(fTrigRec != NULL);
6efe69e7 375
b39b98c8 376 fstream file;
377 file.open(lutpath, fstream::binary | fstream::in);
378 if (not file)
379 {
380 HLTError("Could not open file: %s", lutpath);
381 return false;
382 }
383
384 file.read(reinterpret_cast<char*>(fTrigRec->LookupTableBuffer()), fTrigRec->LookupTableSize());
385 if (file.eof())
386 {
387 HLTError("The file %s was too short to contain a valid lookup table for this component.", lutpath);
388 file.close();
389 return false;
390 }
391 if (file.bad())
392 {
393 HLTError("Could not read from file: %s", lutpath);
394 file.close();
395 return false;
396 }
397
398 file.close();
399 return true;
960d54ad 400}