]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/OnlineAnalysis/AliHLTMUONHitReconstructorComponent.cxx
c0e2d214efe262a2ea7214172a1ddd04c31727a2
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONHitReconstructorComponent.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///*
19 //
20 //  The HitRec Component is designed to deal the rawdata inputfiles to findout the 
21 //  the reconstructed hits. The output is send to the output block for further 
22 //  processing.
23 //
24 //  Author : Indranil Das ( indra.das@saha.ac.in || indra.ehep@gmail.com )
25 // 
26 //*/
27
28 #if __GNUC__>= 3
29 using namespace std;
30 #endif
31
32 #include "AliHLTMUONHitReconstructorComponent.h"
33 #include "AliHLTMUONHitReconstructor.h"
34 #include "AliHLTMUONRecHitsBlockStruct.h"
35 #include "AliHLTLogging.h"
36 #include "AliHLTSystem.h"
37 #include "AliHLTDefinitions.h"
38 #include <stdlib.h>
39 #include <errno.h>
40
41 namespace
42 {
43         // The global object used for automatic component registration, 
44         // Note DO NOT use this component for calculation!
45         AliHLTMUONHitReconstructorComponent gAliHLTMUONHitReconstructorComponent;
46 }
47
48 ClassImp(AliHLTMUONHitReconstructorComponent)
49
50
51 AliHLTMUONHitReconstructorComponent::AliHLTMUONHitReconstructorComponent()
52   : 
53   fHitRec(NULL)
54 {
55   // see header file for class documentation
56 }
57
58 AliHLTMUONHitReconstructorComponent::~AliHLTMUONHitReconstructorComponent()
59 {
60   // see header file for class documentation
61
62 }
63
64 int AliHLTMUONHitReconstructorComponent::DoInit( int argc, const char** argv ){
65   // see header file for class documentation
66
67   fHitRec = new AliHLTMUONHitReconstructor();
68
69   HLTInfo("dHLT hitrec");
70   if (argc==0 && argv==NULL) {
71     Logging( kHLTLogError, "AliHLTMUONHitReconstructorComponent::DoInit", "Arguments missing", " no arguments" );
72     // this is just to get rid of the warning "unused parameter"
73   }
74
75   Int_t i = 0;
76   char lutFileName[500],buspatchFileName[500];
77   int iDDL = -1;
78   while(i<argc){
79     if ( !strcmp( argv[i], "lut" ) ) {
80       if ( argc <= i+1 ) {
81         Logging( kHLTLogError, "AliHLTMUONHitReconstructorComponent::DoInit", "Missing LookupTable filename", "LookupTable filename not specified" );
82         return EINVAL; /* Invalid argument */ 
83       }
84       
85       sprintf(lutFileName,"%s",argv[i+1]);
86
87       i += 2;
88       continue;
89     }// lut argument
90
91     if ( !strcmp( argv[i], "ddl" ) ) {
92       if ( argc <= i+1 ) {
93         Logging( kHLTLogError, "AliHLTMUONHitReconstructorComponent::DoInit", "Missing DDL argument", "DDL number not specified" );
94         HLTError("AliHLTMUONHitReconstructorComponent::DoInit : DDL number is not specified ");
95         return EINVAL;  /* Invalid argument */
96       }
97
98       iDDL = atoi(argv[i+1]);
99
100       i += 2;
101       continue;
102     }// ddl argument
103
104     if ( !strcmp( argv[i], "buspatchmap" ) ) {
105       if ( argc <= i+1 ) {
106         Logging( kHLTLogError, "AliHLTMUONHitReconstructorComponent::DoInit", "Missing buspatch filename", "buspatch filename not specified" );
107         return EINVAL; /* Invalid argument */
108       }
109       
110       sprintf(buspatchFileName,"%s",argv[i+1]);
111
112       i += 2;
113       continue;
114     }// buspatch argument
115
116
117
118   }// end of while loop
119
120   int lutline = fHitRec->GetLutLine(iDDL);
121   AliHLTMUONHitReconstructor::DHLTLut* lookupTable = new AliHLTMUONHitReconstructor::DHLTLut[lutline];
122   if(!ReadLookUpTable(lookupTable,lutFileName,iDDL)){
123     Logging(kHLTLogInfo, "AliHLTMUONHitReconstructorComponent::DoInit", "Failed to read lut", "lut cannot be read, DoInit");
124     return ENOENT ; /* No such file or directory */
125   }else{
126     for(int i = 0;i<lutline; i++){
127       //#ifdef MY_DEBUG
128 //       printf("%d\t%d\t%d\t%f\t%f\t%f\t%d\t\n",
129 //           lookupTable[i].fIdManuChannel,
130 //           lookupTable[i].fIX,
131 //           lookupTable[i].fIY,
132 //           lookupTable[i].fRealX,
133 //           lookupTable[i].fRealY,
134 //           lookupTable[i].fRealZ,
135 //           lookupTable[i].fPcbZone,
136 //           lookupTable[i].fPlane
137 //           );
138       //#endif
139     }
140
141     BusToDetElem busToDetElem;
142     if(!ReadBusPatchToDetElemFile(busToDetElem,buspatchFileName)){
143       Logging(kHLTLogInfo, "AliHLTMUONHitReconstructorComponent::DoInit", "Failed to read buspatchmap", "buspatchmap cannot be read, DoInit");
144       return ENOENT ; /* No such file or directory */
145     }
146
147     fHitRec->SetBusToDetMap(busToDetElem);
148     fHitRec->LoadLookUpTable(lookupTable,iDDL);
149
150   }// reading lut
151
152   return 0;
153 }
154
155 int AliHLTMUONHitReconstructorComponent::DoDeinit(){
156   // see header file for class documentation
157   if(fHitRec)
158     delete fHitRec;
159   HLTInfo("dHLT hitrec");
160   return 0;
161 }
162
163 int AliHLTMUONHitReconstructorComponent::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks, 
164                                       AliHLTComponentTriggerData& trigData, AliHLTUInt8_t* outputPtr, 
165                                       AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks ) {
166   // see header file for class documentation
167   HLTInfo("dHLT hitrec");
168
169 //   if (evtData.fStructSize==0 && blocks==NULL && trigData.fStructSize==0 &&
170 //       outputPtr==0 && size==0)
171 //   {
172 //     outputBlocks.clear();
173 //     // this is just to get rid of the warning "unused parameter"
174 //  }
175
176   unsigned long totalSize = 0;
177   unsigned long mySize;
178   //cout<<"Block Count : "<<evtData.fBlockCnt<<endl;
179   for(UInt_t i=0;i<evtData.fBlockCnt;i++){
180
181     cout<<"0: totalsize : "<<totalSize<<"\tkAliHLTBlockAlignment :"<<kAliHLTBlockAlignment<<"\t size :"<<size<<endl;
182
183     // Align the beginning of this  block to the required value.
184     //    if ( totalSize % kAliHLTBlockAlignment ){
185     //      totalSize += kAliHLTBlockAlignment-(totalSize % kAliHLTBlockAlignment);
186     //    }
187
188     if ( totalSize > size )
189       break;
190     
191     cout<<"1: totalsize : "<<totalSize<<"\tkAliHLTBlockAlignment :"<<kAliHLTBlockAlignment<<endl;
192     // Determine the size we should use for the output for this block (the input block's size times the relative output size)
193     
194
195     int totalDDLSize = blocks[i].fSize/4;
196     int ddlRawDataSize = totalDDLSize - AliHLTMUONHitReconstructor::GetkDDLHeaderSize();
197     int ddlHeader[8];
198     memcpy((char *) & ddlHeader,blocks[i].fPtr,(size_t)4*(AliHLTMUONHitReconstructor::GetkDDLHeaderSize()));
199
200 //     for(int j=0;j<8;j++)
201 //       HLTDebug("ddlHeader[%d] : %d\n",j,ddlHeader[j]);
202
203     int* buffer = new int[ddlRawDataSize];
204     memcpy((int*)buffer,((int*)blocks[i].fPtr + AliHLTMUONHitReconstructor::GetkDDLHeaderSize()),(sizeof(int)*ddlRawDataSize));
205
206 //     for(int j=0;j<ddlRawDataSize;j++)
207 //       HLTDebug("buffer[%d] : %x\n",j,buffer[j]);
208
209     
210     AliHLTMUONRecHitStruct recHit[300];
211     int nofHit = 300;
212    
213     if(! (fHitRec->Run(buffer,&ddlRawDataSize,recHit,&nofHit))){
214       cerr <<"AliHLTMUONHitReconstructorComponent::DoEvent : ERROR In Processing of HitRec Algo "<< endl;
215       return EIO;
216     }
217
218     mySize = sizeof(AliHLTMUONRecHitStruct)*nofHit;
219     
220     HLTInfo("mySize set (1) mySize == %lu B - blocks[%lu].fSize == %lu", 
221              mySize, i, blocks[i].fSize);
222     
223     // Check how much space we have left and adapt this output block's size accordingly.
224     if ( totalSize + mySize > size )
225       mySize = size-totalSize;
226     
227     Logging( kHLTLogInfo, "HLT::Dummy::DoEvent", "mySize set (2)", "mySize == %lu B - totalSize == %lu - size == %lu", 
228                  mySize, totalSize, size );
229
230     if ( mySize<=0 )
231       continue; // No room left to write a further block.
232
233     // Now copy the input block
234     unsigned long copied = 0;
235     // First copy all full multiples of the input block
236     while ( copied+blocks[i].fSize <= mySize )
237       {
238         Logging( kHLTLogInfo, "0 : HLT::Dummy::DoEvent", "Copying", "Copying %lu B - Copied: %lu B - totalSize: %lu B", 
239                  blocks[i].fSize, copied, totalSize );
240         memcpy( outputPtr+totalSize+copied, blocks[i].fPtr, blocks[i].fSize );
241         copied += blocks[i].fSize;
242       }
243     // And the copy the remaining fragment of the block
244     Logging( kHLTLogInfo, "1 : HLT::Dummy::DoEvent", "Copying", "Copying %lu B - Copied: %lu B - totalSize: %lu B", 
245              mySize-copied, copied, totalSize );
246     memcpy( outputPtr+totalSize+copied, blocks[i].fPtr, mySize-copied );
247     Logging( kHLTLogInfo, "HLT::Dummy::DoEvent", "Copied", "Copied: %lu B - totalSize: %lu B", 
248              copied, totalSize );
249     
250
251     
252     AliHLTComponentBlockData bd;
253     FillBlockData( bd );
254     bd.fOffset = totalSize;;
255     bd.fSize = mySize;
256     //bd.fPtr = (AliHLTUInt8_t*)(&recHit[0]);
257     bd.fSpecification = blocks[i].fSpecification;
258     outputBlocks.push_back( bd );
259     
260     totalSize += mySize;
261
262     //    for(int j=0; j<nofHit; j++){
263 //       printf("%d\t\t%d\t\t%f\t%f\t%f\n",
264 //           i,outPtr->fRecPoint[j].fDetElemId,outPtr->fRecPoint[j].fX,
265 //           outPtr->fRecPoint[j].fY,outPtr->fRecPoint[j].fZ);
266 //       printf("1 : %d\t\t%d\t\t%f\t%f\t%f\n",
267 //           i,(((AliHLTMUONRecHitStruct*)bd.fPtr) + j)->fDetElemId,
268 //           (((AliHLTMUONRecHitStruct*)bd.fPtr) + j)->fX,
269 //           (((AliHLTMUONRecHitStruct*)bd.fPtr) + j)->fY,
270 //           (((AliHLTMUONRecHitStruct*)bd.fPtr) + j)->fZ);
271 //    }// nof RecHits
272
273
274     if(totalSize > size){
275       cout<<"size : "<<size<<"\t  totalSize: "<<totalSize<<"\t mySize :"<<mySize<<endl; 
276       Logging( kHLTLogError, "AliHLTMUONHitReconstructorComponent::DoEvent", "Size exceeds the quota", "Size Exceeds the maximum quota" );
277       return EMSGSIZE;
278     }
279
280   }// block loop
281
282   size = totalSize;
283   
284   return 0;
285 }
286
287
288 bool AliHLTMUONHitReconstructorComponent::ReadLookUpTable(
289                 AliHLTMUONHitReconstructor::DHLTLut* lookupTable, const char* lutpath, int iDDL
290         )
291 {
292 // Reads in a lookup table for the hit reconstruction algorithm from file.
293
294         if (iDDL < AliHLTMUONHitReconstructor::GetkDDLOffSet() ||
295                 iDDL >= AliHLTMUONHitReconstructor::GetkDDLOffSet() + AliHLTMUONHitReconstructor::GetkNofDDL())
296         {
297 //              Logging(kHLTLogError, "AliHLTMUONHitReconstructorComponent::ReadLookUpTable", "Invalid DDL")
298 //                      << "DDL number is out of range (must be " << AliHLTLog::kDec << HLTMUONHitRec::fgkDDLOffSet
299 //                      << " <= iDDL < " << AliHLTLog::kDec
300 //                      << HLTMUONHitRec::fgkDDLOffSet + HLTMUONHitRec::fgkNofDDL
301 //                      << ENDLOG;
302                 return false;
303         }
304
305         int lutLine = fHitRec->GetLutLine(iDDL);
306 //      cout<<"LutLine :"<<lutLine<<endl;
307         FILE* fin = fopen(lutpath, "r");
308         if (fin == NULL)
309         {
310 //              LOG(kHLTLogError, "AliHLTMUONHitReconstructorComponent::ReadLookUpTable", "I/O error")
311 //                      << "Failed to open file: " << lutpath << ENDLOG;
312                 return false;
313         }
314
315 #       ifdef DEBUG
316 //      Logging(kHLTLogError, "AliHLTMUONHitReconstructorComponent::ReadLookUpTable", "Trace")
317 //              << "Reading LUT file: " << lutpath << ENDLOG;
318 #       endif
319
320         for(int i=0;i<lutLine;i++)
321         {
322                 fscanf(
323                         fin,
324                         "%d\t%d\t%d\t%f\t%f\t%f\t%d\t%d\n",
325                         &lookupTable[i].fIdManuChannel,
326                         &lookupTable[i].fIX,
327                         &lookupTable[i].fIY,
328                         &lookupTable[i].fRealX,
329                         &lookupTable[i].fRealY,
330                         &lookupTable[i].fRealZ,
331                         &lookupTable[i].fPcbZone,
332                         &lookupTable[i].fPlane
333                 );
334         }
335
336         fclose(fin);
337         return true;
338 }
339
340 // implement this as well.
341
342 bool AliHLTMUONHitReconstructorComponent::ReadBusPatchToDetElemFile(
343                 BusToDetElem& busToDetElem, const char* buspatchmappath
344         )
345 {
346 // Loads the bus patch to detector element ID map from an ASCII file. 
347         
348         char getLine[80];
349         char temp;
350         int detElem, minBusPatch, maxBusPatch;
351
352         FILE* fin = fopen(buspatchmappath, "r");
353         if (fin == NULL)
354         {
355 //              Logging(kHLTLogError, "AliHLTMUONHitReconstructorComponent::ReadBusPatchToDetElemFile", "I/O error")
356 //                      << "Failed to open file: " << buspatchmappath << ENDLOG;
357                 return false;
358         }
359
360 #       ifdef DEBUG
361 //      Logging(kHLTLogError, "AliHLTMUONHitReconstructorComponent::ReadBusPatchToDetElemFile", "Trace")
362 //              << "Reading bus patch mapping file: " << buspatchmappath << ENDLOG;
363 #       endif
364
365         while (feof(fin)==0)
366         {
367                 fgets(getLine,80,fin);
368                 sscanf(getLine, "%d\t%d %c %d", &detElem, &minBusPatch, &temp, &maxBusPatch);
369                 if (detElem >= 700 && detElem <= 1025)
370                 {
371                         for(int i = minBusPatch; i <= maxBusPatch; i++)
372                                 busToDetElem[i] = detElem;
373                 } // detElem condn
374         } // while loop for file
375
376         fclose(fin);
377         return true;
378 }