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