// @(#)root/mathcore:$Id$
// Author: L. Moneta Wed Aug 30 11:15:23 2006

/**********************************************************************
 *                                                                    *
 * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
 *                                                                    *
 *                                                                    *
 **********************************************************************/

// Header file for class DataVector

#ifndef ROOT_Fit_DataVector
#define ROOT_Fit_DataVector

/**
@defgroup FitData Fit Data Classes

Classes for describing the input data for fitting

@ingroup Fit
*/


// #ifndef ROOT_Fit_DataVectorfwd
// #include "Fit/DataVectorfwd.h"
// #endif

#ifndef ROOT_Fit_DataOptions
#include "Fit/DataOptions.h"
#endif


#ifndef ROOT_Fit_DataRange
#include "Fit/DataRange.h"
#endif


#include <vector>
#include <cassert>
#include <iostream>


namespace ROOT {

   namespace Fit {


/**
   Base class for all the fit data types

   @ingroup FitData
 */

class FitData {

public:

   /// construct with default option and data range
   FitData() {}

   /// dummy virtual destructor
   virtual ~FitData() {}

   /// construct passing options and default data range
   FitData(const DataOptions & opt) :
      fOptions(opt)
   {}


   /// construct passing range and default options
   FitData(const DataRange & range) :
      fRange(range)
   {}

   /// construct passing options and data range
   FitData (const DataOptions & opt, const DataRange & range) :
      fOptions(opt),
      fRange(range)
   {}

   /**
      access to options
    */
   const DataOptions & Opt() const { return fOptions; }
   DataOptions & Opt() { return fOptions; }

   /**
      access to range
    */
   const DataRange & Range() const { return fRange; }

   // range cannot be modified afterwards
   // since fit method functions use all data

   /**
       define a max size to avoid allocating too large arrays
   */
   static unsigned int MaxSize()  {
      return (unsigned int) (-1) / sizeof (double);
   }


private:

      DataOptions fOptions;
      DataRange   fRange;

};



/**
   class holding the fit data points. It is template on the type of point,
   which can be for example a binned or unbinned point.
   It is basicaly a wrapper on an std::vector

   @ingroup FitData

*/

class DataVector {

public:


   typedef std::vector<double>      FData;

   /**
      default constructor for a vector of N -data
   */
   explicit DataVector (size_t n ) :
      fData(std::vector<double>(n))

   {
      //if (n!=0) fData.reserve(n);
   }


   /**
      Destructor (no operations)
   */
   ~DataVector ()  {}

   // use default copy constructor and assignment operator


   /**
      const access to underlying vector
    */
   const FData & Data() const { return fData; }

   /**
      non-const access to underlying vector (in case of insertion/deletion) and iterator
    */
   FData & Data()  { return fData; }

#ifndef __CINT__
   /**
      const iterator access
   */
   typedef FData::const_iterator const_iterator;
   typedef FData::iterator iterator;

   const_iterator begin() const { return fData.begin(); }
   const_iterator end() const { return fData.begin()+fData.size(); }

   /**
      non-const iterator access
   */
   iterator begin() { return fData.begin(); }
   iterator end()   { return fData.end(); }

#endif
   /**
      access to the point
    */
   const double & operator[] (unsigned int i)  const { return fData[i]; }
   double & operator[] (unsigned int i)   { return fData[i]; }


   /**
      full size of data vector (npoints * point size)
    */
   size_t Size() const { return fData.size(); }


private:

      FData fData;
};


//       // usefule typedef's of DataVector
//       class BinPoint;

//       // declaration for various type of data vectors
//       typedef DataVector<ROOT::Fit::BinPoint>                    BinData;
//       typedef DataVector<ROOT::Fit::BinPoint>::const_iterator    BinDataIterator;

/**
   class maintaining a pointer to external data
   Using this class avoids copying the data when performing a fit
   NOTE: this class is not thread-safe and should not be used in parallel fits


   @ingroup FitData
 */

class DataWrapper {

public:

   /**
      specialized constructor for 1D data without errors and values
    */
   explicit DataWrapper(const double * dataX ) :
      fDim(1),
      fValues(0),
      fErrors(0),
      fCoords(std::vector<const double * >(1) ),
      fX(std::vector<double>(1) )
   {
      fCoords[0] = dataX;
   }


   /**
      constructor for 1D data (if errors are not present a null pointer should be passed)
    */
   DataWrapper(const double * dataX, const double * val, const double * eval , const double * ex ) :
      fDim(1),
      fValues(val),
      fErrors(eval),
      fCoords(std::vector<const double * >(1) ),
      fErrCoords(std::vector<const double * >(1) ),
      fX(std::vector<double>(1) ),
      fErr(std::vector<double>(1) )
   {
      fCoords[0] = dataX;
      fErrCoords[0] = ex;
   }

   /**
      constructor for 2D data (if errors are not present a null pointer should be passed)
    */
   DataWrapper(const double * dataX, const double * dataY, const double * val, const double * eval, const double * ex , const double * ey  ) :
      fDim(2),
      fValues(val),
      fErrors(eval),
      fCoords(std::vector<const double * >(2) ),
      fErrCoords(std::vector<const double * >(2) ),
      fX(std::vector<double>(2) ),
      fErr(std::vector<double>(2) )
   {
      fCoords[0] = dataX;
      fCoords[1] = dataY;
      fErrCoords[0] = ex;
      fErrCoords[1] = ey;
   }

   /**
      constructor for 3D data (if errors are not present a null pointer should be passed)
    */
   DataWrapper(const double * dataX, const double * dataY, const double * dataZ, const double * val, const double * eval, const double * ex , const double * ey, const double * ez  ) :
      fDim(3),
      fValues(val),
      fErrors(eval),
      fCoords(std::vector<const double * >(3) ),
      fErrCoords(std::vector<const double * >(3) ),
      fX(std::vector<double>(3) ),
      fErr(std::vector<double>(3) )
   {
      fCoords[0] = dataX;
      fCoords[1] = dataY;
      fCoords[2] = dataZ;
      fErrCoords[0] = ex;
      fErrCoords[1] = ey;
      fErrCoords[2] = ez;
   }

   /**
      constructor for multi-dim data without  errors
    */
   template<class Iterator>
   DataWrapper(unsigned int dim,  Iterator  coordItr ) :
      fDim(dim),
      fValues(0),
      fErrors(0),
      fCoords(std::vector<const double * >(coordItr, coordItr+dim) ),
      fX(std::vector<double>(dim) )
   { }


   /**
      constructor for multi-dim data with errors and values (if errors are not present a null pointer should be passed)
    */
   template<class Iterator>
   DataWrapper(size_t dim, Iterator coordItr, const double * val, const double * eval, Iterator errItr ) :
      // use size_t for dim to avoid allocating huge vector on 64 bits when dim=-1
      fDim(dim),
      fValues(val),
      fErrors(eval),
      fCoords(std::vector<const double * >(coordItr, coordItr+dim) ),
      fErrCoords(std::vector<const double * >(errItr, errItr+dim) ),
      fX(std::vector<double>(dim) ),
      fErr(std::vector<double>(dim) )
   { }

   // destructor
   ~DataWrapper() {
      //printf("Delete Data wrapper\n");
      // no operations
   }

   // use default copy constructor and assignment operator
   // copy the pointer of the data not the data


   const double * Coords(unsigned int ipoint) const {
      for (unsigned int i = 0; i < fDim; ++i) {
         const double * x = fCoords[i];
         assert (x != 0);
         fX[i] = x[ipoint];
      }
      return &fX.front();
   }

   double Coord(unsigned int ipoint, unsigned int icoord) const {
         const double * x = fCoords[icoord];
         assert (x != 0);
         return  x[ipoint];
   }


   const double * CoordErrors(unsigned int ipoint) const {
      for (unsigned int i = 0; i < fDim; ++i) {
         const double * err = fErrCoords[i];
         if (err == 0) return 0;
         fErr[i] = err[ipoint];
      }
      return &fErr.front();
   }

   double CoordError(unsigned int ipoint, unsigned int icoord) const {
         const double * err = fErrCoords[icoord];
         return  (err != 0) ? err[ipoint] : 0;
   }


   double Value(unsigned int ipoint) const {
      return fValues[ipoint];
   }

   double Error(unsigned int ipoint) const {
      return (fErrors) ?  fErrors[ipoint]  : 0. ;
   }



private:


   unsigned int fDim;
   const double * fValues;
   const double * fErrors;
   std::vector<const double *> fCoords;
   std::vector<const double *> fErrCoords;
   // cached vector to return x[] and errors on x
   mutable std::vector<double> fX;
   mutable std::vector<double> fErr;

};



   } // end namespace Fit

} // end namespace ROOT



#endif /* ROOT_Fit_DataVector */
 DataVector.h:1
 DataVector.h:2
 DataVector.h:3
 DataVector.h:4
 DataVector.h:5
 DataVector.h:6
 DataVector.h:7
 DataVector.h:8
 DataVector.h:9
 DataVector.h:10
 DataVector.h:11
 DataVector.h:12
 DataVector.h:13
 DataVector.h:14
 DataVector.h:15
 DataVector.h:16
 DataVector.h:17
 DataVector.h:18
 DataVector.h:19
 DataVector.h:20
 DataVector.h:21
 DataVector.h:22
 DataVector.h:23
 DataVector.h:24
 DataVector.h:25
 DataVector.h:26
 DataVector.h:27
 DataVector.h:28
 DataVector.h:29
 DataVector.h:30
 DataVector.h:31
 DataVector.h:32
 DataVector.h:33
 DataVector.h:34
 DataVector.h:35
 DataVector.h:36
 DataVector.h:37
 DataVector.h:38
 DataVector.h:39
 DataVector.h:40
 DataVector.h:41
 DataVector.h:42
 DataVector.h:43
 DataVector.h:44
 DataVector.h:45
 DataVector.h:46
 DataVector.h:47
 DataVector.h:48
 DataVector.h:49
 DataVector.h:50
 DataVector.h:51
 DataVector.h:52
 DataVector.h:53
 DataVector.h:54
 DataVector.h:55
 DataVector.h:56
 DataVector.h:57
 DataVector.h:58
 DataVector.h:59
 DataVector.h:60
 DataVector.h:61
 DataVector.h:62
 DataVector.h:63
 DataVector.h:64
 DataVector.h:65
 DataVector.h:66
 DataVector.h:67
 DataVector.h:68
 DataVector.h:69
 DataVector.h:70
 DataVector.h:71
 DataVector.h:72
 DataVector.h:73
 DataVector.h:74
 DataVector.h:75
 DataVector.h:76
 DataVector.h:77
 DataVector.h:78
 DataVector.h:79
 DataVector.h:80
 DataVector.h:81
 DataVector.h:82
 DataVector.h:83
 DataVector.h:84
 DataVector.h:85
 DataVector.h:86
 DataVector.h:87
 DataVector.h:88
 DataVector.h:89
 DataVector.h:90
 DataVector.h:91
 DataVector.h:92
 DataVector.h:93
 DataVector.h:94
 DataVector.h:95
 DataVector.h:96
 DataVector.h:97
 DataVector.h:98
 DataVector.h:99
 DataVector.h:100
 DataVector.h:101
 DataVector.h:102
 DataVector.h:103
 DataVector.h:104
 DataVector.h:105
 DataVector.h:106
 DataVector.h:107
 DataVector.h:108
 DataVector.h:109
 DataVector.h:110
 DataVector.h:111
 DataVector.h:112
 DataVector.h:113
 DataVector.h:114
 DataVector.h:115
 DataVector.h:116
 DataVector.h:117
 DataVector.h:118
 DataVector.h:119
 DataVector.h:120
 DataVector.h:121
 DataVector.h:122
 DataVector.h:123
 DataVector.h:124
 DataVector.h:125
 DataVector.h:126
 DataVector.h:127
 DataVector.h:128
 DataVector.h:129
 DataVector.h:130
 DataVector.h:131
 DataVector.h:132
 DataVector.h:133
 DataVector.h:134
 DataVector.h:135
 DataVector.h:136
 DataVector.h:137
 DataVector.h:138
 DataVector.h:139
 DataVector.h:140
 DataVector.h:141
 DataVector.h:142
 DataVector.h:143
 DataVector.h:144
 DataVector.h:145
 DataVector.h:146
 DataVector.h:147
 DataVector.h:148
 DataVector.h:149
 DataVector.h:150
 DataVector.h:151
 DataVector.h:152
 DataVector.h:153
 DataVector.h:154
 DataVector.h:155
 DataVector.h:156
 DataVector.h:157
 DataVector.h:158
 DataVector.h:159
 DataVector.h:160
 DataVector.h:161
 DataVector.h:162
 DataVector.h:163
 DataVector.h:164
 DataVector.h:165
 DataVector.h:166
 DataVector.h:167
 DataVector.h:168
 DataVector.h:169
 DataVector.h:170
 DataVector.h:171
 DataVector.h:172
 DataVector.h:173
 DataVector.h:174
 DataVector.h:175
 DataVector.h:176
 DataVector.h:177
 DataVector.h:178
 DataVector.h:179
 DataVector.h:180
 DataVector.h:181
 DataVector.h:182
 DataVector.h:183
 DataVector.h:184
 DataVector.h:185
 DataVector.h:186
 DataVector.h:187
 DataVector.h:188
 DataVector.h:189
 DataVector.h:190
 DataVector.h:191
 DataVector.h:192
 DataVector.h:193
 DataVector.h:194
 DataVector.h:195
 DataVector.h:196
 DataVector.h:197
 DataVector.h:198
 DataVector.h:199
 DataVector.h:200
 DataVector.h:201
 DataVector.h:202
 DataVector.h:203
 DataVector.h:204
 DataVector.h:205
 DataVector.h:206
 DataVector.h:207
 DataVector.h:208
 DataVector.h:209
 DataVector.h:210
 DataVector.h:211
 DataVector.h:212
 DataVector.h:213
 DataVector.h:214
 DataVector.h:215
 DataVector.h:216
 DataVector.h:217
 DataVector.h:218
 DataVector.h:219
 DataVector.h:220
 DataVector.h:221
 DataVector.h:222
 DataVector.h:223
 DataVector.h:224
 DataVector.h:225
 DataVector.h:226
 DataVector.h:227
 DataVector.h:228
 DataVector.h:229
 DataVector.h:230
 DataVector.h:231
 DataVector.h:232
 DataVector.h:233
 DataVector.h:234
 DataVector.h:235
 DataVector.h:236
 DataVector.h:237
 DataVector.h:238
 DataVector.h:239
 DataVector.h:240
 DataVector.h:241
 DataVector.h:242
 DataVector.h:243
 DataVector.h:244
 DataVector.h:245
 DataVector.h:246
 DataVector.h:247
 DataVector.h:248
 DataVector.h:249
 DataVector.h:250
 DataVector.h:251
 DataVector.h:252
 DataVector.h:253
 DataVector.h:254
 DataVector.h:255
 DataVector.h:256
 DataVector.h:257
 DataVector.h:258
 DataVector.h:259
 DataVector.h:260
 DataVector.h:261
 DataVector.h:262
 DataVector.h:263
 DataVector.h:264
 DataVector.h:265
 DataVector.h:266
 DataVector.h:267
 DataVector.h:268
 DataVector.h:269
 DataVector.h:270
 DataVector.h:271
 DataVector.h:272
 DataVector.h:273
 DataVector.h:274
 DataVector.h:275
 DataVector.h:276
 DataVector.h:277
 DataVector.h:278
 DataVector.h:279
 DataVector.h:280
 DataVector.h:281
 DataVector.h:282
 DataVector.h:283
 DataVector.h:284
 DataVector.h:285
 DataVector.h:286
 DataVector.h:287
 DataVector.h:288
 DataVector.h:289
 DataVector.h:290
 DataVector.h:291
 DataVector.h:292
 DataVector.h:293
 DataVector.h:294
 DataVector.h:295
 DataVector.h:296
 DataVector.h:297
 DataVector.h:298
 DataVector.h:299
 DataVector.h:300
 DataVector.h:301
 DataVector.h:302
 DataVector.h:303
 DataVector.h:304
 DataVector.h:305
 DataVector.h:306
 DataVector.h:307
 DataVector.h:308
 DataVector.h:309
 DataVector.h:310
 DataVector.h:311
 DataVector.h:312
 DataVector.h:313
 DataVector.h:314
 DataVector.h:315
 DataVector.h:316
 DataVector.h:317
 DataVector.h:318
 DataVector.h:319
 DataVector.h:320
 DataVector.h:321
 DataVector.h:322
 DataVector.h:323
 DataVector.h:324
 DataVector.h:325
 DataVector.h:326
 DataVector.h:327
 DataVector.h:328
 DataVector.h:329
 DataVector.h:330
 DataVector.h:331
 DataVector.h:332
 DataVector.h:333
 DataVector.h:334
 DataVector.h:335
 DataVector.h:336
 DataVector.h:337
 DataVector.h:338
 DataVector.h:339
 DataVector.h:340
 DataVector.h:341
 DataVector.h:342
 DataVector.h:343
 DataVector.h:344
 DataVector.h:345
 DataVector.h:346
 DataVector.h:347
 DataVector.h:348
 DataVector.h:349
 DataVector.h:350
 DataVector.h:351
 DataVector.h:352
 DataVector.h:353
 DataVector.h:354
 DataVector.h:355
 DataVector.h:356
 DataVector.h:357
 DataVector.h:358
 DataVector.h:359
 DataVector.h:360
 DataVector.h:361
 DataVector.h:362
 DataVector.h:363
 DataVector.h:364
 DataVector.h:365
 DataVector.h:366
 DataVector.h:367
 DataVector.h:368
 DataVector.h:369
 DataVector.h:370
 DataVector.h:371
 DataVector.h:372
 DataVector.h:373
 DataVector.h:374
 DataVector.h:375
 DataVector.h:376
 DataVector.h:377
 DataVector.h:378
 DataVector.h:379
 DataVector.h:380
 DataVector.h:381
 DataVector.h:382
 DataVector.h:383