Fluka user source routine using AliGenerator.
[u/mrichter/AliRoot.git] / TFluka / source_bg.cxx
1 #define METHODDEBUG
2
3 // Fortran 
4 #include "TCallf77.h"
5
6 // Fluka commons
7 #include "Fdblprc.h"  //(DBLPRC) fluka common
8 #include "Fdimpar.h"  //(DIMPAR) fluka parameters
9 #include "Fepisor.h"  //(EPISOR) fluka common
10 #include "Fstack.h"   //(STACK)  fluka common
11 #include "Fstars.h"   //(STARS)  fluka common
12 #include "Fbeam.h"    //(BEAM)   fluka common
13 #include "Fpaprop.h"  //(PAPROP) fluka common
14 #include "Fltclcm.h"  //(LTCLCM) fluka common
15 #include "Fpart.h"  
16 //#include "Fcaslim.h"  //(CASLIM) fluka common
17
18 //Virutal MC
19 #include "AliGenerator.h"
20 #include "AliStack.h"
21 #include "../THijing/AliGenHijing.h"
22
23 #include "TVirtualMCStack.h"
24 #include "TParticle.h"
25 #include "TVector3.h"
26 #include "TRandom.h"
27
28 //Other
29 #include <Riostream.h>
30
31 #ifndef WIN32
32 # define source source_
33 # define geocrs geocrs_
34 # define georeg georeg_
35 # define geohsm geohsm_
36 # define soevsv soevsv_
37 # define mcihad mcihad_
38 # define source_bg source_bg__
39 #else
40 # define source SOURCE
41 # define geocrs GEOCRS
42 # define georeg GEOREG
43 # define geohsm GEOHSM
44 # define soevsv SOEVSV
45 # define mcihad MCIHAD
46 # define source_bg SOURCE_BG
47 #endif
48
49 extern "C" {
50   //
51   // Prototypes for FLUKA functions
52   //
53   void type_of_call geocrs(Double_t &, Double_t &, Double_t &);
54   void type_of_call georeg(Double_t &, Double_t &, Double_t &, 
55                            Int_t &, Int_t &);
56   void type_of_call geohsm(Int_t &, Int_t &, Int_t &, Int_t &);
57   void type_of_call soevsv();
58   int  type_of_call mcihad(const int&);
59  /*
60    *----------------------------------------------------------------------*
61    *                                                                      *
62    *     Created on 07 january 1990   by    Alfredo Ferrari & Paola Sala  *
63    *                                                   Infn - Milan       *
64    *                                                                      *
65    *     Last change on 21-jun-98     by    Alfredo Ferrari               *
66    *                                                                      *
67    *     C++ version on 27-sep-02     by    Isidro Gonzalez               *
68    *                                                                      *
69    *  This is just an example of a possible user written source routine.  *
70    *  note that the beam card still has some meaning - in the scoring the *
71    *  maximum momentum used in deciding the binning is taken from the     *
72    *  beam momentum.  Other beam card parameters are obsolete.            *
73    *                                                                      *
74    *----------------------------------------------------------------------*/
75
76   void source_bg(Int_t& nomore) {
77
78       static Bool_t lfirst       = true;
79       static AliGenHijing* gener = 0;
80       static AliStack* stack     = 0;   
81
82       nomore = 0;
83       TParticle* particle;
84       Int_t itrack = -1;
85       if (lfirst) {
86           EPISOR.tkesum = zerzer;
87           lfirst = false;
88           EPISOR.lussrc = true;
89 //
90 // The generator
91 //
92           gener  = new AliGenHijing(-1);
93 // beam energy 
94           gener->SetEnergyCMS(7000.);
95 // reference frame
96           gener->SetReferenceFrame("LAB");
97 // projectile
98           gener->SetProjectile("P",  1,  1);
99           gener->SetTarget    ("A", 16,  8);
100 // tell hijing to keep the full parent child chain
101           gener->KeepFullEvent();
102 // enable jet quenching
103           gener->SetJetQuenching(0);
104 // enable shadowing
105           gener->SetShadowing(0);
106 // neutral pion and heavy particle decays switched off
107           gener->SetDecaysOff(1);
108 // Don't track spectators
109           gener->SetSpectators(0);
110 // The particle stack
111           stack = new AliStack(1000);
112           gener->SetStack(stack);
113           gener->Init();
114
115       } else {
116           //
117           // Generate event
118           stack->Reset();
119           gener->Generate();
120           Int_t npart = stack->GetNprimary();
121           // Vertex
122           Float_t za   =  4000. * gRandom->Rndm() - 2000.;
123           // Direction
124           Float_t dir  = (gRandom->Rndm() < 0.5) ? 1. : -1.;
125           
126           
127           for (Int_t part=0; part<npart; part++) {
128               particle = stack->Particle(part);
129               Int_t ic = particle->GetFirstDaughter();
130               if (ic != -1) continue;
131               Int_t pdg = particle->GetPdgCode();
132               Int_t intfluka = mcihad(pdg);
133               Int_t ifl = GetFlukaKPTOIP(intfluka);
134               TVector3 polarisation;
135               particle->GetPolarisation(polarisation);
136
137               STACK.lstack++;
138
139               printf("Particle %5d %5d %5d %10s %10.3f %10.3f %10.3f \n", STACK.lstack, pdg, ifl, 
140                      particle->GetName(), particle->Px(), particle->Py(), particle->Pz());
141               
142
143               
144               /* Wt is the weight of the particle*/
145               STACK.wt[STACK.lstack] = oneone;
146               STARS.weipri += STACK.wt[STACK.lstack];
147               
148               STACK.ilo[STACK.lstack] = ifl;
149               /* From this point .....
150                * Particle generation (1 for primaries)
151                */
152               STACK.lo[STACK.lstack] = 1;
153               
154               /* User dependent flag:*/
155               STACK.louse[STACK.lstack] = 0;
156               
157               /* User dependent spare variables:*/
158               Int_t ispr = 0;
159               for (ispr = 0; ispr < mkbmx1; ispr++)
160                   STACK.sparek[STACK.lstack][ispr] = zerzer;
161               
162               /* User dependent spare flags:*/
163               for (ispr = 0; ispr < mkbmx2; ispr++)
164                   STACK.ispark[STACK.lstack][ispr] = 0;
165               
166               /* Save the track number of the stack particle:*/
167               STACK.ispark[STACK.lstack][mkbmx2-1] = itrack;
168               STACK.nparma++;
169               STACK.numpar[STACK.lstack] = STACK.nparma;
170               STACK.nevent[STACK.lstack] = 0;
171               STACK.dfnear[STACK.lstack] = +zerzer;
172               
173               /* Particle age (s)*/
174               STACK.agestk[STACK.lstack] = +zerzer;
175               STACK.aknshr[STACK.lstack] = -twotwo;
176               
177               /* Group number for "low" energy neutrons, set to 0 anyway*/
178               STACK.igroup[STACK.lstack] = 0;
179               
180               /* Kinetic energy */
181               STACK.tke[STACK.lstack] = particle->Energy() - particle->GetMass();
182               
183               
184               /* Particle momentum*/
185               STACK.pmom [STACK.lstack] = particle->P();
186               
187               /* Cosines (tx,ty,tz)*/
188               Double_t cosx = particle->Px()/particle->P();
189               Double_t cosy = particle->Py()/particle->P();
190               Double_t cosz = TMath::Sqrt(oneone - cosx*cosx - cosy*cosy);
191               if (particle->Pz() < 0.)   cosz = -cosz;
192               cosz *= dir;
193               
194               STACK.tx [STACK.lstack] = cosx;
195               STACK.ty [STACK.lstack] = cosy;
196               STACK.tz [STACK.lstack] = cosz;
197               
198               /* Polarization cosines:*/
199               if (polarisation.Mag()) {
200                   Double_t cospolx = polarisation.Px()/polarisation.Mag();
201                   Double_t cospoly = polarisation.Py()/polarisation.Mag();
202                   Double_t cospolz = sqrt(oneone - cospolx*cospolx - cospoly*cospoly);
203                   STACK.tx [STACK.lstack] = cospolx;
204                   STACK.ty [STACK.lstack] = cospoly;
205                   STACK.tz [STACK.lstack] = cospolz;
206               }
207               else {
208                   STACK.txpol [STACK.lstack] = -twotwo;
209                   STACK.typol [STACK.lstack] = +zerzer;
210                   STACK.tzpol [STACK.lstack] = +zerzer;
211               }
212               
213               /* Particle coordinates*/
214               STACK.xa [STACK.lstack] = particle->Vx();
215               STACK.ya [STACK.lstack] = particle->Vy();
216               STACK.za [STACK.lstack] = za;
217               
218               printf("Particle Vertex %10.3f %10.3f %10.3f %10.3f \n",  
219                      STACK.xa [STACK.lstack],  STACK.ya [STACK.lstack],  STACK.za [STACK.lstack], dir);
220               
221               
222               
223               /*  Calculate the total kinetic energy of the primaries: don't change*/
224               Int_t st_ilo =  STACK.ilo[STACK.lstack];
225               if ( st_ilo != 0 )
226                   EPISOR.tkesum += 
227                       ((STACK.tke[STACK.lstack] + PAPROP.amdisc[st_ilo+6])
228                        * STACK.wt[STACK.lstack]);
229               else
230                   EPISOR.tkesum += (STACK.tke[STACK.lstack] * STACK.wt[STACK.lstack]);
231               
232               /*  Here we ask for the region number of the hitting point.
233                *     NREG (LSTACK) = ...
234                *  The following line makes the starting region search much more
235                *  robust if particles are starting very close to a boundary:
236                */
237               geocrs( STACK.tx[STACK.lstack], 
238                       STACK.ty[STACK.lstack], 
239                       STACK.tz[STACK.lstack] );
240               
241               Int_t idisc;
242               
243               georeg ( STACK.xa[STACK.lstack], 
244                        STACK.ya[STACK.lstack], 
245                        STACK.za[STACK.lstack],
246                        STACK.nreg[STACK.lstack], 
247                        idisc);//<-- dummy return variable not used
248               /*  Do not change these cards:*/
249               Int_t igeohsm1 = 1;
250               Int_t igeohsm2 = -11;
251               geohsm ( STACK.nhspnt[STACK.lstack], igeohsm1, igeohsm2, LTCLCM.mlattc );
252               STACK.nlattc[STACK.lstack] = LTCLCM.mlattc;
253               soevsv();
254           }
255       }
256   }
257 }
258
259