Skip to content

Commit 12f1add

Browse files
committed
smart read json fields (finish)
1 parent 014c080 commit 12f1add

File tree

1 file changed

+46
-90
lines changed

1 file changed

+46
-90
lines changed

PWGHF/D2H/Macros/runMassFitter.C

Lines changed: 46 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
#include <RtypesCore.h>
3939

4040
#include <cmath>
41-
#include <cstdio> // for fclose
41+
#include <cstdio>
4242
#include <functional>
4343
#include <map>
4444
#include <stdexcept>
45-
#include <string> // std::string
45+
#include <string>
4646
#include <tuple>
4747
#include <type_traits>
4848
#include <utility>
49-
#include <vector> // std::vector
49+
#include <vector>
5050

5151
#endif
5252

@@ -65,30 +65,13 @@ T readJsonField(const Document& config, const std::string& fieldName, const T& d
6565
template <typename T>
6666
T readJsonField(const Document& config, const std::string& fieldName);
6767

68-
void readJsonVectorValues(std::vector<double>& vec, const Document& config, const std::string& fieldName);
68+
template <typename T>
69+
void readJsonVector(std::vector<T>& vec, const Document& config, const std::string& fieldName, bool isRequired = false);
6970

7071
void readJsonVectorHistogram(std::vector<double>& vec, const Document& config, const std::string& fileNameFieldName, const std::string& histoNameFieldName);
7172

72-
template <typename ValueType>
73-
void readArray(const Value& jsonArray, std::vector<ValueType>& output)
74-
{
75-
for (const auto* it = jsonArray.Begin(); it != jsonArray.End(); it++) {
76-
auto value = it->template Get<ValueType>();
77-
output.emplace_back(value);
78-
}
79-
}
80-
81-
void parseStringArray(const Value& jsonArray, std::vector<std::string>& output)
82-
{
83-
size_t const arrayLength = jsonArray.Size();
84-
for (size_t i = 0; i < arrayLength; i++) {
85-
if (jsonArray[i].IsString()) {
86-
output.emplace_back(jsonArray[i].GetString());
87-
}
88-
}
89-
}
90-
9173
void divideCanvas(TCanvas* c, int nSliceVarBins);
74+
9275
void setHistoStyle(TH1* histo, Color_t color = kBlack, Size_t markerSize = 1);
9376

9477
void runMassFitter(const std::string& configFileName)
@@ -119,8 +102,6 @@ void runMassFitter(const std::string& configFileName)
119102
std::vector<std::string> reflHistoName;
120103
std::vector<std::string> promptSecPeakHistoName;
121104
std::vector<std::string> fdSecPeakHistoName;
122-
TString sliceVarName;
123-
TString sliceVarUnit;
124105
std::vector<double> sliceVarMin;
125106
std::vector<double> sliceVarMax;
126107
std::vector<double> massMin;
@@ -133,79 +114,49 @@ void runMassFitter(const std::string& configFileName)
133114
std::vector<int> bkgFuncConfig;
134115
std::vector<int> sgnFuncConfig;
135116

136-
const Value& inputHistoNameValue = config["InputHistoName"];
137-
parseStringArray(inputHistoNameValue, inputHistoName);
138-
139-
const Value& promptHistoNameValue = config["PromptHistoName"];
140-
parseStringArray(promptHistoNameValue, promptHistoName);
141-
142-
const Value& fdHistoNameValue = config["FDHistoName"];
143-
parseStringArray(fdHistoNameValue, fdHistoName);
117+
readJsonVector(inputHistoName, config, "InputHistoName", true);
118+
readJsonVector(promptHistoName, config, "PromptHistoName");
119+
readJsonVector(fdHistoName, config, "FDHistoName");
120+
readJsonVector(reflHistoName, config, "ReflHistoName");
121+
readJsonVector(promptSecPeakHistoName, config, "PromptSecPeakHistoName");
122+
readJsonVector(fdSecPeakHistoName, config, "FDSecPeakHistoName");
144123

145-
const Value& reflHistoNameValue = config["ReflHistoName"];
146-
parseStringArray(reflHistoNameValue, reflHistoName);
124+
const bool fixMean = readJsonField<bool>(config, "FixMean", false);
125+
const std::string meanFile = readJsonField<std::string>(config, "MeanFile", "");
126+
readJsonVector(fixMeanManual, config, "FixMeanManual");
147127

148-
const Value& promptSecPeakHistoNameValue = config["PromptSecPeakHistoName"];
149-
parseStringArray(promptSecPeakHistoNameValue, promptSecPeakHistoName);
128+
const bool fixSigma = readJsonField<bool>(config, "FixSigma", false);
129+
const std::string sigmaFile = readJsonField<std::string>(config, "SigmaFile", "");
130+
readJsonVector(fixSigmaManual, config, "FixSigmaManual");
150131

151-
const Value& fdSecPeakHistoNameValue = config["FDSecPeakHistoName"];
152-
parseStringArray(fdSecPeakHistoNameValue, fdSecPeakHistoName);
132+
const bool fixSecondSigma = readJsonField<bool>(config, "FixSecondSigma", false);
133+
const std::string secondSigmaFile = readJsonField<std::string>(config, "SecondSigmaFile", "");
134+
readJsonVector(fixSecondSigmaManual, config, "FixSecondSigmaManual");
153135

154-
const bool fixMean = config["FixMean"].GetBool();
155-
const std::string meanFile = config["MeanFile"].GetString();
136+
const bool fixFracDoubleGaus = readJsonField<bool>(config, "FixFracDoubleGaus", false);
137+
const std::string fracDoubleGausFile = readJsonField<std::string>(config, "FracDoubleGausFile", "");
138+
readJsonVector(fixFracDoubleGausManual, config, "FixFracDoubleGausManual");
156139

157-
const Value& fixMeanManualValue = config["FixMeanManual"];
158-
readArray(fixMeanManualValue, fixMeanManual);
140+
const TString sliceVarName = readJsonField<std::string>(config, "SliceVarName");
141+
const TString sliceVarUnit = readJsonField<std::string>(config, "SliceVarUnit");
159142

160-
const bool fixSigma = config["FixSigma"].GetBool();
161-
const std::string sigmaFile = config["SigmaFile"].GetString();
143+
readJsonVector(sliceVarMin, config, "SliceVarMin", true);
144+
readJsonVector(sliceVarMax, config, "SliceVarMax", true);
162145

163-
const Value& fixSigmaManualValue = config["FixSigmaManual"];
164-
readArray(fixSigmaManualValue, fixSigmaManual);
146+
readJsonVector(massMin, config, "MassMin", true);
147+
readJsonVector(massMax, config, "MassMax", true);
165148

166-
const bool fixSecondSigma = config["FixSecondSigma"].GetBool();
167-
const std::string secondSigmaFile = config["SecondSigmaFile"].GetString();
149+
readJsonVector(nRebin, config, "Rebin", true);
168150

169-
const Value& fixSecondSigmaManualValue = config["FixSecondSigmaManual"];
170-
readArray(fixSecondSigmaManualValue, fixSecondSigmaManual);
151+
bool const includeSecPeak = readJsonField<bool>(config, "InclSecPeak", false);
152+
bool const useLikelihood = readJsonField<bool>(config, "UseLikelihood");
171153

172-
const bool fixFracDoubleGaus = config["FixFracDoubleGaus"].GetBool();
173-
const std::string fracDoubleGausFile = config["FracDoubleGausFile"].GetString();
154+
readJsonVector(bkgFuncConfig, config, "BkgFunc", true);
155+
readJsonVector(sgnFuncConfig, config, "SgnFunc", true);
174156

175-
const Value& fixFracDoubleGausManualValue = config["FixFracDoubleGausManual"];
176-
readArray(fixFracDoubleGausManualValue, fixFracDoubleGausManual);
177-
178-
sliceVarName = config["SliceVarName"].GetString();
179-
sliceVarUnit = config["SliceVarUnit"].GetString();
180-
181-
const Value& sliceVarMinValue = config["SliceVarMin"];
182-
readArray(sliceVarMinValue, sliceVarMin);
183-
184-
const Value& sliceVarMaxValue = config["SliceVarMax"];
185-
readArray(sliceVarMaxValue, sliceVarMax);
186-
187-
const Value& massMinValue = config["MassMin"];
188-
readArray(massMinValue, massMin);
189-
190-
const Value& massMaxValue = config["MassMax"];
191-
readArray(massMaxValue, massMax);
192-
193-
const Value& rebinValue = config["Rebin"];
194-
readArray(rebinValue, nRebin);
195-
196-
bool const includeSecPeak = config["InclSecPeak"].GetBool();
197-
bool const useLikelihood = config["UseLikelihood"].GetBool();
198-
199-
const Value& bkgFuncValue = config["BkgFunc"];
200-
readArray(bkgFuncValue, bkgFuncConfig);
201-
202-
const Value& sgnFuncValue = config["SgnFunc"];
203-
readArray(sgnFuncValue, sgnFuncConfig);
204-
205-
const bool enableRefl = config["EnableRefl"].GetBool();
206-
207-
const bool drawBgPrefit = config["drawBgPrefit"].GetBool();
208-
const bool highlightPeakRegion = config["highlightPeakRegion"].GetBool();
157+
const bool enableRefl = readJsonField<bool>(config, "EnableRefl", false);
158+
const bool drawBgPrefit = readJsonField<bool>(config, "drawBgPrefit", true);
159+
const bool highlightPeakRegion = readJsonField<bool>(config, "highlightPeakRegion", true);
209160

210161
const int nSliceVarBins = static_cast<int>(sliceVarMin.size());
211162
std::vector<int> bkgFunc(nSliceVarBins);
@@ -642,14 +593,19 @@ T readJsonField(const Document& config, const std::string& fieldName)
642593
return readJsonField<T>(config, fieldName, T());
643594
}
644595

645-
void readJsonVectorValues(std::vector<double>& vec, const Document& config, const std::string& fieldName)
596+
template <typename T>
597+
void readJsonVector(std::vector<T>& vec, const Document& config, const std::string& fieldName, const bool isRequired)
646598
{
647599
if (!vec.empty()) {
648-
throw std::runtime_error("readJsonVectorValues(): vector is not empty!");
600+
throw std::runtime_error("readJsonVector(): vector is not empty!");
649601
}
650602
if (config.HasMember(fieldName.c_str())) {
651603
const Value& jsonArray = config[fieldName.c_str()];
652-
readArray(jsonArray, vec);
604+
for (auto it = jsonArray.Begin(); it != jsonArray.End(); it++) {
605+
vec.push_back(getJsonValue<T>(*it));
606+
}
607+
} else if (isRequired) {
608+
throw std::runtime_error("readJsonVector(): missing required field " + fieldName);
653609
}
654610
}
655611

0 commit comments

Comments
 (0)