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