]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TFluka/source.cxx
Algorithm fix for some cluster topologies (Sacha)
[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 //  Get the stack 
84     TVirtualMCStack* cppstack = fluka->GetStack();
85
86     TParticle* particle;
87     Int_t itrack = -1;
88     Int_t  nprim  = cppstack->GetNprimary();
89 //  Get the next particle from the stack
90     particle  = cppstack->PopNextTrack(itrack);
91     fluka->SetTrackIsNew(kTRUE);
92
93 //  Is this a secondary not handled by Fluka, i.e. a particle added by user action ?
94     lastParticleWasPrimary = particleIsPrimary;
95     
96     if (itrack >= nprim) {
97         particleIsPrimary = kFALSE;
98     } else {
99         particleIsPrimary = kTRUE;
100     }
101
102     if (lfirst) {
103         SOURCM.tkesum = zerzer;
104         lfirst = false;
105         SOURCM.lussrc = true;
106     } else {
107 //
108 // Post-track actions for primary track
109 //
110         if (particleIsPrimary) {
111             TVirtualMCApplication::Instance()->PostTrack();
112             TVirtualMCApplication::Instance()->FinishPrimary();
113             if ((itrack%10)==0) printf("=== TRACKING PRIMARY %d ===\n", itrack);
114         }
115     }
116
117     // Exit if itrack is negative (-1). Set lsouit to false to mark last track for this event
118
119     if (itrack<0) {
120       nomore = 1;
121       SOURCM.lsouit = false;
122       if (debug) {
123          cout << "\t* SOURCM.lsouit = " << (SOURCM.lsouit?'T':'F') << endl;
124          cout << "\t* No more particles. Exiting..." << endl;
125          cout << "<== source(" << nomore << ")" << endl;
126       }   
127       return;
128     }
129     
130     //
131     // Handle user event abortion
132     if (fluka->EventIsStopped()) {
133         printf("Event has been stopped by user !");
134         fluka->SetStopEvent(kFALSE);
135         nomore = 1;
136         SOURCM.lsouit = false;
137         return;
138     }
139
140     //Get some info about the particle and print it
141     //
142     //pdg code
143     Int_t pdg = particle->GetPdgCode();
144     TVector3 polarisation;
145     particle->GetPolarisation(polarisation);
146     if (debug) {
147        cout << "\t* Particle " << itrack << " retrieved..." << endl;
148        cout << "\t\t+ Name = " << particle->GetName() << endl;
149        cout << "\t\t+ PDG/Fluka code = " << pdg 
150             << " / " << fluka->IdFromPDG(pdg) << endl;
151        cout << "\t\t+ P = (" 
152             << particle->Px() << " , "
153             << particle->Py() << " , "
154             << particle->Pz() << " ) --> "
155             << particle->P() << " GeV " 
156             << particle->Energy() << " GeV "  
157             << particle->GetMass() << " GeV " << endl;
158     }   
159     /* Npflka 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     if (pdg != 50000050 &&  pdg !=  50000051) {
169         FLKSTK.npflka++;
170         Int_t ifl =  fluka-> IdFromPDG(pdg);
171         FLKSTK.iloflk[FLKSTK.npflka] = ifl;
172         /* Wtflk is the weight of the particle*/
173         FLKSTK.wtflk[FLKSTK.npflka] = oneone;
174         SUMCOU.weipri += FLKSTK.wtflk[FLKSTK.npflka];
175         
176         FLKSTK.loflk[FLKSTK.npflka] = 1;
177         
178         /* User dependent flag:*/
179         FLKSTK.louse[FLKSTK.npflka] = 0;
180
181         /* User dependent spare variables:*/
182         Int_t ispr = 0;
183         for (ispr = 0; ispr < mkbmx1; ispr++)
184             FLKSTK.sparek[FLKSTK.npflka][ispr] = zerzer;
185
186         /* User dependent spare flags:*/
187         for (ispr = 0; ispr < mkbmx2; ispr++)
188             FLKSTK.ispark[FLKSTK.npflka][ispr] = 0;
189
190         /* Save the track number of the stack particle:*/
191         FLKSTK.ispark[FLKSTK.npflka][mkbmx2-1] = itrack;
192         FLKSTK.nparma++;
193         FLKSTK.numpar[FLKSTK.npflka] = FLKSTK.nparma;
194         FLKSTK.nevent[FLKSTK.npflka] = 0;
195         FLKSTK.dfnear[FLKSTK.npflka] = +zerzer;
196         
197         /* Particle age (s)*/
198         FLKSTK.agestk[FLKSTK.npflka] = +zerzer;
199         FLKSTK.cmpath[FLKSTK.npflka] = +zerzer;
200         FLKSTK.aknshr[FLKSTK.npflka] = -twotwo;
201
202         /* Group number for "low" energy neutrons, set to 0 anyway*/
203         FLKSTK.igroup[FLKSTK.npflka] = 0;
204         
205         /* Kinetic energy */
206         Double_t p    = particle->P();
207         Double_t mass = PAPROP.am[ifl + 6];
208         FLKSTK.tkeflk[FLKSTK.npflka] =  TMath::Sqrt( p * p + mass * mass) - mass;
209         /* Particle momentum*/
210         FLKSTK.pmoflk [FLKSTK.npflka] = p;
211
212         FLKSTK.txflk [FLKSTK.npflka] = cosx;
213         FLKSTK.tyflk [FLKSTK.npflka] = cosy;
214         FLKSTK.tzflk [FLKSTK.npflka] = 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             FLKSTK.txpol [FLKSTK.npflka] = cospolx;
222             FLKSTK.typol [FLKSTK.npflka] = cospoly;
223             FLKSTK.tzpol [FLKSTK.npflka] = cospolz;
224         }
225         else {
226             FLKSTK.txpol [FLKSTK.npflka] = -twotwo;
227             FLKSTK.typol [FLKSTK.npflka] = +zerzer;
228             FLKSTK.tzpol [FLKSTK.npflka] = +zerzer;
229         }
230         
231         /* Particle coordinates*/
232         // Vertext coordinates;
233         FLKSTK.xflk [FLKSTK.npflka] = particle->Vx();
234         FLKSTK.yflk [FLKSTK.npflka] = particle->Vy();
235         FLKSTK.zflk [FLKSTK.npflka] = particle->Vz();
236     
237         /*  Calculate the total kinetic energy of the primaries: don't change*/
238         Int_t st_ilo =  FLKSTK.iloflk[FLKSTK.npflka];
239         if ( st_ilo != 0 )
240             SOURCM.tkesum += 
241                 ((FLKSTK.tkeflk[FLKSTK.npflka] + PAPROP.amdisc[st_ilo+6])
242                  * FLKSTK.wtflk[FLKSTK.npflka]);
243         else
244             SOURCM.tkesum += (FLKSTK.tkeflk[FLKSTK.npflka] * FLKSTK.wtflk[FLKSTK.npflka]);
245         
246         /*  Here we ask for the region number of the hitting point.
247          *     NRGFLK (LFLKSTK) = ...
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( FLKSTK.txflk[FLKSTK.npflka], 
252                 FLKSTK.tyflk[FLKSTK.npflka], 
253                 FLKSTK.tzflk[FLKSTK.npflka] );
254     
255         Int_t idisc;
256
257         georeg ( FLKSTK.xflk[FLKSTK.npflka], 
258                  FLKSTK.yflk[FLKSTK.npflka], 
259                  FLKSTK.zflk[FLKSTK.npflka],
260                  FLKSTK.nrgflk[FLKSTK.npflka], 
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 ( FLKSTK.nhspnt[FLKSTK.npflka], igeohsm1, igeohsm2, LTCLCM.mlattc );
266         FLKSTK.nlattc[FLKSTK.npflka] = 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 }