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