UnBinData.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: UnBinData.h 34127 2010-06-25 09:13:45Z moneta $
00002 // Author: L. Moneta Wed Aug 30 11:15:23 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class UnBinData
00012 
00013 #ifndef ROOT_Fit_UnBinData
00014 #define ROOT_Fit_UnBinData
00015 
00016 #ifndef ROOT_Fit_DataVector
00017 #include "Fit/DataVector.h"
00018 #endif
00019 
00020 #ifndef ROOT_Math_Error
00021 #include "Math/Error.h"
00022 #endif
00023 
00024 
00025 
00026 namespace ROOT { 
00027 
00028    namespace Fit { 
00029 
00030 
00031 //___________________________________________________________________________________
00032 /** 
00033    Class describing the unbinned data sets (just x coordinates values) of any dimensions
00034 
00035               There is the option to construct UnBindata copying the data in (using the DataVector class) 
00036               or using pointer to external data (DataWrapper) class. 
00037               In general is found to be more efficient to copy the data. 
00038               In case of really large data sets for limiting memory consumption then the other option can be used
00039               Specialized constructor exists for using external data up to 3 dimensions. 
00040 
00041               When the data are copying in the number of points can be set later (or re-set) using Initialize and 
00042               the data are inserted one by one using the Add method. 
00043               It is mandatory to set the size before using the Add method.  
00044 
00045              @ingroup  FitData  
00046 */ 
00047 class UnBinData : public FitData { 
00048 
00049 public : 
00050 
00051    /**
00052       constructor from dimension of point  and max number of points (to pre-allocate vector)
00053     */
00054 
00055    explicit UnBinData(unsigned int maxpoints = 0, unsigned int dim = 1 );
00056 
00057 
00058    /**
00059       constructor from range and default option
00060     */
00061    explicit UnBinData (const DataRange & range,  unsigned int maxpoints = 0, unsigned int dim = 1);
00062 
00063    /**
00064       constructor from options and range
00065     */
00066    UnBinData (const DataOptions & opt, const DataRange & range,  unsigned int maxpoints = 0, unsigned int dim = 1 );
00067 
00068    /**
00069       constructor for 1D external data (data are not copied inside)
00070     */
00071    UnBinData(unsigned int n, const double * dataX );
00072 
00073    /**
00074       constructor for 2D external data (data are not copied inside)
00075     */
00076    UnBinData(unsigned int n, const double * dataX, const double * dataY );
00077 
00078    /**
00079       constructor for 3D external data (data are not copied inside)
00080     */
00081    UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ );
00082 
00083    /**
00084       constructor for multi-dim external data (data are not copied inside)
00085       Uses as argument an iterator of a list (or vector) containing the const double * of the data
00086       An example could be the std::vector<const double *>::begin
00087     */
00088    template<class Iterator> 
00089    UnBinData(unsigned int n, unsigned int dim, Iterator dataItr ) : 
00090       FitData( ), 
00091       fDim(dim), 
00092       fNPoints(n),
00093       fDataVector(0)
00094    { 
00095       fDataWrapper = new DataWrapper(dim, dataItr);
00096    } 
00097 
00098    /**
00099       constructor for 1D data and a range (data are copied inside according to the given range)
00100     */
00101    UnBinData(unsigned int maxpoints, const double * dataX, const DataRange & range);
00102 
00103    /**
00104       constructor for 2D data and a range (data are copied inside according to the given range)
00105     */
00106    UnBinData(unsigned int maxpoints, const double * dataX, const double * dataY, const DataRange & range);
00107 
00108    /**
00109       constructor for 3D data and a range (data are copied inside according to the given range)
00110     */
00111    UnBinData(unsigned int maxpoints, const double * dataX, const double * dataY, const double * dataZ, const DataRange & range);
00112 
00113    /**
00114       constructor for multi-dim external data and a range (data are copied inside according to the range)
00115       Uses as argument an iterator of a list (or vector) containing the const double * of the data
00116       An example could be the std::vector<const double *>::begin
00117     */
00118    template<class Iterator> 
00119    UnBinData(unsigned int maxpoints, unsigned int dim, Iterator dataItr, const DataRange & range ) : 
00120       FitData( ), 
00121       fDim(dim), 
00122       fNPoints(0),
00123       fDataVector(0),
00124       fDataWrapper(0)
00125    { 
00126       unsigned int n = dim*maxpoints; 
00127       if ( n > MaxSize() ) {
00128          MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n );
00129       }
00130       else if (n > 0) { 
00131          fDataVector = new DataVector(n);
00132 
00133          // use data wrapper to get the data
00134          ROOT::Fit::DataWrapper wdata(dim, dataItr); 
00135          for (unsigned int i = 0; i < maxpoints; ++i) { 
00136             bool isInside = true;
00137             for (unsigned int icoord = 0; icoord < dim; ++icoord)  
00138                isInside &= range.IsInside( wdata.Coords(i)[icoord], icoord ); 
00139             if ( isInside ) Add(wdata.Coords(i)); 
00140          }
00141          if (fNPoints < maxpoints) (fDataVector->Data()).resize(fDim*fNPoints);
00142       } 
00143    }
00144 
00145 
00146 private: 
00147    /// copy constructor (private) 
00148    UnBinData(const UnBinData &) : FitData() {}
00149    /// assignment operator  (private) 
00150    UnBinData & operator= (const UnBinData &) { return *this; } 
00151 
00152 public: 
00153 
00154 #ifdef LATER
00155    /**
00156       Create from a compatible UnBinData set
00157     */
00158    
00159    UnBinData (const UnBinData & data , const DataOptions & opt, const DataRange & range) : 
00160       DataVector(opt,range, data.DataSize() ), 
00161       fDim(data.fDim),
00162       fNPoints(data.fNPoints) 
00163    {
00164 //       for (Iterator itr = begin; itr != end; ++itr) 
00165 //          if (itr->IsInRange(range) )
00166 //             Add(*itr); 
00167    } 
00168 #endif
00169 
00170    /**
00171       destructor, delete pointer to internal data or external data wrapper
00172     */
00173    virtual ~UnBinData() {
00174       if (fDataVector) delete fDataVector; 
00175       if (fDataWrapper) delete fDataWrapper; 
00176    }
00177 
00178    /**
00179       preallocate a data set given size and dimension
00180       if a vector already exists with correct dimension (point size) extend the existing one
00181       to a total size of maxpoints (equivalent to a Resize)
00182     */
00183    void Initialize(unsigned int maxpoints, unsigned int dim = 1);
00184 
00185    
00186    /**
00187       return fit point size (for unbin data is equivalent to coordinate dimension)
00188     */
00189    unsigned int PointSize() const { 
00190       return fDim; 
00191    }
00192 
00193    /**
00194       return size of internal data vector (is 0 for external data) 
00195     */
00196    unsigned int DataSize() const { 
00197       return (fDataVector) ? fDataVector->Size() : 0;
00198    }
00199    
00200    /**
00201       add one dim coordinate data
00202    */
00203    void Add(double x) { 
00204       int index = fNPoints*PointSize(); 
00205       assert(fDataVector != 0);
00206       assert(PointSize() == 1);
00207       assert (index + PointSize() <= DataSize() ); 
00208 
00209       (fDataVector->Data())[ index ] = x;
00210 
00211       fNPoints++;
00212    }
00213 
00214    /**
00215       add 2-dim coordinate data
00216    */
00217    void Add(double x, double y) { 
00218       int index = fNPoints*PointSize(); 
00219       assert(fDataVector != 0);
00220       assert(PointSize() == 2);
00221       assert (index + PointSize() <= DataSize() ); 
00222 
00223       (fDataVector->Data())[ index ] = x;
00224       (fDataVector->Data())[ index+1 ] = y;
00225 
00226       fNPoints++;
00227    }
00228 
00229    /**
00230       add 3-dim coordinate data
00231    */
00232    void Add(double x, double y, double z) { 
00233       int index = fNPoints*PointSize(); 
00234       assert(fDataVector != 0);
00235       assert(PointSize() == 3);
00236       assert (index + PointSize() <= DataSize() ); 
00237 
00238       (fDataVector->Data())[ index ] = x;
00239       (fDataVector->Data())[ index+1 ] = y;
00240       (fDataVector->Data())[ index+2 ] = z;
00241 
00242       fNPoints++;
00243    }
00244 
00245    /**
00246       add multi-dim coordinate data
00247    */
00248    void Add(const double *x) { 
00249       int index = fNPoints*PointSize(); 
00250 
00251       assert(fDataVector != 0);
00252       assert (index + PointSize() <= DataSize() ); 
00253 
00254       double * itr = &( (fDataVector->Data()) [ index ]);
00255 
00256       for (unsigned int i = 0; i < fDim; ++i) 
00257          *itr++ = x[i]; 
00258 
00259       fNPoints++;
00260    }
00261 
00262    /**
00263       return pointer to coordinate data
00264     */
00265    const double * Coords(unsigned int ipoint) const { 
00266       if (fDataVector) 
00267          return &( (fDataVector->Data()) [ ipoint*PointSize() ] );
00268       else 
00269          return fDataWrapper->Coords(ipoint); 
00270    }
00271 
00272 
00273    /**
00274       resize the vector to the given npoints 
00275     */
00276    void Resize (unsigned int npoints); 
00277 
00278 
00279    /**
00280       return number of contained points 
00281     */ 
00282    unsigned int NPoints() const { return fNPoints; } 
00283 
00284    /**
00285       return number of contained points 
00286     */ 
00287    unsigned int Size() const { return fNPoints; }
00288 
00289    /**
00290       return coordinate data dimension
00291     */ 
00292    unsigned int NDim() const { return fDim; } 
00293 
00294 protected: 
00295 
00296    void SetNPoints(unsigned int n) { fNPoints = n; }
00297 
00298 private: 
00299 
00300    unsigned int fDim;         // coordinate data dimension
00301    unsigned int fNPoints;     // numer of fit points
00302    
00303    DataVector * fDataVector;     // pointer to internal data vector (null for external data)
00304    DataWrapper * fDataWrapper;   // pointer to structure wrapping external data (null when data are copied in)
00305 
00306 }; 
00307 
00308   
00309    } // end namespace Fit
00310 
00311 } // end namespace ROOT
00312 
00313 
00314 
00315 #endif /* ROOT_Fit_UnBinData */

Generated on Tue Jul 5 14:25:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1