StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSvtEnergySim.cc
1 #include<Stiostream.h>
2 #include "Stiostream.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 
7 #include "StSvtEnergySim.hh"
8 
9 StSvtEnergySim::StSvtEnergySim()
10 {
11  mNumberOfParticles = 0;
12  mMaxEnergy = 0;
13  mParticle = NULL;
14 }
15 
16 StSvtEnergySim::~StSvtEnergySim()
17 {
18  delete [] mParticle;
19 }
20 
21 void StSvtEnergySim::SetParticle(int numOfPar, float maxEnergy)
22 {
23  mNumberOfParticles = numOfPar;
24  mMaxEnergy = maxEnergy;
25  mParticle = new StParticle[mNumberOfParticles];
26 
27 }
28 
29 
30 void StSvtEnergySim::CalculateEnAndMom(char* option, char* parType)
31 {
32  float randNum1 =0, randNum2 = 0;
33 
34  for(int i = 0; i<mNumberOfParticles; i++)
35  {
36 
37  if(parType == "proton")
38  mParticle[i].mass = 931.0;
39 
40  randNum1 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
41 
42  mParticle[i].theta = acos(randNum1);
43 
44  randNum2 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
45 
46  mParticle[i].phi = (float)(2*3.14*randNum2);
47 
48  if(strncmp(option,"expdev",strlen("expdev"))== 0)
49  mParticle[i].energy = mMaxEnergy*Expdev();
50  else if(strncmp(option,"gausdev",strlen("gausdev"))== 0)
51  mParticle[i].energy = mMaxEnergy*Gausdev();
52 
53  //cout<<mParticle[i].energy<<endl;
54 
55  mParticle[i].momentum = ::sqrt((mParticle[i].energy )-(mParticle[i].mass*mParticle[i].mass ));
56 
57  mParticle[i].momentumZ = mParticle[i].momentum *cos( mParticle[i].theta);
58 
59  mParticle[i].momentumXY = mParticle[i].momentum *sin( mParticle[i].theta);
60 
61  mParticle[i].momentumX = mParticle[i].momentum *sin( mParticle[i].theta)*cos( mParticle[i].phi);
62 
63  mParticle[i].momentumY = mParticle[i].momentum *sin( mParticle[i].theta)*sin( mParticle[i].phi);
64 
65  }
66 
67 }
68 
69 float StSvtEnergySim::Expdev(){
70 
71  float dum;
72 
73  do
74  dum = (float)rand()/(float)RAND_MAX;
75  while(dum==0.0);
76  return -::log(dum);
77 
78  }
79 
80 float StSvtEnergySim::Gausdev()
81 {
82 
83  static int iset = 0;
84  static float gset;
85  float fac,rsq,v1,v2;
86 
87  //if(*idum < 0) iset = 0;
88  if(iset == 0)
89  {
90 
91  do {
92  v1 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
93  v2 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
94  rsq = v1*v1 + v2*v2;
95  } while(rsq >= 1.0 || rsq == 1.0);
96 
97  fac = 3.0*::sqrt(-2.0*::log(rsq)/rsq);
98 
99  gset = v1*fac; // gset = 3.0*::sqrt(-2.0*::log(rsq))*(v1/::sqrt(rsq))
100  iset = 1;
101  return v2*fac;
102  }
103  else
104  {
105  iset = 0;
106  return gset;
107  }
108 }
109 
110