Belle II Software  release-05-01-25
DQMCommonUtils.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Peter Kodys, Peter Kvasnicka *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <dqm/utilities/DQMCommonUtils.h>
12 
13 #include <framework/database/DBImportObjPtr.h>
14 #include <framework/database/IntervalOfValidity.h>
15 #include <framework/database/DBObjPtr.h>
16 
17 #include <TVectorT.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 
23 int DQMCommonUtils::SetFlag(int Type, int bin, const double* pars, double ratio, TH1F* hist, TH1F* refhist,
24  TH1I* flaghist)
25 {
26  int iret = 0;
27  float WarningLevel = 6.0;
28  float ErrorLevel = 10.0;
29  auto temp = std::unique_ptr<TH1F>(new TH1F("temp", "temp", hist->GetNbinsX(), hist->GetXaxis()->GetXmin(),
30  hist->GetXaxis()->GetXmax()));
31  double NEvents = 0;
32  double flagInt = 0;
33  double flagrInt = 0;
34  for (int j = 0; j < hist->GetNbinsX(); j++) {
35  double val = hist->GetBinContent(j + 1);
36  NEvents += val;
37  val = val / ratio;
38  temp->SetBinContent(j + 1, val);
39  flagInt += temp->GetBinContent(j + 1);
40  flagrInt += refhist->GetBinContent(j + 1);
41  }
42  if (NEvents < 100) { // not enough information for comparition
43  iret = -1;
44  flaghist->SetBinContent(bin + 1, -1);
45  return iret;
46  }
47  double flag = temp->GetMean();
48  double flagErr = temp->GetMeanError();
49  double flagRMS = temp->GetRMS();
50  double flagRMSErr = temp->GetRMSError();
51  double flagr = refhist->GetMean();
52  double flagrErr = refhist->GetMeanError();
53  double flagrRMS = refhist->GetRMS();
54  double flagrRMSErr = refhist->GetRMSError();
55  TString strDebugInfo = Form("Conditions for Flag--->\n source %f %f+-%f %f+-%f\n referen %f %f+-%f %f+-%f\n",
56  flagInt, flag, flagErr, flagRMS, flagRMSErr,
57  flagrInt, flagr, flagrErr, flagrRMS, flagrRMSErr
58  );
59  B2DEBUG(130, strDebugInfo.Data());
60  if (Type == 1) { // counts+mean+RMS use
61  if ((fabs(flag - flagr) > ErrorLevel * (flagErr + flagrErr)) ||
62  (fabs(flagRMS - flagrRMS) > ErrorLevel * (flagRMSErr + flagrRMSErr)) ||
63  (fabs(flagInt - flagrInt) > ErrorLevel * (sqrt(flagInt) + sqrt(flagrInt)))
64  ) {
65  flaghist->SetBinContent(bin + 1, 2);
66  } else if ((fabs(flag - flagr) > WarningLevel * (flagErr + flagrErr)) ||
67  (fabs(flagRMS - flagrRMS) > WarningLevel * (flagRMSErr + flagrRMSErr)) ||
68  (fabs(flagInt - flagrInt) > WarningLevel * (sqrt(flagInt) + sqrt(flagrInt)))
69  ) {
70  flaghist->SetBinContent(bin + 1, 1);
71  } else {
72  flaghist->SetBinContent(bin + 1, 0);
73  }
74  iret = 1;
75  } else if (Type == 2) { // counts use
76  if (fabs(flagInt - flagrInt) > ErrorLevel * (sqrt(flagInt) + sqrt(flagrInt))) {
77  flaghist->SetBinContent(bin + 1, 2);
78  } else if (fabs(flagInt - flagrInt) > WarningLevel * (sqrt(flagInt) + sqrt(flagrInt))) {
79  flaghist->SetBinContent(bin + 1, 1);
80  } else {
81  flaghist->SetBinContent(bin + 1, 0);
82  }
83  iret = 1;
84  } else if (Type == 3) { // mean use
85  if (fabs(flag - flagr) > ErrorLevel * (flagErr + flagrErr)) {
86  flaghist->SetBinContent(bin + 1, 2);
87  } else if (fabs(flag - flagr) > WarningLevel * (flagErr + flagrErr)) {
88  flaghist->SetBinContent(bin + 1, 1);
89  } else {
90  flaghist->SetBinContent(bin + 1, 0);
91  }
92  iret = 1;
93  } else if (Type == 4) { // RMS use
94  if (fabs(flagRMS - flagrRMS) > ErrorLevel * (flagRMSErr + flagrRMSErr)) {
95  flaghist->SetBinContent(bin + 1, 2);
96  } else if (fabs(flagRMS - flagrRMS) > WarningLevel * (flagRMSErr + flagrRMSErr)) {
97  flaghist->SetBinContent(bin + 1, 1);
98  } else {
99  flaghist->SetBinContent(bin + 1, 0);
100  }
101  iret = 1;
102  } else if (Type == 5) { // counts+mean use
103  if ((fabs(flag - flagr) > ErrorLevel * (flagErr + flagrErr)) ||
104  (fabs(flagInt - flagrInt) > ErrorLevel * (sqrt(flagInt) + sqrt(flagrInt)))
105  ) {
106  flaghist->SetBinContent(bin + 1, 2);
107  } else if ((fabs(flag - flagr) > WarningLevel * (flagErr + flagrErr)) ||
108  (fabs(flagInt - flagrInt) > WarningLevel * (sqrt(flagInt) + sqrt(flagrInt)))
109  ) {
110  flaghist->SetBinContent(bin + 1, 1);
111  } else {
112  flaghist->SetBinContent(bin + 1, 0);
113  }
114  iret = 1;
115  } else if (Type == 9) { // bin content use
116  flagInt = temp->GetBinContent(bin + 1);
117  flagrInt = refhist->GetBinContent(bin + 1);
118  if (fabs(flagInt - flagrInt) > ErrorLevel * (sqrt(flagInt) + sqrt(flagrInt))) {
119  flaghist->SetBinContent(bin + 1, 2);
120  } else if (fabs(flagInt - flagrInt) > WarningLevel * (sqrt(flagInt) + sqrt(flagrInt))) {
121  flaghist->SetBinContent(bin + 1, 1);
122  } else {
123  flaghist->SetBinContent(bin + 1, 0);
124  }
125  iret = 1;
126  } else if (Type == 10) {
127  float flag2 = refhist->Chi2Test(temp.get());
128  flaghist->SetBinContent(bin + 1, 0);
129  if (flag2 > pars[1])
130  flaghist->SetBinContent(bin + 1, 2);
131  if (flag2 > pars[0])
132  flaghist->SetBinContent(bin + 1, 1);
133  iret = 1;
134  } else if (Type == 100) {
135  flaghist->SetBinContent(bin + 1, 0);
136  iret = 1;
137  } else {
138  flaghist->SetBinContent(bin + 1, -3);
139  iret = -1;
140  }
141  strDebugInfo = Form("SetFlag---> %f, type %i\n", flaghist->GetBinContent(bin + 1), Type);
142  B2DEBUG(130, strDebugInfo.Data());
143  return iret;
144 }
145 
146 
147 int DQMCommonUtils::SetFlag(int Type, int bin, const double* pars, double ratio, TH1I* hist, TH1I* refhist,
148  TH1I* flaghist)
149 {
150  auto histF = std::unique_ptr<TH1F>(new TH1F("histF", "histF", hist->GetNbinsX(), hist->GetXaxis()->GetXmin(),
151  hist->GetXaxis()->GetXmax()));
152  auto refhistF = std::unique_ptr<TH1F>(new TH1F("refhistF", "refhistF", refhist->GetNbinsX(), refhist->GetXaxis()->GetXmin(),
153  refhist->GetXaxis()->GetXmax()));
154  for (int j = 0; j < hist->GetNbinsX(); j++) {
155  histF->SetBinContent(j + 1, hist->GetBinContent(j + 1));
156  refhistF->SetBinContent(j + 1, refhist->GetBinContent(j + 1));
157  }
158  int ret = SetFlag(Type, bin, pars, ratio, histF.get(), refhistF.get(), flaghist);
159  return ret;
160 }
161 
162 
163 void DQMCommonUtils::CreateDBHisto(TH1F* HistoDB)
164 {
165  IntervalOfValidity iov(0, 0, -1, -1);
166  TString Name = Form("%s_Ref", HistoDB->GetName());
167  DBImportObjPtr<TVectorD> DBHisto(Name.Data());
168  DBHisto.construct(HistoDB->GetNbinsX() + 3);
169  double* Content = new double[HistoDB->GetNbinsX() + 3];
170  Content[0] = HistoDB->GetNbinsX();
171  Content[1] = HistoDB->GetXaxis()->GetXmin();
172  Content[2] = HistoDB->GetXaxis()->GetXmax();
173  for (int i = 0; i < HistoDB->GetNbinsX(); i++) {
174  Content[i + 3] = HistoDB->GetBinContent(i + 1);
175  }
176  DBHisto->SetElements(Content);
177  DBHisto.import(iov);
178  delete [] Content;
179 }
180 
181 
182 void DQMCommonUtils::CreateDBHisto(TH1I* HistoDB)
183 {
184  IntervalOfValidity iov(0, 0, -1, -1);
185  TString Name = Form("%s_Ref", HistoDB->GetName());
186  DBImportObjPtr<TVectorD> DBHisto(Name.Data());
187  DBHisto.construct(HistoDB->GetNbinsX() + 3);
188  double* Content = new double[HistoDB->GetNbinsX() + 3];
189  Content[0] = HistoDB->GetNbinsX();
190  Content[1] = HistoDB->GetXaxis()->GetXmin();
191  Content[2] = HistoDB->GetXaxis()->GetXmax();
192  for (int i = 0; i < HistoDB->GetNbinsX(); i++) {
193  Content[i + 3] = HistoDB->GetBinContent(i + 1);
194  }
195  DBHisto->SetElements(Content);
196  DBHisto.import(iov);
197  delete [] Content;
198 }
199 
200 
201 void DQMCommonUtils::CreateDBHistoGroup(TH1F** HistoDB, int number)
202 {
203  IntervalOfValidity iov(0, 0, -1, -1);
204  TString Name = Form("%s_Ref", HistoDB[0]->GetName());
205  DBImportObjPtr<TVectorD> DBHisto(Name.Data());
206  DBHisto.construct(number * HistoDB[0]->GetNbinsX() + 3);
207  double* Content = new double[number * HistoDB[0]->GetNbinsX() + 3];
208  Content[0] = HistoDB[0]->GetNbinsX();
209  Content[1] = HistoDB[0]->GetXaxis()->GetXmin();
210  Content[2] = HistoDB[0]->GetXaxis()->GetXmax();
211  for (int j = 0; j < number; j++) {
212  for (int i = 0; i < HistoDB[j]->GetNbinsX(); i++) {
213  Content[j * HistoDB[j]->GetNbinsX() + i + 3] = HistoDB[j]->GetBinContent(i + 1);
214  }
215  }
216  DBHisto->SetElements(Content);
217  DBHisto.import(iov);
218  delete [] Content;
219 }
220 
221 
222 void DQMCommonUtils::CreateDBHistoGroup(TH1I** HistoDB, int number)
223 {
224  IntervalOfValidity iov(0, 0, -1, -1);
225  TString Name = Form("%s_Ref", HistoDB[0]->GetName());
226  DBImportObjPtr<TVectorD> DBHisto(Name.Data());
227  DBHisto.construct(number * HistoDB[0]->GetNbinsX() + 3);
228  double* Content = new double[number * HistoDB[0]->GetNbinsX() + 3];
229  Content[0] = HistoDB[0]->GetNbinsX();
230  Content[1] = HistoDB[0]->GetXaxis()->GetXmin();
231  Content[2] = HistoDB[0]->GetXaxis()->GetXmax();
232  for (int j = 0; j < number; j++) {
233  for (int i = 0; i < HistoDB[j]->GetNbinsX(); i++) {
234  Content[j * HistoDB[j]->GetNbinsX() + i + 3] = HistoDB[j]->GetBinContent(i + 1);
235  }
236  }
237  DBHisto->SetElements(Content);
238  DBHisto.import(iov);
239  delete [] Content;
240 }
241 
242 
243 int DQMCommonUtils::LoadDBHisto(TH1F* HistoDB)
244 {
245  TString Name = Form("%s_Ref", HistoDB->GetName());
246  DBObjPtr<TVectorD> DBHisto(Name.Data());
247  int ret = 0;
248  if (DBHisto.isValid()) {
249  ret = 1;
250  if (HistoDB->GetNbinsX() != (int)DBHisto->GetMatrixArray()[0]) ret = 0;
251  if (HistoDB->GetXaxis()->GetXmin() != DBHisto->GetMatrixArray()[1]) ret = 0;
252  if (HistoDB->GetXaxis()->GetXmax() != DBHisto->GetMatrixArray()[2]) ret = 0;
253  if (ret) {
254  for (int i = 0; i < HistoDB->GetNbinsX(); i++) {
255  HistoDB->SetBinContent(i + 1, (int)DBHisto->GetMatrixArray()[i + 3]);
256  }
257  }
258  }
259  if (!ret) {
260  B2INFO("ERROR to open reference histogram: " << Name.Data());
261  }
262  return ret;
263 }
264 
265 
266 int DQMCommonUtils::LoadDBHisto(TH1I* HistoDB)
267 {
268  TString Name = Form("%s_Ref", HistoDB->GetName());
269  DBObjPtr<TVectorD> DBHisto(Name.Data());
270  int ret = 0;
271  if (DBHisto.isValid()) {
272  ret = 1;
273  if (HistoDB->GetNbinsX() != (int)DBHisto->GetMatrixArray()[0]) ret = 0;
274  if (HistoDB->GetXaxis()->GetXmin() != DBHisto->GetMatrixArray()[1]) ret = 0;
275  if (HistoDB->GetXaxis()->GetXmax() != DBHisto->GetMatrixArray()[2]) ret = 0;
276  if (ret) {
277  for (int i = 0; i < HistoDB->GetNbinsX(); i++) {
278  HistoDB->SetBinContent(i + 1, (int)DBHisto->GetMatrixArray()[i + 3]);
279  }
280  }
281  }
282  if (!ret) {
283  B2INFO("ERROR to open reference histogram: " << Name.Data());
284  }
285  return ret;
286 }
287 
288 
289 int DQMCommonUtils::LoadDBHistoGroup(TH1F** HistoDB, int number)
290 {
291  TString Name = Form("%s_Ref", HistoDB[0]->GetName());
292  DBObjPtr<TVectorD> DBHisto(Name.Data());
293  int ret = 0;
294  if (DBHisto.isValid()) {
295  ret = 1;
296  if (HistoDB[0]->GetNbinsX() != (int)DBHisto->GetMatrixArray()[0]) ret = 0;
297  if (HistoDB[0]->GetXaxis()->GetXmin() != DBHisto->GetMatrixArray()[1]) ret = 0;
298  if (HistoDB[0]->GetXaxis()->GetXmax() != DBHisto->GetMatrixArray()[2]) ret = 0;
299  for (int j = 0; j < number; j++) {
300  for (int i = 0; i < HistoDB[j]->GetNbinsX(); i++) {
301  HistoDB[j]->SetBinContent(i + 1, DBHisto->GetMatrixArray()[j * HistoDB[j]->GetNbinsX() + i + 3]);
302  }
303  }
304  }
305  if (!ret) {
306  B2INFO("ERROR to open reference histogram: " << Name.Data());
307  }
308  return ret;
309 }
310 
311 
312 int DQMCommonUtils::LoadDBHistoGroup(TH1I** HistoDB, int number)
313 {
314  TString Name = Form("%s_Ref", HistoDB[0]->GetName());
315  DBObjPtr<TVectorD> DBHisto(Name.Data());
316  int ret = 0;
317  if (DBHisto.isValid()) {
318  ret = 1;
319  if (HistoDB[0]->GetNbinsX() != (int)DBHisto->GetMatrixArray()[0]) ret = 0;
320  if (HistoDB[0]->GetXaxis()->GetXmin() != DBHisto->GetMatrixArray()[1]) ret = 0;
321  if (HistoDB[0]->GetXaxis()->GetXmax() != DBHisto->GetMatrixArray()[2]) ret = 0;
322  for (int j = 0; j < number; j++) {
323  for (int i = 0; i < HistoDB[j]->GetNbinsX(); i++) {
324  HistoDB[j]->SetBinContent(i + 1, DBHisto->GetMatrixArray()[j * HistoDB[j]->GetNbinsX() + i + 3]);
325  }
326  }
327  }
328  if (!ret) {
329  B2INFO("ERROR to open reference histogram: " << Name.Data());
330  }
331  return ret;
332 }
333 
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::DBImportObjPtr::construct
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Definition: DBImportObjPtr.h:57
Belle2::DBImportBase::import
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:38
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBImportObjPtr
Class for importing a single object to the database.
Definition: DBImportObjPtr.h:33
Belle2::DBAccessorBase::isValid
bool isValid() const
Check whether a valid object was obtained from the database.
Definition: DBAccessorBase.h:75