3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * All rights reserved. *
7 * Primary Authors: Per Thomas Hille, Oystein Djuvsland *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
19 #include "AliCaloRawAnalyzer.h"
20 #include "AliCaloBunchInfo.h"
21 #include "AliCaloFitResults.h"
22 #include "AliHLTCaloRawAnalyzerComponentv3.h"
23 #include "AliHLTCaloChannelDataHeaderStruct.h"
24 #include "AliHLTCaloChannelDataStruct.h"
25 #include "AliHLTCaloMapper.h"
26 #include "AliHLTCaloSanityInspector.h"
27 #include "AliRawReaderMemory.h"
28 #include "AliCaloRawStreamV3.h"
29 #include "AliHLTCaloConstantsHandler.h"
30 #include "AliHLTCaloChannelRawDataStruct.h"
32 #include "TStopwatch.h"
34 #include "AliCaloRawAnalyzerFactory.h"
36 //#include "AliCaloConstants.h"
38 //#include "AliCaloRawAnalyzer.h"
40 //using namespace Algo;
45 ClassImp(AliHLTCaloRawAnalyzerComponentv3);
49 AliHLTCaloRawAnalyzerComponentv3::AliHLTCaloRawAnalyzerComponentv3(TString det, fitAlgorithm algo ):AliHLTCaloProcessor(),
50 AliHLTCaloConstantsHandler(det),
55 fSanityInspectorPtr(0),
56 fRawReaderMemoryPtr(0),
57 fAltroRawStreamPtr(0),
63 fMaxPeakPosition(100),
64 fDoPushBadRawData(false),
65 fDoPushRawData(false),
74 AliHLTCaloRawAnalyzerComponentv3::~AliHLTCaloRawAnalyzerComponentv3()
81 AliHLTCaloRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
83 //See base class for documentation
86 for(int i = 0; i < argc; i++)
88 if(!strcmp("-offset", argv[i]))
90 fOffset = atoi(argv[i+1]);
92 if(!strcmp("-bunchsizecut", argv[i]))
94 fBunchSizeCut = atoi(argv[i+1]);
96 if(!strcmp("-minpeakposition", argv[i]))
98 fMinPeakPosition = atoi(argv[i+1]);
100 if(!strcmp("-maxpeakposition", argv[i]))
102 fMaxPeakPosition = atoi(argv[i+1]);
104 if(!strcmp("-pushrawdata", argv[i]))
106 fDoPushRawData = true;
108 if(!strcmp("-pushbaddata", argv[i]))
110 fDoPushBadRawData = true;
112 if(fDoPushBadRawData && fDoPushRawData)
114 HLTWarning("fDoPushBadRawData and fDoPushRawData in conflict, using fDoPushRawData");
115 fDoPushBadRawData = false;
117 if(!strcmp("-suppressalilogwarnings", argv[i]))
119 AliLog::SetGlobalLogLevel(AliLog::kError); //PHOS sometimes produces bad data -> Fill up the HLT logs...
123 fAnalyzerPtr = AliCaloRawAnalyzerFactory::CreateAnalyzer(fAlgorithm);
124 fAnalyzerPtr->SetIsZeroSuppressed(true);
125 fSanityInspectorPtr = new AliHLTCaloSanityInspector();
127 if( fDoPushRawData == true )
129 fRawDataWriter = new RawDataWriter(fCaloConstants);
131 fRawReaderMemoryPtr = new AliRawReaderMemory();
132 fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, fDetector);
139 AliHLTCaloRawAnalyzerComponentv3::DoDeinit()
142 if(fAltroRawStreamPtr)
144 delete fAltroRawStreamPtr;
145 fAltroRawStreamPtr = 0;
148 if (fRawReaderMemoryPtr) delete fRawReaderMemoryPtr;
149 fRawReaderMemoryPtr=NULL;
150 if (fAltroRawStreamPtr) delete fAltroRawStreamPtr;
151 fAltroRawStreamPtr=NULL;
152 if (fRawDataWriter) delete fRawDataWriter;
154 if (fSanityInspectorPtr) delete fSanityInspectorPtr;
155 fSanityInspectorPtr=NULL;
163 AliHLTCaloRawAnalyzerComponentv3::PrintDebugInfo()
166 static TStopwatch watch; //CRAP PTH
167 static double wlast = -1;
168 static double wcurrent = 0;
172 if( fCaloEventCount %1000 == 0 )
174 cout << __FILE__ << __LINE__ << " : Processing event " << fCaloEventCount << endl;
176 wcurrent = watch.RealTime();
177 cout << __FILE__ << __LINE__ << "The event rate is " <<
178 1000/( wcurrent - wlast ) << " Hz" << endl; watch.Start(kFALSE);
185 AliHLTCaloRawAnalyzerComponentv3::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
188 constBase = sizeof(AliHLTCaloChannelDataHeaderStruct);
189 inputMultiplier = 1.5;
194 AliHLTCaloRawAnalyzerComponentv3::CheckInputDataType(const AliHLTComponentDataType &datatype)
197 vector <AliHLTComponentDataType> validTypes;
198 GetInputDataTypes(validTypes);
200 for(UInt_t i=0; i < validTypes.size(); i++ )
202 if ( datatype == validTypes.at(i) )
208 HLTDebug("Invalid Datatype");
214 AliHLTCaloRawAnalyzerComponentv3::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks,
215 AliHLTComponentTriggerData& /*trigData*/,
216 AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
228 Int_t blockSize = -1;
230 const AliHLTComponentBlockData* iter = NULL;
233 for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
236 if( ! CheckInputDataType(iter->fDataType) )
241 if(iter->fSpecification != fCurrentSpec)
243 fCurrentSpec = iter->fSpecification;
244 InitMapping(iter->fSpecification);
247 blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
248 totSize += blockSize; //Keeping track of the used size
249 AliHLTComponentBlockData bdChannelData;
250 FillBlockData( bdChannelData );
251 bdChannelData.fOffset = 0; //FIXME
252 bdChannelData.fSize = blockSize;
253 bdChannelData.fDataType = GetOutputDataType();
254 bdChannelData.fSpecification = iter->fSpecification;
255 outputBlocks.push_back(bdChannelData);
256 outputPtr += blockSize; //Updating position of the output buffer
260 size = totSize; //telling the framework how much buffer space we have used.
269 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
275 Short_t channelCount = 0;
277 AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr);
278 AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct));
279 totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
280 fRawReaderMemoryPtr->SetMemory( reinterpret_cast<UChar_t*>( iter->fPtr ), static_cast<ULong_t>( iter->fSize ) );
281 fRawReaderMemoryPtr->SetEquipmentID( fMapperPtr->GetDDLFromSpec( iter->fSpecification) + fCaloConstants->GetDDLOFFSET() );
282 fRawReaderMemoryPtr->Reset();
283 fRawReaderMemoryPtr->NextEvent();
285 if( fDoPushRawData == true)
287 fRawDataWriter->NewEvent( );
290 if(fAltroRawStreamPtr->NextDDL())
294 while( fAltroRawStreamPtr->NextChannel() )
296 if( fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 )
303 //UShort_t* firstBunchPtr = 0;
304 int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress());
305 //patch to skip LG in EMC
306 if(fDetector.CompareTo("EMCAL") == 0 && (int)((chId >> 12)&0x1) == 0) continue;
307 if( fDoPushRawData == true)
309 fRawDataWriter->SetChannelId( chId );
312 vector <AliCaloBunchInfo> bvctr;
313 while( fAltroRawStreamPtr->NextBunch() == true )
315 bvctr.push_back( AliCaloBunchInfo( fAltroRawStreamPtr->GetStartTimeBin(),
316 fAltroRawStreamPtr->GetBunchLength(),
317 fAltroRawStreamPtr->GetSignals() ) );
319 nSamples = fAltroRawStreamPtr->GetBunchLength();
320 if( fDoPushRawData == true)
322 fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(),
323 nSamples, fAltroRawStreamPtr->GetEndTimeBin() );
325 //firstBunchPtr = const_cast< UShort_t* >( fAltroRawStreamPtr->GetSignals() );
328 totSize += sizeof( AliHLTCaloChannelDataStruct );
331 HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
335 AliCaloFitResults res = fAnalyzerPtr->Evaluate( bvctr, fAltroRawStreamPtr->GetAltroCFG1(),
336 fAltroRawStreamPtr->GetAltroCFG2() );
338 HLTDebug("Channel energy: %f, max sig: %d, gain = %d, x = %d, z = %d", res.GetAmp(), res.GetMaxSig(),
339 (chId >> 12)&0x1, chId&0x3f, (chId >> 6)&0x3f);
341 channelDataPtr->fChannelID = chId;
342 channelDataPtr->fEnergy = static_cast<Float_t>( res.GetAmp() ) - fOffset;
343 channelDataPtr->fTime = static_cast<Float_t>( res.GetTof() );
344 if(fDetector.CompareTo("EMCAL") == 0) channelDataPtr->fTime = static_cast<Float_t>( res.GetTof() )*100E-9 - fAltroRawStreamPtr->GetL1Phase();
345 channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
347 channelDataPtr++; // Updating position of the free output.
351 if( fDoPushRawData == true)
353 fRawDataWriter->NewChannel();
358 channelDataHeaderPtr->fNChannels = channelCount;
359 channelDataHeaderPtr->fAlgorithm = fAlgorithm;
360 channelDataHeaderPtr->fInfo = 0;
362 if( fDoPushRawData == true)
364 tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
367 channelDataHeaderPtr->fHasRawData = fDoPushRawData;
368 HLTDebug("Number of channels: %d", channelCount);
369 tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct);
376 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) :
378 fCurrentChannelSize(0),
380 fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
381 fCurrentChannelIdPtr(0),
382 fCurrentChannelSizePtr(0),
383 fCurrentChannelDataPtr(0),
387 fRawDataBuffer = new UShort_t[fBufferSize];
392 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::~RawDataWriter()
394 delete [] fRawDataBuffer;
399 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
402 fCurrentChannelIdPtr = fRawDataBuffer;
403 fCurrentChannelSizePtr = fRawDataBuffer +1;
404 fCurrentChannelDataPtr = fRawDataBuffer +2;
410 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
419 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
422 *fCurrentChannelSizePtr = fCurrentChannelSize;
423 fCurrentChannelIdPtr += fCurrentChannelSize;
424 fCurrentChannelSizePtr += fCurrentChannelSize;
425 fCurrentChannelDataPtr += fCurrentChannelSize;
427 fCurrentChannelSize = 2;
433 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata, const int length, const UInt_t starttimebin )
435 fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
436 fCurrentChannelSize ++;
438 fCurrentChannelDataPtr[fBufferIndex] = length;
439 fCurrentChannelSize ++;
444 for(int i=0; i < length; i++)
446 fCurrentChannelDataPtr[ fBufferIndex + i ] = bunchdata[i];
449 fCurrentChannelSize += length;
450 fTotalSize += length;
451 fBufferIndex += length;
456 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid )
458 *fCurrentChannelIdPtr = channeldid;
463 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
465 for(int i=0; i < fBufferSize ; i++ )
467 fRawDataBuffer[i] = 0;
473 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
475 int sizerequested = (sizeof(int)*fTotalSize + sizeused);
477 if( sizerequested > sizetotal )
483 for(int i=0; i < fTotalSize; i++)
485 memPtr[i] = fRawDataBuffer[i];