CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MEtoEDMFormat.h
Go to the documentation of this file.
1 #ifndef MEtoEDMFormat_h
2 #define MEtoEDMFormat_h
3 
12 #include <TObject.h>
13 #include <TH1F.h>
14 #include <TH1S.h>
15 #include <TH1D.h>
16 #include <TH2F.h>
17 #include <TH2S.h>
18 #include <TH2D.h>
19 #include <TH3F.h>
20 #include <TProfile.h>
21 #include <TProfile2D.h>
22 #include <TObjString.h>
23 #include <TString.h>
24 #include <THashList.h>
25 #include <TList.h>
26 
27 #include <iostream>
28 #include <string>
29 #include <vector>
30 #include <memory>
31 #include <map>
32 #include <cstdint>
33 
34 #define METOEDMFORMAT_DEBUG 0
35 
36 template <class T>
37 class MEtoEDM {
38 public:
39  MEtoEDM() {}
40  explicit MEtoEDM(size_t reservedSize) { MEtoEdmObject.reserve(reservedSize); }
41  virtual ~MEtoEDM() {}
42 
43  typedef std::vector<uint32_t> TagList;
44 
45  struct MEtoEDMObject {
49  };
50 
51  typedef std::vector<MEtoEDMObject> MEtoEdmObjectVector;
52 
53  void putMEtoEdmObject(const std::string &name, const T &object) {
55  MEtoEdmObject.push_back(temp);
56  MEtoEdmObject.back().name = name;
57  MEtoEdmObject.back().object = object;
58  }
59 
61 
62  bool CheckBinLabels(const TAxis *a1, const TAxis *a2) {
63  // check that axis have same labels
64  THashList *l1 = (const_cast<TAxis *>(a1))->GetLabels();
65  THashList *l2 = (const_cast<TAxis *>(a2))->GetLabels();
66 
67  if (!l1 && !l2)
68  return true;
69  if (!l1 || !l2) {
70  return false;
71  }
72  // check now labels sizes are the same
73  if (l1->GetSize() != l2->GetSize()) {
74  return false;
75  }
76  for (int i = 1; i <= a1->GetNbins(); ++i) {
77  TString label1 = a1->GetBinLabel(i);
78  TString label2 = a2->GetBinLabel(i);
79  if (label1 != label2) {
80  return false;
81  }
82  }
83  return true;
84  }
85 
86  bool mergeProduct(const MEtoEDM<T> &newMEtoEDM) {
87  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
88  const size_t nObjects = newMEtoEDMObject.size();
89  // NOTE: we remember the present size since we will only add content
90  // from newMEtoEDMObject after this point
91  const size_t nOldObjects = MEtoEdmObject.size();
92 
93  // if the old and new are not the same size, we want to report a problem
94  if (nObjects != nOldObjects) {
95  std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
96  << nObjects << ", old=" << nOldObjects << std::endl;
97  }
98 
99  for (unsigned int i = 0; i < nObjects; ++i) {
100  unsigned int j = 0;
101  // see if the name is already in the old container up to the point where
102  // we may have added new entries in the container
103  const std::string &name = newMEtoEDMObject[i].name;
104  if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
105  j = i;
106  } else {
107  j = 0;
108  while (j < nOldObjects && (MEtoEdmObject[j].name != name))
109  ++j;
110  }
111  if (j >= nOldObjects) {
112  // this value is only in the new container, not the old one
113 #if METOEDMFORMAT_DEBUG
114  std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
115 #endif
116  MEtoEdmObject.push_back(newMEtoEDMObject[i]);
117  } else if (MEtoEdmObject[j].object.CanExtendAllAxes() && newMEtoEDMObject[i].object.CanExtendAllAxes()) {
118  TList list;
119  list.Add((TObject *)&newMEtoEDMObject[i].object);
120  if (MEtoEdmObject[j].object.Merge(&list) == -1) {
121  std::cout << "ERROR MEtoEDM::mergeProducts(): merge failed for '" << name << "'" << std::endl;
122  }
123  } else {
124  // this value is also in the new container: add the two
125  if (MEtoEdmObject[j].object.GetNbinsX() == newMEtoEDMObject[i].object.GetNbinsX() &&
126  MEtoEdmObject[j].object.GetXaxis()->GetXmin() == newMEtoEDMObject[i].object.GetXaxis()->GetXmin() &&
127  MEtoEdmObject[j].object.GetXaxis()->GetXmax() == newMEtoEDMObject[i].object.GetXaxis()->GetXmax() &&
128  MEtoEdmObject[j].object.GetNbinsY() == newMEtoEDMObject[i].object.GetNbinsY() &&
129  MEtoEdmObject[j].object.GetYaxis()->GetXmin() == newMEtoEDMObject[i].object.GetYaxis()->GetXmin() &&
130  MEtoEdmObject[j].object.GetYaxis()->GetXmax() == newMEtoEDMObject[i].object.GetYaxis()->GetXmax() &&
131  MEtoEdmObject[j].object.GetNbinsZ() == newMEtoEDMObject[i].object.GetNbinsZ() &&
132  MEtoEdmObject[j].object.GetZaxis()->GetXmin() == newMEtoEDMObject[i].object.GetZaxis()->GetXmin() &&
133  MEtoEdmObject[j].object.GetZaxis()->GetXmax() == newMEtoEDMObject[i].object.GetZaxis()->GetXmax() &&
134  CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetXaxis(),
135  (TAxis *)newMEtoEDMObject[i].object.GetXaxis()) &&
136  CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetYaxis(),
137  (TAxis *)newMEtoEDMObject[i].object.GetYaxis()) &&
138  CheckBinLabels((TAxis *)MEtoEdmObject[j].object.GetZaxis(),
139  (TAxis *)newMEtoEDMObject[i].object.GetZaxis())) {
140  MEtoEdmObject[j].object.Add(&newMEtoEDMObject[i].object);
141  } else {
142  std::cout
143  << "ERROR MEtoEDM::mergeProducts(): found histograms with different axis limits or different labels, '"
144  << name << "' not merged" << std::endl;
145 #if METOEDMFORMAT_DEBUG
146  std::cout << MEtoEdmObject[j].name << " " << newMEtoEDMObject[i].name << std::endl;
147  std::cout << MEtoEdmObject[j].object.GetNbinsX() << " " << newMEtoEDMObject[i].object.GetNbinsX()
148  << std::endl;
149  std::cout << MEtoEdmObject[j].object.GetXaxis()->GetXmin() << " "
150  << newMEtoEDMObject[i].object.GetXaxis()->GetXmin() << std::endl;
151  std::cout << MEtoEdmObject[j].object.GetXaxis()->GetXmax() << " "
152  << newMEtoEDMObject[i].object.GetXaxis()->GetXmax() << std::endl;
153  std::cout << MEtoEdmObject[j].object.GetNbinsY() << " " << newMEtoEDMObject[i].object.GetNbinsY()
154  << std::endl;
155  std::cout << MEtoEdmObject[j].object.GetYaxis()->GetXmin() << " "
156  << newMEtoEDMObject[i].object.GetYaxis()->GetXmin() << std::endl;
157  std::cout << MEtoEdmObject[j].object.GetYaxis()->GetXmax() << " "
158  << newMEtoEDMObject[i].object.GetYaxis()->GetXmax() << std::endl;
159  std::cout << MEtoEdmObject[j].object.GetNbinsZ() << " " << newMEtoEDMObject[i].object.GetNbinsZ()
160  << std::endl;
161  std::cout << MEtoEdmObject[j].object.GetZaxis()->GetXmin() << " "
162  << newMEtoEDMObject[i].object.GetZaxis()->GetXmin() << std::endl;
163  std::cout << MEtoEdmObject[j].object.GetZaxis()->GetXmax() << " "
164  << newMEtoEDMObject[i].object.GetZaxis()->GetXmax() << std::endl;
165 #endif
166  }
167  }
168  }
169  return true;
170  }
171 
172  void swap(MEtoEDM<T> &iOther) { MEtoEdmObject.swap(iOther.MEtoEdmObject); }
173 
174 private:
176 
177 }; // end class declaration
178 
179 template <>
180 inline bool MEtoEDM<double>::mergeProduct(const MEtoEDM<double> &newMEtoEDM) {
181  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
182  const size_t nObjects = newMEtoEDMObject.size();
183  // NOTE: we remember the present size since we will only add content
184  // from newMEtoEDMObject after this point
185  const size_t nOldObjects = MEtoEdmObject.size();
186 
187  // if the old and new are not the same size, we want to report a problem
188  if (nObjects != nOldObjects) {
189  std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
190  << nObjects << ", old=" << nOldObjects << std::endl;
191  }
192 
193  for (unsigned int i = 0; i < nObjects; ++i) {
194  unsigned int j = 0;
195  // see if the name is already in the old container up to the point where
196  // we may have added new entries in the container
197  const std::string &name = newMEtoEDMObject[i].name;
198  if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
199  j = i;
200  } else {
201  j = 0;
202  while (j < nOldObjects && (MEtoEdmObject[j].name != name))
203  ++j;
204  }
205  if (j >= nOldObjects) {
206  // this value is only in the new container, not the old one
207 #if METOEDMFORMAT_DEBUG
208  std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
209 #endif
210  MEtoEdmObject.push_back(newMEtoEDMObject[i]);
211  }
212  }
213  return true;
214 }
215 
216 template <>
217 inline bool MEtoEDM<int>::mergeProduct(const MEtoEDM<int> &newMEtoEDM) {
218  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
219  const size_t nObjects = newMEtoEDMObject.size();
220  // NOTE: we remember the present size since we will only add content
221  // from newMEtoEDMObject after this point
222  const size_t nOldObjects = MEtoEdmObject.size();
223 
224  // if the old and new are not the same size, we want to report a problem
225  if (nObjects != nOldObjects) {
226  std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
227  << nObjects << ", old=" << nOldObjects << std::endl;
228  }
229 
230  for (unsigned int i = 0; i < nObjects; ++i) {
231  unsigned int j = 0;
232  // see if the name is already in the old container up to the point where
233  // we may have added new entries in the container
234  const std::string &name = newMEtoEDMObject[i].name;
235  if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
236  j = i;
237  } else {
238  j = 0;
239  while (j < nOldObjects && (MEtoEdmObject[j].name != name))
240  ++j;
241  }
242  if (j >= nOldObjects) {
243  // this value is only in the new container, not the old one
244 #if METOEDMFORMAT_DEBUG
245  std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
246 #endif
247  MEtoEdmObject.push_back(newMEtoEDMObject[i]);
248  } else {
249  // this value is also in the new container: add the two
250  if (MEtoEdmObject[j].name.find("EventInfo/processedEvents") != std::string::npos) {
251  MEtoEdmObject[j].object += (newMEtoEDMObject[i].object);
252  }
253  if (MEtoEdmObject[j].name.find("EventInfo/iEvent") != std::string::npos ||
254  MEtoEdmObject[j].name.find("EventInfo/iLumiSection") != std::string::npos) {
255  if (MEtoEdmObject[j].object < newMEtoEDMObject[i].object) {
256  MEtoEdmObject[j].object = (newMEtoEDMObject[i].object);
257  }
258  }
259  }
260  }
261  return true;
262 }
263 
264 template <>
265 inline bool MEtoEDM<long long>::mergeProduct(const MEtoEDM<long long> &newMEtoEDM) {
266  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
267  const size_t nObjects = newMEtoEDMObject.size();
268  // NOTE: we remember the present size since we will only add content
269  // from newMEtoEDMObject after this point
270  const size_t nOldObjects = MEtoEdmObject.size();
271 
272  // if the old and new are not the same size, we want to report a problem
273  if (nObjects != nOldObjects) {
274  std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
275  << nObjects << ", old=" << nOldObjects << std::endl;
276  }
277 
278  for (unsigned int i = 0; i < nObjects; ++i) {
279  unsigned int j = 0;
280  // see if the name is already in the old container up to the point where
281  // we may have added new entries in the container
282  const std::string &name = newMEtoEDMObject[i].name;
283  if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
284  j = i;
285  } else {
286  j = 0;
287  while (j < nOldObjects && (MEtoEdmObject[j].name != name))
288  ++j;
289  }
290  if (j >= nOldObjects) {
291  // this value is only in the new container, not the old one
292 #if METOEDMFORMAT_DEBUG
293  std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
294 #endif
295  MEtoEdmObject.push_back(newMEtoEDMObject[i]);
296  } else {
297  // this value is also in the new container: add the two
298  if (MEtoEdmObject[j].name.find("EventInfo/processedEvents") != std::string::npos) {
299  MEtoEdmObject[j].object += (newMEtoEDMObject[i].object);
300  }
301  if (MEtoEdmObject[j].name.find("EventInfo/iEvent") != std::string::npos ||
302  MEtoEdmObject[j].name.find("EventInfo/iLumiSection") != std::string::npos) {
303  if (MEtoEdmObject[j].object < newMEtoEDMObject[i].object) {
304  MEtoEdmObject[j].object = (newMEtoEDMObject[i].object);
305  }
306  }
307  }
308  }
309  return true;
310 }
311 
312 template <>
313 inline bool MEtoEDM<TString>::mergeProduct(const MEtoEDM<TString> &newMEtoEDM) {
314  const MEtoEdmObjectVector &newMEtoEDMObject = newMEtoEDM.getMEtoEdmObject();
315  const size_t nObjects = newMEtoEDMObject.size();
316  // NOTE: we remember the present size since we will only add content
317  // from newMEtoEDMObject after this point
318  const size_t nOldObjects = MEtoEdmObject.size();
319 
320  // if the old and new are not the same size, we want to report a problem
321  if (nObjects != nOldObjects) {
322  std::cout << "WARNING MEtoEDM::mergeProducts(): the lists of histograms to be merged have different sizes: new="
323  << nObjects << ", old=" << nOldObjects << std::endl;
324  }
325 
326  for (unsigned int i = 0; i < nObjects; ++i) {
327  unsigned int j = 0;
328  // see if the name is already in the old container up to the point where
329  // we may have added new entries in the container
330  const std::string &name = newMEtoEDMObject[i].name;
331  if (i < nOldObjects && (MEtoEdmObject[i].name == name)) {
332  j = i;
333  } else {
334  j = 0;
335  while (j < nOldObjects && (MEtoEdmObject[j].name != name))
336  ++j;
337  }
338  if (j >= nOldObjects) {
339  // this value is only in the new container, not the old one
340 #if METOEDMFORMAT_DEBUG
341  std::cout << "WARNING MEtoEDM::mergeProducts(): adding new histogram '" << name << "'" << std::endl;
342 #endif
343  MEtoEdmObject.push_back(newMEtoEDMObject[i]);
344  }
345  }
346  return true;
347 }
348 
349 #endif
void swap(MEtoEDM< T > &iOther)
virtual ~MEtoEDM()
Definition: MEtoEDMFormat.h:41
MEtoEDM(size_t reservedSize)
Definition: MEtoEDMFormat.h:40
bool mergeProduct(const MEtoEDM< T > &newMEtoEDM)
Definition: MEtoEDMFormat.h:86
void putMEtoEdmObject(const std::string &name, const T &object)
Definition: MEtoEDMFormat.h:53
const MEtoEdmObjectVector & getMEtoEdmObject() const
Definition: MEtoEDMFormat.h:60
bool CheckBinLabels(const TAxis *a1, const TAxis *a2)
Definition: MEtoEDMFormat.h:62
tuple cout
Definition: gather_cfg.py:144
std::vector< MEtoEDMObject > MEtoEdmObjectVector
Definition: MEtoEDMFormat.h:51
std::vector< uint32_t > TagList
Definition: MEtoEDMFormat.h:43
long double T
MEtoEdmObjectVector MEtoEdmObject