Skip to content

Commit bb493d8

Browse files
authored
Merge branch 'master' into runNumberLock
2 parents ae86957 + 6510b5e commit bb493d8

File tree

5 files changed

+84
-50
lines changed

5 files changed

+84
-50
lines changed

source/framework/analysis/src/TRestDataSetGainMap.cxx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ void TRestDataSetGainMap::GenerateGainMap() {
213213
/// \brief Function to calibrate a dataset with this gain map.
214214
///
215215
/// \param dataSetFileName the name of the root file where the TRestDataSet to be calibrated is stored.
216+
/// If the file is not a TRestDataSet, it will be treated as a file pattern for several TRestRun files
217+
/// to generate a temporary TRestDataSet with the needed observables.
216218
/// \param outputFileName the name of the output (root) file where the calibrated TRestDataSet will be
217219
/// exported. If empty, the output file will be named as the input file plus the name of the
218220
/// TRestDataSetGainMap. E.g. "data/myDataSet.root" -> "data/myDataSet_<gmName>.root".
@@ -230,7 +232,17 @@ void TRestDataSetGainMap::CalibrateDataSet(const std::string& dataSetFileName, s
230232

231233
TRestDataSet dataSet;
232234
dataSet.EnableMultiThreading(true);
233-
dataSet.Import(dataSetFileName);
235+
236+
if (TRestTools::isDataSet(dataSetFileName)) {
237+
dataSet.Import(dataSetFileName);
238+
} else {
239+
RESTWarning << dataSetFileName << " is not a dataset. Generating a temporal one..." << RESTendl;
240+
// generate the dataset with the needed observables
241+
dataSet.SetFilePattern(dataSetFileName);
242+
dataSet.SetObservablesList({"*"}); // get all observables
243+
dataSet.GenerateDataSet();
244+
}
245+
234246
auto dataFrame = dataSet.GetDataFrame();
235247

236248
// Define a new column with the identifier (pmID) of the module for each row (event)
@@ -294,17 +306,9 @@ void TRestDataSetGainMap::CalibrateDataSet(const std::string& dataSetFileName, s
294306
}
295307

296308
// Export dataset. Exclude columns if requested.
297-
std::set<std::string> excludeCol; // vector with the explicit column names to be excluded
298309
auto columns = dataSet.GetDataFrame().GetColumnNames();
299-
// Get the columns to be excluded from the list of columns. It accepts wildcards "*" and "?"
300-
for (auto& eC : excludeColumns) {
301-
if (eC.find("*") != std::string::npos || eC.find("?") != std::string::npos) {
302-
for (auto& c : columns)
303-
if (MatchString(c, eC)) excludeCol.insert(c);
304-
} else if (std::find(columns.begin(), columns.end(), eC) != columns.end())
305-
excludeCol.insert(eC);
306-
}
307-
// Remove the calibObsName, calibObsNameFullSpc and pmIDname from the list of columns to be excluded
310+
std::set<std::string> excludeCol = TRestTools::GetMatchingStrings(columns, excludeColumns);
311+
// Never exclude the calibObsName, calibObsNameFullSpc and pmIDname
308312
excludeCol.erase(calibObsName);
309313
excludeCol.erase(calibObsNameFullSpc);
310314
excludeCol.erase(pmIDname);

source/framework/core/inc/TRestDataSet.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class TRestDataSet : public TRestMetadata {
6060
/// It contains a list of the observables that will be added to the final tree or exported file
6161
std::vector<std::string> fObservablesList; //<
6262

63-
/// It contains a list of the process where all observables should be added
64-
std::vector<std::string> fProcessObservablesList; //<
65-
6663
/// A list of metadata members where filters will be applied
6764
std::vector<std::string> fFilterMetadata; //<
6865

@@ -172,7 +169,6 @@ class TRestDataSet : public TRestMetadata {
172169
inline auto GetFilePattern() const { return fFilePattern; }
173170
inline auto GetObservablesList() const { return fObservablesList; }
174171
inline auto GetFileSelection() const { return fFileSelection; }
175-
inline auto GetProcessObservablesList() const { return fProcessObservablesList; }
176172
inline auto GetFilterMetadata() const { return fFilterMetadata; }
177173
inline auto GetFilterContains() const { return fFilterContains; }
178174
inline auto GetFilterGreaterThan() const { return fFilterGreaterThan; }
@@ -215,6 +211,6 @@ class TRestDataSet : public TRestMetadata {
215211
TRestDataSet(const char* cfgFileName, const std::string& name = "");
216212
~TRestDataSet();
217213

218-
ClassDefOverride(TRestDataSet, 8);
214+
ClassDefOverride(TRestDataSet, 9);
219215
};
220216
#endif

source/framework/core/src/TRestDataSet.cxx

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@
228228
/// not exactly the same, then a warning output message will be prompted.
229229
/// - **last**: It will simply register the value of the metadata member
230230
/// from the last file in the list of selected files.
231+
/// - **append**: It will append the metadata member value to the previous
232+
/// value. The user can specify the appender string after the `append`
233+
/// keyword. If no appender is given, then an empty string will be used.
234+
/// It will check if the value is already in the string and not append it
235+
/// if it is. Use _extend_ if you want to force the append.
236+
/// - **extend**: same as _append_ but it will not check if the value is
237+
/// already in the string.
231238
///
232239
/// ### Adding a new column based on relevant quantities
233240
///
@@ -352,29 +359,14 @@ void TRestDataSet::GenerateDataSet() {
352359

353360
///// Disentangling process observables --> producing finalList
354361
TRestRun run(fFileSelection.front());
355-
std::vector<std::string> finalList;
356-
finalList.push_back("runOrigin");
357-
finalList.push_back("eventID");
358-
finalList.push_back("timeStamp");
362+
std::set<std::string> finalList;
363+
finalList.insert("runOrigin");
364+
finalList.insert("eventID");
365+
finalList.insert("timeStamp");
359366

360367
auto obsNames = run.GetAnalysisTree()->GetObservableNames();
361-
for (const auto& obs : fObservablesList) {
362-
if (std::find(obsNames.begin(), obsNames.end(), obs) != obsNames.end()) {
363-
finalList.push_back(obs);
364-
} else {
365-
RESTWarning << " Observable " << obs << " not found in observable list, skipping..." << RESTendl;
366-
}
367-
}
368-
369-
for (const auto& name : obsNames) {
370-
for (const auto& pcs : fProcessObservablesList) {
371-
if (name.find(pcs) == 0) finalList.push_back(name);
372-
}
373-
}
374-
375-
// Remove duplicated observables if any
376-
std::sort(finalList.begin(), finalList.end());
377-
finalList.erase(std::unique(finalList.begin(), finalList.end()), finalList.end());
368+
auto obsFromList = TRestTools::GetMatchingStrings(obsNames, fObservablesList);
369+
finalList.insert(obsFromList.begin(), obsFromList.end());
378370

379371
if (fMT)
380372
ROOT::EnableImplicitMT();
@@ -390,11 +382,11 @@ void TRestDataSet::GenerateDataSet() {
390382
// Adding new user columns added to the dataset
391383
for (const auto& [cName, cExpression] : fColumnNameExpressions) {
392384
RESTInfo << "Adding column to dataset: " << cName << RESTendl;
393-
finalList.emplace_back(cName);
385+
finalList.emplace(cName);
394386
fDataFrame = DefineColumn(cName, cExpression);
395387
}
396388

397-
RegenerateTree(finalList);
389+
RegenerateTree(std::vector<std::string>(finalList.begin(), finalList.end()));
398390

399391
RESTInfo << " - Dataset generated!" << RESTendl;
400392
}
@@ -513,6 +505,26 @@ std::vector<std::string> TRestDataSet::FileSelection() {
513505
}
514506

515507
if (properties.strategy == "last") properties.value = value;
508+
509+
if (properties.strategy.find("append") != std::string::npos) {
510+
// Get appender from "append" til the end of string. E.g. "append," -> "," or "append" -> ""
511+
std::string appender = properties.strategy.substr(properties.strategy.find("append") + 6);
512+
if (properties.value.empty())
513+
properties.value = value;
514+
else
515+
// do not append if value already contains the value to append
516+
if (properties.value.find(value) == std::string::npos)
517+
properties.value += appender + value;
518+
}
519+
520+
if (properties.strategy.find("extend") != std::string::npos) {
521+
// Get appender from "extend" til the end of string. E.g. "extend," -> "," or "extend" -> ""
522+
std::string appender = properties.strategy.substr(properties.strategy.find("extend") + 6);
523+
if (properties.value.empty())
524+
properties.value = value;
525+
else
526+
properties.value += appender + value;
527+
}
516528
}
517529

518530
if (run.GetStartTimestamp() < fStartTime) fStartTime = run.GetStartTimestamp();
@@ -645,21 +657,13 @@ void TRestDataSet::PrintMetadata() {
645657
RESTMetadata << " " << RESTendl;
646658

647659
if (!fObservablesList.empty()) {
648-
RESTMetadata << " Single observables added:" << RESTendl;
660+
RESTMetadata << " Observables added:" << RESTendl;
649661
RESTMetadata << " -------------------------" << RESTendl;
650662
for (const auto& l : fObservablesList) RESTMetadata << " - " << l << RESTendl;
651663

652664
RESTMetadata << " " << RESTendl;
653665
}
654666

655-
if (!fProcessObservablesList.empty()) {
656-
RESTMetadata << " Process observables added: " << RESTendl;
657-
RESTMetadata << " -------------------------- " << RESTendl;
658-
for (const auto& l : fProcessObservablesList) RESTMetadata << " - " << l << RESTendl;
659-
660-
RESTMetadata << " " << RESTendl;
661-
}
662-
663667
if (!fFilterMetadata.empty()) {
664668
RESTMetadata << " Metadata filters: " << RESTendl;
665669
RESTMetadata << " ----------------- " << RESTendl;
@@ -784,7 +788,10 @@ void TRestDataSet::InitFromConfigFile() {
784788

785789
std::vector<std::string> obsList = REST_StringHelper::Split(observables, ",");
786790

787-
for (const auto& l : obsList) fProcessObservablesList.push_back(l);
791+
for (const auto& l : obsList) {
792+
std::string processObsPattern = l + "_*";
793+
fObservablesList.push_back(processObsPattern);
794+
}
788795

789796
obsProcessDefinition = GetNextElement(obsProcessDefinition);
790797
}
@@ -1006,7 +1013,6 @@ TRestDataSet& TRestDataSet::operator=(TRestDataSet& dS) {
10061013
fFilePattern = dS.GetFilePattern();
10071014
fObservablesList = dS.GetObservablesList();
10081015
fFileSelection = dS.GetFileSelection();
1009-
fProcessObservablesList = dS.GetProcessObservablesList();
10101016
fFilterMetadata = dS.GetFilterMetadata();
10111017
fFilterContains = dS.GetFilterContains();
10121018
fFilterGreaterThan = dS.GetFilterGreaterThan();

source/framework/tools/inc/TRestTools.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <map>
3131
#include <memory>
32+
#include <set>
3233
#include <string>
3334
#include <vector>
3435

@@ -85,6 +86,8 @@ class TRestTools {
8586
static std::string GetFileNameRoot(const std::string& fullname);
8687
static std::vector<std::string> GetObservablesInString(const std::string& observablesStr,
8788
bool removeDuplicates = true);
89+
static std::set<std::string> GetMatchingStrings(const std::vector<std::string>& stack,
90+
const std::vector<std::string>& wantedStrings);
8891

8992
static int GetBinaryFileColumns(std::string fname);
9093

source/framework/tools/src/TRestTools.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,31 @@ std::vector<std::string> TRestTools::GetObservablesInString(const std::string& o
861861
return obsList;
862862
}
863863

864+
////////////////////////////////////////////////////////////
865+
/// \brief Returns a set of strings that match the wanted strings from the stack.
866+
/// The wanted strings can contain wildcards "*" and "?".
867+
/// \param stack: vector of strings to be searched
868+
/// \param wantedStrings: vector of strings with the wanted strings to be matched.
869+
/// \return a set of strings that match the wanted strings
870+
/// e.g.
871+
/// Input: stack = {"x1", "x2", "x11", "y1", "y2", "y11", "z1", "z2"},
872+
/// wantedStrings = {"x*", "y?", "z1"},
873+
/// Output: {"x1", "x11", "x2", "y1", "y2", "z1"}
874+
///
875+
std::set<std::string> TRestTools::GetMatchingStrings(const std::vector<std::string>& stack,
876+
const std::vector<std::string>& wantedStrings) {
877+
std::set<std::string> result;
878+
for (auto& ws : wantedStrings) {
879+
if (ws.find("*") != std::string::npos || ws.find("?") != std::string::npos) {
880+
for (auto& c : stack)
881+
if (MatchString(c, ws)) result.insert(c);
882+
} else if (std::find(stack.begin(), stack.end(), ws) != stack.end())
883+
result.insert(ws);
884+
}
885+
// return std::vector<std::string>(result.begin(), result.end()); // convert to vector
886+
return result;
887+
}
888+
864889
///////////////////////////////////////////////
865890
/// \brief Returns the input string but without multiple slashes ("/")
866891
///

0 commit comments

Comments
 (0)