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()) : name(aname), doc(adoc), value(avalue) {}
26  //if (name != other.name) throw cms::Exception("LogicError", "Trying to merge "+name+" with "+other.name+"\n");
27  value += other.value;
28  }
30  return name == other.name; // we don't check the doc, not needed
31  }
32  };
35 
36  template<typename T>
37  struct VectorColumn {
38  typedef T element_type;
40  VectorColumn(const std::string & aname, const std::string & adoc, unsigned int size) : name(aname), doc(adoc), values(size, T()) {}
41  VectorColumn(const std::string & aname, const std::string & adoc, const std::vector<T> & somevalues) : name(aname), doc(adoc), values(somevalues) {}
43  std::vector<T> values;
47  //if (name != other.name) throw cms::Exception("LogicError", "Trying to merge "+name+" with "+other.name+"\n");
48  //if (values.size() != other.values.size()) throw cms::Exception("LogicError", "Trying to merge "+name+" with different number of values!\n");
49  for (unsigned int i = 0, n = values.size(); i < n; ++i) {
50  values[i] += other.values[i];
51  }
52  }
54  return name == other.name && values.size() == other.values.size(); // we don't check the doc, not needed
55  }
56  };
59 
60  const std::vector<FloatColumn> & floatCols() const { return floatCols_; }
61  const std::vector<VFloatColumn> & vfloatCols() const { return vfloatCols_; }
62  const std::vector<IntColumn> & intCols() const { return intCols_; }
63  const std::vector<VIntColumn> & vintCols() const { return vintCols_; }
64 
65  template<typename F>
66  void addFloat(const std::string & name, const std::string & doc, F value) { floatCols_.push_back(FloatColumn(name, doc, value)); }
67 
68  template<typename I>
69  void addInt(const std::string & name, const std::string & doc, I value) { intCols_.push_back(IntColumn(name, doc, value)); }
70 
71  template<typename F>
72  void addVFloat(const std::string & name, const std::string & doc, const std::vector<F> values) {
73  vfloatCols_.push_back(VFloatColumn(name, doc, values.size()));
74  std::copy(values.begin(), values.end(), vfloatCols_.back().values.begin());
75  }
76 
77  template<typename I>
78  void addVInt(const std::string & name, const std::string & doc, const std::vector<I> values) {
79  vintCols_.push_back(VIntColumn(name, doc, values.size()));
80  std::copy(values.begin(), values.end(), vintCols_.back().values.begin());
81  }
82 
83 
85  if (!tryMerge(intCols_, other.intCols_)) return false;
86  if (!tryMerge(vintCols_, other.vintCols_)) return false;
87  if (!tryMerge(floatCols_, other.floatCols_)) return false;
88  if (!tryMerge(vfloatCols_, other.vfloatCols_)) return false;
89  return true;
90  }
91 
92  private:
93  std::vector<FloatColumn> floatCols_;
94  std::vector<VFloatColumn> vfloatCols_;
95  std::vector<IntColumn> intCols_;
96  std::vector<VIntColumn> vintCols_;
97 
98  template<typename T>
99  bool tryMerge(std::vector<T> & one, const std::vector<T> & two) {
100  if (one.size() != two.size()) return false;
101  for (unsigned int i = 0, n = one.size(); i < n; ++i) {
102  if (!one[i].compatible(two[i])) return false;
103  one[i] += two[i];
104  }
105  return true;
106  }
107 };
108 
109 } // namespace nanoaod
110 
111 #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)
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:281
SingleColumn< int_accumulator > IntColumn
long double T
bool mergeProduct(const MergeableCounterTable &other)
SingleColumn(const std::string &aname, const std::string &adoc, T avalue=T())