Belle II Software  release-05-01-25
BackgroundInfo.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/dataobjects/BackgroundInfo.h>
12 #include <framework/logging/Logger.h>
13 #include <iostream>
14 #include <unordered_set>
15 
16 using namespace std;
17 using namespace Belle2;
18 
19 
20 bool BackgroundInfo::canBeMerged(const BackgroundInfo* otherObj)
21 {
22 
23  if (otherObj->getMethod() != m_method) {
24  B2ERROR("BackgroundInfo: objects cannot be merged (different method)");
25  return false;
26  }
27 
28  std::unordered_set<int> bgThis;
29  for (const auto& bg : m_backgrounds) {
30  int key = bg.tag * 16 + bg.fileType;
31  bgThis.emplace(key);
32  }
33  std::unordered_set<int> bgOther;
34  for (const auto& bg : otherObj->getBackgrounds()) {
35  int key = bg.tag * 16 + bg.fileType;
36  bgOther.emplace(key);
37  }
38  if (bgOther != bgThis) {
39  B2ERROR("BackgroundInfo: objects cannot be merged (different backgrounds)");
40  return false;
41  }
42 
43  auto compThis = m_components;
44  std::sort(compThis.begin(), compThis.end());
45  auto compOther = otherObj->getComponents();
46  std::sort(compOther.begin(), compOther.end());
47  if (compOther != compThis) {
48  B2ERROR("BackgroundInfo: objects cannot be merged (different components)");
49  return false;
50  }
51 
52  if (otherObj->getMinTime() != m_minTime) {
53  B2ERROR("BackgroundInfo: objects cannot be merged (different minTime)");
54  return false;
55  }
56 
57  if (otherObj->getMaxTime() != m_maxTime) {
58  B2ERROR("BackgroundInfo: objects cannot be merged (different maxTime)");
59  return false;
60  }
61 
62  if (otherObj->getMinTimeECL() != m_minTimeECL) {
63  B2ERROR("BackgroundInfo: objects cannot be merged (different minTimeECL)");
64  return false;
65  }
66 
67  if (otherObj->getMaxTimeECL() != m_maxTimeECL) {
68  B2ERROR("BackgroundInfo: objects cannot be merged (different maxTimeECL)");
69  return false;
70  }
71 
72  if (otherObj->getMinTimePXD() != m_minTimePXD) {
73  B2ERROR("BackgroundInfo: objects cannot be merged (different minTimePXD)");
74  return false;
75  }
76 
77  if (otherObj->getMaxTimePXD() != m_maxTimePXD) {
78  B2ERROR("BackgroundInfo: objects cannot be merged (different maxTimePXD)");
79  return false;
80  }
81 
82  if (otherObj->getWrapAround() != m_wrapAround) {
83  B2ERROR("BackgroundInfo: objects cannot be merged (different wrapAround)");
84  return false;
85  }
86 
87  if (otherObj->getMaxEdepECL() != m_maxEdepECL) {
88  B2ERROR("BackgroundInfo: objects cannot be merged (different maxEdepECL)");
89  return false;
90  }
91 
92  return true;
93 }
94 
95 
96 void BackgroundInfo::merge(const Mergeable* other)
97 {
98 
99  auto otherObj = static_cast<const BackgroundInfo*>(other);
100 
101  if (otherObj->getMethod() == c_Unknown and otherObj->getBackgrounds().empty())
102  return; // no merge for empty object
103 
104  if (m_method == c_Unknown and m_backgrounds.empty()) { // empty, replace it with other
105  *this = *otherObj;
106  return;
107  }
108 
109  if (!canBeMerged(otherObj)) throw BackgroundInfoNotMergeable();
110 
111  for (const auto& otherBg : otherObj->getBackgrounds()) {
112  bool added{false};
113  for (auto& bg : m_backgrounds) {
114  if (otherBg.tag != bg.tag) continue;
115  if (otherBg.fileType != bg.fileType) continue;
116  if (otherBg.fileNames == bg.fileNames) {
117  if (otherBg.scaleFactor != bg.scaleFactor) {
118  B2ERROR("BackgroundInfo: objects cannot be merged (different scaleFactor)");
119  throw BackgroundInfoNotMergeable();
120  }
121  bg.reused += otherBg.reused;
122  added = true;
123  break;
124  }
125  }
126  if (!added) m_backgrounds.push_back(otherBg);
127  }
128 
129 }
130 
131 void BackgroundInfo::clear()
132 {
133 
134  for (auto& bg : m_backgrounds) {
135  bg.reused = 0;
136  }
137 
138 }
139 
140 
141 void BackgroundInfo::print() const
142 {
143  switch (m_method) {
144  case c_Mixing:
145  cout << "Method: BG mixing" << endl;
146  printForMixing();
147  break;
148  case c_Overlay:
149  cout << "Method: BG overlay" << endl;
150  printForOverlay();
151  break;
152  default:
153  cout << "Method: unknown" << endl;
154  printForMixing();
155  }
156 }
157 
158 
159 void BackgroundInfo::printForMixing() const
160 {
161  cout << "Components: ";
162  if (m_components.empty()) {
163  cout << "all ";
164  } else {
165  for (const auto& component : m_components) cout << component << " ";
166  }
167  cout << endl;
168 
169  cout << "Time window: ";
170  cout << "PXD [" << m_minTimePXD << ", " << m_maxTimePXD << "] ns, ";
171  cout << "ECL [" << m_minTimeECL << ", " << m_maxTimeECL << "] ns, ";
172  cout << "other components [" << m_minTime << ", " << m_maxTime << "] ns";
173  cout << endl;
174 
175  cout << "Wrapping around: ";
176  if (m_wrapAround) {
177  cout << "enabled";
178  } else {
179  cout << "disabled";
180  }
181  cout << endl;
182 
183  cout << "ECL deposited energy cut: " << m_maxEdepECL << " GeV" << endl;
184 
185  cout << "Samples: " << endl;
186  for (const auto& bkg : m_backgrounds) {
187  cout << " " << bkg.type << endl;
188  cout << " equivalent time: " << bkg.realTime / 1000000 << " ms";
189  cout << ", events: " << bkg.numEvents;
190  cout << ", scale factor: " << bkg.scaleFactor;
191  cout << ", rate: " << bkg.rate << " GHz";
192  cout << ", re-used: " << bkg.reused << " times";
193  cout << endl;
194 
195  for (const auto& fileName : bkg.fileNames) cout << " " << fileName << endl;
196  cout << endl;
197 
198  }
199 
200 }
201 
202 void BackgroundInfo::printForOverlay() const
203 {
204  cout << "Components: ";
205  if (m_components.empty()) {
206  cout << "all ";
207  } else {
208  for (const auto& component : m_components) cout << component << " ";
209  }
210  cout << endl;
211 
212  cout << "Samples: " << endl;
213  for (const auto& bkg : m_backgrounds) {
214  cout << " " << bkg.type << endl;
215  cout << " events: " << bkg.numEvents;
216  cout << ", re-used: " << bkg.reused << " times";
217  cout << endl;
218 
219  for (const auto& fileName : bkg.fileNames) cout << " " << fileName << endl;
220  cout << endl;
221  }
222 }
223 
Belle2::BackgroundInfo::getMethod
EMethod getMethod() const
Returns method enum used to add BG.
Definition: BackgroundInfo.h:174
Belle2::BackgroundInfo::getMaxEdepECL
double getMaxEdepECL() const
Returns maximal alowed energy deposited in ECL to use BG events.
Definition: BackgroundInfo.h:234
Belle2::BackgroundInfo
This class stores the information about what background was mixed or overlayed.
Definition: BackgroundInfo.h:36
Belle2::BackgroundInfo::getMaxTimeECL
double getMaxTimeECL() const
Returns upper edge of ECL time window.
Definition: BackgroundInfo.h:210
Belle2::merge
std::vector< std::vector< double > > merge(std::vector< std::vector< std::vector< double >>> toMerge)
merge { vector<double> a, vector<double> b} into {a, b}
Definition: tools.h:44
Belle2::BackgroundInfo::getBackgrounds
const std::vector< BackgroundDescr > & getBackgrounds() const
Returns background descriptions.
Definition: BackgroundInfo.h:180
Belle2::BackgroundInfo::getMinTimePXD
double getMinTimePXD() const
Returns lower edge of PXD time window.
Definition: BackgroundInfo.h:216
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BackgroundInfo::getMinTime
double getMinTime() const
Returns lower edge of the narrow time window.
Definition: BackgroundInfo.h:192
Belle2::BackgroundInfo::getMaxTime
double getMaxTime() const
Returns upper edge of the narrow time window.
Definition: BackgroundInfo.h:198
Belle2::BackgroundInfo::getMinTimeECL
double getMinTimeECL() const
Returns lower edge of ECL time window.
Definition: BackgroundInfo.h:204
Belle2::BackgroundInfo::getWrapAround
bool getWrapAround() const
Returns wrap-around flag.
Definition: BackgroundInfo.h:228
Belle2::BackgroundInfo::getComponents
const std::vector< std::string > & getComponents() const
Returns included components.
Definition: BackgroundInfo.h:186
Belle2::Mergeable
Abstract base class for objects that can be merged.
Definition: Mergeable.h:33
Belle2::BackgroundInfo::getMaxTimePXD
double getMaxTimePXD() const
Returns upper edge of PXD time window.
Definition: BackgroundInfo.h:222