Skip to content

Commit

Permalink
TRestDataSet. Adding cuts documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgalan committed Feb 20, 2024
1 parent 54b941f commit 1a3e2a3
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions source/framework/core/src/TRestDataSet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@
/// where `SolarFlux`,`GeneratorArea` and `Nsim` are the given names of
/// the relevant quantities inside the dataset.
///
/// ### Adding cuts
///
/// It is also possible to add cuts used to filter the data that will
/// be stored inside the dataset. We can do that including a TRestCut
/// definition inside the TRestDataSet.
///
/// For example, the following cut definition would discard entries
/// with unexpected values inside the specified column, `process_status`.
///
/// \code
/// <TRestCut>
/// <cut name="goodData" value="!TMath::IsNaN(process_status)"/>
/// </TRestCut>
/// \endcode
///
///----------------------------------------------------------------------
///
/// REST-for-Physics - Software for Rare Event Searches Toolkit
Expand Down Expand Up @@ -353,7 +368,7 @@ void TRestDataSet::GenerateDataSet() {
fDataSet = MakeCut(fCut);

// Adding new user columns added to the dataset
for (const auto& [cName, cExpression] : fColumnNameExpressions) {
for (const auto & [ cName, cExpression ] : fColumnNameExpressions) {
RESTInfo << "Adding column to dataset: " << cName << RESTendl;
finalList.emplace_back(cName);
fDataSet = DefineColumn(cName, cExpression);
Expand Down Expand Up @@ -384,8 +399,7 @@ std::vector<std::string> TRestDataSet::FileSelection() {

if (!time_stamp_end || !time_stamp_start) {
RESTError << "TRestDataSet::FileSelect. Start or end dates not properly formed. Please, check "
"REST_StringHelper::StringToTimeStamp documentation for valid formats"
<< RESTendl;
"REST_StringHelper::StringToTimeStamp documentation for valid formats" << RESTendl;
return fFileSelection;
}

Expand Down Expand Up @@ -438,7 +452,7 @@ std::vector<std::string> TRestDataSet::FileSelection() {
if (!accept) continue;

Double_t acc = 0;
for (auto& [name, properties] : fQuantity) {
for (auto & [ name, properties ] : fQuantity) {
std::string value = run.ReplaceMetadataMembers(properties.metadata);
const Double_t val = REST_StringHelper::StringToDouble(value);

Expand Down Expand Up @@ -483,7 +497,7 @@ std::vector<std::string> TRestDataSet::FileSelection() {
}

///////////////////////////////////////////////
/// \brief This function apply a TRestCut to the dataframe
/// \brief This function applies a TRestCut to the dataframe
/// and returns a dataframe with the applied cuts. Note that
/// the cuts are not applied directly to the dataframe on
/// TRestDataSet, to do so you should do fDataSet = MakeCut(fCut);
Expand All @@ -495,7 +509,7 @@ ROOT::RDF::RNode TRestDataSet::MakeCut(const TRestCut* cut) {

auto paramCut = cut->GetParamCut();
auto obsList = df.GetColumnNames();
for (const auto& [param, condition] : paramCut) {
for (const auto & [ param, condition ] : paramCut) {
if (std::find(obsList.begin(), obsList.end(), param) != obsList.end()) {
std::string pCut = param + condition;
RESTDebug << "Applying cut " << pCut << RESTendl;
Expand Down Expand Up @@ -542,7 +556,7 @@ ROOT::RDF::RNode TRestDataSet::DefineColumn(const std::string& columnName, const
auto df = fDataSet;

std::string evalFormula = formula;
for (auto const& [name, properties] : fQuantity)
for (auto const & [ name, properties ] : fQuantity)
evalFormula = REST_StringHelper::Replace(evalFormula, name, properties.value);

df = df.Define(columnName, evalFormula);
Expand Down Expand Up @@ -608,7 +622,7 @@ void TRestDataSet::PrintMetadata() {
RESTMetadata << " Relevant quantities: " << RESTendl;
RESTMetadata << " -------------------- " << RESTendl;

for (auto const& [name, properties] : fQuantity) {
for (auto const & [ name, properties ] : fQuantity) {
RESTMetadata << " - Name : " << name << ". Value : " << properties.value
<< ". Strategy: " << properties.strategy << RESTendl;
RESTMetadata << " - Metadata: " << properties.metadata << RESTendl;
Expand All @@ -620,7 +634,7 @@ void TRestDataSet::PrintMetadata() {
if (!fColumnNameExpressions.empty()) {
RESTMetadata << " New columns added to generated dataframe: " << RESTendl;
RESTMetadata << " ---------------------------------------- " << RESTendl;
for (const auto& [cName, cExpression] : fColumnNameExpressions) {
for (const auto & [ cName, cExpression ] : fColumnNameExpressions) {
RESTMetadata << " - Name : " << cName << RESTendl;
RESTMetadata << " - Expression: " << cExpression << RESTendl;
RESTMetadata << " " << RESTendl;
Expand Down Expand Up @@ -786,11 +800,10 @@ void TRestDataSet::Export(const std::string& filename, std::vector<std::string>

std::vector<std::string> columns = fDataSet.GetColumnNames();
if (!excludeColumns.empty()) {
columns.erase(std::remove_if(columns.begin(), columns.end(),
[&excludeColumns](std::string elem) {
return std::find(excludeColumns.begin(), excludeColumns.end(),
elem) != excludeColumns.end();
}),
columns.erase(std::remove_if(columns.begin(), columns.end(), [&excludeColumns](std::string elem) {
return std::find(excludeColumns.begin(), excludeColumns.end(), elem) !=
excludeColumns.end();
}),
columns.end());

RESTInfo << "Re-Generating snapshot." << RESTendl;
Expand Down Expand Up @@ -825,8 +838,7 @@ void TRestDataSet::Export(const std::string& filename, std::vector<std::string>
if (type != "Double_t" && type != "Int_t") {
RESTError << "Branch name : " << bName << " is type : " << type << RESTendl;
RESTError << "Only Int_t and Double_t types are allowed for "
"exporting to ASCII table"
<< RESTendl;
"exporting to ASCII table" << RESTendl;
RESTError << "File will not be generated" << RESTendl;
return;
}
Expand Down Expand Up @@ -861,7 +873,7 @@ void TRestDataSet::Export(const std::string& filename, std::vector<std::string>
}
fprintf(f, "###\n");
fprintf(f, "### Relevant quantities: \n");
for (auto& [name, properties] : fQuantity) {
for (auto & [ name, properties ] : fQuantity) {
fprintf(f, "### - %s : %s - %s\n", name.c_str(), properties.value.c_str(),
properties.description.c_str());
}
Expand Down

0 comments on commit 1a3e2a3

Please sign in to comment.