Jpp  17.3.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JRange.cc File Reference

Example program to test JTOOLS::JRange operations. More...

#include <string>
#include <iostream>
#include <iomanip>
#include "JTools/JRange.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test JTOOLS::JRange operations.

Author
mdejong

Definition in file JRange.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 45 of file JRange.cc.

46 {
47  using namespace std;
48  using namespace JPP;
49 
50  int debug;
51 
52  try {
53 
54  JParser<> zap("Example program to test range operations.");
55 
56  zap['d'] = make_field(debug) = 3;
57 
58  zap(argc, argv);
59  }
60  catch(const exception &error) {
61  FATAL(error.what() << endl);
62  }
63 
64 
65  const double xmin = -1.0;
66  const double xmax = +1.0;
67  const double dx = (xmax - xmin) / 10;
68 
69  const int WIDTH = 12;
70 
71  {
72  typedef JRange<double> JRange_t;
73 
74  JRange_t A(JRange_t::DEFAULT_RANGE);
75  JRange_t B(xmin, xmax);
76 
77  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
78 
79  DEBUG(setw(WIDTH) << left << "inside" << ' ' << B << ' ' << setw(5) << x << ' ' << (B(x) ? "Y" : "N") << endl);
80 
81  ASSERT(B(x) == (x >= xmin && x <= xmax));
82 
83  A.include(x);
84  }
85 
86  DEBUG(setw(WIDTH) << left << "range" << ' ' << A << endl);
87 
88  ASSERT(A == B);
89 
90 
91  for (double x = xmin - (xmax - xmin); x < xmax + (xmax - xmin) + 0.5 * dx; x += dx) {
92 
93  double x1 = x;
94 
95  while (x1 <= xmin) { x1 += (xmax - xmin); }
96  while (x1 > xmax) { x1 -= (xmax - xmin); }
97 
98  ASSERT(x1 == B.mod(x));
99  }
100  }
101 
102  {
103  typedef JRange<double> JRange_t;
104 
105  const double x1 = -1.0;
106  const double x2 = +0.5;
107  const double x3 = -0.5;
108  const double x4 = +1.0;
109 
110  JRange_t A(x1, x2);
111  JRange_t B(x3, x4);
112 
113  DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl);
114 
115  ASSERT(overlap(A,B));
116  ASSERT(join(A,B) == JRange_t(x3,x2));
117  }
118 
119  {
120  typedef JRange<double> JRange_t;
121 
122  const double x1 = -1.0;
123  const double x2 = -0.5;
124  const double x3 = +0.5;
125  const double x4 = +1.0;
126 
127  JRange_t A(x1, x2);
128  JRange_t B(x3, x4);
129 
130  DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl);
131 
132  ASSERT(combine(A,B) == JRange_t(x1,x4));
133  }
134 
135  {
136  typedef JRange<double> JRange_t;
137 
138  const double x1 = -1.0;
139  const double x2 = -0.5;
140  const double x3 = +0.5;
141  const double x4 = +1.0;
142 
143  JRange_t A(x1, x2);
144  JRange_t B(x3, x4);
145 
146  DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl);
147 
148  ASSERT((A + B) == JRange_t(x1+x3,x2+x4));
149  }
150 
151  {
152  typedef JRange<double> JRange_t;
153 
154  struct __A__ {
155  __A__() : value(0.0) {}
156  __A__(const double value) : value(value) {}
157 
158  double get() const { return value; }
159 
160  double value;
161  };
162 
163  vector<__A__> buffer;
164 
165  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
166  buffer.push_back(x);
167  }
168 
169  JRange_t A;
170  JRange_t B;
171 
172  A.setRange(make_array(buffer.begin(), buffer.end(), &__A__::value));
173  B.setRange(make_array(buffer.begin(), buffer.end(), &__A__::get));
174 
175  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl);
176  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl);
177 
178  ASSERT(A == JRange_t(xmin,xmax));
179  ASSERT(B == JRange_t(xmin,xmax));
180  }
181 
182  {
183  typedef JRange<int> JRange_t;
184 
185  JRange_t A(1,100);
186  JRange_t B(A.getLowerLimit(), A.getLowerLimit());
187  JRange_t C(A.getUpperLimit(), A.getUpperLimit());
188 
189  ASSERT(overlap(A,B));
190  ASSERT(overlap(A,C));
191  ASSERT(!overlap(B,C));
192  }
193 
194  return 0;
195 }
const double xmax
Definition: JQuadrature.cc:24
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231
Utility class to parse command line options.
Definition: JParser.hh:1517
void overlap(T p, T q, const double Tmax_s)
Empty overlapping events.
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:668
T get(const JHead &header)
Get object from header.
static const double C
Physics constants.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
JRange< T, JComparator_t > combine(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Combine ranges.
Definition: JRange.hh:685
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62