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