Skip to content

Commit 0cf8f3c

Browse files
committed
Merge branch 'master' of github.com:rest-for-physics/framework into alphaAna
2 parents 792df26 + 7a1761d commit 0cf8f3c

File tree

8 files changed

+306
-39
lines changed

8 files changed

+306
-39
lines changed

cmake/MacroRootDict.cmake

+7-1
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,10 @@ MACRO(COMPILELIB dependency)
519519
set(dirs_included ${dirs_to_include} PARENT_SCOPE)
520520
set(library_added ${libname})
521521
set(library_added ${library_added} PARENT_SCOPE)
522-
ENDMACRO()
522+
523+
# define REST_*Lib e.g. REST_DetectorLib using library name: RestDetector -> REST_DetectorLib
524+
string(REGEX REPLACE "^Rest(.+)$" "REST_\\1Lib" DEFINE_VARIABLE_NAME ${libname})
525+
message(STATUS "Adding compile definition: ${DEFINE_VARIABLE_NAME}")
526+
add_compile_definitions(${DEFINE_VARIABLE_NAME})
527+
528+
ENDMACRO()

cmake/Testing.cmake

-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ endmacro()
1919
macro(ADD_LIBRARY_TEST)
2020
if (TEST)
2121
message(STATUS "Adding tests at ${CMAKE_CURRENT_SOURCE_DIR}")
22-
set(TESTING_EXECUTABLE testRestGeant4)
2322

2423
get_filename_component(DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
25-
2624
string(SUBSTRING ${DIR_NAME} 0 1 FIRST_LETTER)
2725
string(TOUPPER ${FIRST_LETTER} FIRST_LETTER)
2826
string(REGEX REPLACE "^.(.*)" "${FIRST_LETTER}\\1" DIR_NAME_CAPITALIZED "${DIR_NAME}")

source/framework/tools/inc/TRestPhysics.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
#include <iostream>
2727

28+
#include "TMatrixD.h"
2829
#include "TString.h"
2930
#include "TVector3.h"
31+
#include "TVectorD.h"
3032

3133
/// This namespace serves to define physics constants and other basic physical operations
3234
namespace REST_Physics {
@@ -68,7 +70,15 @@ TVector3 MoveToPlane(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a);
6870
TVector3 MoveByDistance(TVector3 pos, TVector3 dir, Double_t d);
6971
TVector3 MoveByDistanceFast(TVector3 pos, TVector3 dir, Double_t d);
7072

71-
TVector3 GetPlaneVectorIntersection(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a);
73+
TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, TVector3 const& n,
74+
TVector3 const& a);
75+
76+
TMatrixD GetConeMatrix(const TVector3& d, const Double_t& cosTheta);
77+
78+
Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d,
79+
const TVector3& v, const Double_t& cosTheta);
80+
Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M,
81+
const TVector3& axis, const TVector3& v);
7282

7383
Double_t DistanceToAxis(const TVector3& axisPoint, const TVector3& axisVector, const TVector3& point);
7484

source/framework/tools/inc/TRestStringHelper.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ Float_t StringToFloat(std::string in);
3434
Double_t StringToDouble(std::string in);
3535
Int_t StringToInteger(std::string in);
3636
std::string IntegerToString(Int_t n);
37+
std::string DoubleToString(Double_t d);
3738
Bool_t StringToBool(std::string in);
3839
Long64_t StringToLong(std::string in);
3940
TVector3 StringTo3DVector(std::string in);
4041
TVector2 StringTo2DVector(std::string in);
4142
std::vector<std::string> Split(std::string in, std::string separator, bool allowBlankString = false,
4243
bool removeWhiteSpaces = false, int startPos = -1);
43-
std::vector<double> StringToElements(std::string in, std::string separator, bool allowBlankString = false,
44-
bool removeWhiteSpaces = false, int starPos = -1);
44+
std::vector<double> StringToElements(std::string in, std::string separator);
45+
std::vector<double> StringToElements(std::string in, std::string headChar, std::string separator,
46+
std::string tailChar);
4547
std::string RemoveWhiteSpaces(std::string in);
4648
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition = 0,
4749
Int_t N = 0);

source/framework/tools/inc/TRestTools.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ class TRestTools {
5454
Int_t skipLines = 0);
5555

5656
template <typename T>
57-
static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns);
57+
static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
58+
59+
static Bool_t IsBinaryFile(std::string fname);
60+
61+
static std::string GetFileNameExtension(std::string fullname);
62+
63+
static int GetBinaryFileColumns(std::string fname);
5864

5965
template <typename T>
60-
static T GetMaxValueFromTable(std::vector<std::vector<T>> data, Int_t column);
66+
static T GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
6167

6268
template <typename T>
63-
static T GetMinValueFromTable(std::vector<std::vector<T>> data, Int_t column);
69+
static T GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
6470

6571
template <typename T>
6672
static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);
@@ -71,6 +77,9 @@ class TRestTools {
7177
template <typename T>
7278
static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
7379

80+
template <typename T>
81+
static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
82+
7483
static Int_t isValidFile(const std::string& path);
7584
static bool fileExists(const std::string& filename);
7685
static bool isRootFile(const std::string& filename);

source/framework/tools/src/TRestPhysics.cxx

+95-1
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,104 @@ Double_t DistanceToAxis(const TVector3& axisPoint, const TVector3& axisVector, c
7878
/// and moving in direction `dir` and the plane defined by its normal vector `n` and the point `a`. This is
7979
/// equivalent to move/translate the position `pos` to the plane.
8080
///
81-
TVector3 GetPlaneVectorIntersection(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a) {
81+
TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& n,
82+
const TVector3& a) {
8283
return MoveToPlane(pos, dir, n, a);
8384
}
8485

86+
///////////////////////////////////////////////
87+
/// \brief It returns the cone matrix M = d^T x d - cosTheta^2 x I, extracted from the document
88+
/// by "David Eberly, Geometric Tools, Redmond WA 98052, Intersection of a Line and a Cone".
89+
///
90+
TMatrixD GetConeMatrix(const TVector3& d, const Double_t& cosTheta) {
91+
double cAxis[3];
92+
d.GetXYZ(cAxis);
93+
94+
TVectorD coneAxis(3, cAxis);
95+
96+
TMatrixD M(3, 3);
97+
M.Rank1Update(coneAxis, coneAxis);
98+
99+
double cT2 = cosTheta * cosTheta;
100+
TMatrixD gamma(3, 3);
101+
gamma.UnitMatrix();
102+
gamma *= cT2;
103+
104+
M -= gamma;
105+
return M;
106+
}
107+
108+
///////////////////////////////////////////////
109+
/// \brief This method will find the intersection of the trajectory defined by the vector starting at
110+
/// `pos` and moving in direction `dir` and the cone defined by its axis vector `d` and the vertex`v`.
111+
/// The cosine of the angle defining the cone should be also given inside the `cosTheta` argument.
112+
///
113+
/// This method will return `t`, which is the value the particle position, `pos`, needs to be displaced
114+
/// by the vector, `dir`, to get the particle at the surface of the cone. If the particle does not
115+
/// cross the cone, then the value returned will be zero (no particle displacement).
116+
//
117+
/// This method is based on the document by "David Eberly, Geometric Tools, Redmond WA 98052,
118+
/// Intersection of a Line and a Cone".
119+
///
120+
Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d,
121+
const TVector3& v, const Double_t& cosTheta) {
122+
TMatrixD M = GetConeMatrix(d, cosTheta);
123+
return GetConeVectorIntersection(pos, dir, M, d, v);
124+
}
125+
126+
///////////////////////////////////////////////
127+
/// \brief This method will find the intersection of the trajectory defined by the vector starting at `pos`
128+
/// and moving in direction `dir` and the cone defined by its characteristic matrix `M`, which is built
129+
/// using the cone axis vector `d` as `d^T x d`, and the vertex`v`. The resulting TVector3 will be the
130+
/// position of the particle placed at the cone surface.
131+
///
132+
/// This method will return `t`, which is the value the particle position, `pos`, needs to be displaced
133+
/// by the vector, `dir`, to get the particle at the surface of the cone. If the particle does not
134+
/// cross the cone, then the value returned will be zero (no particle displacement).
135+
///
136+
/// This method is based on the document by "David Eberly, Geometric Tools, Redmond WA 98052,
137+
/// Intersection of a Line and a Cone".
138+
///
139+
Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M,
140+
const TVector3& axis, const TVector3& v) {
141+
double u[3];
142+
dir.GetXYZ(u);
143+
TMatrixD U(3, 1, u);
144+
TMatrixD Ut(1, 3, u);
145+
146+
double delta[3];
147+
TVector3 deltaV = pos - v;
148+
deltaV.GetXYZ(delta);
149+
TMatrixD D(3, 1, delta);
150+
TMatrixD Dt(1, 3, delta);
151+
152+
TMatrixD C2 = Ut * M * U;
153+
Double_t c2 = C2[0][0];
154+
155+
TMatrixD C1 = Ut * M * D;
156+
Double_t c1 = C1[0][0];
157+
158+
TMatrixD C0 = Dt * M * D;
159+
Double_t c0 = C0[0][0];
160+
161+
Double_t root = c1 * c1 - c0 * c2;
162+
if (root < 0) return 0;
163+
164+
Double_t t1 = (-c1 + TMath::Sqrt(root)) / c2;
165+
Double_t t2 = (-c1 - TMath::Sqrt(root)) / c2;
166+
167+
// The projections along the cone axis. If positive then the solution
168+
// gives the cone intersection with the side defined by `axis`
169+
Double_t h1 = t1 * dir.Dot(axis) + axis.Dot(deltaV);
170+
Double_t h2 = t2 * dir.Dot(axis) + axis.Dot(deltaV);
171+
172+
// We use it to select the root we are interested in
173+
if (h2 > 0)
174+
return t2;
175+
else
176+
return t1;
177+
}
178+
85179
///////////////////////////////////////////////
86180
/// \brief This method transports a position `pos` by a distance `d` in the direction defined by `dir`.
87181
///

source/framework/tools/src/TRestStringHelper.cxx

+33-6
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,10 @@ std::vector<string> REST_StringHelper::Split(std::string in, string separator, b
187187
/// \brief Convert the input string into a vector of double elements
188188
///
189189
/// e.g. Input: "1,2,3,4", Output: {1.,2.,3.,4.}
190-
std::vector<double> REST_StringHelper::StringToElements(std::string in, string separator,
191-
bool allowBlankString, bool removeWhiteSpaces,
192-
int startPos) {
193-
std:
190+
///
191+
std::vector<double> REST_StringHelper::StringToElements(std::string in, string separator) {
194192
vector<double> result;
195-
vector<string> vec_str =
196-
REST_StringHelper::Split(in, separator, allowBlankString, removeWhiteSpaces, startPos);
193+
vector<string> vec_str = REST_StringHelper::Split(in, separator);
197194
for (unsigned int i = 0; i < vec_str.size(); i++) {
198195
double temp = REST_StringHelper::StringToDouble(vec_str[i]);
199196
result.push_back(temp);
@@ -202,6 +199,31 @@ std::vector<double> REST_StringHelper::StringToElements(std::string in, string s
202199
return result;
203200
}
204201

202+
///////////////////////////////////////////////
203+
/// \brief Convert the input string `in` into a vector of double elements
204+
///
205+
/// Called as `StringToElements( in, "[", ",", "]" );` will get the
206+
/// elements from a string with the following format "[a,b,c]" where a,b,c
207+
/// are double numbers.
208+
///
209+
std::vector<double> REST_StringHelper::StringToElements(std::string in, string headChar, string separator,
210+
string tailChar) {
211+
std::vector<double> result;
212+
size_t startPos = in.find(headChar);
213+
size_t endPos = in.find(tailChar);
214+
if (startPos == string::npos || endPos == string::npos) {
215+
return result;
216+
}
217+
std::vector<string> values = Split(in.substr(startPos + 1, endPos - startPos - 1), ",");
218+
219+
for (unsigned int i = 0; i < values.size(); i++) {
220+
double temp = REST_StringHelper::StringToDouble(values[i]);
221+
result.push_back(temp);
222+
}
223+
224+
return result;
225+
}
226+
205227
///////////////////////////////////////////////
206228
/// \brief Returns the input string removing all white spaces.
207229
///
@@ -475,6 +497,11 @@ Int_t REST_StringHelper::StringToInteger(string in) {
475497
///
476498
string REST_StringHelper::IntegerToString(Int_t n) { return Form("%d", n); }
477499

500+
///////////////////////////////////////////////
501+
/// \brief Gets a string from a double
502+
///
503+
string REST_StringHelper::DoubleToString(Double_t d) { return Form("%4.2lf", d); }
504+
478505
Bool_t REST_StringHelper::StringToBool(std::string in) {
479506
return (ToUpper(in) == "TRUE" || ToUpper(in) == "ON");
480507
}

0 commit comments

Comments
 (0)