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