62#include "TRestPhysics.h"
134 Double_t theta = (TMath::Pi() / (
fNodesY - 1)) * nY;
135 Double_t phi = (2 * TMath::Pi() / (
fNodesY - 1)) * nZ - TMath::Pi();
148 return TVector3(x, y, z);
151 return TVector3(0, 0, 0);
161 if (IsNaN(x))
return 0;
164 if (relative) xInside = x;
167 cout <<
"REST WARNING (TRestMesh) : X node (" << x
168 <<
") outside boundaries. Setting it to : " <<
fNodesX - 1 << endl;
173 cout <<
"REST WARNING (TRestMesh) : X node (" << x <<
") outside boundaries. Setting it to : " << 0
194 if (relative) posInside = v;
197 cout <<
"REST WARNING (TRestMesh) : Relative position (" << posInside.X() <<
", " << posInside.Y()
198 <<
", " << posInside.Z() <<
")"
199 <<
") outside boundaries. Setting it to : " <<
fNodesX - 1 << endl;
215 if (IsNaN(y))
return 0;
218 if (relative) yInside = y;
221 cout <<
"REST WARNING (TRestMesh) : Y node (" << y
222 <<
") outside boundaries. Setting it to : " <<
fNodesY - 1 << endl;
227 cout <<
"REST WARNING (TRestMesh) : Y node (" << y <<
") outside boundaries. Setting it to : " << 0
250 if (relative) posInside = v;
253 cout <<
"REST WARNING (TRestMesh) : Relative position (" << posInside.X() <<
", " << posInside.Y()
254 <<
", " << posInside.Z() <<
") outside boundaries. Setting it to : " <<
fNodesY - 1 << endl;
259 return (Int_t)(posInside.Theta() * (
fNodesY - 1) / TMath::Pi());
269 if (IsNaN(z))
return 0;
272 if (relative) zInside = z;
303 if (relative) posInside = v;
306 cout <<
"REST WARNING (TRestMesh) : Relative position (" << posInside.X() <<
", " << posInside.Y()
307 <<
", " << posInside.Z() <<
") outside boundaries. Setting it to : " <<
fNodesZ - 1 << endl;
312 return (Int_t)((posInside.Phi() + TMath::Pi()) * (
fNodesZ - 1) / 2. / TMath::Pi());
319 double nan = numeric_limits<double>::quiet_NaN();
320 for (
unsigned int hit = 0; hit < hits->GetNumberOfHits(); hit++) {
321 if (hits->GetEnergy(hit) <= 0)
continue;
322 REST_HitType type = hits->GetType(hit);
323 this->
AddNode(type == YZ ? nan : hits->GetX(hit), type == XZ ? nan : hits->GetY(hit), hits->GetZ(hit),
324 hits->GetEnergy(hit));
337 cout <<
"TRestMesh::SetNodesFromSphericalHits is only to be used with a spherical mesh!" << endl;
339 for (
unsigned int hit = 0; hit < hits->GetNumberOfHits(); hit++) {
340 if (hits->GetEnergy(hit) <= 0)
continue;
341 this->
AddSphericalNode(hits->GetX(hit), hits->GetY(hit), hits->GetZ(hit), hits->GetEnergy(hit));
353 Bool_t change =
true;
362 if (nbIndex != GROUP_NOT_FOUND) {
370 vector<Int_t> groups;
372 Bool_t groupFound =
false;
373 for (
unsigned int i = 0; i < groups.size(); i++)
374 if (
GetGroupId(n) == groups[i]) groupFound =
true;
376 if (!groupFound) groups.push_back(
GetGroupId(n));
382 if (i != groups[i]) {
420 return GROUP_NOT_FOUND;
427 if (index > (
int)
fNodeGroupID.size())
return GROUP_NOT_FOUND;
449 double nan = numeric_limits<double>::quiet_NaN();
451 if (index > (
int)
fNodeGroupID.size())
return TVector3(nan, nan, nan);
467 Int_t index = NODE_NOT_SET;
469 for (
int i = nx - 1; i <= nx + 1; i++)
470 for (
int j = ny - 1; j <= ny + 1; j++)
471 for (
int k = nz - 1; k <= nz + 1; k++) {
472 if (i != nx || j != ny || k != nz) {
477 return GROUP_NOT_FOUND;
488 if (index == NODE_NOT_SET)
return GROUP_NOT_FOUND;
492 for (
int i = nx - 1; i <= nx + 1; i++)
493 for (
int j = ny - 1; j <= ny + 1; j++)
494 for (
int k = nz - 1; k <= nz + 1; k++) {
495 if (i != nx || j != ny || k != nz) {
497 if (index != NODE_NOT_SET &&
fNodeGroupID[index] != nodeGroup)
return index;
501 return GROUP_NOT_FOUND;
566 if (index == NODE_NOT_SET) {
568 if (gId == GROUP_NOT_FOUND) {
599 if (index == NODE_NOT_SET) {
601 if (gId == GROUP_NOT_FOUND) {
651 Double_t radius2 = relPos.X() * relPos.X() + relPos.Y() * relPos.Y();
653 if (radius2 > R2)
return false;
725 std::vector<TVector3> boundaries;
731 TVector3 planePosition_BottomZ = TVector3(0, 0, -
GetNetSizeZ() / 2.) + netCenter;
733 if (pos != posAtPlane && posAtPlane.X() > xL && posAtPlane.X() < xH && posAtPlane.Y() > yL &&
734 posAtPlane.Y() < yH) {
735 boundaries.push_back(posAtPlane);
738 TVector3 planePosition_TopZ = TVector3(0, 0,
GetNetSizeZ() / 2.) + netCenter;
740 if (pos != posAtPlane && posAtPlane.X() > xL && posAtPlane.X() < xH && posAtPlane.Y() > yL &&
741 posAtPlane.Y() < yH) {
742 boundaries.push_back(posAtPlane);
745 TVector3 planePosition_BottomY = TVector3(0, -
GetNetSizeY() / 2., 0) + netCenter;
747 if (pos != posAtPlane && posAtPlane.X() > xL && posAtPlane.X() < xH && posAtPlane.Z() > zL &&
748 posAtPlane.Z() < zH) {
749 boundaries.push_back(posAtPlane);
752 TVector3 planePosition_TopY = TVector3(0,
GetNetSizeY() / 2., 0) + netCenter;
754 if (pos != posAtPlane && posAtPlane.X() > xL && posAtPlane.X() < xH && posAtPlane.Z() > zL &&
755 posAtPlane.Z() < zH) {
756 boundaries.push_back(posAtPlane);
759 TVector3 planePosition_BottomX = TVector3(-
GetNetSizeX() / 2., 0, 0) + netCenter;
761 if (pos != posAtPlane && posAtPlane.Y() > yL && posAtPlane.Y() < yH && posAtPlane.Z() > zL &&
762 posAtPlane.Z() < zH) {
763 boundaries.push_back(posAtPlane);
766 TVector3 planePosition_TopX = TVector3(
GetNetSizeX() / 2., 0, 0) + netCenter;
768 if (pos != posAtPlane && posAtPlane.Y() > yL && posAtPlane.Y() < yH && posAtPlane.Z() > zL &&
769 posAtPlane.Z() < zH) {
770 boundaries.push_back(posAtPlane);
773 if (boundaries.size() == 2) {
776 Double_t d1 = (boundaries[0] - pos) * dir;
777 Double_t d2 = (boundaries[1] - pos) * dir;
780 if (d1 < 0 || d2 < 0) boundaries.clear();
784 if (d1 > d2) iter_swap(boundaries.begin(), boundaries.begin() + 1);
797 std::vector<TVector3> boundaries;
803 TVector3 pos2D = TVector3(pos.X() - netCenter.X(), pos.Y() - netCenter.Y(), 0);
804 TVector3 dir2D = TVector3(dir.X(), dir.Y(), 0);
806 Double_t product = pos2D * dir2D;
807 Double_t product2 = product * product;
809 Double_t dirMag2 = dir2D.Mag2();
810 Double_t posMag2 = pos2D.Mag2();
811 Double_t root = product2 - dirMag2 * (posMag2 - R2);
816 Double_t t1 = (-product - TMath::Sqrt(root)) / dirMag2;
817 Double_t t2 = (-product + TMath::Sqrt(root)) / dirMag2;
819 TVector3 firstVertex = pos + t1 * dir;
820 TVector3 secondVertex = pos + t2 * dir;
823 boundaries.push_back(firstVertex);
825 boundaries.push_back(secondVertex);
827 if (boundaries.size() == 2) {
829 if (t1 > 0 && t2 > t1) {
842 TVector3 planePosition_BottomZ = TVector3(0, 0, -
GetNetSizeZ() / 2.) + netCenter;
844 TVector3 relPosBottom = posAtPlane - netCenter;
845 relPosBottom.SetZ(0);
847 if (relPosBottom.Mag2() <
fNetSizeX *
fNetSizeX / 4.) boundaries.push_back(posAtPlane);
849 TVector3 planePosition_TopZ = TVector3(0, 0,
GetNetSizeZ() / 2.) + netCenter;
851 TVector3 relPosTop = posAtPlane - netCenter;
856 if (boundaries.size() == 2 && particle) {
858 Double_t d1 = (boundaries[0] - pos) * dir;
859 Double_t d2 = (boundaries[1] - pos) * dir;
862 if (d1 < 0 || d2 < 0) boundaries.clear();
866 if (d1 > d2) iter_swap(boundaries.begin(), boundaries.begin() + 1);
878 std::cout <<
"---------------------------------------------" << endl;
881 <<
" Z : " <<
fNodeZ[i] << endl;
882 std::cout <<
"---------------------------------------------" << endl;
It saves a 3-coordinate position and an energy for each punctual deposition.
A basic class inheriting from TObject to help creating a node grid definition.
void SetSize(Double_t sX, Double_t sY, Double_t sZ)
Sets the origin of the bounding-box and initializes the nodes vector to zero.
TVector3 GetVertex(Int_t id) const
It returns the position of both boundary vertex, bottom vertex identified with id = 0 and top vertex ...
std::vector< TVector3 > GetTrackBoundaries(TVector3 pos, TVector3 dir, Bool_t particle=true)
Finds the intersection of the straight line (track defined by the input arguments given,...
void RemoveNodes()
It initializes all node vectors to zero.
Bool_t IsInside(TVector3 pos)
It returns true if the position is found inside the grid (box,sphere or cylinder).
Int_t FindNeighbourGroup(Int_t nx, Int_t ny, Int_t nz)
Returns the group id of the first node identified in the neighbour cell from cell=(nx,...
Int_t GetNodeY(Double_t y, Bool_t relative=false)
Gets the node index corresponding to the y-coordinate.
TRestMesh()
Default constructor.
std::vector< Int_t > fNodeY
A std::vector storing the Y-dimension cell id.
Int_t GetNodeZ(Double_t z, Bool_t relative=false)
Gets the node index corresponding to the z-coordinate.
void SetNodesFromSphericalHits(TRestHits *hits)
It initializes the nodes using the hit coordinates found inside a TRestHits structure....
Double_t fNetSizeZ
The size of the grid in Z dimension.
Double_t GetY(Int_t nY)
Gets the cartesian position of nodeY.
void SetOrigin(Double_t oX, Double_t oY, Double_t oZ)
Sets the origin of the bounding-box and initializes the nodes vector to zero.
void Regrouping()
Needs TO BE documented.
void AddSphericalNode(Double_t x, Double_t y, Double_t z, Double_t en=0)
If adds corresponding node to xyz-coordinates if not previously defined.
Int_t fNodesX
Number of nodes in X-dimension (R in spherical coordinates)
Int_t GetNodeIndex(Int_t nx, Int_t ny, Int_t nz)
Returns the vector position for a given node index. If the node is not found, -1 will be returned.
std::vector< Int_t > fNodeZ
A std::vector storing the Z-dimension cell id.
std::vector< Int_t > fNodeX
A std::vector storing the X-dimension cell id.
Int_t fNumberOfGroups
Total number of groups found.
std::vector< TVector3 > GetTrackBoundariesCylinder(TVector3 pos, TVector3 dir, Bool_t particle=true)
Needs TO BE documented.
void AddNode(Double_t x, Double_t y, Double_t z, Double_t en=0)
If adds corresponding node to xyz-coordinates if not previously defined.
Int_t fNumberOfNodes
Total number of nodes filled.
TVector3 GetGroupPosition(Int_t index)
It returns the average position for all nodes weighted with their corresponding energy.
Int_t GetNumberOfNodes() const
Returns the total number of nodes added.
Double_t fNetSizeX
The size of the grid in X dimension.
Int_t FindForeignNeighbour(Int_t nx, Int_t ny, Int_t nz)
It identifies a foreign neighbour. I.e. if the group id of the neighbour cell is different to the cel...
Bool_t IsSpherical()
Returns true if the coordinate system is set to spherical.
Bool_t fIsCylindrical
A flag to indentify if we use cylindrical coordinates.
Int_t fNodesZ
Number of nodes in Z-dimension (Phi in spherical coordinates)
Double_t GetNetSizeZ() const
Returns the net size on Z-dimension.
Bool_t IsInsideBoundingBox(TVector3 pos)
It returns true if the position is found inside the bounding box.
Int_t fNodesY
Number of nodes in Y-dimension (Theta in spherical coordinates)
Double_t GetNetSizeX() const
Returns the net size on X-dimension.
void SetNodesFromHits(TRestHits *hits)
It initializes the nodes using the hit coordinates found inside a TRestHits structure.
TVector3 GetNetCenter()
It returns the position of the mesh center.
void Print()
Prints the nodes information.
Double_t GetGroupEnergy(Int_t index)
It returns the total energy of all nodes corresponding to the group id given by argument.
Int_t GetNodeX(Double_t x, Bool_t relative=false)
Gets the node index corresponding to the x-coordinate.
Double_t GetZ(Int_t nZ)
Gets the cartesian position of nodeZ.
TVector3 GetNodeByIndex(Int_t index)
Returns a node by its position in the std::vector.
TVector3 fNetOrigin
The bottom-left corner of the bounding-box grid.
Int_t GetGroupId(Double_t x, Double_t y, Double_t z)
Returns the group id corresponding to the x,y,z coordinate. If the coordinate falls at a non-initiali...
Bool_t IsCylindrical()
Returns true if the coordinate system is set to cylindrical.
std::vector< Int_t > fNodeGroupID
A std::vector storing the group ID of the corresponding nodes activated.
Int_t GetNumberOfGroups() const
Returns the total number of groups identified.
Double_t fNetSizeY
The size of the grid in Y dimension.
void SetNodes(Int_t nX, Int_t nY, Int_t nZ)
Sets the number of nodes and initializes the nodes vector to zero.
Bool_t fIsSpherical
A flag to indentify if we use spherical coordinates.
Double_t GetX(Int_t nX)
Gets the cartesian position of nodeX.
std::vector< Double_t > fEnergy
A std::vector storing the total energy inside the cell id.
Double_t GetNetSizeY() const
Returns the net size on Y-dimension.
TVector3 GetPosition(Int_t nX, Int_t nY, Int_t nZ)
Gets the position of the corresponding node.
~TRestMesh()
Default destructor.
TVector3 MoveToPlane(const TVector3 &pos, const TVector3 &dir, const TVector3 &n, const TVector3 &a)
This method will translate the vector with direction dir starting at position pos to the plane define...