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