NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
UnfoldTool.cxx
Go to the documentation of this file.
1#include "NAGASH/UnfoldTool.h"
2
3using namespace NAGASH;
4
5UnfoldTool::UnfoldTool(std::shared_ptr<MSGTool> msg, int num)
6 : Tool(msg)
7{
8 binnum = num;
9
10 Data_Reco = std::vector<double>(binnum, 0);
11 Data_Reco_Correct = std::vector<double>(binnum, 0);
12 Theory_Truth = std::vector<double>(binnum, 0);
13 Theory_Truth_Correct = std::vector<double>(binnum, 0);
14 Data_Truth = std::vector<double>(binnum, 0);
15 Data_Truth_Correct = std::vector<double>(binnum, 0);
16 Data_Reco_Estimated = std::vector<double>(binnum, 0);
17 Data_Truth_Estimated = std::vector<double>(binnum, 0);
18
19 IsMeasured = std::vector<bool>(binnum, false);
20 IsSet = std::vector<bool>(binnum, false);
21
22 R_Matrix = std::vector<std::vector<double>>(binnum, std::vector<double>(binnum, 0));
23 M_Matrix = std::vector<std::vector<double>>(binnum, std::vector<double>(binnum, 0));
24 MatrixIsSet = std::vector<std::vector<bool>>(binnum, std::vector<bool>(binnum, 0));
25
26 FiducialCorrection = std::vector<double>(binnum, 0);
27 EfficiencyCorrection = std::vector<double>(binnum, 0);
28 FCorrectionIsSet = std::vector<bool>(binnum, false);
29 ECorrectionIsSet = std::vector<bool>(binnum, false);
30
31 ReweightFactor = std::vector<double>(binnum, 0);
32
33 Data_Truth_Correct_EachItr = std::vector<std::vector<double>>(binnum);
34
35 for (int i = 0; i < binnum; i++)
36 for (int j = 0; j < binnum; j++)
37 MatrixIsSet[i][j] = false;
38}
39
41{
42 for (int i = 0; i < binnum; i++)
43 {
44 IsMeasured[i] = false;
45 IsSet[i] = false;
46 FCorrectionIsSet[i] = false;
47 ECorrectionIsSet[i] = false;
48 for (int j = 0; j < binnum; j++)
49 MatrixIsSet[i][j] = false;
51 }
52}
53
54void UnfoldTool::SetData(int index, double value)
55{
56 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::SetData");
57 if (index < 0)
58 {
59 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
60 return;
61 }
62
63 if (index >= binnum)
64 {
65 MSGUser()->MSG_ERROR("Index larger than binnum");
66 return;
67 }
68
69 MSGUser()->MSG_DEBUG("The value of No.", index, " bin for Reco-Data is set to ", value);
70 Data_Reco[index] = value;
71 IsMeasured[index] = true;
72}
73
74void UnfoldTool::SetTheory(int index, double value)
75{
76 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::SetTheory");
77 if (index < 0)
78 {
79 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
80 return;
81 }
82
83 if (index >= binnum)
84 {
85 MSGUser()->MSG_ERROR("Index larger than binnum");
86 return;
87 }
88
89 MSGUser()->MSG_DEBUG("The value of No.", index, " bin for Truth-MC is set to ", value);
90 Theory_Truth[index] = value;
91 IsSet[index] = true;
92}
93
94void UnfoldTool::SetMatrix(int indexi, int indexj, double value)
95{
96 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::SetMatrix");
97 if (indexi < 0)
98 {
99 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
100 return;
101 }
102
103 if (indexi >= binnum)
104 {
105 MSGUser()->MSG_ERROR("Index larger than binnum");
106 return;
107 }
108
109 if (indexj < 0)
110 {
111 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
112 return;
113 }
114
115 if (indexj >= binnum)
116 {
117 MSGUser()->MSG_ERROR("Index larger than binnum");
118 return;
119 }
120
121 MSGUser()->MSG_DEBUG("The value of No.", indexi, ",", indexj, " bin for R-Matrix is set to ", value);
122 R_Matrix[indexi][indexj] = value;
123 MatrixIsSet[indexi][indexj] = true;
124}
125
126void UnfoldTool::SetFiducialCorrection(int index, double value)
127{
128 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::SetFiducial");
129 if (index < 0)
130 {
131 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
132 return;
133 }
134
135 if (index >= binnum)
136 {
137 MSGUser()->MSG_ERROR("Index larger than binnum");
138 return;
139 }
140
141 MSGUser()->MSG_DEBUG("The value of No.", index, " bin for Fiducial is set to ", value);
142 FiducialCorrection[index] = value;
143 FCorrectionIsSet[index] = true;
144}
145
146void UnfoldTool::SetEfficiencyCorrection(int index, double value)
147{
148 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::SetEfficiency");
149 if (index < 0)
150 {
151 MSGUser()->MSG_ERROR("Negative index!!! Index should be positive!!!");
152 return;
153 }
154
155 if (index >= binnum)
156 {
157 MSGUser()->MSG_ERROR("Index larger than binnum");
158 return;
159 }
160
161 MSGUser()->MSG_DEBUG("The value of No.", index, " bin for Efficiency is set to ", value);
162
163 EfficiencyCorrection[index] = value;
164 ECorrectionIsSet[index] = true;
165}
166
168{
169 // Mij = Rji * Truthi / Recoj = Rji * Truthi / Sigmak(Rjk* Truthk)
170 for (int j = 0; j < binnum; j++)
171 {
172 double down = 0;
173 for (int k = 0; k < binnum; k++)
174 down = down + R_Matrix[j][k] * Theory_Truth_Correct[k];
175
176 for (int i = 0; i < binnum; i++)
177 M_Matrix[i][j] = R_Matrix[j][i] * Theory_Truth_Correct[i] / down;
178 }
179}
180
182{
183 for (int i = 0; i < binnum; i++)
184 {
185 if (IsMeasured[i])
186 continue;
187
188 Data_Reco_Estimated[i] = 0;
189 for (int j = 0; j < binnum; j++)
191 }
192}
193
195{
196 for (int i = 0; i < binnum; i++)
197 {
198 if (IsMeasured[i])
199 {
201 for (int j = 0; j < binnum; j++)
202 if (IsMeasured[j])
204 else
206
209 }
210 }
211}
212
214{
215 for (int i = 0; i < binnum; i++)
216 {
217 if (IsMeasured[i])
219 }
220}
221
223{
224 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::PrintResult");
225 MSGUser()->MSG_DEBUG("Binnum Data Theory Original-Data");
226 for (int i = 0; i < binnum; i++)
227 if (IsMeasured[i])
228 MSGUser()->MSG_DEBUG(i, " ", Data_Truth_Correct[i], " ", Theory_Truth[i], " ", Data_Reco[i]);
229 else
230 MSGUser()->MSG_DEBUG(i, " Fixed to Theory ", Theory_Truth[i], " ", Data_Reco_Estimated[i] / FiducialCorrection[i]);
231
232 for (int i = 0; i < binnum; i++)
233 if (IsMeasured[i])
235 else
236 Data_Truth_Correct_EachItr[i].emplace_back(Theory_Truth[i]);
237}
238
239void UnfoldTool::BayesUnfold(int iteration)
240{
241 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::BayesUnfold");
242 if (!Check())
243 {
244 MSGUser()->MSG_ERROR("Check Failed");
245 return;
246 }
247
248 for (int i = 0; i < binnum; i++)
249 {
250 if (IsMeasured[i])
253 }
254
255 bool SaveLess = false;
256 if (iteration > 10000)
257 {
258 SaveLess = true;
259 MSGUser()->MSG_DEBUG("Too many iterations, save only 1, 10, 100, ... iteration");
260 }
261
262 int saveitr = 1;
263 for (int i = 0; i < iteration; i++)
264 {
265 MSGUser()->MSG_DEBUG("Iteration ", i, " Start");
266 BayesMatrix();
269 RenewTheory();
270
271 if (i == saveitr && SaveLess)
272 {
273 PrintResult();
274 saveitr = saveitr * 10;
275 }
276 else if (!SaveLess)
277 PrintResult();
278
279 MSGUser()->MSG_DEBUG("Iteration ", i, " End");
280 }
281}
282
284{
285 for (int i = 0; i < binnum; i++)
286 {
287 Data_Reco_Estimated[i] = 0;
288 for (int j = 0; j < binnum; j++)
290 }
291
292 for (int i = 0; i < binnum; i++)
294}
295
305
306void UnfoldTool::ReweightUnfold(int &iteration)
307{
308 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::ReweightUnfold");
309 if (!Check())
310 {
311 MSGUser()->MSG_ERROR("Check Failed");
312 return;
313 }
314
315 // Initialize ReweightFactor
316 for (int i = 0; i < binnum; i++)
317 ReweightFactor[i] = 1;
318
319 for (int i = 0; i < binnum; i++)
320 {
321 if (IsMeasured[i])
324 }
325
326 bool keepdoing = false;
327 int reweight_count = 0;
328 double chi2 = 0;
329 do
330 {
331 keepdoing = false;
334
335 reweight_count++;
336
337 chi2 = 0;
338 for (int j = 0; j < binnum; j++)
339 {
342 keepdoing = true;
343 MSGUser()->MSG_DEBUG(Data_Reco[j], " ",
345 Data_Reco_Correct[j], " ", Data_Reco_Estimated[j], " ",
346 ReweightFactor[j], " ",
347 Theory_Truth_Correct[j], " ",
350
351 if (reweight_count > 2000)
352 keepdoing = false;
353 }
354 MSGUser()->MSG_DEBUG("Reweight No.", reweight_count, " Chi-square: ", chi2);
355 RenewTheory();
356 } while (keepdoing);
357
358 MSGUser()->MSG_DEBUG("Reweight No.", reweight_count, " Chi-square: ", chi2);
359 PrintResult();
360 iteration = reweight_count;
361}
362
364{
365 // Check every thing need is set
366 for (int i = 0; i < binnum; i++)
367 if (IsSet[i] == false || FCorrectionIsSet[i] == false || ECorrectionIsSet[i] == false)
368 return false;
369
370 for (int i = 0; i < binnum; i++)
371 for (int j = 0; j < binnum; j++)
372 if (MatrixIsSet[i][j] == false)
373 return false;
374
375 return true;
376}
377
379{
380 for (int i = 0; i < Data_Truth_Correct_EachItr[0].size(); i++)
381 {
382 std::cout << "Iteration No." << i << " ";
383 for (int j = 0; j < binnum; j++)
384 std::cout << Data_Truth_Correct_EachItr[j][i] << " ";
385 std::cout << std::endl;
386 }
387}
388
389void UnfoldTool::GetResult(int itration, TH1D *result)
390{
391 auto guard = MSGUser()->StartTitleWithGuard("UnfoldTool::GetResult");
392 if (result == nullptr)
393 {
394 MSGUser()->MSG_ERROR("Hist for result is not set!!!");
395 return;
396 }
397
398 if (result->GetNbinsX() < binnum)
399 {
400 MSGUser()->MSG_ERROR("Not enought bins for the result!!!");
401 return;
402 }
403
404 for (int i = 0; i < binnum; i++)
405 {
406 result->SetBinContent(i + 1, Data_Truth_Correct_EachItr[i][itration]);
407 result->SetBinError(i + 1, 0);
408 }
409}
Provide interface for all tools in NAGASH.
Definition Tool.h:72
std::shared_ptr< MSGTool > MSGUser()
return the MSGTool inside.
Definition Tool.h:91
std::vector< bool > ECorrectionIsSet
Definition UnfoldTool.h:29
std::vector< bool > FCorrectionIsSet
Definition UnfoldTool.h:28
void BayesUnfold(int iteration)
std::vector< double > ReweightFactor
Definition UnfoldTool.h:31
std::vector< double > FiducialCorrection
Definition UnfoldTool.h:26
void EstimateReweightFactor()
std::vector< bool > IsSet
Definition UnfoldTool.h:20
std::vector< std::vector< double > > R_Matrix
Definition UnfoldTool.h:22
std::vector< double > Data_Reco_Correct
Definition UnfoldTool.h:11
void SetMatrix(int indexi, int indexj, double value)
void SetData(int index, double value)
std::vector< double > Data_Truth_Correct
Definition UnfoldTool.h:15
void SetFiducialCorrection(int index, double value)
std::vector< std::vector< bool > > MatrixIsSet
Definition UnfoldTool.h:24
std::vector< double > Data_Reco_Estimated
Definition UnfoldTool.h:16
std::vector< std::vector< double > > Data_Truth_Correct_EachItr
Definition UnfoldTool.h:35
std::vector< double > EfficiencyCorrection
Definition UnfoldTool.h:27
void ReweightUnfold(int &iteration)
void SetEfficiencyCorrection(int index, double value)
std::vector< double > Data_Reco
Definition UnfoldTool.h:10
std::vector< double > Theory_Truth
Definition UnfoldTool.h:12
std::vector< double > Data_Truth_Estimated
Definition UnfoldTool.h:17
std::vector< bool > IsMeasured
Definition UnfoldTool.h:19
std::vector< double > Theory_Truth_Correct
Definition UnfoldTool.h:13
std::vector< double > Data_Truth
Definition UnfoldTool.h:14
void SetTheory(int index, double value)
UnfoldTool(std::shared_ptr< MSGTool > msg, int num)
Definition UnfoldTool.cxx:5
void GetResult(int itration, TH1D *result)
std::vector< std::vector< double > > M_Matrix
Definition UnfoldTool.h:23