From 5d857d52482a2b3b353bc0adbb3dafe1dc1aeb3e Mon Sep 17 00:00:00 2001 From: jgalan Date: Sat, 2 Apr 2022 16:25:46 +0200 Subject: [PATCH 01/18] TRestTools::ExportBinaryTable added --- source/framework/tools/inc/TRestTools.h | 3 +++ source/framework/tools/src/TRestTools.cxx | 27 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index c1065b17e..b0816de96 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -71,6 +71,9 @@ class TRestTools { template static int ExportASCIITable(std::string fname, std::vector>& data); + template + static int ExportBinaryTable(std::string fname, std::vector>& data); + static Int_t isValidFile(const string& path); static bool fileExists(const std::string& filename); static bool isRootFile(const std::string& filename); diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 270c002c3..56fba6cb9 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -158,6 +158,33 @@ template int TRestTools::ExportASCIITable(std::string fname, template int TRestTools::ExportASCIITable(std::string fname, std::vector>& data); +/////////////////////////////////////////////// +/// \brief Writes the contents of the vector table given as argument to `fname` as a binary file. +/// Allowed types are Int_t, Float_t and Double_t. +/// +template +int TRestTools::ExportBinaryTable(std::string fname, std::vector>& data) { + ofstream file(fname, ios::out | ios::binary); + if (!file.is_open()) { + ferr << "Unable to open file for writting : " << fname << endl; + return 1; + } + + for (int n = 0; n < data.size(); n++) + for (int m = 0; m < data[n].size(); m++) { + file.write((char*)&data[n][m], sizeof(T)); + } + file.close(); + + return 0; +} + +template int TRestTools::ExportBinaryTable(std::string fname, std::vector>& data); +template int TRestTools::ExportBinaryTable(std::string fname, + std::vector>& data); +template int TRestTools::ExportBinaryTable(std::string fname, + std::vector>& data); + /////////////////////////////////////////////// /// \brief Reads a binary file containning a fixed-columns table with values /// From 3973da6589bd9373962add66551e526f9ac931c8 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sun, 3 Apr 2022 14:04:04 +0200 Subject: [PATCH 02/18] TRestTools::GetMin,MaxValueFromTable now is not specific for each column --- source/framework/tools/inc/TRestTools.h | 4 +- source/framework/tools/src/TRestTools.cxx | 60 ++++++++++++++++------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index b0816de96..d721831cf 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -57,10 +57,10 @@ class TRestTools { static int ReadBinaryTable(string fName, std::vector>& data, Int_t columns); template - static T GetMaxValueFromTable(std::vector> data, Int_t column); + static T GetMaxValueFromTable(const std::vector>& data, Int_t column = -1); template - static T GetMinValueFromTable(std::vector> data, Int_t column); + static T GetMinValueFromTable(const std::vector>& data, Int_t column = -1); template static T GetLowestIncreaseFromTable(std::vector> data, Int_t column); diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 56fba6cb9..55b234e7d 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -239,48 +239,72 @@ template int TRestTools::ReadBinaryTable(string fName, std::vector -T TRestTools::GetMaxValueFromTable(std::vector> data, Int_t column) { - if (data.size() == 0 || data[0].size() <= column) return 0; - T maxValue = data[0][column]; - for (int n = 0; n < data.size(); n++) - if (maxValue < data[n][column]) maxValue = data[n][column]; +T TRestTools::GetMaxValueFromTable(const std::vector>& data, Int_t column) { + if (data.size() == 0) return 0; + if (column != -1 && data[0].size() <= column) return 0; + + T maxValue = data[0][0]; + if (column == -1) { + for (int n = 0; n < data.size(); n++) + for (int c = 0; c < data[n].size(); c++) + if (maxValue < data[n][c]) maxValue = data[n][c]; + } else { + maxValue = data[0][column]; + for (int n = 0; n < data.size(); n++) + if (maxValue < data[n][column]) maxValue = data[n][column]; + } + return maxValue; } -template Int_t TRestTools::GetMaxValueFromTable(std::vector> data, Int_t column); +template Int_t TRestTools::GetMaxValueFromTable(const std::vector>& data, + Int_t column); -template Float_t TRestTools::GetMaxValueFromTable(std::vector> data, +template Float_t TRestTools::GetMaxValueFromTable(const std::vector>& data, Int_t column); -template Double_t TRestTools::GetMaxValueFromTable(std::vector> data, +template Double_t TRestTools::GetMaxValueFromTable(const std::vector>& data, Int_t column); /////////////////////////////////////////////// /// \brief It returns the minimum value for a particular `column` from the table given by -/// argument. +/// argument. If no column is specified in the arguments, then it gets the minimum from the +/// full table. /// /// This method is available for tables of type Float_t, Double_t and Int_t. /// template -T TRestTools::GetMinValueFromTable(std::vector> data, Int_t column) { - if (data.size() == 0 || data[0].size() <= column) return 0; - T minValue = data[0][column]; - for (int n = 0; n < data.size(); n++) - if (minValue > data[n][column]) minValue = data[n][column]; +T TRestTools::GetMinValueFromTable(const std::vector>& data, Int_t column) { + if (data.size() == 0) return 0; + if (column != -1 && data[0].size() <= column) return 0; + + T minValue = data[0][0]; + if (column == -1) { + for (int n = 0; n < data.size(); n++) + for (int c = 0; c < data[n].size(); c++) + if (minValue > data[n][c]) minValue = data[n][c]; + } else { + minValue = data[0][column]; + for (int n = 0; n < data.size(); n++) + if (minValue > data[n][column]) minValue = data[n][column]; + } + return minValue; } -template Int_t TRestTools::GetMinValueFromTable(std::vector> data, Int_t column); +template Int_t TRestTools::GetMinValueFromTable(const std::vector>& data, + Int_t column); -template Float_t TRestTools::GetMinValueFromTable(std::vector> data, +template Float_t TRestTools::GetMinValueFromTable(const std::vector>& data, Int_t column); -template Double_t TRestTools::GetMinValueFromTable(std::vector> data, +template Double_t TRestTools::GetMinValueFromTable(const std::vector>& data, Int_t column); /////////////////////////////////////////////// From 682eb65bd0af30b41841dace2fbd6e97a4c01b16 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sun, 3 Apr 2022 14:08:09 +0200 Subject: [PATCH 03/18] TRestStringHelper::StringToElements added a second method prototype --- .../framework/tools/inc/TRestStringHelper.h | 4 +-- .../framework/tools/src/TRestStringHelper.cxx | 35 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/source/framework/tools/inc/TRestStringHelper.h b/source/framework/tools/inc/TRestStringHelper.h index b6054d90f..9bfbc5844 100644 --- a/source/framework/tools/inc/TRestStringHelper.h +++ b/source/framework/tools/inc/TRestStringHelper.h @@ -42,8 +42,8 @@ TVector3 StringTo3DVector(std::string in); TVector2 StringTo2DVector(std::string in); std::vector Split(std::string in, string separator, bool allowBlankString = false, bool removeWhiteSpaces = false, int startPos = -1); -std::vector StringToElements(std::string in, string separator, bool allowBlankString = false, - bool removeWhiteSpaces = false, int starPos = -1); +std::vector StringToElements(std::string in, string separator); +std::vector StringToElements(std::string in, string headChar, string separator, string tailChar); std::string RemoveWhiteSpaces(std::string in); std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition = 0, Int_t N = 0); diff --git a/source/framework/tools/src/TRestStringHelper.cxx b/source/framework/tools/src/TRestStringHelper.cxx index 73dbbb0d7..979b76b40 100644 --- a/source/framework/tools/src/TRestStringHelper.cxx +++ b/source/framework/tools/src/TRestStringHelper.cxx @@ -187,13 +187,10 @@ std::vector REST_StringHelper::Split(std::string in, string separator, b /// \brief Convert the input string into a vector of double elements /// /// e.g. Input: "1,2,3,4", Output: {1.,2.,3.,4.} -std::vector REST_StringHelper::StringToElements(std::string in, string separator, - bool allowBlankString, bool removeWhiteSpaces, - int startPos) { -std: +/// +std::vector REST_StringHelper::StringToElements(std::string in, string separator) { vector result; - vector vec_str = - REST_StringHelper::Split(in, separator, allowBlankString, removeWhiteSpaces, startPos); + vector vec_str = REST_StringHelper::Split(in, separator); for (unsigned int i = 0; i < vec_str.size(); i++) { double temp = REST_StringHelper::StringToDouble(vec_str[i]); result.push_back(temp); @@ -202,6 +199,32 @@ std::vector REST_StringHelper::StringToElements(std::string in, string s return result; } +/////////////////////////////////////////////// +/// \brief Convert the input string `in` into a vector of double elements +/// +/// Called as `StringToElements( in, "[", ",", "]" );` will get the +/// elements from a string with the following format "[a,b,c]" where a,b,c +/// are double numbers. +/// +std::vector REST_StringHelper::StringToElements(std::string in, string headChar, string separator, + string tailChar) { + std::vector result; + size_t startPos = in.find(headChar); + size_t endPos = in.find(tailChar); + if (startPos == string::npos || endPos == string::npos) { + ferr << "StringToElements wrong arguments!" << endl; + return result; + } + std::vector values = Split(in.substr(startPos + 1, endPos - startPos - 1), ","); + + for (unsigned int i = 0; i < values.size(); i++) { + double temp = REST_StringHelper::StringToDouble(values[i]); + result.push_back(temp); + } + + return result; +} + /////////////////////////////////////////////// /// \brief Returns the input string removing all white spaces. /// From 2a94d83e42218d6e8a8d179491ac6bbb6b16f2c1 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sun, 3 Apr 2022 15:28:13 +0200 Subject: [PATCH 04/18] TRestStringHelper::DoubleToString method added --- source/framework/tools/inc/TRestStringHelper.h | 1 + source/framework/tools/src/TRestStringHelper.cxx | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/source/framework/tools/inc/TRestStringHelper.h b/source/framework/tools/inc/TRestStringHelper.h index 9bfbc5844..dd6ba6f76 100644 --- a/source/framework/tools/inc/TRestStringHelper.h +++ b/source/framework/tools/inc/TRestStringHelper.h @@ -36,6 +36,7 @@ Float_t StringToFloat(std::string in); Double_t StringToDouble(std::string in); Int_t StringToInteger(std::string in); std::string IntegerToString(Int_t n); +std::string DoubleToString(Double_t d); Bool_t StringToBool(std::string in); Long64_t StringToLong(std::string in); TVector3 StringTo3DVector(std::string in); diff --git a/source/framework/tools/src/TRestStringHelper.cxx b/source/framework/tools/src/TRestStringHelper.cxx index 979b76b40..e3d7b2c0b 100644 --- a/source/framework/tools/src/TRestStringHelper.cxx +++ b/source/framework/tools/src/TRestStringHelper.cxx @@ -498,6 +498,11 @@ Int_t REST_StringHelper::StringToInteger(string in) { /// string REST_StringHelper::IntegerToString(Int_t n) { return Form("%d", n); } +/////////////////////////////////////////////// +/// \brief Gets a string from a double +/// +string REST_StringHelper::DoubleToString(Double_t d) { return Form("%4.2lf", d); } + Bool_t REST_StringHelper::StringToBool(std::string in) { return (ToUpper(in) == "TRUE" || ToUpper(in) == "ON"); } From ca8e5171832c4061d21a89aea172db1011499abe Mon Sep 17 00:00:00 2001 From: Luis Obis <35803280+lobis@users.noreply.github.com> Date: Mon, 4 Apr 2022 11:14:35 +0200 Subject: [PATCH 05/18] Update source/framework/tools/src/TRestTools.cxx --- source/framework/tools/src/TRestTools.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 55b234e7d..12c7397f3 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -281,7 +281,7 @@ template Double_t TRestTools::GetMaxValueFromTable(const std::vector T TRestTools::GetMinValueFromTable(const std::vector>& data, Int_t column) { - if (data.size() == 0) return 0; + if (data.empty()) return 0; if (column != -1 && data[0].size() <= column) return 0; T minValue = data[0][0]; From 5b3cc975d4314934a55f8a0ff0dead7b8c453b0e Mon Sep 17 00:00:00 2001 From: jgalan Date: Tue, 5 Apr 2022 10:15:48 +0200 Subject: [PATCH 06/18] TRestPhysics::GetConeVectorIntersection prototype added --- source/framework/tools/inc/TRestPhysics.h | 9 ++++++++- source/framework/tools/src/TRestPhysics.cxx | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/source/framework/tools/inc/TRestPhysics.h b/source/framework/tools/inc/TRestPhysics.h index c624ef6f2..1f2a07b1d 100644 --- a/source/framework/tools/inc/TRestPhysics.h +++ b/source/framework/tools/inc/TRestPhysics.h @@ -69,7 +69,14 @@ TVector3 MoveToPlane(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a); TVector3 MoveByDistance(TVector3 pos, TVector3 dir, Double_t d); TVector3 MoveByDistanceFast(TVector3 pos, TVector3 dir, Double_t d); -TVector3 GetPlaneVectorIntersection(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a); +TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, TVector3 const& n, + TVector3 const& a); + +TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, + const TVector3& v); + +TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, + const TVector3& v); Double_t DistanceToAxis(const TVector3& axisPoint, const TVector3& axisVector, const TVector3& point); diff --git a/source/framework/tools/src/TRestPhysics.cxx b/source/framework/tools/src/TRestPhysics.cxx index 0bd822855..adf0e2bd1 100644 --- a/source/framework/tools/src/TRestPhysics.cxx +++ b/source/framework/tools/src/TRestPhysics.cxx @@ -76,10 +76,28 @@ Double_t DistanceToAxis(const TVector3& axisPoint, const TVector3& axisVector, c /// and moving in direction `dir` and the plane defined by its normal vector `n` and the point `a`. This is /// equivalent to move/translate the position `pos` to the plane. /// -TVector3 GetPlaneVectorIntersection(TVector3 pos, TVector3 dir, TVector3 n, TVector3 a) { +TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& n, + const TVector3& a) { return MoveToPlane(pos, dir, n, a); } +/////////////////////////////////////////////// +/// \brief This method will find the intersection of the trajectory defined by the vector starting at `pos` +/// and moving in direction `dir` and the cone defined by its axis vector `d` and the vertex`v`. The +/// resulting TVector3 will be the position of the particle placed at the cone surface. +/// +TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, + const TVector3& v); + +/////////////////////////////////////////////// +/// \brief This method will find the intersection of the trajectory defined by the vector starting at `pos` +/// and moving in direction `dir` and the cone defined by its characteristic matrix `M`, which is built +/// using the cone axis vector `d` as `d^T x d`, and the vertex`v`. The resulting TVector3 will be the +/// position of the particle placed at the cone surface. +/// +TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, + const TVector3& v); + /////////////////////////////////////////////// /// \brief This method transports a position `pos` by a distance `d` in the direction defined by `dir`. /// From e131b123b3fbfd3bce29f13705789eef8dc34a44 Mon Sep 17 00:00:00 2001 From: jgalan Date: Tue, 5 Apr 2022 12:22:17 +0200 Subject: [PATCH 07/18] TRestTools::DownloadRemoteFile. Adding succeed message after retying download --- source/framework/tools/src/TRestTools.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 12c7397f3..380e32462 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -816,6 +816,8 @@ std::string TRestTools::DownloadRemoteFile(string url) { if (out == 1024) { warning << "Retrying download in 5 seconds" << endl; std::this_thread::sleep_for(std::chrono::seconds(5)); + } else if (attempts < 10) { + success << "Download suceeded after " << 10 - attempts << " attempts" << endl; } attempts--; } while (out == 1024 && attempts > 0); From 79e5b671b0d9014dadad260df74e4bc50e87bdbc Mon Sep 17 00:00:00 2001 From: jgalan Date: Tue, 5 Apr 2022 12:31:31 +0200 Subject: [PATCH 08/18] TRestPhysics. Implemented cone matrix calculation --- source/framework/tools/inc/TRestPhysics.h | 2 ++ source/framework/tools/src/TRestPhysics.cxx | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/source/framework/tools/inc/TRestPhysics.h b/source/framework/tools/inc/TRestPhysics.h index 1f2a07b1d..1d6780b65 100644 --- a/source/framework/tools/inc/TRestPhysics.h +++ b/source/framework/tools/inc/TRestPhysics.h @@ -26,8 +26,10 @@ #include using namespace std; +#include "TMatrixD.h" #include "TString.h" #include "TVector3.h" +#include "TVectorD.h" /// This namespace serves to define physics constants and other basic physical operations namespace REST_Physics { diff --git a/source/framework/tools/src/TRestPhysics.cxx b/source/framework/tools/src/TRestPhysics.cxx index adf0e2bd1..9e56f8c9c 100644 --- a/source/framework/tools/src/TRestPhysics.cxx +++ b/source/framework/tools/src/TRestPhysics.cxx @@ -87,7 +87,16 @@ TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, co /// resulting TVector3 will be the position of the particle placed at the cone surface. /// TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, - const TVector3& v); + const TVector3& v) { + double cAxis[3]; + d.GetXYZ(cAxis); + + TVectorD coneAxis(3, cAxis); + + TMatrixD M(3, 3); + M.Rank1Update(coneAxis, coneAxis); + return GetConeVectorIntersection(pos, dir, M, v); +} /////////////////////////////////////////////// /// \brief This method will find the intersection of the trajectory defined by the vector starting at `pos` @@ -96,7 +105,11 @@ TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, con /// position of the particle placed at the cone surface. /// TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, - const TVector3& v); + const TVector3& v) { + M.Print(); + + return TVector3(0, 0, 0); +} /////////////////////////////////////////////// /// \brief This method transports a position `pos` by a distance `d` in the direction defined by `dir`. From fc1c38e948bab7ef8a33d06aa67e9c9282c39bdc Mon Sep 17 00:00:00 2001 From: jgalan Date: Tue, 5 Apr 2022 15:14:05 +0200 Subject: [PATCH 09/18] TRestPhysics::GetConeMatrix method added --- source/framework/tools/inc/TRestPhysics.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/framework/tools/inc/TRestPhysics.h b/source/framework/tools/inc/TRestPhysics.h index 1d6780b65..1ad41c6d8 100644 --- a/source/framework/tools/inc/TRestPhysics.h +++ b/source/framework/tools/inc/TRestPhysics.h @@ -74,9 +74,10 @@ TVector3 MoveByDistanceFast(TVector3 pos, TVector3 dir, Double_t d); TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, TVector3 const& n, TVector3 const& a); -TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, - const TVector3& v); +TMatrixD GetConeMatrix(const TVector3& d, const Double_t& cosTheta); +TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, + const TVector3& v, const Double_t& cosTheta); TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, const TVector3& v); From c35d2acbfd75a51c756938f50c1a7123e17dd742 Mon Sep 17 00:00:00 2001 From: jgalan Date: Thu, 7 Apr 2022 22:31:22 +0200 Subject: [PATCH 10/18] TRestStringHelper::StringToElements remove unnecessary output --- source/framework/tools/src/TRestStringHelper.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/source/framework/tools/src/TRestStringHelper.cxx b/source/framework/tools/src/TRestStringHelper.cxx index e3d7b2c0b..b8ea5b19a 100644 --- a/source/framework/tools/src/TRestStringHelper.cxx +++ b/source/framework/tools/src/TRestStringHelper.cxx @@ -212,7 +212,6 @@ std::vector REST_StringHelper::StringToElements(std::string in, string h size_t startPos = in.find(headChar); size_t endPos = in.find(tailChar); if (startPos == string::npos || endPos == string::npos) { - ferr << "StringToElements wrong arguments!" << endl; return result; } std::vector values = Split(in.substr(startPos + 1, endPos - startPos - 1), ","); From b17b9d271aa4cc31c065fc11cc67eed8f50b5f35 Mon Sep 17 00:00:00 2001 From: jgalan Date: Fri, 8 Apr 2022 10:43:00 +0200 Subject: [PATCH 11/18] TRestPhysics::GetConeIntersection impemented --- source/framework/tools/inc/TRestPhysics.h | 6 +- source/framework/tools/src/TRestPhysics.cxx | 83 ++++++++++++++++++--- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/source/framework/tools/inc/TRestPhysics.h b/source/framework/tools/inc/TRestPhysics.h index 1ad41c6d8..39aeeda5e 100644 --- a/source/framework/tools/inc/TRestPhysics.h +++ b/source/framework/tools/inc/TRestPhysics.h @@ -76,10 +76,10 @@ TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, TV TMatrixD GetConeMatrix(const TVector3& d, const Double_t& cosTheta); -TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, +Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, const TVector3& v, const Double_t& cosTheta); -TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, - const TVector3& v); +Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, + const TVector3& axis, const TVector3& v); Double_t DistanceToAxis(const TVector3& axisPoint, const TVector3& axisVector, const TVector3& point); diff --git a/source/framework/tools/src/TRestPhysics.cxx b/source/framework/tools/src/TRestPhysics.cxx index 9e56f8c9c..a4df761ae 100644 --- a/source/framework/tools/src/TRestPhysics.cxx +++ b/source/framework/tools/src/TRestPhysics.cxx @@ -82,12 +82,10 @@ TVector3 GetPlaneVectorIntersection(const TVector3& pos, const TVector3& dir, co } /////////////////////////////////////////////// -/// \brief This method will find the intersection of the trajectory defined by the vector starting at `pos` -/// and moving in direction `dir` and the cone defined by its axis vector `d` and the vertex`v`. The -/// resulting TVector3 will be the position of the particle placed at the cone surface. +/// \brief It returns the cone matrix M = d^T x d - cosTheta^2 x I, extracted from the document +/// by "David Eberly, Geometric Tools, Redmond WA 98052, Intersection of a Line and a Cone". /// -TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, - const TVector3& v) { +TMatrixD GetConeMatrix(const TVector3& d, const Double_t& cosTheta) { double cAxis[3]; d.GetXYZ(cAxis); @@ -95,7 +93,32 @@ TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, con TMatrixD M(3, 3); M.Rank1Update(coneAxis, coneAxis); - return GetConeVectorIntersection(pos, dir, M, v); + + double cT2 = cosTheta * cosTheta; + TMatrixD gamma(3, 3); + gamma.UnitMatrix(); + gamma *= cT2; + + M -= gamma; + return M; +} + +/////////////////////////////////////////////// +/// \brief This method will find the intersection of the trajectory defined by the vector starting at +/// `pos` and moving in direction `dir` and the cone defined by its axis vector `d` and the vertex`v`. +/// The cosine of the angle defining the cone should be also given inside the `cosTheta` argument. +/// +/// This method will return `t`, which is the value the particle position, `pos`, needs to be displaced +/// by the vector, `dir`, to get the particle at the surface of the cone. If the particle does not +/// cross the cone, then the value returned will be zero (no particle displacement). +// +/// This method is based on the document by "David Eberly, Geometric Tools, Redmond WA 98052, +/// Intersection of a Line and a Cone". +/// +Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TVector3& d, + const TVector3& v, const Double_t& cosTheta) { + TMatrixD M = GetConeMatrix(d, cosTheta); + return GetConeVectorIntersection(pos, dir, M, d, v); } /////////////////////////////////////////////// @@ -104,11 +127,51 @@ TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, con /// using the cone axis vector `d` as `d^T x d`, and the vertex`v`. The resulting TVector3 will be the /// position of the particle placed at the cone surface. /// -TVector3 GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, - const TVector3& v) { - M.Print(); +/// This method will return `t`, which is the value the particle position, `pos`, needs to be displaced +/// by the vector, `dir`, to get the particle at the surface of the cone. If the particle does not +/// cross the cone, then the value returned will be zero (no particle displacement). +/// +/// This method is based on the document by "David Eberly, Geometric Tools, Redmond WA 98052, +/// Intersection of a Line and a Cone". +/// +Double_t GetConeVectorIntersection(const TVector3& pos, const TVector3& dir, const TMatrixD& M, + const TVector3& axis, const TVector3& v) { + double u[3]; + dir.GetXYZ(u); + TMatrixD U(3, 1, u); + TMatrixD Ut(1, 3, u); + + double delta[3]; + TVector3 deltaV = pos - v; + deltaV.GetXYZ(delta); + TMatrixD D(3, 1, delta); + TMatrixD Dt(1, 3, delta); + + TMatrixD C2 = Ut * M * U; + Double_t c2 = C2[0][0]; + + TMatrixD C1 = Ut * M * D; + Double_t c1 = C1[0][0]; + + TMatrixD C0 = Dt * M * D; + Double_t c0 = C0[0][0]; + + Double_t root = c1 * c1 - c0 * c2; + if (root < 0) return 0; + + Double_t t1 = (-c1 + TMath::Sqrt(root)) / c2; + Double_t t2 = (-c1 - TMath::Sqrt(root)) / c2; + + // The projections along the cone axis. If positive then the solution + // gives the cone intersection with the side defined by `axis` + Double_t h1 = t1 * dir.Dot(axis) + axis.Dot(deltaV); + Double_t h2 = t2 * dir.Dot(axis) + axis.Dot(deltaV); - return TVector3(0, 0, 0); + // We use it to select the root we are interested in + if (h2 > 0) + return t2; + else + return t1; } /////////////////////////////////////////////// From 63608cb511e29cc86e64b985958ac7dfc35f39fd Mon Sep 17 00:00:00 2001 From: jgalan Date: Fri, 8 Apr 2022 15:42:57 +0200 Subject: [PATCH 12/18] TRestTools::ReadBinaryTable. Fixing a bug that replicated the last row on the output table --- source/framework/tools/src/TRestTools.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 9373f9993..9bfe39ef5 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -223,9 +223,10 @@ int TRestTools::ReadBinaryTable(string fName, std::vector>& data, } std::vector dataArray(columns); + fin.read(reinterpret_cast(&dataArray[0]), columns * sizeof(T)); while (fin.good()) { - fin.read(reinterpret_cast(&dataArray[0]), columns * sizeof(T)); data.push_back(dataArray); + fin.read(reinterpret_cast(&dataArray[0]), columns * sizeof(T)); } return 1; } From 88aedfa18920c59851d53d475e8e18db99643cf9 Mon Sep 17 00:00:00 2001 From: Luis Obis <35803280+lobis@users.noreply.github.com> Date: Fri, 8 Apr 2022 22:24:47 +0200 Subject: [PATCH 13/18] cmake/Testing.cmake - removed useless line --- cmake/Testing.cmake | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmake/Testing.cmake b/cmake/Testing.cmake index 41fe65f74..12a40b7fa 100644 --- a/cmake/Testing.cmake +++ b/cmake/Testing.cmake @@ -19,10 +19,8 @@ endmacro() macro(ADD_LIBRARY_TEST) if (TEST) message(STATUS "Adding tests at ${CMAKE_CURRENT_SOURCE_DIR}") - set(TESTING_EXECUTABLE testRestGeant4) get_filename_component(DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) - string(SUBSTRING ${DIR_NAME} 0 1 FIRST_LETTER) string(TOUPPER ${FIRST_LETTER} FIRST_LETTER) string(REGEX REPLACE "^.(.*)" "${FIRST_LETTER}\\1" DIR_NAME_CAPITALIZED "${DIR_NAME}") From ca225f0e5379527768fe43359ae21a359ca7adad Mon Sep 17 00:00:00 2001 From: jgalan Date: Sat, 9 Apr 2022 18:58:08 +0200 Subject: [PATCH 14/18] TRestTools::GetFileNameExtension method added --- source/framework/tools/inc/TRestTools.h | 1 + source/framework/tools/src/TRestTools.cxx | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index d721831cf..536d7368c 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -84,6 +84,7 @@ class TRestTools { static string ToAbsoluteName(string filename); static vector GetSubdirectories(const string& path, int recursion = -1); static std::pair SeparatePathAndName(const std::string fullname); + static std::string GetFileNameExtension(std::string fullname); static std::string GetPureFileName(std::string fullpathFileName); static std::string SearchFileInPath(vector path, string filename); static Int_t ChecktheFile(std::string cfgFileName); diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 9bfe39ef5..20231538c 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -195,7 +195,7 @@ template int TRestTools::ExportBinaryTable(std::string fname, /// /// \code /// std::vector > fvec; -/// ReadBinaryFile( "myfile.bin", fvec, 6); +/// ReadBinaryTable( "myfile.bin", fvec, 6); /// \endcode /// /// The values on the table will be loaded in the matrix provided through the @@ -538,6 +538,20 @@ std::pair TRestTools::SeparatePathAndName(string fullname) { return result; } +/////////////////////////////////////////////// +/// \brief Gets the file extension as the substring found after the lastest "." +/// +/// Input: "/home/jgalan/abc.txt" Output: "txt" +/// +string TRestTools::GetFileNameExtension(string fullname) { + int pos = fullname.find_last_of('.', -1); + + if (pos != -1) { + return fullname.substr(pos + 1, fullname.size() - pos - 1); + } + return fullname; +} + /////////////////////////////////////////////// /// \brief Returns the input string but without multiple slashes ("/") /// From af9bb01acc816e75a2a34467c1f8b071d5e84f35 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sat, 9 Apr 2022 19:56:29 +0200 Subject: [PATCH 15/18] TRestTools::ReadBinaryTable will attempt to read number of columns from Nxxid/f file extension --- source/framework/tools/inc/TRestTools.h | 4 ++- source/framework/tools/src/TRestTools.cxx | 40 +++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index 536d7368c..d9dc0e31e 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -54,7 +54,9 @@ class TRestTools { static int ReadASCIITable(string fName, std::vector>& data, Int_t skipLines = 0); template - static int ReadBinaryTable(string fName, std::vector>& data, Int_t columns); + static int ReadBinaryTable(string fName, std::vector>& data, Int_t columns = -1); + + static int GetBinaryFileColumns(string fname); template static T GetMaxValueFromTable(const std::vector>& data, Int_t column = -1); diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 20231538c..fa04ef012 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -204,11 +204,20 @@ template int TRestTools::ExportBinaryTable(std::string fname, template int TRestTools::ReadBinaryTable(string fName, std::vector>& data, Int_t columns) { if (!TRestTools::isValidFile((string)fName)) { - cout << "TRestTools::ReadBinaryTable. Error." << endl; - cout << "Cannot open file : " << fName << endl; + ferr << "TRestTools::ReadBinaryTable. Error." << endl; + ferr << "Cannot open file : " << fName << endl; return 0; } + if (columns == -1) { + columns = GetBinaryFileColumns(fName); + if (columns == -1) { + ferr << "TRestTools::ReadBinaryTable. Format extension error." << endl; + ferr << "Please, specify the number of columns at the method 3rd argument" << endl; + return 0; + } + } + std::ifstream fin(fName, std::ios::binary); fin.seekg(0, std::ios::end); const size_t num_elements = fin.tellg() / sizeof(T); @@ -238,6 +247,33 @@ template int TRestTools::ReadBinaryTable(string fName, std::vector(string fName, std::vector>& data, Int_t columns); +int TRestTools::GetBinaryFileColumns(string fname) { + string extension = GetFileNameExtension(fname); + if (extension.find("N") != 0) { + ferr << "Wrong filename extension." << endl; + ferr << "Cannot guess the number of columns" << endl; + return -1; + } + + size_t pos = extension.find("i"); + if (pos != string::npos) { + return StringToInteger(extension.substr(1, pos - 1)); + } + + pos = extension.find("f"); + if (pos != string::npos) { + return StringToInteger(extension.substr(1, pos - 1)); + } + + pos = extension.find("d"); + if (pos != string::npos) { + return StringToInteger(extension.substr(1, pos - 1)); + } + + ferr << "Format " << ToUpper(extension) << " not recognized" << endl; + return -1; +} + /////////////////////////////////////////////// /// \brief It returns the maximum value for a particular `column` from the table given by /// argument. If no column is specified in the arguments, then it gets the maximum from the From 4ec138e33ebf331157c86227307ff6d30b91b7f3 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sat, 9 Apr 2022 20:13:04 +0200 Subject: [PATCH 16/18] TRestStringHelper. Fixing typo introduced while fixing conflicts --- source/framework/tools/inc/TRestStringHelper.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/framework/tools/inc/TRestStringHelper.h b/source/framework/tools/inc/TRestStringHelper.h index a2fd76d53..c143eb229 100644 --- a/source/framework/tools/inc/TRestStringHelper.h +++ b/source/framework/tools/inc/TRestStringHelper.h @@ -39,10 +39,11 @@ Bool_t StringToBool(std::string in); Long64_t StringToLong(std::string in); TVector3 StringTo3DVector(std::string in); TVector2 StringTo2DVector(std::string in); -std::vector Split(std::string in, string separator, bool allowBlankString = false, - bool removeWhiteSpaces = false, int startPos = -1); -std::vector StringToElements(std::string in, string separator); -std::vector StringToElements(std::string in, string headChar, string separator, string tailChar); +std::vector Split(std::string in, std::string separator, bool allowBlankString = false, + bool removeWhiteSpaces = false, int startPos = -1); +std::vector StringToElements(std::string in, std::string separator); +std::vector StringToElements(std::string in, std::string headChar, std::string separator, + std::string tailChar); std::string RemoveWhiteSpaces(std::string in); std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition = 0, Int_t N = 0); From 9831970bd85a5ed156629d8029de38639395c933 Mon Sep 17 00:00:00 2001 From: jgalan Date: Sat, 9 Apr 2022 20:43:30 +0200 Subject: [PATCH 17/18] TRestTools. Fixing more conflicts --- source/framework/tools/inc/TRestTools.h | 2 ++ source/framework/tools/src/TRestTools.cxx | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index 78edb9d26..aaaaff6d2 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -56,6 +56,8 @@ class TRestTools { template static int ReadBinaryTable(std::string fName, std::vector>& data, Int_t columns = -1); + static std::string GetFileNameExtension(std::string fullname); + static int GetBinaryFileColumns(std::string fname); template diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index d27a2b9e5..6ceaa526a 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -160,7 +160,6 @@ template int TRestTools::ExportASCIITable(std::string fname, std::vector>& data); /////////////////////////////////////////////// -<<<<<<< HEAD /// \brief Writes the contents of the vector table given as argument to `fname` as a binary file. /// Allowed types are Int_t, Float_t and Double_t. /// @@ -189,9 +188,6 @@ template int TRestTools::ExportBinaryTable(std::string fname, /////////////////////////////////////////////// /// \brief Reads a binary file containning a fixed-columns table with values -======= -/// \brief Reads a binary file containing a fixed-columns table with values ->>>>>>> origin/master /// /// This method will open the file fName. This file should contain a /// table with numeric values of the type specified inside the syntax < >. From 0d73be7d01fff4a37be819a84230eaf9e8bef82f Mon Sep 17 00:00:00 2001 From: jgalan Date: Mon, 11 Apr 2022 10:15:21 +0200 Subject: [PATCH 18/18] TRestTools::IsBinaryFile method added --- source/framework/tools/inc/TRestTools.h | 2 ++ source/framework/tools/src/TRestTools.cxx | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/source/framework/tools/inc/TRestTools.h b/source/framework/tools/inc/TRestTools.h index aaaaff6d2..7c80306af 100644 --- a/source/framework/tools/inc/TRestTools.h +++ b/source/framework/tools/inc/TRestTools.h @@ -56,6 +56,8 @@ class TRestTools { template static int ReadBinaryTable(std::string fName, std::vector>& data, Int_t columns = -1); + static Bool_t IsBinaryFile(std::string fname); + static std::string GetFileNameExtension(std::string fullname); static int GetBinaryFileColumns(std::string fname); diff --git a/source/framework/tools/src/TRestTools.cxx b/source/framework/tools/src/TRestTools.cxx index 6ceaa526a..c82fe1bd1 100644 --- a/source/framework/tools/src/TRestTools.cxx +++ b/source/framework/tools/src/TRestTools.cxx @@ -248,6 +248,23 @@ template int TRestTools::ReadBinaryTable(string fName, std::vector(string fName, std::vector>& data, Int_t columns); +/////////////////////////////////////////////// +/// \brief It identifies if the filename extension follows the formatting ".Nxyzf", where the +/// number of columns is `xyz`, and the last character is the type of data (f/d/i), float, +/// double and integer respectively. +/// +Bool_t TRestTools::IsBinaryFile(string fname) { + if (GetBinaryFileColumns(fname) > 0) return true; + return false; +} + +/////////////////////////////////////////////// +/// \brief It extracts the number of columns from the filename extension given by argument. +/// The file will containing a binary formatted table with a fixed number of rows and columns. +/// +/// The filename extension will be : ".Nxyzf", where the number of columns is `xyz`, and the +/// last character is the type of data (f/d/i), float, double and integer respectively. +/// int TRestTools::GetBinaryFileColumns(string fname) { string extension = GetFileNameExtension(fname); if (extension.find("N") != 0) {