Skip to content

Commit 014c080

Browse files
committed
smart read json fields (start)
1 parent 95b2b92 commit 014c080

File tree

1 file changed

+82
-7
lines changed

1 file changed

+82
-7
lines changed

PWGHF/D2H/Macros/runMassFitter.C

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <stdexcept>
4545
#include <string> // std::string
4646
#include <tuple>
47+
#include <type_traits>
4748
#include <utility>
4849
#include <vector> // std::vector
4950

@@ -58,6 +59,16 @@ TFile* openFileWithNullptrCheck(const std::string& fileName, const std::string&
5859
template <typename T>
5960
T* getObjectWithNullPtrCheck(TFile* fileIn, const std::string& objectName);
6061

62+
template <typename T>
63+
T readJsonField(const Document& config, const std::string& fieldName, const T& defaultValue);
64+
65+
template <typename T>
66+
T readJsonField(const Document& config, const std::string& fieldName);
67+
68+
void readJsonVectorValues(std::vector<double>& vec, const Document& config, const std::string& fieldName);
69+
70+
void readJsonVectorHistogram(std::vector<double>& vec, const Document& config, const std::string& fileNameFieldName, const std::string& histoNameFieldName);
71+
6172
template <typename ValueType>
6273
void readArray(const Value& jsonArray, std::vector<ValueType>& output)
6374
{
@@ -94,13 +105,13 @@ void runMassFitter(const std::string& configFileName)
94105
config.ParseStream(is);
95106
fclose(configFile);
96107

97-
bool const isMc = config["IsMC"].GetBool();
98-
bool const writeSignalPar = config["WriteSignalPar"].GetBool();
99-
std::string const particleName = config["Particle"].GetString();
100-
std::string const collisionSystem = config["CollisionSystem"].GetString();
101-
std::string const inputFileName = config["InFileName"].GetString();
102-
std::string const reflFileName = config["ReflFileName"].GetString();
103-
TString outputFileName = config["OutFileName"].GetString();
108+
bool const isMc = readJsonField<bool>(config, "IsMC");
109+
bool const writeSignalPar = readJsonField<bool>(config, "WriteSignalPar", true);
110+
std::string const particleName = readJsonField<std::string>(config, "Particle");
111+
std::string const collisionSystem = readJsonField<std::string>(config, "CollisionSystem", "");
112+
std::string const inputFileName = readJsonField<std::string>(config, "InFileName");
113+
std::string const reflFileName = readJsonField<std::string>(config, "ReflFileName", "");
114+
TString outputFileName = readJsonField<std::string>(config, "OutFileName", "mInvFit.root");
104115

105116
std::vector<std::string> inputHistoName;
106117
std::vector<std::string> promptHistoName;
@@ -596,6 +607,70 @@ T* getObjectWithNullPtrCheck(TFile* fileIn, const std::string& objectName)
596607
return ptr;
597608
}
598609

610+
template <typename T>
611+
T getJsonValue(const Value& value)
612+
{
613+
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
614+
return value.GetString();
615+
} else if constexpr (std::is_same_v<std::decay_t<T>, bool>) {
616+
return value.GetBool();
617+
} else if constexpr (std::is_same_v<std::decay_t<T>, int>) {
618+
return value.GetInt();
619+
} else if constexpr (std::is_same_v<std::decay_t<T>, double>) {
620+
return value.GetDouble();
621+
}
622+
throw std::runtime_error("getJsonValue(): unsupported type!");
623+
return T();
624+
}
625+
626+
template <typename T>
627+
T readJsonField(const Document& config, const std::string& fieldName, const T& defaultValue)
628+
{
629+
if (!config.HasMember(fieldName.c_str())) {
630+
return defaultValue;
631+
}
632+
const auto& value = config[fieldName.c_str()];
633+
return getJsonValue<T>(value);
634+
}
635+
636+
template <typename T>
637+
T readJsonField(const Document& config, const std::string& fieldName)
638+
{
639+
if (!config.HasMember(fieldName.c_str())) {
640+
throw std::runtime_error("readJsonField(): missing field " + fieldName);
641+
}
642+
return readJsonField<T>(config, fieldName, T());
643+
}
644+
645+
void readJsonVectorValues(std::vector<double>& vec, const Document& config, const std::string& fieldName)
646+
{
647+
if (!vec.empty()) {
648+
throw std::runtime_error("readJsonVectorValues(): vector is not empty!");
649+
}
650+
if (config.HasMember(fieldName.c_str())) {
651+
const Value& jsonArray = config[fieldName.c_str()];
652+
readArray(jsonArray, vec);
653+
}
654+
}
655+
656+
void readJsonVectorHistogram(std::vector<double>& vec, const Document& config, const std::string& fileNameFieldName, const std::string& histoNameFieldName)
657+
{
658+
if (!vec.empty()) {
659+
throw std::runtime_error("readJsonVectorHistogram(): vector is not empty!");
660+
}
661+
const auto fileName = readJsonField<std::string>(config, fileNameFieldName);
662+
const auto histoName = readJsonField<std::string>(config, histoNameFieldName);
663+
if (fileName.empty() || histoName.empty()) {
664+
return;
665+
}
666+
TFile* inputFile = openFileWithNullptrCheck(fileName);
667+
TH1* histo = getObjectWithNullPtrCheck<TH1>(inputFile, histoName);
668+
for (int iBin = 1; iBin <= histo->GetNbinsX(); iBin++) {
669+
vec.push_back(histo->GetBinContent(iBin));
670+
}
671+
inputFile->Close();
672+
}
673+
599674
int main(int argc, const char* argv[])
600675
{
601676
if (argc == 1) {

0 commit comments

Comments
 (0)