]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDv1.cxx
Fix of TOF problem, Calculation of dEdx and PID during propagation to TOF, Writing...
[u/mrichter/AliRoot.git] / TRD / AliTRDv1.cxx
index f32a01dc906bb216cd5061b256f400a5c3881ed7..19b1a7e4beb092999648b0bf7365e01a26ef511e 100644 (file)
 #include <TVector.h>
 #include <TVirtualMC.h>
 #include <TGeoManager.h>
+#include <TGeoMatrix.h>
+#include <TGeoPhysicalNode.h>
 
 #include "AliConst.h"
 #include "AliLog.h"
+#include "AliTrackReference.h"
 #include "AliMC.h"
 #include "AliRun.h"
 
 #include "AliTRDgeometry.h"
 #include "AliTRDhit.h"
-#include "AliTRDsim.h"
+#include "AliTRDsimTR.h"
 #include "AliTRDv1.h"
 
 ClassImp(AliTRDv1)
@@ -117,14 +120,16 @@ void AliTRDv1::AddAlignableVolumes() const
   TString volPath;
   TString symName;
 
-  TString vpStr  = "ALIC_1/B077_1/BSEGMO";
-  TString vpApp1 = "_1/BTRD";
-  TString vpApp2 = "_1";
-  TString vpApp3 = "/UTR1_1/UTS1_1/UTI1_1/UT";
+  TString vpStr   = "ALIC_1/B077_1/BSEGMO";
+  TString vpApp1  = "_1/BTRD";
+  TString vpApp2  = "_1";
+  TString vpApp3a = "/UTR1_1/UTS1_1/UTI1_1/UT";
+  TString vpApp3b = "/UTR2_1/UTS2_1/UTI2_1/UT";
+  TString vpApp3c = "/UTR3_1/UTS3_1/UTI3_1/UT";
 
-  TString snStr  = "TRD/sm";
-  TString snApp1 = "/st";
-  TString snApp2 = "/pl";
+  TString snStr   = "TRD/sm";
+  TString snApp1  = "/st";
+  TString snApp2  = "/pl";
 
   //
   // The super modules
@@ -154,6 +159,9 @@ void AliTRDv1::AddAlignableVolumes() const
   //                         TRD/sm17/st4/pl5
   //
   for (Int_t isect = 0; isect < AliTRDgeometry::Nsect(); isect++) {
+
+    if (fGeometry->GetSMstatus(isect) == 0) continue;
+
     for (Int_t icham = 0; icham < AliTRDgeometry::Ncham(); icham++) {
       for (Int_t iplan = 0; iplan < AliTRDgeometry::Nplan(); iplan++) {
 
@@ -164,7 +172,22 @@ void AliTRDv1::AddAlignableVolumes() const
         volPath += vpApp1;
         volPath += isect;
         volPath += vpApp2;
-        volPath += vpApp3;
+        switch (isect) {
+        case 13:
+        case 14:
+        case 15:
+          if (icham == 2) {
+            continue;
+         }
+          volPath += vpApp3c;
+          break;
+        case 11:
+        case 12:
+          volPath += vpApp3b;
+          break;
+        default:
+          volPath += vpApp3a;
+       };
         volPath += Form("%02d",idet);
         volPath += vpApp2;
 
@@ -175,7 +198,26 @@ void AliTRDv1::AddAlignableVolumes() const
         symName += snApp2;
         symName += iplan;
 
-        gGeoManager->SetAlignableEntry(symName.Data(),volPath.Data());
+        TGeoPNEntry *alignableEntry = 
+         gGeoManager->SetAlignableEntry(symName.Data(),volPath.Data());
+
+       // Add the tracking to local matrix following the TPC example
+       if (alignableEntry) {
+         const char *path = alignableEntry->GetTitle();
+         if (!gGeoManager->cd(path)) {
+           AliFatal(Form("Volume path %s not valid!",path));
+         }
+         // Is this correct still????
+         TGeoHMatrix *globMatrix = gGeoManager->GetCurrentMatrix();
+         Double_t sectorAngle = 20.0 * (isect % 18) + 10.0;
+         TGeoHMatrix *t2lMatrix  = new TGeoHMatrix();
+         t2lMatrix->RotateZ(sectorAngle);
+         t2lMatrix->MultiplyLeft(&(globMatrix->Inverse()));
+         alignableEntry->SetMatrix(t2lMatrix);
+       }
+       else {
+         AliError(Form("Alignable entry %s is not valid!",symName.Data()));
+       }
 
       }
     }
@@ -225,9 +267,6 @@ void AliTRDv1::CreateTRhit(Int_t det)
   // volume.
   //
 
-  // PDG code electron
-  const Int_t   kPdgElectron = 11;
-
   // Ionization energy
   const Float_t kWion        = 23.53;
 
@@ -237,85 +276,75 @@ void AliTRDv1::CreateTRhit(Int_t det)
   TLorentzVector mom;
   TLorentzVector pos;
 
-  // Create TR at the entrance of the chamber
-  if (gMC->IsTrackEntering()) {
+  Float_t eTR[kNTR];
+  Int_t   nTR;
 
-    // Create TR only for electrons 
-    Int_t iPdg = gMC->TrackPid();
-    if (TMath::Abs(iPdg) != kPdgElectron) {
-      return;
-    }
+  // Create TR photons
+  gMC->TrackMomentum(mom);
+  Float_t pTot = mom.Rho();
+  fTR->CreatePhotons(11,pTot,nTR,eTR);
+  if (nTR > kNTR) {
+    AliFatal(Form("Boundary error: nTR = %d, kNTR = %d",nTR,kNTR));
+  }
 
-    Float_t eTR[kNTR];
-    Int_t   nTR;
+  // Loop through the TR photons
+  for (Int_t iTR = 0; iTR < nTR; iTR++) {
 
-    // Create TR photons
-    gMC->TrackMomentum(mom);
-    Float_t pTot = mom.Rho();
-    fTR->CreatePhotons(iPdg,pTot,nTR,eTR);
-    if (nTR > kNTR) {
-      AliFatal(Form("Boundary error: nTR = %d, kNTR = %d",nTR,kNTR));
-    }
+    Float_t energyMeV = eTR[iTR] * 0.001;
+    Float_t energyeV  = eTR[iTR] * 1000.0;
+    Float_t absLength = 0.0;
+    Float_t sigma     = 0.0;
 
-    // Loop through the TR photons
-    for (Int_t iTR = 0; iTR < nTR; iTR++) {
-
-      Float_t energyMeV = eTR[iTR] * 0.001;
-      Float_t energyeV  = eTR[iTR] * 1000.0;
-      Float_t absLength = 0.0;
-      Float_t sigma     = 0.0;
-
-      // Take the absorbtion in the entrance window into account
-      Double_t muMy = fTR->GetMuMy(energyMeV);
-      sigma         = muMy * fFoilDensity;
-      if (sigma > 0.0) {
-        absLength = gRandom->Exp(1.0/sigma);
-        if (absLength < AliTRDgeometry::MyThick()) {
-          continue;
-       }
-      }
-      else {
+    // Take the absorbtion in the entrance window into account
+    Double_t muMy = fTR->GetMuMy(energyMeV);
+    sigma         = muMy * fFoilDensity;
+    if (sigma > 0.0) {
+      absLength = gRandom->Exp(1.0/sigma);
+      if (absLength < AliTRDgeometry::MyThick()) {
         continue;
       }
+    }
+    else {
+      continue;
+    }
 
-      // The absorbtion cross sections in the drift gas
-      // Gas-mixture (Xe/CO2)
-      Double_t muXe = fTR->GetMuXe(energyMeV);
-      Double_t muCO = fTR->GetMuCO(energyMeV);
-      sigma = (0.85 * muXe + 0.15 * muCO) * fGasDensity * fTR->GetTemp();
-
-      // The distance after which the energy of the TR photon
-      // is deposited.
-      if (sigma > 0.0) {
-        absLength = gRandom->Exp(1.0/sigma);
-        if (absLength > (AliTRDgeometry::DrThick()
-                       + AliTRDgeometry::AmThick())) {
-          continue;
-       }
-      }
-      else {
+    // The absorbtion cross sections in the drift gas
+    // Gas-mixture (Xe/CO2)
+    Double_t muXe = fTR->GetMuXe(energyMeV);
+    Double_t muCO = fTR->GetMuCO(energyMeV);
+    sigma = (0.85 * muXe + 0.15 * muCO) * fGasDensity * fTR->GetTemp();
+
+    // The distance after which the energy of the TR photon
+    // is deposited.
+    if (sigma > 0.0) {
+      absLength = gRandom->Exp(1.0/sigma);
+      if (absLength > (AliTRDgeometry::DrThick()
+                     + AliTRDgeometry::AmThick())) {
         continue;
       }
-
-      // The position of the absorbtion
-      Float_t posHit[3];
-      gMC->TrackPosition(pos);
-      posHit[0] = pos[0] + mom[0] / pTot * absLength;
-      posHit[1] = pos[1] + mom[1] / pTot * absLength;
-      posHit[2] = pos[2] + mom[2] / pTot * absLength;
-
-      // Create the charge 
-      Int_t q = ((Int_t) (energyeV / kWion));
-
-      // Add the hit to the array. TR photon hits are marked 
-      // by negative charge
-      AddHit(gAlice->GetMCApp()->GetCurrentTrackNumber()
-            ,det
-            ,posHit
-            ,-q
-            ,kTRUE); 
-
     }
+    else {
+      continue;
+    }
+
+    // The position of the absorbtion
+    Float_t posHit[3];
+    gMC->TrackPosition(pos);
+    posHit[0] = pos[0] + mom[0] / pTot * absLength;
+    posHit[1] = pos[1] + mom[1] / pTot * absLength;
+    posHit[2] = pos[2] + mom[2] / pTot * absLength;
+
+    // Create the charge 
+    Int_t q = ((Int_t) (energyeV / kWion));
+
+    // Add the hit to the array. TR photon hits are marked 
+    // by negative charge
+    AddHit(gAlice->GetMCApp()->GetCurrentTrackNumber()
+          ,det
+          ,posHit
+          ,-q
+          ,gMC->TrackTime()*1.0e06
+          ,kTRUE);
 
   }
 
@@ -337,7 +366,7 @@ void AliTRDv1::Init()
     AliInfo("TR simulation off");
   }
   else {
-    fTR = new AliTRDsim();
+    fTR = new AliTRDsimTR();
   }
 
   // First ionization potential (eV) for the gas mixture (90% Xe + 10% CO2)
@@ -429,6 +458,10 @@ void AliTRDv1::StepManagerGeant()
   Bool_t   drRegion = kFALSE;
   Bool_t   amRegion = kFALSE;
 
+  TString  cIdPath;
+  Char_t   cIdSector[3];
+           cIdSector[2]  = 0;
+
   TString  cIdCurrent;
   TString  cIdSensDr = "J";
   TString  cIdSensAm = "K";
@@ -496,164 +529,153 @@ void AliTRDv1::StepManagerGeant()
       hits[1] = pos[1];
       hits[2] = pos[2];
 
-      // The sector number (0 - 17)
-      // The numbering goes clockwise and starts at y = 0
-      Float_t phi = kRaddeg*TMath::ATan2(pos[0],pos[1]);
-      if (phi < 90.0) {
-        phi = phi + 270.0;
-      }
-      else {
-        phi = phi -  90.0;
-      }
-      sec = ((Int_t) (phi / 20.0));
+      // The sector number (0 - 17), according to standard coordinate system
+      cIdPath      = gGeoManager->GetPath();
+      cIdSector[0] = cIdPath[21];
+      cIdSector[1] = cIdPath[22];
+      sec = atoi(cIdSector);
 
       // The plane and chamber number
       cIdChamber[0]   = cIdCurrent[2];
       cIdChamber[1]   = cIdCurrent[3];
       Int_t idChamber = (atoi(cIdChamber) % kNdetsec);
-      cha = kNcham - ((Int_t) idChamber / kNplan) - 1;
+      cha = ((Int_t) idChamber / kNplan);
       pla = ((Int_t) idChamber % kNplan);
 
-      // Check on selected volumes
-      Int_t addthishit = 1;
+      // The detector number
+      det = fGeometry->GetDetector(pla,cha,sec);
 
-      // Add this hit
-      if (addthishit) {
+      // Special hits only in the drift region
+      if      ((drRegion) &&
+               (gMC->IsTrackEntering())) {
 
-       // The detector number
-        det = fGeometry->GetDetector(pla,cha,sec);
+        // Create a track reference at the entrance of each
+        // chamber that contains the momentum components of the particle
+        gMC->TrackMomentum(mom);
+        AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
 
-       // Special hits only in the drift region
-        if (drRegion) {
+        // Create the hits from TR photons if electron/positron is
+        // entering the drift volume
+        if ((fTR) && 
+            (TMath::Abs(gMC->TrackPid()) == kPdgElectron)) {
+          CreateTRhit(det);    
+        }
 
-          // Create a track reference at the entrance and
-          // exit of each chamber that contain the
-         // momentum components of the particle
-          if (gMC->IsTrackEntering() || 
-              gMC->IsTrackExiting()) {
-            gMC->TrackMomentum(mom);
-            AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
-          }
+      }
+      else if ((amRegion) && 
+               (gMC->IsTrackExiting())) {
 
-         if (gMC->IsTrackEntering() && 
-              !gMC->IsNewTrack()) {
-           // determine if hit belong to primary track 
-           fPrimaryTrackPid = gAlice->GetMCApp()->GetCurrentTrackNumber();
-           // determine track length when entering the detector
-           fTrackLength0    = gMC->TrackLength();
-         }
-                                       
-         // Create the hits from TR photons
-          if (fTR) CreateTRhit(det);
+        // Create a track reference at the exit of each
+        // chamber that contains the momentum components of the particle
+        gMC->TrackMomentum(mom);
+        AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
 
-        }
+      }
 
-       // Calculate the energy of the delta-electrons
-       // modified by Alex Bercuci (A.Bercuci@gsi.de) on 26.01.06
-       // take into account correlation with the underlying GEANT tracking
-       // mechanism. see
-        // http://www-linux.gsi.de/~abercuci/Contributions/TRD/index.html
-       //
-       // determine the most significant process (last on the processes list)
-       // which caused this hit
-        gMC->StepProcesses(processes);
-        Int_t nofprocesses = processes.GetSize();
-        Int_t pid;
-       if (!nofprocesses) {
-          pid = 0;
-       }
-       else {
-          pid =        processes[nofprocesses-1];              
-       }               
+      // Calculate the energy of the delta-electrons
+      // modified by Alex Bercuci (A.Bercuci@gsi.de) on 26.01.06
+      // take into account correlation with the underlying GEANT tracking
+      // mechanism. see
+      // http://www-linux.gsi.de/~abercuci/Contributions/TRD/index.html
+      //
+      // determine the most significant process (last on the processes list)
+      // which caused this hit
+      gMC->StepProcesses(processes);
+      Int_t nofprocesses = processes.GetSize();
+      Int_t pid;
+      if (!nofprocesses) {
+        pid = 0;
+      }
+      else {
+        pid =  processes[nofprocesses-1];              
+      }                
                
-       // generate Edep according to GEANT parametrisation
-       eDelta = TMath::Exp(fDeltaG->GetRandom()) - kPoti;
-        eDelta = TMath::Max(eDelta,0.0);
-       Float_t prRange = 0.0;
-       Float_t range   = gMC->TrackLength() - fTrackLength0;
-       // merge GEANT tracker information with locally cooked one
-       if (gAlice->GetMCApp()->GetCurrentTrackNumber() == fPrimaryTrackPid) {
-         if      (pid == 27) { 
-           if (eDelta >= kECut) {                
-             prRange = kRa * eDelta * 0.001
-                      * (1.0 - kRb / (1.0 + kRc * eDelta * 0.001)) / kRho;
-              if (prRange >= (3.7 - range)) {
-                eDelta *= 0.1;
-             }
-           }
-         } 
-          else if (pid ==  1) {        
-           if (eDelta <  kECut) {
+      // Generate Edep according to GEANT parametrisation
+      eDelta = TMath::Exp(fDeltaG->GetRandom()) - kPoti;
+      eDelta = TMath::Max(eDelta,0.0);
+      Float_t prRange = 0.0;
+      Float_t range   = gMC->TrackLength() - fTrackLength0;
+      // merge GEANT tracker information with locally cooked one
+      if (gAlice->GetMCApp()->GetCurrentTrackNumber() == fPrimaryTrackPid) {
+       if      (pid == 27) { 
+          if (eDelta >= kECut) {                
+            prRange = kRa * eDelta * 0.001
+                    * (1.0 - kRb / (1.0 + kRc * eDelta * 0.001)) / kRho;
+            if (prRange >= (3.7 - range)) {
+              eDelta *= 0.1;
+            }
+          }
+        } 
+        else if (pid ==  1) {  
+          if (eDelta <  kECut) {
+            eDelta *= 0.5;
+          }
+          else {                
+            prRange = kRa * eDelta * 0.001
+                    * (1.0 - kRb / (1.0 + kRc * eDelta * 0.001)) / kRho;
+            if (prRange >= ((AliTRDgeometry::DrThick()
+                           + AliTRDgeometry::AmThick()) - range)) {
+              eDelta *= 0.05;
+            }
+            else {
               eDelta *= 0.5;
-           }
-           else {                
-             prRange = kRa * eDelta * 0.001
-                      * (1.0 - kRb / (1.0 + kRc * eDelta * 0.001)) / kRho;
-              if (prRange >= ((AliTRDgeometry::DrThick()
-                             + AliTRDgeometry::AmThick()) - range)) {
-                eDelta *= 0.05;
-             }
-             else {
-                eDelta *= 0.5;
-             }
-           }
-         } 
-          else {
-            eDelta = 0.0;
-         }     
-       } 
+            }
+          }
+        } 
         else {
           eDelta = 0.0;
-       }
+        }      
+      } 
+      else {
+        eDelta = 0.0;
+      }
 
-        // Generate the electron cluster size
-        if (eDelta == 0.0) {
-          qTot = 0;
-       }
-       else {
-          qTot = ((Int_t) (eDelta / kWion) + 1);
-       }
+      // Generate the electron cluster size
+      if (eDelta > 0.0) {
 
-       // Create a new dEdx hit
+        qTot = ((Int_t) (eDelta / kWion) + 1);
+
+        // Create a new dEdx hit
         AddHit(gAlice->GetMCApp()->GetCurrentTrackNumber()
               ,det
               ,hits
               ,qTot
+              ,gMC->TrackTime()*1.0e06
               ,drRegion);
-                               
-        // Calculate the maximum step size for the next tracking step
-       // Produce only one hit if Ekin is below cutoff
-        aMass = gMC->TrackMass();
-        if ((gMC->Etot() - aMass) > kEkinMinStep) {
-
-          // The energy loss according to Bethe Bloch
-          iPdg = TMath::Abs(gMC->TrackPid());
-          if ((iPdg != kPdgElectron) ||
-             ((iPdg == kPdgElectron) && 
-               (pTot  < kPTotMaxEl))) {
-            gMC->TrackMomentum(mom);
-            pTot      = mom.Rho();
-            betaGamma = pTot / aMass;
-            pp        = BetheBlochGeant(betaGamma);
-           // Take charge > 1 into account
-            charge     = gMC->TrackCharge();
-            if (TMath::Abs(charge) > 1) {
-              pp = pp * charge*charge;
-           }
-          } 
-          else { 
-            // Electrons above 20 Mev/c are at the plateau
-           pp = kPrim * kPlateau;
-          }
 
-         Int_t nsteps = 0;
-         do {
-            nsteps = gRandom->Poisson(pp);
-          } while(!nsteps);
-          stepSize = 1.0 / nsteps;
-         gMC->SetMaxStep(stepSize);
+      }
+                       
+      // Calculate the maximum step size for the next tracking step
+      // Produce only one hit if Ekin is below cutoff
+      aMass = gMC->TrackMass();
+      if ((gMC->Etot() - aMass) > kEkinMinStep) {
+
+        // The energy loss according to Bethe Bloch
+        iPdg = TMath::Abs(gMC->TrackPid());
+        if ((iPdg != kPdgElectron) ||
+           ((iPdg == kPdgElectron) && 
+             (pTot  < kPTotMaxEl))) {
+          gMC->TrackMomentum(mom);
+          pTot      = mom.Rho();
+          betaGamma = pTot / aMass;
+          pp        = BetheBlochGeant(betaGamma);
+         // Take charge > 1 into account
+          charge     = gMC->TrackCharge();
+          if (TMath::Abs(charge) > 1) {
+            pp = pp * charge*charge;
+          }
+        } 
+        else { 
+          // Electrons above 20 Mev/c are at the plateau
+          pp = kPrim * kPlateau;
+        }
 
-       }
+       Int_t nsteps = 0;
+        do {
+          nsteps = gRandom->Poisson(pp);
+        } while(!nsteps);
+        stepSize = 1.0 / nsteps;
+        gMC->SetMaxStep(stepSize);
 
       }
 
@@ -694,6 +716,10 @@ void AliTRDv1::StepManagerErmilova()
   Bool_t   drRegion = kFALSE;
   Bool_t   amRegion = kFALSE;
 
+  TString  cIdPath;
+  Char_t   cIdSector[3];
+           cIdSector[2]  = 0;
+
   TString  cIdCurrent;
   TString  cIdSensDr = "J";
   TString  cIdSensAm = "K";
@@ -750,61 +776,57 @@ void AliTRDv1::StepManagerErmilova()
       hits[1] = pos[1];
       hits[2] = pos[2];
 
-      // The sector number (0 - 17)
-      // The numbering goes clockwise and starts at y = 0
-      Float_t phi = kRaddeg*TMath::ATan2(pos[0],pos[1]);
-      if (phi < 90.0) { 
-        phi = phi + 270.0;
-      }
-      else {
-        phi = phi -  90.0;
-      }
-      sec = ((Int_t) (phi / 20.0));
+      // The sector number (0 - 17), according to standard coordinate system
+      cIdPath      = gGeoManager->GetPath();
+      cIdSector[0] = cIdPath[21];
+      cIdSector[1] = cIdPath[22];
+      sec = atoi(cIdSector);
 
       // The plane and chamber number
       cIdChamber[0] = cIdCurrent[2];
       cIdChamber[1] = cIdCurrent[3];
       Int_t idChamber = (atoi(cIdChamber) % kNdetsec);
-      cha = kNcham - ((Int_t) idChamber / kNplan) - 1;
+      cha = ((Int_t) idChamber / kNplan);
       pla = ((Int_t) idChamber % kNplan);
 
-      // Check on selected volumes
-      Int_t addthishit = 1;
+      // The detector number
+      det = fGeometry->GetDetector(pla,cha,sec);
 
-      // Add this hit
-      if (addthishit) {
+      // Special hits only in the drift region
+      if      ((drRegion) &&
+               (gMC->IsTrackEntering())) {
 
-       // The detector number
-        det = fGeometry->GetDetector(pla,cha,sec);
+        // Create a track reference at the entrance of each
+        // chamber that contains the momentum components of the particle
+        gMC->TrackMomentum(mom);
+        AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
 
-       // Special hits only in the drift region
-        if (drRegion) {
+        // Create the hits from TR photons if electron/positron is
+        // entering the drift volume
+        if ((fTR) && 
+            (TMath::Abs(gMC->TrackPid()) == kPdgElectron)) {
+          CreateTRhit(det);    
+        }
 
-          // Create a track reference at the entrance and
-          // exit of each chamber that contain the 
-         // momentum components of the particle
-          if (gMC->IsTrackEntering() || 
-              gMC->IsTrackExiting()) {
-            gMC->TrackMomentum(mom);
-            AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
-          }
-          // Create the hits from TR photons
-          if (fTR) {
-            CreateTRhit(det);
-         }
+      }
+      else if ((amRegion) && 
+               (gMC->IsTrackExiting())) {
 
-       }
+        // Create a track reference at the exit of each
+        // chamber that contains the momentum components of the particle
+        gMC->TrackMomentum(mom);
+        AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
 
-        // Calculate the energy of the delta-electrons
-        eDelta = TMath::Exp(fDeltaE->GetRandom()) - kPoti;
-        eDelta = TMath::Max(eDelta,0.0);
-        // Generate the electron cluster size
-        if (eDelta == 0.0) {
-          qTot = 0;
-       }
-       else {
-          qTot = ((Int_t) (eDelta / kWion) + 1);
-       }
+      }
+
+      // Calculate the energy of the delta-electrons
+      eDelta = TMath::Exp(fDeltaE->GetRandom()) - kPoti;
+      eDelta = TMath::Max(eDelta,0.0);
+
+      // Generate the electron cluster size
+      if (eDelta > 0.0) {
+
+        qTot = ((Int_t) (eDelta / kWion) + 1);
 
        // Create a new dEdx hit
         if (drRegion) {
@@ -812,6 +834,7 @@ void AliTRDv1::StepManagerErmilova()
                 ,det
                 ,hits
                 ,qTot
+                ,gMC->TrackTime()*1.0e06
                 ,kTRUE);
        }
         else {
@@ -819,45 +842,46 @@ void AliTRDv1::StepManagerErmilova()
                 ,det
                 ,hits
                 ,qTot
+                ,gMC->TrackTime()*1.0e06
                 ,kFALSE);
        }
 
-        // Calculate the maximum step size for the next tracking step
-       // Produce only one hit if Ekin is below cutoff 
-        aMass = gMC->TrackMass();
-        if ((gMC->Etot() - aMass) > kEkinMinStep) {
-
-          // The energy loss according to Bethe Bloch
-          iPdg  = TMath::Abs(gMC->TrackPid());
-          if ((iPdg != kPdgElectron) ||
-             ((iPdg == kPdgElectron) && 
-               (pTot  < kPTotMaxEl))) {
-            gMC->TrackMomentum(mom);
-            pTot      = mom.Rho();
-            betaGamma = pTot / aMass;
-            pp        = kPrim * BetheBloch(betaGamma);
-           // Take charge > 1 into account
-            charge = gMC->TrackCharge();
-            if (TMath::Abs(charge) > 1) {
-              pp = pp * charge*charge;
-           }
-          } 
-          else { 
-            // Electrons above 20 Mev/c are at the plateau
-           pp = kPrim * kPlateau;
+      }
+
+      // Calculate the maximum step size for the next tracking step
+      // Produce only one hit if Ekin is below cutoff 
+      aMass = gMC->TrackMass();
+      if ((gMC->Etot() - aMass) > kEkinMinStep) {
+
+        // The energy loss according to Bethe Bloch
+        iPdg  = TMath::Abs(gMC->TrackPid());
+        if ((iPdg != kPdgElectron) ||
+           ((iPdg == kPdgElectron) && 
+             (pTot  < kPTotMaxEl))) {
+          gMC->TrackMomentum(mom);
+          pTot      = mom.Rho();
+          betaGamma = pTot / aMass;
+          pp        = kPrim * BetheBloch(betaGamma);
+          // Take charge > 1 into account
+          charge = gMC->TrackCharge();
+          if (TMath::Abs(charge) > 1) {
+            pp = pp * charge*charge;
           }
+        } 
+        else { 
+          // Electrons above 20 Mev/c are at the plateau
+         pp = kPrim * kPlateau;
+        }
       
-          if (pp > 0.0) {
-            do {
-              gMC->GetRandom()->RndmArray(1,random);
-           }
-            while ((random[0] == 1.0) || 
-                   (random[0] == 0.0));
-            stepSize = - TMath::Log(random[0]) / pp; 
-            gMC->SetMaxStep(stepSize);
+        if (pp > 0.0) {
+          do {
+            gMC->GetRandom()->RndmArray(1,random);
          }
-
-       }
+          while ((random[0] == 1.0) || 
+                 (random[0] == 0.0));
+          stepSize = - TMath::Log(random[0]) / pp; 
+          gMC->SetMaxStep(stepSize);
+        }
 
       }
 
@@ -876,6 +900,9 @@ void AliTRDv1::StepManagerFixedStep()
   // this version of the step manager.
   //
 
+  // PDG code electron
+  const Int_t   kPdgElectron = 11;
+
   Int_t    pla = 0;
   Int_t    cha = 0;
   Int_t    sec = 0;
@@ -888,11 +915,15 @@ void AliTRDv1::StepManagerFixedStep()
   Bool_t   drRegion = kFALSE;
   Bool_t   amRegion = kFALSE;
 
+  TString  cIdPath;
+  Char_t   cIdSector[3];
+           cIdSector[2]  = 0;
+
   TString  cIdCurrent;
   TString  cIdSensDr = "J";
   TString  cIdSensAm = "K";
   Char_t   cIdChamber[3];
-  cIdChamber[2] = 0;
+           cIdChamber[2] = 0;
 
   TLorentzVector pos;
   TLorentzVector mom;
@@ -912,13 +943,19 @@ void AliTRDv1::StepManagerFixedStep()
 
   // If not charged track or already stopped or disappeared, just return.
   if ((!gMC->TrackCharge()) || 
-        gMC->IsTrackDisappeared()) return;
+        gMC->IsTrackDisappeared()) {
+    return;
+  }
 
   // Inside a sensitive volume?
   cIdCurrent = gMC->CurrentVolName();
 
-  if (cIdSensDr == cIdCurrent[1]) drRegion = kTRUE;
-  if (cIdSensAm == cIdCurrent[1]) amRegion = kTRUE;
+  if (cIdSensDr == cIdCurrent[1]) {
+    drRegion = kTRUE;
+  }
+  if (cIdSensAm == cIdCurrent[1]) {
+    amRegion = kTRUE;
+  }
 
   if ((!drRegion) && 
       (!amRegion)) {
@@ -931,31 +968,19 @@ void AliTRDv1::StepManagerFixedStep()
   hits[1] = pos[1];
   hits[2] = pos[2];
 
-  // The sector number (0 - 17)
-  // The numbering goes clockwise and starts at y = 0
-  Float_t phi = kRaddeg*TMath::ATan2(pos[0],pos[1]);
-  if (phi < 90.0) {
-    phi = phi + 270.0;
-  }
-  else {          
-    phi = phi -  90.0;
-  }
-  sec = ((Int_t) (phi / 20.0));
+  // The sector number (0 - 17), according to standard coordinate system
+  cIdPath      = gGeoManager->GetPath();
+  cIdSector[0] = cIdPath[21];
+  cIdSector[1] = cIdPath[22];
+  sec = atoi(cIdSector);
 
   // The plane and chamber number
   cIdChamber[0]   = cIdCurrent[2];
   cIdChamber[1]   = cIdCurrent[3];
   Int_t idChamber = (atoi(cIdChamber) % kNdetsec);
-  cha = kNcham - ((Int_t) idChamber / kNplan) - 1;
+  cha = ((Int_t) idChamber / kNplan);
   pla = ((Int_t) idChamber % kNplan);
 
-  // Check on selected volumes
-  Int_t addthishit = 1;
-
-  if (!addthishit) {
-    return;
-  }
-
   // The detector number
   det = fGeometry->GetDetector(pla,cha,sec);
 
@@ -963,38 +988,47 @@ void AliTRDv1::StepManagerFixedStep()
   Int_t trkStat = 0;
 
   // Special hits only in the drift region
-  if (drRegion) {
+  if      ((drRegion) &&
+           (gMC->IsTrackEntering())) {
 
-    // Create a track reference at the entrance and exit of each
-    // chamber that contain the momentum components of the particle
-
-    if (gMC->IsTrackEntering()) {
-      gMC->TrackMomentum(mom);
-      AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
-      trkStat = 1;
-    }
-    if (gMC->IsTrackExiting()) {
-      gMC->TrackMomentum(mom);
-      AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
-      trkStat = 2;
-    }
+    // Create a track reference at the entrance of each
+    // chamber that contains the momentum components of the particle
+    gMC->TrackMomentum(mom);
+    AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
+    trkStat = 1;
 
-    // Create the hits from TR photons
-    if (fTR) {
+    // Create the hits from TR photons if electron/positron is
+    // entering the drift volume
+    if ((fTR) && 
+        (TMath::Abs(gMC->TrackPid()) == kPdgElectron)) {
       CreateTRhit(det);    
     }
 
+  }
+  else if ((amRegion) && 
+           (gMC->IsTrackExiting())) {
+
+    // Create a track reference at the exit of each
+    // chamber that contains the momentum components of the particle
+    gMC->TrackMomentum(mom);
+    AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kTRD);
+    trkStat = 2;
+
   }
   
   // Calculate the charge according to GEANT Edep
   // Create a new dEdx hit
   eDep = TMath::Max(gMC->Edep(),0.0) * 1.0e+09;
   qTot = (Int_t) (eDep / kWion);
-  AddHit(gAlice->GetMCApp()->GetCurrentTrackNumber()
-        ,det
-        ,hits
-        ,qTot
-        ,drRegion);
+  if ((qTot) ||
+      (trkStat)) {
+    AddHit(gAlice->GetMCApp()->GetCurrentTrackNumber()
+          ,det
+          ,hits
+          ,qTot
+          ,gMC->TrackTime()*1.0e06
+          ,drRegion);
+  }
 
   // Set Maximum Step Size
   // Produce only one hit if Ekin is below cutoff
@@ -1193,6 +1227,10 @@ Double_t IntSpecGeant(Double_t *x, Double_t *)
   Int_t    i;
   Double_t energy = x[0];
 
+  if (energy >= arre[npts-1]) {
+    return 0.0;
+  }
+
   for (i = 0; i < npts; i++) {
     if (energy < arre[i]) {
       break;