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