| 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
| 2 | //                         C.Magneville          04/99
 | 
|---|
| 3 | #ifndef GENERALDATA_SEEN
 | 
|---|
| 4 | #define GENERALDATA_SEEN
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include "objfio.h"
 | 
|---|
| 7 | #include <iostream>
 | 
|---|
| 8 | #include "pexceptions.h"
 | 
|---|
| 9 | #include "ppersist.h"
 | 
|---|
| 10 | #include "ntupintf.h"
 | 
|---|
| 11 | #include "poly.h"
 | 
|---|
| 12 | 
 | 
|---|
| 13 | namespace SOPHYA {
 | 
|---|
| 14 | 
 | 
|---|
| 15 | //  Forward class declaration for Fits handler
 | 
|---|
| 16 | template <class T>  class FitsHandler;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | class GeneralFit;
 | 
|---|
| 19 | 
 | 
|---|
| 20 | //================================================================
 | 
|---|
| 21 | // GENERALFITDATA
 | 
|---|
| 22 | //================================================================
 | 
|---|
| 23 | 
 | 
|---|
| 24 | //! Classe de stoquage de donnees a plusieurs variables avec erreur
 | 
|---|
| 25 | class GeneralFitData : public AnyDataObj , public NTupleInterface {
 | 
|---|
| 26 |   friend class GeneralFit;
 | 
|---|
| 27 |   friend class ObjFileIO<GeneralFitData>;
 | 
|---|
| 28 |   friend class FitsHandler<GeneralFitData>;
 | 
|---|
| 29 | public:
 | 
|---|
| 30 |   //! Valeurs par defaut pour l'utilisation des erreurs
 | 
|---|
| 31 |   enum {
 | 
|---|
| 32 |     Def_ErrF = 1, //!< erreurs sur F par defaut
 | 
|---|
| 33 |     Def_ErrX = 0  //!< pas d'erreurs sur les variables X,Y,... par defaut
 | 
|---|
| 34 |     };
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   GeneralFitData(uint_4 nVar, uint_4 nDataAlloc, uint_2 errx=0);
 | 
|---|
| 37 |   GeneralFitData(const GeneralFitData& data, bool clean=false);
 | 
|---|
| 38 |   GeneralFitData();
 | 
|---|
| 39 |   virtual ~GeneralFitData();
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   void SetDataPtr(int_4 ptr = 0);
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   void KillData(int_4 i);
 | 
|---|
| 44 |   void KillData(int_4 i1,int_4 i2);
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   void ValidData(int_4 i);
 | 
|---|
| 47 |   void ValidData(int_4 i1,int_4 i2);
 | 
|---|
| 48 |   void ValidData();
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   void RedefineData1(int_4 i,double x,double f,double err=Def_ErrF,double errx=Def_ErrX);
 | 
|---|
| 51 |   void RedefineData2(int_4 i,double x,double y,double f,double err=Def_ErrF
 | 
|---|
| 52 |                     ,double errx=Def_ErrX,double erry=Def_ErrX);
 | 
|---|
| 53 |   void RedefineData(int_4 i,double* xp,double f,double err=Def_ErrF,double* errxp=NULL);
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   void AddData1(double x, double f, double err=Def_ErrF,double errx=Def_ErrX);
 | 
|---|
| 56 |   void AddData2(double x, double y, double f, double err=Def_ErrF
 | 
|---|
| 57 |                ,double errx=Def_ErrX,double erry=Def_ErrX);
 | 
|---|
| 58 |   void AddData(double* xp, double f, double err=Def_ErrF,double* errxp=NULL);
 | 
|---|
| 59 |   void AddData(float* xp, float f, float err=Def_ErrF,float* errxp=NULL);
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   void SetData1(int_4 nData,double* x,double* f,double *err=NULL,double *errx=NULL);
 | 
|---|
| 62 |   void SetData1(int_4 nData,float* x,float* f,float* err=NULL,float *errx=NULL);
 | 
|---|
| 63 |   void SetData2(int_4 nData,double* x,double* y,double* f,double *err=NULL
 | 
|---|
| 64 |                ,double *errx=NULL,double *erry=NULL);
 | 
|---|
| 65 |   void SetData2(int_4 nData,float* x,float* y,float* f,float* err=NULL
 | 
|---|
| 66 |                ,float *errx=NULL,float *erry=NULL);
 | 
|---|
| 67 |   void SetData(int_4 nData,double** xp,double *f,double *err=NULL,double** errxp=NULL);
 | 
|---|
| 68 |   void SetData(int_4 nData,float** xp,float* f,float* err=NULL,float** errxp=NULL);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   void PrintStatus() const;
 | 
|---|
| 71 |   void PrintData(int_4 i) const;
 | 
|---|
| 72 |   void PrintData(int_4 i1,int_4 i2) const;
 | 
|---|
| 73 |   void PrintData() const;
 | 
|---|
| 74 |   void Show(ostream& os) const;
 | 
|---|
| 75 |   inline void Show() const {Show(cout);}
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   //! Operator equal
 | 
|---|
| 78 |   GeneralFitData& operator = (const GeneralFitData& g);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   //! Retourne la place restante dans la structure (nombre de donnees que l'on peut encore stoquer).
 | 
|---|
| 81 |   inline int_4 GetSpaceFree() const { return mNDataAlloc - mNData; }
 | 
|---|
| 82 |   //! Retourne le nombre de variables Xi
 | 
|---|
| 83 |   inline int_4 NVar()       const {return mNVar;}
 | 
|---|
| 84 |   //! Retourne le nombre de donnees
 | 
|---|
| 85 |   inline int_4 NData()      const {return mNData;}
 | 
|---|
| 86 |   //! Retourne le nombre de bonnes donnees (utilisees pour le fit)
 | 
|---|
| 87 |   inline int_4 NDataGood()  const {return mNDataGood;}
 | 
|---|
| 88 |   //! Retourne la place maximale allouee pour les donnees
 | 
|---|
| 89 |   inline int_4 NDataAlloc() const {return mNDataAlloc;}
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   //! Retourne 1 si point valide, sinon 0
 | 
|---|
| 92 |   inline unsigned short IsValid(int_4 i) const
 | 
|---|
| 93 |                 {if(i>=0 && i<mNData) return mOK[i]; else return 0;}
 | 
|---|
| 94 |   //! Retourne ``true'' si il y a des erreurs sur les variables d'abscisse, ``false'' sinon.
 | 
|---|
| 95 |   inline bool HasXErrors() {if(mErrXP) return true; else return false;}
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   //! Retourne l'abscisse pour 1 dimension (y=f(x)) donnee I
 | 
|---|
| 98 |   inline double X1(int_4 i) const
 | 
|---|
| 99 |                 {if(i>=0 && i<mNData) return mXP[i]; else return 0.;}
 | 
|---|
| 100 |   //! Retourne la 1er abscisse (X) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 101 |   inline double X(int_4 i) const
 | 
|---|
| 102 |                 {if(i>=0 && i<mNData) return mXP[i*mNVar]; else return 0.;}
 | 
|---|
| 103 |   //! Retourne la 2sd abscisse (Y) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 104 |   inline double Y(int_4 i) const
 | 
|---|
| 105 |                 {if(i>=0 && i<mNData && 1<mNVar) return mXP[i*mNVar+1]; else return 0.;}
 | 
|---|
| 106 |   //! etourne la 3ieme abscisse (Z) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 107 |   inline double Z(int_4 i) const
 | 
|---|
| 108 |                 {if(i>=0 && i<mNData && 2<mNVar) return mXP[i*mNVar+2]; else return 0.;}
 | 
|---|
| 109 |   //! Retourne la Jieme abscisse (Xj) pour (v=f(x0,x1,x2,...)) donnee I
 | 
|---|
| 110 |   inline double Absc(int_4 j,int_4 i) const
 | 
|---|
| 111 |                 {if(i>=0 && i<mNData && j<mNVar)return mXP[i*mNVar+j]; else return 0.;}
 | 
|---|
| 112 |   //! Retourne la valeur de la Ieme donnee
 | 
|---|
| 113 |   inline double Val(int_4 i) const
 | 
|---|
| 114 |                 {if(i>=0 && i<mNData) return mF[i]; else return 0.;}
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   //! Retourne l'erreur (dx) sur l'abscisse pour 1 dimension (y=f(x)) donnee I
 | 
|---|
| 117 |   inline double EX1(int_4 i) const
 | 
|---|
| 118 |                 {if(mErrXP && i>=0 && i<mNData) return mErrXP[i]; else return 0.;}
 | 
|---|
| 119 |   //! Retourne l'erreur (dx) sur la 1er abscisse (X) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 120 |   inline double EX(int_4 i) const
 | 
|---|
| 121 |                 {if(mErrXP && i>=0 && i<mNData) return mErrXP[i*mNVar]; else return 0.;}
 | 
|---|
| 122 |   //! Retourne l'erreur (dy) sur la 2sd abscisse (Y) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 123 |   inline double EY(int_4 i) const
 | 
|---|
| 124 |                 {if(mErrXP && i>=0 && i<mNData && 1<mNVar) return mErrXP[i*mNVar+1];
 | 
|---|
| 125 |                  else return 0.;}
 | 
|---|
| 126 |   //! Retourne l'erreur (dz) sur la 3ieme abscisse (Z) pour (v=f(x,y,z,...)) donnee I
 | 
|---|
| 127 |   inline double EZ(int_4 i) const
 | 
|---|
| 128 |                 {if(mErrXP && i>=0 && i<mNData && 2<mNVar) return mErrXP[i*mNVar+2];
 | 
|---|
| 129 |                  else return 0.;}
 | 
|---|
| 130 |   //! Retourne l'erreur (dxj) sur la Jieme abscisse (Xj) pour (v=f(x0,x1,x2,...)) donnee I
 | 
|---|
| 131 |   inline double EAbsc(int_4 j,int_4 i) const
 | 
|---|
| 132 |                 {if(mErrXP && i>=0 && i<mNData && j<mNVar) return mErrXP[i*mNVar+j];
 | 
|---|
| 133 |                  else return 0.;}
 | 
|---|
| 134 |   //! Retourne l'erreur de la Ieme donnee
 | 
|---|
| 135 |   inline double EVal(int_4 i) const
 | 
|---|
| 136 |                 {if(i>=0 && i<mNData) return mErr[i]; else return 0.;}
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   r_8* GetVec(int_4 n, r_8* ret=NULL) const;
 | 
|---|
| 139 |   r_4* GetVecR4(int_4 n, r_4* ret=NULL) const;
 | 
|---|
| 140 |   int_4 GetMnMx(int_4 var,int_4& imin,int_4& imax) const;
 | 
|---|
| 141 |   int_4 GetMnMx(int_4 var,double& min,double& max) const;
 | 
|---|
| 142 |   int_4 GetMeanSigma(int_4 var,double& mean,double& sigma,double min=1.,double max=-1.) const;
 | 
|---|
| 143 |   int_4 GetMoMeMed(int_4 var,double& mode,double& mean,double& median,
 | 
|---|
| 144 |                  double min=1.,double max=-1.,double coeff=0.8) const;
 | 
|---|
| 145 |   int_4 GetMode(int_4 var,double& mode,double min=1.,double max=-1.,double coeff=0.8) const;
 | 
|---|
| 146 |   double PolFit(int_4 varx,Poly& pol,int_4 degre,bool ey=true,double xc=0.) const;
 | 
|---|
| 147 |   double PolFit(int_4 varx,int_4 vary,Poly2& pol,int_4 degre1,int_4 degre2=-1,bool ez=true
 | 
|---|
| 148 |                ,double xc=0.,double yc=0.) const;
 | 
|---|
| 149 |   GeneralFitData FitResidus(GeneralFit& gfit) const;
 | 
|---|
| 150 |   GeneralFitData FitFunction(GeneralFit& gfit) const;
 | 
|---|
| 151 | 
 | 
|---|
| 152 | // Declaration de l interface NTuple
 | 
|---|
| 153 |   virtual sa_size_t     NbLines() const;
 | 
|---|
| 154 |   virtual sa_size_t     NbColumns() const;
 | 
|---|
| 155 |   virtual r_8 *         GetLineD(sa_size_t n) const;
 | 
|---|
| 156 |   virtual r_8           GetCell(sa_size_t n, sa_size_t k) const;
 | 
|---|
| 157 |   virtual r_8           GetCell(sa_size_t n, string const & nom) const;
 | 
|---|
| 158 |   virtual void          GetMinMax(sa_size_t k, double& min, double& max) const; 
 | 
|---|
| 159 |   virtual void          GetMinMax(string const & nom, double& min, double& max) const;
 | 
|---|
| 160 |   virtual sa_size_t     ColumnIndex(string const & nom) const;
 | 
|---|
| 161 |   virtual string        ColumnName(sa_size_t k) const;
 | 
|---|
| 162 |   virtual string        VarList_C(const char* nomx=NULL) const;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   enum FitErrType {DefaultError,ConstantError,SqrtError,ProporError};
 | 
|---|
| 165 |   static double ComputeError(double val,double err,FitErrType errtype=DefaultError
 | 
|---|
| 166 |                             ,double errscale=1.,double errmin=0.,bool nozero=false);
 | 
|---|
| 167 | 
 | 
|---|
| 168 | protected:
 | 
|---|
| 169 |   void Alloc(uint_4 nVar, uint_4 nDataAlloc, uint_2 errx=0);
 | 
|---|
| 170 |   void Delete();
 | 
|---|
| 171 | 
 | 
|---|
| 172 |   int_4   mNVar;        //!< number of variables
 | 
|---|
| 173 |   int_4   mNDataAlloc;  //!< space allocated for datas
 | 
|---|
| 174 |   int_4   mNData;       //!< number of datas
 | 
|---|
| 175 |   int_4   mNDataGood;   //!< number of good datas (not killed)
 | 
|---|
| 176 |   uint_2  mOk_EXP;      //!< true if errors on variables X,Y,..
 | 
|---|
| 177 |   r_8* mXP;             //!< x0 y0 z0, x1 y1 z1, ..., xn yn zn 
 | 
|---|
| 178 |   r_8* mErrXP;          //!< Ex0 Ey0 Ez0, Ex1 Ey1 Ez1, ..., Exn Eyn Ezn 
 | 
|---|
| 179 |   r_8* mF;              //!< F0, F1, ...., Fn
 | 
|---|
| 180 |   r_8* mErr;            //!< EF0, EF1, ...., EFn
 | 
|---|
| 181 |   uint_2* mOK;          //!< 1 si pt valid, 0 sinon
 | 
|---|
| 182 |   r_8* BuffVar;         //!< dummy buffer
 | 
|---|
| 183 |   r_4* BuffVarR4;       //!< dummy buffer
 | 
|---|
| 184 | };
 | 
|---|
| 185 | 
 | 
|---|
| 186 | inline ostream& operator << (ostream& s, GeneralFitData const & g)
 | 
|---|
| 187 |                          {g.Show(s); return(s);}
 | 
|---|
| 188 | 
 | 
|---|
| 189 | /////////////////////////////////////////////////////////////////////////
 | 
|---|
| 190 | // Classe pour la gestion de persistance
 | 
|---|
| 191 | 
 | 
|---|
| 192 | //! For persistance management
 | 
|---|
| 193 | inline POutPersist& operator << (POutPersist& os, GeneralFitData & obj)
 | 
|---|
| 194 | { ObjFileIO<GeneralFitData> fio(&obj);  fio.Write(os);  return(os); }
 | 
|---|
| 195 | //! For persistance management
 | 
|---|
| 196 | inline PInPersist& operator >> (PInPersist& is, GeneralFitData & obj)
 | 
|---|
| 197 | { ObjFileIO<GeneralFitData> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | // Classe pour la gestion de persistance
 | 
|---|
| 200 | // ObjFileIO<GeneralFitData>
 | 
|---|
| 201 | 
 | 
|---|
| 202 | } // Fin du namespace
 | 
|---|
| 203 | 
 | 
|---|
| 204 | #endif
 | 
|---|