#include "AliRun.h"
#include "AliRunLoader.h"
#include "AliDigitizationInput.h"
#include "AliLoader.h"
#include "AliLog.h"
#include "AliMFTDigitizer.h"
#include "AliMFTDigit.h"
#include "AliMFT.h"
#include "AliMFTSegmentation.h"
#include "TObjArray.h"
#include "TClonesArray.h"
#include "AliDigitizer.h"
ClassImp(AliMFTDigitizer)
AliMFTDigitizer::AliMFTDigitizer():
AliDigitizer(),
fNPlanes(0),
fSegmentation(0)
{
}
AliMFTDigitizer::AliMFTDigitizer(AliDigitizationInput *digInp):
AliDigitizer(digInp),
fNPlanes(0),
fSegmentation(0)
{
}
void AliMFTDigitizer::Digitize(Option_t*) {
AliDebug(1, "************************************************************************");
AliDebug(1, "************************ AliMFTDigitizer::Digitize *********************");
AliDebug(1, "************************************************************************");
if (!fSegmentation) {
fSegmentation = new AliMFTSegmentation("AliMFTGeometry.root");
fNPlanes = fSegmentation -> GetNPlanes();
}
AliDebug(1, Form("nPlanes = %d",fNPlanes));
AliDebug(1,Form("Start with %i input(s) for event %i", fDigInput->GetNinputs(), fDigInput->GetOutputEventNr()));
AliRunLoader *pInRunLoader=0;
AliLoader *pInMFTLoader=0;
TClonesArray sDigits[fNMaxPlanes];
for (Int_t iPlane=0; iPlane<fNMaxPlanes; iPlane++) sDigits[iPlane].SetClass("AliMFTDigit");
for (Int_t iFile=0; iFile<fDigInput->GetNinputs(); iFile++) {
pInRunLoader = AliRunLoader::GetRunLoader(fDigInput->GetInputFolderName(iFile));
pInMFTLoader = pInRunLoader->GetLoader("MFTLoader");
if (!pInMFTLoader) {
AliDebug(1,"no MFT lodader, checking in the other input \n");
continue;
}
if (!pInRunLoader->GetAliRun()) pInRunLoader->LoadgAlice();
AliMFT *pInMFT = (AliMFT*) pInRunLoader->GetAliRun()->GetDetector("MFT");
AliDebug(1, "pInMFTLoader->LoadSDigits()...");
pInMFTLoader->LoadSDigits();
AliDebug(1, "... done!");
AliDebug(1, " pInMFTLoader->TreeS()->GetEntry(0);");
pInMFTLoader->TreeS()->GetEntry(0);
AliDebug(1, "... done!");
for (Int_t iPlane=0; iPlane<pInMFT->GetSDigitsList()->GetEntries(); iPlane++) {
for(Int_t iSDig=0; iSDig<((TClonesArray*)pInMFT->GetSDigitsList()->At(iPlane))->GetEntries(); iSDig++) {
AliDebug(2, Form("Reading digit %03d of plane %02d (A)", iSDig, iPlane));
AliMFTDigit *pSDig = (AliMFTDigit*) ((TClonesArray*)pInMFT->GetSDigitsList()->At(iPlane))->At(iSDig);
AliDebug(2, Form("Reading digit %03d of plane %02d (B)", iSDig, iPlane));
pSDig->AddOffset2TrackID(fDigInput->GetMask(iFile));
AliDebug(2, Form("Reading digit %03d of plane %02d (C)", iSDig, iPlane));
new ((sDigits[iPlane])[sDigits[iPlane].GetEntries()]) AliMFTDigit(*pSDig);
AliDebug(2, Form("Reading digit %03d of plane %02d (D)", iSDig, iPlane));
}
}
pInMFTLoader->UnloadSDigits();
pInMFT->ResetSDigits();
}
AliRunLoader *pOutRunLoader = AliRunLoader::GetRunLoader(fDigInput->GetOutputFolderName());
AliLoader *pOutMFTLoader = pOutRunLoader->GetLoader("MFTLoader");
AliRun *pAliRun = pOutRunLoader->GetAliRun();
AliMFT *pOutMFT = (AliMFT*) pAliRun->GetDetector("MFT");
pOutMFTLoader->MakeTree("D");
pOutMFT->MakeBranch("D");
pOutMFT->SetTreeAddress();
SDigits2Digits(sDigits, pOutMFT->GetDigitsList());
pOutMFTLoader->TreeD()->Fill();
pOutMFTLoader->WriteDigits("OVERWRITE");
for (Int_t iPlane=0; iPlane<fNPlanes; iPlane++) sDigits[iPlane].Clear();
pOutMFTLoader->UnloadDigits();
pOutMFT->ResetDigits();
}
void AliMFTDigitizer::SDigits2Digits(TClonesArray *pSDigitList, TObjArray *pDigitList) {
TClonesArray *myDigitList[fNMaxPlanes] = {0};
for (Int_t iPlane=0; iPlane<fNPlanes; iPlane++) {
myDigitList[iPlane] = (TClonesArray*)(*pDigitList)[iPlane];
if (myDigitList[iPlane]->GetEntries()!=0) AliErrorClass("Some of digits lists is not empty");
}
AliDebug(1,"starting loop over planes");
for (Int_t iPlane=0; iPlane<fNPlanes; iPlane++) {
AliMFTDigit *newDig=NULL;
AliMFTDigit *oldDig=NULL;
pSDigitList[iPlane].Sort();
Int_t nDetElem = fSegmentation->GetPlane(iPlane)->GetNActiveElements();
TClonesArray *digitsPerDetElem[fNMaxDetElemPerPlane] = {0};
for (Int_t iDetElem=0; iDetElem<nDetElem; iDetElem++) digitsPerDetElem[iDetElem] = new TClonesArray("AliMFTDigit");
AliDebug(1,"starting loop over sdigits to create digits");
AliDebug(1, Form("MFT plane #%02d has %d SDigits", iPlane, Int_t(pSDigitList[iPlane].GetEntries())));
for (Int_t iSDig=0; iSDig<pSDigitList[iPlane].GetEntries(); iSDig++) {
newDig = (AliMFTDigit*) (pSDigitList[iPlane].At(iSDig));
Int_t localDetElemID = fSegmentation->GetDetElemLocalID(newDig->GetDetElemID());
Bool_t digitExists = kFALSE;
Int_t nDigits = digitsPerDetElem[localDetElemID]->GetEntries();
for (Int_t iDig=0; iDig<nDigits; iDig++) {
oldDig = (AliMFTDigit*) (digitsPerDetElem[localDetElemID]->At(iDig));
if (newDig->GetDetElemID()==oldDig->GetDetElemID() &&
newDig->GetPixelX()==oldDig->GetPixelX() &&
newDig->GetPixelY()==oldDig->GetPixelY() &&
newDig->GetPixelZ()==oldDig->GetPixelZ()) {
digitExists = kTRUE;
MergeDigits(oldDig, newDig);
break;
}
}
if (!digitExists) new ((*digitsPerDetElem[localDetElemID])[digitsPerDetElem[localDetElemID]->GetEntries()]) AliMFTDigit(*newDig);
}
for (Int_t iDetElem=0; iDetElem<nDetElem; iDetElem++) {
for (Int_t iSDig=0; iSDig<digitsPerDetElem[iDetElem]->GetEntries(); iSDig++) {
newDig = (AliMFTDigit*) (digitsPerDetElem[iDetElem]->At(iSDig));
new ((*myDigitList[iPlane])[myDigitList[iPlane]->GetEntries()]) AliMFTDigit(*newDig);
}
}
for (Int_t iDetElem=0; iDetElem<nDetElem; iDetElem++) delete digitsPerDetElem[iDetElem];
AliDebug(1, Form("MFT plane #%02d has %d Digits", iPlane, Int_t(myDigitList[iPlane]->GetEntries())));
AliDebug(1, "ending loop over sdigits to create digits");
}
AliDebug(1,"ending loop over layers");
}
void AliMFTDigitizer::MergeDigits(AliMFTDigit *mainDig, AliMFTDigit *digToSum) {
mainDig -> SetEloss(mainDig->GetEloss() + digToSum->GetEloss());
Bool_t trackExists = kFALSE;
for (Int_t iTrack=0; iTrack<mainDig->GetNMCTracks(); iTrack++) {
if (digToSum->GetMCLabel(0) == mainDig->GetMCLabel(iTrack)) {
trackExists = kTRUE;
break;
}
}
if (!trackExists) mainDig->AddMCLabel(digToSum->GetMCLabel(0));
}