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