]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/CALO/AliHLTCaloRawAnalyzerComponentv3.cxx
Bug fix: AliHLTTPCRawSpacePointContainer
[u/mrichter/AliRoot.git] / HLT / CALO / AliHLTCaloRawAnalyzerComponentv3.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * All rights reserved.                                                   *
6  *                                                                        *
7  * Primary Authors: Per Thomas Hille, Oystein Djuvsland                   *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          * 
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18
19 #include "AliCaloRawAnalyzer.h"
20 #include "AliCaloBunchInfo.h"
21 #include "AliCaloFitResults.h"
22 #include "AliHLTCaloRawAnalyzerComponentv3.h"
23 #include "AliHLTCaloChannelDataHeaderStruct.h"
24 #include "AliHLTCaloChannelDataStruct.h"
25 #include "AliHLTCaloMapper.h"
26 #include "AliHLTCaloSanityInspector.h"
27 #include "AliRawReaderMemory.h"
28 #include "AliCaloRawStreamV3.h"
29 #include "AliHLTCaloConstantsHandler.h"
30 #include "AliHLTCaloChannelRawDataStruct.h"
31 #include "AliLog.h"
32 #include "TStopwatch.h"
33
34 #include "AliCaloRawAnalyzerFactory.h"
35
36 //#include "AliCaloConstants.h"
37
38 //#include "AliCaloRawAnalyzer.h"
39
40 //using namespace Algo;
41
42 #include <vector>
43 using namespace std;
44
45 ClassImp(AliHLTCaloRawAnalyzerComponentv3);
46
47
48
49 AliHLTCaloRawAnalyzerComponentv3::AliHLTCaloRawAnalyzerComponentv3(TString det, fitAlgorithm algo  ):AliHLTCaloProcessor(),
50                                                                                                      AliHLTCaloConstantsHandler(det),
51                                                                                                      fAnalyzerPtr(0),
52                                                                                                      fMapperPtr(0),     
53                                                                                                      fCurrentSpec(-1),
54                                                                                                      fDebug(false),
55                                                                                                      fSanityInspectorPtr(0),
56                                                                                                      fRawReaderMemoryPtr(0),
57                                                                                                      fAltroRawStreamPtr(0),
58                                                                                                      fDetector(det),
59                                                                                                      fAlgorithm(algo),  
60                                                                                                      fOffset(0),
61                                                                                                      fBunchSizeCut(0),
62                                                                                                      fMinPeakPosition(0),
63                                                                                                      fMaxPeakPosition(100),
64                                                                                                      fDoPushBadRawData(false),
65                                                                                                      fDoPushRawData(false),
66                                                                                                      fRawDataWriter(0)
67                                                                         
68 {
69   //Constructor
70   
71 }
72
73
74 AliHLTCaloRawAnalyzerComponentv3::~AliHLTCaloRawAnalyzerComponentv3()
75 {
76   //destructor
77 }
78
79
80 int
81 AliHLTCaloRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
82
83   //See base class for documentation
84   int iResult=0;
85   
86   for(int i = 0; i < argc; i++)
87     {
88       if(!strcmp("-offset", argv[i]))
89         {
90           fOffset = atoi(argv[i+1]);
91         }
92       if(!strcmp("-bunchsizecut", argv[i]))
93         {
94           fBunchSizeCut = atoi(argv[i+1]);
95         }
96       if(!strcmp("-minpeakposition", argv[i]))
97         {
98           fMinPeakPosition = atoi(argv[i+1]);
99         }
100       if(!strcmp("-maxpeakposition", argv[i]))
101         {
102           fMaxPeakPosition = atoi(argv[i+1]);
103         }  
104       if(!strcmp("-pushrawdata", argv[i]))
105         {
106           fDoPushRawData = true;
107         }
108       if(!strcmp("-pushbaddata", argv[i]))
109         {
110           fDoPushBadRawData = true;
111         }
112         if(fDoPushBadRawData && fDoPushRawData) 
113         {
114            HLTWarning("fDoPushBadRawData and fDoPushRawData in conflict, using fDoPushRawData");
115            fDoPushBadRawData = false;
116         }
117         if(!strcmp("-suppressalilogwarnings", argv[i]))
118         {
119             AliLog::SetGlobalLogLevel(AliLog::kError);  //PHOS sometimes produces bad data -> Fill up the HLT logs...
120         }
121     }
122
123   fAnalyzerPtr = AliCaloRawAnalyzerFactory::CreateAnalyzer(fAlgorithm);
124   fAnalyzerPtr->SetIsZeroSuppressed(true);
125   fSanityInspectorPtr = new AliHLTCaloSanityInspector();
126   
127   if( fDoPushRawData == true  )
128     {
129       fRawDataWriter  = new RawDataWriter(fCaloConstants); 
130     }
131   fRawReaderMemoryPtr = new AliRawReaderMemory();
132   fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, fDetector);  
133  
134   return iResult;
135 }
136
137
138 int 
139 AliHLTCaloRawAnalyzerComponentv3::DoDeinit()
140 {
141   //comment
142   if(fAltroRawStreamPtr)
143     {
144       delete fAltroRawStreamPtr;
145       fAltroRawStreamPtr = 0;
146     }
147
148   if (fRawReaderMemoryPtr) delete fRawReaderMemoryPtr;
149   fRawReaderMemoryPtr=NULL;
150   if (fAltroRawStreamPtr) delete fAltroRawStreamPtr;
151   fAltroRawStreamPtr=NULL;
152   if (fRawDataWriter) delete fRawDataWriter;
153   fRawDataWriter=NULL;
154   if (fSanityInspectorPtr) delete fSanityInspectorPtr;
155   fSanityInspectorPtr=NULL;
156
157   return 0;
158 }
159
160
161
162 void 
163 AliHLTCaloRawAnalyzerComponentv3::PrintDebugInfo()
164 {
165   //comment
166   static TStopwatch  watch; //CRAP PTH
167   static double wlast = -1;
168   static double wcurrent = 0;
169   
170   if( true == fDebug )
171     {
172       if( fCaloEventCount %1000 == 0  )
173         {
174           cout << __FILE__ << __LINE__ << " : Processing event "  << fCaloEventCount << endl; 
175           wlast =  wcurrent;
176           wcurrent =  watch.RealTime();
177           cout << __FILE__ << __LINE__ << "The event rate is " <<  
178             1000/( wcurrent  -  wlast ) << "  Hz" << endl;        watch.Start(kFALSE); 
179         }
180     }
181 }
182
183
184 void
185 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
186 {
187   //comment
188   constBase = sizeof(AliHLTCaloChannelDataHeaderStruct);
189   inputMultiplier = 1.5;
190 }
191
192
193 bool 
194 AliHLTCaloRawAnalyzerComponentv3::CheckInputDataType(const AliHLTComponentDataType &datatype)
195 {
196   //comment
197   vector <AliHLTComponentDataType> validTypes;
198   GetInputDataTypes(validTypes);
199   
200   for(UInt_t i=0; i < validTypes.size(); i++ )
201     {
202       if ( datatype  ==  validTypes.at(i) )
203         {
204           return true;
205         }
206     }
207  
208   HLTDebug("Invalid Datatype");
209   return false;
210 }
211
212
213 int 
214 AliHLTCaloRawAnalyzerComponentv3::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks, 
215                                           AliHLTComponentTriggerData& /*trigData*/, 
216                                           AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
217 {
218   //comment
219   if(!IsDataEvent())
220    {
221      size = 0;
222      return 0;
223    }
224   if( true == fDebug ) 
225     { PrintDebugInfo(); 
226     };
227   
228   Int_t blockSize          = -1;
229   UInt_t totSize           = 0;
230   const AliHLTComponentBlockData* iter = NULL; 
231   unsigned long ndx;
232   
233   for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
234     {
235       iter = blocks+ndx;
236       if(  ! CheckInputDataType(iter->fDataType) )
237         {
238           continue;
239         }
240
241       if(iter->fSpecification != fCurrentSpec)
242         {
243           fCurrentSpec = iter->fSpecification;
244           InitMapping(iter->fSpecification);
245         }
246       
247       blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
248       totSize += blockSize; //Keeping track of the used size
249       AliHLTComponentBlockData bdChannelData;
250       FillBlockData( bdChannelData );
251       bdChannelData.fOffset = 0; //FIXME
252       bdChannelData.fSize = blockSize;
253       bdChannelData.fDataType = GetOutputDataType();
254       bdChannelData.fSpecification = iter->fSpecification;
255       outputBlocks.push_back(bdChannelData);
256       outputPtr += blockSize; //Updating position of the output buffer
257     }
258
259   fCaloEventCount++; 
260   size = totSize; //telling the framework how much buffer space we have used.
261    
262   return 0;
263    
264 }//end DoEvent
265
266
267
268 Int_t
269 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
270 {
271   //comment
272   int tmpsize=  0;
273   Int_t crazyness          = 0;
274   Int_t nSamples           = 0;
275   Short_t channelCount     = 0;
276
277   AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr); 
278   AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct)); 
279   totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
280   fRawReaderMemoryPtr->SetMemory(         reinterpret_cast<UChar_t*>( iter->fPtr ),  static_cast<ULong_t>( iter->fSize )  );
281   fRawReaderMemoryPtr->SetEquipmentID(    fMapperPtr->GetDDLFromSpec(  iter->fSpecification) + fCaloConstants->GetDDLOFFSET() );
282   fRawReaderMemoryPtr->Reset();
283   fRawReaderMemoryPtr->NextEvent();
284
285   if( fDoPushRawData == true)
286     {
287       fRawDataWriter->NewEvent( );
288     }
289   
290   if(fAltroRawStreamPtr->NextDDL())
291     {
292       int cnt = 0;
293       fOffset = 0;
294       while( fAltroRawStreamPtr->NextChannel()  )
295         { 
296           if(  fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 ) 
297             {
298               continue; 
299             }
300           else
301             {
302               ++ cnt;
303               //UShort_t* firstBunchPtr = 0;
304               int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress()); 
305                                 //patch to skip LG in EMC
306                                 if(fDetector.CompareTo("EMCAL") == 0 && (int)((chId >> 12)&0x1) == 0) continue;
307               if( fDoPushRawData == true)
308                 {
309                   fRawDataWriter->SetChannelId( chId );
310                 }
311             
312               vector <AliCaloBunchInfo> bvctr;
313               while( fAltroRawStreamPtr->NextBunch() == true )
314                 {
315                   bvctr.push_back( AliCaloBunchInfo( fAltroRawStreamPtr->GetStartTimeBin(), 
316                                                      fAltroRawStreamPtr->GetBunchLength(),
317                                                      fAltroRawStreamPtr->GetSignals() ) );      
318
319                   nSamples = fAltroRawStreamPtr->GetBunchLength();
320                   if( fDoPushRawData == true)
321                     {
322                       fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(), 
323                                                       nSamples,  fAltroRawStreamPtr->GetEndTimeBin()  );
324                     }
325                   //firstBunchPtr = const_cast< UShort_t* >(  fAltroRawStreamPtr->GetSignals()  );
326                 }
327             
328               totSize += sizeof( AliHLTCaloChannelDataStruct );
329               if(totSize > size)
330                 {
331                   HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
332                   return -1;
333                 }
334
335               AliCaloFitResults res = fAnalyzerPtr->Evaluate( bvctr,  fAltroRawStreamPtr->GetAltroCFG1(), 
336                                                               fAltroRawStreamPtr->GetAltroCFG2() );  
337  
338               HLTDebug("Channel energy: %f, max sig: %d, gain = %d, x = %d, z = %d", res.GetAmp(), res.GetMaxSig(), 
339                        (chId >> 12)&0x1, chId&0x3f, (chId >> 6)&0x3f);
340               {
341                 channelDataPtr->fChannelID =  chId;
342                 channelDataPtr->fEnergy = static_cast<Float_t>( res.GetAmp()  ) - fOffset;
343                 channelDataPtr->fTime = static_cast<Float_t>(  res.GetTof() );
344                 if(fDetector.CompareTo("EMCAL") == 0) channelDataPtr->fTime = static_cast<Float_t>(  res.GetTof() )*100E-9 - fAltroRawStreamPtr->GetL1Phase();
345                 channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
346                 channelCount++;
347                 channelDataPtr++; // Updating position of the free output.
348               }   
349             }
350         }
351       if( fDoPushRawData == true)
352         { 
353           fRawDataWriter->NewChannel();
354         }
355     }
356
357   
358   channelDataHeaderPtr->fNChannels   =  channelCount;
359   channelDataHeaderPtr->fAlgorithm   = fAlgorithm;
360   channelDataHeaderPtr->fInfo        = 0;
361   
362   if( fDoPushRawData == true)
363     {
364       tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
365     }
366
367   channelDataHeaderPtr->fHasRawData = fDoPushRawData;
368   HLTDebug("Number of channels: %d", channelCount);
369   tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct); 
370   return  tmpsize;
371
372 }
373
374
375
376 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) : 
377   fRawDataBuffer(0),
378   fCurrentChannelSize(0),
379   fBufferIndex(0),
380   fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
381   fCurrentChannelIdPtr(0),
382   fCurrentChannelSizePtr(0),
383   fCurrentChannelDataPtr(0),
384   fTotalSize(0)
385 {
386   //comment
387   fRawDataBuffer = new UShort_t[fBufferSize];
388   Init();
389 }
390
391
392 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::~RawDataWriter()
393 {
394   delete [] fRawDataBuffer;
395 }
396
397    
398 void  
399 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
400 {
401   //comment
402   fCurrentChannelIdPtr = fRawDataBuffer;
403   fCurrentChannelSizePtr = fRawDataBuffer +1;
404   fCurrentChannelDataPtr = fRawDataBuffer +2;
405   ResetBuffer();
406 }
407
408  
409 void
410 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
411 {
412   //comment
413   Init();
414   fTotalSize = 0;
415 }
416
417
418 void
419 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
420 {
421   //comment
422   *fCurrentChannelSizePtr   = fCurrentChannelSize;
423   fCurrentChannelIdPtr     += fCurrentChannelSize;
424   fCurrentChannelSizePtr    += fCurrentChannelSize;
425   fCurrentChannelDataPtr   += fCurrentChannelSize;
426   fBufferIndex = 0;
427   fCurrentChannelSize = 2;
428   fTotalSize += 2;
429 }
430
431
432 void 
433 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata,  const int length,   const UInt_t starttimebin )
434 {
435   fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
436   fCurrentChannelSize ++;
437   fBufferIndex++;
438   fCurrentChannelDataPtr[fBufferIndex] = length;
439   fCurrentChannelSize ++;
440   fBufferIndex++;
441
442   fTotalSize +=2;
443
444   for(int i=0; i < length; i++)
445     {
446       fCurrentChannelDataPtr[ fBufferIndex + i ] =  bunchdata[i];
447     }
448
449   fCurrentChannelSize += length;
450   fTotalSize += length;
451   fBufferIndex += length;
452 }
453
454
455 void
456 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid  )
457 {
458   *fCurrentChannelIdPtr =  channeldid;
459 }
460
461
462 void
463 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
464 {
465   for(int i=0; i < fBufferSize ; i++ )
466     {
467       fRawDataBuffer[i] = 0;
468     }
469 }
470
471
472 int
473 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
474 {
475   int sizerequested =  (sizeof(int)*fTotalSize + sizeused);
476
477   if(  sizerequested   > sizetotal  )
478     {
479       return 0;
480     }
481   else
482     {
483       for(int i=0; i < fTotalSize; i++)
484         {
485           memPtr[i] = fRawDataBuffer[i]; 
486         }
487       return fTotalSize;
488     }
489 }
490