#include "AliMUONv1.h"
#include "AliMUONConstants.h"
#include "AliMUONResponseFactory.h"
#include "AliMUONHit.h"
#include "AliMUONGeometryBuilder.h"
#include "AliMUONGeometry.h"
#include "AliMUONGeometryTransformer.h"
#include "AliMUONGeometryModule.h"
#include "AliMUONStringIntMap.h"
#include "AliMUONGeometryDetElement.h"
#include "AliMpCDB.h"
#include "AliMpDEManager.h"
#include "AliConst.h"
#include "AliMagF.h"
#include "AliRun.h"
#include "AliMC.h"
#include "AliTrackReference.h"
#include "AliLog.h"
#include <TClonesArray.h>
#include <TF1.h>
#include <TF2.h>
#include <TGeoGlobalMagField.h>
#include <TGeoMatrix.h>
#include <TRandom.h>
#include <TRandom.h>
#include <TVirtualMC.h>
#include <string>
#include "AliMUONVHitStore.h"
using std::endl;
using std::cout;
using std::setw;
ClassImp(AliMUONv1)
AliMUONv1::AliMUONv1()
: AliMUON(),
fAngleEffect(kTRUE),
fMagEffect(kTRUE),
fStepMaxInActiveGas(0.6),
fStepSum(0x0),
fDestepSum(0x0),
fTrackMomentum(),
fTrackPosition(),
fElossRatio(0x0),
fAngleEffect10(0x0),
fAngleEffectNorma(0x0),
fMagAngleEffectNorma(0x0)
{
AliDebug(1,Form("default (empty) ctor this=%p",this));
}
AliMUONv1::AliMUONv1(const char *name, const char* title)
: AliMUON(name, title),
fAngleEffect(kTRUE),
fMagEffect(kTRUE),
fStepMaxInActiveGas(0.6),
fStepSum(0x0),
fDestepSum(0x0),
fTrackMomentum(),
fTrackPosition(),
fElossRatio(0x0),
fAngleEffect10(0x0),
fAngleEffectNorma(0x0),
fMagAngleEffectNorma(0x0)
{
AliDebug(1,Form("ctor this=%p",this));
if ( ! AliMpCDB::LoadMpSegmentation() ) {
AliFatal("Could not access mapping from OCDB !");
}
fStepSum = new Float_t [AliMUONConstants::NCh()];
fDestepSum = new Float_t [AliMUONConstants::NCh()];
for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
fStepSum[i] =0.0;
fDestepSum[i]=0.0;
}
fElossRatio = new TF1("ElossRatio","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",0.5,5.);
fElossRatio->SetParameter(0,1.02138);
fElossRatio->SetParameter(1,-9.54149e-02);
fElossRatio->SetParameter(2,+7.83433e-02);
fElossRatio->SetParameter(3,-9.98208e-03);
fElossRatio->SetParameter(4,+3.83279e-04);
fAngleEffect10 = new TF1("AngleEffect10","[0]+[1]*x+[2]*x*x",0.5,3.0);
fAngleEffect10->SetParameter(0, 1.90691e+02);
fAngleEffect10->SetParameter(1,-6.62258e+01);
fAngleEffect10->SetParameter(2,+1.28247e+01);
fAngleEffectNorma = new TF1("AngleEffectNorma","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.0,10.0);
fAngleEffectNorma->SetParameter(0,4.148);
fAngleEffectNorma->SetParameter(1,-6.809e-01);
fAngleEffectNorma->SetParameter(2,5.151e-02);
fAngleEffectNorma->SetParameter(3,-1.490e-03);
fMagAngleEffectNorma = new TF2("MagAngleEffectNorma","121.24/(([1]+[2]*abs(y))+[3]*abs(x-[0]*y)+[4]*abs((x-[0]*y)*(x-[0]*y))+[5]*abs((x-[0]*y)*(x-[0]*y)*(x-[0]*y))+[6]*abs((x-[0]*y)*(x-[0]*y)*(x-[0]*y)*(x-[0]*y)))",-20.0,20.0,-1.,1.);
fMagAngleEffectNorma->SetParameters(8.6995, 25.4022, 13.8822, 2.4717, 1.1551, -0.0624, 0.0012);
}
AliMUONv1::~AliMUONv1()
{
AliDebug(1,Form("dtor this=%p",this));
delete [] fStepSum;
delete [] fDestepSum;
delete fElossRatio;
delete fAngleEffect10;
delete fAngleEffectNorma;
delete fMagAngleEffectNorma;
}
void AliMUONv1::CreateGeometry()
{
fGeometryBuilder->CreateGeometry();
}
void AliMUONv1::CreateMaterials()
{
fGeometryBuilder->CreateMaterials();
}
void AliMUONv1::UpdateInternalGeometry()
{
if ( ! AliMpCDB::LoadMpSegmentation() ) {
AliFatal("Could not access mapping from OCDB !");
}
fGeometryBuilder->UpdateInternalGeometry();
}
void AliMUONv1::AddAlignableVolumes() const
{
GetGeometryTransformer()->AddAlignableVolumes();
}
void AliMUONv1::Init()
{
AliDebug(1,"Start Init for version 1 - CPC chamber type");
fGeometryBuilder->InitGeometry();
AliDebug(1,"Finished Init for version 1 - CPC chamber type");
AliMUONResponseFactory respFactory("default", fIsTailEffect);
respFactory.Build(this);
}
Int_t AliMUONv1::GetGeomModuleId(Int_t volId) const
{
for (Int_t i = 0; i < AliMUONConstants::NGeomModules(); i++) {
if ( GetGeometry()->GetModule(i)->IsSensitiveVolume(volId) )
return i;
}
return -1;
}
TString AliMUONv1::CurrentVolumePath() const
{
TString path = "";
TString name;
Int_t copyNo;
Int_t imother = 0;
do {
name = TVirtualMC::GetMC()->CurrentVolOffName(imother);
TVirtualMC::GetMC()->CurrentVolOffID(imother++, copyNo);
TString add = "/";
add += name;
add += "_";
add += copyNo;
path.Insert(0,add);
}
while ( name != TString("ALIC") );
return path;
}
void AliMUONv1::StepManager()
{
if( !(TVirtualMC::GetMC()->TrackCharge()) ) return;
static Int_t idvol=-1, iEnter = 0;
Int_t copy;
const Float_t kBig = 1.e10;
static Double_t xyzEnter[3];
Int_t id=TVirtualMC::GetMC()->CurrentVolID(copy);
Int_t iGeomModule = GetGeomModuleId(id);
if (iGeomModule == -1) return;
const AliMUONGeometryModule* kGeometryModule
= GetGeometry()->GetModule(iGeomModule);
AliMUONGeometryDetElement* detElement
= kGeometryModule->FindBySensitiveVolume(CurrentVolumePath());
if (!detElement && iGeomModule < AliMUONConstants::NGeomModules()-2) {
iGeomModule++;
const AliMUONGeometryModule* kGeometryModule2
= GetGeometry()->GetModule(iGeomModule);
detElement
= kGeometryModule2->FindBySensitiveVolume(CurrentVolumePath());
}
Int_t detElemId = 0;
if (detElement) detElemId = detElement->GetUniqueID();
if (!detElemId) {
AliErrorStream()
<< "Geometry module id: "
<< setw(3) << iGeomModule << " "
<< "Current SV: "
<< CurrentVolumePath()
<< " detElemId: "
<< setw(5) << detElemId
<< endl;
Double_t x, y, z;
TVirtualMC::GetMC()->TrackPosition(x, y, z);
AliErrorStream()
<< " global position: "
<< x << ", " << y << ", " << z
<< endl;
AliErrorStream() << "DetElemId not identified." << endl;
}
Int_t iChamber = AliMpDEManager::GetChamberId(detElemId) + 1;
idvol = iChamber -1;
if ( (TVirtualMC::GetMC()->IsTrackEntering() || TVirtualMC::GetMC()->IsTrackExiting() ) ) {
AliTrackReference* trackReference
= AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kMUON);
trackReference->SetUserId(detElemId);
}
if( TVirtualMC::GetMC()->IsTrackEntering() ) {
Float_t theta = fTrackMomentum.Theta();
if ( fIsMaxStep && (TMath::Pi()-theta)*kRaddeg>=15. ) {
TVirtualMC::GetMC()->SetMaxStep(fStepMaxInActiveGas);
}
iEnter = 1;
TVirtualMC::GetMC()->TrackPosition(xyzEnter[0], xyzEnter[1], xyzEnter[2]);
}
Int_t ipart = TVirtualMC::GetMC()->TrackPid();
Float_t mass = TVirtualMC::GetMC()->TrackMass();
fDestepSum[idvol]+=TVirtualMC::GetMC()->Edep();
if ( fStepSum[idvol]==0.0 ) TVirtualMC::GetMC()->TrackMomentum(fTrackMomentum);
fStepSum[idvol]+=TVirtualMC::GetMC()->TrackStep();
if ( TVirtualMC::GetMC()->IsTrackExiting() ||
TVirtualMC::GetMC()->IsTrackStop() ||
TVirtualMC::GetMC()->IsTrackDisappeared()||
(fStepSum[idvol]>fStepMaxInActiveGas) ) {
if ( fIsMaxStep &&
( TVirtualMC::GetMC()->IsTrackExiting() ||
TVirtualMC::GetMC()->IsTrackStop() ||
TVirtualMC::GetMC()->IsTrackDisappeared() ) ) TVirtualMC::GetMC()->SetMaxStep(kBig);
if (fDestepSum[idvol] == 0) {
fStepSum[idvol] = 0;
iEnter = 0;
return;
}
TVirtualMC::GetMC()->TrackPosition(fTrackPosition);
Float_t theta = fTrackMomentum.Theta();
Float_t phi = fTrackMomentum.Phi();
Int_t merge = 0;
Double_t xyz0[3]={0}, xyz1[3]={0}, tmp[3]={0};
if (TVirtualMC::GetMC()->IsTrackExiting() && iEnter != 0) {
TVirtualMC::GetMC()->Gmtod(xyzEnter, xyz0, 1);
fTrackPosition.Vect().GetXYZ(tmp);
TVirtualMC::GetMC()->Gmtod(tmp, xyz1, 1);
Float_t dx = xyz0[0] - xyz1[0];
Float_t dy = xyz0[1] - xyz1[1];
Float_t thLoc = TMath::ATan2 (TMath::Sqrt(dx*dx+dy*dy), TMath::Abs(xyz0[2]-xyz1[2]));
if (thLoc * TMath::RadToDeg() < 15) merge = 1;
}
if (merge) {
Double_t dz = -0.5;
if (xyz1[2] != xyz0[2]) dz = xyz0[2] / (xyz1[2] - xyz0[2]);
tmp[0] = xyz0[0] - (xyz1[0] - xyz0[0]) * dz;
tmp[1] = xyz0[1] - (xyz1[1] - xyz0[1]) * dz;
tmp[2] = xyz0[2] - (xyz1[2] - xyz0[2]) * dz;
TVirtualMC::GetMC()->Gdtom(tmp, xyz1, 1);
fTrackPosition.SetXYZT(xyz1[0], xyz1[1], xyz1[2], fTrackPosition.T());
} else {
TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
fStepSum[idvol]/2.*sin(theta)*sin(phi),
fStepSum[idvol]/2.*cos(theta),0.0 );
fTrackPosition-=backToWire;
}
Float_t betaxGamma = fTrackMomentum.P()/mass;
Float_t sigmaEffect10degrees;
Float_t sigmaEffectThetadegrees;
Float_t eLossParticleELossMip;
Float_t yAngleEffect=0.;
Float_t thetawires = TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) ;
Double_t bField[3] = {0};
fTrackPosition.Vect().GetXYZ(tmp);
TGeoGlobalMagField::Instance()->Field(tmp,bField);
if (fAngleEffect && !fMagEffect){
thetawires = TMath::Abs(thetawires);
if ( (betaxGamma >3.2) && (thetawires*kRaddeg<=15.) ) {
betaxGamma=TMath::Log(betaxGamma);
eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);
sigmaEffectThetadegrees = sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);
if ( (iChamber==1) || (iChamber==2) )
sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg));
yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees);
}
}
else if (fAngleEffect && fMagEffect) {
if ( (betaxGamma >3.2) && (TMath::Abs(thetawires*kRaddeg)<=15.) ) {
betaxGamma=TMath::Log(betaxGamma);
eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);
sigmaEffectThetadegrees = sigmaEffect10degrees/fMagAngleEffectNorma->Eval(thetawires*kRaddeg,bField[0]/10.);
if ( (iChamber==1) || (iChamber==2) )
sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg));
yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees);
}
}
AliMUONHit hit(fIshunt,
gAlice->GetMCApp()->GetCurrentTrackNumber(),
detElemId, ipart,
fTrackPosition.X(),
fTrackPosition.Y()+yAngleEffect,
fTrackPosition.Z(),
TVirtualMC::GetMC()->TrackTime(),
fTrackMomentum.P(),
theta,
phi,
fStepSum[idvol],
fDestepSum[idvol],
fTrackPosition.X(),
fTrackPosition.Y(),
fTrackPosition.Z());
fHitStore->Add(hit);
fStepSum[idvol] =0;
fDestepSum[idvol]=0;
iEnter = 0;
}
}