]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TFluka/TFluka.cxx
Support for user defined primary ions.
[u/mrichter/AliRoot.git] / TFluka / TFluka.cxx
index cde76615b8697c1a4226999c5f294e8106cb485f..a02c34f93606725f5a3586a578d021eb7dd1ebfb 100644 (file)
@@ -33,6 +33,7 @@
 #include <TList.h>
 
 #include "TFluka.h"
+#include "TFlukaIon.h"
 #include "TFlukaCodes.h"
 #include "TCallf77.h"      //For the fortran calls
 #include "Fdblprc.h"       //(DBLPRC) fluka common
@@ -129,16 +130,19 @@ TFluka::TFluka()
    fXsco(0),
    fYsco(0),
    fZsco(0),
+   fPItime(0),
+   fPIlength(0),
+   fNPI(0),
    fTrackIsEntering(kFALSE),
    fTrackIsExiting(kFALSE),
    fTrackIsNew(kFALSE),
    fFieldFlag(kTRUE),
-   fGeneratePemf(kFALSE),
    fDummyBoundary(kFALSE),
    fStopped(kFALSE),
    fStopEvent(kFALSE),
    fStopRun(kFALSE),
    fPrimaryElectronIndex(-1),
+   fLowEnergyNeutronTransport(kFALSE),
    fMaterials(0),
    fNVolumes(0),
    fCurrentFlukaRegion(-1),
@@ -146,11 +150,13 @@ TFluka::TFluka()
    fGeom(0),
    fMCGeo(0),
    fUserConfig(0), 
-   fUserScore(0)
+   fUserScore(0),
+   fUserIon(0)
 { 
   //
   // Default constructor
   //
+    for (Int_t i = 0; i < 4; i++) fPint[i] = 0.;
 } 
  
 //______________________________________________________________________________ 
@@ -167,16 +173,19 @@ TFluka::TFluka(const char *title, Int_t verbosity, Bool_t isRootGeometrySupporte
    fXsco(0),
    fYsco(0),
    fZsco(0),
+   fPItime(0),
+   fPIlength(0),
+   fNPI(0),
    fTrackIsEntering(kFALSE),
    fTrackIsExiting(kFALSE),
    fTrackIsNew(kFALSE),
    fFieldFlag(kTRUE),
-   fGeneratePemf(kFALSE),
    fDummyBoundary(kFALSE),
    fStopped(kFALSE),
    fStopEvent(kFALSE),
    fStopRun(kFALSE),
    fPrimaryElectronIndex(-1),
+   fLowEnergyNeutronTransport(kFALSE),
    fMaterials(0),
    fNVolumes(0),
    fCurrentFlukaRegion(-1),
@@ -184,9 +193,12 @@ TFluka::TFluka(const char *title, Int_t verbosity, Bool_t isRootGeometrySupporte
    fGeom(0),
    fMCGeo(0),
    fUserConfig(new TObjArray(100)),
-   fUserScore(new TObjArray(100)) 
+   fUserScore(new TObjArray(100)),
+   fUserIon(0)
 {
   // create geometry interface
+    for (Int_t i = 0; i < 4; i++) fPint[i] = 0.;
+    
    if (fVerbosityLevel >=3)
        cout << "<== TFluka::TFluka(" << title << ") constructor called." << endl;
    SetCoreInputFileName();
@@ -205,8 +217,8 @@ TFluka::~TFluka()
         cout << "<== TFluka::~TFluka() destructor called." << endl;
     if (fMaterials) delete [] fMaterials;
     
-    delete fGeom;
-    delete fMCGeo;
+//    delete fGeom;
+//    delete fMCGeo;
     
     if (fUserConfig) {
         fUserConfig->Delete();
@@ -255,9 +267,7 @@ void TFluka::Init() {
     // Add ions to PDG Data base
     //
      AddParticlesToPdgDataBase();
-     //
-
-     
+    //
 }
 
 
@@ -299,13 +309,7 @@ void TFluka::BuildPhysics() {
         }
     }
     
-    //
-    // At this stage we have the information on materials and cuts available.
-    // Now create the pemf file
-    
-    if (fGeneratePemf) fGeom->CreatePemfFile();
-    
-    //
+
     // Prepare input file with the current physics settings
     
     InitPhysics(); 
@@ -356,7 +360,7 @@ Bool_t TFluka::ProcessRun(Int_t nevent) {
 //
 // Run steering
 //
-
+    
   if (fVerbosityLevel >=3)
     cout << "==> TFluka::ProcessRun(" << nevent << ") called." 
          << endl;
@@ -598,7 +602,7 @@ void TFluka::Mixture(Int_t& kmat, const char *name, Double_t *a,
      delete [] wmatnew;
      return;     
    }
-   gGeoManager->Mixture(name, a, z, dens, nlmat, wmat, kmat);
+  gGeoManager->Mixture(name, a, z, dens, nlmat, wmat, kmat);
 } 
 
 //______________________________________________________________________________ 
@@ -655,7 +659,8 @@ void TFluka::Gstpar(Int_t itmed, const char* param, Double_t parval) {
        strncmp(param, "HADR",  4) == 0 ||
        strncmp(param, "LOSS",  4) == 0 ||
        strncmp(param, "MULS",  4) == 0 ||
-       strncmp(param, "RAYL",  4) == 0) 
+       strncmp(param, "RAYL",  4) == 0 ||
+       strncmp(param, "STRA",  4) == 0) 
    {
        process = kTRUE;
    } 
@@ -887,7 +892,8 @@ void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Float_t* ppckov,
 //
 //  
 //  Create object holding Cerenkov properties
-//  
+// 
+    
     TFlukaCerenkov* cerenkovProperties = new TFlukaCerenkov(npckov, ppckov, absco, effic, rindex);
 //
 //  Pass object to medium
@@ -919,16 +925,61 @@ void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Float_t* ppckov,
 
 
 //______________________________________________________________________________ 
-void TFluka::SetCerenkov(Int_t /*itmed*/, Int_t /*npckov*/, Double_t * /*ppckov*/,
-                         Double_t * /*absco*/, Double_t * /*effic*/, Double_t * /*rindex*/) {
+void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Double_t *ppckov,
+                         Double_t *absco, Double_t *effic, Double_t *rindex) {
+//
+// Set Cerenkov properties for medium itmed
+//
+// npckov: number of sampling points
+// ppckov: energy values
+// absco:  absorption length
+// effic:  quantum efficiency
+// rindex: refraction index
 //
-//  Double_t version not implemented
+
+//
+//  Double_t version 
+  Float_t* fppckov = CreateFloatArray(ppckov, npckov);
+  Float_t* fabsco  = CreateFloatArray(absco,  npckov);
+  Float_t* feffic  = CreateFloatArray(effic,  npckov);
+  Float_t* frindex = CreateFloatArray(rindex, npckov);
+
+  SetCerenkov(itmed, npckov, fppckov, fabsco, feffic, frindex);
+
+  delete [] fppckov;
+  delete [] fabsco;
+  delete [] feffic;
+  delete [] frindex;
 }  
 
-void TFluka::SetCerenkov(Int_t /*itmed*/, Int_t /*npckov*/, Double_t* /*ppckov*/,
-                         Double_t* /*absco*/, Double_t* /*effic*/, Double_t* /*rindex*/, Double_t* /*rfl*/) {
+void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Double_t* ppckov,
+                         Double_t* absco, Double_t* effic, Double_t* rindex, Double_t* rfl) {
+//
+// Set Cerenkov properties for medium itmed
+//
+// npckov: number of sampling points
+// ppckov: energy values
+// absco:  absorption length
+// effic:  quantum efficiency
+// rindex: refraction index
+// rfl:    reflectivity for boundary to medium itmed
+//
+
 //
-// //  Double_t version not implemented
+// //  Double_t version 
+  Float_t* fppckov = CreateFloatArray(ppckov, npckov);
+  Float_t* fabsco  = CreateFloatArray(absco,  npckov);
+  Float_t* feffic  = CreateFloatArray(effic,  npckov);
+  Float_t* frindex = CreateFloatArray(rindex, npckov);
+  Float_t* frfl    = CreateFloatArray(rfl,    npckov);
+
+  SetCerenkov(itmed, npckov, fppckov, fabsco, feffic, frindex, frfl);
+
+  delete [] fppckov;
+  delete [] fabsco;
+  delete [] feffic;
+  delete [] frindex;
+  delete [] frfl;
 }
 
 // Euclid
@@ -950,7 +1001,11 @@ Int_t TFluka::GetMedium() const {
 //
 //  Get the medium number for the current fluka region
 //
-    return fGeom->GetMedium(); // this I need to check due to remapping !!!
+    if (gGeoManager->IsOutside()) {
+       return (-1);
+    } else {
+       return (fGeom->GetMedium()); // this I need to check due to remapping !!!
+    }
 }
 
 //____________________________________________________________________________ 
@@ -973,11 +1028,24 @@ Int_t TFluka::GetDummyLattice() const
 //_____________________________________________________________________________
 Int_t TFluka::IdFromPDG(Int_t pdg) const 
 {
+
     //
     // Return Fluka code from PDG and pseudo ENDF code
-    
+    Int_t idSpecial[4] = {TFlukaIon::GetIonPdg(2,4), 
+                         TFlukaIon::GetIonPdg(2,3), 
+                         TFlukaIon::GetIonPdg(1,3), 
+                         TFlukaIon::GetIonPdg(1,2)};    
     // Catch the feedback photons
     if (pdg == 50000051) return (kFLUKAoptical);
+    // Ion as primary
+    for (Int_t i = 0; i < 4; i++) {
+       if (pdg == idSpecial[i]) return (i + kFLUKAcodemin);
+    }
+    
+    if ((!fUserIon && pdg == TFlukaIon::GetIonPdg(6,12)) ||
+       ( fUserIon && pdg == fUserIon->GetPdgCode())) 
+       return (-2);
+
     // MCIHAD() goes from pdg to fluka internal.
     Int_t intfluka = mcihad(pdg);
     // KPTOIP array goes from internal to official
@@ -989,8 +1057,12 @@ Int_t TFluka::PDGFromId(Int_t id) const
 {
   //
   // Return PDG code and pseudo ENDF code from Fluka code
-  //                      Alpha     He3       Triton    Deuteron  gen. ion  opt. photon   
-    Int_t idSpecial[6] = {GetIonPdg(2,4), GetIonPdg(2, 3), GetIonPdg(1,3), GetIonPdg(1,2), GetIonPdg(0,0), 50000050};
+    Int_t idSpecial[6] = {TFlukaIon::GetIonPdg(2,4), // alpha 
+                         TFlukaIon::GetIonPdg(2,3), // He3
+                         TFlukaIon::GetIonPdg(1,3), // triton
+                         TFlukaIon::GetIonPdg(1,2), // deuteron 
+                         TFlukaIon::GetIonPdg(0,0), // gen. ion 
+                         50000050};
   // IPTOKP array goes from official to internal
 
     if (id == kFLUKAoptical) {
@@ -1157,11 +1229,9 @@ void TFluka::InitPhysics()
 //
 // Construct file names
     FILE *pFlukaVmcCoreInp, *pFlukaVmcFlukaMat, *pFlukaVmcInp;
-    TString sFlukaVmcCoreInp = getenv("ALICE_ROOT");
-    sFlukaVmcCoreInp +="/TFluka/input/";
     TString sFlukaVmcTmp = "flukaMat.inp";
     TString sFlukaVmcInp = GetInputFileName();
-    sFlukaVmcCoreInp += GetCoreInputFileName();
+    TString sFlukaVmcCoreInp = GetCoreInputFileName();
     
 // Open files 
     if ((pFlukaVmcCoreInp = fopen(sFlukaVmcCoreInp.Data(),"r")) == NULL) {
@@ -1259,6 +1329,8 @@ void TFluka::InitPhysics()
 
 // Add RANDOMIZ card
     fprintf(pFlukaVmcInp,"RANDOMIZ  %10.1f%10.0f\n", 1., Float_t(gRandom->GetSeed()));
+// User defined ion
+    if (fUserIon) fUserIon->WriteUserInputCard(pFlukaVmcInp);
 // Add START and STOP card
     fprintf(pFlukaVmcInp,"START     %10.1f\n",fEventsPerRun);
     fprintf(pFlukaVmcInp,"STOP      \n");
@@ -1274,7 +1346,7 @@ void TFluka::InitPhysics()
 // Initialisation needed for Cerenkov photon production and transport
     TObjArray *matList = GetFlukaMaterials();
     Int_t nmaterial =  matList->GetEntriesFast();
-    fMaterials = new Int_t[nmaterial+3];
+    fMaterials = new Int_t[nmaterial+25];
     
     for (Int_t im = 0; im < nmaterial; im++)
     {
@@ -1293,6 +1365,8 @@ void TFluka::SetMaxStep(Double_t step)
     
 //    Int_t mreg=0, latt=0;
 //    fGeom->GetCurrentRegion(mreg, latt);
+
+    
     Int_t mreg = fGeom->GetCurrentRegion();
     STEPSZ.stepmx[mreg - 1] = step;
 }
@@ -1338,21 +1412,21 @@ void TFluka::TrackPosition(TLorentzVector& position) const
   if (caller == kENDRAW    || caller == kUSDRAW || 
       caller == kBXExiting || caller == kBXEntering || 
       caller == kUSTCKV) { 
-    position.SetX(GetXsco());
-    position.SetY(GetYsco());
-    position.SetZ(GetZsco());
-    position.SetT(TRACKR.atrack);
+      position.SetX(GetXsco());
+      position.SetY(GetYsco());
+      position.SetZ(GetZsco());
+      position.SetT(TRACKR.atrack);
   }
   else if (caller == kMGDRAW) {
       Int_t i = -1;
       if ((i = fPrimaryElectronIndex) > -1) {
          // Primary Electron Ionisation
-         Double_t x, y, z;
-         GetPrimaryElectronPosition(i, x, y, z);
+         Double_t x, y, z, t;
+         GetPrimaryElectronPosition(i, x, y, z, t);
          position.SetX(x);
          position.SetY(y);
          position.SetZ(z);
-         position.SetT(TRACKR.atrack);
+         position.SetT(t);
       } else {
          position.SetX(TRACKR.xtrack[TRACKR.ntrack]);
          position.SetY(TRACKR.ytrack[TRACKR.ntrack]);
@@ -1361,18 +1435,19 @@ void TFluka::TrackPosition(TLorentzVector& position) const
       }
   }
   else if (caller == kSODRAW) { 
-    position.SetX(TRACKR.xtrack[TRACKR.ntrack]);
-    position.SetY(TRACKR.ytrack[TRACKR.ntrack]);
-    position.SetZ(TRACKR.ztrack[TRACKR.ntrack]);
-    position.SetT(0);
+      Int_t ist = FLKSTK.npflka;
+      position.SetX(FLKSTK.xflk[ist]);
+      position.SetY(FLKSTK.yflk[ist]);
+      position.SetZ(FLKSTK.zflk[ist]);
+      position.SetT(FLKSTK.agestk[ist]);
   } else if (caller == kMGResumedTrack) { 
-    position.SetX(TRACKR.spausr[0]);
-    position.SetY(TRACKR.spausr[1]);
-    position.SetZ(TRACKR.spausr[2]);
-    position.SetT(TRACKR.spausr[3]);
+      position.SetX(TRACKR.spausr[0]);
+      position.SetY(TRACKR.spausr[1]);
+      position.SetZ(TRACKR.spausr[2]);
+      position.SetT(TRACKR.spausr[3]);
   }
   else
-    Warning("TrackPosition","position not available");
+      Warning("TrackPosition","position not available");
 }
 
 //______________________________________________________________________________ 
@@ -1392,23 +1467,30 @@ void TFluka::TrackPosition(Double_t& x, Double_t& y, Double_t& z) const
       y = GetYsco();
       z = GetZsco();
   }
-  else if (caller == kMGDRAW || caller == kSODRAW) { 
+  else if (caller == kMGDRAW) { 
       Int_t i = -1;
       if ((i = fPrimaryElectronIndex) > -1) {
-         GetPrimaryElectronPosition(i, x, y, z);
+         Double_t t;
+         GetPrimaryElectronPosition(i, x, y, z, t);
       } else {
          x = TRACKR.xtrack[TRACKR.ntrack];
          y = TRACKR.ytrack[TRACKR.ntrack];
          z = TRACKR.ztrack[TRACKR.ntrack];
       }
   }
+  else if (caller == kSODRAW) { 
+      Int_t ist = FLKSTK.npflka;
+      x = FLKSTK.xflk[ist];
+      y = FLKSTK.yflk[ist];
+      z = FLKSTK.zflk[ist];
+  }
   else if (caller == kMGResumedTrack) {
-    x = TRACKR.spausr[0];
-    y = TRACKR.spausr[1];
-    z = TRACKR.spausr[2];
+      x = TRACKR.spausr[0];
+      y = TRACKR.spausr[1];
+      z = TRACKR.spausr[2];
   }
   else
-    Warning("TrackPosition","position not available");
+      Warning("TrackPosition","position not available");
 }
 
 //______________________________________________________________________________ 
@@ -1425,34 +1507,74 @@ void TFluka::TrackMomentum(TLorentzVector& momentum) const
   FlukaCallerCode_t  caller = GetCaller();
   FlukaProcessCode_t icode  = GetIcode();
   
-  if (caller != kEEDRAW && caller != kMGResumedTrack && 
+  if (caller  != kEEDRAW         && 
+      caller  != kMGResumedTrack && 
+      caller  != kSODRAW         &&
+      caller  != kUSDRAW         &&
       (caller != kENDRAW || (icode != kEMFSCOstopping1 && icode != kEMFSCOstopping2))) {
-    if (TRACKR.ptrack >= 0) {
-      momentum.SetPx(TRACKR.ptrack*TRACKR.cxtrck);
-      momentum.SetPy(TRACKR.ptrack*TRACKR.cytrck);
-      momentum.SetPz(TRACKR.ptrack*TRACKR.cztrck);
-      momentum.SetE(TRACKR.etrack);
-      return;
-    }
-    else {
-      Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack - ParticleMassFPC(TRACKR.jtrack) * ParticleMassFPC(TRACKR.jtrack));
-      momentum.SetPx(p*TRACKR.cxtrck);
-      momentum.SetPy(p*TRACKR.cytrck);
-      momentum.SetPz(p*TRACKR.cztrck);
-      momentum.SetE(TRACKR.etrack);
-      return;
-    }
+      if (TRACKR.ptrack >= 0) {
+         momentum.SetPx(TRACKR.ptrack*TRACKR.cxtrck);
+         momentum.SetPy(TRACKR.ptrack*TRACKR.cytrck);
+         momentum.SetPz(TRACKR.ptrack*TRACKR.cztrck);
+         momentum.SetE(TRACKR.etrack);
+         return;
+      }
+      else {
+         Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack - ParticleMassFPC(TRACKR.jtrack) * ParticleMassFPC(TRACKR.jtrack));
+         momentum.SetPx(p*TRACKR.cxtrck);
+         momentum.SetPy(p*TRACKR.cytrck);
+         momentum.SetPz(p*TRACKR.cztrck);
+         momentum.SetE(TRACKR.etrack);
+         return;
+      }
   } else if  (caller == kMGResumedTrack) {
-    momentum.SetPx(TRACKR.spausr[4]);
-    momentum.SetPy(TRACKR.spausr[5]);
-    momentum.SetPz(TRACKR.spausr[6]);
-    momentum.SetE (TRACKR.spausr[7]);
-    return;
+      momentum.SetPx(TRACKR.spausr[4]);
+      momentum.SetPy(TRACKR.spausr[5]);
+      momentum.SetPz(TRACKR.spausr[6]);
+      momentum.SetE (TRACKR.spausr[7]);
+      return;
   } else if (caller == kENDRAW && (icode == kEMFSCOstopping1 || icode == kEMFSCOstopping2)) {
       momentum.SetPx(0.);
       momentum.SetPy(0.);
       momentum.SetPz(0.);
       momentum.SetE(TrackMass());
+      
+  } else if (caller == kSODRAW) {
+      Int_t ist  = FLKSTK.npflka;
+      Double_t p = FLKSTK.pmoflk[ist];
+      Int_t ifl  = FLKSTK.iloflk[ist];
+      Double_t m = PAPROP.am[ifl + 6];
+      Double_t e = TMath::Sqrt(p * p + m * m);
+      momentum.SetPx(p * FLKSTK.txflk[ist]);
+      momentum.SetPy(p * FLKSTK.tyflk[ist]);
+      momentum.SetPz(p * FLKSTK.tzflk[ist]);
+      momentum.SetE(e);
+  } else if (caller == kUSDRAW) {
+      if (icode == kEMFSCObrems  || 
+         icode == kEMFSCOmoller || 
+         icode == kEMFSCObhabha || 
+         icode == kEMFSCOcompton ) 
+      {
+         momentum.SetPx(fPint[0]);
+         momentum.SetPy(fPint[1]);
+         momentum.SetPz(fPint[2]);
+         momentum.SetE(fPint[3]);
+      } else if (icode == kKASKADdray  || 
+                icode == kKASKADbrems || 
+                icode == kKASKADpair) {
+         momentum.SetPx(GENSTK.plr[0] * GENSTK.cxr[0]);
+         momentum.SetPy(GENSTK.plr[0] * GENSTK.cyr[0]);
+         momentum.SetPz(GENSTK.plr[0] * GENSTK.czr[0]);
+         momentum.SetE (GENSTK.tki[0] + PAPROP.am[GENSTK.kpart[0]+6]);
+      } else {
+         Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack 
+                           - ParticleMassFPC(TRACKR.jtrack) 
+                           * ParticleMassFPC(TRACKR.jtrack));
+         momentum.SetPx(p*TRACKR.cxtrck);
+         momentum.SetPy(p*TRACKR.cytrck);
+         momentum.SetPz(p*TRACKR.cztrck);
+         momentum.SetE(TRACKR.etrack);
+      }
   }
   else
     Warning("TrackMomentum","momentum not available");
@@ -1471,7 +1593,10 @@ void TFluka::TrackMomentum(Double_t& px, Double_t& py, Double_t& pz, Double_t& e
 // PAPROP.am[TRACKR.jtrack] = particle mass in gev
   FlukaCallerCode_t   caller = GetCaller();
   FlukaProcessCode_t  icode  = GetIcode();
-  if (caller != kEEDRAW && caller != kMGResumedTrack && 
+  if (caller != kEEDRAW         && 
+      caller != kMGResumedTrack && 
+      caller != kSODRAW         &&
+      caller != kUSDRAW         &&
       (caller != kENDRAW || (icode != kEMFSCOstopping1 && icode != kEMFSCOstopping2))) {
     if (TRACKR.ptrack >= 0) {
       px = TRACKR.ptrack*TRACKR.cxtrck;
@@ -1499,9 +1624,42 @@ void TFluka::TrackMomentum(Double_t& px, Double_t& py, Double_t& pz, Double_t& e
       py = 0.;
       pz = 0.;
       e  = TrackMass();
+  } else if (caller == kSODRAW) {
+      Int_t ist  = FLKSTK.npflka;
+      Double_t p = FLKSTK.pmoflk[ist];
+      Int_t ifl  = FLKSTK.iloflk[ist];
+      Double_t m = PAPROP.am[ifl + 6];
+               e = TMath::Sqrt(p * p + m * m);
+      px = p * FLKSTK.txflk[ist];
+      py = p * FLKSTK.tyflk[ist];
+      pz = p * FLKSTK.tzflk[ist];
+  } else if (caller == kUSDRAW) {
+      if (icode == kEMFSCObrems  || 
+         icode == kEMFSCOmoller || 
+         icode == kEMFSCObhabha || 
+         icode == kEMFSCOcompton ) 
+      {
+         px = fPint[0];
+         py = fPint[1];
+         pz = fPint[2];
+         e  = fPint[3];
+      } else if (icode == kKASKADdray  || 
+                icode == kKASKADbrems || 
+                icode == kKASKADpair) {
+         px = GENSTK.plr[0] * GENSTK.cxr[0];
+         py = GENSTK.plr[0] * GENSTK.cyr[0];
+         pz = GENSTK.plr[0] * GENSTK.czr[0];
+         e  = GENSTK.tki[0] + PAPROP.am[GENSTK.kpart[0]+6];
+      } else {
+         Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack - ParticleMassFPC(TRACKR.jtrack) *  ParticleMassFPC(TRACKR.jtrack));
+         px = p*TRACKR.cxtrck;
+         py = p*TRACKR.cytrck;
+         pz = p*TRACKR.cztrck;
+         e  = TRACKR.etrack;
+      }
   }
   else
-    Warning("TrackMomentum","momentum not available");
+      Warning("TrackMomentum","momentum not available");
 }
 
 //______________________________________________________________________________ 
@@ -1509,17 +1667,29 @@ Double_t TFluka::TrackStep() const
 {
 // Return the length in centimeters of the current step
 // TRACKR.ctrack = total curved path
-  FlukaCallerCode_t caller = GetCaller();
-  if (caller == kBXEntering || caller == kBXExiting || 
-      caller == kENDRAW     || caller == kUSDRAW || 
-      caller == kUSTCKV     || caller == kMGResumedTrack)
-    return 0.0;
-  else if (caller == kMGDRAW)
-    return TRACKR.ctrack;
-  else {
-    Warning("TrackStep", "track step not available");
-    return 0.0;
-  }  
+    FlukaCallerCode_t caller = GetCaller();
+    if (caller == kMGDRAW) {
+       Int_t i;
+       if ((i = fPrimaryElectronIndex) > -1) {
+           if (i > 0) {
+               return (fPIlength[i] - fPIlength[i-1]); 
+           } else {
+               Double_t s (TRACKR.ctrack - (fPIlength[fNPI - 1] - fPIlength[0]));
+               return s;
+           }
+       } else {
+           return TRACKR.ctrack;
+       }
+    } else if (caller == kBXEntering || caller == kBXExiting || 
+              caller == kENDRAW     || caller == kUSDRAW || 
+              caller == kUSTCKV     || caller == kMGResumedTrack ||
+              caller == kSODRAW)
+    {
+       return 0.0;
+    } else {
+       Warning("TrackStep", "track step not available");
+       return 0.0;
+    }  
 }
 
 //______________________________________________________________________________ 
@@ -1527,30 +1697,51 @@ Double_t TFluka::TrackLength() const
 {
 // TRACKR.cmtrck = cumulative curved path since particle birth
   FlukaCallerCode_t caller = GetCaller();
-  if (caller == kBXEntering || caller == kBXExiting || 
-      caller == kENDRAW || caller == kUSDRAW || caller == kMGDRAW || 
-      caller == kUSTCKV) 
-    return TRACKR.cmtrck;
+  if (caller == kMGDRAW) {
+      Int_t i;
+      if ((i = fPrimaryElectronIndex) > -1) {
+         return fPIlength[i];
+      } else {
+         return TRACKR.cmtrck;
+      }
+      
+  } else if (caller == kBXEntering || caller == kBXExiting || 
+            caller == kENDRAW || caller == kUSDRAW || caller == kUSTCKV) 
+      return TRACKR.cmtrck;
   else if (caller == kMGResumedTrack) 
-    return TRACKR.spausr[8];
+      return TRACKR.spausr[8];
+  else if (caller == kSODRAW)
+      return 0.0;
   else {
-    Warning("TrackLength", "track length not available");
-    return 0.0;
-  } 
+      Warning("TrackLength", "track length not available for caller %5d \n", caller);
+      return 0.0;
+  }
 }
 
+
 //______________________________________________________________________________ 
 Double_t TFluka::TrackTime() const
 {
 // Return the current time of flight of the track being transported
 // TRACKR.atrack = age of the particle
   FlukaCallerCode_t caller = GetCaller();
-  if (caller == kBXEntering || caller == kBXExiting || 
-      caller == kENDRAW     || caller == kUSDRAW    || caller == kMGDRAW || 
-      caller == kUSTCKV)
+  if (caller == kMGDRAW) {
+      Int_t i;
+      if ((i = fPrimaryElectronIndex) > -1) {
+         Double_t t = fPItime[i];
+         return t;
+      } else {
+         return TRACKR.atrack;
+      }
+  } else if (caller == kBXEntering || caller == kBXExiting || 
+            caller == kENDRAW     || caller == kUSDRAW    || 
+            caller == kUSTCKV)
     return TRACKR.atrack;
   else if (caller == kMGResumedTrack)
     return TRACKR.spausr[3];
+  else if (caller == kSODRAW) {
+      return (FLKSTK.agestk[FLKSTK.npflka]);
+  }
   else {
     Warning("TrackTime", "track time not available");
     return 0.0;
@@ -1576,7 +1767,9 @@ Double_t TFluka::Edep() const
   FlukaCallerCode_t caller = GetCaller();
     
   if (caller == kBXExiting || caller == kBXEntering || 
-      caller == kUSDRAW    || caller == kMGResumedTrack) return 0.0;
+      caller == kUSDRAW    || caller == kMGResumedTrack ||
+      caller == kSODRAW) 
+      return 0.0;
   Double_t sum = 0;
   Int_t i = -1;
   
@@ -1637,9 +1830,12 @@ Int_t TFluka::TrackPid() const
 // Return the id of the particle transported
 // TRACKR.jtrack = identity number of the particle
   FlukaCallerCode_t caller = GetCaller();
-  if (caller != kEEDRAW) {
+  if (caller != kEEDRAW && caller != kSODRAW) {
      return PDGFromId( CorrectFlukaId() );
   }
+  else if (caller == kSODRAW) {
+      return PDGFromId(FLKSTK.iloflk[FLKSTK.npflka]);
+  }
   else
     return -1000;
 }
@@ -1652,8 +1848,12 @@ Double_t TFluka::TrackCharge() const
 // TRACKR.jtrack = identity number of the particle
     
   FlukaCallerCode_t caller = GetCaller();
-  if (caller != kEEDRAW) 
-     return PAPROP.ichrge[CorrectFlukaId()+6];
+  if (caller != kEEDRAW && caller != kSODRAW) 
+     return PAPROP.ichrge[CorrectFlukaId() + 6];
+  else if (caller == kSODRAW) {
+      Int_t ifl =  PDGFromId(FLKSTK.iloflk[FLKSTK.npflka]);
+      return PAPROP.ichrge[ifl + 6];
+  }
   else
     return -1000.0;
 }
@@ -1664,8 +1864,12 @@ Double_t TFluka::TrackMass() const
 // PAPROP.am = particle mass in GeV
 // TRACKR.jtrack = identity number of the particle
   FlukaCallerCode_t caller = GetCaller();
-  if (caller != kEEDRAW)
+  if (caller != kEEDRAW && caller != kSODRAW)
      return PAPROP.am[CorrectFlukaId()+6];
+  else if (caller == kSODRAW) {
+      Int_t ifl =  FLKSTK.iloflk[FLKSTK.npflka];
+      return PAPROP.am[ifl + 6];
+  }
   else
     return -1000.0;
 }
@@ -1674,11 +1878,38 @@ Double_t TFluka::TrackMass() const
 Double_t TFluka::Etot() const
 {
 // TRACKR.etrack = total energy of the particle
-  FlukaCallerCode_t caller = GetCaller();
-  if (caller != kEEDRAW)
-    return TRACKR.etrack;
-  else
-    return -1000.0;
+  FlukaCallerCode_t  caller = GetCaller();
+  FlukaProcessCode_t icode  = GetIcode();
+  if (caller != kEEDRAW && caller != kSODRAW && caller != kUSDRAW)
+  {
+      return TRACKR.etrack;
+  } else if (caller == kUSDRAW) {
+      if (icode == kEMFSCObrems  || 
+         icode == kEMFSCOmoller || 
+         icode == kEMFSCObhabha || 
+         icode == kEMFSCOcompton ) {
+         return  fPint[3];
+      }
+      else if (icode == kKASKADdray  || 
+              icode == kKASKADbrems || 
+              icode == kKASKADpair) {
+         return (GENSTK.tki[0] + PAPROP.am[GENSTK.kpart[0]+6]);      
+      } else {
+         return TRACKR.etrack;
+      }
+      
+  }
+  else if (caller == kSODRAW) {
+      Int_t ist  = FLKSTK.npflka;
+      Double_t p = FLKSTK.pmoflk[ist];
+      Int_t ifl  = FLKSTK.iloflk[ist];
+      Double_t m = PAPROP.am[ifl + 6];
+      Double_t e = TMath::Sqrt(p * p + m * m);
+      return e;
+  }
+  printf("Etot %5d %5d \n", caller, icode);
+  
+  return -1000.0;
 }
 
 //
@@ -1798,9 +2029,25 @@ Bool_t   TFluka::IsTrackStop() const
 //______________________________________________________________________________ 
 Bool_t   TFluka::IsTrackAlive() const
 {
-// means not disappeared or not out
-  if (IsTrackDisappeared() || IsTrackOut() ) return 0;
-  else return 1;
+// Means not disappeared or not out
+    FlukaProcessCode_t icode = GetIcode();
+    
+    if (IsTrackOut()               || 
+       IsTrackStop()              ||
+       icode == kKASKADinelint    || // inelastic interaction
+       icode == kKASKADdecay      || // particle decay
+       icode == kEMFSCOanniflight || // in-flight annihilation
+       icode == kEMFSCOannirest   || // annihilation at rest
+       icode == kEMFSCOpair       || // pair production
+       icode == kEMFSCOphotoel    || // Photoelectric effect
+       icode == kKASNEUhadronic      // hadronic interaction
+       ) 
+    {
+       // Exclude the cases for which the particle has disappeared (paused) but will reappear later (= alive).
+       return 0;
+    } else {
+       return 1;
+    }
 }
 
 //
@@ -1919,39 +2166,78 @@ Int_t TFluka::StepProcesses(TArrayI &proc) const
   //
   // Return processes active in the current step
   //
-    FlukaProcessCode_t icode = GetIcode();
+    FlukaProcessCode_t icode   = GetIcode();
+    FlukaCallerCode_t  caller  = GetCaller();
     proc.Set(1);
     TMCProcess iproc;
-    switch (icode) {
-    case kKASKADtimekill:
-    case kEMFSCOtimekill:
-    case kKASNEUtimekill:
-    case kKASHEAtimekill:
-    case kKASOPHtimekill:
-        iproc =  kPTOFlimit;
-        break;
-    case kKASKADstopping:
-    case kKASKADescape:
-    case kEMFSCOstopping1:
-    case kEMFSCOstopping2:
-    case kEMFSCOescape:
-    case kKASNEUstopping:
-    case kKASNEUescape:
-    case kKASHEAescape:
-    case kKASOPHescape:
-        iproc = kPStop;
-        break;
-    case kKASOPHabsorption:
-        iproc = kPLightAbsorption;
-        break;
-    case kKASOPHrefraction:
-        iproc = kPLightRefraction;
-    case kEMFSCOlocaldep : 
-        iproc = kPPhotoelectric;
-        break;
-    default:
-        iproc = ProdProcess(0);
+    if (caller == kBXEntering || caller == kBXExiting || caller == kEEDRAW || caller == kSODRAW) {
+       iproc = kPTransportation;
+    }
+    else if (caller == kUSTCKV) {
+       iproc = kPCerenkov;
+    } else {
+       switch (icode) {
+       case kEMFSCO:
+           if (Edep() > 0.) {
+               iproc = kPEnergyLoss;
+           } else {
+               iproc = kPTransportation;
+           }
+           break;
+       case kKASKAD:
+           if (Edep() > 0.) {
+               iproc = kPEnergyLoss;
+           } else {
+               iproc = kPTransportation;
+           }
+           break;
+       case kKASHEA:
+       case kKASNEU:
+       case kKASOPH:
+       case kKASKADescape:
+       case kEMFSCOescape:
+       case kKASNEUescape:
+       case kKASHEAescape:
+       case kKASOPHescape:
+           iproc = kPTransportation;
+           break;
+       case kKASKADtimekill:
+       case kEMFSCOtimekill:
+       case kKASNEUtimekill:
+       case kKASHEAtimekill:
+       case kKASOPHtimekill:
+           iproc =  kPTOFlimit;
+           break;
+       case kKASKADstopping:
+       case kEMFSCOstopping1:
+       case kEMFSCOstopping2:
+       case kKASNEUstopping:
+           iproc = kPStop;
+           break; 
+       case kKASKADinelint:
+       case kKASNEUhadronic:
+           iproc = kPHadronic;
+           break;
+       case kKASKADinelarecoil:
+           iproc = kPHadronic;
+           break;
+       case kKASKADnelint:
+           iproc = kPHElastic;
+           break;
+       case kKASOPHabsorption:
+           iproc = kPLightAbsorption;
+           break;
+       case kKASOPHrefraction:
+           iproc = kPLightRefraction;
+           break;
+       case kEMFSCOlocaldep : 
+           iproc = kPPhotoelectric;
+           break;
+       default:
+           iproc = ProdProcess(0);
+       }
     }
+    
     proc[0] = iproc;
     return 1;
 }
@@ -1973,6 +2259,19 @@ const char* TFluka::VolName(Int_t id) const
    return fMCGeo->VolName(id);
 }
 
+Int_t TFluka::MediumId(const Text_t* mediumName) const
+{
+    //
+    // Return the unique medium id for medium with name mediumName
+    TList *medlist = gGeoManager->GetListOfMedia();
+    TGeoMedium* med = (TGeoMedium*) medlist->FindObject(mediumName);
+    if (med) {
+       return (med->GetId());
+    } else {
+       return (-1);
+    }
+}
+
 //______________________________________________________________________________ 
 Int_t TFluka::VolId(const Text_t* volName) const
 {
@@ -2023,7 +2322,7 @@ const char* TFluka::CurrentVolName() const
 //
 // Return the current volume name
 //
-  if (gGeoManager->IsOutside()) return 0;
+  if (gGeoManager->IsOutside()) return "OutOfWorld";
   return gGeoManager->GetCurrentVolume()->GetName();
 }
 
@@ -2311,15 +2610,29 @@ void TFluka::AddParticlesToPdgDataBase() const
 // Ions
 //
   pdgDB->AddParticle("Deuteron","Deuteron",2*kAu2Gev+8.071e-3,kTRUE,
-                     0,3,"Ion",GetIonPdg(1,2));
+                     0,3,"Ion",TFlukaIon::GetIonPdg(1,2));
   pdgDB->AddParticle("Triton","Triton",3*kAu2Gev+14.931e-3,kFALSE,
-                     khShGev/(12.33*kYear2Sec),3,"Ion",GetIonPdg(1,3));
+                     khShGev/(12.33*kYear2Sec),3,"Ion",TFlukaIon::GetIonPdg(1,3));
   pdgDB->AddParticle("Alpha","Alpha",4*kAu2Gev+2.424e-3,kTRUE,
-                     khShGev/(12.33*kYear2Sec),6,"Ion",GetIonPdg(2,4));
+                     khShGev/(12.33*kYear2Sec),6,"Ion",TFlukaIon::GetIonPdg(2,4));
   pdgDB->AddParticle("HE3","HE3",3*kAu2Gev+14.931e-3,kFALSE,
-                     0,6,"Ion",GetIonPdg(2,3));
+                     0,6,"Ion",TFlukaIon::GetIonPdg(2,3));
+//
+// Default user ion
+  TFlukaIon::AddIon(12, 6);
+  
+//
+//
+//
+// Special particles
+//
+  pdgDB->AddParticle("Cherenkov","Cherenkov",0,kFALSE,
+                     0,0,"Special",GetSpecialPdg(50));
+  pdgDB->AddParticle("FeedbackPhoton","FeedbackPhoton",0,kFALSE,
+                     0,0,"Special",GetSpecialPdg(51));
 }
 
+
 //
 // Info about primary ionization electrons
 //
@@ -2339,8 +2652,7 @@ Double_t TFluka::GetPrimaryElectronKineticEnergy(Int_t i) const
     Double_t ekin = -1.;
     
     if (i >= 0 && i < ALLDLT.nalldl) {
-       Int_t j = ALLDLT.nalldl - 1 - i;
-        ekin =  ALLDLT.talldl[j];
+        ekin =  ALLDLT.talldl[i];
     } else {
         Warning("GetPrimaryElectronKineticEnergy",
                 "Primary electron index out of range %d %d \n",
@@ -2349,14 +2661,14 @@ Double_t TFluka::GetPrimaryElectronKineticEnergy(Int_t i) const
     return ekin;
 }
 
-void TFluka::GetPrimaryElectronPosition(Int_t i, Double_t& x, Double_t& y, Double_t& z) const
+void TFluka::GetPrimaryElectronPosition(Int_t i, Double_t& x, Double_t& y, Double_t& z, Double_t& t) const
 {
     // Returns position  of primary electron i
         if (i >= 0 && i < ALLDLT.nalldl) {
-           Int_t j = ALLDLT.nalldl - 1 - i;
-           x = ALLDLT.xalldl[j];
-           y = ALLDLT.yalldl[j];
-           z = ALLDLT.zalldl[j];
+           x = ALLDLT.xalldl[i];
+           y = ALLDLT.yalldl[i];
+           z = ALLDLT.zalldl[i];
+           t = ALLDLT.talldl[i];
            return;
        } else {
            Warning("GetPrimaryElectronPosition",
@@ -2367,22 +2679,25 @@ void TFluka::GetPrimaryElectronPosition(Int_t i, Double_t& x, Double_t& y, Doubl
        return;
 }
 
-Int_t TFluka::GetIonPdg(Int_t z, Int_t a, Int_t i) const
+
+//__________________________________________________________________
+Int_t TFluka::GetSpecialPdg(Int_t number) const
 {
-// Acording to
-// http://cepa.fnal.gov/psm/stdhep/pdg/montecarlorpp-2006.pdf
+// Numbering for special particles
+
+  return 50000000 + number;
+}                
 
-  return 1000000000 + 10*1000*z + 10*a + i;
-}  
      
 void  TFluka::PrimaryIonisationStepping(Int_t nprim)
 {
 // Call Stepping for primary ionisation electrons
-    Int_t i;
 // Protection against nprim > mxalld
-
 // Multiple steps for nprim > 0
+    Int_t i;
+    fNPI = nprim;
     if (nprim > 0) {
+       CalcPrimaryIonisationTime();
        for (i = 0; i < nprim; i++) {
            SetCurrentPrimaryElectronIndex(i);
            (TVirtualMCApplication::Instance())->Stepping();
@@ -2397,3 +2712,67 @@ void  TFluka::PrimaryIonisationStepping(Int_t nprim)
     // Reset the index
     SetCurrentPrimaryElectronIndex(-1);
 }
+
+//______________________________________________________________________
+Float_t* TFluka::CreateFloatArray(Double_t* array, Int_t size) const
+{
+// Converts Double_t* array to Float_t*,
+// !! The new array has to be deleted by user.
+// ---
+    
+  Float_t* floatArray;
+  if (size>0) {
+    floatArray = new Float_t[size];
+    for (Int_t i=0; i<size; i++)
+      if (array[i] >= FLT_MAX ) 
+        floatArray[i] = FLT_MAX/100.;
+      else     
+        floatArray[i] = array[i];
+  }
+  else {
+    //floatArray = 0;
+    floatArray = new Float_t[1];
+  }
+  return floatArray;
+}
+
+void TFluka::CalcPrimaryIonisationTime()
+{
+    // Calculates the primary ionisation time
+    if (fPItime) delete [] fPItime; 
+    fPItime = new Double_t[fNPI];
+    if (fPIlength) delete [] fPIlength;
+    fPIlength = new Double_t[fNPI];
+    //
+    Double_t px, py, pz, e, t;
+    TrackMomentum(px, py, pz, e);
+    Double_t p    = TMath::Sqrt(px * px + py * py + pz * pz);
+    Double_t beta = p / e;
+    Double_t x0, y0, z0;
+    fPItime[fNPI -1]   = TRACKR.atrack;
+    fPIlength[fNPI -1] = TRACKR.cmtrck;
+    GetPrimaryElectronPosition(fNPI - 1, x0, y0, z0, t);       
+    if (fNPI > 1) {
+       for (Int_t i = fNPI - 2; i > -1; i--) {
+           Double_t x, y, z, t;
+           GetPrimaryElectronPosition(i, x, y, z, t);  
+           Double_t ds = TMath::Sqrt((x-x0) * (x-x0) + (y-y0) * (y-y0) + (z-z0) * (z-z0));
+           fPItime[i]   = fPItime[i+1]   - ds / (beta * 2.99792458e10);
+           fPIlength[i] = fPIlength[i+1] - ds;
+           x0 = x; y0 = y; z0 = z;
+       }
+    }
+    
+}
+
+Bool_t TFluka::DefineIon(const char* name , Int_t z, Int_t a, Int_t q, Double_t exE, Double_t mass)
+{
+    // User defined ion that can be used as a primary
+    if (fUserIon) {
+       Warning("DefineIon", "Only one user ion can be defined !");
+       return kFALSE;
+    } else {
+       fUserIon = new TFlukaIon(name, z, a, q, exE, mass);
+       return kTRUE;
+    }
+}