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