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