#include <fstream>
#include <TClonesArray.h>
#include <TGeoManager.h>
#include <TString.h>
#include <TMath.h>
#include "AliSurveyObj.h"
#include "AliPHOSEMCAGeometry.h"
#include "AliAlignObjParams.h"
#include "AliPHOSGeometry.h"
#include "AliPHOSSurvey.h"
#include "AliLog.h"
ClassImp(AliPHOSSurvey)
AliPHOSSurvey::AliPHOSSurvey()
: fStrNum(0),
fStripData(0)
{
}
namespace {
struct AliPHOSStripCoords {
Double_t fX1;
Double_t fZ1;
Double_t fX2;
Double_t fZ2;
};
}
AliPHOSSurvey::AliPHOSSurvey(const TString &txtFileName)
: fStrNum(0),
fStripData(0)
{
const AliPHOSGeometry *phosGeom = AliPHOSGeometry::GetInstance("IHEP", "IHEP");
if (!phosGeom) {
AliError("Cannot obtain AliPHOSGeometry instance.");
return;
}
std::ifstream inputFile(txtFileName.Data());
if (!inputFile) {
AliError(("Cannot open the survey file " + txtFileName).Data());
return;
}
AliPHOSEMCAGeometry * emcaGeom = phosGeom->GetEMCAGeometry();
fStrNum = emcaGeom->GetNStripX() * emcaGeom->GetNStripZ();
Int_t dummyInt = 0;
Double_t dummyY = 0.;
Double_t *xReal = new Double_t[fStrNum * 2];
Double_t *zReal = new Double_t[fStrNum * 2];
for (Int_t i = 0; i < fStrNum * 2; ++i) {
xReal[i] = 0.;
zReal[i] = 0.;
}
for (Int_t i = 0; i < fStrNum * 2; ++i) {
if (!inputFile) {
AliError("Error while reading input file.");
delete [] xReal;
delete [] zReal;
return;
}
inputFile>>dummyInt>>xReal[i]>>dummyY>>zReal[i];
xReal[i] *= 0.1;
zReal[i] *= 0.1;
}
InitStripData(xReal, zReal);
delete [] zReal;
delete [] xReal;
}
AliPHOSSurvey::~AliPHOSSurvey()
{
delete [] fStripData;
}
void AliPHOSSurvey::CreateAliAlignObjParams(TClonesArray &array)
{
const AliPHOSGeometry * phosGeom = AliPHOSGeometry::GetInstance("IHEP", "IHEP");
if (!phosGeom) {
AliError("Cannot obtain AliPHOSGeometry instance.");
return;
}
if (!gGeoManager) {
AliWarning("Cannot create local transformations for strip units - gGeoManager does not exist.");
AliInfo("Null shifts and rotations will be created instead.");
return CreateNullObjects(array, phosGeom);
}
AliPHOSEMCAGeometry * emcaGeom = phosGeom->GetEMCAGeometry();
Int_t arrayInd = array.GetEntries(), iIndex = 0;
AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
for (Int_t module = 1; module <= phosGeom->GetNModules(); ++module) {
for (Int_t i = 0, stripNum = 0; i < emcaGeom->GetNStripX(); ++i) {
for (Int_t j = 0; j < emcaGeom->GetNStripZ(); ++j) {
TString stripName(TString::Format("PHOS/Module%d/Strip_%d_%d", module, i, j));
AliPHOSStripDelta t(GetStripTransformation(stripNum++, module));
new(array[arrayInd])
AliAlignObjParams(
stripName.Data(), volid,
t.fXShift, t.fYShift, t.fZShift,
-t.fPsi, -t.fTheta, -t.fPhi,
false
);
++arrayInd;
}
}
}
}
void AliPHOSSurvey::CreateNullObjects(TClonesArray &array, const AliPHOSGeometry *phosGeom)const
{
const AliPHOSEMCAGeometry * emcaGeom = phosGeom->GetEMCAGeometry();
Int_t arrayInd = array.GetEntries(), iIndex = 0;
AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
for (Int_t module = 1; module <= phosGeom->GetNModules(); ++module)
for (Int_t i = 0; i < emcaGeom->GetNStripX(); ++i)
for (Int_t j = 0; j < emcaGeom->GetNStripZ(); ++j) {
TString stripName(TString::Format("PHOS/Module%d/Strip_%d_%d", module, i, j));
new(array[arrayInd]) AliAlignObjParams(stripName.Data(), volid, 0., 0., 0., 0., 0., 0., true);
++arrayInd;
}
}
AliPHOSSurvey::AliPHOSStripDelta AliPHOSSurvey::GetStripTransformation(Int_t stripIndex, Int_t module)const
{
AliPHOSStripDelta t = {0., 0., 0., 0., 0., 0.};
if (module != 3 || !fStripData)
return t;
return fStripData[stripIndex];
}
void AliPHOSSurvey::InitStripData(const Double_t *xReal, const Double_t *zReal)
{
const AliPHOSGeometry *phosGeom = AliPHOSGeometry::GetInstance("IHEP", "IHEP");
AliPHOSEMCAGeometry * emcaGeom = phosGeom->GetEMCAGeometry();
const Float_t *strip = emcaGeom->GetStripHalfSize();
const Float_t *cell = emcaGeom->GetAirCellHalfSize();
AliPHOSStripCoords *idealStrips = new AliPHOSStripCoords[fStrNum];
for (Int_t ix = 0, stripNumber = 0; ix < emcaGeom->GetNStripX(); ++ix) {
for (Int_t iz = 0; iz < emcaGeom->GetNStripZ(); ++iz) {
AliPHOSStripCoords &str = idealStrips[stripNumber++];
str.fX1 = ix * 2 * strip[0];
str.fX2 = str.fX1 + 14 * cell[0];
str.fZ1 = iz * 2 * strip[2];
str.fZ2 = str.fZ1 + 2 * cell[2];
}
}
AliPHOSStripCoords *realStrips = new AliPHOSStripCoords[fStrNum];
for (Int_t j = 0, stripNumber = 0; j < emcaGeom->GetNStripX() * 2; j += 2) {
for (Int_t i = 0; i < emcaGeom->GetNStripZ(); ++i) {
AliPHOSStripCoords &str = realStrips[stripNumber++];
str.fX1 = xReal[i + j * emcaGeom->GetNStripZ()];
str.fZ1 = zReal[i + j * emcaGeom->GetNStripZ()];
str.fX2 = xReal[i + (j + 1) * emcaGeom->GetNStripZ()];
str.fZ2 = zReal[i + (j + 1) * emcaGeom->GetNStripZ()];
}
}
fStripData = new AliPHOSStripDelta[fStrNum];
for (Int_t i = 0; i < fStrNum; ++i) {
const AliPHOSStripCoords &real = realStrips[i];
const AliPHOSStripCoords &ideal = idealStrips[i];
AliPHOSStripDelta &t = fStripData[i];
t.fTheta = TMath::ATan((real.fZ2 - real.fZ1) / (real.fX2 - real.fX1)) -
TMath::ATan((ideal.fZ2 - ideal.fZ1) / (ideal.fX2 - ideal.fX1));
t.fTheta *= TMath::RadToDeg();
t.fXShift = (real.fX1 + real.fX2) / 2 - (ideal.fX1 + ideal.fX2) / 2;
t.fZShift = (real.fZ1 + real.fZ2) / 2 - (ideal.fZ1 + ideal.fZ2) / 2;
t.fYShift = 0., t.fPsi = 0., t.fPhi = 0.;
}
delete [] realStrips;
delete [] idealStrips;
}