]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/CALO/AliHLTCaloRawAnalyzerComponentv3.cxx
- bug fixes to get the digit maker working
[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
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   fRawDataWriter(0),
62   fDoPushBadRawData(false),
63   fDoPushRawData(false)
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     }
138  
139   /*
140   if( fMapperPtr->GetIsInitializedMapping() == false)
141     {
142       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTCaloMapper::Could not initialise mapping from file %s, aborting", fMapperPtr->GetFilePath());
143       return -4;
144     }
145   */
146   return iResult;
147 }
148
149 int 
150 AliHLTCaloRawAnalyzerComponentv3::DoDeinit()
151 {
152   //comment
153
154   if(fAnalyzerPtr)
155     {
156       delete fAnalyzerPtr;
157       fAnalyzerPtr = 0;
158     }
159
160   if(fMapperPtr)
161     {
162       delete  fMapperPtr;
163       fMapperPtr = 0;
164     }
165
166   if(fRawReaderMemoryPtr)
167     {
168       delete fRawReaderMemoryPtr;
169       fRawReaderMemoryPtr = 0;
170     }
171
172   if(fAltroRawStreamPtr)
173     {
174       delete fAltroRawStreamPtr;
175       fAltroRawStreamPtr = 0;
176     }
177
178   return 0;
179 }
180
181
182 /*
183 const char* 
184 AliHLTCaloRawAnalyzerComponentv3::GetComponentID()
185 {
186   //comment
187   return "CaloRawAnalyzerv3";
188 }
189 */
190
191  /* 
192
193 void
194 AliHLTCaloRawAnalyzerComponentv3::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
195 {
196   //comment
197   list.clear();
198   list.push_back( AliHLTCaloDefinitions::fgkDDLPackedRawDataType | kAliHLTDataOriginPHOS);
199 }
200  */ 
201
202
203   /*
204 AliHLTComponentDataType
205 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataType()
206 {
207   //comment
208   return AliHLTCaloDefinitions::fgkChannelDataType;
209 }
210   */
211
212
213    /*
214 void
215 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
216 {
217   //comment
218   constBase = sizeof(AliHLTCaloChannelDataHeaderStruct);
219   inputMultiplier = 0.5;
220 }
221    */
222
223
224
225 Int_t
226 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
227 {
228
229   int tmpsize=  0;
230   Int_t crazyness          = 0;
231   Int_t nSamples           = 0;
232   Short_t channelCount     = 0;
233
234   AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr); 
235   AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct)); 
236  
237   totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
238   fRawReaderMemoryPtr->SetMemory(         reinterpret_cast<UChar_t*>( iter->fPtr ),  static_cast<ULong_t>( iter->fSize )  );
239   fRawReaderMemoryPtr->SetEquipmentID(    fMapperPtr->GetDDLFromSpec(  iter->fSpecification) + 1792  );
240   fRawReaderMemoryPtr->Reset();
241   fRawReaderMemoryPtr->NextEvent();
242  
243   if( fDoPushRawData == true)
244     {
245      fRawDataWriter->NewEvent( );
246     }
247
248   if(fAltroRawStreamPtr->NextDDL())
249   {
250     int cnt = 0;
251     //    fOffset = ( fAltroRawStreamPtr->GetAltroCFG1() >> 10 ) & 0xf;
252     fOffset = 0;
253     while( fAltroRawStreamPtr->NextChannel()  )
254       { 
255         //       cout << __FILE__  << ":" << __LINE__ << ":" <<__FUNCTION__ << "T3"  << endl; 
256         if(  fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 ) 
257           {
258             continue; 
259           }
260         else
261           {
262             ++ cnt;
263             UShort_t* firstBunchPtr = 0;
264             int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress()); 
265             //      HLTError("Channel HW address: %d", fAltroRawStreamPtr->GetHWAddress());
266             if( fDoPushRawData == true)
267               {
268                 fRawDataWriter->SetChannelId( chId );
269               }
270
271             //      return 1;
272             vector <AliCaloBunchInfo> bvctr;
273             while( fAltroRawStreamPtr->NextBunch() == true )
274               {
275                 bvctr.push_back( AliCaloBunchInfo( fAltroRawStreamPtr->GetStartTimeBin(), fAltroRawStreamPtr->GetBunchLength(), fAltroRawStreamPtr->GetSignals() ) );   
276
277                 nSamples = fAltroRawStreamPtr->GetBunchLength();
278                 if( fDoPushRawData == true)
279                   {
280                     fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(), nSamples,  fAltroRawStreamPtr->GetEndTimeBin()  );
281                   }
282                 firstBunchPtr = const_cast< UShort_t* >(  fAltroRawStreamPtr->GetSignals()  );
283               }
284              
285             //return 1;
286           
287             totSize += sizeof( AliHLTCaloChannelDataStruct );
288             if(totSize > size)
289               {
290                 //HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
291                 return -1;
292               }
293
294             //    fAnalyzerPtr->SetData( firstBunchPtr, nSamples);
295             AliCaloFitResults res = fAnalyzerPtr->Evaluate( bvctr,  fAltroRawStreamPtr->GetAltroCFG1(), fAltroRawStreamPtr->GetAltroCFG2() );  
296             
297             
298             //        if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
299             {
300               channelDataPtr->fChannelID =  chId;
301               channelDataPtr->fEnergy = static_cast<Float_t>( res.GetAmp()  ) - fOffset;
302
303               channelDataPtr->fTime = static_cast<Float_t>(  res.GetTof() );
304               channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
305               channelCount++;
306               channelDataPtr++; // Updating position of the free output.
307             }   
308           }
309
310         // This should be the functionality for pushing the raw data for the bad channels
311
312         /*                
313             if ( bad )
314               {
315                 crazyness |= AliHLTCaloCrazynessDefinitions::fgkBadEstBit;
316               }
317
318             channelDataPtr->fChannelID =  chId;
319             channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
320             AliHLTCaloCoordinate c;
321             fMapperPtr->ChannelId2Coordinate(chId, c); 
322             //      HLTError("Got channel, x: %d, z: %d, gain: %d, energy: %f", c.fX, c.fZ, c.fGain, channelDataPtr->fEnergy);
323             channelDataPtr->fTime = static_cast<Float_t>(fAnalyzerPtr->GetTiming());
324             channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
325             channelCount++;
326             channelDataPtr++; // Updating position of the free output.
327             
328                              // Push the raw data if we have crazyness and feature to push raw data for bad channels
329             if (crazyness && fDoPushBadRawData)
330
331               {
332                 channelDataPtr->fCrazyness = crazyness | AliHLTCaloCrazynessDefinitions::fgkRawDataBit;
333                 UShort_t extraSize = sizeof ( AliHLTCaloChannelRawDataStruct ) + ( nSamples-1 ) *sizeof ( UShort_t );
334                 totSize += extraSize;
335                 
336                 if ( totSize > size )
337                   {
338                     HLTError ( "Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size );
339                     return -1;
340                   }
341
342                 AliHLTCaloChannelRawDataStruct *tmpChannel = reinterpret_cast<AliHLTCaloChannelRawDataStruct*> ( channelDataPtr );
343                 memcpy ( & ( tmpChannel->fData ), firstBunchPtr, nSamples*sizeof ( UShort_t ) );
344                 channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*> ( reinterpret_cast<UChar_t*> ( channelDataPtr ) + extraSize );
345               }
346
347         */
348           }
349
350         if( fDoPushRawData == true)
351          { 
352            fRawDataWriter->NewChannel();
353          }
354
355       }
356   }
357
358   // return 1;
359
360   //Writing the header
361   channelDataHeaderPtr->fNChannels   =  channelCount;
362   channelDataHeaderPtr->fAlgorithm   = fAlgorithm;
363   channelDataHeaderPtr->fInfo        = 0;
364
365   // return 1;
366
367
368   if( fDoPushRawData == true)
369     {
370       tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
371     }
372
373   // channelDataHeaderPtr->fHasRawData  = false;
374   channelDataHeaderPtr->fHasRawData = fDoPushRawData;
375   HLTError("Number of channels: %d", channelCount);
376   tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct); 
377   return  tmpsize;
378
379 }
380
381
382
383
384
385 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) :  //fIsFirstChannel(true),
386                                                                     fRawDataBuffer(0),
387                                                                     fCurrentChannelSize(0),
388                                                                     //    fIsFirstChannel(true),
389                                                                     fBufferIndex(0),
390                                                                     //      fBufferSize( NZROWSRCU*NXCOLUMNSRCU*ALTROMAXSAMPLES*NGAINS +1000 ),
391                                                                     fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
392                                                                     fCurrentChannelIdPtr(0),
393                                                                     fCurrentChannelSizePtr(0),
394                                                                     fCurrentChannelDataPtr(0),
395                                                                     fTotalSize(0)
396 {
397   fRawDataBuffer = new UShort_t[fBufferSize];
398   Init();
399 }
400
401
402    
403 void  
404 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
405 {
406   fCurrentChannelIdPtr = fRawDataBuffer;
407   fCurrentChannelSizePtr = fRawDataBuffer +1;
408   fCurrentChannelDataPtr = fRawDataBuffer +2;
409   ResetBuffer();
410 }
411
412  
413 void
414 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
415 {
416   Init();
417   fTotalSize = 0;
418 }
419
420
421 void
422 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
423 {
424   *fCurrentChannelSizePtr   = fCurrentChannelSize;
425   fCurrentChannelIdPtr     += fCurrentChannelSize;
426   fCurrentChannelSizePtr    += fCurrentChannelSize;
427   fCurrentChannelDataPtr   += fCurrentChannelSize;
428   fBufferIndex = 0;
429   fCurrentChannelSize = 2;
430   fTotalSize += 2;
431 }
432
433
434 void 
435 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata,  const int length,   const UInt_t starttimebin )
436 {
437   fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
438   fCurrentChannelSize ++;
439   fBufferIndex++;
440   fCurrentChannelDataPtr[fBufferIndex] = length;
441   fCurrentChannelSize ++;
442   fBufferIndex++;
443
444   fTotalSize +=2;
445
446   for(int i=0; i < length; i++)
447     {
448       fCurrentChannelDataPtr[ fBufferIndex + i ] =  bunchdata[i];
449     }
450
451   fCurrentChannelSize += length;
452   fTotalSize += length;
453   fBufferIndex += length;
454 }
455
456
457 void
458 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid  )
459 {
460   *fCurrentChannelIdPtr =  channeldid;
461 }
462
463
464 void
465 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
466 {
467   for(int i=0; i < fBufferSize ; i++ )
468     {
469       fRawDataBuffer[i] = 0;
470     }
471 }
472
473
474 int
475 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
476 {
477   int sizerequested =  (sizeof(int)*fTotalSize + sizeused);
478
479   if(  sizerequested   > sizetotal  )
480     {
481       return 0;
482   }
483   else
484     {
485       for(int i=0; i < fTotalSize; i++)
486         {
487           memPtr[i] = fRawDataBuffer[i]; 
488         }
489       return fTotalSize;
490    }
491 }
492