From 974c3f4f6df3816f8ab5801527489d5c4506faed Mon Sep 17 00:00:00 2001 From: juanan Date: Tue, 14 Mar 2023 09:43:38 +0100 Subject: [PATCH 1/6] Changes on TRestCut, adding possibility of adding cut by parameter --- source/framework/core/inc/TRestCut.h | 12 +++++++- source/framework/core/src/TRestCut.cxx | 38 ++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/source/framework/core/inc/TRestCut.h b/source/framework/core/inc/TRestCut.h index 7b7bf743e..8e4095b3b 100644 --- a/source/framework/core/inc/TRestCut.h +++ b/source/framework/core/inc/TRestCut.h @@ -30,8 +30,15 @@ //! A class to help on cuts definitions. To be used with TRestAnalysisTree class TRestCut : public TRestMetadata { private: + // Vector of TCuts std::vector fCuts; + // Vector of cut strings e.g. when you use a complex cut + std::vector fCutStrings; + + // Vector of parameter cat, first item is parameter and second is the condition + std::vector > fParamCut; + protected: void Initialize() override; void InitFromConfigFile() override; @@ -40,6 +47,9 @@ class TRestCut : public TRestMetadata { void AddCut(TCut cut); TCut GetCut(std::string name); + auto GetCutStrings() const {return fCutStrings;} + auto GetParamCut() const {return fParamCut;} + void PrintMetadata() override; Int_t Write(const char* name, Int_t option, Int_t bufsize) override; @@ -49,7 +59,7 @@ class TRestCut : public TRestMetadata { // Destructor ~TRestCut() {} - ClassDefOverride(TRestCut, 1); // Template for a REST "event process" class inherited from + ClassDefOverride(TRestCut, 2); // Template for a REST "event process" class inherited from // TRestEventProcess }; #endif diff --git a/source/framework/core/src/TRestCut.cxx b/source/framework/core/src/TRestCut.cxx index 7fc4a8d4a..222191a02 100644 --- a/source/framework/core/src/TRestCut.cxx +++ b/source/framework/core/src/TRestCut.cxx @@ -26,6 +26,7 @@ /// /// /// +/// /// /// /// Note that the notations " AND " and " OR " will be replaced by " && " and " || " @@ -44,6 +45,9 @@ /// 2021-dec: First concept. /// Ni Kaixiang /// +/// 2023-March: +/// +/// /// \class TRestCut /// ///
@@ -67,11 +71,33 @@ void TRestCut::Initialize() { fCuts.clear(); } void TRestCut::InitFromConfigFile() { auto ele = GetElement("cut"); while (ele != nullptr) { + string name = GetParameter("name", ele, ""); + if (name.empty() || name == "Not defined") { + RESTError << "< cut does not contain a name!" << RESTendl; + exit(1); + } + string cutStr = GetParameter("value", ele, ""); - cutStr = Replace(cutStr, " AND ", " && "); - cutStr = Replace(cutStr, " OR ", " || "); - AddCut(TCut(name.c_str(), cutStr.c_str())); + string variable = GetParameter("variable", ele, ""); + string condition = GetParameter("condition", ele, ""); + + if(!cutStr.empty()){ + cutStr = Replace(cutStr, " AND ", " && "); + cutStr = Replace(cutStr, " OR ", " || "); + fCutStrings.push_back(cutStr); + AddCut(TCut(name.c_str(), cutStr.c_str())); + } else if (!variable.empty() && !condition.empty()){ + fParamCut.push_back(std::make_pair(variable, condition)); + string cutVar = variable + condition; + AddCut(TCut(name.c_str(), cutVar.c_str())); + } else { + RESTError << "TRestCut does not contain a valid parameter/condition or cut string!" << RESTendl; + RESTError << "" << RESTendl; + RESTError << " Date: Tue, 14 Mar 2023 09:47:46 +0100 Subject: [PATCH 2/6] Implementing TRestCut inside TRestDataSet --- source/framework/core/inc/TRestDataSet.h | 3 +- source/framework/core/src/TRestDataSet.cxx | 70 +++++++++++----------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/source/framework/core/inc/TRestDataSet.h b/source/framework/core/inc/TRestDataSet.h index b79975b22..7d2242b98 100644 --- a/source/framework/core/inc/TRestDataSet.h +++ b/source/framework/core/inc/TRestDataSet.h @@ -28,6 +28,7 @@ #include #include "TRestMetadata.h" +#include "TRestCut.h" struct RelevantQuantity { /// The associated metadata member used to register the relevant quantity @@ -77,7 +78,7 @@ class TRestDataSet : public TRestMetadata { std::map fQuantity; //< /// Parameter cuts over the selected dataset - std::vector > fParamCut; + TRestCut *fCut = nullptr; /// The total integrated run time of selected files Double_t fTotalDuration = 0; //< diff --git a/source/framework/core/src/TRestDataSet.cxx b/source/framework/core/src/TRestDataSet.cxx index e1d588bc8..932e859a7 100644 --- a/source/framework/core/src/TRestDataSet.cxx +++ b/source/framework/core/src/TRestDataSet.cxx @@ -104,9 +104,10 @@ /// /// /// // Will apply a cut to the observables -/// -/// -/// +/// +/// +/// +/// /// // Will add all the observables from the process `rawAna` /// /// @@ -289,24 +290,37 @@ void TRestDataSet::Initialize() { ROOT::EnableImplicitMT(); ROOT::RDataFrame df("AnalysisTree", fFileSelection); - - std::string pCut = ""; - for (const auto& [param, cut] : fParamCut) { - if (std::find(finalList.begin(), finalList.end(), param) != finalList.end()) { - if (!pCut.empty()) pCut += " && "; - pCut += param + cut; - } else { - RESTWarning << " Cut observable " << param << " not found in observable list, skipping..." - << RESTendl; + fDataSet = df; + + if(fCut) { + auto paramCut = fCut->GetParamCut(); + for(const auto& [param, condition] : paramCut){ + if(std::find(finalList.begin(), finalList.end(), param) != finalList.end()){ + std::string pCut =param + condition; + RESTDebug << "Applying cut " << pCut << RESTendl; + fDataSet = fDataSet.Filter(pCut); + } else { + RESTWarning << " Cut observable " << param << " not found in observable list, skipping..." << RESTendl; + } } - } - if (!pCut.empty()) { - RESTDebug << "Applying cut " << pCut << RESTendl; - fDataSet = df.Filter(pCut); - } else { - fDataSet = df; - } + auto cutString = fCut->GetCutStrings(); + for(const auto& pCut : cutString){ + bool added = false; + for (const auto& obs : finalList) { + if(pCut.find(obs) != std::string::npos){ + RESTDebug << "Applying cut " << pCut << RESTendl; + fDataSet = fDataSet.Filter(pCut); + added = true; + break; + } + } + + if(!added){ + RESTWarning << " Cut string " << pCut << " not found in observable list, skipping..." << RESTendl; + } + } + } std::string user = getenv("USER"); std::string fOutName = "/tmp/rest_output_" + user + ".root"; @@ -585,24 +599,8 @@ void TRestDataSet::InitFromConfigFile() { quantityDefinition = GetNextElement(quantityDefinition); } - TiXmlElement* cutDefinition = GetElement("cut"); - while (cutDefinition != nullptr) { - std::string variable = GetFieldValue("variable", cutDefinition); - if (variable.empty() || variable == "Not defined") { - RESTError << "< paramCut variable key does not contain a name!" << RESTendl; - exit(1); - } + fCut = (TRestCut*)InstantiateChildMetadata("TRestCut"); - std::string condition = GetFieldValue("condition", cutDefinition); - if (condition.empty() || condition == "Not defined") { - RESTError << "< paramCut condition key does not contain a metadata value!" << RESTendl; - exit(1); - } - - fParamCut.push_back(std::make_pair(variable, condition)); - - cutDefinition = GetNextElement(cutDefinition); - } } /////////////////////////////////////////////// From 3ce28f50d9a1d7279b39bac7ec496f71110e21fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Mar 2023 08:52:21 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/framework/core/inc/TRestCut.h | 4 +- source/framework/core/inc/TRestDataSet.h | 4 +- source/framework/core/src/TRestCut.cxx | 41 +++++++++-------- source/framework/core/src/TRestDataSet.cxx | 51 +++++++++++----------- 4 files changed, 50 insertions(+), 50 deletions(-) diff --git a/source/framework/core/inc/TRestCut.h b/source/framework/core/inc/TRestCut.h index 8e4095b3b..311e6ac1e 100644 --- a/source/framework/core/inc/TRestCut.h +++ b/source/framework/core/inc/TRestCut.h @@ -47,8 +47,8 @@ class TRestCut : public TRestMetadata { void AddCut(TCut cut); TCut GetCut(std::string name); - auto GetCutStrings() const {return fCutStrings;} - auto GetParamCut() const {return fParamCut;} + auto GetCutStrings() const { return fCutStrings; } + auto GetParamCut() const { return fParamCut; } void PrintMetadata() override; diff --git a/source/framework/core/inc/TRestDataSet.h b/source/framework/core/inc/TRestDataSet.h index 7d2242b98..4c1402b10 100644 --- a/source/framework/core/inc/TRestDataSet.h +++ b/source/framework/core/inc/TRestDataSet.h @@ -27,8 +27,8 @@ #include -#include "TRestMetadata.h" #include "TRestCut.h" +#include "TRestMetadata.h" struct RelevantQuantity { /// The associated metadata member used to register the relevant quantity @@ -78,7 +78,7 @@ class TRestDataSet : public TRestMetadata { std::map fQuantity; //< /// Parameter cuts over the selected dataset - TRestCut *fCut = nullptr; + TRestCut* fCut = nullptr; /// The total integrated run time of selected files Double_t fTotalDuration = 0; //< diff --git a/source/framework/core/src/TRestCut.cxx b/source/framework/core/src/TRestCut.cxx index 222191a02..1680a946d 100644 --- a/source/framework/core/src/TRestCut.cxx +++ b/source/framework/core/src/TRestCut.cxx @@ -45,9 +45,9 @@ /// 2021-dec: First concept. /// Ni Kaixiang /// -/// 2023-March: +/// 2023-March: +/// /// -/// /// \class TRestCut /// ///
@@ -71,31 +71,30 @@ void TRestCut::Initialize() { fCuts.clear(); } void TRestCut::InitFromConfigFile() { auto ele = GetElement("cut"); while (ele != nullptr) { - string name = GetParameter("name", ele, ""); - if (name.empty() || name == "Not defined") { + if (name.empty() || name == "Not defined") { RESTError << "< cut does not contain a name!" << RESTendl; exit(1); - } + } string cutStr = GetParameter("value", ele, ""); string variable = GetParameter("variable", ele, ""); string condition = GetParameter("condition", ele, ""); - if(!cutStr.empty()){ - cutStr = Replace(cutStr, " AND ", " && "); - cutStr = Replace(cutStr, " OR ", " || "); - fCutStrings.push_back(cutStr); - AddCut(TCut(name.c_str(), cutStr.c_str())); - } else if (!variable.empty() && !condition.empty()){ - fParamCut.push_back(std::make_pair(variable, condition)); - string cutVar = variable + condition; - AddCut(TCut(name.c_str(), cutVar.c_str())); + if (!cutStr.empty()) { + cutStr = Replace(cutStr, " AND ", " && "); + cutStr = Replace(cutStr, " OR ", " || "); + fCutStrings.push_back(cutStr); + AddCut(TCut(name.c_str(), cutStr.c_str())); + } else if (!variable.empty() && !condition.empty()) { + fParamCut.push_back(std::make_pair(variable, condition)); + string cutVar = variable + condition; + AddCut(TCut(name.c_str(), cutVar.c_str())); } else { - RESTError << "TRestCut does not contain a valid parameter/condition or cut string!" << RESTendl; - RESTError << "" << RESTendl; - RESTError << "" << RESTendl; + RESTError << "GetParamCut(); - for(const auto& [param, condition] : paramCut){ - if(std::find(finalList.begin(), finalList.end(), param) != finalList.end()){ - std::string pCut =param + condition; - RESTDebug << "Applying cut " << pCut << RESTendl; - fDataSet = fDataSet.Filter(pCut); - } else { - RESTWarning << " Cut observable " << param << " not found in observable list, skipping..." << RESTendl; - } + if (fCut) { + auto paramCut = fCut->GetParamCut(); + for (const auto& [param, condition] : paramCut) { + if (std::find(finalList.begin(), finalList.end(), param) != finalList.end()) { + std::string pCut = param + condition; + RESTDebug << "Applying cut " << pCut << RESTendl; + fDataSet = fDataSet.Filter(pCut); + } else { + RESTWarning << " Cut observable " << param << " not found in observable list, skipping..." + << RESTendl; + } } - auto cutString = fCut->GetCutStrings(); - for(const auto& pCut : cutString){ - bool added = false; - for (const auto& obs : finalList) { - if(pCut.find(obs) != std::string::npos){ - RESTDebug << "Applying cut " << pCut << RESTendl; - fDataSet = fDataSet.Filter(pCut); - added = true; - break; + auto cutString = fCut->GetCutStrings(); + for (const auto& pCut : cutString) { + bool added = false; + for (const auto& obs : finalList) { + if (pCut.find(obs) != std::string::npos) { + RESTDebug << "Applying cut " << pCut << RESTendl; + fDataSet = fDataSet.Filter(pCut); + added = true; + break; + } } - } - if(!added){ - RESTWarning << " Cut string " << pCut << " not found in observable list, skipping..." << RESTendl; - } + if (!added) { + RESTWarning << " Cut string " << pCut << " not found in observable list, skipping..." + << RESTendl; + } } - } + } std::string user = getenv("USER"); std::string fOutName = "/tmp/rest_output_" + user + ".root"; @@ -600,7 +602,6 @@ void TRestDataSet::InitFromConfigFile() { } fCut = (TRestCut*)InstantiateChildMetadata("TRestCut"); - } /////////////////////////////////////////////// From 1d1c788290beb24ee7fb04cb2a6c1cc5a86ef983 Mon Sep 17 00:00:00 2001 From: Javier Galan Date: Wed, 22 Mar 2023 08:48:18 +0100 Subject: [PATCH 4/6] Update source/framework/core/inc/TRestCut.h --- source/framework/core/inc/TRestCut.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/framework/core/inc/TRestCut.h b/source/framework/core/inc/TRestCut.h index 311e6ac1e..1be94a6c7 100644 --- a/source/framework/core/inc/TRestCut.h +++ b/source/framework/core/inc/TRestCut.h @@ -36,7 +36,7 @@ class TRestCut : public TRestMetadata { // Vector of cut strings e.g. when you use a complex cut std::vector fCutStrings; - // Vector of parameter cat, first item is parameter and second is the condition + // Vector of parameter cuts, first item is parameter and second is the condition std::vector > fParamCut; protected: From b3946ec043ad5a10983da9a55d1afb1026acdb10 Mon Sep 17 00:00:00 2001 From: juanan Date: Wed, 22 Mar 2023 09:31:14 +0100 Subject: [PATCH 5/6] Adding copy operator to TRestCut --- source/framework/core/inc/TRestCut.h | 7 +++++-- source/framework/core/src/TRestCut.cxx | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/source/framework/core/inc/TRestCut.h b/source/framework/core/inc/TRestCut.h index 1be94a6c7..a9cd5cc5d 100644 --- a/source/framework/core/inc/TRestCut.h +++ b/source/framework/core/inc/TRestCut.h @@ -47,8 +47,11 @@ class TRestCut : public TRestMetadata { void AddCut(TCut cut); TCut GetCut(std::string name); - auto GetCutStrings() const { return fCutStrings; } - auto GetParamCut() const { return fParamCut; } + inline auto GetCutStrings() const {return fCutStrings;} + inline auto GetParamCut() const {return fParamCut;} + inline auto GetCuts() const {return fCuts;} + + TRestCut& operator=(TRestCut& cut); void PrintMetadata() override; diff --git a/source/framework/core/src/TRestCut.cxx b/source/framework/core/src/TRestCut.cxx index 1680a946d..f13793f14 100644 --- a/source/framework/core/src/TRestCut.cxx +++ b/source/framework/core/src/TRestCut.cxx @@ -101,6 +101,15 @@ void TRestCut::InitFromConfigFile() { } } +TRestCut& TRestCut::operator=(TRestCut& cut) { + SetName(cut.GetName()); + SetTitle(cut.GetTitle()); + fCuts = cut.GetCuts(); + fCutStrings = cut.GetCutStrings(); + fParamCut = cut. GetParamCut(); + return *this; +} + void TRestCut::AddCut(TCut cut) { if ((string)cut.GetName() == "") { RESTWarning << "TRestCut::AddCut: cannot add cut without name!" << RESTendl; From 87da4025aab8f365f41ec19c52a664bf20eaedaa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Mar 2023 08:33:54 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/framework/core/inc/TRestCut.h | 6 +++--- source/framework/core/src/TRestCut.cxx | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/framework/core/inc/TRestCut.h b/source/framework/core/inc/TRestCut.h index a9cd5cc5d..71028fda6 100644 --- a/source/framework/core/inc/TRestCut.h +++ b/source/framework/core/inc/TRestCut.h @@ -47,9 +47,9 @@ class TRestCut : public TRestMetadata { void AddCut(TCut cut); TCut GetCut(std::string name); - inline auto GetCutStrings() const {return fCutStrings;} - inline auto GetParamCut() const {return fParamCut;} - inline auto GetCuts() const {return fCuts;} + inline auto GetCutStrings() const { return fCutStrings; } + inline auto GetParamCut() const { return fParamCut; } + inline auto GetCuts() const { return fCuts; } TRestCut& operator=(TRestCut& cut); diff --git a/source/framework/core/src/TRestCut.cxx b/source/framework/core/src/TRestCut.cxx index f13793f14..59ab840ce 100644 --- a/source/framework/core/src/TRestCut.cxx +++ b/source/framework/core/src/TRestCut.cxx @@ -102,12 +102,12 @@ void TRestCut::InitFromConfigFile() { } TRestCut& TRestCut::operator=(TRestCut& cut) { - SetName(cut.GetName()); - SetTitle(cut.GetTitle()); - fCuts = cut.GetCuts(); - fCutStrings = cut.GetCutStrings(); - fParamCut = cut. GetParamCut(); - return *this; + SetName(cut.GetName()); + SetTitle(cut.GetTitle()); + fCuts = cut.GetCuts(); + fCutStrings = cut.GetCutStrings(); + fParamCut = cut.GetParamCut(); + return *this; } void TRestCut::AddCut(TCut cut) {