UnivTimeKG.cc
Go to the documentation of this file.
1 #include "UnivTimeKG.h"
2 
3 #include <utl/Math.h>
4 #include <utl/AugerUnits.h>
5 
6 #include <Math/PdfFuncMathCore.h>
7 #include <Math/ProbFuncMathCore.h>
8 #include <Math/QuantFuncMathCore.h>
9 
10 #include <fstream>
11 #include <cmath>
12 
13 
14 /*
15  Implementation of the time model (base class)
16 
17  ===========
18  parameters:
19  ===========
20 
21  icomp: signal component number:
22  pure muonic (0)
23  pure electromagnetic (1)
24  electromagnetic from muon decay (2)
25  electromagnetic from hadron jets (3)
26 
27  r: distance to shower axis in shower plane CS
28  psi: azimuth to shower axis in shower plane CS
29  DX: distance to Xmax, parallel to shower axis
30  t: relative time for certain bin in trace referred to plane front arriving at station
31 
32  Values are given in Offline units
33 */
34 
35 
36 using namespace std;
37 
38 
39 namespace UnivTimeKG {
40 
41 
42  unsigned int
43  FindIndex(const vector<double>& array, const double value)
44  {
45 //#warning this should be replaced with std::upper_bound or std::lower_bound
46  unsigned int imin = 0;
47  unsigned int imax = array.size() - 1;
48 
49  while (imax - imin > 1) {
50  const unsigned int i = (imax + imin) / 2;
51  if (array[i] <= value)
52  imin = i;
53  else
54  imax = i;
55  if (array[i] == value)
56  break;
57  }
58  return imin;
59  }
60 
61 
62  double
63  int1_2p(const double x0, const double y0, const double x1, const double y1, const double x)
64  {
65  return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
66  }
67 
68 
69  double
70  int1(const vector<double>& xs, const vector<double>& ys, const double x)
71  {
72  const unsigned int i = FindIndex(xs, x);
73  const double x0 = xs[i];
74  const double x1 = xs[i + 1];
75  const double y0 = ys[i];
76  const double y1 = ys[i + 1];
77  return int1_2p(x0, y0, x1, y1, x);
78  }
79 
80 
81  double
82  int1(const double xs[], const double ys[], const double x, const int n)
83  {
84  const vector<double> vxs(xs, xs + n);
85  const vector<double> vys(ys, ys + n);
86  return int1(vxs, vys, x);
87  }
88 
89 
90  double
91  int2(const vector<double>& x1s, const vector<double>& x2s, const vector<vector<double> >& Ys, const double x1, const double x2)
92  {
93  const unsigned int j = FindIndex(x2s, x2);
94  const double x21 = x2s[j];
95  const double x22 = x2s[j + 1];
96 
97  const double fx1 = int1(x1s, Ys[j], x1);
98  const double fx2 = int1(x1s, Ys[j + 1], x1);
99  const double fxy = int1_2p(x21, fx1, x22, fx2, x2);
100 
101  return fxy;
102  }
103 
104 
106  { }
107 
108 
110  { }
111 
112 
113  void
114  TimeModel::addInterpolationTable(const vector<double>& rs, const vector<double>& psis, const vector<vector<double> >& ys)
115  {
116  IntTable t;
117  t.rs = rs;
118  t.psis = psis;
119  t.ys = ys;
120  tables.push_back(t);
121  }
122 
123 
124  void
125  TimeModel::addInterpolationTable(const std::string& filename)
126  {
127  ifstream file(filename.c_str());
128 
129  vector<double> rs;
130  vector<double> psis;
131  vector<vector<double> > ys;
132 
133  unsigned int Nr, Npsi;
134  if (!(file >> Nr >> Npsi)) {
135  cerr << "error reading file!" << endl;
136  return;
137  }
138 
139  double y = 0;
140 
141  for (unsigned int i = 0; i < Nr; ++i) {
142  if (file >> y)
143  rs.push_back(y);
144  else {
145  cerr << "error reading file!" << endl;
146  return;
147  }
148  }
149 
150  for (unsigned int j = 0; j < Npsi; ++j) {
151  if (file >> y)
152  psis.push_back(y);
153  else {
154  cerr << "error reading file!" << endl;
155  return;
156  }
157  }
158 
159  for (unsigned int i = 0; i < Nr; ++i) {
160  vector<double> line;
161  for (unsigned int j = 0; j < Npsi; ++j) {
162  if (file >> y)
163  line.push_back(y);
164  else {
165  cerr << "error reading file!" << endl;
166  return;
167  }
168  }
169  ys.push_back(line);
170  }
171 
172  addInterpolationTable(rs, psis, ys);
173  }
174 
175 
176  void
177  TimeModel::clearInterpolationTables()
178  {
179  tables.clear();
180  }
181 
182 
183  void
184  TimeModel::interpolateParameters(const double DX, const double r, const double ppsi, vector<double>& output)
185  {
186  // psi in [0,180] deg + left-right-symmetry assumed in showers
187  const double psi = fabs(utl::NormalizeAngleMinusPiPi(ppsi));
188 
189  if (interpMode == 0) {
190  vector<double> intp_pars;
191  for (unsigned int i = 0, n = tables.size(); i < n; ++i) {
192  IntTable& t = tables[i];
193  intp_pars.push_back(int2(t.psis, t.rs, t.ys, psi, r));
194  }
195 
196  for (unsigned int ipar = 0; ipar < nParams; ++ipar)
197  output.push_back(getShapeParameter(ipar, intp_pars, DX));
198  } else {
199  // warning: assuming all parameters are defined on the same grid (r,psi)
200  IntTable& t0 = tables[0];
201  const unsigned int ir = FindIndex(t0.rs, r);
202  const unsigned int ip = FindIndex(t0.psis, psi);
203 
204  const vector<double> gp_rs(t0.rs.begin() + ir, t0.rs.begin() + ir + 2);
205  const vector<double> gp_psis(t0.psis.begin() + ip, t0.psis.begin() + ip + 2);
206 
207  for (unsigned int ipar = 0; ipar < nParams; ++ipar) {
208  vector<vector<double> > gpshapepars;
209 
210  for (unsigned int cur_ir = ir; cur_ir <= ir + 1; ++cur_ir) {
211 
212  vector<double> tmp;
213  for (unsigned int cur_ip = ip; cur_ip <= ip + 1; ++cur_ip) {
214 
215  vector<double> gppars; // parameters of the current grid point
216  for (unsigned int i = 0, n = tables.size(); i < n; ++i)
217  gppars.push_back(tables[i].ys[cur_ir][cur_ip]);
218  tmp.push_back(getShapeParameter(ipar, gppars, DX));
219 
220  }
221  gpshapepars.push_back(tmp);
222  }
223 
224  output.push_back(int2(gp_psis, gp_rs, gpshapepars, psi, r));
225  }
226  }
227  }
228 
229 
230  double
231  TimeModel::firstParticlePdf(const double t, const double npart)
232  {
233  return npart * pow((1 - cdf(t)), npart - 1) * pdf(t);
234  }
235 
236 
237  double
238  TimeModel::firstParticlePdfSmeared(const double t, const double npart)
239  {
240  const double sigma = 25;
241  const double dtau = 2;
242  double f = 0;
243  for (double tau = -3 * sigma; tau < 3 * sigma; tau += dtau)
244  f += ROOT::Math::normal_pdf(tau, sigma, 0) * firstParticlePdf(t - tau, npart) * dtau;
245  return f;
246  }
247 
248 
249  double
250  TimeModel::getRisetime()
251  {
252  return invcdf(0.5) - invcdf(0.1);
253  }
254 
255 
256  double
257  TimeModel::getFalltime()
258  {
259  return invcdf(0.9) - invcdf(0.5);
260  }
261 
262 }
struct Station * array
std::vector< double > rs
Definition: UnivTimeKG.h:26
double NormalizeAngleMinusPiPi(const double x)
Normalize angle to lie between -pi and pi (-180 and 180 deg)
unsigned int FindIndex(const vector< double > &array, const double value)
Definition: UnivTimeKG.cc:43
double int2(const vector< double > &x1s, const vector< double > &x2s, const vector< vector< double > > &Ys, const double x1, const double x2)
Definition: UnivTimeKG.cc:91
double pow(const double x, const unsigned int i)
double int1_2p(const double x0, const double y0, const double x1, const double y1, const double x)
Definition: UnivTimeKG.cc:63
double int1(const vector< double > &xs, const vector< double > &ys, const double x)
Definition: UnivTimeKG.cc:70
std::vector< double > psis
Definition: UnivTimeKG.h:27
const string file
std::vector< std::vector< double > > ys
Definition: UnivTimeKG.h:28
char * filename
Definition: dump1090.h:266
TimeModel(double angle_in, int primary, int Da_flag_in=1)
Definition: TimeModel.cc:51

, generated on Tue Sep 26 2023.