#include <TClonesArray.h>
#include <TTree.h>
#include "AliRunLoader.h"
#include "AliLoader.h"
#include "AliLog.h"
#include "AliVZEROdigit.h"
#include "AliVZEROCalibData.h"
#include "AliT0digit.h"
#include "AliTRDptrgParam.h"
#include "AliTRDptrgLUT.h"
#include "AliTRDptrgFEB.h"
ClassImp(AliTRDptrgFEB)
AliTRDptrgFEB::AliTRDptrgFEB(AliRunLoader *rl)
: TObject(),
fRunLoader(rl),
fParam(0),
fLUTArray(0),
fType(AliTRDptrgParam::kUndefined),
fOperatingMode(AliTRDptrgParam::kDigits),
fInputChannelCount(0),
fPosition(AliTRDptrgParam::kUnknown),
fID(0),
fThreshold(0)
{
AliError("default ctor - not recommended");
}
AliTRDptrgFEB::AliTRDptrgFEB(AliRunLoader *rl, AliTRDptrgParam::AliTRDptrgFEBType_t febType,
AliTRDptrgParam::AliTRDptrgOperatingMode_t operatingMode,
AliTRDptrgParam::AliTRDptrgFEBPosition_t position, Int_t id,
AliTRDptrgParam *param)
: TObject(),
fRunLoader(rl),
fParam(param),
fLUTArray(0),
fType(febType),
fOperatingMode(operatingMode),
fInputChannelCount(0),
fPosition(position),
fID(id),
fThreshold(0x0)
{
this->LoadParams();
}
AliTRDptrgFEB::~AliTRDptrgFEB()
{
if (this->fParam == 0x0) {
if (this->fThreshold != 0x0) {
delete[] this->fThreshold;
this->fThreshold = 0x0;
}
}
this->fLUTArray.Delete();
}
Int_t AliTRDptrgFEB::LoadDigits()
{
if (this->fType == AliTRDptrgParam::kVZERO) {
AliLoader* loader = this->fRunLoader->GetLoader( "VZEROLoader" );
if (!loader) {
AliError("Cannot get VZERO loader");
return -1;
}
loader->LoadDigits("READ");
TTree* vzeroDigitsTree = loader->TreeD();
if (!vzeroDigitsTree) {
AliError("Cannot get the VZERO digit tree");
return -1;
}
TClonesArray* vzeroDigits = NULL;
TBranch* digitBranch = vzeroDigitsTree->GetBranch("VZERODigit");
digitBranch->SetAddress(&vzeroDigits);
vzeroDigitsTree->GetEvent(0);
Int_t nDigits = vzeroDigits->GetEntriesFast();
AliDebug(5, Form("Found a whole of %d digits", nDigits));
Int_t inputVector = 0x0;
for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {
AliDebug(5, "Looping over digit");
AliVZEROdigit* digit = (AliVZEROdigit*)vzeroDigits->At(iDigit);
Int_t pmNumber = digit->PMNumber();
Int_t feeBoard = AliVZEROCalibData::GetBoardNumber(pmNumber);
Int_t board = feeBoard % 4;
Int_t channel = pmNumber % 8;
Int_t position = -1;
if ((pmNumber >= 32) && (pmNumber <= 63)) {
position = 1;
}
else if ((pmNumber >= 0) && (pmNumber <= 31)) {
position = 2;
}
AliDebug(5,
Form("pmNumber: %d; feeBoard: %d; board: %d; channel: %d; position %d",
pmNumber, feeBoard, board, channel, position));
if (position == -1) {
AliError("Wrong VZERO pmt position found");
loader->UnloadDigits();
return -1;
}
if ((position == this->fPosition) && (board == this->fID)) {
AliDebug(5, "Found an digit corresponding to the current FEB");
Float_t value = digit->ADC();
AliDebug(5, Form("ADC value: %f\n", value));
Int_t channelBitMask = 0x01;
channelBitMask <<= channel;
if (value >= this->fThreshold[channel]) {
inputVector |= channelBitMask;
AliDebug(5,
Form("Threshold exceeded in channel %d, new inputVector 0x%x",
channel, inputVector));
}
}
}
AliDebug(5, Form("inputVector: 0x%x", inputVector));
loader->UnloadDigits();
return inputVector;
}
else if (this->fType == AliTRDptrgParam::kTZERO) {
AliLoader * fT0Loader = this->fRunLoader->GetLoader("T0Loader");
if (!fT0Loader) {
AliError("Cannot get T0 loader");
return -1;
}
fT0Loader->LoadDigits("READ");
TTree* treeD = fT0Loader->TreeD();
if (!treeD) {
AliError("no digits tree");
return -1;
}
AliT0digit* digits = new AliT0digit();
TBranch *brDigits = treeD->GetBranch("T0");
if (brDigits) {
brDigits->SetAddress(&digits);
}
else {
AliError("Branch T0 DIGIT not found");
return -1;
}
brDigits->GetEntry(0);
TArrayI qtc0(24);
TArrayI qtc1(24);
digits->GetQT0(qtc0);
digits->GetQT1(qtc1);
Int_t inputVector = 0x0;
Int_t nStart = 0;
if (this->fPosition == AliTRDptrgParam::kC) {
nStart = 0;
}
else if (this->fPosition == AliTRDptrgParam::kA) {
nStart = 12;
}
Int_t channelBitMask = 0x01;
for (Int_t i = 0 + nStart; i < nStart + 12; i++) {
AliDebug(5, Form("channel: %d", i));
Int_t value = qtc1[i] - qtc0[i];
if (value > (Int_t)this->fThreshold[i - nStart]) {
inputVector |= channelBitMask;
AliDebug(5, Form("Threshold exceeded in channel %d,", i));
AliDebug(5, Form("new inputVector 0x%x", inputVector));
AliDebug(5, Form("channelBitMask 0x%x", channelBitMask));
}
channelBitMask <<= 1;
}
delete digits;
return inputVector;
}
return -1;
}
Int_t AliTRDptrgFEB::LoadAndProcessHits()
{
AliError("LoadAndProcessHits() - not yet implemented!\n");
if (this->fType == AliTRDptrgParam::kVZERO) {
return 0;
}
else if (this->fType == AliTRDptrgParam::kTZERO) {
return 0;
}
return -1;
}
Bool_t AliTRDptrgFEB::LoadParams()
{
if (this->fParam == 0x0) {
AliWarning("No paramater object specified - start loading defaults\n");
if (this->fType == AliTRDptrgParam::kVZERO) {
this->fThreshold = new UInt_t[8];
for (Int_t i = 0; i < 8; i++) {
this->fThreshold[i] = 10;
}
AliTRDptrgLUT* lut = new AliTRDptrgLUT();
this->fLUTArray.AddLast(lut);
lut = new AliTRDptrgLUT();
this->fLUTArray.AddLast(lut);
Int_t* initData = new Int_t[256];
lut = dynamic_cast<AliTRDptrgLUT*>(this->fLUTArray.At(0));
if (lut) {
for (Int_t i = 0; i < 256; i++ ) {
initData[i] = i;
}
lut->InitTable(8, 8, initData, kTRUE);
}
lut = dynamic_cast<AliTRDptrgLUT*>(this->fLUTArray.At(1));
if (lut) {
for (Int_t i = 255; i >= 0; i--) {
initData[255 - i] = i;
}
lut->InitTable(8, 8, initData, kTRUE);
}
delete [] initData;
}
else {
this->fThreshold = new UInt_t[12];
for (Int_t i = 0; i < 12; i++) {
this->fThreshold[i] = 10;
}
AliTRDptrgLUT* lut = new AliTRDptrgLUT();
this->fLUTArray.AddLast(lut);
lut = new AliTRDptrgLUT();
this->fLUTArray.AddLast(lut);
lut = dynamic_cast<AliTRDptrgLUT*>(this->fLUTArray.At(0));
Int_t* initData = new Int_t[4096];
if (lut) {
for (Int_t i = 0; i < 4096; i++ ) {
initData[i] = i;
}
lut->InitTable(12, 12, initData, kTRUE);
}
lut = dynamic_cast<AliTRDptrgLUT*>(this->fLUTArray.At(1));
if (lut) {
for (Int_t i = 4096; i > 0; i--) {
initData[4096 - i] = i;
}
lut->InitTable(12, 12, initData, kTRUE);
}
delete [] initData;
}
return false;
}
else {
if (this->fType == AliTRDptrgParam::kVZERO) {
this->fThreshold =
this->fParam->GetFEBV0Thresholds(this->fPosition, (this->fID - 1));
AliTRDptrgLUT* LUT = new AliTRDptrgLUT();
LUT->InitTable(8, 8, this->fParam->GetFEBV0LUT(this->fPosition,
(this->fID - 1),
0), kFALSE);
this->fLUTArray.AddLast(LUT);
LUT = new AliTRDptrgLUT();
LUT->InitTable(8, 8, this->fParam->GetFEBV0LUT(this->fPosition,
(this->fID - 1),
1), kFALSE);
this->fLUTArray.AddLast(LUT);
}
else {
this->fThreshold =
this->fParam->GetFEBT0Thresholds(this->fPosition);
AliTRDptrgLUT* LUT = new AliTRDptrgLUT();
LUT->InitTable(12, 12, fParam->GetFEBT0LUT(this->fPosition, 0), kFALSE);
this->fLUTArray.AddLast(LUT);
LUT = new AliTRDptrgLUT();
LUT->InitTable(12, 12, fParam->GetFEBT0LUT(this->fPosition, 1), kFALSE);
this->fLUTArray.AddLast(LUT);
}
return true;
}
return false;
}
Int_t* AliTRDptrgFEB::Simulate()
{
Int_t *result = new Int_t;
(*result) = -1;
if (this->fOperatingMode == AliTRDptrgParam::kDigits) {
Int_t inputVector = this->LoadDigits();
delete result;
Int_t nLUTs = this->fLUTArray.GetEntriesFast();
result = new Int_t[nLUTs + 1];
result[0] = nLUTs;
for (Int_t iLUT = 0; iLUT < nLUTs; iLUT++) {
AliDebug(4, Form("FEB: (pos=%d,id=%d,lut=%d,vector=0x%x)",
this->fPosition, this->fID, iLUT, inputVector));
AliTRDptrgLUT *lutTmp = dynamic_cast<AliTRDptrgLUT*>(this->fLUTArray[iLUT]);
if (lutTmp) {
result[iLUT + 1] = lutTmp->LookUp(inputVector);
}
AliDebug(4, Form("FEB result[%d] = 0x%x",(iLUT + 1),result[iLUT + 1]));
}
}
else if (this->fOperatingMode == AliTRDptrgParam::kHits) {
return result;
}
return result;
}