Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing TRestCut inside TRestDataSet #391

Merged
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion source/framework/core/inc/TRestCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCut> fCuts;

// Vector of cut strings e.g. when you use a complex cut
std::vector<std::string> fCutStrings;

// Vector of parameter cuts, first item is parameter and second is the condition
std::vector<std::pair<std::string, std::string> > fParamCut;

protected:
void Initialize() override;
void InitFromConfigFile() override;
Expand All @@ -40,6 +47,12 @@ 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; }

TRestCut& operator=(TRestCut& cut);

void PrintMetadata() override;

Int_t Write(const char* name, Int_t option, Int_t bufsize) override;
Expand All @@ -49,7 +62,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
3 changes: 2 additions & 1 deletion source/framework/core/inc/TRestDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <ROOT/RDataFrame.hxx>

#include "TRestCut.h"
#include "TRestMetadata.h"

struct RelevantQuantity {
Expand Down Expand Up @@ -77,7 +78,7 @@ class TRestDataSet : public TRestMetadata {
std::map<std::string, RelevantQuantity> fQuantity; //<

/// Parameter cuts over the selected dataset
std::vector<std::pair<std::string, std::string> > fParamCut;
TRestCut* fCut = nullptr;

/// The total integrated run time of selected files
Double_t fTotalDuration = 0; //<
Expand Down
46 changes: 41 additions & 5 deletions source/framework/core/src/TRestCut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/// <TRestCut/>
/// <cut name="cc1" value="XX>10 AND XX<90"/>
/// <cut name="cc2" value="sAna_ThresholdIntegral<100e3"/>
/// <cut name="cc3" variable="sAna_ThresholdIntegral" condition=">0">
/// </TRestCut>
///
/// Note that the notations " AND " and " OR " will be replaced by " && " and " || "
Expand All @@ -44,6 +45,9 @@
/// 2021-dec: First concept.
/// Ni Kaixiang
///
/// 2023-March:
///
///
/// \class TRestCut
///
/// <hr>
Expand All @@ -68,14 +72,44 @@ 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 << "<cut name='cc1' value='XX>10 AND XX<90'/>" << RESTendl;
RESTError << "<cut name='cc3' variable='sAna_ThresholdIntegral' condition='>0'" << RESTendl;
exit(1);
}

ele = GetNextElement(ele);
}
}

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;
Expand Down Expand Up @@ -105,8 +139,10 @@ TCut TRestCut::GetCut(string name) {
void TRestCut::PrintMetadata() {
TRestMetadata::PrintMetadata();
RESTMetadata << " " << RESTendl;
RESTMetadata << "Number of TCut objects added: " << fCuts.size() << RESTendl;
RESTMetadata << " " << RESTendl;
RESTMetadata << "Cuts added: " << RESTendl;
for (const auto& cut : fCuts) {
RESTMetadata << cut.GetName() << " " << cut.GetTitle() << RESTendl;
}
RESTMetadata << "+++" << RESTendl;
}

Expand Down
71 changes: 35 additions & 36 deletions source/framework/core/src/TRestDataSet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@
/// <observables list="g4Ana_totalEdep:hitsAna_energy" />
///
/// // Will apply a cut to the observables
/// <cut variable="rawAna_NumberOfGoodSignals" condition=">10" />
/// <cut variable="rawAna_NumberOfGoodSignals" condition="<100" />
///
/// <TRestCut>
/// <cut name="c1" variable="rawAna_NumberOfGoodSignals" condition=">10" />
/// <cut name="c1" variable="rawAna_NumberOfGoodSignals" condition="<100" />
/// </TRestCut>
/// // Will add all the observables from the process `rawAna`
/// <processObservables list="rate:rawAna" />
///
Expand Down Expand Up @@ -289,23 +290,38 @@ 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");
Expand Down Expand Up @@ -585,24 +601,7 @@ 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);
}

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);
}
fCut = (TRestCut*)InstantiateChildMetadata("TRestCut");
}

///////////////////////////////////////////////
Expand Down