npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
NtNtupleFill.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_NTNTUPLEFILL_HH_
2 #define NPSTAT_NTNTUPLEFILL_HH_
3 
4 /*!
5 // \file NtNtupleFill.hh
6 //
7 // \brief Fill a homogeneous ntuple using contents of another ntuple.
8 //
9 // For use with "cycleOverRows", "conditionalCycleOverRows", and other
10 // similar methods of AbsNtuple.
11 //
12 // Author: I. Volobouev
13 //
14 // February 2012
15 */
16 
17 #include <cassert>
18 #include <stdexcept>
19 
20 namespace npstat {
21  /**
22  // This convenience class fills an ntuple using contents of another ntuple
23  */
24  template <class Ntuple>
26  {
27  public:
28  typedef Ntuple ntuple_type;
29 
30  /**
31  // The constructor assumes that correctness of the ntuple
32  // column numbers has already been verified. Column
33  // numbers are thus best generated with AbsNtuple
34  // methods "columnIndices". Column number columnMap[0]
35  // of the source ntuple (whose "cycleOverRows" method will
36  // be called) will be mapped into the first column of the
37  // destination ntuple, columnMap[1] into the second, etc.
38  // The number of "columnMap" elements must be equal either
39  // to the number of columns in the destination ntuple or
40  // to zero. In the latter case an automatic trivial
41  // sequential column mapping will be generated.
42  */
43  inline NtNtupleFill(Ntuple* destination,
44  const std::vector<unsigned long>& columnMap)
45  : dest_(destination),
46  coordCols_(columnMap)
47  {
48  assert(dest_);
49  const unsigned long nCols = dest_->nColumns();
50  if (!nCols) throw std::invalid_argument(
51  "In npstat::NtNtupleFill constructor: "
52  "can not fill zero-column ntuples");
53  const unsigned long mapLen = coordCols_.size();
54  if (mapLen != 0 && mapLen != nCols) throw std::invalid_argument(
55  "In npstat::NtNtupleFill constructor: "
56  "incompatible number of elements in the column map");
57  buffer_.resize(nCols);
58  }
59 
60  private:
61  NtNtupleFill();
62 
63  Ntuple* dest_;
64  std::vector<unsigned long> coordCols_;
65  mutable std::vector<typename Ntuple::value_type> buffer_;
66 
67  public:
68  template <typename T>
69  inline void accumulate(const T* rowContents,
70  const unsigned long nCols) const
71  {
72  const unsigned long dim = buffer_.size();
73  typename Ntuple::value_type * c = &buffer_[0];
74  if (coordCols_.empty())
75  {
76  if (nCols < dim) throw std::invalid_argument(
77  "In npstat::NtNtupleFill::accumulate: "
78  "insufficient number of columns in the source ntuple");
79  for (unsigned long i=0; i<dim; ++i)
80  c[i] = static_cast<typename Ntuple::value_type>(
81  rowContents[i]);
82  }
83  else
84  {
85  const unsigned long* idx = &coordCols_[0];
86  for (unsigned long i=0; i<dim; ++i)
87  c[i] = static_cast<typename Ntuple::value_type>(
88  rowContents[idx[i]]);
89  }
90  dest_->fill(c, dim);
91  }
92  };
93 
94  //@{
95  /** Helper utility function for making NtNtupleFill objects */
96  template <typename Ntuple>
98  Ntuple& destination,
99  const std::vector<unsigned long>& columnMap)
100  {
101  return NtNtupleFill<Ntuple>(&destination, columnMap);
102  }
103 
104  template <typename Ntuple>
105  inline NtNtupleFill<Ntuple> make_NtNtupleFill(Ntuple& destination)
106  {
107  std::vector<unsigned long> dummyMap;
108  return NtNtupleFill<Ntuple>(&destination, dummyMap);
109  }
110  //@}
111 }
112 
113 #endif // NPSTAT_NTNTUPLEFILL_HH_
Definition: NtNtupleFill.hh:26
NtNtupleFill(Ntuple *destination, const std::vector< unsigned long > &columnMap)
Definition: NtNtupleFill.hh:43
Definition: AbsArrayProjector.hh:14
NtNtupleFill< Ntuple > make_NtNtupleFill(Ntuple &destination, const std::vector< unsigned long > &columnMap)
Definition: NtNtupleFill.hh:97