]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/CALO/AliHLTCaloRawAnalyzerComponentv3.cxx
Adding possibility to suppress warnings from AliLog for the raw analyzer.
[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 "AliHLTCaloConstants.h"
33 #include "AliHLTCaloRcuProcessor.h"
34 #include "AliHLTCaloCrazynessDefinitions.h"
35 #include "AliHLTCaloChannelRawDataStruct.h"
36 #include "AliHLTCaloCoordinate.h"
37 #include "AliLog.h"
38
39 //#include "AliCALOBunchInfo.h"
40 //AliCALORawAnalyzer
41
42 #include <vector>
43 using namespace std;
44
45 ClassImp(AliHLTCaloRawAnalyzerComponentv3);
46
47 AliHLTCaloRawAnalyzerComponentv3::AliHLTCaloRawAnalyzerComponentv3(TString det):
48   AliHLTCaloConstantsHandler(det),
49   AliHLTCaloRcuProcessor(),
50   //  fCaloEventCount(0),
51   fAnalyzerPtr(0),
52   fMapperPtr(0),     
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   //comment
66
67   //  fMapperPtr = new AliHLTCaloMapper();
68
69   fRawReaderMemoryPtr = new AliRawReaderMemory();
70
71   fAltroRawStreamPtr = new AliAltroRawStreamV3(fRawReaderMemoryPtr);
72
73   fSanityInspectorPtr = new AliHLTCaloSanityInspector();
74   
75   if( fDoPushRawData == true  )
76   {
77     fRawDataWriter  = new RawDataWriter(fCaloConstants); 
78   }
79   
80   fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, det);  
81
82 }
83
84
85 AliHLTCaloRawAnalyzerComponentv3::~AliHLTCaloRawAnalyzerComponentv3()
86 {
87   //comment
88   DoDeinit();
89 }
90
91
92
93 int
94 AliHLTCaloRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
95
96
97   //See base class for documentation
98   //  fPrintInfo = kFALSE;
99
100   int iResult=0;
101   
102   //  fMapperPtr = new AliHLTCaloMapper();
103   
104   //InitMapping(); 
105
106   for(int i = 0; i < argc; i++)
107     {
108       if(!strcmp("-offset", argv[i]))
109         {
110           fOffset = atoi(argv[i+1]);
111         }
112       if(!strcmp("-bunchsizecut", argv[i]))
113         {
114           fBunchSizeCut = atoi(argv[i+1]);
115         }
116       if(!strcmp("-minpeakposition", argv[i]))
117         {
118           fMinPeakPosition = atoi(argv[i+1]);
119         }
120       if(!strcmp("-maxpeakposition", argv[i]))
121         {
122           fMaxPeakPosition = atoi(argv[i+1]);
123         }  
124       if(!strcmp("-pushrawdata", argv[i]))
125         {
126           fDoPushRawData = true;
127         }
128       if(!strcmp("-pushbaddata", argv[i]))
129         {
130           fDoPushBadRawData = true;
131         }
132         if(fDoPushBadRawData && fDoPushRawData) 
133         {
134            HLTWarning("fDoPushBadRawData and fDoPushRawData in conflict, using fDoPushRawData");
135            fDoPushBadRawData = false;
136         }
137         if(!strcmp("-suppressalilogwarnings", argv[i]))
138         {
139             AliLog::SetGlobalLogLevel(AliLog::kError);  //PHOS sometimes produces bad data -> Fill up the HLT logs...
140         }
141     }
142  
143   /*
144   if( fMapperPtr->GetIsInitializedMapping() == false)
145     {
146       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTCaloMapper::Could not initialise mapping from file %s, aborting", fMapperPtr->GetFilePath());
147       return -4;
148     }
149   */
150   return iResult;
151 }
152
153 int 
154 AliHLTCaloRawAnalyzerComponentv3::DoDeinit()
155 {
156   //comment
157
158   if(fAnalyzerPtr)
159     {
160       delete fAnalyzerPtr;
161       fAnalyzerPtr = 0;
162     }
163
164   if(fMapperPtr)
165     {
166       delete  fMapperPtr;
167       fMapperPtr = 0;
168     }
169
170   if(fRawReaderMemoryPtr)
171     {
172       delete fRawReaderMemoryPtr;
173       fRawReaderMemoryPtr = 0;
174     }
175
176   if(fAltroRawStreamPtr)
177     {
178       delete fAltroRawStreamPtr;
179       fAltroRawStreamPtr = 0;
180     }
181
182   return 0;
183 }
184
185
186 /*
187 const char* 
188 AliHLTCaloRawAnalyzerComponentv3::GetComponentID()
189 {
190   //comment
191   return "CaloRawAnalyzerv3";
192 }
193 */
194
195  /* 
196
197 void
198 AliHLTCaloRawAnalyzerComponentv3::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
199 {
200   //comment
201   list.clear();
202   list.push_back( AliHLTCaloDefinitions::fgkDDLPackedRawDataType | kAliHLTDataOriginPHOS);
203 }
204  */ 
205
206
207   /*
208 AliHLTComponentDataType
209 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataType()
210 {
211   //comment
212   return AliHLTCaloDefinitions::fgkChannelDataType;
213 }
214   */
215
216
217    /*
218 void
219 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
220 {
221   //comment
222   constBase = sizeof(AliHLTCaloChannelDataHeaderStruct);
223   inputMultiplier = 0.5;
224 }
225    */
226
227
228
229 Int_t
230 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
231 {
232
233   int tmpsize=  0;
234   Int_t crazyness          = 0;
235   Int_t nSamples           = 0;
236   Short_t channelCount     = 0;
237
238   AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr); 
239   AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct)); 
240  
241   totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
242   fRawReaderMemoryPtr->SetMemory(         reinterpret_cast<UChar_t*>( iter->fPtr ),  static_cast<ULong_t>( iter->fSize )  );
243   fRawReaderMemoryPtr->SetEquipmentID(    fMapperPtr->GetDDLFromSpec(  iter->fSpecification) + fCaloConstants->GetDDLOFFSET() );
244   fRawReaderMemoryPtr->Reset();
245   fRawReaderMemoryPtr->NextEvent();
246  
247   if( fDoPushRawData == true)
248     {
249      fRawDataWriter->NewEvent( );
250     }
251
252   if(fAltroRawStreamPtr->NextDDL())
253   {
254     int cnt = 0;
255     //    fOffset = ( fAltroRawStreamPtr->GetAltroCFG1() >> 10 ) & 0xf;
256     fOffset = 0;
257     while( fAltroRawStreamPtr->NextChannel()  )
258       { 
259         //       cout << __FILE__  << ":" << __LINE__ << ":" <<__FUNCTION__ << "T3"  << endl; 
260         if(  fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 ) 
261           {
262             continue; 
263           }
264         else
265           {
266             ++ cnt;
267             UShort_t* firstBunchPtr = 0;
268             int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress()); 
269             //      HLTError("Channel HW address: %d", fAltroRawStreamPtr->GetHWAddress());
270             if( fDoPushRawData == true)
271               {
272                 fRawDataWriter->SetChannelId( chId );
273               }
274
275             //      return 1;
276             vector <AliCaloBunchInfo> bvctr;
277             while( fAltroRawStreamPtr->NextBunch() == true )
278               {
279                 bvctr.push_back( AliCaloBunchInfo( fAltroRawStreamPtr->GetStartTimeBin(), fAltroRawStreamPtr->GetBunchLength(), fAltroRawStreamPtr->GetSignals() ) );   
280
281                 nSamples = fAltroRawStreamPtr->GetBunchLength();
282                 if( fDoPushRawData == true)
283                   {
284                     fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(), nSamples,  fAltroRawStreamPtr->GetEndTimeBin()  );
285                   }
286                 firstBunchPtr = const_cast< UShort_t* >(  fAltroRawStreamPtr->GetSignals()  );
287               }
288              
289             //return 1;
290           
291             totSize += sizeof( AliHLTCaloChannelDataStruct );
292             if(totSize > size)
293               {
294                 //HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
295                 return -1;
296               }
297
298             //    fAnalyzerPtr->SetData( firstBunchPtr, nSamples);
299             AliCaloFitResults res = fAnalyzerPtr->Evaluate( bvctr,  fAltroRawStreamPtr->GetAltroCFG1(), fAltroRawStreamPtr->GetAltroCFG2() );  
300  
301             HLTDebug("Channel energy: %f, max sig: %d, gain = %d, x = %d, z = %d", res.GetAmp(), res.GetMaxSig(), (chId >> 12)&0x1, chId&0x3f, (chId >> 6)&0x3f);
302             
303             //        if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
304             {
305               channelDataPtr->fChannelID =  chId;
306               channelDataPtr->fEnergy = static_cast<Float_t>( res.GetAmp()  ) - fOffset;
307
308               channelDataPtr->fTime = static_cast<Float_t>(  res.GetTof() );
309               channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
310               channelCount++;
311               channelDataPtr++; // Updating position of the free output.
312             }   
313           }
314
315         // This should be the functionality for pushing the raw data for the bad channels
316
317         /*                
318             if ( bad )
319               {
320                 crazyness |= AliHLTCaloCrazynessDefinitions::fgkBadEstBit;
321               }
322
323             channelDataPtr->fChannelID =  chId;
324             channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
325             AliHLTCaloCoordinate c;
326             fMapperPtr->ChannelId2Coordinate(chId, c); 
327             HLTDebug("Got channel, x: %d, z: %d, gain: %d, energy: %f", c.fX, c.fZ, c.fGain, channelDataPtr->fEnergy);
328             channelDataPtr->fTime = static_cast<Float_t>(fAnalyzerPtr->GetTiming());
329             channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
330             channelCount++;
331             channelDataPtr++; // Updating position of the free output.
332             
333                              // Push the raw data if we have crazyness and feature to push raw data for bad channels
334             if (crazyness && fDoPushBadRawData)
335
336               {
337                 channelDataPtr->fCrazyness = crazyness | AliHLTCaloCrazynessDefinitions::fgkRawDataBit;
338                 UShort_t extraSize = sizeof ( AliHLTCaloChannelRawDataStruct ) + ( nSamples-1 ) *sizeof ( UShort_t );
339                 totSize += extraSize;
340                 
341                 if ( totSize > size )
342                   {
343                     HLTError ( "Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size );
344                     return -1;
345                   }
346
347                 AliHLTCaloChannelRawDataStruct *tmpChannel = reinterpret_cast<AliHLTCaloChannelRawDataStruct*> ( channelDataPtr );
348                 memcpy ( & ( tmpChannel->fData ), firstBunchPtr, nSamples*sizeof ( UShort_t ) );
349                 channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*> ( reinterpret_cast<UChar_t*> ( channelDataPtr ) + extraSize );
350               }
351
352         */
353           }
354
355         if( fDoPushRawData == true)
356          { 
357            fRawDataWriter->NewChannel();
358          }
359
360   }
361
362
363 // return 1;
364
365 //Writing the header
366   channelDataHeaderPtr->fNChannels   =  channelCount;
367   channelDataHeaderPtr->fAlgorithm   = fAlgorithm;
368   channelDataHeaderPtr->fInfo        = 0;
369
370   // return 1;
371
372
373   if( fDoPushRawData == true)
374     {
375       tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
376     }
377
378   // channelDataHeaderPtr->fHasRawData  = false;
379   channelDataHeaderPtr->fHasRawData = fDoPushRawData;
380   HLTDebug("Number of channels: %d", channelCount);
381   tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct); 
382   return  tmpsize;
383
384 }
385
386
387
388
389
390 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) :  //fIsFirstChannel(true),
391                                                                     fRawDataBuffer(0),
392                                                                     fCurrentChannelSize(0),
393                                                                     //    fIsFirstChannel(true),
394                                                                     fBufferIndex(0),
395                                                                     //      fBufferSize( NZROWSRCU*NXCOLUMNSRCU*ALTROMAXSAMPLES*NGAINS +1000 ),
396                                                                     fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
397                                                                     fCurrentChannelIdPtr(0),
398                                                                     fCurrentChannelSizePtr(0),
399                                                                     fCurrentChannelDataPtr(0),
400                                                                     fTotalSize(0)
401 {
402   fRawDataBuffer = new UShort_t[fBufferSize];
403   Init();
404 }
405
406
407    
408 void  
409 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
410 {
411   fCurrentChannelIdPtr = fRawDataBuffer;
412   fCurrentChannelSizePtr = fRawDataBuffer +1;
413   fCurrentChannelDataPtr = fRawDataBuffer +2;
414   ResetBuffer();
415 }
416
417  
418 void
419 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
420 {
421   Init();
422   fTotalSize = 0;
423 }
424
425
426 void
427 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
428 {
429   *fCurrentChannelSizePtr   = fCurrentChannelSize;
430   fCurrentChannelIdPtr     += fCurrentChannelSize;
431   fCurrentChannelSizePtr    += fCurrentChannelSize;
432   fCurrentChannelDataPtr   += fCurrentChannelSize;
433   fBufferIndex = 0;
434   fCurrentChannelSize = 2;
435   fTotalSize += 2;
436 }
437
438
439 void 
440 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata,  const int length,   const UInt_t starttimebin )
441 {
442   fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
443   fCurrentChannelSize ++;
444   fBufferIndex++;
445   fCurrentChannelDataPtr[fBufferIndex] = length;
446   fCurrentChannelSize ++;
447   fBufferIndex++;
448
449   fTotalSize +=2;
450
451   for(int i=0; i < length; i++)
452     {
453       fCurrentChannelDataPtr[ fBufferIndex + i ] =  bunchdata[i];
454     }
455
456   fCurrentChannelSize += length;
457   fTotalSize += length;
458   fBufferIndex += length;
459 }
460
461
462 void
463 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid  )
464 {
465   *fCurrentChannelIdPtr =  channeldid;
466 }
467
468
469 void
470 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
471 {
472   for(int i=0; i < fBufferSize ; i++ )
473     {
474       fRawDataBuffer[i] = 0;
475     }
476 }
477
478
479 int
480 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
481 {
482   int sizerequested =  (sizeof(int)*fTotalSize + sizeused);
483
484   if(  sizerequested   > sizetotal  )
485     {
486       return 0;
487   }
488   else
489     {
490       for(int i=0; i < fTotalSize; i++)
491         {
492           memPtr[i] = fRawDataBuffer[i]; 
493         }
494       return fTotalSize;
495    }
496 }
497