ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4PenelopeSamplingData.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4PenelopeSamplingData.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // Author: Luciano Pandola
28 //
29 // History:
30 // --------
31 // 09 Dec 2009 L Pandola First implementation
32 //
34 
35 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...
37  np(nPoints)
38 {
39  //create vectors
40  x = new G4DataVector();
41  pac = new G4DataVector();
42  a = new G4DataVector();
43  b = new G4DataVector();
44  ITTL = new std::vector<size_t>;
45  ITTU = new std::vector<size_t>;
46 }
47 
48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...
50 {
51  if (x) delete x;
52  if (pac) delete pac;
53  if (a) delete a;
54  if (b) delete b;
55  if (ITTL) delete ITTL;
56  if (ITTU) delete ITTU;
57 }
58 
59 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.......oooOO0OOooo...
61 {
62  size_t points = x->size();
63 
64  //check everything is all right
65  if (pac->size() != points || a->size() != points ||
66  b->size() != points || ITTL->size() != points ||
67  ITTU->size() != points)
68  {
70  ed << "Data vectors look to have different dimensions !" << G4endl;
71  G4Exception("G4PenelopeSamplingData::GetNumberOfStoredPoints()","em2040",
72  FatalException,ed);
73  }
74  return points;
75 }
76 
77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...
79 {
80  if (x) delete x;
81  if (pac) delete pac;
82  if (a) delete a;
83  if (b) delete b;
84  if (ITTL) delete ITTL;
85  if (ITTU) delete ITTU;
86  //create vectors
87  x = new G4DataVector();
88  pac = new G4DataVector();
89  a = new G4DataVector();
90  b = new G4DataVector();
91  ITTL = new std::vector<size_t>;
92  ITTU = new std::vector<size_t>;
93 }
94 
95 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...
97  size_t ITTL0,size_t ITTU0)
98 {
99  x->push_back(x0);
100  pac->push_back(pac0);
101  a->push_back(a0);
102  b->push_back(b0);
103  ITTL->push_back(ITTL0);
104  ITTU->push_back(ITTU0);
105 
106  //check how many points we do have now
107  size_t nOfPoints = GetNumberOfStoredPoints();
108 
109  if (nOfPoints > ((size_t)np))
110  {
111  G4cout << "G4PenelopeSamplingData::AddPoint() " << G4endl;
112  G4cout << "WARNING: Up to now there are " << nOfPoints << " points in the table" << G4endl;
113  G4cout << "while the anticipated (declared) number is " << np << G4endl;
114  }
115  return;
116 }
117 
118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
120 {
121 
122  G4cout << "*************************************************************************" << G4endl;
123  G4cout << GetNumberOfStoredPoints() << " points" << G4endl;
124  G4cout << "*************************************************************************" << G4endl;
125  for (size_t i=0;i<GetNumberOfStoredPoints();i++)
126  {
127  G4cout << i << " " << (*x)[i] << " " << (*pac)[i] << " " << (*a)[i] << " " <<
128  (*b)[i] << " " << (*ITTL)[i] << " " << (*ITTU)[i] << G4endl;
129  }
130  G4cout << "*************************************************************************" << G4endl;
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
135 {
136  if (index < x->size())
137  return (*x)[index];
138  else
139  return 0;
140 }
141 
142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
144 {
145  if (index < pac->size())
146  return (*pac)[index];
147  else
148  return 0;
149 }
150 
151 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
153 {
154  if (index < a->size())
155  return (*a)[index];
156  else
157  return 0;
158 }
159 
160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
162 {
163  if (index < b->size())
164  return (*b)[index];
165  else
166  return 0;
167 }
168 
169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
171 {
172  //One passes here a random number in (0,1).
173  //Notice: it possible that is between (0,b) with b<1
174  size_t points = GetNumberOfStoredPoints();
175 
176  size_t itn = (size_t) (maxRand*(points-1));
177  size_t i = (*ITTL)[itn];
178  size_t j = (*ITTU)[itn];
179 
180  while ((j-i) > 1)
181  {
182  size_t k = (i+j)/2;
183  if (maxRand > (*pac)[k])
184  i = k;
185  else
186  j = k;
187  }
188 
189  //Sampling from the rational inverse cumulative distribution
190  G4double result = 0;
191 
192  G4double rr = maxRand - (*pac)[i];
193  if (rr > 1e-16)
194  {
195  G4double d = (*pac)[i+1]-(*pac)[i];
196  result = (*x)[i]+
197  ((1.0+(*a)[i]+(*b)[i])*d*rr/
198  (d*d+((*a)[i]*d+(*b)[i]*rr)*rr))*((*x)[i+1]-(*x)[i]);
199  }
200  else
201  result = (*x)[i];
202 
203  return result;
204 }