Subversion Repositories f9daq

Rev

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