#include <TTree.h>
#include <TBranch.h>
#include <TObjArray.h>
#include <TRefArray.h>
#include <TClonesArray.h>
#include <TMath.h>
#include <TLorentzVector.h>
#include "AliRunLoader.h"
#include "AliEMCAL.h"
#include "AliEMCALLoader.h"
#include "AliESDVertex.h"
#include "AliEMCALHit.h"
#include "AliEMCALDigit.h"
#include "AliEMCALRecPoint.h"
#include "AliESDCaloCells.h"
#include "AliESDCaloCluster.h"
#include "AliEveEMCALData.h"
#include "AliEveEMCALSModuleData.h"
class Riostream;
class TObject;
class TEveUtil;
class TEvePointSet;
class AliRun;
class AliESDEvent;
class AliEMCAL;
class AliEMCALGeometry;
class AliEveEMCALSModule;
using std::cout;
using std::endl;
ClassImp(AliEveEMCALData)
AliEveEMCALData::AliEveEMCALData():
TObject(),
TEveRefCnt(),
fEmcal(0x0),
fGeom(0x0),
fNode(0x0),
fHMatrix(0),
fTree(0x0),
fESD(0x0),
fNsm(12),
fNsmfull(10),
fNsmhalf(2),
fSM(12),
fSMfull(10),
fSMhalf(2),
fRunLoader(0),
fDebug(0),
fPoint(0)
{
CreateAllSModules();
}
AliEveEMCALData::AliEveEMCALData(AliRunLoader* rl, TGeoNode* node, TGeoHMatrix* m):
TObject(),
TEveRefCnt(),
fEmcal(0x0),
fGeom(0x0),
fNode(node),
fHMatrix(m),
fTree(0x0),
fESD(0x0),
fNsm(12),
fNsmfull(10),
fNsmhalf(2),
fSM(12),
fSMfull(10),
fSMhalf(2),
fRunLoader(rl),
fDebug(0),
fPoint(0)
{
InitEMCALGeom(rl);
CreateAllSModules();
}
AliEveEMCALData::~AliEveEMCALData()
{
DeleteSuperModules();
delete fTree;
delete fGeom;
delete fNode;
delete fHMatrix;
delete fPoint;
}
AliEveEMCALData::AliEveEMCALData(const AliEveEMCALData &edata) :
TObject(edata),
TEveRefCnt(edata),
fEmcal(edata.fEmcal),
fGeom(edata.fGeom),
fNode(edata.fNode),
fHMatrix(edata.fHMatrix),
fTree(edata.fTree),
fESD(edata.fESD),
fNsm(edata.fNsm),
fNsmfull(edata.fNsmfull),
fNsmhalf(edata.fNsmhalf),
fSM(edata.fSM),
fSMfull(edata.fSMfull),
fSMhalf(edata.fSMhalf),
fRunLoader(edata.fRunLoader),
fDebug(edata.fDebug),
fPoint(edata.fPoint)
{
InitEMCALGeom(edata.fRunLoader);
CreateAllSModules();
}
AliEveEMCALData& AliEveEMCALData::operator=(const AliEveEMCALData &edata)
{
if (this != &edata) {
}
return *this;
}
void AliEveEMCALData::SetTree(TTree* const tree)
{
fTree = tree;
}
void AliEveEMCALData::SetESD(AliESDEvent* const esd)
{
fESD = esd;
}
void AliEveEMCALData::SetNode(TGeoNode* const node)
{
fNode = node;
}
void AliEveEMCALData::InitEMCALGeom(AliRunLoader* const rl)
{
fEmcal = (AliEMCAL*) rl->GetAliRun()->GetDetector("EMCAL");
fGeom = (AliEMCALGeometry*) fEmcal->GetGeometry();
}
void AliEveEMCALData::GetGeomInfo(Int_t id, Int_t &iSupMod, Double_t& x, Double_t& y, Double_t& z)
{
Int_t iTower = 0 ;
Int_t iIphi = 0 ;
Int_t iIeta = 0 ;
fGeom->GetCellIndex(id,iSupMod,iTower,iIphi,iIeta);
fGeom->RelPosCellInSModule(id, x, y, z);
}
void AliEveEMCALData::CreateAllSModules()
{
for(Int_t sm = 0; sm < fNsm; sm++)
CreateSModule(sm);
}
void AliEveEMCALData::CreateSModule(Int_t sm)
{
if(fSM[sm] == 0) fSM[sm] = new AliEveEMCALSModuleData(sm,fGeom,fNode,fHMatrix);
if(fSMfull[sm] == 0 && sm < 10) fSMfull[sm] = new AliEveEMCALSModuleData(sm,fGeom,fNode,fHMatrix);
if(fSMhalf[sm-10] == 0 && sm > 10) fSMhalf[sm-10] = new AliEveEMCALSModuleData(sm,fGeom,fNode,fHMatrix);
}
void AliEveEMCALData::DropAllSModules()
{
for (Int_t sm = 0; sm < fNsm; sm++) {
if (fSM[sm] != 0)
fSM[sm]->DropData();
}
}
void AliEveEMCALData::DeleteSuperModules()
{
for (Int_t sm = 0; sm < fNsm; sm++)
{
fSM[sm] = 0;
delete fSM[sm];
}
for(Int_t smf = 0; smf < fNsmfull; smf++)
{
fSMfull[smf] = 0;
delete fSMfull[smf];
}
for(Int_t smh = 0; smh < fNsmhalf; smh++)
{
fSMhalf[smh] = 0;
delete fSMhalf[smh];
}
}
void AliEveEMCALData::LoadHits(TTree* const t)
{
TObjArray *harr=NULL;
TBranch *hbranch=t->GetBranch("EMCAL");
hbranch->SetAddress(&harr);
if(hbranch->GetEvent(0)) {
for(Int_t ih = 0; ih < harr->GetEntriesFast(); ih++) {
AliEMCALHit* hit =(AliEMCALHit*)harr->UncheckedAt(ih);
if(hit != 0){
if(fDebug>1) cout << "Hit info " << hit->GetId() << " " << hit->GetEnergy() << endl;
Int_t id = hit->GetId();
Double_t xl = 0.; Double_t yl = 0.; Double_t zl = 0.;
Double_t x = hit->X();
Double_t y = hit->Y();
Double_t z = hit->Z();
Double_t amp = hit->GetEnergy();
Int_t iSupMod = 0;
GetGeomInfo(id,iSupMod,xl,yl,zl);
fSM[iSupMod]->RegisterHit(id,iSupMod,amp,x,y,z);
}
}
}
}
void AliEveEMCALData::LoadHitsFromEMCALLoader(AliEMCALLoader* const emcl)
{
AliEMCALHit* hit;
TClonesArray *hits = 0;
TTree *treeH = emcl->TreeH();
if (treeH) {
Int_t nTrack = treeH->GetEntries();
TBranch * branchH = treeH->GetBranch("EMCAL");
branchH->SetAddress(&hits);
for (Int_t iTrack = 0; iTrack < nTrack; iTrack++) {
branchH->GetEntry(iTrack);
for(Int_t ihit = 0; ihit< hits->GetEntries();ihit++){
hit = static_cast<AliEMCALHit *>(hits->At(ihit)) ;
if(hit != 0){
if(fDebug>1) cout << "Hit info " << hit->GetId() << " " << hit->GetEnergy() << endl;
Int_t id = hit->GetId();
Double_t xl = 0.; Double_t yl = 0.; Double_t zl = 0.;
Double_t x = hit->X();
Double_t y = hit->Y();
Double_t z = hit->Z();
Double_t amp = hit->GetEnergy();
Int_t iSupMod = 0;
GetGeomInfo(id,iSupMod,xl,yl,zl);
fSM[iSupMod]->RegisterHit(id,iSupMod,amp,x,y,z);
}
}
hits->Clear();
}
}
}
void AliEveEMCALData::LoadDigits(TTree *t)
{
TClonesArray *digits = 0;
t->SetBranchAddress("EMCAL", &digits);
t->GetEntry(0);
Int_t nEnt = digits->GetEntriesFast();
cout << "nEnt: " << nEnt << endl;
AliEMCALDigit * dig;
Double_t ampFlo = -1 ;
Int_t id = -1 ;
Int_t iSupMod = 0 ;
Double_t x, y, z;
for (Int_t idig = 0; idig < nEnt; idig++)
{
dig = static_cast<AliEMCALDigit *>(digits->At(idig));
if(dig != 0) {
id = dig->GetId() ;
ampFlo = dig->GetAmplitude();
GetGeomInfo(id,iSupMod,x,y,z);
fSM[iSupMod]->RegisterDigit(id,iSupMod,ampFlo,x,y,z);
}
else {
cout << "Digit object empty" << endl;
return;
}
}
cout << "after loop on digits !" << endl;
}
void AliEveEMCALData::LoadDigitsFromEMCALLoader(AliEMCALLoader* const emcl)
{
AliEMCALDigit* dig;
TClonesArray *digits = (TClonesArray*)emcl->Digits();
Double_t ampFlo = -1 ;
Int_t id = -1 ;
Int_t iSupMod = 0 ;
Double_t x, y, z;
for(Int_t idig = 0; idig< digits->GetEntries();idig++){
dig = static_cast<AliEMCALDigit *>(digits->At(idig)) ;
if(dig != 0){
if(fDebug>1) cout << "Digit info " << dig->GetId() << " " << dig->GetAmplitude() << endl;
id = dig->GetId() ;
ampFlo = dig->GetAmplitude();
GetGeomInfo(id,iSupMod,x,y,z);
fSM[iSupMod]->RegisterDigit(id,iSupMod,ampFlo,x,y,z);
}
else {
cout << "Digit object empty" << endl;
return;
}
}
}
void AliEveEMCALData::LoadDigitsFromESD()
{
AliESDCaloCells &cells= *(fESD->GetEMCALCells());
Int_t ncell = cells.GetNumberOfCells() ;
Int_t iSupMod = 0 ;
Double_t x, y, z;
for (Int_t icell= 0; icell < ncell; icell++)
{
Int_t id = cells.GetCellNumber(icell);
Double_t ampFlo = cells.GetAmplitude(icell);
GetGeomInfo(id,iSupMod,x,y,z);
fSM[iSupMod]->RegisterDigit(id,iSupMod,ampFlo,x,y,z);
if(iSupMod<fNsmfull) fSMfull[iSupMod]->RegisterDigit(id,iSupMod,ampFlo,x,y,z);
if(iSupMod>fNsmfull) fSMhalf[iSupMod-10]->RegisterDigit(id,iSupMod,ampFlo,x,y,z);
}
}
void AliEveEMCALData::LoadRecPoints(TTree* const t)
{
TObjArray *carr=NULL;
TBranch *cbranch=t->GetBranch("EMCALECARP");
cbranch->SetAddress(&carr);
if(cbranch->GetEvent(0)) {
for(Int_t ic = 0; ic < carr->GetEntriesFast(); ic++) {
AliEMCALRecPoint* rp =(AliEMCALRecPoint*)carr->UncheckedAt(ic);
if(rp){
if(fDebug>1) cout << "RecPoint info " << rp->GetAbsId() << " " << rp->GetEnergy() << endl;
Int_t iSupMod = rp->GetSuperModuleNumber();
Double_t amp = (Double_t)rp->GetEnergy();
Double_t ampFlo = amp/0.0153;
TVector3 lpos;
rp->GetLocalPosition(lpos);
fSM[iSupMod]->RegisterCluster(iSupMod,ampFlo,lpos[0],lpos[1],lpos[2]);
}
}
}
}
void AliEveEMCALData::LoadRecPointsFromEMCALLoader(AliEMCALLoader* const emcl)
{
AliEMCALRecPoint* rp;
TClonesArray *clusters = (TClonesArray*)emcl->RecPoints();
for(Int_t iclu = 0; iclu< clusters->GetEntries();iclu++){
rp = static_cast<AliEMCALRecPoint *>(clusters->At(iclu)) ;
if(rp){
if(fDebug>1) cout << "RecPoint info " << rp->GetAbsId() << " " << rp->GetEnergy() << endl;
Int_t iSupMod = rp->GetSuperModuleNumber();
Double_t amp = (Double_t)rp->GetEnergy();
Double_t ampFlo = amp/0.0153;
TVector3 lpos;
rp->GetLocalPosition(lpos);
fSM[iSupMod]->RegisterCluster(iSupMod,ampFlo,lpos[0],lpos[1],lpos[2]);
}
}
}
void AliEveEMCALData::LoadRecPointsFromESD()
{
Int_t iSupMod = 0 ;
Double_t x, y, z;
Int_t iSM = 0 ;
Int_t iT = 0 ;
Int_t iIp = 0 ;
Int_t iIe = 0 ;
Double_t xd, yd, zd;
Float_t pos[3] ;
AliESDVertex* primVertex =(AliESDVertex*) fESD->GetVertex();
Double_t vertexPosition[3] ;
primVertex->GetXYZ(vertexPosition) ;
TRefArray * caloClusters = new TRefArray();
fESD->GetEMCALClusters(caloClusters);
Int_t nclus = caloClusters->GetEntries();
cout << "nclus: " << nclus << endl;
for (Int_t iclus = 0; iclus < nclus; iclus++)
{
AliESDCaloCluster *clus = (AliESDCaloCluster *) caloClusters->At(iclus) ;
Double_t energy = clus->E() ;
Double_t eneInt = energy/0.0153;
Double_t disp = clus->GetDispersion() ;
clus->GetPosition(pos) ;
TVector3 vpos(pos[0],pos[1],pos[2]) ;
TLorentzVector p4 ;
TVector3 p3;
clus->GetMomentum(p4,vertexPosition);
p3.SetXYZ(p4[0],p4[1],p4[2]);
Double_t eta = p3.Eta();
Double_t phi = ( (p3.Phi()) < 0) ? (p3.Phi()) + 2. * TMath::Pi() : (p3.Phi());
Int_t mult = clus->GetNCells() ;
if(fDebug>2) {
cout << "In cluster: " << iclus << ", ncells: " << mult << ", energy : " << energy <<
", disp: " << disp << endl;
cout << "Cluster " << iclus << ", eta: " << eta << ", phi: " << phi << endl;
}
Int_t clusId = 0;
fGeom->GetAbsCellIdFromEtaPhi(eta,phi,clusId);
if(fDebug>2) {
cout << "Abs Cluster Id: " << clusId << ", xc: " << pos[0] <<
", yc: " << pos[1] << ", zc: " << pos[2] << endl;
}
GetGeomInfo(clusId,iSupMod,x,y,z);
Int_t digMult = clus->GetNCells() ;
UShort_t *digID = clus->GetCellsAbsId() ;
for(Int_t i=0; i<digMult; i++){
fGeom->RelPosCellInSModule(digID[i], xd, yd, zd);
fGeom->GetCellIndex(digID[i],iSM,iT,iIp,iIe);
}
fSM[iSupMod]->RegisterCluster(iSM,eneInt,x,y,z);
}
}
AliEveEMCALSModuleData* AliEveEMCALData::GetSModuleData(Int_t sm)
{
if (sm < 0 || sm > fNsm)
{
printf("The number of super modules must be lower or equal to %d",fNsm);
return 0;
}
return fSM[sm];
}
void AliEveEMCALData::LoadRaw() const
{
}