66b365f7e77e4377a8db43815ac49ce36e0d0db6
[u/mrichter/AliRoot.git] / HLT / CALO / AliHLTCaloRawAnalyzerComponentv3.cxx
1
2 /**************************************************************************
3  * This file is property of and copyright by the ALICE HLT Project        * 
4  * All rights reserved.                                                   *
5  *                                                                        *
6  * Primary Authors: Per Thomas Hille, Oystein Djuvsland                   *
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 #include "AliHLTCaloRawAnalyzer.h"
18 #include "AliHLTCaloRawAnalyzerComponentv3.h"
19 #include "AliHLTCaloChannelDataHeaderStruct.h"
20 #include "AliHLTCaloChannelDataStruct.h"
21 #include "AliHLTCaloMapper.h"
22 #include "AliHLTCaloSanityInspector.h"
23 #include "AliAltroRawStreamV3.h"
24 #include "AliRawReaderMemory.h"
25 #include "AliAltroRawStreamV3.h"
26 #include "AliCaloRawStreamV3.h"
27 #include "AliHLTCaloConstantsHandler.h"
28 #include "AliHLTCaloConstants.h"
29
30 AliHLTCaloRawAnalyzerComponentv3::AliHLTCaloRawAnalyzerComponentv3(TString det):
31   AliHLTCaloConstantsHandler(det),
32   AliHLTProcessor(),
33   fAnalyzerPtr(0),
34   fMapperPtr(0),     
35   //  fkDoPushRawData(false),
36    fkDoPushRawData(false),
37   fPhosEventCount(0),
38   fSanityInspectorPtr(0),
39   fRawReaderMemoryPtr(0),
40   fAltroRawStreamPtr(0),
41   fAlgorithm(0),  
42   fOffset(0),
43   fBunchSizeCut(0),
44   fMinPeakPosition(0),
45   fMaxPeakPosition(100),
46   fRawDataWriter(0) 
47 {
48   //comment
49
50   //  fMapperPtr = new AliHLTCaloMapper();
51
52   fRawReaderMemoryPtr = new AliRawReaderMemory();
53
54   fAltroRawStreamPtr = new AliAltroRawStreamV3(fRawReaderMemoryPtr);
55
56   fSanityInspectorPtr = new AliHLTCaloSanityInspector();
57  
58   // if( fkDoPushRawData == true  )
59     {
60       fRawDataWriter  = new RawDataWriter(fCaloConstants); 
61     }
62
63     fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, TString("EMCAL"));  
64
65 }
66
67
68 AliHLTCaloRawAnalyzerComponentv3::~AliHLTCaloRawAnalyzerComponentv3()
69 {
70   //comment
71   Deinit();
72 }
73
74
75
76 int 
77 AliHLTCaloRawAnalyzerComponentv3::Deinit()
78 {
79   //comment
80   if(fAnalyzerPtr)
81     {
82       delete fAnalyzerPtr;
83       fAnalyzerPtr = 0;
84     }
85   if(fMapperPtr)
86     {
87       delete  fMapperPtr;
88       fMapperPtr = 0;
89     }
90   if(fRawReaderMemoryPtr)
91     {
92       delete fRawReaderMemoryPtr;
93       fRawReaderMemoryPtr = 0;
94     }
95   if(fAltroRawStreamPtr)
96     {
97       delete fAltroRawStreamPtr;
98       fAltroRawStreamPtr = 0;
99     }
100   return 0;
101 }
102
103 /*
104 const char* 
105 AliHLTCaloRawAnalyzerComponentv3::GetComponentID()
106 {
107   //comment
108   return "CaloRawAnalyzerv3";
109 }
110 */
111
112
113  /*
114 void
115 AliHLTCaloRawAnalyzerComponentv3::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
116 {
117   //comment
118   list.clear();
119   list.push_back( AliHLTCaloDefinitions::fgkDDLPackedRawDataType | kAliHLTDataOriginPHOS);
120 }
121  */
122
123 AliHLTComponentDataType 
124 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataType()
125 {
126   //comment
127   return AliHLTCaloDefinitions::fgkChannelDataType;
128 }
129
130 void
131 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
132 {
133   //comment
134   constBase = sizeof(AliHLTCaloChannelDataHeaderStruct);
135   inputMultiplier = 0.5;
136 }
137
138
139 int 
140 AliHLTCaloRawAnalyzerComponentv3::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks, AliHLTComponentTriggerData& /*trigData*/, 
141                                          AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
142 {
143
144   /*
145
146   if( fPhosEventCount%300 == 0 )
147     {
148       cout << __FILE__<<__LINE__<< " Processing event " << fPhosEventCount << endl;
149     }
150   */
151
152   //  Int_t blockSize          = 0;
153  
154   Int_t blockSize          = -1;
155   UInt_t totSize           = 0;
156   const AliHLTComponentBlockData* iter = NULL; 
157   unsigned long ndx;
158
159   for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
160     {
161       iter = blocks+ndx;
162       if( CheckInputDataType(  iter->fDataType ) == false )
163         {
164           continue;
165         }
166       else
167         {
168           InitMapping( iter->fSpecification); 
169           
170           blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
171           
172           //  blockSize = 1;
173
174           if(blockSize == -1) // If the processing returns -1 we are out of buffer and return an error msg.
175             {
176               return -ENOBUFS;
177             }
178           
179           totSize += blockSize; //Keeping track of the used size
180           AliHLTComponentBlockData bdChannelData;
181           FillBlockData( bdChannelData );
182           bdChannelData.fOffset = 0; //FIXME
183           bdChannelData.fSize = blockSize;
184           
185           //      bdChannelData.fDataType = AliHLTPHOSDefinitions::fgkChannelDataType;
186           bdChannelData.fDataType = AliHLTCaloDefinitions::fgkChannelDataType;
187
188           bdChannelData.fSpecification = iter->fSpecification;
189           outputBlocks.push_back(bdChannelData);
190           outputPtr += blockSize; //Updating position of the output buffer
191         }
192
193       fPhosEventCount++; 
194       size = totSize; //telling the framework how much buffer space we have used.
195     }
196
197   
198 return 0;
199   
200 }//end DoEvent
201
202
203
204 Int_t
205 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
206 {
207  
208   int tmpsize=  0;
209   Int_t crazyness          = 0;
210   Int_t nSamples           = 0;
211   Short_t channelCount     = 0;
212
213   AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr); 
214   AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct)); 
215
216  
217
218  
219   totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
220   fRawReaderMemoryPtr->SetMemory(         reinterpret_cast<UChar_t*>( iter->fPtr ),  static_cast<ULong_t>( iter->fSize )  );
221   fRawReaderMemoryPtr->SetEquipmentID(    fMapperPtr->GetDDLFromSpec(  iter->fSpecification) + 4608  );
222   fRawReaderMemoryPtr->Reset();
223   fRawReaderMemoryPtr->NextEvent();
224  
225   if( fkDoPushRawData == true)
226     {
227      fRawDataWriter->NewEvent( );
228     }
229
230
231   /*
232   if(fAltroRawStreamPtr != NULL)
233     {
234       delete fAltroRawStreamPtr;
235       fAltroRawStreamPtr=NULL;
236     }
237   */
238   
239   
240   //return 1;
241
242
243   //   fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, TString("EMCAL"));
244   
245
246   //  return 1;
247
248   fAltroRawStreamPtr->NextDDL();
249   {
250     int cnt = 0;
251     while( fAltroRawStreamPtr->NextChannel()  )
252       { 
253         //       cout << __FILE__  << ":" << __LINE__ << ":" <<__FUNCTION__ << "T3"  << endl; 
254         if(  fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 ) 
255           {
256             continue; 
257           }
258         else
259           {
260             ++ cnt;
261             UShort_t* firstBunchPtr = 0;
262             int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress()); 
263             if( fkDoPushRawData == true)
264               {
265                 fRawDataWriter->SetChannelId( chId );
266               }
267
268             //      return 1;
269
270             while( fAltroRawStreamPtr->NextBunch() == true )
271               {
272                 nSamples = fAltroRawStreamPtr->GetBunchLength();
273                 if( fkDoPushRawData == true)
274                   {
275                     fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(), nSamples,  fAltroRawStreamPtr->GetEndTimeBin()  );
276                   }
277                 firstBunchPtr = const_cast< UShort_t* >(  fAltroRawStreamPtr->GetSignals()  );
278               }
279              
280             //return 1;
281           
282              totSize += sizeof( AliHLTCaloChannelDataStruct );
283             if(totSize > size)
284               {
285                 HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
286                 return -1;
287               }
288
289             fAnalyzerPtr->SetData( firstBunchPtr, nSamples);
290
291             fAnalyzerPtr->Evaluate(0, nSamples);  
292             //    fAnalyzerPtr->Evaluate(0,  25);  
293             
294
295             //      return 1;
296   
297             //        if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
298             {
299               channelDataPtr->fChannelID =  chId;
300               channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
301
302               channelDataPtr->fTime = static_cast<Float_t>(fAnalyzerPtr->GetTiming());
303               channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
304               channelCount++;
305               channelDataPtr++; // Updating position of the free output.
306             }   
307           }
308
309         //      return 1;
310
311         if( fkDoPushRawData == true)
312          { 
313            fRawDataWriter->NewChannel();
314          }
315
316       }
317   }
318
319   // return 1;
320
321   //Writing the header
322   channelDataHeaderPtr->fNChannels   =  channelCount;
323   channelDataHeaderPtr->fAlgorithm   = fAlgorithm;
324   channelDataHeaderPtr->fInfo        = 0;
325
326   // return 1;
327
328
329   if( fkDoPushRawData == true)
330     {
331       tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
332     }
333
334   // channelDataHeaderPtr->fHasRawData  = false;
335   channelDataHeaderPtr->fHasRawData = fkDoPushRawData;
336   HLTDebug("Number of channels: %d", channelCount);
337   tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct); 
338   return  tmpsize;
339
340 }
341
342
343
344
345 int
346 AliHLTCaloRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
347
348
349   //See base class for documentation
350   //  fPrintInfo = kFALSE;
351
352   int iResult=0;
353   
354   //  fMapperPtr = new AliHLTCaloMapper();
355   
356   //InitMapping(); 
357
358   for(int i = 0; i < argc; i++)
359     {
360       if(!strcmp("-offset", argv[i]))
361         {
362           fOffset = atoi(argv[i+1]);
363         }
364       if(!strcmp("-bunchsizecut", argv[i]))
365         {
366           fBunchSizeCut = atoi(argv[i+1]);
367         }
368       if(!strcmp("-minpeakposition", argv[i]))
369         {
370           fMinPeakPosition = atoi(argv[i+1]);
371         }
372       if(!strcmp("-maxpeakposition", argv[i]))
373         {
374           fMaxPeakPosition = atoi(argv[i+1]);
375         }  
376     }
377  
378   /*
379   if( fMapperPtr->GetIsInitializedMapping() == false)
380     {
381       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTCaloMapper::Could not initialise mapping from file %s, aborting", fMapperPtr->GetFilePath());
382       return -4;
383     }
384   */
385
386   return iResult;
387 }
388
389
390
391
392 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) :  //fIsFirstChannel(true),
393                                                                     fRawDataBuffer(0),
394                                                                     fCurrentChannelSize(0),
395                                                                     //    fIsFirstChannel(true),
396                                                                     fBufferIndex(0),
397                                                                     //      fBufferSize( NZROWSRCU*NXCOLUMNSRCU*ALTROMAXSAMPLES*NGAINS +1000 ),
398                                                                     fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
399                                                                     fCurrentChannelIdPtr(0),
400                                                                     fCurrentChannelSizePtr(0),
401                                                                     fCurrentChannelDataPtr(0),
402                                                                     fTotalSize(0)
403 {
404   fRawDataBuffer = new UShort_t[fBufferSize];
405   Init();
406 }
407
408
409    
410 void  
411 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
412 {
413   fCurrentChannelIdPtr = fRawDataBuffer;
414   fCurrentChannelSizePtr = fRawDataBuffer +1;
415   fCurrentChannelDataPtr = fRawDataBuffer +2;
416   ResetBuffer();
417 }
418
419  
420 void
421 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
422 {
423   Init();
424   fTotalSize = 0;
425 }
426
427
428 void
429 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
430 {
431   *fCurrentChannelSizePtr   = fCurrentChannelSize;
432   fCurrentChannelIdPtr     += fCurrentChannelSize;
433   fCurrentChannelSizePtr    += fCurrentChannelSize;
434   fCurrentChannelDataPtr   += fCurrentChannelSize;
435   fBufferIndex = 0;
436   fCurrentChannelSize = 2;
437   fTotalSize += 2;
438 }
439
440
441 void 
442 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata,  const int length,   const UInt_t starttimebin )
443 {
444   fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
445   fCurrentChannelSize ++;
446   fBufferIndex++;
447   fCurrentChannelDataPtr[fBufferIndex] = length;
448   fCurrentChannelSize ++;
449   fBufferIndex++;
450
451   fTotalSize +=2;
452
453   for(int i=0; i < length; i++)
454     {
455       fCurrentChannelDataPtr[ fBufferIndex + i ] =  bunchdata[i];
456     }
457
458   fCurrentChannelSize += length;
459   fTotalSize += length;
460   fBufferIndex += length;
461 }
462
463
464 void
465 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid  )
466 {
467   *fCurrentChannelIdPtr =  channeldid;
468 }
469
470
471 void
472 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
473 {
474   for(int i=0; i < fBufferSize ; i++ )
475     {
476       fRawDataBuffer[i] = 0;
477     }
478 }
479
480
481 int
482 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
483 {
484   int sizerequested =  (sizeof(int)*fTotalSize + sizeused);
485
486   if(  sizerequested   > sizetotal  )
487     {
488       return 0;
489   }
490   else
491     {
492       for(int i=0; i < fTotalSize; i++)
493         {
494           memPtr[i] = fRawDataBuffer[i]; 
495         }
496       return fTotalSize;
497    }
498 }
499