Geant4 11.3.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4LEPTSDistribution Class Reference

#include <G4LEPTSDistribution.hh>

Public Member Functions

 G4LEPTSDistribution ()=default
 
void ReadFile (const G4String &fileName)
 
G4bool ReadFile (FILE *fp, G4int nData)
 
G4double Sample (G4double, G4double)
 
G4bool IsFileFound () const
 

Detailed Description

Definition at line 36 of file G4LEPTSDistribution.hh.

Constructor & Destructor Documentation

◆ G4LEPTSDistribution()

G4LEPTSDistribution::G4LEPTSDistribution ( )
default

Member Function Documentation

◆ IsFileFound()

G4bool G4LEPTSDistribution::IsFileFound ( ) const
inline

Definition at line 46 of file G4LEPTSDistribution.hh.

46{ return bFileFound; }

◆ ReadFile() [1/2]

void G4LEPTSDistribution::ReadFile ( const G4String & fileName)

Definition at line 36 of file G4LEPTSDistribution.cc.

37{
38 G4int eB, out, out2;
39 G4float float_data1,float_data2;
40 G4double sum, esum;
41 FILE * fp;
42
43 for (eB=0; eB<10000; ++eB)
44 {
45 E[eB]=0.0;
46 f[eB]=0.0;
47 F[eB]=0.0;
48 eF[eB]=0.0;
49 }
50
51 if ((fp=fopen(fileName.c_str(), "r"))==nullptr)
52 {
53 NoBins = 0;
54 bFileFound = false;
55 return;
56 }
57
58 bFileFound = true;
59
60 out=1;
61 eB=1;
62 while (out==1)
63 {
64 out = fscanf(fp,"%f \n",&float_data1);
65 out2 = fscanf(fp,"%f \n",&float_data2);
66 if (out==1 && out2==1)
67 {
68 E[eB]=(G4double)float_data1;
69 f[eB]=(G4double)float_data2;
70 ++eB;
71 }
72 }
73
74 fclose(fp);
75
76 NoBins=eB-1; //=1272+1 or 9607+1;
77
78 if( NoBins >= NMAX )
79 {
80 std::ostringstream message;
81 message << "ERROR !!!! Eloss NoBins = " << NoBins;
82 G4Exception("G4LEPTSDistribution::ReadFile()", "ReadError",
83 FatalException, message);
84 }
85
86 sum=0.0;
87 esum=0.0;
88 for (eB=0; eB<=NoBins; ++eB)
89 {
90 if( f[eB] > 0)
91 {
92 sum+=f[eB];
93 esum+=E[eB]*f[eB];
94 }
95 F[eB]=sum;
96 eF[eB]=esum;
97 }
98
99 for (eB=0; eB<=NoBins; ++eB)
100 {
101 eF[eB] = eF[eB]/F[eB];
102 F[eB] = F[eB]/F[NoBins];
103 }
104}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
#define NMAX
float G4float
Definition G4Types.hh:84
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85

◆ ReadFile() [2/2]

G4bool G4LEPTSDistribution::ReadFile ( FILE * fp,
G4int nData )

Definition at line 106 of file G4LEPTSDistribution.cc.

107{
108
109 G4int eB, out, out2;
110 G4float float_data1,float_data2;
111 G4double sum, esum;
112
113 for (eB=0; eB<10000; ++eB)
114 {
115 E[eB]=0.0;
116 f[eB]=0.0;
117 F[eB]=0.0;
118 eF[eB]=0.0;
119 }
120
121 bFileFound = true;
122 out=1;
123 eB=1;
124
125 for( G4int id = 0; id < nData; ++id )
126 {
127 out = fscanf(fp,"%f \n",&float_data1);
128 out2 = fscanf(fp,"%f \n",&float_data2);
129 if (out==1 && out2==1){
130 E[eB]=(G4double)float_data1;
131 f[eB]=(G4double)float_data2;
132 ++eB;
133 }
134 else
135 {
136 return true;
137 }
138 }
139
140 NoBins=eB-1; //=1272+1 or 9607+1;
141
142 if( NoBins >= NMAX )
143 {
144 std::ostringstream message;
145 message << "ERROR !!!! Eloss NoBins = " << NoBins;
146 G4Exception("G4LEPTSDistribution::ReadFile()", "ReadError",
147 FatalException, message);
148 }
149
150 sum=0.0;
151 esum=0.0;
152 for (eB=0; eB<=NoBins; ++eB)
153 {
154 if( f[eB] > 0)
155 {
156 sum+=f[eB];
157 esum+=E[eB]*f[eB];
158 }
159 F[eB]=sum;
160 eF[eB]=esum;
161 }
162
163 for (eB=0; eB<=NoBins; ++eB)
164 {
165 eF[eB] = eF[eB]/F[eB];
166 F[eB] = F[eB]/F[NoBins];
167 }
168
169 return false;
170}

◆ Sample()

G4double G4LEPTSDistribution::Sample ( G4double eMin,
G4double eMax )

Definition at line 172 of file G4LEPTSDistribution.cc.

173{
174 // Sample Energy from Cumulative distr. G4interval [eMin, eMax]
175
176 if( eMin > eMax) return 0.0;
177
178 G4int i,j,k=0, iMin, iMax;
179
180 i=0; j=NoBins;
181 while ((j-i)>1)
182 {
183 k=(i+j)/2;
184 if( E[k] < eMax ) i=k;
185 else j=k;
186 }
187 iMax = i;
188
189 i=0; j=NoBins;
190 while ((j-i)>1)
191 {
192 k=(i+j)/2;
193 if( E[k] < eMin ) i=k;
194 else j=k;
195 }
196 iMin = i;
197
198 G4double rnd = F[iMin] + (F[iMax] - F[iMin]) * G4UniformRand();
199
200 i=0; j=NoBins;
201 while ((j-i)>1)
202 {
203 k=(i+j)/2;
204 if( F[k]<rnd) i=k;
205 else j=k;
206 }
207
208 G4double Sampled = E[k];
209
210 if( Sampled < eMin) Sampled = eMin;
211 else if( Sampled > eMax) Sampled = eMax;
212
213 return Sampled;
214}
#define G4UniformRand()
Definition Randomize.hh:52

Referenced by G4LEPTSElossDistr::Sample().


The documentation for this class was generated from the following files: