]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TFluka/TFluka.cxx
Stepping called right after source using sodraw.
[u/mrichter/AliRoot.git] / TFluka / TFluka.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //
19 // Realisation of the TVirtualMC interface for the FLUKA code
20 // (See official web side http://www.fluka.org/).
21 //
22 // This implementation makes use of the TGeo geometry modeller.
23 // User configuration is via automatic generation of FLUKA input cards.
24 //
25 // Authors:
26 // A. Fasso
27 // E. Futo
28 // A. Gheata
29 // A. Morsch
30 //
31
32 #include <Riostream.h>
33 #include <TList.h>
34
35 #include "TFluka.h"
36 #include "TFlukaCodes.h"
37 #include "TCallf77.h"      //For the fortran calls
38 #include "Fdblprc.h"       //(DBLPRC) fluka common
39 #include "Fsourcm.h"       //(SOURCM) fluka common
40 #include "Fgenstk.h"       //(GENSTK)  fluka common
41 #include "Fiounit.h"       //(IOUNIT) fluka common
42 #include "Fpaprop.h"       //(PAPROP) fluka common
43 #include "Fpart.h"         //(PART)   fluka common
44 #include "Ftrackr.h"       //(TRACKR) fluka common
45 #include "Fpaprop.h"       //(PAPROP) fluka common
46 #include "Ffheavy.h"       //(FHEAVY) fluka common
47 #include "Fopphst.h"       //(OPPHST) fluka common
48 #include "Fflkstk.h"       //(FLKSTK) fluka common
49 #include "Fstepsz.h"       //(STEPSZ) fluka common
50 #include "Fopphst.h"       //(OPPHST) fluka common
51 #include "Fltclcm.h"       //(LTCLCM) fluka common
52 #include "Falldlt.h"       //(ALLDLT) fluka common
53
54 #include "TVirtualMC.h"
55 #include "TMCProcess.h"
56 #include "TGeoManager.h"
57 #include "TGeoMaterial.h"
58 #include "TGeoMedium.h"
59 #include "TFlukaMCGeometry.h"
60 #include "TGeoMCGeometry.h"
61 #include "TFlukaCerenkov.h"
62 #include "TFlukaConfigOption.h"
63 #include "TFlukaScoringOption.h"
64 #include "TLorentzVector.h"
65 #include "TArrayI.h"
66 #include "TArrayD.h"
67 #include "TDatabasePDG.h"
68 #include "TStopwatch.h"
69
70
71 // Fluka methods that may be needed.
72 #ifndef WIN32 
73 # define flukam  flukam_
74 # define fluka_openinp fluka_openinp_
75 # define fluka_openout fluka_openout_
76 # define fluka_closeinp fluka_closeinp_
77 # define mcihad mcihad_
78 # define mpdgha mpdgha_
79 # define newplo newplo_
80 # define genout genout_
81 # define flkend flkend_
82 #else 
83 # define flukam  FLUKAM
84 # define fluka_openinp FLUKA_OPENINP
85 # define fluka_openout FLUKA_OPENOUT
86 # define fluka_closeinp FLUKA_CLOSEINP
87 # define mcihad MCIHAD
88 # define mpdgha MPDGHA
89 # define newplo NEWPLO
90 # define genout GENOUT
91 # define flkend FLKEND
92 #endif
93
94 extern "C" 
95 {
96   //
97   // Prototypes for FLUKA functions
98   //
99   void type_of_call flukam(const int&);
100   void type_of_call newplo();
101   void type_of_call genout();
102   void type_of_call flkend();
103   void type_of_call fluka_openinp(const int&, DEFCHARA);
104   void type_of_call fluka_openout(const int&, DEFCHARA);
105   void type_of_call fluka_closeinp(const int&);
106   int  type_of_call mcihad(const int&);
107   int  type_of_call mpdgha(const int&);
108 }
109
110 //
111 // Class implementation for ROOT
112 //
113 ClassImp(TFluka)
114
115 //
116 //----------------------------------------------------------------------------
117 // TFluka constructors and destructors.
118 //______________________________________________________________________________
119 TFluka::TFluka()
120   :TVirtualMC(),
121    fVerbosityLevel(0),
122    fNEvent(0),
123    fInputFileName(""),
124    fCoreInputFileName(""),
125    fCaller(kNoCaller),
126    fIcode(kNoProcess),
127    fNewReg(-1),
128    fRull(0),
129    fXsco(0),
130    fYsco(0),
131    fZsco(0),
132    fTrackIsEntering(kFALSE),
133    fTrackIsExiting(kFALSE),
134    fTrackIsNew(kFALSE),
135    fFieldFlag(kTRUE),
136    fDummyBoundary(kFALSE),
137    fStopped(kFALSE),
138    fStopEvent(kFALSE),
139    fStopRun(kFALSE),
140    fPrimaryElectronIndex(-1),
141    fMaterials(0),
142    fNVolumes(0),
143    fCurrentFlukaRegion(-1),
144    fNCerenkov(0),
145    fGeom(0),
146    fMCGeo(0),
147    fUserConfig(0), 
148    fUserScore(0)
149
150   //
151   // Default constructor
152   //
153
154  
155 //______________________________________________________________________________ 
156 TFluka::TFluka(const char *title, Int_t verbosity, Bool_t isRootGeometrySupported)
157   :TVirtualMC("TFluka",title, isRootGeometrySupported),
158    fVerbosityLevel(verbosity),
159    fNEvent(0),
160    fInputFileName(""),
161    fCoreInputFileName(""),
162    fCaller(kNoCaller),
163    fIcode(kNoProcess),
164    fNewReg(-1),
165    fRull(0),
166    fXsco(0),
167    fYsco(0),
168    fZsco(0),
169    fTrackIsEntering(kFALSE),
170    fTrackIsExiting(kFALSE),
171    fTrackIsNew(kFALSE),
172    fFieldFlag(kTRUE),
173    fDummyBoundary(kFALSE),
174    fStopped(kFALSE),
175    fStopEvent(kFALSE),
176    fStopRun(kFALSE),
177    fPrimaryElectronIndex(-1),
178    fMaterials(0),
179    fNVolumes(0),
180    fCurrentFlukaRegion(-1),
181    fNCerenkov(0),
182    fGeom(0),
183    fMCGeo(0),
184    fUserConfig(new TObjArray(100)),
185    fUserScore(new TObjArray(100)) 
186 {
187   // create geometry interface
188    if (fVerbosityLevel >=3)
189        cout << "<== TFluka::TFluka(" << title << ") constructor called." << endl;
190    SetCoreInputFileName();
191    SetInputFileName();
192    fMCGeo = new TGeoMCGeometry("MCGeo", "TGeo Implementation of VirtualMCGeometry", kFALSE);
193    fGeom  = new TFlukaMCGeometry("geom", "FLUKA VMC Geometry");
194    if (verbosity > 2) fGeom->SetDebugMode(kTRUE);
195    PrintHeader();
196 }
197
198 //______________________________________________________________________________ 
199 TFluka::~TFluka()
200 {
201     // Destructor
202     if (fVerbosityLevel >=3)
203         cout << "<== TFluka::~TFluka() destructor called." << endl;
204     if (fMaterials) delete [] fMaterials;
205     
206     delete fGeom;
207     delete fMCGeo;
208     
209     if (fUserConfig) {
210         fUserConfig->Delete();
211         delete fUserConfig;
212     }
213     
214     if (fUserScore) {
215         fUserScore->Delete();
216         delete fUserScore;
217     }
218 }
219
220 //
221 //______________________________________________________________________________
222 // TFluka control methods
223 //______________________________________________________________________________ 
224 void TFluka::Init() {
225 //
226 //  Geometry initialisation
227 //
228     if (fVerbosityLevel >=3) cout << "==> TFluka::Init() called." << endl;
229     
230     if (!gGeoManager) new TGeoManager("geom", "FLUKA geometry");
231     fApplication->ConstructGeometry();
232     if (!gGeoManager->IsClosed()) {
233        TGeoVolume *top = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
234        gGeoManager->SetTopVolume(top);
235        gGeoManager->CloseGeometry("di");
236     } else {
237        TGeoNodeCache *cache = gGeoManager->GetCache();
238        if (!cache->HasIdArray()) {
239           Warning("Init", "Node ID tracking must be enabled with TFluka: enabling...\n");
240           cache->BuildIdArray();
241        }   
242     }           
243     fNVolumes = fGeom->NofVolumes();
244     fGeom->CreateFlukaMatFile("flukaMat.inp");   
245     if (fVerbosityLevel >=3) {
246        printf("== Number of volumes: %i\n ==", fNVolumes);
247        cout << "\t* InitPhysics() - Prepare input file to be called" << endl; 
248     }
249
250     fApplication->InitGeometry();
251     fApplication->ConstructOpGeometry();
252     //
253     // Add ions to PDG Data base
254     //
255      AddParticlesToPdgDataBase();
256      //
257 }
258
259
260 //______________________________________________________________________________ 
261 void TFluka::FinishGeometry() {
262 //
263 // Build-up table with region to medium correspondance
264 //
265   if (fVerbosityLevel >=3) {
266     cout << "==> TFluka::FinishGeometry() called." << endl;
267     printf("----FinishGeometry - applying misalignment if any\n");
268     cout << "<== TFluka::FinishGeometry() called." << endl;
269   }  
270   TVirtualMCApplication::Instance()->MisalignGeometry();
271
272
273 //______________________________________________________________________________ 
274 void TFluka::BuildPhysics() {
275 //
276 //  Prepare FLUKA input files and call FLUKA physics initialisation
277 //
278     
279     if (fVerbosityLevel >=3)
280         cout << "==> TFluka::BuildPhysics() called." << endl;
281
282     
283     if (fVerbosityLevel >=3) {
284         TList *medlist = gGeoManager->GetListOfMedia();
285         TIter next(medlist);
286         TGeoMedium*   med = 0x0;
287         TGeoMaterial* mat = 0x0;
288         Int_t ic = 0;
289         
290         while((med = (TGeoMedium*)next()))
291         {
292             mat = med->GetMaterial();
293             printf("Medium %5d %12s %5d %5d\n", ic, (med->GetName()), med->GetId(), mat->GetIndex());
294             ic++;
295         }
296     }
297     
298
299     // Prepare input file with the current physics settings
300     
301     InitPhysics(); 
302 //  Open fortran files    
303     const char* fname = fInputFileName;
304     fluka_openinp(lunin, PASSCHARA(fname));
305     fluka_openout(11, PASSCHARA("fluka.out"));
306 //  Read input cards    
307     cout << "==> TFluka::BuildPhysics() Read input cards." << endl;
308     TStopwatch timer;
309     timer.Start();
310     GLOBAL.lfdrtr = true;
311     flukam(1);
312     cout << "<== TFluka::BuildPhysics() Read input cards End"
313          << Form(" R:%.2fs C:%.2fs", timer.RealTime(),timer.CpuTime()) << endl;
314 //  Close input file
315     fluka_closeinp(lunin);
316 //  Finish geometry    
317     FinishGeometry();
318 }  
319
320 //______________________________________________________________________________ 
321 void TFluka::ProcessEvent() {
322 //
323 // Process one event
324 //
325     if (fStopRun) {
326         Warning("ProcessEvent", "User Run Abortion: No more events handled !\n");
327         fNEvent += 1;
328         return;
329     }
330
331     if (fVerbosityLevel >=3)
332         cout << "==> TFluka::ProcessEvent() called." << endl;
333     fApplication->GeneratePrimaries();
334     SOURCM.lsouit = true;
335     flukam(1);
336     if (fVerbosityLevel >=3)
337         cout << "<== TFluka::ProcessEvent() called." << endl;
338     //
339     // Increase event number
340     //
341     fNEvent += 1;
342 }
343
344 //______________________________________________________________________________ 
345 Bool_t TFluka::ProcessRun(Int_t nevent) {
346 //
347 // Run steering
348 //
349
350   if (fVerbosityLevel >=3)
351     cout << "==> TFluka::ProcessRun(" << nevent << ") called." 
352          << endl;
353
354   if (fVerbosityLevel >=2) {
355     cout << "\t* GLOBAL.fdrtr = " << (GLOBAL.lfdrtr?'T':'F') << endl;
356     cout << "\t* Calling flukam again..." << endl;
357   }
358
359   Int_t todo = TMath::Abs(nevent);
360   for (Int_t ev = 0; ev < todo; ev++) {
361       TStopwatch timer;
362       timer.Start();
363       fApplication->BeginEvent();
364       ProcessEvent();
365       fApplication->FinishEvent();
366       cout << "Event: "<< ev
367            << Form(" R:%.2fs C:%.2fs", timer.RealTime(),timer.CpuTime()) << endl;
368   }
369
370   if (fVerbosityLevel >=3)
371     cout << "<== TFluka::ProcessRun(" << nevent << ") called." 
372          << endl;
373   
374   // Write fluka specific scoring output
375   genout();
376   newplo();
377   flkend();
378   
379   return kTRUE;
380 }
381
382 //_____________________________________________________________________________
383 // methods for building/management of geometry
384
385 // functions from GCONS 
386 //____________________________________________________________________________ 
387 void TFluka::Gfmate(Int_t imat, char *name, Float_t &a, Float_t &z,  
388                     Float_t &dens, Float_t &radl, Float_t &absl,
389                     Float_t* /*ubuf*/, Int_t& /*nbuf*/) {
390 //
391    TGeoMaterial *mat;
392    TIter next (gGeoManager->GetListOfMaterials());
393    while ((mat = (TGeoMaterial*)next())) {
394      if (mat->GetUniqueID() == (UInt_t)imat) break;
395    }
396    if (!mat) {
397       Error("Gfmate", "no material with index %i found", imat);
398       return;
399    }
400    sprintf(name, "%s", mat->GetName());
401    a = mat->GetA();
402    z = mat->GetZ();
403    dens = mat->GetDensity();
404    radl = mat->GetRadLen();
405    absl = mat->GetIntLen();
406
407
408 //______________________________________________________________________________ 
409 void TFluka::Gfmate(Int_t imat, char *name, Double_t &a, Double_t &z,  
410                     Double_t &dens, Double_t &radl, Double_t &absl,
411                     Double_t* /*ubuf*/, Int_t& /*nbuf*/) {
412 //
413    TGeoMaterial *mat;
414    TIter next (gGeoManager->GetListOfMaterials());
415    while ((mat = (TGeoMaterial*)next())) {
416      if (mat->GetUniqueID() == (UInt_t)imat) break;
417    }
418    if (!mat) {
419       Error("Gfmate", "no material with index %i found", imat);
420       return;
421    }
422    sprintf(name, "%s", mat->GetName());
423    a = mat->GetA();
424    z = mat->GetZ();
425    dens = mat->GetDensity();
426    radl = mat->GetRadLen();
427    absl = mat->GetIntLen();
428
429
430 // detector composition
431 //______________________________________________________________________________ 
432 void TFluka::Material(Int_t& kmat, const char* name, Double_t a, 
433                       Double_t z, Double_t dens, Double_t radl, Double_t absl,
434                       Float_t* buf, Int_t nwbuf) {
435 //
436    Double_t* dbuf = fGeom->CreateDoubleArray(buf, nwbuf);  
437    Material(kmat, name, a, z, dens, radl, absl, dbuf, nwbuf);
438    delete [] dbuf;
439
440
441 //______________________________________________________________________________ 
442 void TFluka::Material(Int_t& kmat, const char* name, Double_t a, 
443                       Double_t z, Double_t dens, Double_t radl, Double_t absl,
444                       Double_t* /*buf*/, Int_t /*nwbuf*/) {
445 //
446 // Define a material
447   TGeoMaterial *mat;
448   kmat = gGeoManager->GetListOfMaterials()->GetSize();
449   if ((z-Int_t(z)) > 1E-3) {
450      mat = fGeom->GetMakeWrongMaterial(z);
451      if (mat) {
452         mat->SetRadLen(radl,absl);
453         mat->SetUniqueID(kmat);
454         return;
455      }
456   }      
457   gGeoManager->Material(name, a, z, dens, kmat, radl, absl);
458
459
460 //______________________________________________________________________________ 
461 void TFluka::Mixture(Int_t& kmat, const char *name, Float_t *a, 
462                      Float_t *z, Double_t dens, Int_t nlmat, Float_t *wmat) {
463 //
464 // Define a material mixture
465 //
466   Double_t* da = fGeom->CreateDoubleArray(a, TMath::Abs(nlmat));  
467   Double_t* dz = fGeom->CreateDoubleArray(z, TMath::Abs(nlmat));  
468   Double_t* dwmat = fGeom->CreateDoubleArray(wmat, TMath::Abs(nlmat));  
469
470   Mixture(kmat, name, da, dz, dens, nlmat, dwmat);
471   for (Int_t i=0; i<nlmat; i++) {
472     a[i] = da[i]; z[i] = dz[i]; wmat[i] = dwmat[i];
473   }  
474
475   delete [] da;
476   delete [] dz;
477   delete [] dwmat;
478
479
480 //______________________________________________________________________________ 
481 void TFluka::Mixture(Int_t& kmat, const char *name, Double_t *a, 
482                      Double_t *z, Double_t dens, Int_t nlmat, Double_t *wmat) {
483 //
484   // Defines mixture OR COMPOUND IMAT as composed by 
485   // THE BASIC NLMAT materials defined by arrays A,Z and WMAT
486   // 
487   // If NLMAT > 0 then wmat contains the proportion by
488   // weights of each basic material in the mixture. 
489   // 
490   // If nlmat < 0 then WMAT contains the number of atoms 
491   // of a given kind into the molecule of the COMPOUND
492   // In this case, WMAT in output is changed to relative
493   // weigths.
494   //
495   Int_t i,j;
496   if (nlmat < 0) {
497      nlmat = - nlmat;
498      Double_t amol = 0;
499      for (i=0;i<nlmat;i++) {
500         amol += a[i]*wmat[i];
501      }
502      for (i=0;i<nlmat;i++) {
503         wmat[i] *= a[i]/amol;
504      }
505   }
506   kmat = gGeoManager->GetListOfMaterials()->GetSize();
507   // Check if we have elements with fractional Z
508   TGeoMaterial *mat = 0;
509   TGeoMixture *mix = 0;
510   Bool_t mixnew = kFALSE;
511   for (i=0; i<nlmat; i++) {
512      if (z[i]-Int_t(z[i]) < 1E-3) continue;
513      // We have found an element with fractional Z -> loop mixtures to look for it
514      for (j=0; j<kmat; j++) {
515         mat = (TGeoMaterial*)gGeoManager->GetListOfMaterials()->At(j);
516         if (!mat) break;
517         if (!mat->IsMixture()) continue;
518         mix = (TGeoMixture*)mat;
519         if (TMath::Abs(z[i]-mix->GetZ()) >1E-3) continue;
520         mixnew = kTRUE;
521         break;
522      }
523      if (!mixnew) Warning("Mixture","%s : cannot find component %i with fractional Z=%f\n", name, i, z[i]);
524      break;
525   }   
526   if (mixnew) {
527      Int_t nlmatnew = nlmat+mix->GetNelements()-1;
528      Double_t *anew = new Double_t[nlmatnew];
529      Double_t *znew = new Double_t[nlmatnew];
530      Double_t *wmatnew = new Double_t[nlmatnew];
531      Int_t ind=0;
532      for (j=0; j<nlmat; j++) {
533         if (j==i) continue;
534         anew[ind] = a[j];
535         znew[ind] = z[j];
536         wmatnew[ind] = wmat[j];
537         ind++;
538      }
539      for (j=0; j<mix->GetNelements(); j++) {
540         anew[ind] = mix->GetAmixt()[j];
541         znew[ind] = mix->GetZmixt()[j];
542         wmatnew[ind] = wmat[i]*mix->GetWmixt()[j];
543         ind++;
544      }
545      Mixture(kmat, name, anew, znew, dens, nlmatnew, wmatnew);
546      delete [] anew;
547      delete [] znew;
548      delete [] wmatnew;
549      return;
550   }   
551   // Now we need to compact identical elements within the mixture
552   // First check if this happens   
553   mixnew = kFALSE;  
554   for (i=0; i<nlmat-1; i++) {
555      for (j=i+1; j<nlmat; j++) {
556         if (z[i] == z[j]) {
557            mixnew = kTRUE;
558            break;
559         }
560      }   
561      if (mixnew) break;
562   }   
563   if (mixnew) {
564      Int_t nlmatnew = 0;
565      Double_t *anew = new Double_t[nlmat];
566      Double_t *znew = new Double_t[nlmat];
567      memset(znew, 0, nlmat*sizeof(Double_t));
568      Double_t *wmatnew = new Double_t[nlmat];
569      Bool_t skipi;
570      for (i=0; i<nlmat; i++) {
571         skipi = kFALSE;
572         for (j=0; j<nlmatnew; j++) {
573            if (z[i] == z[j]) {
574               wmatnew[j] += wmat[i];
575               skipi = kTRUE;
576               break;
577            }
578         }   
579         if (skipi) continue;    
580         anew[nlmatnew] = a[i];
581         znew[nlmatnew] = z[i];
582         wmatnew[nlmatnew] = wmat[i];
583         nlmatnew++;
584      }
585      Mixture(kmat, name, anew, znew, dens, nlmatnew, wmatnew);
586      delete [] anew;
587      delete [] znew;
588      delete [] wmatnew;
589      return;     
590    }
591    gGeoManager->Mixture(name, a, z, dens, nlmat, wmat, kmat);
592
593
594 //______________________________________________________________________________ 
595 void TFluka::Medium(Int_t& kmed, const char *name, Int_t nmat, 
596                     Int_t isvol, Int_t ifield, Double_t fieldm, Double_t tmaxfd,
597                     Double_t stemax, Double_t deemax, Double_t epsil,
598                     Double_t stmin, Float_t* ubuf, Int_t nbuf) {
599   // Define a medium
600   // 
601   kmed = gGeoManager->GetListOfMedia()->GetSize()+1;
602   fMCGeo->Medium(kmed, name, nmat, isvol, ifield, fieldm, tmaxfd, stemax, deemax, 
603              epsil, stmin, ubuf, nbuf);
604
605
606 //______________________________________________________________________________ 
607 void TFluka::Medium(Int_t& kmed, const char *name, Int_t nmat, 
608                     Int_t isvol, Int_t ifield, Double_t fieldm, Double_t tmaxfd,
609                     Double_t stemax, Double_t deemax, Double_t epsil,
610                     Double_t stmin, Double_t* ubuf, Int_t nbuf) {
611   // Define a medium
612   // 
613   kmed = gGeoManager->GetListOfMedia()->GetSize()+1;
614   fMCGeo->Medium(kmed, name, nmat, isvol, ifield, fieldm, tmaxfd, stemax, deemax, 
615              epsil, stmin, ubuf, nbuf);
616
617
618 //______________________________________________________________________________ 
619 void TFluka::Matrix(Int_t& krot, Double_t thetaX, Double_t phiX, 
620                     Double_t thetaY, Double_t phiY, Double_t thetaZ,
621                     Double_t phiZ) {
622 //        
623   krot = gGeoManager->GetListOfMatrices()->GetEntriesFast();
624   fMCGeo->Matrix(krot, thetaX, phiX, thetaY, phiY, thetaZ, phiZ); 
625
626
627 //______________________________________________________________________________ 
628 void TFluka::Gstpar(Int_t itmed, const char* param, Double_t parval) {
629 //
630 //
631 //
632    Bool_t process = kFALSE;
633    Bool_t modelp  = kFALSE;
634    
635    if (strncmp(param, "DCAY",  4) == 0 ||
636        strncmp(param, "PAIR",  4) == 0 ||
637        strncmp(param, "COMP",  4) == 0 ||
638        strncmp(param, "PHOT",  4) == 0 ||
639        strncmp(param, "PFIS",  4) == 0 ||
640        strncmp(param, "DRAY",  4) == 0 ||
641        strncmp(param, "ANNI",  4) == 0 ||
642        strncmp(param, "BREM",  4) == 0 ||
643        strncmp(param, "MUNU",  4) == 0 ||
644        strncmp(param, "CKOV",  4) == 0 ||
645        strncmp(param, "HADR",  4) == 0 ||
646        strncmp(param, "LOSS",  4) == 0 ||
647        strncmp(param, "MULS",  4) == 0 ||
648        strncmp(param, "RAYL",  4) == 0) 
649    {
650        process = kTRUE;
651    } 
652    
653    if (strncmp(param, "PRIMIO_N",  8) == 0 ||
654        strncmp(param, "PRIMIO_E",  8) == 0)
655    {
656        modelp = kTRUE;
657    }
658    
659    if (process) {
660        // Process switch
661        SetProcess(param, Int_t (parval), itmed);
662    } else if (modelp) {
663        // Model parameters
664        SetModelParameter(param, parval, itmed);
665    } else {
666        // Cuts
667        SetCut(param, parval, itmed);
668    }
669    
670    
671 }    
672
673 // functions from GGEOM 
674 //_____________________________________________________________________________
675 void TFluka::Gsatt(const char *name, const char *att, Int_t val)
676
677   // Set visualisation attributes for one volume
678   char vname[5];
679   fGeom->Vname(name,vname);
680   char vatt[5];
681   fGeom->Vname(att,vatt);
682   gGeoManager->SetVolumeAttribute(vname, vatt, val);
683 }
684
685 //______________________________________________________________________________ 
686 Int_t TFluka::Gsvolu(const char *name, const char *shape, Int_t nmed,  
687                      Float_t *upar, Int_t np)  {
688 //
689     return fMCGeo->Gsvolu(name, shape, nmed, upar, np); 
690 }
691
692 //______________________________________________________________________________ 
693 Int_t TFluka::Gsvolu(const char *name, const char *shape, Int_t nmed,  
694                      Double_t *upar, Int_t np)  {
695 //
696     return fMCGeo->Gsvolu(name, shape, nmed, upar, np); 
697 }
698  
699 //______________________________________________________________________________ 
700 void TFluka::Gsdvn(const char *name, const char *mother, Int_t ndiv, 
701                    Int_t iaxis) {
702 //
703     fMCGeo->Gsdvn(name, mother, ndiv, iaxis); 
704
705
706 //______________________________________________________________________________ 
707 void TFluka::Gsdvn2(const char *name, const char *mother, Int_t ndiv, 
708                     Int_t iaxis, Double_t c0i, Int_t numed) {
709 //
710     fMCGeo->Gsdvn2(name, mother, ndiv, iaxis, c0i, numed); 
711
712
713 //______________________________________________________________________________ 
714 void TFluka::Gsdvt(const char *name, const char *mother, Double_t step, 
715                    Int_t iaxis, Int_t numed, Int_t ndvmx) {
716 //        
717     fMCGeo->Gsdvt(name, mother, step, iaxis, numed, ndvmx); 
718
719
720 //______________________________________________________________________________ 
721 void TFluka::Gsdvt2(const char *name, const char *mother, Double_t step, 
722                     Int_t iaxis, Double_t c0, Int_t numed, Int_t ndvmx) {
723 //
724     fMCGeo->Gsdvt2(name, mother, step, iaxis, c0, numed, ndvmx); 
725
726
727 //______________________________________________________________________________ 
728 void TFluka::Gsord(const char * /*name*/, Int_t /*iax*/) {
729 //
730 // Nothing to do with TGeo
731
732
733 //______________________________________________________________________________ 
734 void TFluka::Gspos(const char *name, Int_t nr, const char *mother,  
735                    Double_t x, Double_t y, Double_t z, Int_t irot,
736                    const char *konly) {
737 //
738   fMCGeo->Gspos(name, nr, mother, x, y, z, irot, konly); 
739
740
741 //______________________________________________________________________________ 
742 void TFluka::Gsposp(const char *name, Int_t nr, const char *mother,  
743                     Double_t x, Double_t y, Double_t z, Int_t irot,
744                     const char *konly, Float_t *upar, Int_t np)  {
745   //
746   fMCGeo->Gsposp(name, nr, mother, x, y, z, irot, konly, upar, np); 
747
748
749 //______________________________________________________________________________ 
750 void TFluka::Gsposp(const char *name, Int_t nr, const char *mother,  
751                     Double_t x, Double_t y, Double_t z, Int_t irot,
752                     const char *konly, Double_t *upar, Int_t np)  {
753   //
754   fMCGeo->Gsposp(name, nr, mother, x, y, z, irot, konly, upar, np); 
755
756
757 //______________________________________________________________________________ 
758 void TFluka::Gsbool(const char* /*onlyVolName*/, const char* /*manyVolName*/) {
759 //
760 // Nothing to do with TGeo
761 }
762
763 //______________________________________________________________________
764 Bool_t TFluka::GetTransformation(const TString &volumePath,TGeoHMatrix &mat)
765 {
766     // Returns the Transformation matrix between the volume specified
767     // by the path volumePath and the Top or mater volume. The format
768     // of the path volumePath is as follows (assuming ALIC is the Top volume)
769     // "/ALIC_1/DDIP_1/S05I_2/S05H_1/S05G_3". Here ALIC is the top most
770     // or master volume which has only 1 instance of. Of all of the daughter
771     // volumes of ALICE, DDIP volume copy #1 is indicated. Similarly for
772     // the daughter volume of DDIP is S05I copy #2 and so on.
773     // Inputs:
774     //   TString& volumePath  The volume path to the specific volume
775     //                        for which you want the matrix. Volume name
776     //                        hierarchy is separated by "/" while the
777     //                        copy number is appended using a "_".
778     // Outputs:
779     //  TGeoHMatrix &mat      A matrix with its values set to those
780     //                        appropriate to the Local to Master transformation
781     // Return:
782     //   A logical value if kFALSE then an error occurred and no change to
783     //   mat was made.
784
785    // We have to preserve the modeler state
786    return fMCGeo->GetTransformation(volumePath, mat);
787 }   
788    
789 //______________________________________________________________________
790 Bool_t TFluka::GetShape(const TString &volumePath,TString &shapeType,
791                         TArrayD &par)
792 {
793     // Returns the shape and its parameters for the volume specified
794     // by volumeName.
795     // Inputs:
796     //   TString& volumeName  The volume name
797     // Outputs:
798     //   TString &shapeType   Shape type
799     //   TArrayD &par         A TArrayD of parameters with all of the
800     //                        parameters of the specified shape.
801     // Return:
802     //   A logical indicating whether there was an error in getting this
803     //   information
804    return fMCGeo->GetShape(volumePath, shapeType, par);
805 }
806    
807 //______________________________________________________________________
808 Bool_t TFluka::GetMaterial(const TString &volumeName,
809                             TString &name,Int_t &imat,
810                             Double_t &a,Double_t &z,Double_t &dens,
811                             Double_t &radl,Double_t &inter,TArrayD &par)
812 {
813     // Returns the Material and its parameters for the volume specified
814     // by volumeName.
815     // Note, Geant3 stores and uses mixtures as an element with an effective
816     // Z and A. Consequently, if the parameter Z is not integer, then
817     // this material represents some sort of mixture.
818     // Inputs:
819     //   TString& volumeName  The volume name
820     // Outputs:
821     //   TSrting   &name       Material name
822     //   Int_t     &imat       Material index number
823     //   Double_t  &a          Average Atomic mass of material
824     //   Double_t  &z          Average Atomic number of material
825     //   Double_t  &dens       Density of material [g/cm^3]
826     //   Double_t  &radl       Average radiation length of material [cm]
827     //   Double_t  &inter      Average interaction length of material [cm]
828     //   TArrayD   &par        A TArrayD of user defined parameters.
829     // Return:
830     //   kTRUE if no errors
831    return fMCGeo->GetMaterial(volumeName,name,imat,a,z,dens,radl,inter,par);
832 }
833
834 //______________________________________________________________________
835 Bool_t TFluka::GetMedium(const TString &volumeName,TString &name,
836                          Int_t &imed,Int_t &nmat,Int_t &isvol,Int_t &ifield,
837                          Double_t &fieldm,Double_t &tmaxfd,Double_t &stemax,
838                          Double_t &deemax,Double_t &epsil, Double_t &stmin,
839                          TArrayD &par)
840 {
841     // Returns the Medium and its parameters for the volume specified
842     // by volumeName.
843     // Inputs:
844     //   TString& volumeName  The volume name.
845     // Outputs:
846     //   TString  &name       Medium name
847     //   Int_t    &nmat       Material number defined for this medium
848     //   Int_t    &imed       The medium index number
849     //   Int_t    &isvol      volume number defined for this medium
850     //   Int_t    &iflield    Magnetic field flag
851     //   Double_t &fieldm     Magnetic field strength
852     //   Double_t &tmaxfd     Maximum angle of deflection per step
853     //   Double_t &stemax     Maximum step size
854     //   Double_t &deemax     Maximum fraction of energy allowed to be lost
855     //                        to continuous process.
856     //   Double_t &epsil      Boundary crossing precision
857     //   Double_t &stmin      Minimum step size allowed
858     //   TArrayD  &par        A TArrayD of user parameters with all of the
859     //                        parameters of the specified medium.
860     // Return:
861     //   kTRUE if there where no errors
862    return fMCGeo->GetMedium(volumeName,name,imed,nmat,isvol,ifield,fieldm,tmaxfd,stemax,deemax,epsil,stmin,par);
863 }         
864
865 //______________________________________________________________________________ 
866 void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Float_t* ppckov,
867                          Float_t* absco, Float_t* effic, Float_t* rindex) {
868 //
869 // Set Cerenkov properties for medium itmed
870 //
871 // npckov: number of sampling points
872 // ppckov: energy values
873 // absco:  absorption length
874 // effic:  quantum efficiency
875 // rindex: refraction index
876 //
877 //
878 //  
879 //  Create object holding Cerenkov properties
880 //  
881     TFlukaCerenkov* cerenkovProperties = new TFlukaCerenkov(npckov, ppckov, absco, effic, rindex);
882 //
883 //  Pass object to medium
884     TGeoMedium* medium = gGeoManager->GetMedium(itmed);
885     medium->SetCerenkovProperties(cerenkovProperties);
886 }  
887
888 void TFluka::SetCerenkov(Int_t itmed, Int_t npckov, Float_t* ppckov,
889                          Float_t* absco, Float_t* effic, Float_t* rindex, Float_t* rfl) {
890 //
891 // Set Cerenkov properties for medium itmed
892 //
893 // npckov: number of sampling points
894 // ppckov: energy values
895 // absco:  absorption length
896 // effic:  quantum efficiency
897 // rindex: refraction index
898 // rfl:    reflectivity for boundary to medium itmed
899 //
900 //  
901 //  Create object holding Cerenkov properties
902 //  
903     TFlukaCerenkov* cerenkovProperties = new TFlukaCerenkov(npckov, ppckov, absco, effic, rindex, rfl);
904 //
905 //  Pass object to medium
906     TGeoMedium* medium = gGeoManager->GetMedium(itmed);
907     medium->SetCerenkovProperties(cerenkovProperties);
908 }  
909
910
911 //______________________________________________________________________________ 
912 void TFluka::SetCerenkov(Int_t /*itmed*/, Int_t /*npckov*/, Double_t * /*ppckov*/,
913                          Double_t * /*absco*/, Double_t * /*effic*/, Double_t * /*rindex*/) {
914 //
915 //  Double_t version not implemented
916 }  
917
918 void TFluka::SetCerenkov(Int_t /*itmed*/, Int_t /*npckov*/, Double_t* /*ppckov*/,
919                          Double_t* /*absco*/, Double_t* /*effic*/, Double_t* /*rindex*/, Double_t* /*rfl*/) {
920 //
921 // //  Double_t version not implemented
922 }
923
924 // Euclid
925 //______________________________________________________________________________ 
926 void TFluka::WriteEuclid(const char* /*fileName*/, const char* /*topVol*/, 
927                           Int_t /*number*/, Int_t /*nlevel*/) {
928 //
929 // Not with TGeo
930    Warning("WriteEuclid", "Not implemented !");
931
932
933
934
935 //_____________________________________________________________________________
936 // methods needed by the stepping
937 //____________________________________________________________________________ 
938
939 Int_t TFluka::GetMedium() const {
940 //
941 //  Get the medium number for the current fluka region
942 //
943     return fGeom->GetMedium(); // this I need to check due to remapping !!!
944 }
945
946 //____________________________________________________________________________ 
947 Int_t TFluka::GetDummyRegion() const
948 {
949 // Returns index of the dummy region.
950    return fGeom->GetDummyRegion();
951 }   
952
953 //____________________________________________________________________________ 
954 Int_t TFluka::GetDummyLattice() const
955 {
956 // Returns index of the dummy lattice.
957    return fGeom->GetDummyLattice();
958 }   
959
960 //____________________________________________________________________________ 
961 // particle table usage
962 // ID <--> PDG transformations
963 //_____________________________________________________________________________
964 Int_t TFluka::IdFromPDG(Int_t pdg) const 
965 {
966     //
967     // Return Fluka code from PDG and pseudo ENDF code
968     
969     // Catch the feedback photons
970     if (pdg == 50000051) return (kFLUKAoptical);
971     // MCIHAD() goes from pdg to fluka internal.
972     Int_t intfluka = mcihad(pdg);
973     // KPTOIP array goes from internal to official
974     return GetFlukaKPTOIP(intfluka);
975 }
976
977 //______________________________________________________________________________ 
978 Int_t TFluka::PDGFromId(Int_t id) const 
979 {
980   //
981   // Return PDG code and pseudo ENDF code from Fluka code
982   //                      Alpha     He3       Triton    Deuteron  gen. ion  opt. photon   
983     Int_t idSpecial[6] = {GetIonPdg(2,4), GetIonPdg(2, 3), GetIonPdg(1,3), GetIonPdg(1,2), GetIonPdg(0,0), 50000050};
984   // IPTOKP array goes from official to internal
985
986     if (id == kFLUKAoptical) {
987 // Cerenkov photon
988 //        if (fVerbosityLevel >= 3)
989 //            printf("\n PDGFromId: Cerenkov Photon \n");
990         return  50000050;
991     }
992 // Error id    
993     if (id == 0 || id < kFLUKAcodemin || id > kFLUKAcodemax) {
994         if (fVerbosityLevel >= 3)
995             printf("PDGFromId: Error id = 0 %5d %5d\n", id, fCaller);
996         return -1;
997     }
998 // Good id    
999     if (id > 0) {
1000         Int_t intfluka = GetFlukaIPTOKP(id);
1001         if (intfluka == 0) {
1002             if (fVerbosityLevel >= 3)
1003                 printf("PDGFromId: Error intfluka = 0: %d\n", id);
1004             return -1;
1005         } else if (intfluka < 0) {
1006             if (fVerbosityLevel >= 3)
1007                 printf("PDGFromId: Error intfluka < 0: %d\n", id);
1008             return -1;
1009         }
1010 //        if (fVerbosityLevel >= 3)
1011 //            printf("mpdgha called with %d %d \n", id, intfluka);
1012         return mpdgha(intfluka);
1013     } else {
1014         // ions and optical photons
1015         return idSpecial[id - kFLUKAcodemin];
1016     }
1017 }
1018
1019 void TFluka::StopTrack()
1020 {
1021     // Set stopping conditions
1022     // Works for photons and charged particles
1023     fStopped = kTRUE;
1024 }
1025   
1026 //_____________________________________________________________________________
1027 // methods for physics management
1028 //____________________________________________________________________________ 
1029 //
1030 // set methods
1031 //
1032
1033 void TFluka::SetProcess(const char* flagName, Int_t flagValue, Int_t imed)
1034 {
1035 //  Set process user flag for material imat
1036 //
1037 //    
1038 //  Update if already in the list
1039 //
1040     TIter next(fUserConfig);
1041     TFlukaConfigOption* proc;
1042     while((proc = (TFlukaConfigOption*)next()))
1043     { 
1044         if (proc->Medium() == imed) {
1045             proc->SetProcess(flagName, flagValue);
1046             return;
1047         }
1048     }
1049     proc = new TFlukaConfigOption(imed);
1050     proc->SetProcess(flagName, flagValue);
1051     fUserConfig->Add(proc);
1052 }
1053
1054 //______________________________________________________________________________ 
1055 Bool_t TFluka::SetProcess(const char* flagName, Int_t flagValue)
1056 {
1057 //  Set process user flag 
1058 //
1059 //    
1060     SetProcess(flagName, flagValue, -1);
1061     return kTRUE;  
1062 }
1063
1064 //______________________________________________________________________________ 
1065 void TFluka::SetCut(const char* cutName, Double_t cutValue, Int_t imed)
1066 {
1067 // Set user cut value for material imed
1068 //
1069     TIter next(fUserConfig);
1070     TFlukaConfigOption* proc;
1071     while((proc = (TFlukaConfigOption*)next()))
1072     { 
1073         if (proc->Medium() == imed) {
1074             proc->SetCut(cutName, cutValue);
1075             return;
1076         }
1077     }
1078
1079     proc = new TFlukaConfigOption(imed);
1080     proc->SetCut(cutName, cutValue);
1081     fUserConfig->Add(proc);
1082 }
1083
1084
1085 //______________________________________________________________________________ 
1086 void TFluka::SetModelParameter(const char* parName, Double_t parValue, Int_t imed)
1087 {
1088 // Set model parameter for material imed
1089 //
1090     TIter next(fUserConfig);
1091     TFlukaConfigOption* proc;
1092     while((proc = (TFlukaConfigOption*)next()))
1093     { 
1094         if (proc->Medium() == imed) {
1095             proc->SetModelParameter(parName, parValue);
1096             return;
1097         }
1098     }
1099
1100     proc = new TFlukaConfigOption(imed);
1101     proc->SetModelParameter(parName, parValue);
1102     fUserConfig->Add(proc);
1103 }
1104
1105 //______________________________________________________________________________ 
1106 Bool_t TFluka::SetCut(const char* cutName, Double_t cutValue)
1107 {
1108 // Set user cut value 
1109 //
1110 //    
1111     SetCut(cutName, cutValue, -1);
1112     return kTRUE;
1113 }
1114
1115
1116 void TFluka::SetUserScoring(const char* option, const char* sdum, Int_t npr, char* outfile, Float_t* what)
1117 {
1118 //
1119 // Adds a user scoring option to the list
1120 //
1121     TFlukaScoringOption* opt = new TFlukaScoringOption(option, sdum, npr,outfile,what);
1122     fUserScore->Add(opt);
1123 }
1124 //______________________________________________________________________________
1125 void TFluka::SetUserScoring(const char* option, const char* sdum, Int_t npr, char* outfile, Float_t* what, 
1126                             const char* det1, const char* det2, const char* det3)
1127 {
1128 //
1129 // Adds a user scoring option to the list
1130 //
1131     TFlukaScoringOption* opt = new TFlukaScoringOption(option, sdum, npr, outfile, what, det1, det2, det3);
1132     fUserScore->Add(opt);
1133 }
1134
1135 //______________________________________________________________________________ 
1136 Double_t TFluka::Xsec(char*, Double_t, Int_t, Int_t)
1137 {
1138   Warning("Xsec", "Not yet implemented.!\n"); return -1.;
1139 }
1140
1141
1142 //______________________________________________________________________________ 
1143 void TFluka::InitPhysics()
1144 {
1145 //
1146 // Physics initialisation with preparation of FLUKA input cards
1147 //
1148 // Construct file names
1149     FILE *pFlukaVmcCoreInp, *pFlukaVmcFlukaMat, *pFlukaVmcInp;
1150     TString sFlukaVmcCoreInp = getenv("ALICE_ROOT");
1151     sFlukaVmcCoreInp +="/TFluka/input/";
1152     TString sFlukaVmcTmp = "flukaMat.inp";
1153     TString sFlukaVmcInp = GetInputFileName();
1154     sFlukaVmcCoreInp += GetCoreInputFileName();
1155     
1156 // Open files 
1157     if ((pFlukaVmcCoreInp = fopen(sFlukaVmcCoreInp.Data(),"r")) == NULL) {
1158         Warning("InitPhysics", "\nCannot open file %s\n",sFlukaVmcCoreInp.Data());
1159         exit(1);
1160     }
1161     if ((pFlukaVmcFlukaMat = fopen(sFlukaVmcTmp.Data(),"r")) == NULL) {
1162         Warning("InitPhysics", "\nCannot open file %s\n",sFlukaVmcTmp.Data());
1163         exit(1);
1164     }
1165     if ((pFlukaVmcInp = fopen(sFlukaVmcInp.Data(),"w")) == NULL) {
1166         Warning("InitPhysics", "\nCannot open file %s\n",sFlukaVmcInp.Data());
1167         exit(1);
1168     }
1169
1170 // Copy core input file 
1171     Char_t sLine[255];
1172     Float_t fEventsPerRun;
1173     
1174     while ((fgets(sLine,255,pFlukaVmcCoreInp)) != NULL) {
1175         if (strncmp(sLine,"GEOEND",6) != 0)
1176             fprintf(pFlukaVmcInp,"%s",sLine); // copy until GEOEND card
1177         else {
1178             fprintf(pFlukaVmcInp,"GEOEND\n");   // add GEOEND card
1179             goto flukamat;
1180         }
1181     } // end of while until GEOEND card
1182     
1183
1184  flukamat:
1185     while ((fgets(sLine,255,pFlukaVmcFlukaMat)) != NULL) { // copy flukaMat.inp file
1186         fprintf(pFlukaVmcInp,"%s\n",sLine);
1187     }
1188     
1189     while ((fgets(sLine,255,pFlukaVmcCoreInp)) != NULL) { 
1190         if (strncmp(sLine,"START",5) != 0)
1191             fprintf(pFlukaVmcInp,"%s\n",sLine);
1192         else {
1193             sscanf(sLine+10,"%10f",&fEventsPerRun);
1194             goto fin;
1195         }
1196     } //end of while until START card
1197     
1198  fin:
1199
1200     
1201 // Pass information to configuration objects
1202     
1203     Float_t fLastMaterial = fGeom->GetLastMaterialIndex();
1204     TFlukaConfigOption::SetStaticInfo(pFlukaVmcInp, 3, fLastMaterial, fGeom);
1205     
1206     TIter next(fUserConfig);
1207     TFlukaConfigOption* proc;
1208     while((proc = dynamic_cast<TFlukaConfigOption*> (next()))) proc->WriteFlukaInputCards();
1209 //
1210 // Process Fluka specific scoring options
1211 //
1212     TFlukaScoringOption::SetStaticInfo(pFlukaVmcInp, fGeom);
1213     Float_t loginp        = -49.0;
1214     Int_t inp             = 0;
1215     Int_t nscore          = fUserScore->GetEntries();
1216     
1217     TFlukaScoringOption *mopo = 0;
1218     TFlukaScoringOption *mopi = 0;
1219
1220     for (Int_t isc = 0; isc < nscore; isc++) 
1221     {
1222         mopo = dynamic_cast<TFlukaScoringOption*> (fUserScore->At(isc));
1223         char*    fileName = mopo->GetFileName();
1224         Int_t    size     = strlen(fileName);
1225         Float_t  lun      = -1.;
1226 //
1227 // Check if new output file has to be opened
1228         for (Int_t isci = 0; isci < isc; isci++) {
1229
1230         
1231             mopi = dynamic_cast<TFlukaScoringOption*> (fUserScore->At(isci));
1232             if(strncmp(mopi->GetFileName(), fileName, size)==0) {
1233                 //
1234                 // No, the file already exists
1235                 lun = mopi->GetLun();
1236                 mopo->SetLun(lun);
1237                 break;
1238             }
1239         } // inner loop
1240
1241         if (lun == -1.) {
1242             // Open new output file
1243             inp++;
1244             mopo->SetLun(loginp + inp);
1245             mopo->WriteOpenFlukaFile();
1246         }
1247         mopo->WriteFlukaInputCards();
1248     }
1249
1250 // Add RANDOMIZ card
1251     fprintf(pFlukaVmcInp,"RANDOMIZ  %10.1f%10.0f\n", 1., Float_t(gRandom->GetSeed()));
1252 // Add START and STOP card
1253     fprintf(pFlukaVmcInp,"START     %10.1f\n",fEventsPerRun);
1254     fprintf(pFlukaVmcInp,"STOP      \n");
1255    
1256   
1257 // Close files
1258    fclose(pFlukaVmcCoreInp);
1259    fclose(pFlukaVmcFlukaMat);
1260    fclose(pFlukaVmcInp);
1261
1262
1263 //
1264 // Initialisation needed for Cerenkov photon production and transport
1265     TObjArray *matList = GetFlukaMaterials();
1266     Int_t nmaterial =  matList->GetEntriesFast();
1267     fMaterials = new Int_t[nmaterial+25];
1268     
1269     for (Int_t im = 0; im < nmaterial; im++)
1270     {
1271         TGeoMaterial* material = dynamic_cast<TGeoMaterial*> (matList->At(im));
1272         Int_t idmat = material->GetIndex();
1273         fMaterials[idmat] = im;
1274     }
1275 } // end of InitPhysics
1276
1277
1278 //______________________________________________________________________________ 
1279 void TFluka::SetMaxStep(Double_t step)
1280 {
1281 // Set the maximum step size
1282 //    if (step > 1.e4) return;
1283     
1284 //    Int_t mreg=0, latt=0;
1285 //    fGeom->GetCurrentRegion(mreg, latt);
1286     Int_t mreg = fGeom->GetCurrentRegion();
1287     STEPSZ.stepmx[mreg - 1] = step;
1288 }
1289
1290
1291 Double_t TFluka::MaxStep() const
1292 {
1293 // Return the maximum for current medium
1294     Int_t mreg, latt;
1295     fGeom->GetCurrentRegion(mreg, latt);
1296     return (STEPSZ.stepmx[mreg - 1]);
1297 }
1298
1299 //______________________________________________________________________________ 
1300 void TFluka::SetMaxNStep(Int_t)
1301 {
1302 // SetMaxNStep is dummy procedure in TFluka !
1303   if (fVerbosityLevel >=3)
1304   cout << "SetMaxNStep is dummy procedure in TFluka !" << endl;
1305 }
1306
1307 //______________________________________________________________________________ 
1308 void TFluka::SetUserDecay(Int_t)
1309 {
1310 // SetUserDecay is dummy procedure in TFluka !
1311   if (fVerbosityLevel >=3)
1312   cout << "SetUserDecay is dummy procedure in TFluka !" << endl;
1313 }
1314
1315 //
1316 // dynamic properties
1317 //
1318 //______________________________________________________________________________ 
1319 void TFluka::TrackPosition(TLorentzVector& position) const
1320 {
1321 // Return the current position in the master reference frame of the
1322 // track being transported
1323 // TRACKR.atrack = age of the particle
1324 // TRACKR.xtrack = x-position of the last point
1325 // TRACKR.ytrack = y-position of the last point
1326 // TRACKR.ztrack = z-position of the last point
1327   FlukaCallerCode_t caller = GetCaller();
1328   if (caller == kENDRAW    || caller == kUSDRAW || 
1329       caller == kBXExiting || caller == kBXEntering || 
1330       caller == kUSTCKV) { 
1331       position.SetX(GetXsco());
1332       position.SetY(GetYsco());
1333       position.SetZ(GetZsco());
1334       position.SetT(TRACKR.atrack);
1335   }
1336   else if (caller == kMGDRAW) {
1337       Int_t i = -1;
1338       if ((i = fPrimaryElectronIndex) > -1) {
1339           // Primary Electron Ionisation
1340           Double_t x, y, z;
1341           GetPrimaryElectronPosition(i, x, y, z);
1342           position.SetX(x);
1343           position.SetY(y);
1344           position.SetZ(z);
1345           position.SetT(TRACKR.atrack);
1346       } else {
1347           position.SetX(TRACKR.xtrack[TRACKR.ntrack]);
1348           position.SetY(TRACKR.ytrack[TRACKR.ntrack]);
1349           position.SetZ(TRACKR.ztrack[TRACKR.ntrack]);
1350           position.SetT(TRACKR.atrack);
1351       }
1352   }
1353   else if (caller == kSODRAW) { 
1354       Int_t ist = FLKSTK.npflka;
1355       position.SetX(FLKSTK.xflk[ist]);
1356       position.SetY(FLKSTK.yflk[ist]);
1357       position.SetZ(FLKSTK.zflk[ist]);
1358       position.SetT(FLKSTK.agestk[ist]);
1359   } else if (caller == kMGResumedTrack) { 
1360       position.SetX(TRACKR.spausr[0]);
1361       position.SetY(TRACKR.spausr[1]);
1362       position.SetZ(TRACKR.spausr[2]);
1363       position.SetT(TRACKR.spausr[3]);
1364   }
1365   else
1366       Warning("TrackPosition","position not available");
1367 }
1368
1369 //______________________________________________________________________________ 
1370 void TFluka::TrackPosition(Double_t& x, Double_t& y, Double_t& z) const
1371 {
1372 // Return the current position in the master reference frame of the
1373 // track being transported
1374 // TRACKR.atrack = age of the particle
1375 // TRACKR.xtrack = x-position of the last point
1376 // TRACKR.ytrack = y-position of the last point
1377 // TRACKR.ztrack = z-position of the last point
1378   FlukaCallerCode_t caller = GetCaller();
1379   if (caller == kENDRAW    || caller == kUSDRAW || 
1380       caller == kBXExiting || caller == kBXEntering || 
1381       caller == kUSTCKV) { 
1382       x = GetXsco();
1383       y = GetYsco();
1384       z = GetZsco();
1385   }
1386   else if (caller == kMGDRAW) { 
1387       Int_t i = -1;
1388       if ((i = fPrimaryElectronIndex) > -1) {
1389           GetPrimaryElectronPosition(i, x, y, z);
1390       } else {
1391           x = TRACKR.xtrack[TRACKR.ntrack];
1392           y = TRACKR.ytrack[TRACKR.ntrack];
1393           z = TRACKR.ztrack[TRACKR.ntrack];
1394       }
1395   }
1396   else if (caller == kSODRAW) { 
1397       Int_t ist = FLKSTK.npflka;
1398       x = FLKSTK.xflk[ist];
1399       y = FLKSTK.yflk[ist];
1400       z = FLKSTK.zflk[ist];
1401   }
1402   else if (caller == kMGResumedTrack) {
1403       x = TRACKR.spausr[0];
1404       y = TRACKR.spausr[1];
1405       z = TRACKR.spausr[2];
1406   }
1407   else
1408       Warning("TrackPosition","position not available");
1409 }
1410
1411 //______________________________________________________________________________ 
1412 void TFluka::TrackMomentum(TLorentzVector& momentum) const
1413 {
1414 // Return the direction and the momentum (GeV/c) of the track
1415 // currently being transported
1416 // TRACKR.ptrack = momentum of the particle (not always defined, if
1417 //               < 0 must be obtained from etrack) 
1418 // TRACKR.cx,y,ztrck = direction cosines of the current particle
1419 // TRACKR.etrack = total energy of the particle
1420 // TRACKR.jtrack = identity number of the particle
1421 // PAPROP.am[TRACKR.jtrack] = particle mass in gev
1422   FlukaCallerCode_t  caller = GetCaller();
1423   FlukaProcessCode_t icode  = GetIcode();
1424   
1425   if (caller != kEEDRAW         && 
1426       caller != kMGResumedTrack && 
1427       caller != kSODRAW         &&
1428       (caller != kENDRAW || (icode != kEMFSCOstopping1 && icode != kEMFSCOstopping2))) {
1429       if (TRACKR.ptrack >= 0) {
1430           momentum.SetPx(TRACKR.ptrack*TRACKR.cxtrck);
1431           momentum.SetPy(TRACKR.ptrack*TRACKR.cytrck);
1432           momentum.SetPz(TRACKR.ptrack*TRACKR.cztrck);
1433           momentum.SetE(TRACKR.etrack);
1434           return;
1435       }
1436       else {
1437           Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack - ParticleMassFPC(TRACKR.jtrack) * ParticleMassFPC(TRACKR.jtrack));
1438           momentum.SetPx(p*TRACKR.cxtrck);
1439           momentum.SetPy(p*TRACKR.cytrck);
1440           momentum.SetPz(p*TRACKR.cztrck);
1441           momentum.SetE(TRACKR.etrack);
1442           return;
1443       }
1444   } else if  (caller == kMGResumedTrack) {
1445       momentum.SetPx(TRACKR.spausr[4]);
1446       momentum.SetPy(TRACKR.spausr[5]);
1447       momentum.SetPz(TRACKR.spausr[6]);
1448       momentum.SetE (TRACKR.spausr[7]);
1449       return;
1450   } else if (caller == kENDRAW && (icode == kEMFSCOstopping1 || icode == kEMFSCOstopping2)) {
1451       momentum.SetPx(0.);
1452       momentum.SetPy(0.);
1453       momentum.SetPz(0.);
1454       momentum.SetE(TrackMass());
1455       
1456   } else if (caller == kSODRAW) {
1457       Int_t ist  = FLKSTK.npflka;
1458       Double_t p = FLKSTK.pmoflk[ist];
1459       Int_t ifl  = FLKSTK.iloflk[ist];
1460       Double_t m = PAPROP.am[ifl + 6];
1461       Double_t e = TMath::Sqrt(p * p + m * m);
1462       momentum.SetPx(p * FLKSTK.txflk[ist]);
1463       momentum.SetPy(p * FLKSTK.tyflk[ist]);
1464       momentum.SetPz(p * FLKSTK.tzflk[ist]);
1465       momentum.SetE(e);
1466   }
1467   else
1468     Warning("TrackMomentum","momentum not available");
1469 }
1470
1471 //______________________________________________________________________________ 
1472 void TFluka::TrackMomentum(Double_t& px, Double_t& py, Double_t& pz, Double_t& e) const
1473 {
1474 // Return the direction and the momentum (GeV/c) of the track
1475 // currently being transported
1476 // TRACKR.ptrack = momentum of the particle (not always defined, if
1477 //               < 0 must be obtained from etrack) 
1478 // TRACKR.cx,y,ztrck = direction cosines of the current particle
1479 // TRACKR.etrack = total energy of the particle
1480 // TRACKR.jtrack = identity number of the particle
1481 // PAPROP.am[TRACKR.jtrack] = particle mass in gev
1482   FlukaCallerCode_t   caller = GetCaller();
1483   FlukaProcessCode_t  icode  = GetIcode();
1484   if (caller != kEEDRAW         && 
1485       caller != kMGResumedTrack && 
1486       caller != kSODRAW         &&
1487       (caller != kENDRAW || (icode != kEMFSCOstopping1 && icode != kEMFSCOstopping2))) {
1488     if (TRACKR.ptrack >= 0) {
1489       px = TRACKR.ptrack*TRACKR.cxtrck;
1490       py = TRACKR.ptrack*TRACKR.cytrck;
1491       pz = TRACKR.ptrack*TRACKR.cztrck;
1492       e  = TRACKR.etrack;
1493       return;
1494     }
1495     else {
1496       Double_t p = sqrt(TRACKR.etrack * TRACKR.etrack - ParticleMassFPC(TRACKR.jtrack) *  ParticleMassFPC(TRACKR.jtrack));
1497       px = p*TRACKR.cxtrck;
1498       py = p*TRACKR.cytrck;
1499       pz = p*TRACKR.cztrck;
1500       e  = TRACKR.etrack;
1501       return;
1502     }
1503   } else if (caller == kMGResumedTrack) {
1504       px = TRACKR.spausr[4];
1505       py = TRACKR.spausr[5];
1506       pz = TRACKR.spausr[6];
1507       e  = TRACKR.spausr[7];
1508       return;
1509   } else if (caller == kENDRAW && (icode == kEMFSCOstopping1 || icode == kEMFSCOstopping2)) {
1510       px = 0.;
1511       py = 0.;
1512       pz = 0.;
1513       e  = TrackMass();
1514   } else if (caller == kSODRAW) {
1515       Int_t ist  = FLKSTK.npflka;
1516       Double_t p = FLKSTK.pmoflk[ist];
1517       Int_t ifl  = FLKSTK.iloflk[ist];
1518       Double_t m = PAPROP.am[ifl + 6];
1519                e = TMath::Sqrt(p * p + m * m);
1520       px = p * FLKSTK.txflk[ist];
1521       py = p * FLKSTK.tyflk[ist];
1522       pz = p * FLKSTK.tzflk[ist];
1523   }
1524   else
1525       Warning("TrackMomentum","momentum not available");
1526 }
1527
1528 //______________________________________________________________________________ 
1529 Double_t TFluka::TrackStep() const
1530 {
1531 // Return the length in centimeters of the current step
1532 // TRACKR.ctrack = total curved path
1533     FlukaCallerCode_t caller = GetCaller();
1534     if (caller == kBXEntering || caller == kBXExiting || 
1535         caller == kENDRAW     || caller == kUSDRAW || 
1536         caller == kUSTCKV     || caller == kMGResumedTrack ||
1537         caller == kSODRAW)
1538         return 0.0;
1539   else if (caller == kMGDRAW)
1540     return TRACKR.ctrack;
1541   else {
1542     Warning("TrackStep", "track step not available");
1543     return 0.0;
1544   }  
1545 }
1546
1547 //______________________________________________________________________________ 
1548 Double_t TFluka::TrackLength() const
1549 {
1550 // TRACKR.cmtrck = cumulative curved path since particle birth
1551   FlukaCallerCode_t caller = GetCaller();
1552   if (caller == kBXEntering || caller == kBXExiting || 
1553       caller == kENDRAW || caller == kUSDRAW || caller == kMGDRAW || 
1554       caller == kUSTCKV) 
1555     return TRACKR.cmtrck;
1556   else if (caller == kMGResumedTrack) 
1557     return TRACKR.spausr[8];
1558   else {
1559     Warning("TrackLength", "track length not available");
1560     return 0.0;
1561   } 
1562 }
1563
1564 //______________________________________________________________________________ 
1565 Double_t TFluka::TrackTime() const
1566 {
1567 // Return the current time of flight of the track being transported
1568 // TRACKR.atrack = age of the particle
1569   FlukaCallerCode_t caller = GetCaller();
1570   if (caller == kBXEntering || caller == kBXExiting || 
1571       caller == kENDRAW     || caller == kUSDRAW    || caller == kMGDRAW || 
1572       caller == kUSTCKV)
1573     return TRACKR.atrack;
1574   else if (caller == kMGResumedTrack)
1575     return TRACKR.spausr[3];
1576   else if (caller == kSODRAW) {
1577       return (FLKSTK.agestk[FLKSTK.npflka]);
1578   }
1579   else {
1580     Warning("TrackTime", "track time not available");
1581     return 0.0;
1582   }   
1583 }
1584
1585 //______________________________________________________________________________ 
1586 Double_t TFluka::Edep() const
1587 {
1588 // Energy deposition
1589 // if TRACKR.ntrack = 0, TRACKR.mtrack = 0:
1590 // -->local energy deposition (the value and the point are not recorded in TRACKR)
1591 //    but in the variable "rull" of the procedure "endraw.cxx"
1592 // if TRACKR.ntrack > 0, TRACKR.mtrack = 0:
1593 // -->no energy loss along the track
1594 // if TRACKR.ntrack > 0, TRACKR.mtrack > 0:
1595 // -->energy loss distributed along the track
1596 // TRACKR.dtrack = energy deposition of the jth deposition event
1597
1598   // If coming from bxdraw we have 2 steps of 0 length and 0 edep
1599   // If coming from usdraw we just signal particle production - no edep
1600   // If just first time after resuming, no edep for the primary
1601   FlukaCallerCode_t caller = GetCaller();
1602     
1603   if (caller == kBXExiting || caller == kBXEntering || 
1604       caller == kUSDRAW    || caller == kMGResumedTrack ||
1605       caller == kSODRAW) 
1606       return 0.0;
1607   Double_t sum = 0;
1608   Int_t i = -1;
1609   
1610   // Material with primary ionisation activated but number of primary electrons nprim = 0
1611   if (fPrimaryElectronIndex == -2) return 0.0;
1612   // nprim > 0
1613   if ((i = fPrimaryElectronIndex) > -1) {
1614       // Primary ionisation
1615       sum = GetPrimaryElectronKineticEnergy(i);
1616       if (sum > 100.) {
1617           printf("edep > 100. %d %d %f \n", i, ALLDLT.nalldl, sum);
1618       }
1619       return sum;
1620   } else {
1621       // Normal ionisation
1622       if (TRACKR.mtrack > 1) printf("Edep: %6d\n", TRACKR.mtrack);
1623       
1624       for ( Int_t j=0;j<TRACKR.mtrack;j++) {
1625           sum +=TRACKR.dtrack[j];  
1626       }
1627       if (TRACKR.ntrack == 0 && TRACKR.mtrack == 0)
1628           return fRull + sum;
1629       else {
1630           return sum;
1631       }
1632   }
1633 }
1634
1635 //______________________________________________________________________________ 
1636 Int_t TFluka::CorrectFlukaId() const
1637 {
1638    // since we don't put photons and e- created bellow transport cut on the vmc stack
1639    // and there is a call to endraw for energy deposition for each of them
1640    // and they have the track number of their parent, but different identity (pdg)
1641    // so we want to assign also their parent identity.
1642
1643    if( (IsTrackStop())
1644         && TRACKR.ispusr[mkbmx2 - 4] == TRACKR.ispusr[mkbmx2 - 1]
1645         && TRACKR.jtrack != TRACKR.ispusr[mkbmx2 - 3] ) {
1646       if (fVerbosityLevel >=3)
1647          cout << "CorrectFlukaId() for icode=" << GetIcode()
1648                << " track=" << TRACKR.ispusr[mkbmx2 - 1]
1649                << " current PDG=" << PDGFromId(TRACKR.jtrack)
1650                << " assign parent PDG=" << PDGFromId(TRACKR.ispusr[mkbmx2 - 3]) << endl;
1651       return TRACKR.ispusr[mkbmx2 - 3]; // assign parent identity
1652    }
1653    if (TRACKR.jtrack <= 64){
1654        return TRACKR.jtrack;
1655    } else {
1656        return TRACKR.j0trck;
1657    }
1658 }
1659
1660
1661 //______________________________________________________________________________ 
1662 Int_t TFluka::TrackPid() const
1663 {
1664 // Return the id of the particle transported
1665 // TRACKR.jtrack = identity number of the particle
1666   FlukaCallerCode_t caller = GetCaller();
1667   if (caller != kEEDRAW && caller != kSODRAW) {
1668      return PDGFromId( CorrectFlukaId() );
1669   }
1670   else if (caller == kSODRAW) {
1671       return PDGFromId(FLKSTK.iloflk[FLKSTK.npflka]);
1672   }
1673   else
1674     return -1000;
1675 }
1676
1677 //______________________________________________________________________________ 
1678 Double_t TFluka::TrackCharge() const
1679 {
1680 // Return charge of the track currently transported
1681 // PAPROP.ichrge = electric charge of the particle
1682 // TRACKR.jtrack = identity number of the particle
1683     
1684   FlukaCallerCode_t caller = GetCaller();
1685   if (caller != kEEDRAW && caller != kSODRAW) 
1686      return PAPROP.ichrge[CorrectFlukaId() + 6];
1687   else if (caller == kSODRAW) {
1688       Int_t ifl =  PDGFromId(FLKSTK.iloflk[FLKSTK.npflka]);
1689       return PAPROP.ichrge[ifl + 6];
1690   }
1691   else
1692     return -1000.0;
1693 }
1694
1695 //______________________________________________________________________________ 
1696 Double_t TFluka::TrackMass() const
1697 {
1698 // PAPROP.am = particle mass in GeV
1699 // TRACKR.jtrack = identity number of the particle
1700   FlukaCallerCode_t caller = GetCaller();
1701   if (caller != kEEDRAW && caller != kSODRAW)
1702      return PAPROP.am[CorrectFlukaId()+6];
1703   else if (caller == kSODRAW) {
1704       Int_t ifl =  PDGFromId(FLKSTK.iloflk[FLKSTK.npflka]);
1705       return PAPROP.am[ifl + 6];
1706   }
1707   else
1708     return -1000.0;
1709 }
1710
1711 //______________________________________________________________________________ 
1712 Double_t TFluka::Etot() const
1713 {
1714 // TRACKR.etrack = total energy of the particle
1715   FlukaCallerCode_t caller = GetCaller();
1716   if (caller != kEEDRAW && caller != kSODRAW)
1717     return TRACKR.etrack;
1718   else if (caller == kSODRAW) {
1719       Int_t ist  = FLKSTK.npflka;
1720       Double_t p = FLKSTK.pmoflk[ist];
1721       Int_t ifl  = FLKSTK.iloflk[ist];
1722       Double_t m = PAPROP.am[ifl + 6];
1723       Double_t e = TMath::Sqrt(p * p + m * m);
1724       return e;
1725   }
1726   else
1727     return -1000.0;
1728 }
1729
1730 //
1731 // track status
1732 //
1733 //______________________________________________________________________________ 
1734 Bool_t   TFluka::IsNewTrack() const
1735 {
1736 // Return true for the first call of Stepping()
1737    return fTrackIsNew;
1738 }
1739
1740 void     TFluka::SetTrackIsNew(Bool_t flag)
1741 {
1742 // Return true for the first call of Stepping()
1743    fTrackIsNew = flag;
1744
1745 }
1746
1747
1748 //______________________________________________________________________________ 
1749 Bool_t   TFluka::IsTrackInside() const
1750 {
1751 // True if the track is not at the boundary of the current volume
1752 // In Fluka a step is always inside one kind of material
1753 // If the step would go behind the region of one material,
1754 // it will be shortened to reach only the boundary.
1755 // Therefore IsTrackInside() is always true.
1756   FlukaCallerCode_t caller = GetCaller();
1757   if (caller == kBXEntering || caller == kBXExiting)
1758     return 0;
1759   else
1760     return 1;
1761 }
1762
1763 //______________________________________________________________________________ 
1764 Bool_t   TFluka::IsTrackEntering() const
1765 {
1766 // True if this is the first step of the track in the current volume
1767
1768   FlukaCallerCode_t caller = GetCaller();
1769   if (caller == kBXEntering)
1770     return 1;
1771   else return 0;
1772 }
1773
1774 //______________________________________________________________________________ 
1775 Bool_t   TFluka::IsTrackExiting() const
1776 {
1777 // True if track is exiting volume
1778 //
1779   FlukaCallerCode_t caller = GetCaller();
1780   if (caller == kBXExiting)
1781     return 1;
1782   else return 0;
1783 }
1784
1785 //______________________________________________________________________________ 
1786 Bool_t   TFluka::IsTrackOut() const
1787 {
1788 // True if the track is out of the setup
1789 // means escape
1790   FlukaProcessCode_t icode = GetIcode();
1791     
1792   if (icode == kKASKADescape ||
1793       icode == kEMFSCOescape ||
1794       icode == kKASNEUescape ||
1795       icode == kKASHEAescape ||
1796       icode == kKASOPHescape) 
1797        return 1;
1798   else return 0;
1799 }
1800
1801 //______________________________________________________________________________ 
1802 Bool_t   TFluka::IsTrackDisappeared() const
1803 {
1804 // All inelastic interactions and decays
1805 // fIcode from usdraw
1806   FlukaProcessCode_t icode = GetIcode();
1807   if (icode == kKASKADinelint    || // inelastic interaction
1808       icode == kKASKADdecay      || // particle decay
1809       icode == kKASKADdray       || // delta ray generation by hadron
1810       icode == kKASKADpair       || // direct pair production
1811       icode == kKASKADbrems      || // bremsstrahlung (muon)
1812       icode == kEMFSCObrems      || // bremsstrahlung (electron)
1813       icode == kEMFSCOmoller     || // Moller scattering
1814       icode == kEMFSCObhabha     || // Bhaba scattering
1815       icode == kEMFSCOanniflight || // in-flight annihilation
1816       icode == kEMFSCOannirest   || // annihilation at rest
1817       icode == kEMFSCOpair       || // pair production
1818       icode == kEMFSCOcompton    || // Compton scattering
1819       icode == kEMFSCOphotoel    || // Photoelectric effect
1820       icode == kKASNEUhadronic   || // hadronic interaction
1821       icode == kKASHEAdray          // delta-ray
1822       ) return 1;
1823   else return 0;
1824 }
1825
1826 //______________________________________________________________________________ 
1827 Bool_t   TFluka::IsTrackStop() const
1828 {
1829 // True if the track energy has fallen below the threshold
1830 // means stopped by signal or below energy threshold
1831   FlukaProcessCode_t icode = GetIcode();
1832   if (icode == kKASKADstopping  || // stopping particle
1833       icode == kKASKADtimekill  || // time kill 
1834       icode == kEMFSCOstopping1 || // below user-defined cut-off
1835       icode == kEMFSCOstopping2 || // below user cut-off
1836       icode == kEMFSCOtimekill  || // time kill
1837       icode == kKASNEUstopping  || // neutron below threshold
1838       icode == kKASNEUtimekill  || // time kill
1839       icode == kKASHEAtimekill  || // time kill
1840       icode == kKASOPHtimekill) return 1; // time kill
1841   else return 0;
1842 }
1843
1844 //______________________________________________________________________________ 
1845 Bool_t   TFluka::IsTrackAlive() const
1846 {
1847 // means not disappeared or not out
1848   if (IsTrackDisappeared() || IsTrackOut() ) return 0;
1849   else return 1;
1850 }
1851
1852 //
1853 // secondaries
1854 //
1855
1856 //______________________________________________________________________________ 
1857 Int_t TFluka::NSecondaries() const
1858
1859 {
1860 // Number of secondary particles generated in the current step
1861 // GENSTK.np = number of secondaries except light and heavy ions
1862 // FHEAVY.npheav = number of secondaries for light and heavy secondary ions
1863     FlukaCallerCode_t caller = GetCaller();
1864     if (caller == kUSDRAW)  // valid only after usdraw
1865         return GENSTK.np + FHEAVY.npheav;
1866     else if (caller == kUSTCKV) {
1867         // Cerenkov Photon production
1868         return fNCerenkov;
1869     }
1870     return 0;
1871 } // end of NSecondaries
1872
1873 //______________________________________________________________________________ 
1874 void TFluka::GetSecondary(Int_t isec, Int_t& particleId,
1875                 TLorentzVector& position, TLorentzVector& momentum)
1876 {
1877 // Copy particles from secondary stack to vmc stack
1878 //
1879
1880     FlukaCallerCode_t caller = GetCaller();
1881     if (caller == kUSDRAW) {  // valid only after usdraw
1882         if (GENSTK.np > 0) {
1883             // Hadronic interaction
1884             if (isec >= 0 && isec < GENSTK.np) {
1885                 particleId = PDGFromId(GENSTK.kpart[isec]);
1886                 position.SetX(fXsco);
1887                 position.SetY(fYsco);
1888                 position.SetZ(fZsco);
1889                 position.SetT(TRACKR.atrack);
1890                 momentum.SetPx(GENSTK.plr[isec]*GENSTK.cxr[isec]);
1891                 momentum.SetPy(GENSTK.plr[isec]*GENSTK.cyr[isec]);
1892                 momentum.SetPz(GENSTK.plr[isec]*GENSTK.czr[isec]);
1893                 momentum.SetE(GENSTK.tki[isec] + PAPROP.am[GENSTK.kpart[isec]+6]);
1894             }
1895             else if (isec >= GENSTK.np && isec < GENSTK.np + FHEAVY.npheav) {
1896                 Int_t jsec = isec - GENSTK.np;
1897                 particleId = FHEAVY.kheavy[jsec]; // this is Fluka id !!!
1898                 position.SetX(fXsco);
1899                 position.SetY(fYsco);
1900                 position.SetZ(fZsco);
1901                 position.SetT(TRACKR.atrack);
1902                 momentum.SetPx(FHEAVY.pheavy[jsec]*FHEAVY.cxheav[jsec]);
1903                 momentum.SetPy(FHEAVY.pheavy[jsec]*FHEAVY.cyheav[jsec]);
1904                 momentum.SetPz(FHEAVY.pheavy[jsec]*FHEAVY.czheav[jsec]);
1905                 if (FHEAVY.tkheav[jsec] >= 3 && FHEAVY.tkheav[jsec] <= 6)
1906                     momentum.SetE(FHEAVY.tkheav[jsec] + PAPROP.am[jsec+6]);
1907                 else if (FHEAVY.tkheav[jsec] > 6)
1908                     momentum.SetE(FHEAVY.tkheav[jsec] + FHEAVY.amnhea[jsec]); // to be checked !!!
1909             }
1910             else
1911                 Warning("GetSecondary","isec out of range");
1912         }
1913     } else if (caller == kUSTCKV) {
1914         Int_t index = OPPHST.lstopp - isec;
1915         position.SetX(OPPHST.xoptph[index]);
1916         position.SetY(OPPHST.yoptph[index]);
1917         position.SetZ(OPPHST.zoptph[index]);
1918         position.SetT(OPPHST.agopph[index]);
1919         Double_t p = OPPHST.poptph[index];
1920         
1921         momentum.SetPx(p * OPPHST.txopph[index]);
1922         momentum.SetPy(p * OPPHST.tyopph[index]);
1923         momentum.SetPz(p * OPPHST.tzopph[index]);
1924         momentum.SetE(p);
1925     }
1926     else
1927         Warning("GetSecondary","no secondaries available");
1928     
1929 } // end of GetSecondary
1930
1931
1932 //______________________________________________________________________________ 
1933 TMCProcess TFluka::ProdProcess(Int_t) const
1934
1935 {
1936 // Name of the process that has produced the secondary particles
1937 // in the current step
1938
1939     Int_t mugamma = (TRACKR.jtrack == kFLUKAphoton || 
1940                      TRACKR.jtrack == kFLUKAmuplus ||
1941                      TRACKR.jtrack == kFLUKAmuminus);
1942     FlukaProcessCode_t icode = GetIcode();
1943
1944     if      (icode == kKASKADdecay)                                   return kPDecay;
1945     else if (icode == kKASKADpair || icode == kEMFSCOpair)            return kPPair;
1946     else if (icode == kEMFSCOcompton)                                 return kPCompton;
1947     else if (icode == kEMFSCOphotoel)                                 return kPPhotoelectric;
1948     else if (icode == kKASKADbrems      || icode == kEMFSCObrems)     return kPBrem;
1949     else if (icode == kKASKADdray       || icode == kKASHEAdray)      return kPDeltaRay;
1950     else if (icode == kEMFSCOmoller     || icode == kEMFSCObhabha)    return kPDeltaRay;
1951     else if (icode == kEMFSCOanniflight || icode == kEMFSCOannirest)  return kPAnnihilation;
1952     else if (icode == kKASKADinelint) {
1953         if (!mugamma)                                                 return kPHadronic;
1954         else if (TRACKR.jtrack == kFLUKAphoton)                       return kPPhotoFission;
1955         else                                                          return kPMuonNuclear;
1956     }
1957     else if (icode == kEMFSCOrayleigh)                                return kPRayleigh;
1958 // Fluka codes 100, 300 and 400 still to be investigasted
1959     else                                                              return kPNoProcess;
1960 }
1961
1962
1963 Int_t TFluka::StepProcesses(TArrayI &proc) const
1964 {
1965   //
1966   // Return processes active in the current step
1967   //
1968     FlukaProcessCode_t icode = GetIcode();
1969     proc.Set(1);
1970     TMCProcess iproc;
1971     switch (icode) {
1972     case kKASKADtimekill:
1973     case kEMFSCOtimekill:
1974     case kKASNEUtimekill:
1975     case kKASHEAtimekill:
1976     case kKASOPHtimekill:
1977         iproc =  kPTOFlimit;
1978         break;
1979     case kKASKADstopping:
1980     case kKASKADescape:
1981     case kEMFSCOstopping1:
1982     case kEMFSCOstopping2:
1983     case kEMFSCOescape:
1984     case kKASNEUstopping:
1985     case kKASNEUescape:
1986     case kKASHEAescape:
1987     case kKASOPHescape:
1988         iproc = kPStop;
1989         break;
1990     case kKASOPHabsorption:
1991         iproc = kPLightAbsorption;
1992         break;
1993     case kKASOPHrefraction:
1994         iproc = kPLightRefraction;
1995     case kEMFSCOlocaldep : 
1996         iproc = kPPhotoelectric;
1997         break;
1998     default:
1999         iproc = ProdProcess(0);
2000     }
2001     proc[0] = iproc;
2002     return 1;
2003 }
2004 //______________________________________________________________________________ 
2005 Int_t TFluka::VolId2Mate(Int_t id) const
2006 {
2007 //
2008 // Returns the material number for a given volume ID
2009 //
2010    return fMCGeo->VolId2Mate(id);
2011 }
2012
2013 //______________________________________________________________________________ 
2014 const char* TFluka::VolName(Int_t id) const
2015 {
2016 //
2017 // Returns the volume name for a given volume ID
2018 //
2019    return fMCGeo->VolName(id);
2020 }
2021
2022 //______________________________________________________________________________ 
2023 Int_t TFluka::VolId(const Text_t* volName) const
2024 {
2025 //
2026 // Converts from volume name to volume ID.
2027 // Time consuming. (Only used during set-up)
2028 // Could be replaced by hash-table
2029 //
2030     char sname[20];
2031     Int_t len;
2032     strncpy(sname, volName, len = strlen(volName));
2033     sname[len] = 0;
2034     while (sname[len - 1] == ' ') sname[--len] = 0;
2035     return fMCGeo->VolId(sname);
2036 }
2037
2038 //______________________________________________________________________________ 
2039 Int_t TFluka::CurrentVolID(Int_t& copyNo) const
2040 {
2041 //
2042 // Return the logical id and copy number corresponding to the current fluka region
2043 //
2044   if (gGeoManager->IsOutside()) return 0;
2045   TGeoNode *node = gGeoManager->GetCurrentNode();
2046   copyNo = node->GetNumber();
2047   Int_t id = node->GetVolume()->GetNumber();
2048   return id;
2049
2050
2051 //______________________________________________________________________________ 
2052 Int_t TFluka::CurrentVolOffID(Int_t off, Int_t& copyNo) const
2053 {
2054 //
2055 // Return the logical id and copy number of off'th mother 
2056 // corresponding to the current fluka region
2057 //
2058   if (off<0 || off>gGeoManager->GetLevel()) return 0;
2059   if (off==0) return CurrentVolID(copyNo);
2060   TGeoNode *node = gGeoManager->GetMother(off);
2061   if (!node) return 0;
2062   copyNo = node->GetNumber();
2063   return node->GetVolume()->GetNumber();
2064 }
2065
2066 //______________________________________________________________________________ 
2067 const char* TFluka::CurrentVolName() const
2068 {
2069 //
2070 // Return the current volume name
2071 //
2072   if (gGeoManager->IsOutside()) return 0;
2073   return gGeoManager->GetCurrentVolume()->GetName();
2074 }
2075
2076 //______________________________________________________________________________ 
2077 const char* TFluka::CurrentVolOffName(Int_t off) const
2078 {
2079 //
2080 // Return the volume name of the off'th mother of the current volume
2081 //
2082   if (off<0 || off>gGeoManager->GetLevel()) return 0;
2083   if (off==0) return CurrentVolName();
2084   TGeoNode *node = gGeoManager->GetMother(off);
2085   if (!node) return 0;
2086   return node->GetVolume()->GetName();
2087 }
2088
2089 const char* TFluka::CurrentVolPath() {
2090   // Return the current volume path
2091   return gGeoManager->GetPath(); 
2092 }
2093 //______________________________________________________________________________ 
2094 Int_t TFluka::CurrentMaterial(Float_t & a, Float_t & z, 
2095                       Float_t & dens, Float_t & radl, Float_t & absl) const
2096 {
2097 //
2098 //  Return the current medium number and material properties
2099 //
2100   Int_t copy;
2101   Int_t id  =  TFluka::CurrentVolID(copy);
2102   Int_t med =  TFluka::VolId2Mate(id);
2103   TGeoVolume*     vol = gGeoManager->GetCurrentVolume();
2104   TGeoMaterial*   mat = vol->GetMaterial();
2105   a    = mat->GetA();
2106   z    = mat->GetZ();
2107   dens = mat->GetDensity();
2108   radl = mat->GetRadLen();
2109   absl = mat->GetIntLen();
2110   
2111   return med;
2112 }
2113
2114 //______________________________________________________________________________ 
2115 void TFluka::Gmtod(Float_t* xm, Float_t* xd, Int_t iflag)
2116 {
2117 // Transforms a position from the world reference frame
2118 // to the current volume reference frame.
2119 //
2120 //  Geant3 desription:
2121 //  ==================
2122 //       Computes coordinates XD (in DRS) 
2123 //       from known coordinates XM in MRS 
2124 //       The local reference system can be initialized by
2125 //         - the tracking routines and GMTOD used in GUSTEP
2126 //         - a call to GMEDIA(XM,NUMED)
2127 //         - a call to GLVOLU(NLEVEL,NAMES,NUMBER,IER) 
2128 //             (inverse routine is GDTOM) 
2129 //
2130 //        If IFLAG=1  convert coordinates 
2131 //           IFLAG=2  convert direction cosinus
2132 //
2133 // ---
2134    Double_t xmL[3], xdL[3];
2135    Int_t i;
2136    for (i=0;i<3;i++) xmL[i]=xm[i];
2137    if (iflag == 1) gGeoManager->MasterToLocal(xmL,xdL);
2138    else            gGeoManager->MasterToLocalVect(xmL,xdL);
2139    for (i=0;i<3;i++) xd[i] = xdL[i];
2140 }
2141   
2142 //______________________________________________________________________________ 
2143 void TFluka::Gmtod(Double_t* xm, Double_t* xd, Int_t iflag)
2144 {
2145 //
2146 // See Gmtod(Float_t*, Float_t*, Int_t)
2147 //
2148    if (iflag == 1) gGeoManager->MasterToLocal(xm,xd);
2149    else            gGeoManager->MasterToLocalVect(xm,xd);
2150 }
2151
2152 //______________________________________________________________________________ 
2153 void TFluka::Gdtom(Float_t* xd, Float_t* xm, Int_t iflag)
2154 {
2155 // Transforms a position from the current volume reference frame
2156 // to the world reference frame.
2157 //
2158 //  Geant3 desription:
2159 //  ==================
2160 //  Computes coordinates XM (Master Reference System
2161 //  knowing the coordinates XD (Detector Ref System)
2162 //  The local reference system can be initialized by
2163 //    - the tracking routines and GDTOM used in GUSTEP
2164 //    - a call to GSCMED(NLEVEL,NAMES,NUMBER)
2165 //        (inverse routine is GMTOD)
2166 // 
2167 //   If IFLAG=1  convert coordinates
2168 //      IFLAG=2  convert direction cosinus
2169 //
2170 // ---
2171    Double_t xmL[3], xdL[3];
2172    Int_t i;
2173    for (i=0;i<3;i++) xdL[i] = xd[i];
2174    if (iflag == 1) gGeoManager->LocalToMaster(xdL,xmL);
2175    else            gGeoManager->LocalToMasterVect(xdL,xmL);
2176    for (i=0;i<3;i++) xm[i]=xmL[i];
2177 }
2178
2179 //______________________________________________________________________________ 
2180 void TFluka::Gdtom(Double_t* xd, Double_t* xm, Int_t iflag)
2181 {
2182 //
2183 // See Gdtom(Float_t*, Float_t*, Int_t)
2184 //
2185    if (iflag == 1) gGeoManager->LocalToMaster(xd,xm);
2186    else            gGeoManager->LocalToMasterVect(xd,xm);
2187 }
2188
2189 //______________________________________________________________________________
2190 TObjArray *TFluka::GetFlukaMaterials()
2191 {
2192 //
2193 // Get array of Fluka materials
2194    return fGeom->GetMatList();
2195 }   
2196
2197 //______________________________________________________________________________
2198 void TFluka::SetMreg(Int_t l, Int_t lttc) 
2199 {
2200 // Set current fluka region
2201    fCurrentFlukaRegion = l;
2202    fGeom->SetMreg(l,lttc);
2203 }
2204
2205
2206
2207
2208 //______________________________________________________________________________
2209 TString TFluka::ParticleName(Int_t pdg) const
2210 {
2211     // Return particle name for particle with pdg code pdg.
2212     Int_t ifluka = IdFromPDG(pdg);
2213     return TString((CHPPRP.btype[ifluka - kFLUKAcodemin]), 8);
2214 }
2215  
2216
2217 //______________________________________________________________________________
2218 Double_t TFluka::ParticleMass(Int_t pdg) const
2219 {
2220     // Return particle mass for particle with pdg code pdg.
2221     Int_t ifluka = IdFromPDG(pdg);
2222     return (PAPROP.am[ifluka - kFLUKAcodemin]);
2223 }
2224
2225 //______________________________________________________________________________
2226 Double_t TFluka::ParticleMassFPC(Int_t fpc) const
2227 {
2228     // Return particle mass for particle with Fluka particle code fpc
2229     return (PAPROP.am[fpc - kFLUKAcodemin]);
2230 }
2231
2232 //______________________________________________________________________________
2233 Double_t TFluka::ParticleCharge(Int_t pdg) const
2234 {
2235     // Return particle charge for particle with pdg code pdg.
2236     Int_t ifluka = IdFromPDG(pdg);
2237     return Double_t(PAPROP.ichrge[ifluka - kFLUKAcodemin]);
2238 }
2239
2240 //______________________________________________________________________________
2241 Double_t TFluka::ParticleLifeTime(Int_t pdg) const
2242 {
2243     // Return particle lifetime for particle with pdg code pdg.
2244     Int_t ifluka = IdFromPDG(pdg);
2245     return (PAPROP.tmnlf[ifluka - kFLUKAcodemin]);
2246 }
2247
2248 //______________________________________________________________________________
2249 void TFluka::Gfpart(Int_t pdg, char* name, Int_t& type, Float_t& mass, Float_t& charge, Float_t& tlife)
2250 {
2251     // Retrieve particle properties for particle with pdg code pdg.
2252     
2253     strcpy(name, ParticleName(pdg).Data());
2254     type   = ParticleMCType(pdg);
2255     mass   = ParticleMass(pdg);
2256     charge = ParticleCharge(pdg);
2257     tlife  = ParticleLifeTime(pdg);
2258 }
2259
2260 //______________________________________________________________________________
2261 void TFluka::PrintHeader()
2262 {
2263     //
2264     // Print a header
2265     printf("\n");
2266     printf("\n");    
2267     printf("------------------------------------------------------------------------------\n");
2268     printf("- You are using the TFluka Virtual Monte Carlo Interface to FLUKA.           -\n");    
2269     printf("- Please see the file fluka.out for FLUKA output and licensing information.  -\n");    
2270     printf("------------------------------------------------------------------------------\n");
2271     printf("\n");
2272     printf("\n");    
2273 }
2274
2275
2276 #define pshckp pshckp_
2277 #define ustckv ustckv_
2278
2279
2280 extern "C" {
2281   void pshckp(Double_t & px, Double_t & py, Double_t & pz, Double_t & e,
2282               Double_t & vx, Double_t & vy, Double_t & vz, Double_t & tof,
2283               Double_t & polx, Double_t & poly, Double_t & polz, Double_t & wgt, Int_t& ntr)
2284   {
2285     //
2286     // Pushes one cerenkov photon to the stack
2287     //
2288     
2289     TFluka* fluka =  (TFluka*) gMC;
2290     TVirtualMCStack* cppstack = fluka->GetStack();
2291     Int_t parent =  TRACKR.ispusr[mkbmx2-1];
2292     cppstack->PushTrack(0, parent, 50000050,
2293                         px, py, pz, e,
2294                         vx, vy, vz, tof,
2295                         polx, poly, polz,
2296                         kPCerenkov, ntr, wgt, 0);
2297     if (fluka->GetVerbosityLevel() >= 3)
2298             printf("pshckp: track=%d parent=%d lattc=%d %s\n", ntr, parent, TRACKR.lt1trk, fluka->CurrentVolName());
2299   }
2300     
2301     void ustckv(Int_t & nphot, Int_t & mreg, Double_t & x, Double_t & y, Double_t & z)
2302     {
2303         //
2304         // Calls stepping in order to signal cerenkov production
2305         //
2306         TFluka *fluka = (TFluka*)gMC;
2307         fluka->SetMreg(mreg, TRACKR.lt1trk); //LTCLCM.mlatm1);
2308         fluka->SetXsco(x);
2309         fluka->SetYsco(y);
2310         fluka->SetZsco(z);
2311         fluka->SetNCerenkov(nphot);
2312         fluka->SetCaller(kUSTCKV);
2313         if (fluka->GetVerbosityLevel() >= 3)
2314             printf("ustckv: %10d mreg=%d lattc=%d  newlat=%d (%f, %f, %f) edep=%f vol=%s\n",
2315                     nphot, mreg, TRACKR.lt1trk, LTCLCM.newlat, x, y, z, fluka->Edep(), fluka->CurrentVolName());
2316    
2317     // check region lattice consistency (debug Ernesto)
2318     // *****************************************************
2319    Int_t nodeId;
2320    Int_t volId = fluka->CurrentVolID(nodeId);
2321    Int_t crtlttc = gGeoManager->GetCurrentNodeId()+1;
2322
2323    if( mreg != volId  && !gGeoManager->IsOutside() ) {
2324        cout << "  ustckv:   track=" << TRACKR.ispusr[mkbmx2-1] << " pdg=" << fluka->PDGFromId(TRACKR.jtrack)
2325             << " icode=" << fluka->GetIcode() << " gNstep=" << fluka->GetNstep() << endl
2326             << "               fluka   mreg=" << mreg << " mlttc=" << TRACKR.lt1trk << endl
2327             << "               TGeo   volId=" << volId << " crtlttc=" << crtlttc << endl
2328             << "     common TRACKR   lt1trk=" << TRACKR.lt1trk << " lt2trk=" << TRACKR.lt2trk << endl
2329             << "     common LTCLCM   newlat=" << LTCLCM.newlat << " mlatld=" <<  LTCLCM.mlatld << endl
2330             << "                     mlatm1=" << LTCLCM.mlatm1 << " mltsen=" <<  LTCLCM.mltsen << endl
2331             << "                     mltsm1=" << LTCLCM.mltsm1 << " mlattc=" << LTCLCM.mlattc << endl;
2332         if( TRACKR.lt1trk == crtlttc ) cout << "   *************************************************************" << endl;
2333     }
2334     // *****************************************************
2335
2336
2337
2338         (TVirtualMCApplication::Instance())->Stepping();
2339     }
2340 }
2341
2342 //______________________________________________________________________________
2343 void TFluka::AddParticlesToPdgDataBase() const
2344 {
2345
2346 //
2347 // Add particles to the PDG data base
2348
2349     TDatabasePDG *pdgDB = TDatabasePDG::Instance();
2350
2351     const Double_t kAu2Gev   = 0.9314943228;
2352     const Double_t khSlash   = 1.0545726663e-27;
2353     const Double_t kErg2Gev  = 1/1.6021773349e-3;
2354     const Double_t khShGev   = khSlash*kErg2Gev;
2355     const Double_t kYear2Sec = 3600*24*365.25;
2356 //
2357 // Ions
2358 //
2359   pdgDB->AddParticle("Deuteron","Deuteron",2*kAu2Gev+8.071e-3,kTRUE,
2360                      0,3,"Ion",GetIonPdg(1,2));
2361   pdgDB->AddParticle("Triton","Triton",3*kAu2Gev+14.931e-3,kFALSE,
2362                      khShGev/(12.33*kYear2Sec),3,"Ion",GetIonPdg(1,3));
2363   pdgDB->AddParticle("Alpha","Alpha",4*kAu2Gev+2.424e-3,kTRUE,
2364                      khShGev/(12.33*kYear2Sec),6,"Ion",GetIonPdg(2,4));
2365   pdgDB->AddParticle("HE3","HE3",3*kAu2Gev+14.931e-3,kFALSE,
2366                      0,6,"Ion",GetIonPdg(2,3));
2367 }
2368
2369 //
2370 // Info about primary ionization electrons
2371 //
2372
2373 //______________________________________________________________________________
2374 Int_t TFluka::GetNPrimaryElectrons()
2375 {
2376     // Get number of primary electrons
2377     return ALLDLT.nalldl;
2378 }
2379
2380 //______________________________________________________________________________
2381 Double_t TFluka::GetPrimaryElectronKineticEnergy(Int_t i) const
2382 {
2383     // Returns kinetic energy of primary electron i
2384
2385     Double_t ekin = -1.;
2386     
2387     if (i >= 0 && i < ALLDLT.nalldl) {
2388         ekin =  ALLDLT.talldl[i];
2389     } else {
2390         Warning("GetPrimaryElectronKineticEnergy",
2391                 "Primary electron index out of range %d %d \n",
2392                 i, ALLDLT.nalldl);
2393     }
2394     return ekin;
2395 }
2396
2397 void TFluka::GetPrimaryElectronPosition(Int_t i, Double_t& x, Double_t& y, Double_t& z) const
2398 {
2399     // Returns position  of primary electron i
2400         if (i >= 0 && i < ALLDLT.nalldl) {
2401             x = ALLDLT.xalldl[i];
2402             y = ALLDLT.yalldl[i];
2403             z = ALLDLT.zalldl[i];
2404             return;
2405         } else {
2406             Warning("GetPrimaryElectronPosition",
2407                     "Primary electron index out of range %d %d \n",
2408                     i, ALLDLT.nalldl);
2409             return;
2410         }
2411         return;
2412 }
2413
2414 Int_t TFluka::GetIonPdg(Int_t z, Int_t a, Int_t i) const
2415 {
2416 // Acording to
2417 // http://cepa.fnal.gov/psm/stdhep/pdg/montecarlorpp-2006.pdf
2418
2419   return 1000000000 + 10*1000*z + 10*a + i;
2420 }  
2421      
2422 void  TFluka::PrimaryIonisationStepping(Int_t nprim)
2423 {
2424 // Call Stepping for primary ionisation electrons
2425     Int_t i;
2426 // Protection against nprim > mxalld
2427
2428 // Multiple steps for nprim > 0
2429     if (nprim > 0) {
2430         for (i = 0; i < nprim; i++) {
2431             SetCurrentPrimaryElectronIndex(i);
2432             (TVirtualMCApplication::Instance())->Stepping();
2433             if (i == 0) SetTrackIsNew(kFALSE);
2434         }       
2435     } else {
2436         // No primary electron ionisation
2437         // Call Stepping anyway but flag nprim = 0 as index = -2
2438         SetCurrentPrimaryElectronIndex(-2);
2439         (TVirtualMCApplication::Instance())->Stepping();
2440     }
2441     // Reset the index
2442     SetCurrentPrimaryElectronIndex(-1);
2443 }