Adding optional flag to choose if we should log a warning if we get an unexpected...
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONHitReconstructorComponent.cxx
CommitLineData
b0201cbe 1/**************************************************************************
960d54ad 2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
b0201cbe 4 * *
960d54ad 5 * Primary Authors: *
6 * Indranil Das <indra.das@saha.ac.in> *
b0201cbe 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 *
960d54ad 13 * about the suitability of this software for any purpose. It is *
b0201cbe 14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
16
17/* $Id$ */
18
baff881d 19///*
20//
21// The HitRec Component is designed to deal the rawdata inputfiles to findout the
22// the reconstructed hits. The output is send to the output block for further
23// processing.
24//
25// Author : Indranil Das ( indra.das@saha.ac.in || indra.ehep@gmail.com )
26//
27//*/
b0201cbe 28
960d54ad 29#include "AliHLTMUONRecHitsBlockStruct.h"
b0201cbe 30#include "AliHLTMUONHitReconstructorComponent.h"
b12fe461 31#include "AliHLTMUONHitReconstructor.h"
960d54ad 32#include "AliHLTMUONConstants.h"
5ff5f960 33#include "AliHLTMUONDataBlockWriter.h"
b0201cbe 34#include "AliHLTLogging.h"
35#include "AliHLTSystem.h"
36#include "AliHLTDefinitions.h"
37#include <stdlib.h>
38#include <errno.h>
39
40namespace
41{
42 // The global object used for automatic component registration,
43 // Note DO NOT use this component for calculation!
44 AliHLTMUONHitReconstructorComponent gAliHLTMUONHitReconstructorComponent;
45}
46
b0201cbe 47ClassImp(AliHLTMUONHitReconstructorComponent)
48
49
50AliHLTMUONHitReconstructorComponent::AliHLTMUONHitReconstructorComponent()
960d54ad 51 :
52 fHitRec(NULL),
53 fDDLDir(""),
54 fDDL(0),
55 fReaderType(false)
b0201cbe 56{
b0201cbe 57}
58
960d54ad 59
b0201cbe 60AliHLTMUONHitReconstructorComponent::~AliHLTMUONHitReconstructorComponent()
61{
960d54ad 62}
b0201cbe 63
960d54ad 64const char* AliHLTMUONHitReconstructorComponent::GetComponentID()
65{
66 return "MUONHitRec"; // The ID of this component
b0201cbe 67}
68
b0201cbe 69
960d54ad 70void AliHLTMUONHitReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
71{
72 list.clear();
73 list.push_back( AliHLTMUONConstants::TrackingDDLRawDataType() );
74}
b0201cbe 75
b0201cbe 76
960d54ad 77AliHLTComponentDataType AliHLTMUONHitReconstructorComponent::GetOutputDataType()
78{
79 return AliHLTMUONConstants::RecHitsBlockDataType();
80}
b0201cbe 81
b0201cbe 82
960d54ad 83void AliHLTMUONHitReconstructorComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
84{
85 constBase = 0;
86 inputMultiplier = 1;
87}
b0201cbe 88
b0201cbe 89
960d54ad 90// Spawn function, return new instance of this class
91AliHLTComponent* AliHLTMUONHitReconstructorComponent::Spawn()
92{
93 return new AliHLTMUONHitReconstructorComponent;
94}
b0201cbe 95
b0201cbe 96
960d54ad 97int AliHLTMUONHitReconstructorComponent::DoInit( int argc, const char** argv )
98{
99 // perform initialization. We check whether our relative output size is specified in the arguments.
100
101 HLTInfo("Initialising DHLT HitReconstruction Component");
b0201cbe 102
960d54ad 103 fHitRec = new AliHLTMUONHitReconstructor();
104
b0201cbe 105
960d54ad 106 // this is to get rid of the warning "unused parameter"
107 if (argc==0 && argv==NULL) {
108 HLTError("Arguments missing", " no arguments" );
109 }
b0201cbe 110
960d54ad 111 char lutFileName[500],buspatchFileName[500];
b0201cbe 112
960d54ad 113 int i = 0;
114 char* cpErr;
115 while ( i < argc )
116 {
117 HLTDebug("argv[%d] == %s", i, argv[i] );
118
119 if ( !strcmp( argv[i], "lut" ) ) {
120 if ( argc <= i+1 ) {
121 HLTError("LookupTable filename not specified" );
122 return EINVAL; /* Invalid argument */
123 }
124
125 sprintf(lutFileName,"%s",argv[i+1]);
126
127 i += 2;
128 continue;
129 }// lut argument
130
131
132 if ( !strcmp( argv[i], "ddl" ) ) {
133 if ( argc <= i+1 ) {
134 HLTError("DDL number not specified" );
135 return EINVAL; /* Invalid argument */
136 }
137
138 fDDL = strtoul( argv[i+1], &cpErr, 0 );
139 if ( *cpErr )
140 {
141 HLTError("Cannot convert '%s' to DDL Number ", argv[i+1] );
142 return EINVAL;
143 }
144
145 i += 2;
146 continue;
147 }// ddl argument
148
149
150 if ( !strcmp( argv[i], "rawdir" ) ) {
151 if ( argc <= i+1 ) {
152 HLTError("DDL directory not specified" );
153 return EINVAL; /* Invalid argument */
154 }
155
156 fDDLDir = argv[i+1] ;
157
158 i += 2;
159 continue;
160 }// ddl directory argument
161
162
163 if ( !strcmp( argv[i], "buspatchmap" ) ) {
164 if ( argc <= i+1 ) {
165 HLTError("Buspatch filename not specified" );
166 return EINVAL; /* Invalid argument */
167 }
168
169 sprintf(buspatchFileName,"%s",argv[i+1]);
170
171 i += 2;
172 continue;
173 }// buspatch argument
174
175 if ( !strcmp( argv[i], "rawreader" ) ) {
176 fReaderType = true; // true when using rawreader for standalone it is set to false.
177 i += 1;
178 continue;
179 }
180
181 HLTError("Unknown option '%s'", argv[i] );
182 return EINVAL;
183
184 }//while loop
185
186 int lutline = fHitRec->GetLutLine(fDDL);
baff881d 187 AliHLTMUONHitReconstructor::DHLTLut* lookupTable = new AliHLTMUONHitReconstructor::DHLTLut[lutline];
960d54ad 188 if(!ReadLookUpTable(lookupTable,lutFileName)){
189 HLTError("Failed to read lut, lut cannot be read, DoInit");
b0201cbe 190 return ENOENT ; /* No such file or directory */
191 }else{
960d54ad 192
b0201cbe 193 BusToDetElem busToDetElem;
960d54ad 194 BusToDDL busToDDL;
195 if(!ReadBusPatchToDetElemFile(busToDetElem,busToDDL,buspatchFileName)){
196 HLTError("Failed to read buspatchmap, buspatchmap cannot be read, DoInit");
b0201cbe 197 return ENOENT ; /* No such file or directory */
198 }
960d54ad 199
b0201cbe 200 fHitRec->SetBusToDetMap(busToDetElem);
960d54ad 201 fHitRec->SetBusToDDLMap(busToDDL);
202 fHitRec->LoadLookUpTable(lookupTable,fDDL);
203
b0201cbe 204 }// reading lut
205
960d54ad 206 delete []lookupTable;
207
208 HLTInfo("Initialisation of DHLT HitReconstruction Component is done");
209
b0201cbe 210 return 0;
211}
212
960d54ad 213
214int AliHLTMUONHitReconstructorComponent::DoDeinit()
215{
b0201cbe 216 if(fHitRec)
217 delete fHitRec;
960d54ad 218
219 HLTInfo(" Deinitialising DHLT HitReconstruction Component");
220
b0201cbe 221 return 0;
222}
223
b0201cbe 224
960d54ad 225int AliHLTMUONHitReconstructorComponent::DoEvent(
226 const AliHLTComponentEventData& evtData,
227 const AliHLTComponentBlockData* blocks,
228 AliHLTComponentTriggerData& trigData,
229 AliHLTUInt8_t* outputPtr,
230 AliHLTUInt32_t& size,
231 std::vector<AliHLTComponentBlockData>& outputBlocks
232 )
233{
234 // Process an event
b0201cbe 235 unsigned long totalSize = 0;
960d54ad 236
237 HLTDebug("Event : %d has : %lu blocks",(int)evtData.fEventID,evtData.fBlockCnt);
238
239 // Loop over all input blocks in the event
240 for ( unsigned long n = 0; n < evtData.fBlockCnt; n++ )
241 {
242
243 HLTDebug("block : %d, block rawData : %p, block.fSize (bytes) : %d, blocks.fDataType.fID : %s, blocks.fDataType.fOrigin : %s, required type : %s\n",
244 n,blocks[n].fPtr,blocks[n].fSize,(char *)(blocks[n].fDataType.fID),
245 (char *)(blocks[n].fDataType.fOrigin,(char *)(AliHLTMUONConstants::TrackingDDLRawDataType().fID)));
246
b0201cbe 247
960d54ad 248 if(strncmp((char *)(blocks[n].fDataType.fID),(char *)(AliHLTMUONConstants::TrackingDDLRawDataType().fID),kAliHLTComponentDataTypefIDsize)) continue;
249
250
251 if ( totalSize > size )
252 break;
253
254 int totalDDLSize = blocks[n].fSize/sizeof(int);
255
256 if(!totalDDLSize) continue;
257
258 int ddlRawDataSize = totalDDLSize - fHitRec->GetkDDLHeaderSize();
259 int *buffer = (int *)blocks[n].fPtr;
260
261
262 for(int j=0;j<totalDDLSize;j++)
263 HLTDebug("buffer[%d] : %x\n",j,buffer[j]);
264
265 buffer = (int *)((int *)blocks[n].fPtr + fHitRec->GetkDDLHeaderSize()) ;
266
267 AliHLTMUONRecHitStruct recHit[300];
268 int nofHit = 300;
269
270 if(! (fHitRec->Run(buffer,&ddlRawDataSize,recHit,&nofHit))){
271 HLTError("ERROR In Processing of HitRec Algo ");
272 return EIO;
b0201cbe 273 }
960d54ad 274
275 unsigned long mySize = sizeof(AliHLTMUONRecHitStruct) * nofHit;
276
277 HLTDebug("Event %d and block %d has nofHit %d\n",(int)evtData.fEventID,n,nofHit);
278
279 // Check how much space we have left and adapt this output block's size accordingly.
280 if ( totalSize + mySize > size )
281 mySize = size-totalSize;
282
283 Logging( kHLTLogDebug, "HLT::MUONHitRec::DoEvent", "mySize set (2)", "mySize == %lu B - totalSize == %lu - size == %lu",
284 mySize, totalSize, size );
285
286 if ( mySize<=0 )
287 continue; // No room left to write a further block.
288
5ff5f960 289 AliHLTMUONRecHitsBlockWriter block(outputPtr+totalSize, size-totalSize);
290 if (not block.InitCommonHeader())
291 {
292 HLTError("There is not enough space in the output buffer for the new data block.",
293 " We require at least %u bytes, but have %u bytes left.",
294 sizeof(AliHLTMUONRecHitsBlockWriter::HeaderType),
295 block.BufferSize()
296 );
297 break;
298 }
299
960d54ad 300 // Now copy the input block
301 unsigned long copied = 0;
302 // First copy all full multiples of the input block
303
304 // And the copy the remaining fragment of the block
305 Logging( kHLTLogDebug, "1 : HLT::MUONHitRec::DoEvent", "Copying", "Copying %lu B - Copied: %lu B - totalSize: %lu B",
306 mySize-copied, copied, totalSize );
5ff5f960 307 memcpy( block.GetArray(), &recHit[0], mySize);
960d54ad 308 Logging( kHLTLogDebug, "HLT::MUONHitRec::DoEvent", "Copied", "Copied: %lu B - totalSize: %lu B",
309 copied, totalSize );
310
5ff5f960 311 block.SetNumberOfEntries(nofHit);
312
960d54ad 313 // Fill a block data structure for our output block.
314 AliHLTComponentBlockData bd;
315 FillBlockData(bd);
316 bd.fPtr = outputPtr;
317 // This block's start (offset) is after all other blocks written so far.
318 bd.fOffset = totalSize;
5ff5f960 319 bd.fSize = block.BytesUsed();
960d54ad 320 bd.fDataType = AliHLTMUONConstants::RecHitsBlockDataType();
321 bd.fSpecification = blocks[n].fSpecification;
322 outputBlocks.push_back(bd);
323
324 // Increase the total amount of data written so far to our output memory
5ff5f960 325 totalSize += block.BytesUsed();
b0201cbe 326 }
960d54ad 327 // Finally we set the total size of output memory we consumed.
b0201cbe 328 size = totalSize;
329
330 return 0;
331}
332
333
960d54ad 334bool AliHLTMUONHitReconstructorComponent::ReadLookUpTable(AliHLTMUONHitReconstructor::DHLTLut* lookupTable, const char* lutpath)
b0201cbe 335{
960d54ad 336 if (fDDL < AliHLTMUONHitReconstructor::GetkDDLOffSet() ||
337 fDDL >= AliHLTMUONHitReconstructor::GetkDDLOffSet() + AliHLTMUONHitReconstructor::GetkNofDDL()){
338 HLTError("DDL number is out of range");
339 return false;
340 }
341
342 int lutLine = fHitRec->GetLutLine(fDDL);
343
344 FILE* fin = fopen(lutpath, "r");
345 if (fin == NULL){
346 HLTError("Failed to open file: %s",lutpath);
347 return false;
348 }
349
350 for(int i=0;i<lutLine;i++){
351 fscanf(
352 fin,
353 "%d\t%d\t%d\t%f\t%f\t%f\t%d\t%d\n",
354 &lookupTable[i].fIdManuChannel,
355 &lookupTable[i].fIX,
356 &lookupTable[i].fIY,
357 &lookupTable[i].fRealX,
358 &lookupTable[i].fRealY,
359 &lookupTable[i].fRealZ,
360 &lookupTable[i].fPcbZone,
361 &lookupTable[i].fPlane
362 );
363 }
364
365 fclose(fin);
366 return true;
b0201cbe 367}
368
b0201cbe 369
960d54ad 370bool AliHLTMUONHitReconstructorComponent::ReadBusPatchToDetElemFile(BusToDetElem& busToDetElem, BusToDDL& busToDDL, const char* buspatchmappath)
b0201cbe 371{
960d54ad 372 char getLine[80];
373 char temp;
374 int detElem, minBusPatch, maxBusPatch, ddl;
375
376 FILE* fin = fopen(buspatchmappath, "r");
377 if (fin == NULL){
378 HLTError("Failed to open file: %s",buspatchmappath);
379 return false;
380 }
381
382 while (feof(fin)==0){
383 fgets(getLine,80,fin);
384 sscanf(getLine, "%d\t%d %c %d\t%d", &detElem, &minBusPatch, &temp, &maxBusPatch,&ddl);
385 if (detElem >= 700 && detElem <= 1025){
386
387 for(int i = minBusPatch; i <= maxBusPatch; i++){
388 busToDetElem[i] = detElem;
389 busToDDL[i] = ddl;
390 }//for loop
391 } // detElem condn
392 } // while loop for file
393
394 fclose(fin);
395 return true;
b0201cbe 396}