Skip to content

Commit 8dc9f22

Browse files
authored
Merge pull request #487 from rest-for-physics/dSMergeIssue
Dataset merge issue
2 parents 9c2fae7 + 8c48a98 commit 8dc9f22

2 files changed

Lines changed: 65 additions & 18 deletions

File tree

source/framework/core/inc/TRestDataSet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class TRestDataSet : public TRestMetadata {
169169
inline void SetQuantity(const std::map<std::string, RelevantQuantity>& quantity) { fQuantity = quantity; }
170170

171171
TRestDataSet& operator=(TRestDataSet& dS);
172+
Bool_t Merge(const TRestDataSet& dS);
172173
void Import(const std::string& fileName);
173174
void Import(std::vector<std::string> fileNames);
174175
void Export(const std::string& filename);

source/framework/core/src/TRestDataSet.cxx

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,35 @@ TRestDataSet& TRestDataSet::operator=(TRestDataSet& dS) {
894894
fFilterEqualsTo = dS.GetFilterEqualsTo();
895895
fQuantity = dS.GetQuantity();
896896
fTotalDuration = dS.GetTotalTimeInSeconds();
897-
fFileSelection = dS.GetFileSelection();
898897
fCut = dS.GetCut();
899898

900899
return *this;
901900
}
902901

902+
///////////////////////////////////////////////
903+
/// \brief This function merge different TRestDataSet
904+
/// metadata in current dataSet
905+
///
906+
Bool_t TRestDataSet::Merge(const TRestDataSet& dS) {
907+
auto obsNames = GetObservablesList();
908+
for (const auto& obs : fObservablesList) {
909+
if (std::find(obsNames.begin(), obsNames.end(), obs) != obsNames.end()) {
910+
RESTError << "Cannot merge dataSets with different observable list " << RESTendl;
911+
return false;
912+
}
913+
}
914+
915+
if (fStartTime > dS.GetStartTime()) fStartTime = dS.GetStartTime();
916+
if (fEndTime < dS.GetEndTime()) fEndTime = dS.GetEndTime();
917+
918+
auto fileSelection = dS.GetFileSelection();
919+
fFileSelection.insert(fFileSelection.end(), fileSelection.begin(), fileSelection.end());
920+
921+
fTotalDuration += dS.GetTotalTimeInSeconds();
922+
923+
return true;
924+
}
925+
903926
///////////////////////////////////////////////
904927
/// \brief This function imports metadata from a root file
905928
/// it import metadata info from the previous dataSet
@@ -956,25 +979,48 @@ void TRestDataSet::Import(std::vector<std::string> fileNames) {
956979
return;
957980
}
958981

959-
if (fileNames.size() == 0) return;
960-
961-
TFile* file = TFile::Open(fileNames[0].c_str(), "READ");
962-
if (file != nullptr) {
963-
TIter nextkey(file->GetListOfKeys());
964-
TKey* key;
965-
while ((key = (TKey*)nextkey())) {
966-
std::string kName = key->GetClassName();
967-
if (REST_Reflection::GetClassQuick(kName.c_str()) != nullptr &&
968-
REST_Reflection::GetClassQuick(kName.c_str())->InheritsFrom("TRestDataSet")) {
969-
TRestDataSet* dS = file->Get<TRestDataSet>(key->GetName());
970-
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info)
971-
dS->PrintMetadata();
972-
*this = *dS;
982+
int count = 0;
983+
auto it = fileNames.begin();
984+
while (it != fileNames.end()) {
985+
std::string fileName = *it;
986+
TFile* file = TFile::Open(fileName.c_str(), "READ");
987+
bool isValid = false;
988+
if (file != nullptr) {
989+
TIter nextkey(file->GetListOfKeys());
990+
TKey* key;
991+
while ((key = (TKey*)nextkey())) {
992+
std::string kName = key->GetClassName();
993+
if (REST_Reflection::GetClassQuick(kName.c_str()) != nullptr &&
994+
REST_Reflection::GetClassQuick(kName.c_str())->InheritsFrom("TRestDataSet")) {
995+
TRestDataSet* dS = file->Get<TRestDataSet>(key->GetName());
996+
if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info)
997+
dS->PrintMetadata();
998+
999+
if (count == 0) {
1000+
*this = *dS;
1001+
isValid = true;
1002+
} else {
1003+
isValid = Merge(*dS);
1004+
}
1005+
1006+
if (isValid) count++;
1007+
}
9731008
}
1009+
} else {
1010+
RESTError << "Cannot open " << fileName << RESTendl;
9741011
}
975-
} else {
976-
RESTError << "Cannot open " << fileNames[0] << RESTendl;
977-
exit(1);
1012+
1013+
if (!isValid) {
1014+
RESTError << fileName << " is not a valid dataSet skipping..." << RESTendl;
1015+
it = fileNames.erase(it);
1016+
} else {
1017+
++it;
1018+
}
1019+
}
1020+
1021+
if (fileNames.empty()) {
1022+
RESTError << "File selection is empty, dataSet will not be imported " << RESTendl;
1023+
return;
9781024
}
9791025

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

0 commit comments

Comments
 (0)