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