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

Adding possibility to add a cut on observable parameters in TRestDataSet #386

Merged
merged 21 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9ebc9f4
Adding possibility to add an observable cut in TRestDataSet
juanangp Mar 3, 2023
47ced4c
Trying to optimize dataSet observable selection
juanangp Mar 3, 2023
95488b6
Reverting changes since the observable list is not applied at the RDa…
juanangp Mar 3, 2023
f67d948
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 3, 2023
ebe5aae
Merge branch 'master' into datasetCut
jgalan Mar 9, 2023
ec81007
TRestDataSet now stores a list of the files used to generate the dataset
jgalan Mar 13, 2023
974c3f4
Changes on TRestCut, adding possibility of adding cut by parameter
juanangp Mar 14, 2023
a1e02c9
Implementing TRestCut inside TRestDataSet
juanangp Mar 14, 2023
3ce28f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 14, 2023
1d1c788
Update source/framework/core/inc/TRestCut.h
jgalan Mar 22, 2023
b3946ec
Adding copy operator to TRestCut
juanangp Mar 22, 2023
87da402
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
1aff997
Merge pull request #391 from rest-for-physics/trestcut
juanangp Mar 22, 2023
3a8b00e
Adding possitility to Import a TRestDataSet
juanangp Mar 22, 2023
a91e20c
New function MakeCut that returns a RDataFrame with the cuts applied
juanangp Mar 22, 2023
6e4567e
Using GenerateDataSet to create a TRESTDataSet from TRestRuns instead…
juanangp Mar 22, 2023
380628d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
599a80d
Update source/framework/core/inc/TRestCut.h
juanangp Mar 23, 2023
83e10e9
Update source/framework/core/inc/TRestCut.h
juanangp Mar 23, 2023
5468e20
Update source/framework/core/inc/TRestCut.h
juanangp Mar 23, 2023
f23d1b1
Adding brief description of the changes
juanangp Mar 23, 2023
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
53 changes: 44 additions & 9 deletions 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 All @@ -47,10 +48,10 @@ struct RelevantQuantity {
class TRestDataSet : public TRestMetadata {
private:
/// All the selected runs will have a starting date after fStartTime
std::string fStartTime = "2000/01/01"; //<
std::string fFilterStartTime = "2000/01/01"; //<

/// All the selected runs will have an ending date before fEndTime
std::string fEndTime = "3000/12/31"; //<
std::string fFilterEndTime = "3000/12/31"; //<

/// A glob file pattern that must be satisfied by all files
std::string fFilePattern = ""; //<
Expand All @@ -76,30 +77,41 @@ class TRestDataSet : public TRestMetadata {
/// The properties of a relevant quantity that we want to store together with the dataset
std::map<std::string, RelevantQuantity> fQuantity; //<

/// Parameter cuts over the selected dataset
TRestCut* fCut = nullptr;

/// The total integrated run time of selected files
Double_t fTotalDuration = 0; //<

/// The resulting RDataFrame object after initialization
ROOT::RDataFrame fDataSet = 0; //!
/// A list populated by the FileSelection method using the conditions of the dataset
std::vector<std::string> fFileSelection; //<

/// TimeStamp for the start time of the first file
Double_t fStartTime = REST_StringHelper::StringToTimeStamp(fFilterEndTime);

/// TimeStamp for the end time of the last file
Double_t fEndTime = REST_StringHelper::StringToTimeStamp(fFilterStartTime);

/// The resulting RDF::RNode object after initialization
ROOT::RDF::RNode fDataSet = ROOT::RDataFrame(0); //!

/// A pointer to the generated tree
TTree* fTree = nullptr; //!

/// A list populated by the FileSelection method using the conditions of the dataset
std::vector<std::string> fFileSelection; //!

void InitFromConfigFile() override;

protected:
virtual std::vector<std::string> FileSelection();

public:
/// Gives access to the RDataFrame
ROOT::RDataFrame GetDataFrame() const {
ROOT::RDF::RNode GetDataFrame() const {
if (fTree == nullptr) RESTWarning << "DataFrame has not been yet initialized" << RESTendl;
return fDataSet;
}

void SetDataSet(const ROOT::RDF::RNode& dS) { fDataSet = dS; }

/// Gives access to the tree
TTree* GetTree() const {
if (fTree == nullptr) {
Expand All @@ -122,14 +134,37 @@ class TRestDataSet : public TRestMetadata {
/// It returns the accumulated run time in seconds
Double_t GetTotalTimeInSeconds() const { return fTotalDuration; }

inline auto GetFilterStartTime() const { return fFilterStartTime; }
inline auto GetFilterEndTime() const { return fFilterEndTime; }
inline auto GetStartTime() const { return fStartTime; }
inline auto GetEndTime() const { return fEndTime; }
inline auto GetFilePattern() const { return fFilePattern; }
inline auto GetObservablesList() const { return fObservablesList; }
inline auto GetProcessObservablesList() const { return fProcessObservablesList; }
inline auto GetFilterMetadata() const { return fFilterMetadata; }
inline auto GetFilterContains() const { return fFilterContains; }
inline auto GetFilterGreaterThan() const { return fFilterGreaterThan; }
inline auto GetFilterLowerThan() const { return fFilterLowerThan; }
inline auto GetQuantity() const { return fQuantity; }
inline auto GetCut() const { return fCut; }

inline void SetFilePattern(const std::string& pattern) { fFilePattern = pattern; }

TRestDataSet& operator=(TRestDataSet& dS);
void Import(const std::string& fileName);
void Export(const std::string& filename);

ROOT::RDF::RNode MakeCut(const TRestCut* cut);

void PrintMetadata() override;
void Initialize() override;

void GenerateDataSet();

TRestDataSet();
TRestDataSet(const char* cfgFileName, const std::string& name = "");
~TRestDataSet();

ClassDefOverride(TRestDataSet, 1);
ClassDefOverride(TRestDataSet, 2);
};
#endif
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: Updating metadata structures
/// JuanAn García
///
/// \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
Loading