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 
8 namespace nanoaod {
9 
11  public:
13  typedef long long int_accumulator; // we accumulate in long long int, to avoid overflow
14  typedef double float_accumulator; // we accumulate in double, to preserve precision
15 
16  template <typename T>
17  struct SingleColumn {
18  typedef T value_type;
20  SingleColumn(const std::string& aname, const std::string& adoc, T avalue = T())
21  : name(aname), doc(adoc), value(avalue) {}
27  //if (name != other.name) throw cms::Exception("LogicError", "Trying to merge "+name+" with "+other.name+"\n");
28  value += other.value;
29  }
31  return name == other.name; // we don't check the doc, not needed
32  }
33  };
36 
37  template <typename T>
38  struct VectorColumn {
39  typedef T element_type;
41  VectorColumn(const std::string& aname, const std::string& adoc, unsigned int size)
42  : name(aname), doc(adoc), values(size, T()) {}
43  VectorColumn(const std::string& aname, const std::string& adoc, const std::vector<T>& somevalues)
44  : name(aname), doc(adoc), values(somevalues) {}
46  std::vector<T> values;
50  //if (name != other.name) throw cms::Exception("LogicError", "Trying to merge "+name+" with "+other.name+"\n");
51  //if (values.size() != other.values.size()) throw cms::Exception("LogicError", "Trying to merge "+name+" with different number of values!\n");
52  for (unsigned int i = 0, n = values.size(); i < n; ++i) {
53  values[i] += other.values[i];
54  }
55  }
57  return name == other.name && values.size() == other.values.size(); // we don't check the doc, not needed
58  }
59  };
62 
63  const std::vector<FloatColumn>& floatCols() const { return floatCols_; }
64  const std::vector<VFloatColumn>& vfloatCols() const { return vfloatCols_; }
65  const std::vector<IntColumn>& intCols() const { return intCols_; }
66  const std::vector<VIntColumn>& vintCols() const { return vintCols_; }
67 
68  template <typename F>
69  void addFloat(const std::string& name, const std::string& doc, F value) {
70  floatCols_.push_back(FloatColumn(name, doc, value));
71  }
72 
73  template <typename I>
74  void addInt(const std::string& name, const std::string& doc, I value) {
75  intCols_.push_back(IntColumn(name, doc, value));
76  }
77 
78  template <typename F>
79  void addVFloat(const std::string& name, const std::string& doc, const std::vector<F> values) {
80  vfloatCols_.push_back(VFloatColumn(name, doc, values.size()));
81  std::copy(values.begin(), values.end(), vfloatCols_.back().values.begin());
82  }
83 
84  template <typename I>
85  void addVInt(const std::string& name, const std::string& doc, const std::vector<I> values) {
86  vintCols_.push_back(VIntColumn(name, doc, values.size()));
87  std::copy(values.begin(), values.end(), vintCols_.back().values.begin());
88  }
89 
91  if (!tryMerge(intCols_, other.intCols_))
92  return false;
93  if (!tryMerge(vintCols_, other.vintCols_))
94  return false;
95  if (!tryMerge(floatCols_, other.floatCols_))
96  return false;
97  if (!tryMerge(vfloatCols_, other.vfloatCols_))
98  return false;
99  return true;
100  }
101 
102  void swap(MergeableCounterTable& iOther) {
103  floatCols_.swap(iOther.floatCols_);
104  vfloatCols_.swap(iOther.vfloatCols_);
105  intCols_.swap(iOther.intCols_);
106  vintCols_.swap(iOther.vintCols_);
107  }
108 
109  private:
110  std::vector<FloatColumn> floatCols_;
111  std::vector<VFloatColumn> vfloatCols_;
112  std::vector<IntColumn> intCols_;
113  std::vector<VIntColumn> vintCols_;
114 
115  template <typename T>
116  bool tryMerge(std::vector<T>& one, const std::vector<T>& two) {
117  if (one.size() != two.size())
118  return false;
119  for (unsigned int i = 0, n = one.size(); i < n; ++i) {
120  if (!one[i].compatible(two[i]))
121  return false;
122  one[i] += two[i];
123  }
124  return true;
125  }
126  };
127 
128 } // namespace nanoaod
129 
130 #endif
SingleColumn< float_accumulator > FloatColumn
size
Write out results.
void addInt(const std::string &name, const std::string &doc, I value)
void operator+=(const SingleColumn< T > &other)
bool compatible(const VectorColumn< T > &other)
VectorColumn(const std::string &aname, const std::string &adoc, unsigned int size)
const std::vector< VIntColumn > & vintCols() const
void addFloat(const std::string &name, const std::string &doc, F value)
std::vector< IntColumn > intCols_
void addVInt(const std::string &name, const std::string &doc, const std::vector< I > values)
void swap(MergeableCounterTable &iOther)
const std::vector< IntColumn > & intCols() const
VectorColumn(const std::string &aname, const std::string &adoc, const std::vector< T > &somevalues)
const std::vector< FloatColumn > & floatCols() const
void operator+=(const VectorColumn< T > &other)
void addVFloat(const std::string &name, const std::string &doc, const std::vector< F > values)
bool tryMerge(std::vector< T > &one, const std::vector< T > &two)
const std::complex< double > I
Definition: I.h:8
std::vector< VIntColumn > vintCols_
Definition: value.py:1
std::vector< VFloatColumn > vfloatCols_
bool compatible(const SingleColumn< T > &other)
std::vector< FloatColumn > floatCols_
const std::vector< VFloatColumn > & vfloatCols() const
VectorColumn< float_accumulator > VFloatColumn
VectorColumn< int_accumulator > VIntColumn
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
SingleColumn< int_accumulator > IntColumn
long double T
bool mergeProduct(const MergeableCounterTable &other)
SingleColumn(const std::string &aname, const std::string &adoc, T avalue=T())