npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
NtRectangularCut.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_NTRECTANGULARCUT_HH_
2 #define NPSTAT_NTRECTANGULARCUT_HH_
3 
4 /*!
5 // \file NtRectangularCut.hh
6 //
7 // \brief Rectangular cuts for homogeneous ntuples
8 //
9 // Author: I. Volobouev
10 //
11 // November 2011
12 */
13 
14 #include "npstat/nm/BoxND.hh"
15 
16 namespace npstat {
17  /**
18  // This convenience class implements simple rectangular cuts
19  // on ntuple contents. Cut lower bound is included and cut upper bound
20  // is excluded from the passing sample. It is possible to specify
21  // min >= max for any rectangle dimension. In this case no ntuple rows
22  // will be accepted by the cut.
23  //
24  // The template parameter should coincide with the ntuple value_type.
25  //
26  // For use inside "conditionalCycleOverRows", and other similar methods
27  // of AbsNtuple.
28  */
29  template<typename Numeric>
31  {
32  template <typename Num2> friend class NtRectangularCut;
33 
34  public:
35  /** Default constructor creates an "all pass" cut */
36  inline NtRectangularCut() : nCuts_(0) {}
37 
38  //@{
39  /**
40  // For this convenience constructor, there must be
41  // an automatic conversion from type T to Numeric.
42  //
43  // It is assumed that correctness of the ntuple column numbers
44  // has already been verified. Use "validColumn()" ntuple method
45  // or other similar checks to ensure this correctness and do not
46  // reuse cut objects with ntuples that might have different
47  // column structure.
48  */
49  template<typename T>
50  NtRectangularCut(unsigned long c0, const T& min0, const T& max0);
51 
52  template<typename T>
53  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
54  unsigned long c1, const T& min1, const T& max1);
55 
56  template<typename T>
57  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
58  unsigned long c1, const T& min1, const T& max1,
59  unsigned long c2, const T& min2, const T& max2);
60 
61  template<typename T>
62  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
63  unsigned long c1, const T& min1, const T& max1,
64  unsigned long c2, const T& min2, const T& max2,
65  unsigned long c3, const T& min3, const T& max3);
66 
67  template<typename T>
68  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
69  unsigned long c1, const T& min1, const T& max1,
70  unsigned long c2, const T& min2, const T& max2,
71  unsigned long c3, const T& min3, const T& max3,
72  unsigned long c4, const T& min4, const T& max4);
73 
74  template<typename T>
75  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
76  unsigned long c1, const T& min1, const T& max1,
77  unsigned long c2, const T& min2, const T& max2,
78  unsigned long c3, const T& min3, const T& max3,
79  unsigned long c4, const T& min4, const T& max4,
80  unsigned long c5, const T& min5, const T& max5);
81 
82  template<typename T>
83  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
84  unsigned long c1, const T& min1, const T& max1,
85  unsigned long c2, const T& min2, const T& max2,
86  unsigned long c3, const T& min3, const T& max3,
87  unsigned long c4, const T& min4, const T& max4,
88  unsigned long c5, const T& min5, const T& max5,
89  unsigned long c6, const T& min6, const T& max6);
90 
91  template<typename T>
92  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
93  unsigned long c1, const T& min1, const T& max1,
94  unsigned long c2, const T& min2, const T& max2,
95  unsigned long c3, const T& min3, const T& max3,
96  unsigned long c4, const T& min4, const T& max4,
97  unsigned long c5, const T& min5, const T& max5,
98  unsigned long c6, const T& min6, const T& max6,
99  unsigned long c7, const T& min7, const T& max7);
100 
101  template<typename T>
102  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
103  unsigned long c1, const T& min1, const T& max1,
104  unsigned long c2, const T& min2, const T& max2,
105  unsigned long c3, const T& min3, const T& max3,
106  unsigned long c4, const T& min4, const T& max4,
107  unsigned long c5, const T& min5, const T& max5,
108  unsigned long c6, const T& min6, const T& max6,
109  unsigned long c7, const T& min7, const T& max7,
110  unsigned long c8, const T& min8, const T& max8);
111 
112  template<typename T>
113  NtRectangularCut(unsigned long c0, const T& min0, const T& max0,
114  unsigned long c1, const T& min1, const T& max1,
115  unsigned long c2, const T& min2, const T& max2,
116  unsigned long c3, const T& min3, const T& max3,
117  unsigned long c4, const T& min4, const T& max4,
118  unsigned long c5, const T& min5, const T& max5,
119  unsigned long c6, const T& min6, const T& max6,
120  unsigned long c7, const T& min7, const T& max7,
121  unsigned long c8, const T& min8, const T& max8,
122  unsigned long c9, const T& min9, const T& max9);
123  //@}
124 
125  /**
126  // Constructor for a cut of arbitrary dimensionality. Note that
127  // it is impossible to specify a box with negative side length.
128  */
129  template<typename T>
130  NtRectangularCut(const std::vector<unsigned long>& ntupleColumns,
131  const BoxND<T>& acceptedBox);
132 
133  /**
134  // Add a cut on column contents to the set. Returns *this, so that
135  // a bunch of calls of this method can be stacked.
136  */
137  template<typename T>
139  const T& min, const T& max,
140  bool inverted=false);
141 
142  /** Add all cuts from another object of similar kind */
143  template<typename T>
145 
146  /** Invert the meaning of all cuts added so far to this object */
147  void invert();
148 
149  /** The number of cuts defined so far */
150  inline unsigned long dim() const {return nCuts_;}
151 
152  /** In this method, cutNumber parameter should be less than dim() */
153  inline unsigned long cutColumn(const unsigned long cutNumber) const
154  {return cutData_.at(cutNumber).col;}
155 
156  /**
157  // In principle, min can be larger than max. Because of this, we
158  // can't just return Interval<Numeric> when inspecting cut values.
159  */
160  void cutInterval(unsigned long cutNumber,
161  Numeric* minValue, Numeric* maxValue) const;
162 
163  /** Was the cut meaning inverted? */
164  bool isInverted(unsigned long cutNumber) const;
165 
166  /**
167  // The following method returns the number of unique columns.
168  // It may be useful to check that nUniqueColumns() == dim() after
169  // the cut is fully constructed (unless you intentionally include
170  // more than one cut for some column).
171  */
172  unsigned long nUniqueColumns() const;
173 
174  /**
175  // Convert to column vector / cut box. Intervals in which
176  // min >= max are converted into [min, min]. "acceptedBox"
177  // method will throw std::invalid_argument if any of the
178  // cuts have been inverted.
179  */
180  std::vector<unsigned long> ntupleColumns() const;
181  BoxND<Numeric> acceptedBox() const;
182 
183  /** Operator which returns "true" if an ntuple row passes the cut */
184  bool operator()(unsigned long rowNumber,
185  const Numeric* row, unsigned long nCols) const;
186 
187  /** Comparison for equality */
188  bool operator==(const NtRectangularCut<Numeric>& r) const;
189 
190  /** Logical negation of operator== */
191  inline bool operator!=(const NtRectangularCut<Numeric>& r) const
192  {return !(*this == r);}
193 
194  //@{
195  /** Method related to "geners" I/O */
196  inline gs::ClassId classId() const {return gs::ClassId(*this);}
197  bool write(std::ostream& of) const;
198  //@}
199 
200  static const char* classname();
201  static inline unsigned version() {return 1;}
202  static void restore(const gs::ClassId& id, std::istream& in,
203  NtRectangularCut* cut);
204  private:
205  struct RCut
206  {
207  inline RCut(unsigned long icol, Numeric imin,
208  Numeric imax, bool inv)
209  : col(icol), min(imin), max(imax), inverted(inv) {}
210 
211  inline bool operator==(const RCut& r) const
212  {return col == r.col && min == r.min &&
213  inverted == r.inverted &&
214  (max == r.max || (max <= min && r.max <= min));}
215 
216  unsigned long col;
217  Numeric min;
218  Numeric max;
219  bool inverted;
220  };
221 
222  std::vector<RCut> cutData_;
223  unsigned long nCuts_;
224 
225 #ifdef SWIG
226  public:
227  inline void addCut2(unsigned long cutColumn,
228  const Numeric& min, const Numeric& max,
229  bool inverted=false)
230  {addCut(cutColumn, min, max, inverted);}
231 
232  inline void addCut2(const NtRectangularCut<Numeric>& another)
233  {addCut(another);}
234 #endif // SWIG
235  };
236 }
237 
238 #include "npstat/stat/NtRectangularCut.icc"
239 
240 #endif // NPSTAT_NTRECTANGULARCUT_HH_
Template to represent rectangles, boxes, and hyperboxes.
Definition: NtRectangularCut.hh:31
bool isInverted(unsigned long cutNumber) const
NtRectangularCut(const std::vector< unsigned long > &ntupleColumns, const BoxND< T > &acceptedBox)
NtRectangularCut & addCut(unsigned long cutColumn, const T &min, const T &max, bool inverted=false)
bool operator==(const NtRectangularCut< Numeric > &r) const
std::vector< unsigned long > ntupleColumns() const
NtRectangularCut & addCut(const NtRectangularCut< T > &another)
gs::ClassId classId() const
Definition: NtRectangularCut.hh:196
unsigned long dim() const
Definition: NtRectangularCut.hh:150
bool operator()(unsigned long rowNumber, const Numeric *row, unsigned long nCols) const
unsigned long nUniqueColumns() const
NtRectangularCut(unsigned long c0, const T &min0, const T &max0)
unsigned long cutColumn(const unsigned long cutNumber) const
Definition: NtRectangularCut.hh:153
bool operator!=(const NtRectangularCut< Numeric > &r) const
Definition: NtRectangularCut.hh:191
void cutInterval(unsigned long cutNumber, Numeric *minValue, Numeric *maxValue) const
NtRectangularCut()
Definition: NtRectangularCut.hh:36
Definition: AbsArrayProjector.hh:14
Definition: BoxND.hh:25