CellGeo.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 // \file CellGeo.cxx
3 // \brief Cell geometry construction and call routines
4 // \version $Id: CellGeo.cxx,v 1.7 2012-11-14 21:09:52 jcoelho Exp $
5 // \author $Author: jcoelho $
6 //////////////////////////////////////////////////////////////////////////
7 #include <iostream>
8 
9 #include "TGeoManager.h"
10 #include "TGeoBBox.h"
11 #include "TGeoMatrix.h"
12 #include "TGeoNode.h"
13 #include "TVector3.h"
14 
17 
18 namespace geo
19 {
20  std::vector<TGeoHMatrix> CellGeo::fgParentMatrices;
21 
22  /// Construct a cell geometry
23  CellGeo::CellGeo(const std::vector<const TGeoNode*>& n, int depth)
24  : fTranslatedMatrix(0)
25  {
26  fCellNode = n[depth];
27 
28  // The current split between parentMatrix and the final step is a
29  // compromise between speed and memory usage. This way, we can find our
30  // transformation matrix from the world frame with only one multiply, but
31  // we use something like 16x less storage than if we kept the final matrix
32  // for every single cell.
33 
34  TGeoHMatrix parentMatrix = *n.front()->GetMatrix();
35  // This loop is omitting the last entry in 'n'. That'll be available
36  // through fCellNode.
37  for(int i = 1; i < depth; ++i){
38  parentMatrix.Multiply(n[i]->GetMatrix());
39  }
40 
41  // We know that the geometry construction traverses depth-first, so if this
42  // parent matrix has seen before it will actually have been immediately
43  // prior.
44  if(fgParentMatrices.empty() ||
45  !(parentMatrix == fgParentMatrices.back())){
46  fgParentMatrices.push_back(parentMatrix);
47 
48  // Every module in the detector is made of two halves, so count
49  // 2*#modules to get the expected number of parent matrices.
50  if(fgParentMatrices.size() == 64*14*12*2+1){
51  std::cerr << "Warning: CellGeo::fgParentMatrices size exceeds "
52  << 64*14*12*2 << " expected." << std::endl;
53  }
54 
55  // If all is well, this is the last matrix, so don't waste any additional
56  // space in the vector.
57  if(fgParentMatrices.size() == 64*14*12*2){
58  fgParentMatrices.shrink_to_fit();
59  }
60  }
61  // Either it matches the previous matrix, or we just added it to the list,
62  // either way it's the last index.
64 
65  // Construct the unique id for this cell
66  fId = NodesToUniqueId(n, depth);
67  }
68 
69  //......................................................................
71  {
72  delete fTranslatedMatrix;
73  }
74 
75  //......................................................................
76 
77  /// Transform a position from local frame to world frame
78  /// \param local : 3D array. Position in the local frame Input.
79  /// \param world : 3D array. Position in the world frame. Returned.
80  void CellGeo::LocalToWorld(const double* local, double* world) const
81  {
82  GetGeoMatrix().LocalToMaster(local,world);
83  }
84 
85  //......................................................................
86 
87  /// Transform a 3-vector from local frame to world frame
88  /// \param local : 3D array. Position in the local frame Input.
89  /// \param world : 3D array. Position in the world frame. Returned.
90  void CellGeo::LocalToWorldVect(const double* local, double* world) const
91  {
92  GetGeoMatrix().LocalToMasterVect(local,world);
93  }
94 
95  //......................................................................
96 
97  /// Transform a position from world frame to local frame
98  /// \param world : 3D array. Position in the world frame. Input.
99  /// \param local : 3D array. Position in the local frame Returned.
100  void CellGeo::WorldToLocal(const double* local, double* world) const
101  {
102  GetGeoMatrix().MasterToLocal(local,world);
103  }
104 
105  //......................................................................
106 
107  /// Transform a 3-vector from world frame to local frame
108  /// \param world : 3D array. Position in the world frame. Input.
109  /// \param local : 3D array. Position in the local frame Returned.
110  void CellGeo::WorldToLocalVect(const double* local, double* world) const
111  {
112  GetGeoMatrix().MasterToLocalVect(local,world);
113  }
114 
115  //......................................................................
116 
117  void CellGeo::TranslateCell(double dx, double dy, double dz)
118  {
119 
120  /// Set rotation angles to 0 for now
121  double phi = 0;
122  double theta = 0;
123  double psi = 0;
124 
125  /// Build the translation and rotation matrices
126  /// phi, theta and psi are euler angles
127  TGeoRotation *rot = new TGeoRotation("alignmentRot",phi,theta,psi);
128  /// dx, dy and dz are the translations we want
129  TGeoTranslation *trans = new TGeoTranslation("",dx,dy,dz);
130  TGeoCombiTrans *comb = new TGeoCombiTrans(*trans,*rot);
131 
132  this->TranslateCell(comb);
133 
134  }
135 
136  //......................................................................
137 
138  void CellGeo::TranslateCell(TGeoCombiTrans* comb)
139  {
141 
142  fTranslatedMatrix = new TGeoHMatrix(GetGeoMatrix());
143  fTranslatedMatrix->MultiplyLeft(comb);
144  }
145 
146  //......................................................................
147 
149  {
150  delete fTranslatedMatrix;
151  }
152 
153  //......................................................................
154 
155  /// Return the center position of a cell.
156  /// \param xyz : 3-D array. The returned location.
157  /// \param localz : Distance along the length of the cell
158  /// (cm). Default is center of cell
159  void CellGeo::GetCenter(double* xyz, double localz) const
160  {
161  static double xyzLocal[3] = {0,0,0};
162  xyzLocal[2] = localz;
163  this->LocalToWorld(xyzLocal, xyz);
164  }
165 
166  /// Return the center position of a cell.
167  /// \param xyz :a Tvector3 object. The returned location.
168  /// \param localz : Distance along the length of the cell
169  /// (cm). Default is center of cell
170  void CellGeo::GetCenter(TVector3& xyz, double localz) const
171  {
172  double xyzArray[3];
173  this->GetCenter(xyzArray, localz);
174  xyz.SetXYZ(xyzArray[0], xyzArray[1], xyzArray[2]);
175  }
176 
177 
178  //......................................................................
179 
180  /// Return the distance from a point in the cell to the read out side
181  /// of the cell
182  /// \param localz : Z position in cm in local cell frame
183  /// \returns Distance in cm
184  double CellGeo::DistToReadOut(double localz) const
185  {
186  return this->HalfL()-localz;
187  }
188 
189  //......................................................................
190 
191  double CellGeo::HalfW() const
192  {
193  return ((TGeoBBox*)fCellNode->GetVolume()->GetShape())->GetDX();
194  }
195 
196  //......................................................................
197 
198  double CellGeo::HalfL() const
199  {
200  return ((TGeoBBox*)fCellNode->GetVolume()->GetShape())->GetDZ();
201  }
202 
203  //......................................................................
204 
205  double CellGeo::HalfD() const
206  {
207  return ((TGeoBBox*)fCellNode->GetVolume()->GetShape())->GetDY();
208  }
209 
210  //......................................................................
211  TGeoHMatrix CellGeo::GetGeoMatrix() const
212  {
214 
215  TGeoHMatrix ret = fgParentMatrices[fParentMatrixIdx];
216  ret.Multiply(fCellNode->GetMatrix());
217  return ret;
218  }
219 
220 } // end namespace geo
221 ////////////////////////////////////////////////////////////////////////
double HalfL() const
Definition: CellGeo.cxx:198
double HalfD() const
Definition: CellGeo.cxx:205
void GetCenter(double *xyz, double localz=0.0) const
Definition: CellGeo.cxx:159
void WorldToLocal(const double *local, double *world) const
Definition: CellGeo.cxx:100
void LocalToWorld(const double *local, double *world) const
Definition: CellGeo.cxx:80
const TGeoNode * fCellNode
Pointer to the cell node.
Definition: CellGeo.h:60
double HalfW() const
Definition: CellGeo.cxx:191
OStream cerr
Definition: OStream.cxx:7
void WorldToLocalVect(const double *local, double *world) const
Definition: CellGeo.cxx:110
CellUniqueId NodesToUniqueId(const std::vector< const TGeoNode * > &n, unsigned int depth)
CellGeo(const std::vector< const TGeoNode * > &n, int depth)
Construct a cell geometry.
Definition: CellGeo.cxx:23
Give every cell in the geometry a unique ID number based on the TGeo path to the node.
double DistToReadOut(double localz) const
Definition: CellGeo.cxx:184
void LocalToWorldVect(const double *local, double *world) const
Definition: CellGeo.cxx:90
double dy[NP][NC]
double dx[NP][NC]
static std::vector< TGeoHMatrix > fgParentMatrices
Collection of unique parent matrices seen.
Definition: CellGeo.h:70
double dz[NP][NC]
#define local
Definition: gzguts.h:107
void TranslateCell(double dx, double dy=0, double dz=0)
Definition: CellGeo.cxx:117
TGeoHMatrix GetGeoMatrix() const
Transformation matrix to world frame.
Definition: CellGeo.cxx:211
TGeoHMatrix * fTranslatedMatrix
Adjusted matrix, if TranslateCell() has been called.
Definition: CellGeo.h:65
int fParentMatrixIdx
Index into fgParentMatrices.
Definition: CellGeo.h:68
CellUniqueId fId
Node ID number.
Definition: CellGeo.h:59
Helper for AttenCurve.
Definition: Path.h:10
void RestoreOriginal()
Restore geometry to original.
Definition: CellGeo.cxx:148