added stuff
[u/mrichter/AliRoot.git] / TFluka / source.cxx
1 // Fortran 
2 #include "TCallf77.h"
3
4 // Fluka commons
5 #include "Fdblprc.h"  //(DBLPRC) fluka common
6 #include "Fdimpar.h"  //(DIMPAR) fluka parameters
7 #include "Fsourcm.h"  //(EPISOR) fluka common
8 #include "Fflkstk.h"  //(FLKSTK)  fluka common
9 #include "Fsumcou.h"  //(SUMCOU)  fluka common
10 #include "Fpaprop.h"  //(PAPROP) fluka common
11 #include "Fltclcm.h"  //(LTCLCM) fluka common
12 #include "Fopphst.h"  //(OPPHST) fluka common
13
14 //Virutal MC
15 #include "TFluka.h"
16
17 #include "TVirtualMCStack.h"
18 //#include "TVirtualMCApplication.h"
19
20 #include "TParticle.h"
21 #include "TVector3.h"
22
23 //Other
24 #include <Riostream.h>
25
26 #ifndef WIN32
27 # define source source_
28 # define geocrs geocrs_
29 # define georeg georeg_
30 # define geohsm geohsm_
31 # define soevsv soevsv_
32 #else
33 # define source SOURCE
34 # define geocrs GEOCRS
35 # define georeg GEOREG
36 # define geohsm GEOHSM
37 # define soevsv SOEVSV
38 #endif
39
40 extern "C" {
41   //
42   // Prototypes for FLUKA functions
43   //
44   void type_of_call geocrs(Double_t &, Double_t &, Double_t &);
45   void type_of_call georeg(Double_t &, Double_t &, Double_t &, 
46                            Int_t &, Int_t &);
47   void type_of_call geohsm(Int_t &, Int_t &, Int_t &, Int_t &);
48   void type_of_call soevsv();
49  /*
50    *----------------------------------------------------------------------*
51    *                                                                      *
52    *     Created on 07 january 1990   by    Alfredo Ferrari & Paola Sala  *
53    *                                                   Infn - Milan       *
54    *                                                                      *
55    *     Last change on 21-jun-98     by    Alfredo Ferrari               *
56    *                                                                      *
57    *     C++ version on 27-sep-02     by    Isidro Gonzalez               *
58    *                                                                      *
59    *  This is just an example of a possible user written source routine.  *
60    *  note that the beam card still has some meaning - in the scoring the *
61    *  maximum momentum used in deciding the binning is taken from the     *
62    *  beam momentum.  Other beam card parameters are obsolete.            *
63    *                                                                      *
64    *----------------------------------------------------------------------*/
65
66   void source(Int_t& nomore) {
67 // Get the pointer to TFluka
68     TFluka* fluka = (TFluka*)gMC;
69
70     Int_t verbosityLevel = fluka->GetVerbosityLevel();
71     Bool_t debug = (verbosityLevel>=3)?kTRUE:kFALSE;
72     if (debug) {
73       cout << "==> source(" << nomore << ")" << endl;
74       cout << "\t* SOURCM.lsouit = " << (SOURCM.lsouit?'T':'F') << endl;
75     }  
76
77     static Bool_t lfirst = true;
78     static Bool_t particleIsPrimary = true;
79     static Bool_t lastParticleWasPrimary = true;
80
81     nomore = 0;
82
83     
84 //  Get the stack 
85     TVirtualMCStack* cppstack = fluka->GetStack();
86
87     TParticle* particle;
88     Int_t itrack = -1;
89     Int_t  nprim  = cppstack->GetNprimary();
90 //  Get the next particle from the stack
91     particle  = cppstack->PopNextTrack(itrack);
92     fluka->SetTrackIsNew(kTRUE);
93     if (itrack == (nprim - 1)) lfirst = true;
94 //  Is this a secondary not handled by Fluka, i.e. a particle added by user action ?
95     lastParticleWasPrimary = particleIsPrimary;
96     
97     if (itrack >= nprim) {
98         particleIsPrimary = kFALSE;
99     } else {
100         particleIsPrimary = kTRUE;
101     }
102
103     if (lfirst) {
104         SOURCM.tkesum = zerzer;
105         lfirst = false;
106         SOURCM.lussrc = true;
107     } else {
108 //
109 // Post-track actions for primary track
110 //
111         if (particleIsPrimary) {
112             TVirtualMCApplication::Instance()->PostTrack();
113             TVirtualMCApplication::Instance()->FinishPrimary();
114             if ((itrack%10)==0)
115                 cout << "=== TRACKING PRIMARY "<< itrack <<" ===" << endl;
116             //printf("=== TRACKING PRIMARY %d ===\n", itrack);
117         }
118     }
119
120     // Exit if itrack is negative (-1). Set lsouit to false to mark last track for this event
121
122     if (itrack<0) {
123       nomore = 1;
124       SOURCM.lsouit = false;
125       if (debug) {
126          cout << "\t* SOURCM.lsouit = " << (SOURCM.lsouit?'T':'F') << endl;
127          cout << "\t* No more particles. Exiting..." << endl;
128          cout << "<== source(" << nomore << ")" << endl;
129       }   
130       return;
131     }
132     
133     //
134     // Handle user event abortion
135     if (fluka->EventIsStopped()) {
136         printf("Event has been stopped by user !");
137         fluka->SetStopEvent(kFALSE);
138         nomore = 1;
139         SOURCM.lsouit = false;
140         return;
141     }
142
143     //Get some info about the particle and print it
144     //
145     //pdg code
146     Int_t pdg = particle->GetPdgCode();
147     TVector3 polarisation;
148     particle->GetPolarisation(polarisation);
149     if (debug) {
150        cout << "\t* Particle " << itrack << " retrieved..." << endl;
151        cout << "\t\t+ Name = " << particle->GetName() << endl;
152        cout << "\t\t+ PDG/Fluka code = " << pdg 
153             << " / " << fluka->IdFromPDG(pdg) << endl;
154        cout << "\t\t+ P = (" 
155             << particle->Px() << " , "
156             << particle->Py() << " , "
157             << particle->Pz() << " ) --> "
158             << particle->P() << " GeV "
159             << particle->Energy() << " GeV "
160             << particle->GetMass() << " GeV " << endl;
161     }   
162     /* Npflka is the stack counter: of course any time source is called it
163      * must be =0
164      */
165     /* Cosines (tx,ty,tz)*/
166     Double_t cosx = particle->Px()/particle->P();
167     Double_t cosy = particle->Py()/particle->P();
168     Double_t cosz = TMath::Sqrt(oneone - cosx*cosx - cosy*cosy);
169     if (particle->Pz() < 0.) cosz = -cosz;    
170
171     if (pdg != 50000050 &&  pdg !=  50000051) {
172         FLKSTK.npflka++;
173         Int_t ifl =  fluka-> IdFromPDG(pdg);
174         FLKSTK.iloflk[FLKSTK.npflka] = ifl;
175         /* Wtflk is the weight of the particle*/
176         FLKSTK.wtflk[FLKSTK.npflka] = oneone;
177         SUMCOU.weipri += FLKSTK.wtflk[FLKSTK.npflka];
178         
179         FLKSTK.loflk[FLKSTK.npflka] = 1;
180         
181         /* User dependent flag:*/
182         FLKSTK.louse[FLKSTK.npflka] = 0;
183
184         /* User dependent spare variables:*/
185         Int_t ispr = 0;
186         for (ispr = 0; ispr < mkbmx1; ispr++)
187             FLKSTK.sparek[FLKSTK.npflka][ispr] = zerzer;
188
189         /* User dependent spare flags:*/
190         for (ispr = 0; ispr < mkbmx2; ispr++)
191             FLKSTK.ispark[FLKSTK.npflka][ispr] = 0;
192
193         /* Save the track number of the stack particle:*/
194         FLKSTK.ispark[FLKSTK.npflka][mkbmx2-1] = itrack;
195         FLKSTK.nparma++;
196         FLKSTK.numpar[FLKSTK.npflka] = FLKSTK.nparma;
197         FLKSTK.nevent[FLKSTK.npflka] = 0;
198         FLKSTK.dfnear[FLKSTK.npflka] = +zerzer;
199         
200         /* Particle age (s)*/
201         FLKSTK.agestk[FLKSTK.npflka] = +zerzer;
202         FLKSTK.cmpath[FLKSTK.npflka] = +zerzer;
203         FLKSTK.aknshr[FLKSTK.npflka] = -twotwo;
204
205         /* Group number for "low" energy neutrons, set to 0 anyway*/
206         FLKSTK.igroup[FLKSTK.npflka] = 0;
207         
208         /* Kinetic energy */
209         Double_t p    = particle->P();
210         Double_t mass = PAPROP.am[ifl + 6];
211         FLKSTK.tkeflk[FLKSTK.npflka] =  TMath::Sqrt( p * p + mass * mass) - mass;
212         /* Particle momentum*/
213         FLKSTK.pmoflk [FLKSTK.npflka] = p;
214
215         FLKSTK.txflk [FLKSTK.npflka] = cosx;
216         FLKSTK.tyflk [FLKSTK.npflka] = cosy;
217         FLKSTK.tzflk [FLKSTK.npflka] = cosz;
218     
219         /* Polarization cosines:*/
220         if (polarisation.Mag()) {
221             Double_t cospolx = polarisation.Px() / polarisation.Mag();
222             Double_t cospoly = polarisation.Py() / polarisation.Mag();
223             Double_t cospolz = sqrt(oneone - cospolx * cospolx - cospoly * cospoly);
224             FLKSTK.txpol [FLKSTK.npflka] = cospolx;
225             FLKSTK.typol [FLKSTK.npflka] = cospoly;
226             FLKSTK.tzpol [FLKSTK.npflka] = cospolz;
227         }
228         else {
229             FLKSTK.txpol [FLKSTK.npflka] = -twotwo;
230             FLKSTK.typol [FLKSTK.npflka] = +zerzer;
231             FLKSTK.tzpol [FLKSTK.npflka] = +zerzer;
232         }
233
234         /* Particle coordinates*/
235         // Vertext coordinates;
236         FLKSTK.xflk [FLKSTK.npflka] = particle->Vx();
237         FLKSTK.yflk [FLKSTK.npflka] = particle->Vy();
238         FLKSTK.zflk [FLKSTK.npflka] = particle->Vz();
239
240         /*  Calculate the total kinetic energy of the primaries: don't change*/
241         Int_t st_ilo =  FLKSTK.iloflk[FLKSTK.npflka];
242         if ( st_ilo != 0 )
243             SOURCM.tkesum +=
244                 ((FLKSTK.tkeflk[FLKSTK.npflka] + PAPROP.amdisc[st_ilo+6])
245                  * FLKSTK.wtflk[FLKSTK.npflka]);
246         else
247             SOURCM.tkesum += (FLKSTK.tkeflk[FLKSTK.npflka] * FLKSTK.wtflk[FLKSTK.npflka]);
248
249         /*  Here we ask for the region number of the hitting point.
250          *     NRGFLK (LFLKSTK) = ...
251          *  The following line makes the starting region search much more
252          *  robust if particles are starting very close to a boundary:
253          */
254         geocrs( FLKSTK.txflk[FLKSTK.npflka],
255                 FLKSTK.tyflk[FLKSTK.npflka],
256                 FLKSTK.tzflk[FLKSTK.npflka] );
257     
258         Int_t idisc;
259
260         georeg ( FLKSTK.xflk[FLKSTK.npflka],
261                  FLKSTK.yflk[FLKSTK.npflka],
262                  FLKSTK.zflk[FLKSTK.npflka],
263                  FLKSTK.nrgflk[FLKSTK.npflka],
264                  idisc);//<-- dummy return variable not used
265         /*  Do not change these cards:*/
266         Int_t igeohsm1 = 1;
267         Int_t igeohsm2 = -11;
268         geohsm ( FLKSTK.nhspnt[FLKSTK.npflka], igeohsm1, igeohsm2, LTCLCM.mlattc );
269         FLKSTK.nlattc[FLKSTK.npflka] = LTCLCM.mlattc;
270         soevsv();
271     } else {
272         //
273         // Next particle is optical photon
274         //
275         OPPHST.lstopp++;
276         OPPHST.donear [OPPHST.lstopp - 1] = 0.;
277
278         OPPHST.xoptph [OPPHST.lstopp - 1] = particle->Vx();
279         OPPHST.yoptph [OPPHST.lstopp - 1] = particle->Vy();
280         OPPHST.zoptph [OPPHST.lstopp - 1] = particle->Vz();
281
282         OPPHST.txopph [OPPHST.lstopp - 1] = cosx;
283         OPPHST.tyopph [OPPHST.lstopp - 1] = cosy;
284         OPPHST.tzopph [OPPHST.lstopp - 1] = cosz;
285
286
287         if (polarisation.Mag()) {
288             Double_t cospolx = polarisation.Px() / polarisation.Mag();
289             Double_t cospoly = polarisation.Py() / polarisation.Mag();
290             Double_t cospolz = sqrt(oneone - cospolx * cospolx - cospoly * cospoly);
291             OPPHST.txpopp [OPPHST.lstopp - 1] = cospolx;
292             OPPHST.typopp [OPPHST.lstopp - 1] = cospoly;
293             OPPHST.tzpopp [OPPHST.lstopp - 1] = cospolz;
294         }
295         else {
296             OPPHST.txpopp [OPPHST.lstopp - 1] = -twotwo;
297             OPPHST.typopp [OPPHST.lstopp - 1] = +zerzer;
298             OPPHST.tzpopp [OPPHST.lstopp - 1] = +zerzer;
299         }
300
301         geocrs( OPPHST.txopph[OPPHST.lstopp - 1],
302                 OPPHST.tyopph[OPPHST.lstopp - 1],
303                 OPPHST.tzopph[OPPHST.lstopp - 1] );
304
305         Int_t idisc;
306
307         georeg ( OPPHST.xoptph[OPPHST.lstopp - 1],
308                  OPPHST.yoptph[OPPHST.lstopp - 1],
309                  OPPHST.zoptph[OPPHST.lstopp - 1],
310                  OPPHST.nregop[OPPHST.lstopp - 1],
311                  idisc);//<-- dummy return variable not used
312
313         OPPHST.wtopph [OPPHST.lstopp - 1] = particle->GetWeight();
314         OPPHST.poptph [OPPHST.lstopp - 1] = particle->P();
315         OPPHST.agopph [OPPHST.lstopp - 1] = particle->T();
316         OPPHST.cmpopp [OPPHST.lstopp - 1] = +zerzer;
317         OPPHST.loopph [OPPHST.lstopp - 1] = 0;
318         OPPHST.louopp [OPPHST.lstopp - 1] = itrack;
319         OPPHST.nlatop [OPPHST.lstopp - 1] = LTCLCM.mlattc;
320      }
321
322 //
323 //  Pre-track actions at for primary tracks
324 //
325     if (particleIsPrimary) {
326         TVirtualMCApplication::Instance()->BeginPrimary();
327         TVirtualMCApplication::Instance()->PreTrack();
328     }
329 //
330     if (debug) cout << "<== source(" << nomore << ")" << endl;
331   }
332 }