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