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