#include "TClonesArray.h"
#include "TVector3.h"
#include "TParticle.h"
#include <TGeoManager.h>
#include <TGeoMatrix.h>
#include "AliLog.h"
#include "AliPHOSEMCAGeometry.h"
#include "AliPHOSCPVGeometry.h"
#include "AliPHOSSupportGeometry.h"
#include "AliPHOSGeoUtils.h"
ClassImp(AliPHOSGeoUtils)
AliPHOSGeoUtils::AliPHOSGeoUtils():
fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
{
fXtlArrSize[0]=0.;
fXtlArrSize[1]=0.;
fXtlArrSize[2]=0.;
for(Int_t mod=0; mod<5; mod++){
fEMCMatrix[mod]=0 ;
for(Int_t istrip=0; istrip<224; istrip++)
fStripMatrix[mod][istrip]=0 ;
fCPVMatrix[mod]=0;
fPHOSMatrix[mod]=0 ;
}
}
AliPHOSGeoUtils::AliPHOSGeoUtils(const AliPHOSGeoUtils & rhs)
: TNamed(rhs),
fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
{
Fatal("cpy ctor", "not implemented") ;
for(Int_t mod=0; mod<5; mod++){
fEMCMatrix[mod]=0 ;
for(Int_t istrip=0; istrip<224; istrip++)
fStripMatrix[mod][istrip]=0 ;
fCPVMatrix[mod]=0;
fPHOSMatrix[mod]=0 ;
}
}
AliPHOSGeoUtils::AliPHOSGeoUtils(const Text_t* name, const Text_t* title)
: TNamed(name, title),
fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
{
fGeometryEMCA = new AliPHOSEMCAGeometry() ;
fGeometryCPV = new AliPHOSCPVGeometry() ;
fGeometrySUPP = new AliPHOSSupportGeometry() ;
fNModules = 5;
fNPhi = fGeometryEMCA->GetNPhi() ;
fNZ = fGeometryEMCA->GetNZ() ;
fNCristalsInModule = fNPhi*fNZ ;
fNCellsXInStrip= fGeometryEMCA->GetNCellsXInStrip() ;
fNCellsZInStrip= fGeometryEMCA->GetNCellsZInStrip() ;
fNStripZ = fGeometryEMCA->GetNStripZ() ;
fXtlArrSize[0]=fGeometryEMCA->GetInnerThermoHalfSize()[0] ;
fXtlArrSize[1]=fGeometryEMCA->GetInnerThermoHalfSize()[1] ;
fXtlArrSize[2]=fGeometryEMCA->GetInnerThermoHalfSize()[2] ;
const Float_t * inthermo = fGeometryEMCA->GetInnerThermoHalfSize() ;
const Float_t * strip = fGeometryEMCA->GetStripHalfSize() ;
const Float_t * splate = fGeometryEMCA->GetSupportPlateHalfSize();
const Float_t * crystal = fGeometryEMCA->GetCrystalHalfSize() ;
const Float_t * pin = fGeometryEMCA->GetAPDHalfSize() ;
const Float_t * preamp = fGeometryEMCA->GetPreampHalfSize() ;
fCrystalShift=-inthermo[1]+strip[1]+splate[1]+crystal[1]-fGeometryEMCA->GetAirGapLed()/2.+pin[1]+preamp[1] ;
fCryCellShift=crystal[1]-(fGeometryEMCA->GetAirGapLed()-2*pin[1]-2*preamp[1])/2;
fCryStripShift=fCryCellShift+splate[1] ;
fCellStep = 2.*fGeometryEMCA->GetAirCellHalfSize()[0] ;
fNumberOfCPVPadsPhi = fGeometryCPV->GetNumberOfCPVPadsPhi() ;
fNumberOfCPVPadsZ = fGeometryCPV->GetNumberOfCPVPadsZ() ;
fPadSizePhi = fGeometryCPV->GetCPVPadSizePhi() ;
fPadSizeZ = fGeometryCPV->GetCPVPadSizeZ() ;
fCPVBoxSizeY= fGeometryCPV->GetCPVBoxSize(1) ;
for(Int_t mod=0; mod<5; mod++){
fEMCMatrix[mod]=0 ;
for(Int_t istrip=0; istrip<224; istrip++)
fStripMatrix[mod][istrip]=0 ;
fCPVMatrix[mod]=0;
fPHOSMatrix[mod]=0 ;
}
}
AliPHOSGeoUtils & AliPHOSGeoUtils::operator = (const AliPHOSGeoUtils & ) {
Fatal("assignment operator", "not implemented") ;
return *this ;
}
AliPHOSGeoUtils::~AliPHOSGeoUtils(void)
{
if(fGeometryEMCA){
delete fGeometryEMCA; fGeometryEMCA = 0 ;
}
if(fGeometryCPV){
delete fGeometryCPV; fGeometryCPV=0 ;
}
if(fGeometrySUPP){
delete fGeometrySUPP ; fGeometrySUPP=0 ;
}
if(fMisalArray){
delete fMisalArray; fMisalArray=0 ;
}
for(Int_t mod=0; mod<5; mod++){
delete fEMCMatrix[mod] ;
for(Int_t istrip=0; istrip<224; istrip++)
delete fStripMatrix[mod][istrip];
delete fCPVMatrix[mod];
delete fPHOSMatrix[mod];
}
}
Bool_t AliPHOSGeoUtils::AbsToRelNumbering(Int_t absId, Int_t * relid) const
{
Float_t id = absId ;
Int_t phosmodulenumber = (Int_t)TMath:: Ceil( id / fNCristalsInModule ) ;
if ( phosmodulenumber > fNModules ) {
id -= fNPhi * fNZ * fNModules ;
Float_t nCPV = fNumberOfCPVPadsPhi * fNumberOfCPVPadsZ ;
relid[0] = (Int_t) TMath::Ceil( id / nCPV ) ;
relid[1] = -1 ;
id -= ( relid[0] - 1 ) * nCPV ;
relid[2] = (Int_t) TMath::Ceil( id / fNumberOfCPVPadsZ ) ;
relid[3] = (Int_t) ( id - ( relid[2] - 1 ) * fNumberOfCPVPadsZ ) ;
}
else {
relid[0] = phosmodulenumber ;
relid[1] = 0 ;
id -= ( phosmodulenumber - 1 ) * fNPhi * fNZ ;
relid[2] = (Int_t)TMath::Ceil( id / fNZ ) ;
relid[3] = (Int_t)( id - ( relid[2] - 1 ) * fNZ ) ;
}
return kTRUE ;
}
Bool_t AliPHOSGeoUtils::RelToAbsNumbering(const Int_t * relid, Int_t & absId) const
{
if ( relid[1] == 0 ) {
absId =
( relid[0] - 1 ) * fNPhi * fNZ
+ ( relid[2] - 1 ) * fNZ
+ relid[3] ;
}
else {
absId = fNPhi * fNZ * fNModules
+ ( relid[0] - 1 ) * fNumberOfCPVPadsPhi * fNumberOfCPVPadsZ
+ ( relid[2] - 1 ) * fNumberOfCPVPadsZ
+ relid[3] ;
}
return kTRUE ;
}
void AliPHOSGeoUtils::RelPosInModule(const Int_t * relid, Float_t & x, Float_t & z) const
{
if(relid[1]==0){
Double_t pos[3]= {0.0,-fCryCellShift,0.};
Double_t posC[3]={0.0,0.0,0.};
Int_t strip=1+((Int_t) TMath::Ceil((Double_t)relid[2]/fNCellsXInStrip))*fNStripZ-
(Int_t) TMath::Ceil((Double_t)relid[3]/fNCellsZInStrip) ;
pos[0]=((relid[2]-1)%fNCellsXInStrip-fNCellsXInStrip/2+0.5)*fCellStep ;
pos[2]=(-(relid[3]-1)%fNCellsZInStrip+fNCellsZInStrip/2-0.5)*fCellStep ;
Int_t mod = relid[0] ;
const TGeoHMatrix * m2 = GetMatrixForStrip(mod, strip) ;
m2->LocalToMaster(pos,posC);
Double_t posL2[3]={posC[0],posC[1],posC[2]};
const TGeoHMatrix *mPHOS2 = GetMatrixForModule(mod) ;
mPHOS2->MasterToLocal(posC,posL2);
x=posL2[0] ;
z=-posL2[2];
return ;
}
else{
Int_t row = relid[2] ;
Int_t column = relid[3] ;
Double_t pos[3]= {0.0,0.0,0.};
Double_t posC[3]={0.0,0.0,0.};
pos[0] = - ( fNumberOfCPVPadsPhi/2. - row - 0.5 ) * fPadSizePhi ;
pos[2] = - ( fNumberOfCPVPadsZ /2. - column - 0.5 ) * fPadSizeZ ;
const TGeoHMatrix *m = GetMatrixForCPV(relid[0]) ;
m->LocalToMaster(pos,posC);
Double_t posL[3]={0.,0.,0.,} ;
const TGeoHMatrix *mPHOS = GetMatrixForPHOS(relid[0]) ;
mPHOS->MasterToLocal(posC,posL);
x=posL[0] ;
z=posL[1];
return ;
}
}
void AliPHOSGeoUtils::RelPosToAbsId(Int_t module, Double_t x, Double_t z, Int_t & absId) const
{
Int_t relid[4]={module,0,1,1} ;
relid[2] = static_cast<Int_t>(TMath::Ceil( x/ fCellStep + fNPhi / 2.) );
relid[3] = fNZ+1-static_cast<Int_t>(TMath::Ceil(-z/ fCellStep + fNZ / 2.) ) ;
if(relid[2]<1)relid[2]=1 ;
if(relid[3]<1)relid[3]=1 ;
if(relid[2]>fNPhi)relid[2]=fNPhi ;
if(relid[3]>fNZ)relid[3]=fNZ ;
RelToAbsNumbering(relid,absId) ;
}
void AliPHOSGeoUtils::RelPosToRelId(Int_t module, Double_t x, Double_t z, Int_t * relId) const
{
Int_t absId ;
RelPosToAbsId(module, x,z,absId) ;
AbsToRelNumbering(absId,relId) ;
}
void AliPHOSGeoUtils::RelPosInAlice(Int_t id, TVector3 & pos ) const
{
if (!gGeoManager){
AliFatal("Geo manager not initialized\n");
}
Int_t relid[4] ;
AbsToRelNumbering(id , relid) ;
if(relid[1]==0){
Double_t ps[3]= {0.0,-fCryStripShift,0.};
Double_t psC[3]={0.0,0.0,0.};
Int_t strip=1+((Int_t) TMath::Ceil((Double_t)relid[2]/fNCellsXInStrip))*fNStripZ-
(Int_t) TMath::Ceil((Double_t)relid[3]/fNCellsZInStrip) ;
ps[0]=((relid[2]-1)%fNCellsXInStrip-fNCellsXInStrip/2+0.5)*fCellStep ;
ps[2]=(-(relid[3]-1)%fNCellsZInStrip+fNCellsZInStrip/2-0.5)*fCellStep ;
Int_t mod = relid[0] ;
const TGeoHMatrix * m2 = GetMatrixForStrip(mod, strip) ;
m2->LocalToMaster(ps,psC);
pos.SetXYZ(psC[0],psC[1],psC[2]) ;
}
else{
Int_t row = relid[2] ;
Int_t column = relid[3] ;
Double_t ps[3]= {0.0,fCPVBoxSizeY/2.,0.};
Double_t psC[3]={0.0,0.0,0.};
pos[0] = - ( fNumberOfCPVPadsPhi/2. - row - 0.5 ) * fPadSizePhi ;
pos[2] = - ( fNumberOfCPVPadsZ /2. - column - 0.5 ) * fPadSizeZ ;
const TGeoHMatrix *m = GetMatrixForCPV(relid[0]) ;
m->LocalToMaster(ps,psC);
pos.SetXYZ(psC[0],psC[1],-psC[2]) ;
}
}
void AliPHOSGeoUtils::Local2Global(Int_t mod, Float_t x, Float_t z,
TVector3& globalPosition) const
{
Double_t posL[3]={x,-fCrystalShift,-z} ;
Double_t posG[3] ;
const TGeoHMatrix *mPHOS = GetMatrixForModule(mod) ;
mPHOS->LocalToMaster(posL,posG);
globalPosition.SetXYZ(posG[0],posG[1],posG[2]) ;
}
void AliPHOSGeoUtils::Global2Local(TVector3& localPosition,
const TVector3& globalPosition,
Int_t module) const
{
Double_t posG[3]={globalPosition.X(),globalPosition.Y(),globalPosition.Z()} ;
Double_t posL[3]={0.,0.,0.} ;
const TGeoHMatrix *mPHOS = GetMatrixForModule(module) ;
if(mPHOS){
mPHOS->MasterToLocal(posG,posL);
localPosition.SetXYZ(posL[0],posL[1]+fCrystalShift,-posL[2]) ;
}
else{
localPosition.SetXYZ(999.,999.,999.) ;
}
}
Bool_t AliPHOSGeoUtils::GlobalPos2RelId(TVector3 & global, Int_t * relId){
TVector3 loc ;
for(Int_t mod=1; mod<=fNModules; mod++){
Global2Local(loc,global,mod) ;
if((TMath::Abs(loc.Z())<fXtlArrSize[2]) && (TMath::Abs(loc.X())<fXtlArrSize[0])){
RelPosToRelId(mod,loc.X(),loc.Z(),relId);
return kTRUE ;
}
}
return kFALSE ;
}
Bool_t AliPHOSGeoUtils::ImpactOnEmc(const TParticle * particle,
Int_t & moduleNumber, Double_t & z, Double_t & x) const
{
Double_t vtx[3]={particle->Vx(),particle->Vy(),particle->Vz()} ;
return ImpactOnEmc(vtx,particle->Theta(),particle->Phi(),moduleNumber,z,x);
}
Bool_t AliPHOSGeoUtils::ImpactOnEmc(const Double_t * vtx, Double_t theta, Double_t phi,
Int_t & moduleNumber, Double_t & z, Double_t & x) const
{
TVector3 p(TMath::Sin(theta)*TMath::Cos(phi),TMath::Sin(theta)*TMath::Sin(phi),TMath::Cos(theta)) ;
return ImpactOnEmc(vtx,p,moduleNumber,z,x) ;
}
Bool_t AliPHOSGeoUtils::ImpactOnEmc(const Double_t * vtx, const TVector3 &p,
Int_t & moduleNumber, Double_t & z, Double_t & x) const
{
TVector3 v(vtx[0],vtx[1],vtx[2]) ;
for(Int_t imod=1; imod<=fNModules ; imod++){
Double_t tmp[3]={0.,-fCrystalShift,0.} ;
const TGeoHMatrix *m = GetMatrixForModule(imod) ;
if(!m)
continue ;
Double_t posG[3]={0.,0.,0.} ;
m->LocalToMaster(tmp,posG);
TVector3 n(posG[0],posG[1],posG[2]) ;
Double_t direction=n.Dot(p) ;
if(direction<=0.)
continue ;
Double_t fr = (n.Mag2()-n.Dot(v))/direction ;
n-=v+fr*p ;
n*=-1. ;
if(TMath::Abs(TMath::Abs(n.Z())<fXtlArrSize[2]) && n.Pt()<fXtlArrSize[0]){
moduleNumber = imod ;
z=n.Z() ;
x=TMath::Sign(n.Pt(),n.X()) ;
return kTRUE ;
}
}
x=0; z=0 ;
moduleNumber=0 ;
return kFALSE ;
}
void AliPHOSGeoUtils::GetIncidentVector(const TVector3 &vtx, Int_t module, Float_t x,Float_t z, TVector3 &vInc) const {
Global2Local(vInc,vtx,module) ;
vInc.SetXYZ(vInc.X()+x,vInc.Y(),vInc.Z()+z) ;
}
const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForModule(Int_t mod)const {
if(gGeoManager){
char path[255] ;
snprintf(path,255,"/ALIC_1/PHOS_%d/PEMC_1/PCOL_1/PTIO_1/PCOR_1/PAGA_1/PTII_1",mod) ;
if (!gGeoManager->cd(path)){
AliWarning(Form("Geo manager can not find path %s \n",path));
return 0;
}
return gGeoManager->GetCurrentMatrix();
}
if(fEMCMatrix[mod-1]){
return fEMCMatrix[mod-1] ;
}
else{
return 0 ;
}
return 0 ;
}
const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForStrip(Int_t mod, Int_t strip)const {
if(gGeoManager){
char path[255] ;
snprintf(path,255,"/ALIC_1/PHOS_%d/PEMC_1/PCOL_1/PTIO_1/PCOR_1/PAGA_1/PTII_1/PSTR_%d",mod,strip) ;
if (!gGeoManager->cd(path)){
AliWarning(Form("Geo manager can not find path %s \n",path));
return 0 ;
}
return gGeoManager->GetCurrentMatrix();
}
if(fStripMatrix[mod-1][strip-1]){
return fStripMatrix[mod-1][strip-1] ;
}
else{
AliWarning("Can not find PHOS misalignment matrixes\n") ;
AliWarning("Either import TGeoManager from geometry.root or \n");
AliWarning("read stored matrixes from AliESD Header: \n") ;
AliWarning("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
return 0 ;
}
return 0 ;
}
const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForCPV(Int_t mod)const {
if(gGeoManager){
char path[255] ;
snprintf(path,255,"/ALIC_1/PHOS_%d/PCPV_1",mod) ;
if (!gGeoManager->cd(path)){
AliWarning(Form("Geo manager can not find path %s \n",path));
return 0 ;
}
return gGeoManager->GetCurrentMatrix();
}
if(fCPVMatrix[mod-1]){
return fCPVMatrix[mod-1] ;
}
else{
AliWarning("Can not find PHOS misalignment matrixes\n") ;
AliWarning("Either import TGeoManager from geometry.root or \n");
AliWarning("read stored matrixes from AliESD Header: \n") ;
AliWarning("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
return 0 ;
}
return 0 ;
}
const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForPHOS(Int_t mod)const {
if(gGeoManager){
char path[255] ;
snprintf(path,255,"/ALIC_1/PHOS_%d",mod) ;
if (!gGeoManager->cd(path)){
AliWarning(Form("Geo manager can not find path %s \n",path));
return 0 ;
}
return gGeoManager->GetCurrentMatrix();
}
if(fPHOSMatrix[mod-1]){
return fPHOSMatrix[mod-1] ;
}
else{
AliWarning("Can not find PHOS misalignment matrixes\n") ;
AliWarning("Either import TGeoManager from geometry.root or \n");
AliWarning("read stored matrixes from AliESD Header: \n") ;
AliWarning("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
return 0 ;
}
return 0 ;
}
void AliPHOSGeoUtils::SetMisalMatrix(const TGeoHMatrix * m, Int_t mod){
if(fPHOSMatrix[mod]){
return ;
}
if(m==NULL)
return ;
fPHOSMatrix[mod]= new TGeoHMatrix(*m) ;
if(!fMisalArray)
fMisalArray = new TClonesArray("TGeoHMatrix",1120+10) ;
Int_t nr = fMisalArray->GetEntriesFast() ;
Double_t rotEMC[9]={1.,0.,0.,0.,0.,-1.,0.,1.,0.} ;
const Float_t * inthermo = fGeometryEMCA->GetInnerThermoHalfSize() ;
const Float_t * strip = fGeometryEMCA->GetStripHalfSize() ;
const Float_t * covparams = fGeometryEMCA->GetAlCoverParams() ;
const Float_t * warmcov = fGeometryEMCA->GetWarmAlCoverHalfSize() ;
Float_t z = fGeometryCPV->GetCPVBoxSize(1) / 2. - warmcov[2] + covparams[3]-inthermo[1] ;
Double_t locTII[3]={0.,0.,z} ;
Double_t globTII[3] ;
if (fEMCMatrix[mod] == NULL)
fEMCMatrix[mod] = new((*fMisalArray)[nr])TGeoHMatrix() ;
nr++ ;
fEMCMatrix[mod]->SetRotation(rotEMC) ;
fEMCMatrix[mod]->MultiplyLeft(fPHOSMatrix[mod]) ;
fPHOSMatrix[mod]->LocalToMaster(locTII,globTII) ;
fEMCMatrix[mod]->SetTranslation(globTII) ;
Double_t loc[3]={0.,inthermo[1] - strip[1],0.} ;
Double_t glob[3] ;
Int_t istrip=0 ;
for(Int_t irow = 0; irow < fGeometryEMCA->GetNStripX(); irow ++){
loc[0] = (2*irow + 1 - fGeometryEMCA->GetNStripX())* strip[0] ;
for(Int_t icol = 0; icol < fGeometryEMCA->GetNStripZ(); icol ++){
loc[2] = (2*icol + 1 - fGeometryEMCA->GetNStripZ()) * strip[2] ;
fEMCMatrix[mod]->LocalToMaster(loc,glob) ;
if (fStripMatrix[mod][istrip] == NULL)
fStripMatrix[mod][istrip] = new((*fMisalArray)[nr])TGeoHMatrix(*(fEMCMatrix[mod])) ;
nr++ ;
fStripMatrix[mod][istrip]->SetTranslation(glob) ;
istrip++;
}
}
const Float_t * emcParams = fGeometryEMCA->GetEMCParams() ;
Double_t globCPV[3] ;
Double_t locCPV[3]={0.,0.,- emcParams[3]} ;
Double_t rot[9]={1.,0.,0.,0.,0.,1.,0.,-1.,0.} ;
if (fCPVMatrix[mod] == NULL)
fCPVMatrix[mod] = new((*fMisalArray)[nr])TGeoHMatrix() ;
nr++ ;
fCPVMatrix[mod]->SetRotation(rot) ;
fCPVMatrix[mod]->MultiplyLeft(fPHOSMatrix[mod]) ;
fCPVMatrix[mod]->ReflectY(kFALSE) ;
fPHOSMatrix[mod]->LocalToMaster(locCPV,globCPV) ;
fCPVMatrix[mod]->SetTranslation(globCPV) ;
}