Belle II Software development
shapes.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9// The shape structs below expose their parameters both as named members and as
10// the t[] array of an anonymous union; cppcheck does not see the named ones.
11// cppcheck-suppress-file unusedStructMember
12#include "ecl/geometry/shapes.h"
13#include <G4TessellatedSolid.hh>
14#include <G4TriangularFacet.hh>
15#include <G4QuadrangularFacet.hh>
16#include <G4ExtrudedSolid.hh>
17#include <G4Trap.hh>
18#include "ecl/geometry/BelleCrystal.h"
19#include <cassert>
20#include <iostream>
21#include <fstream>
22#include <iterator>
23#include <CLHEP/Matrix/Vector.h>
24#include <CLHEP/Matrix/Matrix.h>
25#include <G4TwoVector.hh>
26#include <G4Vector3D.hh>
27#include <framework/utilities/FileSystem.h>
28#include <framework/logging/Logger.h>
29
30using namespace std;
31
32namespace Belle2 {
37 namespace ECL {
38 double cosd(double x) {return cos(x * (M_PI / 180));}
39 double sind(double x) {return sin(x * (M_PI / 180));}
40 double tand(double x) {return tan(x * (M_PI / 180));}
41
42 double volume(const G4ThreeVector& v0, const G4ThreeVector& v1, const G4ThreeVector& v2, const G4ThreeVector& v3)
43 {
44 G4ThreeVector x = v1 - v0, y = v2 - v0, z = v3 - v0;
45 return x.cross(y) * z;
46 }
47
48 G4ThreeVector newvertex(double d, const G4ThreeVector& v0, const G4ThreeVector& v1, const G4ThreeVector& v2,
49 const G4ThreeVector& v3)
50 {
51 G4ThreeVector x = v1 - v0, y = v2 - v0, z = v3 - v0;
52 G4ThreeVector nxy = x.cross(y).unit();
53 G4ThreeVector nyz = y.cross(z).unit();
54 G4ThreeVector nzx = z.cross(x).unit();
55
56 CLHEP::HepMatrix A(3, 3);
57 A[0][0] = nxy.x(), A[0][1] = nxy.y(), A[0][2] = nxy.z();
58 A[1][0] = nyz.x(), A[1][1] = nyz.y(), A[1][2] = nyz.z();
59 A[2][0] = nzx.x(), A[2][1] = nzx.y(), A[2][2] = nzx.z();
60
61 CLHEP::HepVector B(3);
62 B[0] = v0 * nxy - d;
63 B[1] = v0 * nyz - d;
64 B[2] = v0 * nzx - d;
65 // int ierr;
66 // cout<<A<<" "<<B<<" "<<A.inverse(ierr)<<endl;
67 CLHEP::HepVector r = A.inverse() * B; //CLHEP::solve(A, B);
68
69 return G4ThreeVector(r[0], r[1], r[2]);
70 }
71
72 G4ThreeVector moveto(double r, double phi)
73 {
74 return G4ThreeVector(r * cosd(phi), r * sind(phi), 0);
75 };
76
77
78 G4VSolid* shape_t::get_solid(const string& prefix, double wrapthick, G4Translate3D& shift) const
79 {
80 return get_bellecrystal(prefix, wrapthick, shift);
81 }
82
83 G4ThreeVector centerofgravity(const map<int, G4ThreeVector>& v, int i0, int n)
84 {
85 double cx = 0, cy = 0, A = 0;
86 for (int j = 0; j < n; j++) {
87 int j0 = j + i0, j1 = ((j + 1) % n) + i0;
88 const G4ThreeVector& v0 = v.find(j0)->second, &v1 = v.find(j1)->second;
89 double t = v0.x() * v1.y() - v0.y() * v1.x();
90 // cout<<j0<<" "<<j1<<" "<<t<<" "<<v0.x()<<" "<<v0.y()<<endl;
91 A += t;
92 cx += (v0.x() + v1.x()) * t;
93 cy += (v0.y() + v1.y()) * t;
94 }
95 cx /= 3 * A;
96 cy /= 3 * A;
97 return G4ThreeVector(cx, cy, v.find(i0)->second.z());
98 }
99
100 Point_t centerofgravity(const Point_t* b, const Point_t* e)
101 {
102 double cx = 0, cy = 0, A = 0;
103 int n = e - b;
104 for (int j = 0; j < n; j++) {
105 int j0 = j, j1 = ((j + 1) % n);
106 const Point_t& v0 = b[j0], &v1 = b[j1];
107 double t = v0.x * v1.y - v0.y * v1.x;
108 A += t;
109 cx += (v0.x + v1.x) * t;
110 cy += (v0.y + v1.y) * t;
111 }
112 cx /= 3 * A;
113 cy /= 3 * A;
114 Point_t t = {cx, cy};
115 return t;
116 }
117
119 struct quadrilateral_t: public shape_t {
120 quadrilateral_t() {}
121 virtual ~quadrilateral_t() override {}
123 virtual map<int, G4ThreeVector> make_verticies(double wrapthick) const = 0;
124
126 G4VSolid* get_tesselatedsolid(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
127 {
128 map<int, G4ThreeVector> v = make_verticies(wrapthick);
129
130 string name(prefix);
131 name += to_string(nshape);
132 G4TessellatedSolid* s = new G4TessellatedSolid(name.c_str());
133
134 // Now add the facets to the solid
135 // top plane
136 s->AddFacet(new G4QuadrangularFacet(v[1], v[4], v[3], v[2], ABSOLUTE));
137 // bottom plane
138 s->AddFacet(new G4QuadrangularFacet(v[5], v[6], v[7], v[8], ABSOLUTE));
139 // lateral sides
140 s->AddFacet(new G4QuadrangularFacet(v[1], v[2], v[6], v[5], ABSOLUTE));
141 s->AddFacet(new G4QuadrangularFacet(v[2], v[3], v[7], v[6], ABSOLUTE));
142 s->AddFacet(new G4QuadrangularFacet(v[3], v[4], v[8], v[7], ABSOLUTE));
143 s->AddFacet(new G4QuadrangularFacet(v[4], v[1], v[5], v[8], ABSOLUTE));
144
145 // Finally declare the solid is complete
146 s->SetSolidClosed(true);
147 return s;
148 }
149
151 G4VSolid* get_extrudedsolid(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
152 {
153 map<int, G4ThreeVector> v = make_verticies(wrapthick);
154
155 string name(prefix);
156 name += to_string(nshape);
157
158 std::vector<G4TwoVector> p1, p2;
159 p1.push_back(G4TwoVector(v[1].x(), v[1].y()));
160 p1.push_back(G4TwoVector(v[2].x(), v[2].y()));
161 p1.push_back(G4TwoVector(v[3].x(), v[3].y()));
162 p1.push_back(G4TwoVector(v[4].x(), v[4].y()));
163
164 p2.push_back(G4TwoVector(v[1 + 4].x(), v[1 + 4].y()));
165 p2.push_back(G4TwoVector(v[2 + 4].x(), v[2 + 4].y()));
166 p2.push_back(G4TwoVector(v[3 + 4].x(), v[3 + 4].y()));
167 p2.push_back(G4TwoVector(v[4 + 4].x(), v[4 + 4].y()));
168
169 double sum = 0, smin = 1e9, smax = -1e9;
170 for (int i = 0; i < 4; i++) {
171 for (int j = i + 1; j < 4; j++) {
172 double s2 = (p2[j] - p2[i]).mag2() / (p1[j] - p1[i]).mag2();
173 double s = sqrt(s2);
174 sum += s;
175 if (s > smax) smax = s;
176 if (s < smin) smin = s;
177 }
178 }
179 double ave = sum / 6;
180
181 double scale = ave;
182 G4TwoVector off1(0, 0), off2(scale * p1[0].x() - p2[0].x(), scale * p1[0].y() - p2[0].y());
183
184 return new G4ExtrudedSolid(name, p1, abs(v[1].z()), off1, 1, -off2, scale);
185 }
186
188 G4VSolid* get_trapezoid(const string& prefix, double wrapthick, G4Translate3D& shift) const override
189 {
190 map<int, G4ThreeVector> v = make_verticies(wrapthick);
191
192 // make sides @ +-Y parallel
193 G4ThreeVector b = v[4] - v[1];
194 G4ThreeVector d12 = v[2] - v[1], d43 = v[3] - v[4];
195 double b2 = b.mag2();
196 double h1 = b.cross(d12).mag2();
197 double h4 = b.cross(d43).mag2();
198 // if(abs(h1/h4-1)<0.02) cout<<"Shape = "<<nshape<<" "<<h1<<" "<<h4<<" "<<h1/h4-1<<endl;
199 if (h1 < h4) {
200 G4ThreeVector d24 = v[4] - v[2];
201 double d43b = d43 * b, s = (b2 * (d24 * d43) - (d24 * b) * d43b) / (d43b * d43b - b2 * d43.mag2());
202 v[3] = v[4] + s * d43;
203 v[7] = v[8] + s * (v[7] - v[8]);
204 } else {
205 G4ThreeVector d31 = v[1] - v[3];
206 double d12b = d12 * b, s = (b2 * (d31 * d12) - (d31 * b) * d12b) / (d12b * d12b - b2 * d12.mag2());
207 v[2] = v[1] + s * d12;
208 v[6] = v[5] + s * (v[6] - v[5]);
209 }
210
211 string name(prefix);
212 name += to_string(nshape);
213
214 G4ThreeVector pt[8];
215 pt[0] = v[4];
216 pt[1] = v[1];
217 pt[2] = v[3];
218 pt[3] = v[2];
219 pt[4] = v[8];
220 pt[5] = v[5];
221 pt[6] = v[7];
222 pt[7] = v[6];
223
224 auto alignz = [&](int i, int j) {pt[j].setZ(pt[i].z());};
225 auto aligny = [&](int i, int j) {pt[j].setY(pt[i].y());};
226
227 alignz(0, 1);
228 alignz(0, 2);
229 alignz(0, 3);
230
231 alignz(4, 1 + 4);
232 alignz(4, 2 + 4);
233 alignz(4, 3 + 4);
234
235 aligny(0, 1);
236 aligny(2, 3);
237
238 aligny(4, 5);
239 aligny(6, 7);
240
241 double dx = pt[0].x() + pt[1].x() + pt[4].x() + pt[5].x() + pt[2].x() + pt[3].x() + pt[6].x() + pt[7].x();
242 dx /= 8;
243 double dy = pt[0].y() + pt[2].y() + pt[4].y() + pt[6].y();
244 dy /= 4;
245 for (int j = 0; j < 8; j++) {
246 pt[j].setX(pt[j].x() - dx);
247 pt[j].setY(pt[j].y() - dy);
248 }
249
250 // int oprec = cout.precision(17);
251 // cout<<dx<<" "<<dy<<endl;
252 // cout.precision(oprec);
253
254 shift = G4Translate3D(dx, dy, 0);
255 G4VSolid* shape = new G4Trap(name.c_str(), pt);
256 // cout<<name<<" "<<shape->GetCubicVolume()*1e-3<<" "<<Volume<<endl;
257 // G4VSolid *shape = new BelleCrystal(name.c_str(), 4, pt);
258 return shape;
259 }
260
262 G4VSolid* get_bellecrystal(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
263 {
264 map<int, G4ThreeVector> v = make_verticies(wrapthick);
265
266 string name(prefix);
267 name += to_string(nshape);
268
269 G4ThreeVector pt[8];
270 pt[0] = v[4];
271 pt[1] = v[1];
272 pt[2] = v[3];
273 pt[3] = v[2];
274 pt[4] = v[8];
275 pt[5] = v[5];
276 pt[6] = v[7];
277 pt[7] = v[6];
278
279 for (int i = 0; i < 8; i++) pt[i] = v[i + 1];
280 G4VSolid* shape = new BelleCrystal(name.c_str(), 4, pt);
281 // cout<<name<<" "<<shape->GetCubicVolume()*1e-3<<" "<<Volume<<endl;
282 // for(int i=0;i<100000;i++){
283 // G4ThreeVector a = shape->GetPointOnSurface();
284 // cout<<a.x()<<" "<<a.y()<<" "<<a.z()<<endl;
285 // }
286 // exit(0);
287 return shape;
288 }
289 };
290
292 struct quadrilateral_barrel_t: public quadrilateral_t {
293 union {
294 struct {
295 double A, B, H, a, b, h, alpha, beta, betap, gamma, Volume, Weight;
296 };
297 double t[12] = {};
298 };
299 quadrilateral_barrel_t() {}
300 virtual ~quadrilateral_barrel_t() override {}
301
303 bool istrap() const override
304 {
305 return true;
306 }
307
309 map<int, G4ThreeVector> make_verticies(double wrapthick) const override
310 {
311 map<int, G4ThreeVector> v;
312 // ensure sides to be parallel
313 double wh = h * h, wH = H * H, wnorm = wh + wH;
314 double tn = ((b - a) / 2 / h * wh + (B - A) / 2 / H * wH) / wnorm, d = h * tn, D = H * tn;
315 // double tn = tan(alpha*M_PI/180), d = h*tn, D = H*tn;
316 double m = (a + b) * 0.5, M = (A + B) * 0.5;
317
318 const double eps = 0.5e-3; // crystal sides are defined with 0.5 micron precision
319 if (fabs(a - (m - d)) > eps || fabs(b - (m + d)) > eps || fabs(A - (M - D)) > eps || fabs(B - (M + D)) > eps) {
320 double alfa = atan(tn) * 180 / M_PI;
321 B2WARNING("Cannot make parallel sides better than 0.5 mcm: alpha =" << alpha << " alpha from sides = " << alfa << " da = " << a -
322 (m - d) << " db = " << b - (m + d) << " dA = " << A - (M - D) << " dB = " << B - (M + D));
323 }
324
325 v[1] = G4ThreeVector(-(m - d) / 2, h / 2, -150);
326 v[2] = G4ThreeVector(-(m + d) / 2, -h / 2, -150);
327 v[3] = G4ThreeVector((m + d) / 2, -h / 2, -150);
328 v[4] = G4ThreeVector((m - d) / 2, h / 2, -150);
329 v[5] = G4ThreeVector(-(M - D) / 2, H / 2, 150);
330 v[6] = G4ThreeVector(-(M + D) / 2, -H / 2, 150);
331 v[7] = G4ThreeVector((M + D) / 2, -H / 2, 150);
332 v[8] = G4ThreeVector((M - D) / 2, H / 2, 150);
333
334 if (wrapthick != 0) {
335 map<int, G4ThreeVector> nv;
336 nv[1] = newvertex(wrapthick, v[1], v[5], v[2], v[4]);
337 nv[2] = newvertex(wrapthick, v[2], v[6], v[3], v[1]);
338 nv[3] = newvertex(wrapthick, v[3], v[7], v[4], v[2]);
339 nv[4] = newvertex(wrapthick, v[4], v[8], v[1], v[3]);
340 nv[5] = newvertex(wrapthick, v[5], v[1], v[8], v[6]);
341 nv[6] = newvertex(wrapthick, v[6], v[2], v[5], v[7]);
342 nv[7] = newvertex(wrapthick, v[7], v[3], v[6], v[8]);
343 nv[8] = newvertex(wrapthick, v[8], v[4], v[7], v[5]);
344 std::swap(nv, v);
345 }
346
347 // if(nshape==1){ for(int j=1;j<=8;j++) cout<<v[j]<<" "; cout<<endl;}
348
349 return v;
350 }
351 };
352
354 struct quadrilateral_endcap_t: public quadrilateral_t {
355 union {
356 struct {
357 double A, B, C, D, a, b, c, d, H_aA, H_dD, dg13, dg24, dg57, dg68, a1, a2, a3, a4, Volume, Weight;
358 };
359 double t[20] = {};
360 };
361 quadrilateral_endcap_t() {}
362 virtual ~quadrilateral_endcap_t() override {}
363
365 bool istrap() const override
366 {
367 double h1 = sind(a1) * D, h4 = sind(a4) * C;
368 return abs(h1 - h4) < 0.01 * h1;
369 }
370
372 map<int, G4ThreeVector> make_verticies(double wrapthick) const override
373 {
374 double minh = std::min(sind(a1) * D, sind(a4) * C);
375 double h2 = minh / 2;
376 double db = (tand(a1 - 90) - tand(a4 - 90)) * h2 / 2;
377
378 map<int, G4ThreeVector> v;
379 v[5] = G4ThreeVector(-A / 2 + db, -h2, -150);
380 v[6] = v[5] + moveto(D, a1);
381 v[8] = G4ThreeVector(A / 2 + db, -h2, -150);
382 v[7] = v[8] + moveto(C, 180 - a4);
383
384 // adjust position of v[2],v[3],v[7],v[6] to have all 4 points in a plane, other combination already in a plane by the construction
385 G4ThreeVector vB = v[7] - v[6], va(a, 0, 0), vd = moveto(d, a1), vc = moveto(c, 180 - a4);
386 double delta = vB.cross(va + vc - vd).z() / vB.cross(vc + vd).z(); // delta should be very small ~10^-6 or less
387
388 vd *= 1 + delta;
389 vc *= 1 - delta;
390
391 v[1] = v[5] + G4ThreeVector((H_aA * cosd(a1) + H_dD) / sind(a1), H_aA, 300);
392 v[2] = v[1] + vd;
393 v[4] = v[1] + va;
394 v[3] = v[4] + vc;
395
396 for (int j = 1; j <= 8; j++) v[j] = G4ThreeVector(v[j].x(), -v[j].y(), -v[j].z());
397
398 // G4ThreeVector c0 = centerofgravity(v, 1, 4);
399 // G4ThreeVector c1 = centerofgravity(v, 5, 4);
400 // G4ThreeVector cz = 0.5*(c0+c1);
401 // cout<<c0<<" "<<c1<<" "<<cz<<endl;
402
403 if (wrapthick != 0) {
404 map<int, G4ThreeVector> nv;
405 nv[1] = newvertex(wrapthick, v[1], v[5], v[2], v[4]);
406 nv[2] = newvertex(wrapthick, v[2], v[6], v[3], v[1]);
407 nv[3] = newvertex(wrapthick, v[3], v[7], v[4], v[2]);
408 nv[4] = newvertex(wrapthick, v[4], v[8], v[1], v[3]);
409 nv[5] = newvertex(wrapthick, v[5], v[1], v[8], v[6]);
410 nv[6] = newvertex(wrapthick, v[6], v[2], v[5], v[7]);
411 nv[7] = newvertex(wrapthick, v[7], v[3], v[6], v[8]);
412 nv[8] = newvertex(wrapthick, v[8], v[4], v[7], v[5]);
413 std::swap(nv, v);
414 }
415 // if(nshape==2){ for(int j=1;j<=8;j++) cout<<v[j]<<" "; cout<<endl;}
416 return v;
417 }
418 };
419
421 struct pent_t: public shape_t {
422 union {
423 struct {
424 double A, C, D, a, c, d, B, b, H_aA, H_dD, dg13, dg24, dg57, dg68, a1, a4, a2, a3, a9, Volume, Weight;
425 };
426 double t[21] = {};
427 };
429 pent_t(): _adjusted(false) {}
430 virtual ~pent_t() override {}
431
433 void adjust()
434 {
435 if (!_adjusted) {
436 // adjust sizes to have flat sides
437 H_dD -= 0.00005551197484235;
438 B += 0.0011245236213532729;
439 b += -0.00044853029662963;
440 _adjusted = true;
441 }
442 }
443
445 bool istrap() const override { return false;}
446
448 map<int, G4ThreeVector> make_verticies(double wrapthick) const
449 {
450 assert(_adjusted);
451
452 double h2 = cosd(a1 - 90) * D / 2;
453 map<int, G4ThreeVector> v;
454 v[5] = G4ThreeVector(-A / 2, -h2, -150);
455 v[6] = v[5] + moveto(D, a1);
456 v[10] = v[6] + moveto(B, a1 + a2 - 180);
457 v[8] = G4ThreeVector(A / 2, -h2, -150);
458 v[7] = v[8] + moveto(D, 180 - a1);
459
460 v[1] = v[5] + G4ThreeVector((H_aA * cosd(a1) + H_dD) / sind(a1), H_aA, 300);
461 v[2] = v[1] + moveto(d, a1);
462 v[9] = v[2] + moveto(b, a1 + a2 - 180);
463 v[4] = v[1] + moveto(a, 0);
464 v[3] = v[4] + moveto(d, 180 - a1);
465
466 for (int j = 1; j <= 10; j++) v[j] = G4ThreeVector(v[j].x(), -v[j].y(), -v[j].z());
467 if (wrapthick != 0) {
468 map<int, G4ThreeVector> nv;
469 nv[ 1] = newvertex(wrapthick, v[1], v[5], v[2], v[4]);
470 nv[ 2] = newvertex(wrapthick, v[2], v[6], v[9], v[1]);
471 nv[ 9] = newvertex(wrapthick, v[9], v[10], v[3], v[2]);
472 nv[ 3] = newvertex(wrapthick, v[3], v[7], v[4], v[9]);
473 nv[ 4] = newvertex(wrapthick, v[4], v[8], v[1], v[3]);
474 nv[ 5] = newvertex(wrapthick, v[5], v[1], v[8], v[6]);
475 nv[ 6] = newvertex(wrapthick, v[6], v[2], v[5], v[10]);
476 nv[10] = newvertex(wrapthick, v[10], v[9], v[6], v[7]);
477 nv[ 7] = newvertex(wrapthick, v[7], v[3], v[10], v[8]);
478 nv[ 8] = newvertex(wrapthick, v[8], v[4], v[7], v[5]);
479 std::swap(nv, v);
480 }
481 return v;
482 }
483
485 G4VSolid* get_tesselatedsolid(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
486 {
487 if (nshape != 36) return nullptr; // only one crystal has pentagon shape
488
489 map<int, G4ThreeVector> v = make_verticies(wrapthick);
490
491 string name(prefix);
492 name += to_string(nshape);
493 G4TessellatedSolid* s = new G4TessellatedSolid(name.c_str());
494
495 // Now add the facets to the solid
496 // top plane
497 s->AddFacet(new G4QuadrangularFacet(v[1], v[4], v[3], v[2], ABSOLUTE));
498 s->AddFacet(new G4TriangularFacet(v[2], v[3], v[9], ABSOLUTE));
499
500 //bottom plane
501 s->AddFacet(new G4QuadrangularFacet(v[5], v[6], v[7], v[8], ABSOLUTE));
502 s->AddFacet(new G4TriangularFacet(v[6], v[10], v[7], ABSOLUTE));
503
504 //sides
505 s->AddFacet(new G4QuadrangularFacet(v[1], v[2], v[6], v[5], ABSOLUTE));
506 s->AddFacet(new G4QuadrangularFacet(v[2], v[9], v[10], v[6], ABSOLUTE));
507 s->AddFacet(new G4QuadrangularFacet(v[9], v[3], v[7], v[10], ABSOLUTE));
508 s->AddFacet(new G4QuadrangularFacet(v[3], v[4], v[8], v[7], ABSOLUTE));
509 s->AddFacet(new G4QuadrangularFacet(v[4], v[1], v[5], v[8], ABSOLUTE));
510
511 // Finally declare the solid is complete
512 s->SetSolidClosed(true);
513 return s;
514 }
515
517 G4VSolid* get_extrudedsolid(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
518 {
519 map<int, G4ThreeVector> v = make_verticies(wrapthick);
520
521 string name(prefix);
522 name += to_string(nshape);
523
524 std::vector<G4TwoVector> p1, p2;
525 p1.push_back(G4TwoVector(v[1].x(), v[1].y()));
526 p1.push_back(G4TwoVector(v[2].x(), v[2].y()));
527 p1.push_back(G4TwoVector(v[9].x(), v[9].y()));
528 p1.push_back(G4TwoVector(v[3].x(), v[3].y()));
529 p1.push_back(G4TwoVector(v[4].x(), v[4].y()));
530
531 p2.push_back(G4TwoVector(v[1 + 4].x(), v[1 + 4].y()));
532 p2.push_back(G4TwoVector(v[2 + 4].x(), v[2 + 4].y()));
533 p2.push_back(G4TwoVector(v[ 10].x(), v[ 10].y()));
534 p2.push_back(G4TwoVector(v[3 + 4].x(), v[3 + 4].y()));
535 p2.push_back(G4TwoVector(v[4 + 4].x(), v[4 + 4].y()));
536
537 double sum = 0, smin = 1e9, smax = -1e9;
538 int count = 0;
539 for (int i = 0; i < 5; i++) {
540 for (int j = i + 1; j < 5; j++) {
541 double s2 = (p2[j] - p2[i]).mag2() / (p1[j] - p1[i]).mag2();
542 double s = sqrt(s2);
543 sum += s;
544 if (s > smax) smax = s;
545 if (s < smin) smin = s;
546 count++;
547 }
548 }
549 double ave = sum / count;
550
551 double scale = ave;
552 G4TwoVector off1(0, 0), off2(scale * p1[0].x() - p2[0].x(), scale * p1[0].y() - p2[0].y());
553
554 return new G4ExtrudedSolid(name, p1, abs(v[1].z()), off1, 1, -off2, scale);
555 }
556
558 G4VSolid* get_trapezoid(const string& prefix, double wrapthick, G4Translate3D& shift) const override
559 {
560 if (nshape != 36) return nullptr; // only one crystal has pentagon shape
561
562 map<int, G4ThreeVector> v = make_verticies(wrapthick);
563
564 string name(prefix);
565 name += to_string(nshape);
566
567 G4ThreeVector pt[8];
568 pt[0] = v[4];
569 pt[1] = v[1];
570 pt[2] = v[3];
571 pt[3] = v[2];
572 pt[4] = v[8];
573 pt[5] = v[5];
574 pt[6] = v[7];
575 pt[7] = v[6];
576
577 auto alignz = [&](int i, int j) {pt[j].setZ(pt[i].z());};
578 auto aligny = [&](int i, int j) {pt[j].setY(pt[i].y());};
579
580 alignz(0, 1);
581 alignz(0, 2);
582 alignz(0, 3);
583
584 alignz(4, 1 + 4);
585 alignz(4, 2 + 4);
586 alignz(4, 3 + 4);
587
588 aligny(0, 1);
589 aligny(2, 3);
590
591 aligny(4, 5);
592 aligny(6, 7);
593
594
595 double dx = pt[0].x() + pt[1].x() + pt[4].x() + pt[5].x() + pt[2].x() + pt[3].x() + pt[6].x() + pt[7].x();
596 dx /= 8;
597 double dy = pt[0].y() + pt[2].y() + pt[4].y() + pt[6].y();
598 dy /= 4;
599 for (int j = 0; j < 8; j++) {
600 pt[j].setX(pt[j].x() - dx);
601 pt[j].setY(pt[j].y() - dy);
602 }
603 shift = G4Translate3D(dx, dy, 0);
604
605 // cout<<name<<" "<<dx<<" "<<dy<<endl;
606
607 G4VSolid* shape = new G4Trap(name.c_str(), pt);
608 // G4VSolid *shape = new BelleCrystal(name.c_str(), 4, pt);
609 return shape;
610 }
611
613 G4VSolid* get_bellecrystal(const string& prefix, double wrapthick, G4Translate3D& shift UNUSED) const override
614 {
615 map<int, G4ThreeVector> v = make_verticies(wrapthick);
616
617 string name(prefix);
618 name += to_string(nshape);
619
620 G4ThreeVector pt[10];
621 pt[0] = v[1];
622 pt[1] = v[2];
623 pt[2] = v[9];
624 pt[3] = v[3];
625 pt[4] = v[4];
626 pt[5] = v[5];
627 pt[6] = v[6];
628 pt[7] = v[10];
629 pt[8] = v[7];
630 pt[9] = v[8];
631
632 G4VSolid* shape = new BelleCrystal(name.c_str(), 5, pt);
633 // cout<<name<<" "<<shape->GetCubicVolume()*1e-3<<" "<<Volume<<endl;
634 // for(int i=0;i<100000;i++){
635 // G4ThreeVector a = shape->GetPointOnSurface();
636 // cout<<a.x()<<" "<<a.y()<<" "<<a.z()<<endl;
637 // }
638 // exit(0);
639 // if(prefix.find("crystal")!=string::npos){
640 // pentspeed(shape);
641 // exit(0);
642 // }
643 // if(prefix.find("wrap")!=string::npos){
644 // pentspeed2(shape);
645 // exit(0);
646 // }
647 return shape;
648 }
649 };
650
651 vector<shape_t*> load_shapes(const string& fname)
652 {
653 vector<shape_t*> shapes;
654 std::string fnamef = Belle2::FileSystem::findFile(fname);
655
656 ifstream IN(fnamef.c_str());
657 string tmp;
658 while (getline(IN, tmp)) {
659 size_t ic = tmp.find("#");
660 if (ic != string::npos) tmp.erase(ic);
661 istringstream iss(tmp);
662 vector<string> t;
663 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(t));
664 if (t.size() > 0) {
665 shape_t* shape = nullptr;
666 if (t.size() == 21) {
667 shape = new quadrilateral_endcap_t();
668 quadrilateral_endcap_t& trap = static_cast<quadrilateral_endcap_t&>(*shape);
669
670 istringstream in(t[0]);
671 in >> trap.nshape;
672
673 for (size_t i = 1; i < t.size(); i++) {
674 in.str(t[i]); in.seekg(0, ios_base::beg);
675 in >> trap.t[i - 1];
676 }
677 } else if (t.size() == 13) {
678 shape = new quadrilateral_barrel_t();
679 quadrilateral_barrel_t& trap = static_cast<quadrilateral_barrel_t&>(*shape);
680
681 istringstream in(t[0]);
682 in >> trap.nshape;
683
684 for (size_t i = 1; i < t.size(); i++) {
685 in.str(t[i]); in.seekg(0, ios_base::beg);
686 in >> trap.t[i - 1];
687 }
688 } else if (t.size() == 22) {
689 shape = new pent_t();
690 pent_t& pent = static_cast<pent_t&>(*shape);
691
692 istringstream in(t[0]);
693 in >> pent.nshape;
694
695 for (size_t i = 1; i < t.size(); i++) {
696 in.str(t[i]); in.seekg(0, ios_base::beg);
697 in >> pent.t[i - 1];
698 }
699 pent.adjust();
700 }
701 shapes.push_back(shape);
702 }
703 }
704
705 return shapes;
706
707 // for(unsigned int i=0;i<shapes.size();i++){
708 // shape_t &t = *shapes[i];
709 // crystals[t.nshape] = t.get_solid(prefix, 0.250-0.006);
710 // }
711
712 // return crystals;
713 }
714
715 vector<cplacement_t> load_placements(const string& fname)
716 {
717 vector<cplacement_t> plcmnt;
718 std::string fnamef = Belle2::FileSystem::findFile(fname);
719
720 ifstream IN(fnamef.c_str());
721 string tmp;
722 while (getline(IN, tmp)) {
723 size_t ic = tmp.find("#");
724 if (ic != string::npos) tmp.erase(ic);
725 istringstream iss(tmp);
726 vector<string> t;
727 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(t));
728 if (t.size() == 7) {
729 cplacement_t p;
730 istringstream in(t[0]);
731 in >> p.nshape;
732 in.str(t[1]); in.seekg(0, ios_base::beg);
733 in >> p.Rphi1;
734 in.str(t[2]); in.seekg(0, ios_base::beg);
735 in >> p.Rtheta;
736 in.str(t[3]); in.seekg(0, ios_base::beg);
737 in >> p.Rphi2;
738 in.str(t[4]); in.seekg(0, ios_base::beg);
739 in >> p.Pr;
740 in.str(t[5]); in.seekg(0, ios_base::beg);
741 in >> p.Ptheta;
742 in.str(t[6]); in.seekg(0, ios_base::beg);
743 in >> p.Pphi;
744 plcmnt.push_back(p);
745 }
746 }
747
748 return plcmnt;
749 }
750
751 G4Transform3D get_transform(const cplacement_t& t)
752 {
753 G4Transform3D r = G4Rotate3D(t.Rphi1, G4Vector3D(sin(t.Rtheta) * cos(t.Rphi2), sin(t.Rtheta) * sin(t.Rphi2), cos(t.Rtheta)));
754 G4Transform3D p = G4Translate3D(t.Pr * sin(t.Ptheta) * cos(t.Pphi), t.Pr * sin(t.Ptheta) * sin(t.Pphi), t.Pr * cos(t.Ptheta));
755 return p * r;
756 }
757
758 Belle2::ECLCrystalsShapeAndPosition loadCrystalsShapeAndPosition()
759 {
760 stringstream buffer;
761 auto fillbuffer = [&buffer](const string & fname) {
762 string path = Belle2::FileSystem::findFile(fname);
763 ifstream IN(path.c_str());
764 buffer.clear(); buffer.str("");
765 buffer << IN.rdbuf();
766 };
767
768 Belle2::ECLCrystalsShapeAndPosition a;
769 fillbuffer("/ecl/data/crystal_shape_forward.dat"); a.setShapeForward(buffer.str());
770 fillbuffer("/ecl/data/crystal_shape_barrel.dat"); a.setShapeBarrel(buffer.str());
771 fillbuffer("/ecl/data/crystal_shape_backward.dat"); a.setShapeBackward(buffer.str());
772 fillbuffer("/ecl/data/crystal_placement_forward.dat"); a.setPlacementForward(buffer.str());
773 fillbuffer("/ecl/data/crystal_placement_barrel.dat"); a.setPlacementBarrel(buffer.str());
774 fillbuffer("/ecl/data/crystal_placement_backward.dat"); a.setPlacementBackward(buffer.str());
775 return a;
776 // Belle2::IntervalOfValidity iov(0, 0, -1, -1); // IOV (0,0,-1,-1) is valid for all runs and experiments
777 // Belle2::Database::Instance().storeData<Belle2::ECLCrystalsShapeAndPosition>(&a, iov);
778 }
779
780 vector<shape_t*> load_shapes(stringstream& IN)
781 {
782 vector<shape_t*> shapes;
783 string tmp;
784 while (getline(IN, tmp)) {
785 size_t ic = tmp.find("#");
786 if (ic != string::npos) tmp.erase(ic);
787 istringstream iss(tmp);
788 vector<string> t;
789 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(t));
790 if (t.size() > 0) {
791 shape_t* shape = nullptr;
792 if (t.size() == 21) {
793 shape = new quadrilateral_endcap_t();
794 quadrilateral_endcap_t& trap = static_cast<quadrilateral_endcap_t&>(*shape);
795
796 istringstream in(t[0]);
797 in >> trap.nshape;
798
799 for (size_t i = 1; i < t.size(); i++) {
800 in.str(t[i]); in.seekg(0, ios_base::beg);
801 in >> trap.t[i - 1];
802 }
803 } else if (t.size() == 13) {
804 shape = new quadrilateral_barrel_t();
805 quadrilateral_barrel_t& trap = static_cast<quadrilateral_barrel_t&>(*shape);
806
807 istringstream in(t[0]);
808 in >> trap.nshape;
809
810 for (size_t i = 1; i < t.size(); i++) {
811 in.str(t[i]); in.seekg(0, ios_base::beg);
812 in >> trap.t[i - 1];
813 }
814 } else if (t.size() == 22) {
815 shape = new pent_t();
816 pent_t& pent = static_cast<pent_t&>(*shape);
817
818 istringstream in(t[0]);
819 in >> pent.nshape;
820
821 for (size_t i = 1; i < t.size(); i++) {
822 in.str(t[i]); in.seekg(0, ios_base::beg);
823 in >> pent.t[i - 1];
824 }
825 pent.adjust();
826 }
827 shapes.push_back(shape);
828 }
829 }
830
831 return shapes;
832 }
833
834 vector<cplacement_t> load_placements(stringstream& IN)
835 {
836 vector<cplacement_t> plcmnt;
837 string tmp;
838 while (getline(IN, tmp)) {
839 size_t ic = tmp.find("#");
840 if (ic != string::npos) tmp.erase(ic);
841 istringstream iss(tmp);
842 vector<string> t;
843 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(t));
844 if (t.size() == 7) {
845 cplacement_t p;
846 istringstream in(t[0]);
847 in >> p.nshape;
848 in.str(t[1]); in.seekg(0, ios_base::beg);
849 in >> p.Rphi1;
850 in.str(t[2]); in.seekg(0, ios_base::beg);
851 in >> p.Rtheta;
852 in.str(t[3]); in.seekg(0, ios_base::beg);
853 in >> p.Rphi2;
854 in.str(t[4]); in.seekg(0, ios_base::beg);
855 in >> p.Pr;
856 in.str(t[5]); in.seekg(0, ios_base::beg);
857 in >> p.Ptheta;
858 in.str(t[6]); in.seekg(0, ios_base::beg);
859 in >> p.Pphi;
860 plcmnt.push_back(p);
861 }
862 }
863
864 return plcmnt;
865 }
866
867 vector<cplacement_t> load_placements(const Belle2::ECLCrystalsShapeAndPosition* crystals, enum ECLParts part)
868 {
869 // Belle2::DBObjPtr<Belle2::ECLCrystalsShapeAndPosition> crystals;
870 // if (!crystals.isValid()) B2FATAL("No crystal's data in the database.");
871
872 // stringstream buffer;
873 // auto fillbuffer = [&buffer](const string &fname) {
874 // string path = Belle2::FileSystem::findFile(fname);
875 // ifstream IN(path.c_str());
876 // buffer.clear(); buffer.str("");
877 // buffer << IN.rdbuf();
878 // };
879
880 // Belle2::ECLCrystalsShapeAndPosition *crystals = new Belle2::ECLCrystalsShapeAndPosition();
881 // fillbuffer("/ecl/data/crystal_shape_forward.dat"); crystals->setShapeForward(buffer.str());
882 // fillbuffer("/ecl/data/crystal_shape_barrel.dat"); crystals->setShapeBarrel(buffer.str());
883 // fillbuffer("/ecl/data/crystal_shape_backward.dat"); crystals->setShapeBackward(buffer.str());
884 // fillbuffer("/ecl/data/crystal_placement_forward.dat"); crystals->setPlacementForward(buffer.str());
885 // fillbuffer("/ecl/data/crystal_placement_barrel.dat"); crystals->setPlacementBarrel(buffer.str());
886 // fillbuffer("/ecl/data/crystal_placement_backward.dat"); crystals->setPlacementBackward(buffer.str());
887
888 stringstream IN;
889 if (part == ECLParts::forward)
890 IN.str(crystals->getPlacementForward());
891 else if (part == ECLParts::barrel)
892 IN.str(crystals->getPlacementBarrel());
893 else if (part == ECLParts::backward)
894 IN.str(crystals->getPlacementBackward());
895 return load_placements(IN);
896 }
897
898 vector<shape_t*> load_shapes(const Belle2::ECLCrystalsShapeAndPosition* crystals, enum ECLParts part)
899 {
900 stringstream IN;
901 if (part == ECLParts::forward)
902 IN.str(crystals->getShapeForward());
903 else if (part == ECLParts::barrel)
904 IN.str(crystals->getShapeBarrel());
905 else if (part == ECLParts::backward)
906 IN.str(crystals->getShapeBackward());
907 return load_shapes(IN);
908 }
909
910 void testtest()
911 {
912 Belle2::ECLCrystalsShapeAndPosition crystals = loadCrystalsShapeAndPosition();
913 vector<cplacement_t> bp = load_placements(&crystals, ECLParts::forward);
914 for (vector<cplacement_t>::const_iterator it = bp.begin(); it != bp.end(); ++it) {
915 const cplacement_t& t = *it;
916 cout << t.nshape << " " << t.Rphi1 << " " << t.Rtheta << " " << t.Rphi2 << " " << t.Pr << " " << t.Ptheta << " " << t.Pphi << endl;
917 }
918 exit(0);
919 }
920 }
922}
const std::string & getPlacementBarrel() const
Return crystal placement in barrel.
const std::string & getShapeBackward() const
Return crystal shape in backward endcap.
const std::string & getShapeForward() const
Return crystal shape in forward endcap.
const std::string & getShapeBarrel() const
Return crystal shape in barrel.
const std::string & getPlacementBackward() const
Return crystal placement in backward endcap.
const std::string & getPlacementForward() const
Return crystal placement in forward endcap.
a Belle crystal in Geant4
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
double tan(double a)
tan for double
Definition beamHelpers.h:31
double atan(double a)
atan for double
Definition beamHelpers.h:34
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.
STL namespace.
struct for Point
placement struct
Definition shapes.h:49
pentagon shape
Definition shapes.cc:421
G4VSolid * get_tesselatedsolid(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get tessellated solid
Definition shapes.cc:485
bool istrap() const override
is trapped?
Definition shapes.cc:445
G4VSolid * get_extrudedsolid(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get extruded solid
Definition shapes.cc:517
G4VSolid * get_bellecrystal(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get Belle crystal
Definition shapes.cc:613
void adjust()
adjust sizes to have flat sides
Definition shapes.cc:433
map< int, G4ThreeVector > make_verticies(double wrapthick) const
create map of vertices
Definition shapes.cc:448
G4VSolid * get_trapezoid(const string &prefix, double wrapthick, G4Translate3D &shift) const override
get trapezoid
Definition shapes.cc:558
bool _adjusted
are sizes adjusted?
Definition shapes.cc:428
quadrilateral struct for barrel
Definition shapes.cc:292
bool istrap() const override
is trapped
Definition shapes.cc:303
map< int, G4ThreeVector > make_verticies(double wrapthick) const override
create map of vertices
Definition shapes.cc:309
quadrilateral struct for end cap
Definition shapes.cc:354
bool istrap() const override
is trapped?
Definition shapes.cc:365
map< int, G4ThreeVector > make_verticies(double wrapthick) const override
create map of vertices
Definition shapes.cc:372
G4VSolid * get_tesselatedsolid(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get tessellated solid
Definition shapes.cc:126
virtual map< int, G4ThreeVector > make_verticies(double wrapthick) const =0
create map of vertices
G4VSolid * get_extrudedsolid(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get extruded solid
Definition shapes.cc:151
G4VSolid * get_bellecrystal(const string &prefix, double wrapthick, G4Translate3D &shift UNUSED) const override
get Belle crystal
Definition shapes.cc:262
G4VSolid * get_trapezoid(const string &prefix, double wrapthick, G4Translate3D &shift) const override
get trapezoid
Definition shapes.cc:188
int nshape
shapes
Definition shapes.h:30
G4VSolid * get_solid(const std::string &prefix, double wrapthick, G4Translate3D &shift) const
get solid
Definition shapes.cc:78
virtual G4VSolid * get_bellecrystal(const std::string &prefix, double wrapthick, G4Translate3D &shift) const =0
get Belle crystal