2 /**************************************************************************
3 * This file is property of and copyright by the ALICE HLT Project *
4 * All rights reserved. *
6 * Primary Authors: Per Thomas Hille, Oystein Djuvsland *
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 **************************************************************************/
17 //#include "AliHLTCaloRawAnalyzer.h"
18 #include "AliCaloRawAnalyzer.h"
19 #include "AliCaloBunchInfo.h"
20 #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 "AliAltroRawStreamV3.h"
28 #include "AliRawReaderMemory.h"
29 #include "AliAltroRawStreamV3.h"
30 #include "AliCaloRawStreamV3.h"
31 #include "AliHLTCaloConstantsHandler.h"
32 #include "AliHLTCaloCrazynessDefinitions.h"
33 #include "AliHLTCaloChannelRawDataStruct.h"
34 #include "AliHLTCaloCoordinate.h"
40 //ClassImp(AliHLTCaloRawAnalyzerComponentv3);
43 #include "TStopwatch.h"
44 TStopwatch fgWatch; //CRAP PTH
47 AliHLTCaloRawAnalyzerComponentv3::AliHLTCaloRawAnalyzerComponentv3(TString det):AliHLTCaloProcessor(),
48 AliHLTCaloConstantsHandler(det),
53 fSanityInspectorPtr(0),
54 fRawReaderMemoryPtr(0),
55 fAltroRawStreamPtr(0),
60 fMaxPeakPosition(100),
61 fDoPushBadRawData(false),
62 fDoPushRawData(false),
67 fRawReaderMemoryPtr = new AliRawReaderMemory();
68 fAltroRawStreamPtr = new AliAltroRawStreamV3(fRawReaderMemoryPtr);
69 fSanityInspectorPtr = new AliHLTCaloSanityInspector();
71 if( fDoPushRawData == true )
73 fRawDataWriter = new RawDataWriter(fCaloConstants);
76 fAltroRawStreamPtr = new AliCaloRawStreamV3(fRawReaderMemoryPtr, det);
80 AliHLTCaloRawAnalyzerComponentv3::~AliHLTCaloRawAnalyzerComponentv3()
82 delete fRawReaderMemoryPtr;
83 delete fAltroRawStreamPtr;
84 delete fRawDataWriter;
85 delete fSanityInspectorPtr;
91 AliHLTCaloRawAnalyzerComponentv3::DoInit( int argc, const char** argv )
93 //See base class for documentation
94 // fPrintInfo = kFALSE;
96 // fMapperPtr = new AliHLTCaloMapper();
98 for(int i = 0; i < argc; i++)
100 if(!strcmp("-offset", argv[i]))
102 fOffset = atoi(argv[i+1]);
104 if(!strcmp("-bunchsizecut", argv[i]))
106 fBunchSizeCut = atoi(argv[i+1]);
108 if(!strcmp("-minpeakposition", argv[i]))
110 fMinPeakPosition = atoi(argv[i+1]);
112 if(!strcmp("-maxpeakposition", argv[i]))
114 fMaxPeakPosition = atoi(argv[i+1]);
116 if(!strcmp("-pushrawdata", argv[i]))
118 fDoPushRawData = true;
120 if(!strcmp("-pushbaddata", argv[i]))
122 fDoPushBadRawData = true;
124 if(fDoPushBadRawData && fDoPushRawData)
126 HLTWarning("fDoPushBadRawData and fDoPushRawData in conflict, using fDoPushRawData");
127 fDoPushBadRawData = false;
129 if(!strcmp("-suppressalilogwarnings", argv[i]))
131 AliLog::SetGlobalLogLevel(AliLog::kError); //PHOS sometimes produces bad data -> Fill up the HLT logs...
141 AliHLTCaloRawAnalyzerComponentv3::DoDeinit()
146 // delete fMapperPtr;
150 // if(fRawReaderMemoryPtr)
152 // delete fRawReaderMemoryPtr;
153 // fRawReaderMemoryPtr = 0;
156 if(fAltroRawStreamPtr)
158 delete fAltroRawStreamPtr;
159 fAltroRawStreamPtr = 0;
168 AliHLTCaloRawAnalyzerComponentv3::PrintDebugInfo()
170 // static int evntcnt = 0;
171 static double wlast = -1;
172 static double wcurrent = 0;
177 if( fCaloEventCount %1000 == 0 )
179 cout << __FILE__ << __LINE__ << " : Processing event " << fCaloEventCount << endl;
181 wcurrent = fgWatch.RealTime();
182 ////cout << wlast << ":" << wcurrent << endl;
183 cout << __FILE__ << __LINE__ << "The event rate is " <<
184 1000/( wcurrent - wlast ) << " Hz" << endl; fgWatch.Start(kFALSE);
185 // wlast = fgWatch.RealTime();
193 AliHLTCaloRawAnalyzerComponentv3::CheckInputDataType(const AliHLTComponentDataType &datatype)
195 vector <AliHLTComponentDataType> validTypes;
196 GetInputDataTypes(validTypes);
198 // if( true == fDebug)
200 // cout << __FILE__ << __LINE__ << " : fID = " << datatype.fID << " : fOrigin = "
201 // << datatype.fOrigin << endl;
204 for(int i=0; i < validTypes.size(); i++ )
206 if ( datatype == validTypes.at(i) )
212 HLTDebug("Invalid Datatype");
218 AliHLTCaloRawAnalyzerComponentv3::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks,
219 AliHLTComponentTriggerData& /*trigData*/,
220 AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
228 if( true == fDebug ) { PrintDebugInfo(); };
231 Int_t blockSize = -1;
233 const AliHLTComponentBlockData* iter = NULL;
238 for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
241 if( ! CheckInputDataType(iter->fDataType) )
242 // CheckInputDataType
249 // InitMapping( iter->fSpecification);
251 // blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
253 // if(blockSize == -1) // If the processing returns -1 we are out of buffer and return an error msg.
258 if(iter->fSpecification != fCurrentSpec)
260 fCurrentSpec = iter->fSpecification;
261 InitMapping(iter->fSpecification);
264 blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
267 totSize += blockSize; //Keeping track of the used size
268 AliHLTComponentBlockData bdChannelData;
269 FillBlockData( bdChannelData );
270 bdChannelData.fOffset = 0; //FIXME
271 bdChannelData.fSize = blockSize;
272 bdChannelData.fDataType = GetOutputDataType();
273 bdChannelData.fSpecification = iter->fSpecification;
274 outputBlocks.push_back(bdChannelData);
275 outputPtr += blockSize; //Updating position of the output buffer
276 // fCaloEventCount++;
277 // size = totSize; //telling the framework how much buffer space we have used.
281 size = totSize; //telling the framework how much buffer space we have used.
290 AliHLTCaloRawAnalyzerComponentv3::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
296 Short_t channelCount = 0;
298 AliHLTCaloChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTCaloChannelDataHeaderStruct*>(outputPtr);
299 AliHLTCaloChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*>(outputPtr+sizeof(AliHLTCaloChannelDataHeaderStruct));
300 totSize += sizeof( AliHLTCaloChannelDataHeaderStruct );
301 fRawReaderMemoryPtr->SetMemory( reinterpret_cast<UChar_t*>( iter->fPtr ), static_cast<ULong_t>( iter->fSize ) );
302 fRawReaderMemoryPtr->SetEquipmentID( fMapperPtr->GetDDLFromSpec( iter->fSpecification) + fCaloConstants->GetDDLOFFSET() );
303 fRawReaderMemoryPtr->Reset();
304 fRawReaderMemoryPtr->NextEvent();
306 if( fDoPushRawData == true)
308 fRawDataWriter->NewEvent( );
311 if(fAltroRawStreamPtr->NextDDL())
314 // fOffset = ( fAltroRawStreamPtr->GetAltroCFG1() >> 10 ) & 0xf;
316 while( fAltroRawStreamPtr->NextChannel() )
318 if( fAltroRawStreamPtr->GetHWAddress() < 128 || ( fAltroRawStreamPtr->GetHWAddress() ^ 0x800) < 128 )
325 UShort_t* firstBunchPtr = 0;
326 int chId = fMapperPtr->GetChannelID(iter->fSpecification, fAltroRawStreamPtr->GetHWAddress());
328 if( fDoPushRawData == true)
330 fRawDataWriter->SetChannelId( chId );
333 vector <AliCaloBunchInfo> bvctr;
334 while( fAltroRawStreamPtr->NextBunch() == true )
336 bvctr.push_back( AliCaloBunchInfo( fAltroRawStreamPtr->GetStartTimeBin(),
337 fAltroRawStreamPtr->GetBunchLength(),
338 fAltroRawStreamPtr->GetSignals() ) );
340 nSamples = fAltroRawStreamPtr->GetBunchLength();
341 if( fDoPushRawData == true)
343 fRawDataWriter->WriteBunchData( fAltroRawStreamPtr->GetSignals(),
344 nSamples, fAltroRawStreamPtr->GetEndTimeBin() );
346 firstBunchPtr = const_cast< UShort_t* >( fAltroRawStreamPtr->GetSignals() );
351 totSize += sizeof( AliHLTCaloChannelDataStruct );
354 //HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
358 // fAnalyzerPtr->SetData( firstBunchPtr, nSamples);
359 AliCaloFitResults res = fAnalyzerPtr->Evaluate( bvctr, fAltroRawStreamPtr->GetAltroCFG1(), fAltroRawStreamPtr->GetAltroCFG2() );
361 HLTDebug("Channel energy: %f, max sig: %d, gain = %d, x = %d, z = %d", res.GetAmp(), res.GetMaxSig(),
362 (chId >> 12)&0x1, chId&0x3f, (chId >> 6)&0x3f);
364 // if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
366 channelDataPtr->fChannelID = chId;
367 channelDataPtr->fEnergy = static_cast<Float_t>( res.GetAmp() ) - fOffset;
368 channelDataPtr->fTime = static_cast<Float_t>( res.GetTof() );
369 channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
371 channelDataPtr++; // Updating position of the free output.
375 // This should be the functionality for pushing the raw data for the bad channels
380 crazyness |= AliHLTCaloCrazynessDefinitions::fgkBadEstBit;
383 channelDataPtr->fChannelID = chId;
384 channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
385 AliHLTCaloCoordinate c;
386 fMapperPtr->ChannelId2Coordinate(chId, c);
387 HLTDebug("Got channel, x: %d, z: %d, gain: %d, energy: %f", c.fX, c.fZ, c.fGain, channelDataPtr->fEnergy);
388 channelDataPtr->fTime = static_cast<Float_t>(fAnalyzerPtr->GetTiming());
389 channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
391 channelDataPtr++; // Updating position of the free output.
393 // Push the raw data if we have crazyness and feature to push raw data for bad channels
394 if (crazyness && fDoPushBadRawData)
397 channelDataPtr->fCrazyness = crazyness | AliHLTCaloCrazynessDefinitions::fgkRawDataBit;
398 UShort_t extraSize = sizeof ( AliHLTCaloChannelRawDataStruct ) + ( nSamples-1 ) *sizeof ( UShort_t );
399 totSize += extraSize;
401 if ( totSize > size )
403 HLTError ( "Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size );
407 AliHLTCaloChannelRawDataStruct *tmpChannel = reinterpret_cast<AliHLTCaloChannelRawDataStruct*> ( channelDataPtr );
408 memcpy ( & ( tmpChannel->fData ), firstBunchPtr, nSamples*sizeof ( UShort_t ) );
409 channelDataPtr = reinterpret_cast<AliHLTCaloChannelDataStruct*> ( reinterpret_cast<UChar_t*> ( channelDataPtr ) + extraSize );
414 if( fDoPushRawData == true)
416 fRawDataWriter->NewChannel();
421 channelDataHeaderPtr->fNChannels = channelCount;
422 channelDataHeaderPtr->fAlgorithm = fAlgorithm;
423 channelDataHeaderPtr->fInfo = 0;
425 if( fDoPushRawData == true)
427 tmpsize += fRawDataWriter->CopyBufferToSharedMemory( (UShort_t *)channelDataPtr, size, totSize);
430 channelDataHeaderPtr->fHasRawData = fDoPushRawData;
431 HLTDebug("Number of channels: %d", channelCount);
432 tmpsize += sizeof(AliHLTCaloChannelDataStruct)*channelCount + sizeof(AliHLTCaloChannelDataHeaderStruct);
439 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::RawDataWriter(AliHLTCaloConstants* cConst) :
441 fCurrentChannelSize(0),
443 fBufferSize( 64*56*cConst->GetNGAINS()*cConst->GetALTROMAXSAMPLES() +1000 ),
444 fCurrentChannelIdPtr(0),
445 fCurrentChannelSizePtr(0),
446 fCurrentChannelDataPtr(0),
449 fRawDataBuffer = new UShort_t[fBufferSize];
454 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::~RawDataWriter()
456 delete [] fRawDataBuffer;
461 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::Init()
463 fCurrentChannelIdPtr = fRawDataBuffer;
464 fCurrentChannelSizePtr = fRawDataBuffer +1;
465 fCurrentChannelDataPtr = fRawDataBuffer +2;
471 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewEvent()
479 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::NewChannel( )
481 *fCurrentChannelSizePtr = fCurrentChannelSize;
482 fCurrentChannelIdPtr += fCurrentChannelSize;
483 fCurrentChannelSizePtr += fCurrentChannelSize;
484 fCurrentChannelDataPtr += fCurrentChannelSize;
486 fCurrentChannelSize = 2;
492 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::WriteBunchData(const UShort_t *bunchdata, const int length, const UInt_t starttimebin )
494 fCurrentChannelDataPtr[fBufferIndex] = starttimebin;
495 fCurrentChannelSize ++;
497 fCurrentChannelDataPtr[fBufferIndex] = length;
498 fCurrentChannelSize ++;
503 for(int i=0; i < length; i++)
505 fCurrentChannelDataPtr[ fBufferIndex + i ] = bunchdata[i];
508 fCurrentChannelSize += length;
509 fTotalSize += length;
510 fBufferIndex += length;
515 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::SetChannelId( const UShort_t channeldid )
517 *fCurrentChannelIdPtr = channeldid;
522 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::ResetBuffer()
524 for(int i=0; i < fBufferSize ; i++ )
526 fRawDataBuffer[i] = 0;
532 AliHLTCaloRawAnalyzerComponentv3::RawDataWriter::CopyBufferToSharedMemory(UShort_t *memPtr, const int sizetotal, const int sizeused )
534 int sizerequested = (sizeof(int)*fTotalSize + sizeused);
536 if( sizerequested > sizetotal )
542 for(int i=0; i < fTotalSize; i++)
544 memPtr[i] = fRawDataBuffer[i];