EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoordSystem.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CoordSystem.cxx
1 /* -------------------------------------------------------------------------- */
2 /* CoordSystem.cc */
3 /* */
4 /* Coordinate system handling service routines. */
5 /* */
6 /* A.Kisselev, PNPI, St.Petersburg, Russia. */
7 /* e-mail: kisselev@hermes.desy.de */
8 /* -------------------------------------------------------------------------- */
9 
10 #include <cassert>
11 #include <cstdlib>
12 
13 #include <CoordSystem.h>
14 
15 /* ========================================================================== */
16 /* Well, it looks reasonable to establish a standalone routine which shapes */
17 /* up the coordinate system frame; */
18 
19 CoordSystem::CoordSystem(unsigned char _system_type, int _coord_num, char
20  _coord_names[])
21 {
22  // Only 2 types are known for now;
23  assert(_system_type == _CARTESIAN_ || _system_type == _CYLINDRICAL_);
24  system_type = _system_type;
25 
26  // Well, do not want to bother with >3 case;
27  assert(_coord_num > 0 && _coord_num <= 3);// return NULL;
28  coord_num = _coord_num;
29 
30  if (!_coord_names)
31  {
32  assert(coord_num == 3);
33  }
34  else
35  {
36  // Set fake[] flags per default;
37  for(int ip=0; ip<3; ip++)
38  fake[ip] = 1;
39 
40  // Check that there is no overlapping;
41  for(int ip=0; ip<coord_num; ip++)
42  for(int iq=0; iq<ip; iq++)
43  assert(_coord_names[ip] != _coord_names[iq]);
44 
45  // Use _coord_names[] array and find proper element;
46  for(int ip=0; ip<coord_num; ip++)
47  {
48  t_coord_name *cname = find_coord_by_name(system_type, _coord_names[ip]);
49  // Coordinate name is unknown;
50  assert(cname);
51 
52  // True coordinate --> reset 'fake' flag;
53  fake[cname->id] = 0;
54  } /*for*/
55  } /*if*/
56 } /* CoordSystem::CoordSystem */
57 
58 /* ========================================================================== */
59 /* This procedure assumes that compressed coordinates are in a right order, */
60 /* but few of them may be missing (say 2 coordinates can NOT be 'ZR', while */
61 /* 'RZ' is possible); */
62 
64 {
65  int idx = 0;
66 
67  // If all 3 coordinates are used, no expansion needed; but reset pointer to NULL;
68  if (coord_num == 3)
69  {
70  *expansion = 0;
71  return 0;
72  } /*if*/
73 
74  int *ptr = *expansion = (int*)malloc(3*sizeof(int));
75  if (!ptr) return -1;
76 
77  for(int ip=0; ip<3; ip++)
78  if (fake[ip])
79  ptr[ip] = -1;
80  else
81  ptr[ip] = idx++;
82 
83  return 0;
84 } /* CoordSystem::calculateExpansionRules */
85 
86 /* ========================================================================== */
87 
88 //
89 // -> Has not been checked?;
90 //
91 
92 void CoordSystem::projectToLocalCoordinates(double in[], double out[])
93 {
94  int idx= 0;
95  double buffer[3], *ptr;
96 
97  // Save all 3 components if in[] and out[] is the same pointer;
98  // again, no overlap check :-);
99  if (in == out)
100  {
101  for(int ik=0; ik<3; ik++)
102  buffer[ik] = in[ik];
103 
104  ptr = buffer;
105  }
106  else
107  ptr = in;
108 
109  // Calculate all projected components;
110  for(int ik=0; ik<3; ik++)
111  {
112  if (fake[ik]) continue;
113 
114  out[idx++] = ptr[ik];
115  } /*for*/
116 } /* CoordSystem::projectToLocalCoordinates */
117 
118 /* ========================================================================== */
119 
120 void expand_to_global_coordinates(int ldim, double in[], double out[], int l2g[])
121 {
122  double buffer[3], *ptr;
123 
124  // Save all local components if in[] and out[] are the same;
125  // well, no overlap check :-);
126  if (in == out)
127  {
128  for(int ik=0; ik<ldim; ik++)
129  buffer[ik] = in[ik];
130 
131  ptr = buffer;
132  }
133  else
134  ptr = in;
135 
136  // Calculate all 3 components;
137  for(int ik=0; ik<3; ik++)
138  out[ik] = (l2g[ik] == -1) ? 0. : ptr[l2g[ik]];
139 } /* expand_to_global_coordinates */
140 
141 /* ========================================================================== */