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