Subversion Repositories f9daq

Rev

Rev 72 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 f9daq 1
#include "include/guide.h"
2
 
3
#include <iostream>
4
 
5
// vector output shortcut
6
void printv(TVector3 v)
7
{
72 f9daq 8
  printf("(x,y,z) = (%.4lf, %.4lf, %.4lf)\n", v.x(), v.y(), v.z());
25 f9daq 9
}
10
// TVector3::Rotate does not seem accurate enough
11
TVector3 rotatey(TVector3 v, double theta)
12
{
72 f9daq 13
  return TVector3(v.x() * TMath::Cos(theta) + v.z() * TMath::Sin(theta),
14
      v.y(),
15
      -v.x() * TMath::Sin(theta) + v.z() * TMath::Cos(theta));
25 f9daq 16
}
17
// another shortcut not found in TMath
18
int sign(double in)
19
{
72 f9daq 20
  if(in >= 0.0) return 1;
21
  else return -1;
25 f9daq 22
}
23
 
73 f9daq 24
void CRay::Set(TVector3 r0, TVector3 k0)
25 f9daq 25
{
73 f9daq 26
  r = r0; k = k0.Unit();
25 f9daq 27
}
28
//-----------------------------------------------------------------------------
29
//void CRay::Set(double x0, double y0, double z0, double l0, double m0, double n0)
30
//{
72 f9daq 31
//r.SetXYZ(x0, y0, z0);
32
//n.SetXYZ(l0, m0, n0); n = n.Unit();
25 f9daq 33
//}
34
//-----------------------------------------------------------------------------
35
/*
36
CRay& CRay::operator = (const CRay& p)
37
{
38
        r.SetXYZ(p.GetR().x(), p.GetR().y(), p.GetR().z());
39
        //this->r.SetXYZ(p.x(), p.y(), p.z());
40
        n.SetXYZ(p.GetN().x(), p.GetN().y(), p.GetN().z());
41
        return *this;
42
} */
43
void CRay::Print()
44
{
72 f9daq 45
  printf("---> CRay::Print() <---\n");
46
  printf("(x,y,z)=(%.2lf, %.2lf, %.2lf); (l,m,n)=(%.2lf, %.2lf, %.2lf)\n",
73 f9daq 47
      r.x(), r.y(), r.z(), k.x(), k.y(), k.z());
25 f9daq 48
}
49
void CRay::Draw()
50
{
72 f9daq 51
  double t = 50.0;
52
  TPolyLine3D *line3d = new TPolyLine3D(2);
53
  //line3d->SetPoint(0, r.x() - t*n.x(), r.y() - t*n.y(), r.z() - t*n.z());
54
  line3d->SetPoint(0, r.x(), r.y(), r.z());
73 f9daq 55
  line3d->SetPoint(1, r.x() + t*k.x(), k.y() + t*k.y(), r.z() + t*k.z());
72 f9daq 56
  line3d->SetLineWidth(1);
57
  line3d->SetLineColor(color);
25 f9daq 58
 
72 f9daq 59
  line3d->Draw();
25 f9daq 60
}
61
void CRay::Draw(double x_from, double x_to)
62
{
72 f9daq 63
  double A1, A2;
25 f9daq 64
  TPolyLine3D *line3d = new TPolyLine3D(2);
65
 
73 f9daq 66
  if(k.x() < MARGIN) {
72 f9daq 67
      A1 = A2 = 0.0;
68
  } else {
73 f9daq 69
      A1 = (x_from - r.x())/k.x();
70
      A2 = (x_to - r.x())/k.x();
72 f9daq 71
  }
25 f9daq 72
 
73 f9daq 73
  line3d->SetPoint(0, x_from, A1*k.y()+r.y(), A1*k.z()+r.z());
74
  line3d->SetPoint(1, x_to, A2*k.y()+r.y(), A2*k.z()+r.z());
72 f9daq 75
  line3d->SetLineWidth(1);
76
  line3d->SetLineColor(color);
77
 
78
  line3d->Draw();
25 f9daq 79
}
80
void CRay::DrawS(double x_from, double t)
81
{
72 f9daq 82
  double A1;
83
  TPolyLine3D *line3d = new TPolyLine3D(2);
25 f9daq 84
 
73 f9daq 85
  if(k.x() < MARGIN)
72 f9daq 86
    A1 = 0.0;
87
  else
73 f9daq 88
    A1 = (x_from - r.x())/k.x();
25 f9daq 89
 
73 f9daq 90
  line3d->SetPoint(0, x_from, A1*k.y()+r.y(), A1*k.z()+r.z());
91
  line3d->SetPoint(1, r.x() + t*k.x(), r.y() + t*k.y(), r.z() + t*k.z());
72 f9daq 92
  line3d->SetLineWidth(1);
93
  line3d->SetLineColor(color);
94
 
95
  line3d->Draw();
25 f9daq 96
}
97
 
98
 
99
CPlane4::CPlane4() :
72 f9daq 100
            n(TVector3(1.0, 0.0, 0.0)),
101
            A(0),
102
            B(0),
103
            C(0),
104
            D(0)
25 f9daq 105
{ r[0] = TVector3(0.0,-1.0,-1.0);
72 f9daq 106
r[1] = TVector3(0.0,-1.0, 1.0);
107
r[2] = TVector3(0.0, 1.0, 1.0);
108
r[3] = TVector3(0.0, 1.0,-1.0);
109
for(int i=0;i<4;i++) edge[i] = TVector3(0,0,0);
110
for(int i=0;i<4;i++) angle_r[i] = 0;
25 f9daq 111
};
112
CPlane4::CPlane4(TVector3 r1, TVector3 r2, TVector3 r3, TVector3 r4)
113
{
72 f9daq 114
  //Set(r1, r2, r3, r4);
115
  //}
116
  //-----------------------------------------------------------------------------
117
  // za izracun parametrov ravnine je en vektor prevec, vendar tega
118
  // rabim kot zadnji vogal poligona
119
  //void CPlane4::Set(TVector3 r1, TVector3 r2, TVector3 r3, TVector3 r4)
120
  //{
121
  double x1,y1,z1, x2,y2,z2, x3,y3,z3;
25 f9daq 122
 
72 f9daq 123
  x1 = r1.x(); y1 = r1.y(); z1 = r1.z();
124
  x2 = r2.x(); y2 = r2.y(); z2 = r2.z();
125
  x3 = r3.x(); y3 = r3.y(); z3 = r3.z();
25 f9daq 126
 
72 f9daq 127
  A = y3*(z1 - z2) + y1*(z2 - z3) + y2*(z3 - z1);
128
  B = x3*(z2 - z1) + x1*(z3 - z2) + x2*(z1 - z3);
129
  C = x3*(y1 - y2) + x1*(y2 - y3) + x2*(y3 - y1);
130
  D = y3*(x1*z2 - x2*z1) + x3*(y2*z1 - y1*z2) + z3*(x2*y1 - x1*y2);
25 f9daq 131
 
72 f9daq 132
  r[0] = r1; r[1] = r2; r[2] = r3; r[3] = r4;
133
  n.SetXYZ(A, B, C);
134
  n = n.Unit();
25 f9daq 135
 
72 f9daq 136
  for(int i=0;i<4;i++)
137
    edge[i] = r[i-3 ? i+1 : 0] - r[i];
138
 
139
  for(int i=0;i<4;i++)
140
    angle_r[i] = TMath::ACos(/*TMath::Abs*/( ((-edge[i ? i-1 : 3]).Unit()) * (edge[i].Unit()) ));
25 f9daq 141
};
142
 
143
void CPlane4::Set(TVector3 r1, TVector3 r2, TVector3 r3, TVector3 r4)
144
{
145
  double x1,y1,z1, x2,y2,z2, x3,y3,z3;
146
 
72 f9daq 147
  x1 = r1.x(); y1 = r1.y(); z1 = r1.z();
148
  x2 = r2.x(); y2 = r2.y(); z2 = r2.z();
149
  x3 = r3.x(); y3 = r3.y(); z3 = r3.z();
25 f9daq 150
 
72 f9daq 151
  A = y3*(z1 - z2) + y1*(z2 - z3) + y2*(z3 - z1);
152
  B = x3*(z2 - z1) + x1*(z3 - z2) + x2*(z1 - z3);
153
  C = x3*(y1 - y2) + x1*(y2 - y3) + x2*(y3 - y1);
154
  D = y3*(x1*z2 - x2*z1) + x3*(y2*z1 - y1*z2) + z3*(x2*y1 - x1*y2);
25 f9daq 155
 
72 f9daq 156
  r[0] = r1; r[1] = r2; r[2] = r3; r[3] = r4;
157
  n.SetXYZ(A, B, C);
158
  n = n.Unit();
25 f9daq 159
 
72 f9daq 160
  for(int i=0;i<4;i++)
161
    edge[i] = r[i-3 ? i+1 : 0] - r[i];
162
 
163
  for(int i=0;i<4;i++)
164
    angle_r[i] = TMath::ACos(/*TMath::Abs*/( ((-edge[i ? i-1 : 3]).Unit()) * (edge[i].Unit()) ));
25 f9daq 165
};
166
 
167
CPlane4::CPlane4(TVector3 *vr)
168
{
169
  double x1,y1,z1, x2,y2,z2, x3,y3,z3;
170
 
72 f9daq 171
  x1 = vr[0].x(); y1 = vr[0].y(); z1 = vr[0].z();
172
  x2 = vr[1].x(); y2 = vr[1].y(); z2 = vr[1].z();
173
  x3 = vr[2].x(); y3 = vr[2].y(); z3 = vr[2].z();
25 f9daq 174
 
72 f9daq 175
  A = y3*(z1 - z2) + y1*(z2 - z3) + y2*(z3 - z1);
176
  B = x3*(z2 - z1) + x1*(z3 - z2) + x2*(z1 - z3);
177
  C = x3*(y1 - y2) + x1*(y2 - y3) + x2*(y3 - y1);
178
  D = y3*(x1*z2 - x2*z1) + x3*(y2*z1 - y1*z2) + z3*(x2*y1 - x1*y2);
25 f9daq 179
 
72 f9daq 180
  r[0] = vr[0]; r[1] = vr[1]; r[2] = vr[2]; r[3] = vr[3];
181
  n.SetXYZ(A, B, C);
182
  n = n.Unit();
25 f9daq 183
 
72 f9daq 184
  for(int i=0;i<4;i++)
185
    edge[i] = r[i-3 ? i+1 : 0] - r[i];
186
 
187
  for(int i=0;i<4;i++)
188
    angle_r[i] = TMath::ACos(/*TMath::Abs*/( ((-edge[i ? i-1 : 3]).Unit()) * (edge[i].Unit()) ));
25 f9daq 189
};
190
//-----------------------------------------------------------------------------
191
// posce presecisce !neskoncne! ravnine s premico (class CRay)
192
// ce najde presecisce vrne 1
193
int CPlane4::GetIntersection(TVector3 *vec, CRay ray)
194
{
72 f9daq 195
  TVector3 N; //nenormirani vektor (A,B,C)
196
  double num, den; //stevec, imenovalec
197
  double t;
198
  TVector3 tmp;
25 f9daq 199
 
72 f9daq 200
  N.SetXYZ(A,B,C);
25 f9daq 201
 
72 f9daq 202
  num = N*ray.GetR() + D;
73 f9daq 203
  den = N*ray.GetK();
25 f9daq 204
 
72 f9daq 205
  if (dbg) printf("t = %6.3lf / %6.3lf =  %6.3lf\n", num, den, num/den);
206
 
207
  //if(den == 0)
208
  if(TMath::Abs(den) < MARGIN) {
209
      //if(num == 0)
210
      if(TMath::Abs(num) < MARGIN) {
211
          if (dbg) printf("The ray is on the surface!\n");
212
          return 0; //return 2; // premica lezi na ravnini
213
      }
214
      else {
215
          if (dbg) printf("The ray is parallel to the surface!\n");
216
          return 0; // ni presecisca
217
      }
218
  }
219
 
220
  t = num / den;
221
 
222
  tmp = ray.GetR();
73 f9daq 223
  tmp -= t*ray.GetK();
72 f9daq 224
  *vec = tmp;
225
  return 1;
25 f9daq 226
}
227
//-----------------------------------------------------------------------------
228
// ali je vektor vec, ki lezi na ravnini skupaj z e1 in e2, med njima
229
// angle_r je kot med e1 in e2, vsi vektorji imajo skupno izhodisce
230
int CPlane4::IsInTri(TVector3 vec, TVector3 e1, TVector3 e2, double angle)
231
{
232
  double angle_ve1, angle_ve2;
233
 
72 f9daq 234
  if(dbg) printf("--- CPlane4::IsInTri ---\n");
25 f9daq 235
 
72 f9daq 236
  angle_ve1 = TMath::ACos(/*TMath::Abs*/( (e1.Unit()) * (vec.Unit()) ));
237
  angle_ve2 = TMath::ACos(/*TMath::Abs*/( (e2.Unit()) * (vec.Unit()) ));
25 f9daq 238
 
72 f9daq 239
  if(dbg)
240
    {
241
      printf("angle_ve1 = %lf\n", angle_ve1*DEGREE);
242
      printf("angle_ve2 = %lf\n", angle_ve2*DEGREE);
243
      printf("angle_sum = %lf\n", (angle_ve1 + angle_ve2)*DEGREE);
244
      printf("  angle_r   = %lf\n", angle*DEGREE);
245
    }
246
 
25 f9daq 247
  bool difference = (MARGIN < TMath::Abs(angle - (angle_ve1 + angle_ve2)));
248
  if (dbg) printf("  MARGIN < Difference = %d\n", difference);
249
  return (int) !difference;
250
}
251
//-----------------------------------------------------------------------------
252
// ali je vektor vec, ki lezi na ravnini!, znotraj meja, ki jih definirajo
253
// strije vogali te ravnine r[i]
254
int CPlane4::IsVectorIn(TVector3 vec)
255
{
256
  int status;
257
 
72 f9daq 258
  if(dbg) printf("--- CPlane4::IsVectorIn ---\n");
25 f9daq 259
 
72 f9daq 260
  for(int i=0;i<3;i++)
261
    {
262
      status = IsInTri(vec - r[i], edge[i], -edge[i ? i-1 : 3], angle_r[i]);
263
      if(dbg) printf("  [%d] vec is %s\n", i, status ? "inside" : "outside");
264
      if(!status) return 0;
265
    }
266
 
267
  return 1;
25 f9daq 268
}
269
int CPlane4::TestIntersection(CRay in)
270
{
72 f9daq 271
  TVector3 tmp;
25 f9daq 272
 
72 f9daq 273
  if( GetIntersection(&tmp, in) )
274
    if( IsVectorIn(tmp) )
275
      return 1;
276
 
277
  return 0;
25 f9daq 278
}
279
int CPlane4::TestIntersection(TVector3 *vec, CRay in)
280
{
72 f9daq 281
  TVector3 tmp;
25 f9daq 282
 
72 f9daq 283
  if( GetIntersection(&tmp, in) )
284
    if( IsVectorIn(tmp) ) {
285
        *vec = tmp;
286
        return 1;
287
    }
288
 
289
  return 0;
25 f9daq 290
}
291
void CPlane4::Print()
292
{
72 f9daq 293
  printf("--- CPlane4::Print() ---\n");
294
  printf("  r=(%.2lf, %.2lf, %.2lf); n=(%.2lf, %.2lf, %.2lf); ",
295
      r[0].x(), r[0].y(), r[0].z(), n.x(), n.y(), n.z());
296
  printf(  "(A,B,C,D)=(%.2lf, %.2lf, %.2lf, %.2lf) \n", A, B, C, D);
297
  for(int i=0;i<4;i++) printf("  edge[%d] = (%lf, %lf, %lf)\n", i, edge[i].x(), edge[i].y(), edge[i].z());
298
  for(int i=0;i<4;i++) printf("  angle[%d] = %lf\n", i, angle_r[i]*DEGREE);
25 f9daq 299
}
300
void CPlane4::Draw(int color, int width)
301
{
72 f9daq 302
  TPolyLine3D *line3d = new TPolyLine3D(5);
25 f9daq 303
 
72 f9daq 304
  for(int i=0;i<4;i++) line3d->SetPoint(i, r[i].x(), r[i].y(), r[i].z());
305
  line3d->SetPoint(4, r[0].x(), r[0].y(), r[0].z());
306
  line3d->SetLineWidth(width); line3d->SetLineColor(color);
25 f9daq 307
 
72 f9daq 308
  line3d->Draw();
25 f9daq 309
}
310
 
311
 
312
CSurface::CSurface(int type0):
72 f9daq 313
      type(type0)
25 f9daq 314
{
72 f9daq 315
  TVector3 vr[4];
316
  TDatime now;
25 f9daq 317
 
72 f9daq 318
  vr[0].SetXYZ(0.0,-1.0,-1.0);
319
  vr[1].SetXYZ(0.0,-1.0, 1.0);
320
  vr[2].SetXYZ(0.0, 1.0, 1.0);
321
  vr[3].SetXYZ(0.0, 1.0,-1.0);
322
  //CPlane4::Set(vr);
323
  SetIndex(1.0, 1.5);
324
 
325
  reflection = c_reflectivity;
326
  rand.SetSeed(now.Get());
327
 
328
  SetFresnel();
25 f9daq 329
}
330
CSurface::CSurface(int type0, TVector3 r1, TVector3 r2, TVector3 r3, TVector3 r4, double n10, double n20, double reflectivity)
331
{
72 f9daq 332
  TDatime now;
333
 
334
  type = type0; CPlane4::Set(r1, r2, r3, r4);
335
  SetIndex(n10, n20);
336
 
337
  reflection = reflectivity;
338
  rand.SetSeed(now.Get());
339
 
340
  SetFresnel();
25 f9daq 341
}
342
CSurface::CSurface(int type0, TVector3 *vr, double n10, double n20, double reflectivity)
343
{
72 f9daq 344
  TDatime now;
345
 
346
  type = type0; CPlane4::Set(vr);
347
  SetIndex(n10, n20);
348
 
349
  reflection = reflectivity;
350
  rand.SetSeed(now.Get());
351
 
352
  SetFresnel();
25 f9daq 353
}
354
void CSurface::SetIndex(double n10, double n20)
355
{
72 f9daq 356
  n1 = n10; n2 = n20; n1_n2 = n1/n2;
357
 
358
  if(n1 > n2)
359
    cosTtotal = TMath::Sqrt( 1 - TMath::Power(n2/n1, 2) );
360
  else
361
    cosTtotal = 0.0;
25 f9daq 362
}
363
//-----------------------------------------------------------------------------
364
// sprejme zarek, vrne uklonjen/odbit zarek in presecisce
365
// vrne 0 ce ni presecisca; 1 ce se je lomil
366
// 2 ce se je odbil; -2 ce se je absorbiral
367
int CSurface::PropagateRay(CRay in, CRay *out, TVector3 *intersection)
368
{
369
  if (dbg) printf("--- CSurface::PropagateRay ---\n");
71 f9daq 370
  double cosTi; // incident ray angle
371
  double cosTt; // transmited ray angle
25 f9daq 372
  TVector3 intersect, transmit;
373
 
72 f9daq 374
  if( !(GetIntersection(&intersect, in) == 1) )
375
    return 0;
376
 
377
  *intersection = intersect;
378
  if( !IsVectorIn(intersect) )
379
    return 0;
380
 
381
  // --------------- Fresnel ----------------------------------------------------
382
  // R_f = a_te * R_te  +  a_tm * R_tm
383
  // e - electrical/perependicular
384
  // m - magnetic polarization/parallel
385
  double r_te=0;
386
  double r_tm=0;
387
  double R_te=0; // s reflection coefficient
388
  double R_tm=0; // p reflection coefficient
389
  double R_f = 0.0;
390
  double a_te = 0.0; // s-wave amplitude, cos Alpha
391
  double a_tm = 0.0; // p-wave amplitude, sin Alpha
392
  TVector3 v_te; // unit s-polarization vector
393
  TVector3 v_tm; // unit p-polarization vector
394
  TVector3 v_tm_t;// transmited polarization parallel with the plane of incidence
73 f9daq 395
  TVector3 pol_t = in.GetP(); // incident polarization
72 f9daq 396
  int sign_n; // sign of normal direction vs. inbound ray
397
  double cosTN; // debug
398
 
73 f9daq 399
  // Decomposition of incident polarization vector
400
  // using unit vectors v_tm & v_te
401
  // in a_tm and a_te components
402
  //if(fresnel) {
403
      // s-polarization unit vector v_te
72 f9daq 404
      // is in the plane orthogonal to the plane of incidence
405
      // defined as the plane spanned by
406
      // incident surface vector n and wave vector k
73 f9daq 407
      // k in this notation is in.GetK()
408
      v_te = n.Cross(in.GetK());
72 f9daq 409
      v_te = v_te.Unit();
73 f9daq 410
      v_tm = -v_te.Cross(in.GetK());
72 f9daq 411
      v_tm = v_tm.Unit();
412
      if(dbg) {
413
          printf("  v_te = "); printv(v_te);
414
          printf("  v_tm = "); printv(v_tm);
415
      }
416
 
417
      double cosAf = v_te * in.GetP();
73 f9daq 418
      double alpha = acos(cosAf);
419
      if(dbg) printf("  cosAf = %lf (Af = %lf)\n", cosAf, alpha*DEGREE);
72 f9daq 420
 
421
      a_te = cosAf;
422
      a_tm = TMath::Sqrt(1 - cosAf*cosAf);
423
      if(dbg) printf("  a_te = %lf, a_tm = %lf\n", a_te, a_tm);
73 f9daq 424
  //}
72 f9daq 425
  // ----------------------------------------------------------------------------
426
 
427
  // reflection probability
428
  double p_ref = rand.Uniform(0.0, 1.0);
429
 
430
  if(type == SURF_TOTAL) type = SURF_REFRA;
431
  switch(type){
432
  // ----------------------------------------------------------------------------
433
  // --------------- refraction from n1 to n2 -----------------------------------
434
  // ----------------------------------------------------------------------------
435
  case SURF_REFRA:
73 f9daq 436
    cosTi = in.GetK() * n;
72 f9daq 437
    if(dbg) printf("  cosTi = %lf (Ti = %lf)\n", cosTi, TMath::ACos(cosTi)*DEGREE);
438
    sign_n = -sign(cosTi);
439
    if(dbg) printf("  sign_n = %d\n", sign_n);
440
    cosTi = TMath::Abs(cosTi);
441
 
442
    // Check if there can be total reflection: n1 > n2
443
    if(N1_N2(-sign_n) < 1.0)
444
      cosTtotal = TMath::Sqrt( 1 - TMath::Power(N1_N2(-sign_n), 2) );
445
    else
446
      cosTtotal = 0.0;
447
 
448
    if(dbg) printf("  cosTtotal = %lf (Ttotal = %lf)\n", cosTtotal, TMath::ACos(cosTtotal)*DEGREE);
449
    // reflection dependance on polarization missing
450
    // reflection hardcoded to 0.96
451
    if (dbg) printf("   reflection probability = %f\n", p_ref);
452
 
453
    // If n1>n2 and theta>thetaCritical, total reflection
454
    if(cosTi < cosTtotal) {
455
        if(dbg) printf("  TOTAL\n");
73 f9daq 456
        transmit = in.GetK() + sign_n*2*cosTi*n;
72 f9daq 457
 
458
        if(dbg) {
459
            cosTN = TMath::Abs(transmit.Unit() * n);
460
            printf("  cosTN = %lf (TN = %lf) (Abs(TN) = %lf)\n", cosTN, TMath::ACos(cosTN)*DEGREE, TMath::ACos(TMath::Abs(cosTN))*DEGREE);
461
        }
462
        out->Set(intersect, transmit);
463
 
73 f9daq 464
        // Shift implemented, but only linear polarization is implemented
465
        if (dbg) printf("CSurface: Propagate TOTAL\n");
466
        v_tm_t = -v_te.Cross(transmit);
467
        v_tm_t = v_tm_t.Unit();
468
        // shift the p and s components
469
        double n12 = N1_N2(-sign_n);
470
        double deltaP = 2 * atan(sqrt(1 - cosTi*cosTi - pow(n12,2))/(pow(n12,2)*cosTi));
471
        double deltaS = 2 * atan(sqrt(1 - cosTi*cosTi - pow(n12,2))/cosTi);
472
        double delta = deltaP - deltaS;
473
        alpha += delta;
474
        a_tm = sin(alpha);
475
        a_te = cos(alpha);
476
        if (dbg) printf("  deltaP = %f deltaS = %f; new a_tm = %f, a_te = %f",
477
            deltaP, deltaS, a_tm, a_te);
478
        pol_t = a_tm*v_tm_t + a_te*v_te;
479
        if (dbg) printv(pol_t);
480
        out->setPolarization(pol_t);
481
 
72 f9daq 482
        return REFLECTION;
483
    } else {
484
        // reflection or refraction according to Fresnel equations
485
        if(dbg) printf("  REFRACTION\n");
486
        if(dbg) printf("  N1_N2(sign_n) = %lf\n", N1_N2(sign_n));
487
        cosTt = TMath::Sqrt(1 - TMath::Power(N1_N2(sign_n), 2)*(1 - TMath::Power(cosTi, 2)));
488
        if(dbg) printf("  cosTt = %lf (Tt = %lf) \n", cosTt, TMath::ACos(cosTt)*DEGREE);
489
 
73 f9daq 490
        transmit = N1_N2(sign_n)*in.GetK() + sign_n*(N1_N2(sign_n)*cosTi - cosTt)*n;
72 f9daq 491
        if(dbg) {printf("  transmit.Unit() = "); printv(transmit.Unit());}
492
        if(dbg) {
493
            cosTN = transmit.Unit() * n;
494
            printf("  cosTN = %lf (TN = %lf) (Abs(TN) = %lf)\n", cosTN, TMath::ACos(cosTN)*DEGREE, TMath::ACos(TMath::Abs(cosTN))*DEGREE);
495
        }
496
 
497
        //if(cosTi<=cosTtotal) cosTt = TMath::Sqrt(1 - TMath::Power(N1_N2(sign_n), 2)*(1 - TMath::Power(cosTi, 2)));
498
        //if(fresnel) {
499
        r_te = (n1*cosTi - n2*cosTt)/(n1*cosTi + n2*cosTt); // transverse
500
        r_tm = (n2*cosTi - n1*cosTt)/(n1*cosTt + n2*cosTi); // paralel
501
 
502
        if(dbg) printf("  r_te = %lf, r_tm = %lf\n", r_te, r_tm);
503
 
504
        // transmited polarization
505
        v_tm_t = -v_te.Cross(transmit);
506
        v_tm_t = v_tm_t.Unit();
73 f9daq 507
        pol_t = a_te*(1.0 - TMath::Abs(r_te))*v_te + a_tm*(1.0 - TMath::Abs(r_tm))*v_tm_t;
72 f9daq 508
 
509
        if(dbg) {
510
            printf("  v_tm_t = "); printv(v_tm_t);
511
            printf("  pol_t = "); printv(pol_t);
512
        }
513
 
71 f9daq 514
        // Fresnel coefficients
72 f9daq 515
        R_te = TMath::Power(r_te, 2);
516
        R_tm = TMath::Power(r_tm, 2);
517
        R_f = a_te*a_te*R_te + a_tm*a_tm*R_tm;
25 f9daq 518
 
72 f9daq 519
        if (dbg) printf("  R_te = %lf, R_tm = %lf, R_f = %lf\n", R_te, R_tm, R_f);
520
    }
25 f9daq 521
 
72 f9daq 522
    if(p_ref >= R_f) { // se lomi
523
        if (dbg) printf("   SURFACE REFRACTED. Return.\n");
524
        out->Set(intersect, transmit);
73 f9daq 525
        out->setPolarization(pol_t);
72 f9daq 526
        return REFRACTION;
527
    } else { // se odbije
528
        if (dbg) printf("   SURFACE REFLECTED. p_ref=%f, R_f=%f\n", p_ref, R_f);
73 f9daq 529
        transmit = in.GetK() + sign_n*2*cosTi*n;
530
        TVector3 v_tm_r = -v_te.Cross(transmit);
531
        v_tm_r = v_tm_r.Unit();
72 f9daq 532
        out->Set(intersect, transmit);
73 f9daq 533
        //pol_t = -in.GetP() + sign_n*2*cosTi*n;
534
        pol_t = a_te*(1.0 - TMath::Abs(r_te))*v_te + a_tm*(1.0 - TMath::Abs(r_tm))*v_tm_r;
535
        out->setPolarization(pol_t);
72 f9daq 536
        return REFLECTION;
537
    }
538
 
539
    //}
540
    break;
541
 
542
    // ----------------------------------------------------------------------------
543
    // --------------- reflection at "reflection" probability ---------------------
544
    // ----------------------------------------------------------------------------
545
  case SURF_REFLE:
546
    p_ref = rand.Uniform(0.0, 1.0);
547
    if(p_ref < reflection) { // se odbije
73 f9daq 548
        cosTi = in.GetK() * n;
549
        transmit = in.GetK() - 2*cosTi*n;
72 f9daq 550
        out->Set(intersect, transmit);
551
        return REFLECTION; //sdhfvjhsdbfjhsdbcvjhsb
552
    } else { // se ne odbije
73 f9daq 553
        transmit = in.GetK();
72 f9daq 554
        out->Set(intersect, transmit);
555
        return ABSORBED;
556
    }
557
    break;
558
 
559
    // total reflection from n1 to n2 with R probbability
560
  case SURF_IMPER:
561
    p_ref = rand.Uniform(0.0, 1.0);
562
    if(p_ref < reflection) { // se odbije
73 f9daq 563
        cosTi = in.GetK() * n;
72 f9daq 564
        if(TMath::Abs(cosTi) < cosTtotal) { // totalni odboj
73 f9daq 565
            transmit = in.GetK() - 2*cosTi*n;
72 f9daq 566
            out->Set(intersect, transmit);
567
        } else { // ni tot. odboja
73 f9daq 568
            transmit = in.GetK();
72 f9daq 569
            out->Set(intersect, transmit);
570
            return ABSORBED;
571
        }
572
    } else { // se ne odbije
73 f9daq 573
        transmit = in.GetK();
72 f9daq 574
        out->Set(intersect, transmit);
575
        return ABSORBED;
576
    }
577
    break;
578
 
579
  default:
580
    *out = in;
581
    break;
582
  }
583
 
584
  return REFRACTION;
25 f9daq 585
}
586
 
72 f9daq 587
Guide::Guide(TVector3 center0, DetectorParameters &parameters) :
588
    _d(parameters.getD()),
589
    _n1(parameters.getN1()),
590
    _n2(parameters.getN2()),
591
    _n3(parameters.getN3()),
592
    _r(c_reflectivity),
593
    _absorption(0),
594
    _A(0),
595
    _badCoupling(parameters.badCoupling())
25 f9daq 596
{
72 f9daq 597
  double t;
598
  TDatime now;
599
  rand.SetSeed(now.Get());
600
  center = center0;
601
  double b = parameters.getB();
602
  double a = parameters.getA();
603
  // if PlateOn, then n0 = n3 (optical grease), else = n1 (air)
604
  //double n0 = (parameters.getPlateOn() ? parameters.getN3(): n1);
605
  double n0 = (parameters.getPlateOn() ? _n2 : _n1);
606
  int fresnel = parameters.getFresnel();
25 f9daq 607
 
72 f9daq 608
  // light guide edges
609
  t = b/2.0;
610
  vodnik_edge[0].SetXYZ(0.0, t,-t);
611
  vodnik_edge[1].SetXYZ(0.0, t, t);
612
  vodnik_edge[2].SetXYZ(0.0,-t, t);
613
  vodnik_edge[3].SetXYZ(0.0,-t,-t);
614
  t = a/2.0;
615
  vodnik_edge[4].SetXYZ(_d, t,-t);
616
  vodnik_edge[5].SetXYZ(_d, t, t);
617
  vodnik_edge[6].SetXYZ(_d,-t, t);
618
  vodnik_edge[7].SetXYZ(_d,-t,-t);
25 f9daq 619
 
72 f9daq 620
  for(int i = 0; i<8; i++) vodnik_edge[i] += center;
25 f9daq 621
 
72 f9daq 622
  // light guide surfaces
623
  s_side[0] = new CSurface(SURF_REFRA, vodnik_edge, n0, _n2, _r);
624
  s_side[0]->FlipN();
625
 
626
  s_side[1] = new CSurface(SURF_REFRA, vodnik_edge[3], vodnik_edge[2],
627
      vodnik_edge[6], vodnik_edge[7], _n2, _n1, _r);
628
  s_side[2] = new CSurface(SURF_REFRA, vodnik_edge[2], vodnik_edge[1],
629
      vodnik_edge[5], vodnik_edge[6], _n2, _n1, _r);
630
  s_side[3] = new CSurface(SURF_REFRA, vodnik_edge[1], vodnik_edge[0],
631
      vodnik_edge[4], vodnik_edge[5], _n2, _n1, _r);
632
  s_side[4] = new CSurface(SURF_REFRA, vodnik_edge[0], vodnik_edge[3],
633
      vodnik_edge[7], vodnik_edge[4], _n2, _n1, _r);
634
  // n3 - ref ind at the exit, grease, air
635
  s_side[5] = new CSurface(SURF_REFRA, &vodnik_edge[4], _n2, _n3, _r);
636
  s_side[5]->FlipN();
637
  // exit surface in the case of bad coupling
638
  noCoupling = new CSurface(SURF_REFRA, &vodnik_edge[4], _n2, 1.0, _r);
639
  noCoupling->FlipN();
640
  // grease = specific pattern area of coupling
641
  TVector3 activePosition(center);
642
  activePosition += TVector3(_d, 0, 0);
643
  TVector3 normal(1,0,0);
73 f9daq 644
  grease = new CPlaneR(activePosition, normal, 0.95*a/2.0);
72 f9daq 645
 
646
  if(fresnel) for(int i=0; i<6; i++) s_side[i]->SetFresnel(1);
647
 
648
  // statistics histograms
649
  hfate = (TH1F*)gROOT->FindObject("hfate"); if(hfate) delete hfate;
650
  hfate = new TH1F("hfate", "Ray fate", 8, -3.5, 4.5);
651
  (hfate->GetXaxis())->SetBinLabel(1, "Back Ref");
652
  (hfate->GetXaxis())->SetBinLabel(2, "No Ref");
653
  (hfate->GetXaxis())->SetBinLabel(3, "Refrac");
654
  (hfate->GetXaxis())->SetBinLabel(4, "LG Miss");
655
  (hfate->GetXaxis())->SetBinLabel(5, "Exit");
656
  (hfate->GetXaxis())->SetBinLabel(6, "Enter");
657
  (hfate->GetXaxis())->SetBinLabel(7, "Rays");
658
  (hfate->GetXaxis())->SetBinLabel(8, "Absorb");
659
 
660
  hnodb_all = (TH1F*)gROOT->FindObject("hnodb_all"); if(hnodb_all) delete hnodb_all;
661
  hnodb_all = new TH1F("hnodb_all", "", MAX_REFLECTIONS, -0.5, MAX_REFLECTIONS-0.5);
662
 
663
  hnodb_exit = (TH1F*)gROOT->FindObject("hnodb_exit"); if(hnodb_exit) delete hnodb_exit;
664
  hnodb_exit = new TH1F("hnodb_exit", "", MAX_REFLECTIONS, -0.5, MAX_REFLECTIONS-0.5);
665
 
666
  int nBins = nch + 1;
667
  hin = (TH2F*)gROOT->FindObject("hin"); if(hin) delete hin;
668
  hin = new TH2F("hin", ";x [mm]; y[mm]", nBins, -b/2.0, +b/2.0, nBins, -b/2.0, +b/2.0);
669
 
670
  hout = (TH2F*)gROOT->FindObject("hout"); if(hout) delete hout;
671
  hout = new TH2F("hout", ";x [mm];y [mm]", nBins, -a/2.0, +a/2.0, nBins, -a/2.0, +a/2.0);
25 f9daq 672
}
673
//-----------------------------------------------------------------------------
674
// Sledi zarku skozi vodnik. Vrne:                                             
675
//  0, ce zgresi vstopno ploskev                                               
676
//  1, ce zadane izstopno ploskev                                              
677
// -1, ce se v vodniku ne odbije totalno 
678
//  2, enter the light guide, bin 2 of hfate = refraction                                     
679
// -2, ce se ne odbije zaradi koncnega R stranic                               
680
// -3, ce se odbije nazaj in gre nazaj ven skozi sprednjo ploskev              
681
// +4, ce se absorbira v materialu                                             
682
Fate Guide::PropagateRay(CRay in, CRay *out, int *n_points, TVector3 *points)
683
{
684
  if (dbg) printf("--- GUIDE::PropagateRay ---\n");
72 f9daq 685
  // ray0 - incident ray
686
  // ray1 - trans/refl ray
25 f9daq 687
  CRay ray0;
688
  CRay ray1;
689
  TVector3 vec0, vec1;
690
  int inters_i = 0;
72 f9daq 691
 
692
  ray0 = in;
693
  int n_odb = 0;
694
  int last_hit = 0;
695
  int propagation = 0;
696
  int result = s_side[0]->PropagateRay(ray0, &ray1, &vec1);
697
  if( !(result) ) {
698
      // ce -NI- presecisca z vstopno
699
      if (dbg) printf("  GUIDE: missed the light guide\n");
700
      fate = missed;
701
      //hfate->Fill(0);
702
  } else if(result == REFLECTION) {
703
      if (dbg) printf(" REFLECTED on the entry surface!\n");
704
      fate = backreflected;
705
      //hfate->Fill(-3);
706
  } else {
707
      if (dbg) printf("  GUIDE: ray entered\n");
708
      points[0] = ray1.GetR();
709
      hfate->Fill(enter); // enter
710
      hin->Fill(vec1.y(), vec1.z());
711
      if (dbg) printf("  GUIDE: n_odb = %d\n", n_odb);
712
 
713
      while (n_odb++ < MAX_REFLECTIONS) {
714
          if (dbg) printf("  GUIDE: Boundary test: %d\n",n_odb);
715
          ray0 = ray1;
716
          vec0 = vec1;
717
          propagation = 11;
718
          for(inters_i=0; inters_i<6; inters_i++) {
719
              if (dbg) printf("  GUIDE: Test intersection with surface %d \n", inters_i);
720
              if( inters_i != last_hit) {
721
                  int testBoundary = s_side[inters_i]->TestIntersection(&vec1, ray1);
722
                  if( testBoundary ) {
723
                      if (dbg) printf("  GUIDE: ray intersects with LG surface %d\n",inters_i);
724
                      break;
725
                  }
726
              }
727
          }
728
          points[n_odb] = vec1;
729
          if(inters_i == 0) {
730
              fate = backreflected;
731
              //hfate->Fill(backreflected);
732
              break;
733
          } // backreflection
734
 
735
          // the passage is possible, test propagation
736
          propagation = s_side[inters_i]->PropagateRay(ray0, &ray1, &vec1);
737
 
738
          if (dbg) printf("  GUIDE: surface = %d, propagation = %d\n", inters_i, propagation);
739
 
740
 
741
          if(propagation == ABSORBED) {
742
              fate = noreflection;
743
              break;
744
          } //refraction due to finite reflectivity
745
 
746
          if(inters_i == 5) {
747
              if (_badCoupling) {
748
                  TVector3 hitVector(0,0,0);
749
                  bool hitActive = grease->TestIntersection(&hitVector, ray0);
750
                  if (hitActive and dbg) printf("   GUIDE: hit grease\n");
751
                  if (!hitActive) propagation = noCoupling->PropagateRay(ray0, &ray1, &vec1);
752
              }
753
              // check on which side the vector is?
73 f9daq 754
              TVector3 ray = ray1.GetK();
72 f9daq 755
              TVector3 exitNormal = s_side[5]->GetN();
756
              if (dbg) printf("ray*n_5 = %lf\n", ray*exitNormal);
757
              if (ray*exitNormal > 0) {
758
                  if (dbg) printf("  GUIDE: ray is backreflected from exit window.\n");
759
                  fate = backreflected;
760
                  n_odb++;
761
                  points[n_odb] = vec1;
762
                  ray0 = ray1;
763
                  break;
764
              }
765
              fate =  hitExit;
766
              hout->Fill(vec1.y(), vec1.z());
767
              hnodb_exit->Fill(n_odb-1);
768
              n_odb++;
769
              points[n_odb] = vec1;
770
              ray0 = ray1;
771
              break;
772
          }
773
 
774
          if(propagation == REFRACTION) {
775
              fate = refracted;
776
              n_odb++;
777
              points[n_odb] = vec1;
778
              ray0 = ray1;
779
              break;
780
          } // no total reflection when should be
781
 
782
          last_hit = inters_i;
783
      }
784
  }
785
 
786
  //--- material absorption ---
787
  if(_absorption) {
788
      double travel = 0.0;
789
      if (dbg) printf("n_odb = %d\n", n_odb);
790
      for(int point = 0; point < n_odb-1; point++) {
791
          travel += (points[point] - points[point+1]).Mag();
792
          if (dbg) printf("travel = %lf\n", travel);
793
      }
794
      double T_abs = TMath::Exp(-travel/_A);
795
      if(dbg)printf("T_abs = %lf\n", T_abs);
796
      double p_abs = rand.Uniform(0.0, 1.0);
797
      if(dbg)printf("p_abs = %lf\n", p_abs);
798
 
799
      if(p_abs > T_abs) fate = absorbed; // absorption
800
  }
801
  //--- material absorption ---
802
 
803
  hfate->Fill(fate);
804
  hfate->Fill(rays);
805
  hnodb_all->Fill(n_odb-2);
806
  *n_points = n_odb+1;
807
  *out = ray0;
808
  return fate;
25 f9daq 809
}
810
void Guide::GetVFate(int *out)
811
{
72 f9daq 812
  for(int i=0;i<7;i++) out[i] = (int)hfate->GetBinContent(i+1);
25 f9daq 813
}
814
void Guide::Draw(int color, int width)
815
{
72 f9daq 816
  for(int i = 0; i<6; i++) s_side[i]->Draw(color, width);
25 f9daq 817
}
818
void Guide::DrawSkel(int color, int width)
819
{
72 f9daq 820
  TPolyLine3D *line3d = new TPolyLine3D(2);
821
  line3d->SetLineWidth(width); line3d->SetLineColor(color);
25 f9daq 822
 
72 f9daq 823
  for(int i=0; i<4; i++) {
824
      line3d->SetPoint(0, vodnik_edge[i+0].x(), vodnik_edge[i+0].y(), vodnik_edge[i+0].z());
825
      line3d->SetPoint(1, vodnik_edge[i+4].x(), vodnik_edge[i+4].y(), vodnik_edge[i+4].z());
826
      line3d->DrawClone();
827
  }
25 f9daq 828
}
829
 
73 f9daq 830
 
25 f9daq 831
int CPlaneR::TestIntersection(TVector3 *vec, CRay ray)
832
{
72 f9daq 833
  double num, den; //stevec, imenovalec
834
  double t;
835
  TVector3 tmp;
25 f9daq 836
 
72 f9daq 837
  if(dbg) printf("---> CPlaneR::TestIntersection <---\n");
838
  if(dbg) {printf("c = "); printv(center); printf(" | n = "); printv(n); printf("\n");}
25 f9daq 839
 
72 f9daq 840
  double D = - n*center;
841
  num = n*ray.GetR() + D;
73 f9daq 842
  den = n*ray.GetK();
72 f9daq 843
 
844
  if(dbg) printf("D = %.4lf | num = %.4lf | den = %.4lf\n", D, num, den);
845
 
846
  if(TMath::Abs(den) < MARGIN) {
847
      if(TMath::Abs(num) < MARGIN)
848
        return 0;
849
      else
850
        return 0;
851
  }
852
 
853
  t = num / den;
854
 
855
  if(dbg) printf("t = %.4lf | ", t);
856
 
857
  tmp = ray.GetR();
73 f9daq 858
  tmp -= t*ray.GetK();
72 f9daq 859
  *vec = tmp;
860
 
861
  if(dbg) {printv(tmp); printf(" | Rv = %.4lf <> R = %.4lf\n", ((tmp - center).Mag()), _r);}
862
 
863
 
864
  if( ((tmp - center).Mag()) < _r )
865
    return 1;
866
  else
867
    return 0;
25 f9daq 868
}
73 f9daq 869
 
25 f9daq 870
void CPlaneR::Draw(int color, int width)
871
{
72 f9daq 872
  const int NN = 32;
873
  double phi, x, y;
25 f9daq 874
 
72 f9daq 875
  TPolyLine3D *arc;
876
  arc = new TPolyLine3D(NN+1);
877
  arc->SetLineWidth(width);
878
  arc->SetLineColor(color);
879
 
880
  for(int i=0; i<=NN; i++) {
881
      phi = i*2.0*TMath::Pi()/NN;
882
      x = _r*TMath::Cos(phi);
883
      y = _r*TMath::Sin(phi);
884
      arc->SetPoint(i, center.x(),  x,  y);
885
  }
886
  arc->Draw();
25 f9daq 887
}
888
 
889
 
890
CDetector::CDetector(TVector3 center0, DetectorParameters& parameters) :
72 f9daq 891
      center(center0),
892
      glass_on(parameters.getGlassOn()),
893
      glass_d(parameters.getGlassD()),
894
      col_in(2),
895
      col_lg(8),
896
      col_out(4),
897
      col_rgla(6),
898
      col_LG(1),
899
      col_glass(4),
900
      col_active(7),
901
      guide_on(parameters.getGuideOn()),
902
      guide(new Guide(center0, parameters)),
903
      plate(new Plate(parameters)),
904
      _plateWidth(parameters.getPlateWidth()),
905
      _plateOn(parameters.getPlateOn()),
906
      offsetY(parameters.getOffsetY()),
907
      offsetZ(parameters.getOffsetZ())
908
{
909
  //  };
910
 
911
  //-----------------------------------------------------------------------------
912
  //void CDetector::Init()
913
  //{
25 f9daq 914
  double d = parameters.getD();
72 f9daq 915
  double x_offset;
916
  if(guide_on) x_offset = center.x();
917
  else x_offset = center.x() - d;
918
 
919
  double b = parameters.getB();
920
  //double n1 = parameters.getN1();
921
  //double n2 = parameters.getN2();
922
  double n3 = parameters.getN3();
923
  double reflectivity = c_reflectivity;
924
  double x_gap = parameters.getGap().X();
925
  double y_gap = parameters.getGap().Y();
926
  double z_gap = parameters.getGap().Z();
927
 
928
  // additional glass between at top of SiPM
929
  // example: epoxy n=1.60
930
  double n4 = 1.57;
931
  TVector3 plane_v[4];
932
  int nBins = nch + 1;
933
  double p_size = b/2.0;
934
  plane_v[0].SetXYZ(x_offset+d+glass_d, y_gap + p_size, z_gap - p_size);
935
  plane_v[1].SetXYZ(x_offset+d+glass_d, y_gap + p_size, z_gap + p_size);
936
  plane_v[2].SetXYZ(x_offset+d+glass_d, y_gap - p_size, z_gap + p_size);
937
  plane_v[3].SetXYZ(x_offset+d+glass_d, y_gap - p_size, z_gap - p_size);
938
  glass = new CSurface(SURF_REFRA, plane_v, n3, n4, reflectivity);
939
  glass->FlipN();
940
 
941
  // additional circular glass between LG and SiPM
942
  glass_circle = new CPlaneR(TVector3(x_offset+d+glass_d, y_gap, z_gap), TVector3(-1.0, 0.0, 0.0), b);
943
 
944
  hglass = (TH2F*)gROOT->FindObject("hglass"); if(hglass) delete hglass;
945
  hglass = new TH2F("hglass", "",
946
      nBins, y_gap - p_size, y_gap + p_size,
947
      nBins, z_gap - p_size, z_gap + p_size);
948
 
949
  // SiPM active surface
950
  p_size = parameters.getActive()/2.0;
951
  if (dbg) cout<<"SiPM active length "<<parameters.getActive()<<endl;
952
 
953
  plane_v[0].SetXYZ(x_offset+d+x_gap, y_gap + p_size, z_gap - p_size);
954
  plane_v[1].SetXYZ(x_offset+d+x_gap, y_gap + p_size, z_gap + p_size);
955
  plane_v[2].SetXYZ(x_offset+d+x_gap, y_gap - p_size, z_gap + p_size);
956
  plane_v[3].SetXYZ(x_offset+d+x_gap, y_gap - p_size, z_gap - p_size);
957
  active = new CPlane4(plane_v);
958
  //active surface in case of bad coupling is circle d=a
959
  TVector3 activePosition(center);
960
  activePosition += TVector3(d + x_gap, 0, 0);
961
  TVector3 normal(1,0,0);
962
  grease = new CPlaneR(activePosition, normal, 1.0*p_size);
963
 
964
  hactive = (TH2F*)gROOT->FindObject("hactive"); if(hactive) delete hactive;
965
  //hactive = new TH2F("hactive", "Active area hits", nBins, y_gap - p_size, y_gap + p_size, nBins, z_gap - p_size, z_gap + p_size);
966
  hactive = new TH2F("hactive", ";x [mm];y [mm]", nBins, y_gap - p_size + offsetY, y_gap + p_size + offsetY, nBins, z_gap - p_size + offsetZ, z_gap + p_size + offsetZ);
967
 
968
  p_size = b/2.0;
969
  //p_size = 2.5;
970
  //p_size = M*0.6;
971
  hlaser = (TH2F*)gROOT->FindObject("hlaser"); if(hlaser) delete hlaser;
972
  hlaser = new TH2F("hlaser", ";x [mm]; y [mm]", nBins, -p_size+offsetY, p_size+offsetY, nBins, -p_size+offsetZ, p_size+offsetZ);
973
 
974
  // collection surface in SiPM plane
975
  p_size = 1.4*b/2.0;
976
  plane_v[0].SetXYZ(x_offset+d+x_gap, y_gap + p_size, z_gap - p_size);
977
  plane_v[1].SetXYZ(x_offset+d+x_gap, y_gap + p_size, z_gap + p_size);
978
  plane_v[2].SetXYZ(x_offset+d+x_gap, y_gap - p_size, z_gap + p_size);
979
  plane_v[3].SetXYZ(x_offset+d+x_gap, y_gap - p_size, z_gap - p_size);
980
  detector = new CPlane4(plane_v);
981
 
982
  hdetector = (TH2F*)gROOT->FindObject("hdetector"); if(hdetector) delete hdetector;
983
  //hdetector = new TH2F("hdetector", "Hits detector plane", nBins, y_gap - p_size, y_gap + p_size, nBins, z_gap - p_size, z_gap + p_size);
984
  hdetector = new TH2F("hdetector", ";x [mm]; y [mm]", nBins, y_gap-p_size + offsetY, y_gap + p_size + offsetY, nBins, z_gap - p_size + offsetZ, z_gap + p_size + offsetZ);
985
 
986
  /*
25 f9daq 987
        window_circle = new CPlaneR(TVector3(x_offset+d+window_d, y_gap, z_gap), TVector3(-1.0, 0.0, 0.0), window_R);  
72 f9daq 988
 
25 f9daq 989
        p_size = M*a;
990
        plane_v[0].SetXYZ(x_offset+d+window_d, y_gap + p_size, z_gap - p_size);
991
        plane_v[1].SetXYZ(x_offset+d+window_d, y_gap + p_size, z_gap + p_size);
992
        plane_v[2].SetXYZ(x_offset+d+window_d, y_gap - p_size, z_gap + p_size);
993
        plane_v[3].SetXYZ(x_offset+d+window_d, y_gap - p_size, z_gap - p_size);
994
        window = new CSurface(SURF_REFRA, plane_v, n1, n2, reflectivity); window->FlipN();
72 f9daq 995
 
25 f9daq 996
        hwindow = (TH2F*)gROOT->FindObject("hwindow"); if(hwindow) delete hwindow;
997
        hwindow = new TH2F("hwindow", "Hits Window", nch, y_gap - window_R, y_gap + window_R, nch, z_gap - window_R, z_gap + window_R);
72 f9daq 998
   */
999
  p_size = b/2.0;
1000
  histoPlate = (TH2F*)gROOT->FindObject("histoPlate"); if(histoPlate) delete histoPlate;
1001
  histoPlate = new TH2F("histoPlate", "Hits on glass plate", nBins, -p_size, +p_size, nBins, -p_size, +p_size);
25 f9daq 1002
}
54 f9daq 1003
 
25 f9daq 1004
//-----------------------------------------------------------------------------
1005
// vrne 1 ce je zadel aktvino povrsino
1006
// vrne <1 ce jo zgresi
1007
int CDetector::Propagate(CRay in, CRay *out, int draw)
1008
// Sledi zarku skozi vodnik. Vrne:                                             
1009
//  0, ce zgresi vstopno ploskev MISSED                                              
1010
//  1, ce zadane izstopno ploskev HIT                                             
1011
// -1, ce se v vodniku ne odbije totalno REFRACTED
1012
//  2, enter the light guide, bin 2 of hfate EXIT                                     
1013
// -2, ce se ne odbije zaradi koncnega R stranic - no total reflection REFRACTED                             
1014
// -3, ce se odbije nazaj in gre nazaj ven skozi sprednjo ploskev BACK_REFLECTED             
1015
// +4, ce se absorbira v materialu ABSORBED
1016
{
1017
  if (dbg) printf("--- Detector::Propagate ---\n");
72 f9daq 1018
  //CRay *ray0 = new CRay; ray0->Set(in.GetR(), in.GetN()); ray0->SetColor(col_in);
1019
  CRay *rayin = new CRay(in);
1020
  rayin->SetColor(col_in);
1021
  CRay *rayout = new CRay(in);
1022
  rayout->SetColor(col_in);
25 f9daq 1023
 
72 f9daq 1024
  const int max_n_points = guide->GetMAXODB() + 2;
1025
  TVector3 pointsPlate[max_n_points];
1026
  //TVector3 intersection;
1027
  Fate fatePlate;
1028
  int nPointsPlate;
1029
  TPolyLine3D *line3d = new TPolyLine3D(2);
1030
  line3d->SetLineWidth(1);
1031
  line3d->SetLineColor(4);
70 f9daq 1032
 
72 f9daq 1033
  // Draw the plate and propagate the ray through
1034
  // check if the ray should be reflected??
54 f9daq 1035
 
72 f9daq 1036
  if(_plateOn) {
71 f9daq 1037
 
72 f9daq 1038
      fatePlate = plate->propagateRay(*rayin, rayout, &nPointsPlate, pointsPlate);
1039
      if(draw) rayin->DrawS(center.x()- _plateWidth, -10.0);
1040
      if(draw) {
1041
          if(fatePlate == missed) {
1042
              rayout->SetColor(col_in);
1043
              rayout->DrawS(center.x() - _plateWidth, -10.0);
1044
          }
1045
          else if(fatePlate == backreflected){
73 f9daq 1046
              rayout->SetColor(kBlack);
1047
              rayout->DrawS(center.x() - _plateWidth, 7.0);
72 f9daq 1048
              if (dbg) printf("Backreflected at plate!\n");
1049
          }
1050
          else {
1051
              int p_i;
1052
              for(p_i = 0; p_i < nPointsPlate-1; p_i++) {
1053
                  line3d->SetPoint(0, pointsPlate[p_i].x(), pointsPlate[p_i].y(), pointsPlate[p_i].z());
1054
                  line3d->SetPoint(1, pointsPlate[p_i+1].x(), pointsPlate[p_i+1].y(), pointsPlate[p_i+1].z());
1055
                  line3d->DrawClone();
1056
              }
1057
              rayout->DrawS(pointsPlate[p_i].x(), -0.1);
1058
              if(fatePlate == noreflection) { // lost on plate side
1059
                  rayout->SetColor(col_out);
1060
                  rayout->DrawS(pointsPlate[p_i].x(), 10.0);
1061
              }
1062
          }
1063
      }
1064
 
1065
      if(! (fatePlate == hitExit or fatePlate == refracted) ) {
1066
          guide->GetHFate()->Fill(rays);
1067
          if (dbg)printf("CDetector::propagate Simulated ray missed the entry surface!\n");
1068
          if (fatePlate == backreflected)
1069
            guide->GetHFate()->Fill(fatePlate); // reflected back
1070
          else
1071
            guide->GetHFate()->Fill(noreflection); //lost on plate side
1072
          return fatePlate;
1073
      }
1074
 
1075
      //Ray hits light guide
1076
      histoPlate->Fill(pointsPlate[0].y(), pointsPlate[0].z()); // entry point
1077
 
1078
  }
1079
  else {
1080
      //rayout = rayin;
1081
      if(draw) rayout->DrawS(center.x(), -10.0);
1082
  }
1083
 
1084
  // If the ray is not reflected in the plate
1085
  // Draw the light guide and propagate the ray through
1086
 
1087
  //const int max_n_points = guide->GetMAXODB() + 2;
1088
  TVector3 points[max_n_points];
1089
  TVector3 presecisce;
1090
 
1091
  int n_points;
1092
  int fate_glass;
1093
  CRay *ray0 = new CRay(*rayout);
1094
  // delete rayout; -> creates dangling reference when tries to delete ray0!
1095
  //delete rayin; -> delete rayout!
1096
  CRay *ray1 = new CRay;
1097
 
1098
  fate = guide->PropagateRay(*ray0, ray1, &n_points, points);
1099
  if (dbg) {
1100
      if (fate == backreflected) printf("DETECTOR::backreflected\n");
1101
  }
1102
 
1103
  line3d->SetLineColor(col_lg);
1104
  int p_i;
1105
  if(guide_on) {
1106
      if(draw) {
1107
          if(fate == missed) {
1108
              if (dbg) printf("Detector: fate=missed\n");
1109
              TVector3 r = ray1->GetR();
73 f9daq 1110
              TVector3 k = ray1->GetK();
1111
              ray1->Set(r,k);
72 f9daq 1112
              ray1->DrawS(center.x(), 10.0);
1113
          } else {
1114
              for(p_i = 0; p_i < n_points-1; p_i++) {
1115
                  line3d->SetPoint(0, points[p_i].x(), points[p_i].y(), points[p_i].z());
1116
                  line3d->SetPoint(1, points[p_i+1].x(), points[p_i+1].y(), points[p_i+1].z());
1117
                  line3d->DrawClone();
1118
              }
1119
              if(fate != noreflection) {
1120
                  if (dbg) printf("Detector: fate != noreflection, fate = %d\n", (int)fate);
1121
                  if(glass_on) {/*if(fate == 1)*/ ray1->Draw(points[p_i].x(), center.x() + guide->getD() + glass_d);}
1122
                  else {
1123
                      ray1->SetColor(col_out);
1124
                      ray1->DrawS(points[p_i].x(), 10.0);
1125
                  }
1126
              }
1127
          }
1128
      }
1129
 
1130
 
1131
      if(! (fate == hitExit or fate == refracted) ) {
1132
          if (dbg) printf("Detector: fate != hit, refracted\n");
1133
          *out = *ray1;
1134
          delete ray0;
1135
          delete ray1;
1136
          delete rayout;
1137
          delete rayin;
1138
          return fate;
1139
      }
1140
  } else {
1141
      if (dbg) printf("Detector: fate = hit or refracted");
1142
      ray1 = ray0;
1143
      if(draw) {
1144
          //double epoxy = parameters->getGlassD();
1145
          if(glass_on) ray1->Draw(center.x(), center.x() + glass_d);
1146
          else ray1->DrawS(center.x(), 10.0);
1147
      }
1148
  }
1149
 
1150
  fate = missed; // zgresil aktivno povrsino
1151
  if(glass_on) {
1152
      *ray0 = *ray1;
1153
      ray1->SetColor(col_rgla);
1154
      fate_glass = glass->PropagateRay(*ray0, ray1, &presecisce);
1155
      if(fate_glass == REFRACTION) {
1156
          hglass->Fill(presecisce.y(), presecisce.z());
1157
          if(draw) ray1->DrawS(presecisce.x(), 10.0);
1158
          //if(active->TestIntersection(&presecisce, *ray1)) {
1159
          //fate = hitExit;
1160
          //hactive->Fill(offsetY + presecisce.y(), offsetZ + presecisce.z());
1161
          //hlaser->Fill((in.GetR()).y() + offsetY, (in.GetR()).z() + offsetZ);
1162
          //}
1163
          //if(detector->TestIntersection(&presecisce, *ray1))
1164
          //hdetector->Fill(offsetY + presecisce.y(), offsetZ + presecisce.z());
1165
          //} else if(fate_glass == REFLECTION) {
1166
          else
1167
            if(draw) ray1->DrawS(presecisce.x(), 10.0);
1168
      }
1169
  }
1170
 
1171
  // Main test: ray and SiPM surface
1172
  if(active->TestIntersection(&presecisce, *ray1)) {
1173
      fate = hitExit;
1174
      hactive->Fill(offsetY + presecisce.y(), offsetZ + presecisce.z());
1175
      hlaser->Fill((in.GetR()).y() + offsetY, (in.GetR()).z() + offsetZ);
1176
  }
1177
  // If it is on the same plane as SiPM
1178
  if(detector->TestIntersection(&presecisce, *ray1))
1179
    hdetector->Fill(offsetY + presecisce.y(), offsetZ + presecisce.z());
1180
  //}
1181
  //} else {
1182
  //if(draw) ray1->Draw(presecisce.x(), center.x()+d+window_d);
1183
  //}
1184
 
1185
  *out = *ray1;
1186
  delete ray0;
1187
  delete ray1;
1188
  delete rayout;
1189
  delete rayin;
1190
  return fate;
25 f9daq 1191
}
73 f9daq 1192
 
25 f9daq 1193
void CDetector::Draw(int width)
1194
{
72 f9daq 1195
  if(guide_on) {
1196
      if( TMath::Abs(guide->getN1()-guide->getN2()) < MARGIN ) {
1197
          if(_plateOn) plate->drawSkel(col_LG, width);
1198
          guide->DrawSkel(col_LG, width);
1199
      }
1200
      else {
1201
          if(_plateOn) plate->draw(4, width);
1202
          guide->Draw(col_LG, width);
1203
      }
1204
  }
1205
 
1206
  if(glass_on) glass_circle->Draw(col_glass, width);
1207
  //window_circle->Draw(col_glass, width);
1208
  active->Draw(col_active, width);
25 f9daq 1209
}
1210
 
73 f9daq 1211
 
25 f9daq 1212
Plate::Plate(DetectorParameters& parameters)
1213
{
1214
  TVector3 center = CENTER;
1215
  const double b = parameters.getB();
1216
  const double n1 = parameters.getN1();
72 f9daq 1217
  const double n2 = parameters.getN2();
1218
  const double t = b/2.;
1219
  const double plateWidth = parameters.getPlateWidth();
1220
  center.SetX( CENTER.X() - plateWidth );
1221
 
1222
  plate_edge[0].SetXYZ(0.0, t,-t);
1223
  plate_edge[1].SetXYZ(0.0, t, t);
1224
  plate_edge[2].SetXYZ(0.0,-t, t);
1225
  plate_edge[3].SetXYZ(0.0,-t,-t);
1226
  plate_edge[4].SetXYZ(plateWidth, t,-t);
1227
  plate_edge[5].SetXYZ(plateWidth, t, t);
1228
  plate_edge[6].SetXYZ(plateWidth,-t, t);
1229
  plate_edge[7].SetXYZ(plateWidth,-t,-t);
1230
 
1231
  for(int i = 0; i<8; i++) plate_edge[i] += center;
1232
 
1233
  sides[0] = new CSurface(SURF_REFRA, plate_edge, n1, n2, c_reflectivity);
1234
  sides[0]->FlipN();
1235
 
1236
  sides[1] = new CSurface(SURF_REFRA, plate_edge[3], plate_edge[2], plate_edge[6], plate_edge[7], n2, n2, c_reflectivity);
1237
  sides[2] = new CSurface(SURF_REFRA, plate_edge[2], plate_edge[1], plate_edge[5], plate_edge[6], n2, n2, c_reflectivity);
1238
  sides[3] = new CSurface(SURF_REFRA, plate_edge[1], plate_edge[0], plate_edge[4], plate_edge[5], n2, n2, c_reflectivity);
1239
  sides[4] = new CSurface(SURF_REFRA, plate_edge[0], plate_edge[3], plate_edge[7], plate_edge[4], n2, n2, c_reflectivity);
1240
 
1241
  sides[5] = new CSurface(SURF_REFRA, &plate_edge[4], n2, n2, c_reflectivity);
1242
  sides[5]->FlipN();
1243
 
1244
  for(int i=0; i<6; i++) sides[i]->SetFresnel(1);
25 f9daq 1245
}
1246
 
1247
void Plate::draw(int color, int width)
1248
{
72 f9daq 1249
  for(int i = 0; i<6; i++) sides[i]->Draw(color, width);
25 f9daq 1250
}
1251
 
1252
void Plate::drawSkel(int color, int width)
1253
{
72 f9daq 1254
  TPolyLine3D line3d(2);
1255
  line3d.SetLineWidth(width);
1256
  line3d.SetLineColor(color);
25 f9daq 1257
 
72 f9daq 1258
  for(int i=0; i<4; i++) {
1259
      line3d.SetPoint(0, plate_edge[i+0].x(), plate_edge[i+0].y(), plate_edge[i+0].z());
1260
      line3d.SetPoint(1, plate_edge[i+4].x(), plate_edge[i+4].y(), plate_edge[i+4].z());
1261
      line3d.DrawClone();
1262
  }
25 f9daq 1263
}
1264
 
54 f9daq 1265
Fate Plate::propagateRay(CRay in, CRay *out, int *n_points, TVector3 *points)
25 f9daq 1266
{
1267
  CRay ray0;
1268
  CRay ray1;
1269
  TVector3 vec0, vec1;
54 f9daq 1270
  Fate fate = enter;
25 f9daq 1271
  int inters_i = 0;
72 f9daq 1272
 
1273
  ray0 = in;
1274
  int n_odb = 0;
1275
  int last_hit = 0;
1276
  int propagation = 0;
1277
 
1278
  int result = sides[0]->PropagateRay(ray0, &ray1, &vec1);
1279
  if( !result ) {
1280
      // ce -NI- presecisca z vstopno
1281
      fate = missed;
1282
  } else if(result == REFLECTION) {
1283
      if (dbg) printf("PLATE: reflected\n");
73 f9daq 1284
      ray0 = ray1;
72 f9daq 1285
      fate = backreflected;
1286
  } else {
1287
      points[0] = ray1.GetR();
1288
      //hfate->Fill(enter);
1289
      //hin->Fill(vec1.y(), vec1.z());
1290
      while (n_odb++ < MAX_REFLECTIONS) {
1291
          ray0 = ray1;
1292
          vec0 = vec1;
1293
          propagation = 11;
1294
          for(inters_i=0; inters_i<6; inters_i++) {
1295
              if( inters_i != last_hit) {
1296
                  if( sides[inters_i]->TestIntersection(&vec1, ray1) ) break;
1297
              }
1298
          }
1299
          points[n_odb] = vec1;
1300
          if(inters_i == 0) {
73 f9daq 1301
              ray0 = ray1;
72 f9daq 1302
              fate = backreflected;
1303
              break;} // backreflection
1304
 
1305
          propagation = sides[inters_i]->PropagateRay(ray0, &ray1, &vec1);
1306
          if(inters_i == 5) { // successfull exit
1307
              fate = hitExit;
1308
              //hout->Fill(vec1.y(), vec1.z());
1309
              //hnodb_exit->Fill(n_odb-1);
1310
              n_odb++;
1311
              points[n_odb] = vec1;
1312
              ray0 = ray1;
1313
              break;
1314
          }
1315
          if(propagation == 1) {
1316
              fate = noreflection; //at side
1317
              n_odb++;
1318
              points[n_odb] = vec1;
1319
              ray0 = ray1;
1320
              break;} // no total reflection when should be
1321
 
1322
          if(propagation == -2) {
1323
              fate = noreflection;
1324
              break;
1325
          } // absorption due to finite reflectivity
1326
 
1327
          last_hit = inters_i;
1328
      }
1329
  }
1330
 
1331
  *n_points = n_odb+1;
1332
  *out = ray0;
1333
  return fate;
25 f9daq 1334
};
1335
 
1336