CMS 3D CMS Logo

MergeableCounterTable.h
Go to the documentation of this file.
1 #ifndef DataFormats_NanoAOD_MergeableCounterTable_h
2 #define DataFormats_NanoAOD_MergeableCounterTable_h
3 
5 #include <vector>
6 #include <string>
7 #include <algorithm>
8 
9 namespace nanoaod {
10 
12  public:
14  typedef long long int_accumulator; // we accumulate in long long int, to avoid overflow
15  typedef double float_accumulator; // we accumulate in double, to preserve precision
16 
17  template <typename T>
18  struct SingleColumn {
19  typedef T value_type;
21  SingleColumn(const std::string& aname, const std::string& adoc, T avalue = T())
22  : name(aname), doc(adoc), value(avalue) {}
26  if (!compatible(other))
27  throw cms::Exception("LogicError",
28  "Trying to merge " + name + " with " + other.name + " failed compatibility test.\n");
29  value += other.value;
30  }
32  return name == other.name; // we don't check the doc, not needed
33  }
34  };
37 
38  template <typename T>
41  SingleWithNormColumn(const std::string& aname, const std::string& adoc, T avalue = T(), const double anorm = 0)
42  : SingleColumn<T>(aname, adoc, avalue), norm(anorm) {}
43  double norm;
45  if (!this->compatible(other))
46  throw cms::Exception(
47  "LogicError", "Trying to merge " + this->name + " with " + other.name + " failed compatibility test.\n");
48  auto newNorm = norm + other.norm;
49  this->value = (newNorm != 0) ? (this->value * norm + other.value * other.norm) / newNorm : 0;
50  norm = newNorm;
51  }
52  };
54 
55  template <typename T>
56  struct VectorColumn {
57  typedef T element_type;
59  VectorColumn(const std::string& aname, const std::string& adoc, unsigned int size)
60  : name(aname), doc(adoc), values(size, T()) {}
61  VectorColumn(const std::string& aname, const std::string& adoc, const std::vector<T>& somevalues)
62  : name(aname), doc(adoc), values(somevalues) {}
64  std::vector<T> values;
66  if (!compatible(other))
67  throw cms::Exception("LogicError",
68  "Trying to merge " + name + " with " + other.name + " failed compatibility test.\n");
69  for (unsigned int i = 0, n = values.size(); i < n; ++i) {
70  values[i] += other.values[i];
71  }
72  }
74  return name == other.name && values.size() == other.values.size(); // we don't check the doc, not needed
75  }
76  };
79 
80  template <typename T>
82  double norm;
84  VectorWithNormColumn(const std::string& aname, const std::string& adoc, unsigned int size, double anorm = 0)
85  : VectorColumn<T>(aname, adoc, size), norm(anorm) {}
87  const std::string& adoc,
88  const std::vector<T>& somevalues,
89  double anorm = 0)
90  : VectorColumn<T>(aname, adoc, somevalues), norm(anorm) {}
92  if (!this->compatible(other))
93  throw cms::Exception(
94  "LogicError", "Trying to merge " + this->name + " with " + other.name + " failed compatibility test.\n");
95  auto newNorm = norm + other.norm;
96  for (unsigned int i = 0, n = this->values.size(); i < n; ++i) {
97  this->values[i] =
98  (newNorm != 0) ? (this->values[i] * norm + other.values[i] * other.norm) / (norm + other.norm) : 0;
99  }
100  norm = newNorm;
101  }
102  };
104 
105  const std::vector<FloatColumn>& floatCols() const { return floatCols_; }
106  const std::vector<VFloatColumn>& vfloatCols() const { return vfloatCols_; }
107  const std::vector<FloatWithNormColumn>& floatWithNormCols() const { return floatWithNormCols_; }
108  const std::vector<VFloatWithNormColumn>& vfloatWithNormCols() const { return vfloatWithNormCols_; }
109  const std::vector<IntColumn>& intCols() const { return intCols_; }
110  const std::vector<VIntColumn>& vintCols() const { return vintCols_; }
111 
112  template <typename F>
113  void addFloat(const std::string& name, const std::string& doc, F value) {
114  floatCols_.push_back(FloatColumn(name, doc, value));
115  }
116 
117  template <typename F>
118  void addFloatWithNorm(const std::string& name, const std::string& doc, F value, double norm) {
120  }
121 
122  template <typename I>
123  void addInt(const std::string& name, const std::string& doc, I value) {
124  intCols_.push_back(IntColumn(name, doc, value));
125  }
126 
127  template <typename F>
128  void addVFloat(const std::string& name, const std::string& doc, const std::vector<F> values) {
129  vfloatCols_.push_back(VFloatColumn(name, doc, values.size()));
130  std::copy(values.begin(), values.end(), vfloatCols_.back().values.begin());
131  }
132 
133  template <typename F>
134  void addVFloatWithNorm(const std::string& name, const std::string& doc, const std::vector<F> values, double norm) {
135  vfloatWithNormCols_.push_back(VFloatWithNormColumn(name, doc, values.size(), norm));
136  std::copy(values.begin(), values.end(), vfloatWithNormCols_.back().values.begin());
137  }
138 
139  template <typename I>
140  void addVInt(const std::string& name, const std::string& doc, const std::vector<I> values) {
141  vintCols_.push_back(VIntColumn(name, doc, values.size()));
142  std::copy(values.begin(), values.end(), vintCols_.back().values.begin());
143  }
144 
146  if (!tryMerge(intCols_, other.intCols_))
147  return false;
148  if (!tryMerge(vintCols_, other.vintCols_))
149  return false;
150  if (!tryMerge(floatCols_, other.floatCols_))
151  return false;
152  if (!tryMerge(vfloatCols_, other.vfloatCols_))
153  return false;
154  if (!tryMerge(floatWithNormCols_, other.floatWithNormCols_))
155  return false;
156  if (!tryMerge(vfloatWithNormCols_, other.vfloatWithNormCols_))
157  return false;
158  return true;
159  }
160 
161  void swap(MergeableCounterTable& iOther) {
162  floatCols_.swap(iOther.floatCols_);
163  vfloatCols_.swap(iOther.vfloatCols_);
166  intCols_.swap(iOther.intCols_);
167  vintCols_.swap(iOther.vintCols_);
168  }
169 
170  private:
171  std::vector<FloatColumn> floatCols_;
172  std::vector<VFloatColumn> vfloatCols_;
173  std::vector<FloatWithNormColumn> floatWithNormCols_;
174  std::vector<VFloatWithNormColumn> vfloatWithNormCols_;
175  std::vector<IntColumn> intCols_;
176  std::vector<VIntColumn> vintCols_;
177 
178  template <typename T>
179  bool tryMerge(std::vector<T>& one, const std::vector<T>& two) {
180  for (auto y : two) {
181  auto x = std::find_if(one.begin(), one.end(), [&y](const T& x) { return x.name == y.name; });
182  if (x == one.end())
183  one.push_back(y);
184  else
185  (*x) += y;
186  }
187  return true;
188  }
189  };
190 
191 } // namespace nanoaod
192 
193 #endif
nanoaod::MergeableCounterTable::VectorWithNormColumn::norm
double norm
Definition: MergeableCounterTable.h:82
nanoaod::MergeableCounterTable::intCols_
std::vector< IntColumn > intCols_
Definition: MergeableCounterTable.h:175
nanoaod::MergeableCounterTable::SingleColumn::value_type
T value_type
Definition: MergeableCounterTable.h:19
nanoaod::MergeableCounterTable::floatCols_
std::vector< FloatColumn > floatCols_
Definition: MergeableCounterTable.h:171
common_cff.doc
doc
Definition: common_cff.py:54
mps_fire.i
i
Definition: mps_fire.py:355
nanoaod::MergeableCounterTable::addFloat
void addFloat(const std::string &name, const std::string &doc, F value)
Definition: MergeableCounterTable.h:113
nanoaod::MergeableCounterTable::intCols
const std::vector< IntColumn > & intCols() const
Definition: MergeableCounterTable.h:109
nanoaod::MergeableCounterTable::SingleColumn::operator+=
void operator+=(const SingleColumn< T > &other)
Definition: MergeableCounterTable.h:25
nanoaod
Definition: FlatTable.h:13
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
nanoaod::MergeableCounterTable::addInt
void addInt(const std::string &name, const std::string &doc, I value)
Definition: MergeableCounterTable.h:123
nanoaod::MergeableCounterTable::vfloatWithNormCols
const std::vector< VFloatWithNormColumn > & vfloatWithNormCols() const
Definition: MergeableCounterTable.h:108
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
nanoaod::MergeableCounterTable::VectorWithNormColumn::VectorWithNormColumn
VectorWithNormColumn()
Definition: MergeableCounterTable.h:83
nanoaod::MergeableCounterTable::SingleWithNormColumn::norm
double norm
Definition: MergeableCounterTable.h:43
nanoaod::MergeableCounterTable::VectorColumn::compatible
bool compatible(const VectorColumn< T > &other)
Definition: MergeableCounterTable.h:73
nanoaod::MergeableCounterTable::swap
void swap(MergeableCounterTable &iOther)
Definition: MergeableCounterTable.h:161
nanoaod::MergeableCounterTable::SingleColumn::SingleColumn
SingleColumn()
Definition: MergeableCounterTable.h:20
nanoaod::MergeableCounterTable::vintCols
const std::vector< VIntColumn > & vintCols() const
Definition: MergeableCounterTable.h:110
nanoaod::MergeableCounterTable::VectorWithNormColumn::VectorWithNormColumn
VectorWithNormColumn(const std::string &aname, const std::string &adoc, unsigned int size, double anorm=0)
Definition: MergeableCounterTable.h:84
nanoaod::MergeableCounterTable::SingleColumn::name
std::string name
Definition: MergeableCounterTable.h:23
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
nanoaod::MergeableCounterTable::VectorColumn::doc
std::string doc
Definition: MergeableCounterTable.h:63
nanoaod::MergeableCounterTable::addVInt
void addVInt(const std::string &name, const std::string &doc, const std::vector< I > values)
Definition: MergeableCounterTable.h:140
nanoaod::MergeableCounterTable::SingleColumn::doc
std::string doc
Definition: MergeableCounterTable.h:23
Exhume::I
const std::complex< double > I
Definition: I.h:8
nanoaod::MergeableCounterTable::float_accumulator
double float_accumulator
Definition: MergeableCounterTable.h:15
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
nanoaod::MergeableCounterTable::VectorWithNormColumn::VectorWithNormColumn
VectorWithNormColumn(const std::string &aname, const std::string &adoc, const std::vector< T > &somevalues, double anorm=0)
Definition: MergeableCounterTable.h:86
trackingPlots.other
other
Definition: trackingPlots.py:1465
nanoaod::MergeableCounterTable
Definition: MergeableCounterTable.h:11
nanoaod::MergeableCounterTable::tryMerge
bool tryMerge(std::vector< T > &one, const std::vector< T > &two)
Definition: MergeableCounterTable.h:179
nanoaod::MergeableCounterTable::SingleWithNormColumn::operator+=
void operator+=(const SingleWithNormColumn< T > &other)
Definition: MergeableCounterTable.h:44
nanoaod::MergeableCounterTable::SingleColumn::value
T value
Definition: MergeableCounterTable.h:24
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
nanoaod::MergeableCounterTable::VectorColumn::element_type
T element_type
Definition: MergeableCounterTable.h:57
nanoaod::MergeableCounterTable::SingleWithNormColumn::SingleWithNormColumn
SingleWithNormColumn(const std::string &aname, const std::string &adoc, T avalue=T(), const double anorm=0)
Definition: MergeableCounterTable.h:41
nanoaod::MergeableCounterTable::VectorColumn::VectorColumn
VectorColumn(const std::string &aname, const std::string &adoc, unsigned int size)
Definition: MergeableCounterTable.h:59
nanoaod::MergeableCounterTable::floatWithNormCols_
std::vector< FloatWithNormColumn > floatWithNormCols_
Definition: MergeableCounterTable.h:173
nanoaod::MergeableCounterTable::SingleColumn::SingleColumn
SingleColumn(const std::string &aname, const std::string &adoc, T avalue=T())
Definition: MergeableCounterTable.h:21
nanoaod::MergeableCounterTable::SingleWithNormColumn::SingleWithNormColumn
SingleWithNormColumn()
Definition: MergeableCounterTable.h:40
nanoaod::MergeableCounterTable::floatWithNormCols
const std::vector< FloatWithNormColumn > & floatWithNormCols() const
Definition: MergeableCounterTable.h:107
nanoaod::MergeableCounterTable::VectorColumn::values
std::vector< T > values
Definition: MergeableCounterTable.h:64
value
Definition: value.py:1
nanoaod::MergeableCounterTable::mergeProduct
bool mergeProduct(const MergeableCounterTable &other)
Definition: MergeableCounterTable.h:145
nanoaod::MergeableCounterTable::addFloatWithNorm
void addFloatWithNorm(const std::string &name, const std::string &doc, F value, double norm)
Definition: MergeableCounterTable.h:118
nanoaod::MergeableCounterTable::SingleWithNormColumn
Definition: MergeableCounterTable.h:39
nanoaod::MergeableCounterTable::floatCols
const std::vector< FloatColumn > & floatCols() const
Definition: MergeableCounterTable.h:105
nanoaod::MergeableCounterTable::VectorColumn::operator+=
void operator+=(const VectorColumn< T > &other)
Definition: MergeableCounterTable.h:65
nanoaod::MergeableCounterTable::vintCols_
std::vector< VIntColumn > vintCols_
Definition: MergeableCounterTable.h:176
nanoaod::MergeableCounterTable::VectorColumn
Definition: MergeableCounterTable.h:56
nanoaod::MergeableCounterTable::FloatColumn
SingleColumn< float_accumulator > FloatColumn
Definition: MergeableCounterTable.h:35
nanoaod::MergeableCounterTable::IntColumn
SingleColumn< int_accumulator > IntColumn
Definition: MergeableCounterTable.h:36
nanoaod::MergeableCounterTable::FloatWithNormColumn
SingleWithNormColumn< float_accumulator > FloatWithNormColumn
Definition: MergeableCounterTable.h:53
nanoaod::MergeableCounterTable::VectorWithNormColumn
Definition: MergeableCounterTable.h:81
nanoaod::MergeableCounterTable::MergeableCounterTable
MergeableCounterTable()
Definition: MergeableCounterTable.h:13
nanoaod::MergeableCounterTable::addVFloat
void addVFloat(const std::string &name, const std::string &doc, const std::vector< F > values)
Definition: MergeableCounterTable.h:128
T
long double T
Definition: Basic3DVectorLD.h:48
nanoaod::MergeableCounterTable::VectorColumn::VectorColumn
VectorColumn(const std::string &aname, const std::string &adoc, const std::vector< T > &somevalues)
Definition: MergeableCounterTable.h:61
nanoaod::MergeableCounterTable::SingleColumn::compatible
bool compatible(const SingleColumn< T > &other)
Definition: MergeableCounterTable.h:31
Exception
Definition: hltDiff.cc:246
nanoaod::MergeableCounterTable::VIntColumn
VectorColumn< int_accumulator > VIntColumn
Definition: MergeableCounterTable.h:78
nanoaod::MergeableCounterTable::VectorWithNormColumn::operator+=
void operator+=(const VectorWithNormColumn< T > &other)
Definition: MergeableCounterTable.h:91
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
nanoaod::MergeableCounterTable::vfloatWithNormCols_
std::vector< VFloatWithNormColumn > vfloatWithNormCols_
Definition: MergeableCounterTable.h:174
nanoaod::MergeableCounterTable::int_accumulator
long long int_accumulator
Definition: MergeableCounterTable.h:14
nanoaod::MergeableCounterTable::VectorColumn::name
std::string name
Definition: MergeableCounterTable.h:63
nanoaod::MergeableCounterTable::SingleColumn
Definition: MergeableCounterTable.h:18
nanoaod::MergeableCounterTable::vfloatCols
const std::vector< VFloatColumn > & vfloatCols() const
Definition: MergeableCounterTable.h:106
nanoaod::MergeableCounterTable::VFloatWithNormColumn
VectorWithNormColumn< float_accumulator > VFloatWithNormColumn
Definition: MergeableCounterTable.h:103
nanoaod::MergeableCounterTable::VectorColumn::VectorColumn
VectorColumn()
Definition: MergeableCounterTable.h:58
nanoaod::MergeableCounterTable::vfloatCols_
std::vector< VFloatColumn > vfloatCols_
Definition: MergeableCounterTable.h:172
nanoaod::MergeableCounterTable::VFloatColumn
VectorColumn< float_accumulator > VFloatColumn
Definition: MergeableCounterTable.h:77
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
nanoaod::MergeableCounterTable::addVFloatWithNorm
void addVFloatWithNorm(const std::string &name, const std::string &doc, const std::vector< F > values, double norm)
Definition: MergeableCounterTable.h:134