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

Dataset merge issue #487

Merged
merged 5 commits into from
Nov 6, 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
1 change: 1 addition & 0 deletions source/framework/core/inc/TRestDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class TRestDataSet : public TRestMetadata {
inline void SetQuantity(const std::map<std::string, RelevantQuantity>& quantity) { fQuantity = quantity; }

TRestDataSet& operator=(TRestDataSet& dS);
Bool_t Merge(const TRestDataSet& dS);
void Import(const std::string& fileName);
void Import(std::vector<std::string> fileNames);
void Export(const std::string& filename);
Expand Down
82 changes: 64 additions & 18 deletions source/framework/core/src/TRestDataSet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,35 @@ TRestDataSet& TRestDataSet::operator=(TRestDataSet& dS) {
fFilterEqualsTo = dS.GetFilterEqualsTo();
fQuantity = dS.GetQuantity();
fTotalDuration = dS.GetTotalTimeInSeconds();
fFileSelection = dS.GetFileSelection();
fCut = dS.GetCut();

return *this;
}

///////////////////////////////////////////////
/// \brief This function merge different TRestDataSet
/// metadata in current dataSet
///
Bool_t TRestDataSet::Merge(const TRestDataSet& dS) {
auto obsNames = GetObservablesList();
for (const auto& obs : fObservablesList) {
if (std::find(obsNames.begin(), obsNames.end(), obs) != obsNames.end()) {
RESTError << "Cannot merge dataSets with different observable list " << RESTendl;
return false;
}
}

if (fStartTime > dS.GetStartTime()) fStartTime = dS.GetStartTime();
if (fEndTime < dS.GetEndTime()) fEndTime = dS.GetEndTime();

auto fileSelection = dS.GetFileSelection();
fFileSelection.insert(fFileSelection.end(), fileSelection.begin(), fileSelection.end());

fTotalDuration += dS.GetTotalTimeInSeconds();

return true;
}

///////////////////////////////////////////////
/// \brief This function imports metadata from a root file
/// it import metadata info from the previous dataSet
Expand Down Expand Up @@ -956,25 +979,48 @@ void TRestDataSet::Import(std::vector<std::string> fileNames) {
return;
}

if (fileNames.size() == 0) return;

TFile* file = TFile::Open(fileNames[0].c_str(), "READ");
if (file != nullptr) {
TIter nextkey(file->GetListOfKeys());
TKey* key;
while ((key = (TKey*)nextkey())) {
std::string kName = key->GetClassName();
if (REST_Reflection::GetClassQuick(kName.c_str()) != nullptr &&
REST_Reflection::GetClassQuick(kName.c_str())->InheritsFrom("TRestDataSet")) {
TRestDataSet* dS = file->Get<TRestDataSet>(key->GetName());
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info)
dS->PrintMetadata();
*this = *dS;
int count = 0;
auto it = fileNames.begin();
while (it != fileNames.end()) {
std::string fileName = *it;
TFile* file = TFile::Open(fileName.c_str(), "READ");
bool isValid = false;
if (file != nullptr) {
TIter nextkey(file->GetListOfKeys());
TKey* key;
while ((key = (TKey*)nextkey())) {
std::string kName = key->GetClassName();
if (REST_Reflection::GetClassQuick(kName.c_str()) != nullptr &&
REST_Reflection::GetClassQuick(kName.c_str())->InheritsFrom("TRestDataSet")) {
TRestDataSet* dS = file->Get<TRestDataSet>(key->GetName());
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info)
dS->PrintMetadata();

if (count == 0) {
*this = *dS;
isValid = true;
} else {
isValid = Merge(*dS);
}

if (isValid) count++;
}
}
} else {
RESTError << "Cannot open " << fileName << RESTendl;
}
} else {
RESTError << "Cannot open " << fileNames[0] << RESTendl;
exit(1);

if (!isValid) {
RESTError << fileName << " is not a valid dataSet skipping..." << RESTendl;
it = fileNames.erase(it);
} else {
++it;
}
}

if (fileNames.empty()) {
RESTError << "File selection is empty, dataSet will not be imported " << RESTendl;
return;
}

RESTInfo << "Opening list of files. First file: " << fileNames[0] << RESTendl;
Expand Down