]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSRawAnalyzerComponentv3.cxx
- starting implementing ocdb access
[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
45   fRawDataWriter(0)
46 {
47   //comment
48   fMapperPtr = new AliHLTPHOSMapper();
49
50   fRawReaderMemoryPtr = new AliRawReaderMemory();
51
52   fAltroRawStreamPtr = new AliAltroRawStreamV3(fRawReaderMemoryPtr);
53
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
179
180 Int_t
181 AliHLTPHOSRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
182 {
183   //comment
184   int tmpsize=  0;
185   Int_t crazyness          = 0;
186   Int_t nSamples           = 0;
187   Short_t channelCount     = 0;
188
189   // Firs we want to write a header to the output
190   AliHLTPHOSChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTPHOSChannelDataHeaderStruct*>(outputPtr); 
191
192   // Then comes the channel data
193   AliHLTPHOSChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTPHOSChannelDataStruct*>(outputPtr+sizeof(AliHLTPHOSChannelDataHeaderStruct)); 
194   //Adding to the total size of data written
195   totSize += sizeof( AliHLTPHOSChannelDataHeaderStruct );
196
197   fRawReaderMemoryPtr->SetMemory(         reinterpret_cast<UChar_t*>( iter->fPtr ),  static_cast<ULong_t>( iter->fSize )  );
198   fRawReaderMemoryPtr->SetEquipmentID(    fMapperPtr->GetDDLFromSpec(  iter->fSpecification) + 1792  );
199   fRawReaderMemoryPtr->Reset();
200   fRawReaderMemoryPtr->NextEvent();
201   
202   if( fDoPushRawData == true)
203     {
204       fRawDataWriter->NewEvent( );
205     }
206   if(fAltroRawStreamPtr != NULL)
207     {
208       delete fAltroRawStreamPtr;
209       fAltroRawStreamPtr=NULL;
210     }
211   
212   fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, TString("PHOS"));
213
214   if(fAltroRawStreamPtr->NextDDL())
215     {
216       int cnt = 0;
217       while( fAltroRawStreamPtr->NextChannel()  )
218         { 
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->GetStartTimeBin()  );
240                       fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(), nSamples,  fAltroRawStreamPtr->GetEndTimeBin()  );
241                     }
242                   firstBunchPtr = const_cast< UShort_t* >(  fAltroRawStreamPtr->GetSignals()  );
243                 }
244               if(firstBunchPtr)
245                 {             
246                   totSize += sizeof( AliHLTPHOSChannelDataStruct );
247                   if(totSize > size)
248                     {
249                       HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
250                       return -1;
251                     }
252
253                   fAnalyzerPtr->SetData( firstBunchPtr, nSamples);
254                   fAnalyzerPtr->Evaluate(0, nSamples);  
255            
256                   //          if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
257                   {
258                     channelDataPtr->fChannelID =  chId;
259                     channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
260                 
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             }
269           if(fDoPushRawData)
270             {
271               fRawDataWriter->NewChannel();
272             }
273         }
274     }
275   
276   //Writing the header
277   channelDataHeaderPtr->fNChannels   =  channelCount;
278   channelDataHeaderPtr->fAlgorithm   = fAlgorithm;
279   channelDataHeaderPtr->fInfo        = 0;
280
281   if( fDoPushRawData == true)
282     {
283       tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
284     }
285
286   // channelDataHeaderPtr->fHasRawData  = false;
287   channelDataHeaderPtr->fHasRawData = fDoPushRawData;
288
289   HLTDebug("Number of channels: %d", channelCount);
290   //returning the size used
291   // delete fAltroRawStreamPtr;
292   tmpsize += sizeof(AliHLTPHOSChannelDataStruct)*channelCount + sizeof(AliHLTPHOSChannelDataHeaderStruct); 
293
294   //  return sizeof(AliHLTPHOSChannelDataStruct)*channelCount + sizeof(AliHLTPHOSChannelDataHeaderStruct);
295   return  tmpsize;
296
297 }
298  
299
300 int 
301 AliHLTPHOSRawAnalyzerComponentv3::WriteRawData(AliHLTPHOSChannelDataStruct*) //dtaPtr)
302 {
303   return 0;
304 }
305
306 int
307 AliHLTPHOSRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
308
309
310   //See base class for documentation
311   // fPrintInfo = kFALSE;
312   int iResult=0;
313   fMapperPtr = new AliHLTPHOSMapper();
314
315   for(int i = 0; i < argc; i++)
316     {
317       if(!strcmp("-offset", argv[i]))
318         {
319           fOffset = atoi(argv[i+1]);
320         }
321       if(!strcmp("-bunchsizecut", argv[i]))
322         {
323           fBunchSizeCut = atoi(argv[i+1]);
324         }
325       if(!strcmp("-minpeakposition", argv[i]))
326         {
327           fMinPeakPosition = atoi(argv[i+1]);
328         }
329       if(!strcmp("-maxpeakposition", argv[i]))
330         {
331           fMaxPeakPosition = atoi(argv[i+1]);
332         }  
333       if(!strcmp("-pushrawdata", argv[i]))
334         {
335           fDoPushRawData = true;
336         }
337     }
338  
339   if(fMapperPtr->GetIsInitializedMapping() == false)
340     {
341       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTPHOSMapper::Could not initialise mapping from file %s, aborting", fMapperPtr->GetFilePath());
342       return -4;
343     }
344
345   return iResult;
346 }
347
348
349
350
351 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::RawDataWriter() :  //fIsFirstChannel(true),
352                                                                     fRawDataBuffer(0),
353                                                                     fCurrentChannelSize(0),
354                                                                     //    fIsFirstChannel(true),
355                                                                     fBufferIndex(0),
356                                                                     fBufferSize( NZROWSRCU*NXCOLUMNSRCU*ALTROMAXSAMPLES*NGAINS +1000 ),
357                                                                     fCurrentChannelIdPtr(0),
358                                                                     fCurrentChannelSizePtr(0),
359                                                                     fCurrentChannelDataPtr(0),
360                                                                     fTotalSize(0)
361 {
362   fRawDataBuffer = new UShort_t[fBufferSize];
363   Init();
364 }
365
366
367    
368 void  
369 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::Init()
370 {
371   fCurrentChannelIdPtr = fRawDataBuffer;
372   fCurrentChannelSizePtr = fRawDataBuffer +1;
373   fCurrentChannelDataPtr = fRawDataBuffer +2;
374   ResetBuffer();
375 }
376
377  
378 void
379 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::NewEvent()
380 {
381   Init();
382   fTotalSize = 0;
383 }
384
385
386 void
387 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
388 {
389   *fCurrentChannelSizePtr   = fCurrentChannelSize;
390   fCurrentChannelIdPtr     += fCurrentChannelSize;
391   fCurrentChannelSizePtr    += fCurrentChannelSize;
392   fCurrentChannelDataPtr   += fCurrentChannelSize;
393   fBufferIndex = 0;
394   fCurrentChannelSize = 2;
395   fTotalSize += 2;
396 }
397
398
399 void 
400 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata,  const int length,   const UInt_t starttimebin )
401 {
402   fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
403   fCurrentChannelSize ++;
404   fBufferIndex++;
405   fCurrentChannelDataPtr[fBufferIndex] = length;
406   fCurrentChannelSize ++;
407   fBufferIndex++;
408
409   fTotalSize +=2;
410
411   for(int i=0; i < length; i++)
412     {
413       fCurrentChannelDataPtr[ fBufferIndex + i ] =  bunchdata[i];
414     }
415
416   fCurrentChannelSize += length;
417   fTotalSize += length;
418   fBufferIndex += length;
419 }
420
421
422 void
423 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid  )
424 {
425   *fCurrentChannelIdPtr =  channeldid;
426 }
427
428
429 void
430 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
431 {
432   for(int i=0; i < fBufferSize ; i++ )
433     {
434       fRawDataBuffer[i] = 0;
435     }
436 }
437
438
439 int
440 AliHLTPHOSRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
441 {
442   int sizerequested =  (sizeof(int)*fTotalSize + sizeused);
443
444   if(  sizerequested   > sizetotal  )
445     {
446       return 0;
447   }
448   else
449     {
450       for(int i=0; i < fTotalSize; i++)
451         {
452           memPtr[i] = fRawDataBuffer[i]; 
453         }
454       return fTotalSize;
455    }
456 }
457