ROOT   6.14/05 Reference Guide
DataRange.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Wed Aug 30 11:05:02 2006
3
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10
11 // Implementation file for class DataRange
12
13 #include "Fit/DataRange.h"
14 #include "Math/Error.h"
15
16 #include <algorithm>
17 #include <limits>
18
19 namespace ROOT {
20
21  namespace Fit {
22
23 DataRange::DataRange(double xmin, double xmax) :
24  fRanges( std::vector<RangeSet> (1) )
25 {
26  // construct a range for [xmin, xmax]
27  if (xmin < xmax) {
28  RangeSet rx(1);
29  rx[0] = std::make_pair(xmin, xmax);
30  fRanges[0] = rx;
31  }
32 }
33
34
35 DataRange::DataRange(double xmin, double xmax, double ymin, double ymax) :
36  fRanges( std::vector<RangeSet> (2) )
37 {
38  // construct a range for [xmin, xmax] , [ymin, ymax]
39  if (xmin < xmax) {
40  RangeSet rx(1);
41  rx[0] = std::make_pair(xmin, xmax);
42  fRanges[0] = rx;
43  }
44
45  if (ymin < ymax) {
46  RangeSet ry(1);
47  ry[0] = std::make_pair(ymin, ymax);
48  fRanges[1] = ry;
49  }
50 }
51
52 DataRange::DataRange(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) :
53  fRanges( std::vector<RangeSet> (3) )
54 {
55  // construct a range for [xmin, xmax] , [ymin, ymax] , [zmin, zmax]
56  if (xmin < xmax) {
57  RangeSet rx(1);
58  rx[0] = std::make_pair(xmin, xmax);
59  fRanges[0] = rx;
60  }
61  if (ymin < ymax) {
62  RangeSet ry(1);
63  ry[0] = std::make_pair(ymin, ymax);
64  fRanges[1] = ry;
65  }
66  if (zmin < zmax) {
67  RangeSet rz(1);
68  rz[0] = std::make_pair(zmin, zmax);
69  fRanges[2] = rz;
70  }
71 }
72
73 bool lessRange( const std::pair<double,double> & r1, const std::pair<double,double> & r2 ) {
74  // compare ranges using max position so in case of included ranges smaller one comes first
75  return r1.second < r2.second;
76 }
77
78 std::pair<double, double> DataRange::operator() (unsigned int icoord,unsigned int irange) const {
79  if ( Size(icoord) > irange )
80  return fRanges[icoord].at(irange);
81  else if (irange == 0) {
82  // return [-inf +inf] for the other dimension
83  double xmin = 0; double xmax = 0;
84  GetInfRange(xmin,xmax);
85  return std::make_pair(xmin,xmax);
86  }
87  else {
88  // in case the irange-th does not exist for the given coordinate
89  MATH_ERROR_MSG("DataRange::operator()","invalid range number - return (0,0)");
90  return std::pair<double,double>(0,0);
91  }
92 }
93
94 void DataRange::AddRange(unsigned int icoord , double xmin, double xmax ) {
95  // add a range [xmin,xmax] for the new coordinate icoord
96
97  if (xmin >= xmax) return; // no op in case of bad values
98
99  // case the coordinate is larger than the current allocated vector size
100  if (icoord >= fRanges.size() ) {
101  RangeSet rx(1);
102  rx[0] = std::make_pair(xmin, xmax);
103  fRanges.resize(icoord+1);
104  fRanges[icoord] = rx;
105  return;
106  }
107  RangeSet & rs = fRanges[icoord];
108  // case the vector of the ranges is empty in the given coordinate
109  if ( rs.size() == 0) {
110  rs.push_back(std::make_pair(xmin,xmax) );
111  return;
112  }
113  // case of an already existing range
114  // need to establish a policy (use OR or AND )
115
116  CleanRangeSet(icoord,xmin,xmax);
117  // add the new one
118  rs.push_back(std::make_pair(xmin,xmax) );
119  // sort range in increasing values of xmax
120  std::sort( rs.begin(), rs.end() , lessRange);
121
122 }
123
124 void DataRange::SetRange(unsigned int icoord , double xmin, double xmax ) {
125  // set a new range [xmin,xmax] for the new coordinate icoord
126
127  if (xmin >= xmax) return; // no op in case of bad values
128
129  // case the coordinate is larger than the current allocated vector size
130  if (icoord >= fRanges.size() ) {
131  fRanges.resize(icoord+1);
132  RangeSet rs(1);
133  rs[0] = std::make_pair(xmin, xmax);
134  fRanges[icoord] = rs;
135  return;
136  }
138  RangeSet & rs = fRanges[icoord];
139  // deleting existing ones if (exists)
140  if (rs.size() > 1) MATH_WARN_MSG("DataRange::SetRange","remove existing range and keep only the set one");
141  rs.resize(1);
142  rs[0] = std::make_pair(xmin, xmax);
143  return;
144 }
145
146 bool DataRange::IsInside(double x, unsigned int icoord ) const {
147  // check if a point is in range
148
149  if (Size(icoord) == 0) return true; // no range existing (is like -inf, +inf)
150  const RangeSet & ranges = fRanges[icoord];
151  for (RangeSet::const_iterator itr = ranges.begin(); itr != ranges.end(); ++itr) {
152  if ( x < (*itr).first ) return false;
153  if ( x <= (*itr).second) return true;
154  }
155  return false; // point is larger than last xmax
156 }
157
158 void DataRange::Clear(unsigned int icoord ) {
159  // remove all ranges for coordinate icoord
160  if (Size(icoord) == 0) return; // no op in this case
161  fRanges[icoord].clear();
162 }
163
164
165 void DataRange::CleanRangeSet(unsigned int icoord, double xmin, double xmax) {
166  // remove all the existing ranges between xmin and xmax
167  // called when a new range is inserted
168
169  // loop on existing ranges
170  RangeSet & ranges = fRanges[icoord];
171  for (RangeSet::iterator itr = ranges.begin(); itr != ranges.end(); ++itr) {
172  // delete included ranges
173  if ( itr->first >= xmin && itr->second <= xmax) {
174  itr = ranges.erase(itr);
175  // itr goes to next element, so go back before adding
176  --itr;
177  }
178  }
179
180 }
181
182 void DataRange::GetInfRange(double &xmin, double &xmax) {
183  // get the full range [-inf, +inf] for xmin and xmax
184  xmin = -std::numeric_limits<double>::infinity();
185  xmax = std::numeric_limits<double>::infinity();
186 }
187
188  } // end namespace Fit
189
190 } // end namespace ROOT
191
void Clear(unsigned int icoord=0)
clear all ranges in one coordinate (is now -inf, +inf)
Definition: DataRange.cxx:158
float xmin
Definition: THbookFile.cxx:93
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
float ymin
Definition: THbookFile.cxx:93
RangeIntervals fRanges
Definition: DataRange.h:231
STL namespace.
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:47
void CleanRangeSet(unsigned int icoord, double xmin, double xmax)
internal function to remove all the existing ranges between xmin and xmax called when a new range is ...
Definition: DataRange.cxx:165
bool lessRange(const std::pair< double, double > &r1, const std::pair< double, double > &r2)
Definition: DataRange.cxx:73
bool IsInside(double x, unsigned int icoord=0) const
check if a point is inside the range for the given coordinate
Definition: DataRange.cxx:146
Double_t x[n]
Definition: legend1.C:17
DataRange(unsigned int dim=1)
Default constructor (infinite range)
Definition: DataRange.h:44
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
float ymax
Definition: THbookFile.cxx:93
void SetRange(unsigned int icoord, double xmin, double xmax)
set a range [xmin,xmax] for the new coordinate icoord If more range exists for other coordinates...
Definition: DataRange.cxx:124
unsigned int Size(unsigned int icoord=0) const
return range size for coordinate icoord (starts from zero) Size == 0 indicates no range is present [-...
Definition: DataRange.h:70
float xmax
Definition: THbookFile.cxx:93
std::vector< std::pair< double, double > > RangeSet
Definition: DataRange.h:38
void AddRange(unsigned int icoord, double xmin, double xmax)
add a range [xmin,xmax] for the new coordinate icoord Adding a range does not delete existing one...
Definition: DataRange.cxx:94
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
std::pair< double, double > operator()(unsigned int icoord=0, unsigned int irange=0) const
return the i-th range for the coordinate icoord.
Definition: DataRange.cxx:78
static void GetInfRange(double &x1, double &x2)
Definition: DataRange.cxx:182