ndmspc  0.20250128.0
Results.cxx
1 #include <TFile.h>
2 #include <THnSparse.h>
3 #include <TH1.h>
4 #include "Results.h"
5 
6 #include "Core.h"
7 
9 ClassImp(Ndmspc::Results);
11 
12 namespace Ndmspc {
13 Results::Results() : TObject() {}
14 Results::~Results() {}
15 bool Results::LoadConfig(std::string configfilename, std::string userconfig, std::string environment,
16  std::string userConfigRaw)
17 {
18  bool configLoaded = Core::LoadConfig(configfilename, userconfig, environment, userConfigRaw);
19  if (!gCfg["ndmspc"]["data"]["histogram"]["enabled"].is_null() ||
20  gCfg["ndmspc"]["data"]["histogram"]["enabled"].is_boolean()) {
21  if (gCfg["ndmspc"]["data"]["histogram"]["enabled"].get<bool>()) fDataSource = DataSource::histogram;
22  }
23  std::string hostUrl = gCfg["ndmspc"]["output"]["host"].get<std::string>();
24  std::string path;
25  if (!hostUrl.empty()) path = hostUrl + "/";
26  path += gCfg["ndmspc"]["output"]["dir"].get<std::string>() + "/";
27 
28  for (auto & cut : gCfg["ndmspc"]["cuts"]) {
29  if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
30  path += cut["axis"].get<std::string>() + "_";
31  fCuts.push_back(cut["axis"].get<std::string>());
32  }
33 
34  path[path.size() - 1] = '/';
35 
36  path += environment + "/";
37 
38  fInputFileName = path + fResultFileName;
39 
40  return configLoaded;
41 }
42 void Results::Print(Option_t * option) const
43 {
47  // Printf("%s", gCfg.dump(2).c_str());
48 
49  int iAxis = 0;
50  for (auto & an : fAxes) {
51 
52  printf("axis: %15s(%s)\t[val=%d] \t[", an.c_str(), fAxesTypes[iAxis].c_str(), fPoint[iAxis]);
53  if (fAxesLabels.find(an) == fAxesLabels.end()) {
54  Printf("]");
55  continue;
56  }
57  int iLabel = 0;
58  for (auto & al : fAxesLabels.at(an)) {
59  if (al.empty()) continue;
60  if (fAxesLabels.at(an).back() == al) {
61  printf("%s(%d)", al.c_str(), iLabel + 1);
62  }
63  else {
64  printf("%s(%d),", al.c_str(), iLabel + 1);
65  }
66  iLabel++;
67  }
68 
69  Printf("]");
70  iAxis++;
71  }
72 }
73 
74 bool Results::LoadResults()
75 {
79  Printf("Opening file '%s' ...", fInputFileName.c_str());
80 
81  fInputFile = TFile::Open(fInputFileName.c_str());
82  if (!fInputFile) {
83 
84  Printf("Error: Input file '%s' was not found !!!", fInputFileName.c_str());
85  return false;
86  }
87 
88  fResultHnSparse = (THnSparse *)fInputFile->Get(fResultsHnSparseName.c_str());
89  if (!fResultHnSparse) {
90  Printf("Error: Results THnSparse histogram '%s' was not found !!!", fResultsHnSparseName.c_str());
91  return false;
92  }
93 
94  fMapAxesType = (TH1 *)fInputFile->Get(fMapAxesTypeName.c_str());
95  if (!fMapAxesType) {
96  Printf("Error: 'mapAxesType' histogram was not found !!!");
97  return false;
98  }
99 
100  int pointsSize = gCfg["ndmspc"]["result"]["axes"].size() + 1;
101  if (fDataSource == DataSource::histogram) pointsSize += gCfg["ndmspc"]["result"]["data"]["defaults"].size() + 1;
102  // int points[pointsSize];
103  // int iPoint = 0;
104  // points[iPoint++] = nAxisY;
105 
106  fPoint.clear();
107  fAxes.clear();
108  fAxesTypes.clear();
109  fAxesLabels.clear();
110  fAxesBinSizes.clear();
111  for (int iDim = 0; iDim < fResultHnSparse->GetNdimensions(); iDim++) {
112  TAxis * a = (TAxis *)fResultHnSparse->GetAxis(iDim);
113  if (a == nullptr) {
114  Printf("Error: Axis 'id=%d' was not found !!!", iDim);
115  return false;
116  }
117  fPoint.push_back(-1);
118  std::string axisName = a->GetName();
119  fAxes.push_back(axisName);
120  if (a->IsAlphanumeric()) {
121  for (int iLabel = 0; iLabel < a->GetNbins(); iLabel++) {
122  fAxesLabels[axisName].push_back(a->GetBinLabel(iLabel));
123  }
124  }
125  fAxesBinSizes[axisName] = a->GetNbins();
126  fAxesTypes.push_back(fMapAxesType->GetXaxis()->GetBinLabel(iDim + 1));
127  }
128 
129  if (fCurrentParameterName.empty()) {
130  int idxDefault = gCfg["ndmspc"]["result"]["parameters"]["default"].get<int>();
131  fCurrentParameterName = gCfg["ndmspc"]["result"]["parameters"]["labels"][idxDefault].get<std::string>();
132  }
133  Printf("Paremeter: %s", fCurrentParameterName.c_str());
134 
135  TAxis * a = (TAxis *)fResultHnSparse->GetListOfAxes()->FindObject(fParametesAxisName.c_str());
136  if (a == nullptr) {
137  Printf("Error: Axis '%s' was not found !!!", fParametesAxisName.c_str());
138  return false;
139  }
140  Int_t id = fResultHnSparse->GetListOfAxes()->IndexOf(a);
141  Int_t idBin = a->FindBin(fCurrentParameterName.c_str());
142  if (idBin < 0) {
143  Printf("Could not find bin label '%s' in '%s' axis !!!", fParametesAxisName.c_str(), fCurrentParameterName.c_str());
144  return false;
145  }
146 
147  Printf("Axis: %d [parameters] SetRange(%d,%d)", id, idBin, idBin);
148  fPoint[id] = idBin;
149  fResultHnSparse->GetAxis(id)->SetRange(idBin, idBin);
150 
151  // int iAxisStart = 1;
152  // json axesArray = gCfg["ndmspc"]["result"]["axes"];
153  // int idTmp;
154  // bool isDataSys = true;
155  // bool hasDataMc = false;
156  // for (int iAxis = iAxisStart; iAxis < fResultHnSparse->GetNdimensions(); iAxis++) {
157  // idBin = 1;
158  // std::string axisType = fMapAxesType->GetXaxis()->GetBinLabel(iAxis + 1);
159  // // Printf("Axis: %d [%s]", iAxis, axisType.c_str());
160  // if (!hasDataMc) hasDataMc = !axisType.compare("data");
161  // if (!axisType.compare("proj")) {
162  // isDataSys = false;
163  // fProjectionAxes.push_back(iAxis);
164  // }
165  // else if (!axisType.compare("sys") || !axisType.compare("data")) {
166  // if (isDataSys) {
167  // idTmp = iAxis - iAxisStart;
168  // idBin = gCfg["ndmspc"]["result"]["data"]["defaults"][idTmp].get<int>();
169  // }
170  // else {
171  // idTmp = iAxis - iAxisStart - fNDimCuts;
172  // if (histogramEnabled) idTmp -= gCfg["ndmspc"]["result"]["data"]["defaults"].size();
173  // // Printf("%s %d", axesArray.dump().c_str(), idTmp);
174  // idBin = axesArray[idTmp]["default"].get<int>() + 1;
175  // }
176  // }
177  // a = (TAxis *)fResultHnSparse->GetAxis(iAxis);
178  // Printf("Axis: %d [%s][%s] SetRange(%d,%d)", iAxis, axisType.c_str(), a->GetName(), idBin, idBin);
179  // points[iAxis] = a->GetNbins();
180  // fParameterPoint[iAxis] = idBin;
181  // fResultHnSparse->GetAxis(iAxis)->SetRange(idBin, idBin);
182  // std::string l = a->GetBinLabel(idBin);
183  // if (l.empty()) {
184  // fMapTitle += std::to_string(a->GetBinLowEdge(idBin));
185  // }
186  // else {
187  // fMapTitle += l;
188  // }
189  // fMapTitle += " ";
190  // }
191  // fMapTitle[fMapTitle.size() - 1] = ']';
192  //
193  // for (auto & p : fParameterPoint) {
194  // printf("%d ", p);
195  // }
196  // printf("\n");
197  //
198  // Printf("fMapTitle='%s'", fMapTitle.c_str());
199  GenerateTitle();
200  ApplyPoints();
201 
202  Print();
203  return true;
204 }
205 
206 bool Results::ApplyPoints()
207 {
211  Printf("Apply points ...");
212 
213  int iAxis = 0;
214  for (auto & p : fPoint) {
215  if (p < 0) {
216  // Printf("Warning: Point[%d] is %d !!! Setting it to 1", iAxis, p);
217  p = 1;
218  }
219  Printf("ApplyPoint : %s [%s] SetRange(%d,%d)", fAxes[iAxis].c_str(), fAxesTypes[iAxis].c_str(), p, p);
220  fResultHnSparse->GetAxis(iAxis)->SetRange(p, p);
221  iAxis++;
222  }
223 
224  return true;
225 }
226 
227 void Results::GenerateTitle()
228 {
232  fMapTitle = fCurrentParameterName + " [";
233 
234  TAxis * a;
235  for (int iAxis = 1; iAxis < fResultHnSparse->GetNdimensions(); iAxis++) {
236  a = (TAxis *)fResultHnSparse->GetAxis(iAxis);
237  std::string l = a->GetBinLabel(fPoint[iAxis]);
238  if (l.empty()) {
239  fMapTitle += std::to_string(fPoint[iAxis]);
240  }
241  else {
242  fMapTitle += l;
243  }
244  fMapTitle += " ";
245  }
246 
247  fMapTitle += "]";
248 
249  Printf("Map title: '%s'", fMapTitle.c_str());
250 }
251 
252 void Results::Draw(Option_t * option)
253 {
257 
258  Printf("Draw results ...");
259  LoadResults();
260 }
261 } // namespace Ndmspc
Results object.
Definition: Results.h:20