]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/OnlineAnalysis/AliHLTMUONTriggerReconstructorComponent.cxx
Updates (B. Vulpescu)
[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 #include "AliHLTMUONTriggerReconstructorComponent.h"
25 #include "AliHLTMUONTriggerReconstructor.h"
26 #include "AliHLTMUONHitReconstructor.h"
27 #include "AliHLTMUONConstants.h"
28 #include "AliHLTMUONDataBlockWriter.h"
29 #include <cstdlib>
30 #include <cerrno>
31 #include <cassert>
32
33 namespace
34 {
35         // This is a global object used for automatic component registration,
36         // do not use this for calculation.
37         AliHLTMUONTriggerReconstructorComponent gAliHLTMUONTriggerReconstructorComponent;
38
39 } // end of namespace
40
41
42 ClassImp(AliHLTMUONTriggerReconstructorComponent)
43     
44     
45 AliHLTMUONTriggerReconstructorComponent::AliHLTMUONTriggerReconstructorComponent() :
46         fTrigRec(NULL),
47         fDDLDir(""),
48         fDDL(0),
49         fWarnForUnexpecedBlock(false),
50         fSuppressPartialTrigs(false)
51 {
52 }
53
54
55 AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
56 {
57 }
58
59
60 const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
61 {
62         return AliHLTMUONConstants::TriggerReconstructorId();
63 }
64
65
66 void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
67 {
68         list.clear();
69         list.push_back( AliHLTMUONConstants::TriggerDDLRawDataType() );
70 }
71
72
73 AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
74 {
75         return AliHLTMUONConstants::TriggerRecordsBlockDataType();
76 }
77
78
79 void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
80                 unsigned long& constBase, double& inputMultiplier
81         )
82 {
83         constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
84         inputMultiplier = 4;
85 }
86
87
88 AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
89 {
90         return new AliHLTMUONTriggerReconstructorComponent;
91 }
92
93
94 int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
95 {
96   // perform initialization. We check whether our relative output size is
97   // specified in the arguments.
98   
99   HLTInfo("Initialising DHLT Trigger Record Component");
100
101   fWarnForUnexpecedBlock = false;
102   fSuppressPartialTrigs = false;
103   fTrigRec = new AliHLTMUONTriggerReconstructor();
104       
105   // this is just 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],reglocFileName[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       if ( !strcmp( argv[i], "ddl" ) ) {
131         if ( argc <= i+1 ) {
132           HLTError("DDL number not specified" );
133           return EINVAL;  /* Invalid argument */
134         }
135
136         fDDL = strtoul( argv[i+1], &cpErr, 0 );
137         if ( *cpErr )
138           {
139             HLTError("Cannot convert '%s' to DDL Number ", argv[i+1] );
140             return EINVAL;
141           }
142         //fDDL = atoi(argv[i+1]);
143         
144         i += 2;
145         continue;
146       }// ddl argument
147           
148       if ( !strcmp( argv[i], "rawdir" ) ) {
149         if ( argc <= i+1 ) {
150           HLTError("DDL directory not specified" );
151           return EINVAL;  /* Invalid argument */
152         }
153
154         fDDLDir = argv[i+1] ;
155         i += 2;
156         continue;
157       }// ddl directory argument
158
159       if ( !strcmp( argv[i], "reglocmap" ) ) {
160         if ( argc <= i+1 ) {
161           HLTError("Regional to Local Card mapping  filename not specified" );
162           return EINVAL; /* Invalid argument */
163         }
164
165         sprintf(reglocFileName,"%s",argv[i+1]);
166
167         i += 2;
168         continue;
169       }// regtolocalmap argument
170           
171       if ( !strcmp( argv[i], "-warn_on_unexpected_block" ) ) {
172         fWarnForUnexpecedBlock = true;
173         i++;
174         continue;
175       }
176           
177       if ( !strcmp( argv[i], "-suppress_partial_triggers" ) ) {
178         fSuppressPartialTrigs = true;
179         i++;
180         continue;
181       }
182
183       HLTError("Unknown option '%s'", argv[i] );
184       return EINVAL;
185           
186     }//while loop
187
188     int lutline = fTrigRec->GetLutLine();
189     AliHLTMUONHitReconstructor::DHLTLut* lookupTable = new AliHLTMUONHitReconstructor::DHLTLut[lutline];
190     if(!ReadLookUpTable(lookupTable,lutFileName)){
191       HLTError("Failed to read lut, lut cannot be read");
192       return ENOENT ; /* No such file or directory */
193     }else{
194       
195       fTrigRec->LoadLookUpTable(lookupTable,fDDL+AliHLTMUONTriggerReconstructor::GetkDDLOffSet());
196
197       AliHLTMUONTriggerReconstructor::RegToLoc regToLocMap[128]; // 16(locCard)*8(regCard)
198       if(!ReadRegToLocMap(regToLocMap,reglocFileName)){
199         HLTError("Failed to read RegToLocMap file");
200         return ENOENT ; /* No such file or directory */
201       }
202
203       if(!(fTrigRec->SetRegToLocCardMap(regToLocMap))){
204         HLTError("Failed to assign RegToLocMap to TrigRec Class due to memory problem");
205         return ENOMEM ; /*cannot allocate memory*/
206       }
207       
208     }// reading lut
209
210     delete []lookupTable;
211
212     HLTInfo("Initialisation of DHLT Trigger Record Component is done");
213
214     return 0;
215 }
216
217
218 int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
219 {
220         HLTInfo("Deinitialising DHLT Trigger Record Component");
221
222         if(fTrigRec)
223         {
224                 delete fTrigRec;
225                 fTrigRec = NULL;
226         }
227         return 0;
228 }
229
230
231 int AliHLTMUONTriggerReconstructorComponent::DoEvent(
232                 const AliHLTComponentEventData& evtData,
233                 const AliHLTComponentBlockData* blocks, 
234                 AliHLTComponentTriggerData& trigData,
235                 AliHLTUInt8_t* outputPtr, 
236                 AliHLTUInt32_t& size,
237                 std::vector<AliHLTComponentBlockData>& outputBlocks
238         )
239 {
240         // Process an event
241         unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
242
243         HLTDebug("Processing event %llu with %u input data blocks.",
244                 evtData.fEventID, evtData.fBlockCnt
245         );
246         
247         // Loop over all input blocks in the event and run the trigger DDL
248         // reconstruction algorithm on the raw data.
249         for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
250         {
251 #ifdef __DEBUG
252                 char id[kAliHLTComponentDataTypefIDsize+1];
253                 for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
254                         id[i] = blocks[n].fDataType.fID[i];
255                 id[kAliHLTComponentDataTypefIDsize] = '\0';
256                 char origin[kAliHLTComponentDataTypefOriginSize+1];
257                 for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
258                         origin[i] = blocks[n].fDataType.fOrigin[i];
259                 origin[kAliHLTComponentDataTypefOriginSize] = '\0';
260 #endif // __DEBUG
261                 HLTDebug("Handling block: %u, with fDataType.fID = '%s',"
262                           " fDataType.fID = '%s', fPtr = %p and fSize = %u bytes.",
263                         n, static_cast<char*>(id), static_cast<char*>(origin),
264                         blocks[n].fPtr, blocks[n].fSize
265                 );
266
267                 if (blocks[n].fDataType != AliHLTMUONConstants::TriggerDDLRawDataType())
268                 {
269                         // Log a message indicating that we got a data block that we
270                         // do not know how to handle.
271                         char id[kAliHLTComponentDataTypefIDsize+1];
272                         for (int i = 0; i < kAliHLTComponentDataTypefIDsize; i++)
273                                 id[i] = blocks[n].fDataType.fID[i];
274                         id[kAliHLTComponentDataTypefIDsize] = '\0';
275                         char origin[kAliHLTComponentDataTypefOriginSize+1];
276                         for (int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++)
277                                 origin[i] = blocks[n].fDataType.fOrigin[i];
278                         origin[kAliHLTComponentDataTypefOriginSize] = '\0';
279                         
280                         if (fWarnForUnexpecedBlock)
281                                 HLTWarning("Received a data block of a type we can not handle: %s origin %s",
282                                         static_cast<char*>(id), static_cast<char*>(origin)
283                                 );
284                         else
285                                 HLTDebug("Received a data block of a type we can not handle: %s origin %s",
286                                         static_cast<char*>(id), static_cast<char*>(origin)
287                                 );
288                         
289                         continue;
290                 }
291                 
292                 // Create a new output data block and initialise the header.
293                 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
294                 if (not block.InitCommonHeader())
295                 {
296                         HLTError("There is not enough space in the output buffer for the new data block.",
297                                  " We require at least %u bytes, but have %u bytes left.",
298                                 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
299                                 block.BufferSize()
300                         );
301                         break;
302                 }
303
304                 AliHLTUInt32_t totalDDLSize = blocks[n].fSize / sizeof(AliHLTUInt32_t);
305                 AliHLTUInt32_t ddlRawDataSize = totalDDLSize - fTrigRec->GetkDDLHeaderSize();
306                 AliHLTUInt32_t* buffer = reinterpret_cast<AliHLTUInt32_t*>(blocks[n].fPtr)
307                         + fTrigRec->GetkDDLHeaderSize();
308                 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
309
310                 bool runOk = fTrigRec->Run(
311                                 buffer, ddlRawDataSize,
312                                 block.GetArray(), nofTrigRec,
313                                 fSuppressPartialTrigs
314                         );
315                 if (not runOk)
316                 {
317                         HLTError("Error while processing of trigger DDL reconstruction algorithm.");
318                         size = totalSize; // Must tell the framework how much buffer space was used.
319                         return EIO;
320                 }
321                 
322                 // nofTrigRec should now contain the number of triggers actually found
323                 // and filled into the output data block, so we can set this number.
324                 assert( nofTrigRec <= block.MaxNumberOfEntries() );
325                 block.SetNumberOfEntries(nofTrigRec);
326                 
327                 HLTDebug("Number of trigger records found is %d", nofTrigRec);
328                 
329                 // Fill a block data structure for our output block.
330                 AliHLTComponentBlockData bd;
331                 FillBlockData(bd);
332                 bd.fPtr = outputPtr;
333                 // This block's start (offset) is after all other blocks written so far.
334                 bd.fOffset = totalSize;
335                 bd.fSize = block.BytesUsed();
336                 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
337                 bd.fSpecification = blocks[n].fSpecification;
338                 outputBlocks.push_back(bd);
339                 
340                 HLTDebug("Created a new output data block at fPtr = %p,"
341                           " with fOffset = %u (0x%.X) and fSize = %u bytes.", 
342                         bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
343                 );
344                 
345                 // Increase the total amount of data written so far to our output memory.
346                 totalSize += block.BytesUsed();
347         }
348         
349         // Finally we set the total size of output memory we consumed.
350         size = totalSize;
351         return 0;
352 }
353
354
355 bool AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(AliHLTMUONHitReconstructor::DHLTLut* lookupTable, const char* lutpath)
356 {
357   if (fDDL < 0 || fDDL >= 2){
358     HLTError("DDL number is out of range");
359     return false;
360   }
361   
362   int lutLine = fTrigRec->GetLutLine();
363   
364   FILE* fin = fopen(lutpath, "r");
365   if (fin == NULL){
366     HLTError("Failed to open file: %s",lutpath);
367     return false;
368   }
369   
370   for(int i=0;i<lutLine;i++){
371     fscanf(
372            fin,
373            "%d\t%d\t%d\t%f\t%f\t%f\t%d\t%d\n",
374            &lookupTable[i].fIdManuChannel,
375            &lookupTable[i].fIX,
376            &lookupTable[i].fIY,
377            &lookupTable[i].fRealX,
378            &lookupTable[i].fRealY,
379            &lookupTable[i].fRealZ,
380            &lookupTable[i].fPcbZone,
381            &lookupTable[i].fPlane
382            );
383   }
384   
385   fclose(fin);
386   return true;
387 }
388
389
390 bool AliHLTMUONTriggerReconstructorComponent::ReadRegToLocMap(AliHLTMUONTriggerReconstructor::RegToLoc* regToLocMap,const char* reglocFileName)
391 {
392   int iTrigDDL,iReg,iLoc,locId,switchWord,detElemId[4];
393   int index;
394
395   memset(regToLocMap,-1,128*sizeof(AliHLTMUONTriggerReconstructor::RegToLoc));
396
397   char s[100];
398   ifstream fin(reglocFileName);
399   
400   if(!fin){
401     HLTError("Failed to open file %s",reglocFileName);
402     return false;
403   }
404
405   while(fin.getline(s,100)){
406     sscanf(s,"%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",
407            &iTrigDDL,&iReg,&iLoc,&locId,&switchWord,&detElemId[0],&detElemId[1],&detElemId[2],&detElemId[3]);
408     if(iTrigDDL==fDDL){
409       index = iReg*16 + iLoc;
410       regToLocMap[index].fTrigDDL = iTrigDDL ; 
411       regToLocMap[index].fRegId = iReg ;
412       regToLocMap[index].fLoc = iLoc ;
413       regToLocMap[index].fLocId = locId ;  
414       regToLocMap[index].fSwitch = switchWord ;
415       for(int idet = 0; idet<4; idet++)
416         regToLocMap[index].fDetElemId[idet] = detElemId[idet] ;
417     }// if matches with fDDL
418   }//file loop
419   
420   fin.close();
421   return true;
422 }