[763] | 1 | #include <stdio.h>
|
---|
| 2 | #include <string.h>
|
---|
| 3 |
|
---|
[2615] | 4 | #include "sopnamsp.h"
|
---|
[763] | 5 | #include "strutil.h"
|
---|
| 6 | #include "perrors.h"
|
---|
| 7 | #include "ntuple.h"
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | #define LENNAME 8
|
---|
| 11 | #define LENNAME1 (LENNAME+1)
|
---|
[2663] | 12 | #define BADVAL -1.e19
|
---|
[763] | 13 |
|
---|
[1371] | 14 | /*!
|
---|
| 15 | \class SOPHYA::NTuple
|
---|
| 16 | \ingroup HiStats
|
---|
[2667] | 17 | Simple NTuple class (a Table or 2-D data set) with double or
|
---|
| 18 | single precision floating point value columns.
|
---|
[1405] | 19 | \sa SOPHYA::ObjFileIO<NTuple>
|
---|
| 20 |
|
---|
| 21 | \code
|
---|
| 22 | #include "ntuple.h"
|
---|
| 23 | // ...
|
---|
| 24 | char * names[3] = {"XPos", "YPos", "Val"};
|
---|
[2667] | 25 | // NTuple (Table) creation with 3 columns (double precision)
|
---|
[1405] | 26 | NTuple nt(3, names);
|
---|
| 27 | // Filling the NTuple
|
---|
[2667] | 28 | r_8 x[3];
|
---|
[1405] | 29 | for(int i=0; i<63; i++) {
|
---|
| 30 | x[0] = (i%9)-4.; x[1] = (i/9)-3.; x[2] = x[0]*x[0]+x[1]*x[1];
|
---|
| 31 | nt.Fill(x);
|
---|
| 32 | }
|
---|
| 33 | // Printing table info
|
---|
| 34 | cout << nt ;
|
---|
| 35 | // Saving object into a PPF file
|
---|
| 36 | POutPersist po("nt.ppf");
|
---|
| 37 | po << nt ;
|
---|
| 38 | \endcode
|
---|
[1371] | 39 | */
|
---|
| 40 |
|
---|
[763] | 41 | //++
|
---|
| 42 | // Class NTuple
|
---|
| 43 | // Lib Outils++
|
---|
| 44 | // include ntuple.h
|
---|
| 45 | //
|
---|
| 46 | // Classe de ntuples
|
---|
| 47 | //--
|
---|
| 48 | //++
|
---|
| 49 | // Links Parents
|
---|
| 50 | // PPersist
|
---|
| 51 | // NTupleInterface
|
---|
| 52 | //--
|
---|
| 53 |
|
---|
| 54 | /* --Methode-- */
|
---|
[2808] | 55 | //! Default constructor - To be used when reading in an NTuple.
|
---|
[763] | 56 | //++
|
---|
| 57 | NTuple::NTuple()
|
---|
| 58 | //
|
---|
| 59 | // Createur par defaut
|
---|
| 60 | //--
|
---|
| 61 | {
|
---|
| 62 | mNVar = mNEnt = mBlk = mNBlk = 0;
|
---|
| 63 | mVar = NULL;
|
---|
| 64 | mVarD = NULL;
|
---|
[2663] | 65 | mFgDouble = true;
|
---|
[763] | 66 | mInfo = NULL;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
[1405] | 70 | //! Constructor with specification of number of columns and column name
|
---|
| 71 | /*!
|
---|
| 72 | \param nvar : Number of columns in the table
|
---|
| 73 | \param noms : Array of column names (length(name) < 8 characters)
|
---|
| 74 | \param blk : Optional argument specifying number of table lines
|
---|
| 75 | in a given data block
|
---|
[2663] | 76 | \param fgdouble : if \b true: internal data kept as double precision values (r_8),
|
---|
| 77 | simple precision (r_4) otherwise
|
---|
[1405] | 78 | */
|
---|
[763] | 79 | //++
|
---|
[2663] | 80 | NTuple::NTuple(int nvar, char** noms, int blk, bool fgdouble)
|
---|
[763] | 81 | //
|
---|
| 82 | // Createur d'un ntuple de `nvar' variables dont les
|
---|
| 83 | // noms sont dans le tableau de cahines de caracteres `noms'
|
---|
| 84 | // avec `blk' d'evenements par blocks.
|
---|
| 85 | //--
|
---|
| 86 | {
|
---|
| 87 | mNVar = mNEnt = mBlk = mNBlk = 0;
|
---|
| 88 | mVar = NULL;
|
---|
| 89 | mVarD = NULL;
|
---|
| 90 | mInfo = NULL;
|
---|
[2507] | 91 | if (nvar <= 0) throw ParmError("NTuple::NTuple(nvar<=0) ");
|
---|
[763] | 92 | mNVar = nvar;
|
---|
| 93 | mVar = new r_4[nvar];
|
---|
| 94 | mVarD = new r_8[nvar];
|
---|
| 95 | if (blk < 10) blk = 10;
|
---|
| 96 | mBlk = blk;
|
---|
[2663] | 97 |
|
---|
| 98 | if (fgdouble) {
|
---|
| 99 | r_8* pt = new r_8[nvar*blk];
|
---|
| 100 | mNBlk = 1;
|
---|
| 101 | mPtrD.push_back(pt);
|
---|
| 102 | mFgDouble = true;
|
---|
| 103 | }
|
---|
| 104 | else {
|
---|
| 105 | r_4* pt = new r_4[nvar*blk];
|
---|
| 106 | mNBlk = 1;
|
---|
| 107 | mPtr.push_back(pt);
|
---|
| 108 | mFgDouble = false;
|
---|
| 109 | }
|
---|
| 110 | for(int i=0; i<nvar; i++) mNames.push_back(noms[i]);
|
---|
[763] | 111 | return;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[1405] | 114 | //! Copy constructor - Copies the table definition and associated data
|
---|
[763] | 115 | // cmv 8/10/99
|
---|
| 116 | //++
|
---|
| 117 | NTuple::NTuple(const NTuple& NT)
|
---|
| 118 | //
|
---|
| 119 | // Createur par copie (clone).
|
---|
| 120 | //--
|
---|
| 121 | : mNVar(0), mNEnt(0), mBlk(0), mNBlk(0)
|
---|
[2663] | 122 | , mVar(NULL), mVarD(NULL), mInfo(NULL)
|
---|
[763] | 123 | {
|
---|
| 124 | if(NT.mNVar<=0) return; // cas ou NT est cree par defaut
|
---|
| 125 | mNVar = NT.mNVar;
|
---|
| 126 | mBlk = NT.mBlk;
|
---|
| 127 | mVar = new r_4[NT.mNVar];
|
---|
| 128 | mVarD = new r_8[NT.mNVar];
|
---|
| 129 |
|
---|
[2663] | 130 | mNames = NT.mNames;
|
---|
| 131 |
|
---|
[763] | 132 | int i;
|
---|
[2663] | 133 | mFgDouble = NT.mFgDouble;
|
---|
| 134 | if (mFgDouble) {
|
---|
| 135 | r_8* pt = new r_8[mNVar*mBlk];
|
---|
| 136 | mNBlk = 1; mPtrD.push_back(pt);
|
---|
| 137 | if(NT.mNEnt > 0)
|
---|
| 138 | for(i=0;i<NT.mNEnt;i++) {pt=NT.GetVecD(i,NULL); Fill(pt);}
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | r_4* pt = new r_4[mNVar*mBlk];
|
---|
| 142 | mNBlk = 1; mPtr.push_back(pt);
|
---|
| 143 | for(i=0;i<NT.mNEnt;i++) {pt=NT.GetVec(i,NULL); Fill(pt);}
|
---|
| 144 | }
|
---|
[763] | 145 |
|
---|
[2663] | 146 | mNames = NT.mNames;
|
---|
[763] | 147 |
|
---|
| 148 | if(NT.mInfo!=NULL) {mInfo = new DVList; *mInfo = *(NT.mInfo);}
|
---|
| 149 |
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[2663] | 153 | /* --Methode--
|
---|
[1405] | 154 | //! Constructor with table initialized from a PPF file
|
---|
[763] | 155 | //++
|
---|
| 156 | NTuple::NTuple(char *flnm)
|
---|
| 157 | //
|
---|
| 158 | // Createur lecture fichier ppersist.
|
---|
| 159 | //--
|
---|
| 160 | {
|
---|
| 161 | mNVar = mNEnt = mBlk = mNBlk = 0;
|
---|
| 162 | mVar = NULL;
|
---|
| 163 | mVarD = NULL;
|
---|
| 164 | mNames = NULL;
|
---|
| 165 | mInfo = NULL;
|
---|
| 166 | PInPersist s(flnm);
|
---|
| 167 | ObjFileIO<NTuple> fiont(this);
|
---|
| 168 | fiont.Read(s);
|
---|
| 169 | }
|
---|
[2663] | 170 | */
|
---|
[763] | 171 |
|
---|
| 172 | /* --Methode-- */
|
---|
| 173 | NTuple::~NTuple()
|
---|
| 174 | {
|
---|
| 175 | Clean();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /* --Methode-- */
|
---|
[1405] | 179 | //! Clear the data table definition and deletes the associated data
|
---|
[763] | 180 | void NTuple::Clean()
|
---|
| 181 | {
|
---|
| 182 | if (mVar) delete[] mVar;
|
---|
| 183 | if (mVarD) delete[] mVarD;
|
---|
| 184 | if (mInfo) delete mInfo;
|
---|
| 185 | int i;
|
---|
[2663] | 186 | if(mNBlk>0) {
|
---|
| 187 | if (mFgDouble) for(i=0; i<mNBlk; i++) delete[] mPtrD[i];
|
---|
| 188 | else for(i=0; i<mNBlk; i++) delete[] mPtr[i];
|
---|
| 189 | }
|
---|
| 190 | if (mFgDouble) mPtrD.erase(mPtrD.begin(), mPtrD.end());
|
---|
| 191 | else mPtr.erase(mPtr.begin(), mPtr.end());
|
---|
[763] | 192 | mNVar = mNEnt = mBlk = mNBlk = 0;
|
---|
| 193 | mVar = NULL;
|
---|
| 194 | mVarD = NULL;
|
---|
| 195 | mInfo = NULL;
|
---|
| 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* --Methode-- cmv 08/10/99 */
|
---|
[1405] | 200 | //! = operator, copies the data table definition and its contents
|
---|
[763] | 201 | //++
|
---|
| 202 | NTuple& NTuple::operator = (const NTuple& NT)
|
---|
| 203 | //
|
---|
| 204 | // Operateur egal (clone).
|
---|
| 205 | //--
|
---|
| 206 | {
|
---|
| 207 | if(this == &NT) return *this;
|
---|
| 208 | Clean();
|
---|
| 209 | if(NT.mNVar<=0) return *this; // cas ou NT est cree par defaut
|
---|
| 210 | mNVar = NT.mNVar;
|
---|
| 211 | mBlk = NT.mBlk;
|
---|
| 212 | mVar = new r_4[NT.mNVar];
|
---|
| 213 | mVarD = new r_8[NT.mNVar];
|
---|
| 214 |
|
---|
[2663] | 215 | mNames = NT.mNames;
|
---|
| 216 |
|
---|
[763] | 217 | int i;
|
---|
[2663] | 218 | mFgDouble = NT.mFgDouble;
|
---|
| 219 | if (mFgDouble) {
|
---|
| 220 | r_8* pt = new r_8[mNVar*mBlk];
|
---|
| 221 | mNBlk = 1; mPtrD.push_back(pt);
|
---|
| 222 | if(NT.mNEnt > 0)
|
---|
| 223 | for(i=0;i<NT.mNEnt;i++) {pt=NT.GetVecD(i,NULL); Fill(pt);}
|
---|
| 224 | }
|
---|
| 225 | else {
|
---|
| 226 | r_4* pt = new r_4[mNVar*mBlk];
|
---|
| 227 | mNBlk = 1; mPtr.push_back(pt);
|
---|
| 228 | for(i=0;i<NT.mNEnt;i++) {pt=NT.GetVec(i,NULL); Fill(pt);}
|
---|
| 229 | }
|
---|
[763] | 230 |
|
---|
| 231 | if(NT.mInfo!=NULL) {mInfo = new DVList; *mInfo = *(NT.mInfo);}
|
---|
| 232 |
|
---|
| 233 | return *this;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /* --Methode-- */
|
---|
[1405] | 237 | //! Adds an entry (a line) to the table
|
---|
| 238 | /*!
|
---|
| 239 | \param x : content of the line to be appended to the table
|
---|
| 240 | */
|
---|
[763] | 241 | //++
|
---|
| 242 | void NTuple::Fill(r_4* x)
|
---|
| 243 | //
|
---|
| 244 | // Remplit le ntuple avec le tableau cd reels `x'.
|
---|
| 245 | //--
|
---|
| 246 | {
|
---|
| 247 | int numb = mNEnt/mBlk;
|
---|
| 248 | if (numb >= mNBlk) {
|
---|
[2663] | 249 | if (mFgDouble) {
|
---|
| 250 | r_8* pt = new r_8[mNVar*mBlk];
|
---|
| 251 | mNBlk++;
|
---|
| 252 | mPtrD.push_back(pt);
|
---|
| 253 | }
|
---|
| 254 | else {
|
---|
| 255 | r_4* pt = new r_4[mNVar*mBlk];
|
---|
| 256 | mNBlk++;
|
---|
| 257 | mPtr.push_back(pt);
|
---|
| 258 | }
|
---|
[763] | 259 | }
|
---|
[2663] | 260 |
|
---|
[763] | 261 | int offb = mNEnt-numb*mBlk;
|
---|
[2663] | 262 | if (mFgDouble)
|
---|
| 263 | for(int i=0; i<mNVar; i++) (mPtrD[numb]+offb*mNVar)[i] = x[i];
|
---|
| 264 | else memcpy((mPtr[numb]+offb*mNVar), x, mNVar*sizeof(r_4));
|
---|
[763] | 265 | mNEnt++;
|
---|
| 266 | return;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[2663] | 269 | /* --Methode-- */
|
---|
| 270 | //! Adds an entry (a line) to the table (double precision)
|
---|
| 271 | /*!
|
---|
| 272 | \param x : content of the line to be appended to the table
|
---|
| 273 | */
|
---|
| 274 | //++
|
---|
| 275 | void NTuple::Fill(r_8* x)
|
---|
| 276 | //
|
---|
| 277 | // Remplit le ntuple avec le tableau double precision `x'.
|
---|
| 278 | //--
|
---|
| 279 | {
|
---|
| 280 | int numb = mNEnt/mBlk;
|
---|
| 281 | if (numb >= mNBlk) {
|
---|
| 282 | if (mFgDouble) {
|
---|
| 283 | r_8* pt = new r_8[mNVar*mBlk];
|
---|
| 284 | mNBlk++;
|
---|
| 285 | mPtrD.push_back(pt);
|
---|
| 286 | }
|
---|
| 287 | else {
|
---|
| 288 | r_4* pt = new r_4[mNVar*mBlk];
|
---|
| 289 | mNBlk++;
|
---|
| 290 | mPtr.push_back(pt);
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
[763] | 293 |
|
---|
[2663] | 294 | int offb = mNEnt-numb*mBlk;
|
---|
| 295 | if (mFgDouble)
|
---|
| 296 | memcpy((mPtrD[numb]+offb*mNVar), x, mNVar*sizeof(r_8));
|
---|
| 297 | else
|
---|
| 298 | for(int i=0; i<mNVar; i++) (mPtr[numb]+offb*mNVar)[i] = x[i];
|
---|
| 299 |
|
---|
| 300 | mNEnt++;
|
---|
| 301 | return;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 |
|
---|
[763] | 305 | /* --Methode-- */
|
---|
| 306 | //++
|
---|
| 307 | float NTuple::GetVal(int n, int k) const
|
---|
| 308 | //
|
---|
| 309 | // Retourne la valeur de la variable `k' de l'evenement `n'.
|
---|
| 310 | //--
|
---|
| 311 | {
|
---|
| 312 | if (n >= mNEnt) return(BADVAL);
|
---|
| 313 | if ( (k < 0) || (k >= mNVar) ) return(BADVAL);
|
---|
| 314 | int numb = n/mBlk;
|
---|
| 315 | int offb = n-numb*mBlk;
|
---|
[2663] | 316 | if ( mFgDouble) return(*(mPtrD[numb]+offb*mNVar+k));
|
---|
| 317 | else return(*(mPtr[numb]+offb*mNVar+k));
|
---|
[763] | 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | /* --Methode-- */
|
---|
| 322 | //++
|
---|
| 323 | int NTuple::IndexNom(const char* nom) const
|
---|
| 324 | //
|
---|
| 325 | // Retourne le numero de la variable de nom `nom'.
|
---|
| 326 | //--
|
---|
| 327 | {
|
---|
| 328 | int i;
|
---|
[2663] | 329 | string snom = nom;
|
---|
[763] | 330 | for(i=0; i<mNVar; i++)
|
---|
[2663] | 331 | if ( mNames[i] == snom) return(i);
|
---|
[763] | 332 | return(-1);
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 |
|
---|
[2663] | 336 | static char nomretour[256];
|
---|
[763] | 337 | /* --Methode-- */
|
---|
| 338 | //++
|
---|
| 339 | char* NTuple::NomIndex(int k) const
|
---|
| 340 | //
|
---|
| 341 | // Retourne le nom de la variable numero 'k'
|
---|
| 342 | //--
|
---|
| 343 | {
|
---|
| 344 | nomretour[0] = '\0';
|
---|
[2663] | 345 | if ((k >= 0) && (k < mNVar)) strncpy(nomretour, mNames[k].c_str(), 255);
|
---|
[763] | 346 | return(nomretour);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | /* --Methode-- */
|
---|
| 351 | //++
|
---|
| 352 | r_4* NTuple::GetVec(int n, r_4* ret) const
|
---|
| 353 | //
|
---|
| 354 | // Retourne l'evenement `n' dans le vecteur `ret'.
|
---|
| 355 | //--
|
---|
| 356 | {
|
---|
| 357 | int i;
|
---|
| 358 | if (ret == NULL) ret = mVar;
|
---|
| 359 | if (n >= mNEnt) {
|
---|
| 360 | for(i=0; i<mNVar; i++) ret[i] = BADVAL;
|
---|
| 361 | return(ret);
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | int numb = n/mBlk;
|
---|
| 365 | int offb = n-numb*mBlk;
|
---|
[2663] | 366 | if (mFgDouble) for(i=0; i<mNVar; i++) ret[i] = (mPtrD[numb]+offb*mNVar)[i];
|
---|
| 367 | else memcpy(ret, (mPtr[numb]+offb*mNVar), mNVar*sizeof(r_4));
|
---|
[763] | 368 | return(ret);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /* --Methode-- */
|
---|
| 372 | //++
|
---|
| 373 | r_8* NTuple::GetVecD(int n, r_8* ret) const
|
---|
| 374 | //
|
---|
| 375 | // Retourne l'evenement `n' dans le vecteur `ret'.
|
---|
| 376 | //--
|
---|
| 377 | {
|
---|
| 378 | int i;
|
---|
| 379 | if (ret == NULL) ret = mVarD;
|
---|
[2663] | 380 | if (n >= mNEnt) {
|
---|
| 381 | for(i=0; i<mNVar; i++) ret[i] = BADVAL;
|
---|
| 382 | return(ret);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | int numb = n/mBlk;
|
---|
| 386 | int offb = n-numb*mBlk;
|
---|
[2667] | 387 | if (mFgDouble) memcpy(ret, (mPtrD[numb]+offb*mNVar), mNVar*sizeof(r_8));
|
---|
| 388 | else for(i=0; i<mNVar; i++) ret[i] = (mPtr[numb]+offb*mNVar)[i];
|
---|
[763] | 389 | return(ret);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 |
|
---|
| 394 | /* --Methode-- */
|
---|
| 395 | //++
|
---|
| 396 | DVList& NTuple::Info()
|
---|
| 397 | //
|
---|
| 398 | // Renvoie une référence sur l'objet DVList Associé
|
---|
| 399 | //--
|
---|
| 400 | {
|
---|
| 401 | if (mInfo == NULL) mInfo = new DVList;
|
---|
| 402 | return(*mInfo);
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /* --Methode-- */
|
---|
| 406 | //++
|
---|
| 407 | void NTuple::Print(int num, int nmax) const
|
---|
| 408 | //
|
---|
| 409 | // Imprime `nmax' evenements a partir du numero `num'.
|
---|
| 410 | //--
|
---|
| 411 | {
|
---|
| 412 | int i,j;
|
---|
| 413 |
|
---|
| 414 | printf("Num ");
|
---|
[2663] | 415 | for(i=0; i<mNVar; i++) printf("%8s ", mNames[i].c_str());
|
---|
[763] | 416 | putchar('\n');
|
---|
| 417 |
|
---|
| 418 | if (nmax <= 0) nmax = 1;
|
---|
| 419 | if (num < 0) num = 0;
|
---|
| 420 | nmax += num;
|
---|
| 421 | if (nmax > mNEnt) nmax = mNEnt;
|
---|
| 422 | for(i=num; i<nmax; i++) {
|
---|
| 423 | GetVec(i, NULL);
|
---|
| 424 | printf("%6d ", i);
|
---|
| 425 | for(j=0; j<mNVar; j++) printf("%8g ", (float)mVar[j]);
|
---|
| 426 | putchar('\n');
|
---|
| 427 | }
|
---|
| 428 | return;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | /* --Methode-- */
|
---|
[1405] | 432 | //! Prints table definition and number of entries
|
---|
[763] | 433 | //++
|
---|
| 434 | void NTuple::Show(ostream& os) const
|
---|
| 435 | //
|
---|
| 436 | // Imprime l'information generale sur le ntuple.
|
---|
| 437 | //--
|
---|
| 438 | {
|
---|
[2663] | 439 | char * tt = "float";
|
---|
| 440 | if (mFgDouble) tt = "double";
|
---|
| 441 | os << "NTuple T=" << tt << " : NVar= " << mNVar << " NEnt=" << mNEnt
|
---|
[763] | 442 | << " (Blk Sz,Nb= " << mBlk << " ," << mNBlk << ")\n";
|
---|
| 443 | os << " Variables Min Max \n";
|
---|
| 444 | int i;
|
---|
| 445 | double min, max;
|
---|
| 446 | char buff[128];
|
---|
| 447 | for(i=0; i<mNVar; i++) {
|
---|
| 448 | GetMinMax(i, min, max);
|
---|
[2663] | 449 | sprintf(buff, "%3d %16s %10g %10g \n", i, mNames[i].c_str(), min, max);
|
---|
[763] | 450 | os << (string)buff ;
|
---|
| 451 | }
|
---|
| 452 | os << endl;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 | /* --Methode-- */
|
---|
[1405] | 457 | //! Fills the table, reading lines from an ascii file
|
---|
| 458 | /*!
|
---|
| 459 | \param fn : file name
|
---|
| 460 | \param defval : default value for empty cells in the ascii file
|
---|
| 461 | */
|
---|
[763] | 462 | //++
|
---|
| 463 | int NTuple::FillFromASCIIFile(string const& fn, float defval)
|
---|
| 464 | //
|
---|
| 465 | // Remplit le ntuple a partir d'un fichier ASCII.
|
---|
| 466 | // Renvoie le nombre de lignes ajoutees.
|
---|
| 467 | //--
|
---|
| 468 | {
|
---|
| 469 | if (NbColumns() < 1) {
|
---|
| 470 | cout << "NTuple::FillFromASCIIFile() Ntuple has " << NbColumns() << " columns" << endl;
|
---|
| 471 | return(-1);
|
---|
| 472 | }
|
---|
| 473 | FILE * fip = fopen(fn.c_str(), "r");
|
---|
| 474 | if (fip == NULL) {
|
---|
| 475 | cout << "NTuple::FillFromASCIIFile() Error opening file " << fn << endl;
|
---|
| 476 | return(-2);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | char lineb[4096];
|
---|
| 480 | char *line;
|
---|
| 481 | char* ccp;
|
---|
[809] | 482 | int j,kk;
|
---|
[763] | 483 | int postab, posb;
|
---|
[2663] | 484 | double* xv = new double[NbColumns()];
|
---|
[763] | 485 |
|
---|
| 486 | int nlread = 0;
|
---|
| 487 | int nvar = NbColumns();
|
---|
| 488 | // On boucle sur toutes les lignes
|
---|
| 489 | while (fgets(lineb,4096,fip) != NULL) {
|
---|
| 490 | lineb[4095] = '\0';
|
---|
| 491 | j = 0; line = lineb;
|
---|
| 492 | // On enleve les espaces et tab de debut
|
---|
| 493 | while ( (line[j] != '\0') && ((line[j] == ' ') || (line[j] == '\t')) ) j++;
|
---|
| 494 | line = lineb+j;
|
---|
| 495 | // Il faut que le premier caractere non-espace soit un digit, ou + ou - ou .
|
---|
| 496 | if (!( isdigit(line[0]) || (line[0] == '+') || (line[0] == '-') || (line[0] == '.') )) continue;
|
---|
| 497 | ccp = line;
|
---|
| 498 | for(kk=0; kk<nvar; kk++) xv[kk] = defval;
|
---|
| 499 | for(kk=0; kk<nvar; kk++) {
|
---|
| 500 | // Les mots sont separes par des espaces ou des tab
|
---|
| 501 | postab = posc(ccp, '\t' );
|
---|
| 502 | posb = posc(ccp, ' ' );
|
---|
| 503 | if (postab >= 0) {
|
---|
| 504 | if (posb < 0) posb = postab;
|
---|
| 505 | else if (postab < posb) posb = postab;
|
---|
| 506 | }
|
---|
| 507 | if (posb >= 0) ccp[posb] = '\0';
|
---|
| 508 | if ( isdigit(line[0]) || (line[0] == '+') || (line[0] == '-') || (line[0] == '.') )
|
---|
| 509 | xv[kk] = atof(ccp);
|
---|
| 510 | if (posb < 0) break;
|
---|
| 511 | ccp += posb+1; j = 0;
|
---|
| 512 | while ( (ccp[j] != '\0') && ((ccp[j] == ' ') || (ccp[j] == '\t')) ) j++;
|
---|
| 513 | ccp += j;
|
---|
| 514 | }
|
---|
| 515 | Fill(xv);
|
---|
| 516 | nlread++;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | delete[] xv;
|
---|
| 520 | cout << "NTuple::FillFromASCIIFile( " << fn << ") " << nlread << " fill from file " << endl;
|
---|
| 521 | return(nlread);
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 |
|
---|
| 525 | // ------- Implementation de l interface NTuple ---------
|
---|
| 526 |
|
---|
| 527 | /* --Methode-- */
|
---|
[2682] | 528 | sa_size_t NTuple::NbLines() const
|
---|
[763] | 529 | {
|
---|
| 530 | return(NEntry());
|
---|
| 531 | }
|
---|
| 532 | /* --Methode-- */
|
---|
[2682] | 533 | sa_size_t NTuple::NbColumns() const
|
---|
[763] | 534 | {
|
---|
| 535 | return(NVar());
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | /* --Methode-- */
|
---|
[2682] | 539 | r_8 * NTuple::GetLineD(sa_size_t n) const
|
---|
[763] | 540 | {
|
---|
| 541 | return(GetVecD(n));
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | /* --Methode-- */
|
---|
[2682] | 545 | r_8 NTuple::GetCell(sa_size_t n, sa_size_t k) const
|
---|
[763] | 546 | {
|
---|
| 547 | return(GetVal(n, k));
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | /* --Methode-- */
|
---|
[2682] | 551 | r_8 NTuple::GetCell(sa_size_t n, string const & nom) const
|
---|
[763] | 552 | {
|
---|
| 553 | return(GetVal(n, nom.c_str()));
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /* --Methode-- */
|
---|
| 557 | //++
|
---|
[2682] | 558 | void NTuple::GetMinMax(sa_size_t k, double& min, double& max) const
|
---|
[763] | 559 | //
|
---|
| 560 | // Retourne le minimum et le maximum de la variable `k'.
|
---|
| 561 | //--
|
---|
| 562 | {
|
---|
| 563 | min = 9.e19; max = -9.e19;
|
---|
| 564 | if ( (k < 0) || (k >= mNVar) ) return;
|
---|
| 565 | int jb,ib,i;
|
---|
| 566 | double x;
|
---|
| 567 | i=0;
|
---|
| 568 | for(jb=0; jb< mNBlk; jb++)
|
---|
| 569 | for(ib=0; ib< mBlk; ib++) {
|
---|
| 570 | if (i >= mNEnt) break;
|
---|
| 571 | i++;
|
---|
[2663] | 572 | x = (mFgDouble) ? *(mPtrD[jb]+ib*mNVar+k) : *(mPtr[jb]+ib*mNVar+k);
|
---|
[763] | 573 | if(i==1) {min = x; max = x;}
|
---|
| 574 | if (x < min) min = x;
|
---|
| 575 | if (x > max) max = x;
|
---|
| 576 | }
|
---|
| 577 | return;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | /* --Methode-- */
|
---|
| 581 | void NTuple::GetMinMax(string const & nom, double& min, double& max) const
|
---|
| 582 | {
|
---|
| 583 | GetMinMax(IndexNom(nom.c_str()), min, max);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | /* --Methode-- */
|
---|
[2682] | 587 | sa_size_t NTuple::ColumnIndex(string const & nom) const
|
---|
[763] | 588 | {
|
---|
| 589 | return(IndexNom(nom.c_str()));
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | /* --Methode-- */
|
---|
[2682] | 593 | string NTuple::ColumnName(sa_size_t k) const
|
---|
[763] | 594 | {
|
---|
| 595 | return(NomIndex(k));
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | /* --Methode-- */
|
---|
| 599 | //++
|
---|
| 600 | string NTuple::VarList_C(const char* nomx) const
|
---|
| 601 | //
|
---|
| 602 | // Retourne une chaine de caracteres avec la declaration des noms de
|
---|
| 603 | // variables. si "nomx!=NULL" , des instructions d'affectation
|
---|
| 604 | // a partir d'un tableau "nomx[i]" sont ajoutees.
|
---|
| 605 | //--
|
---|
| 606 | {
|
---|
| 607 | string rets="";
|
---|
| 608 | int i;
|
---|
| 609 | for(i=0; i<mNVar; i++) {
|
---|
| 610 | if ( (i%5 == 0) && (i > 0) ) rets += ";";
|
---|
| 611 | if (i%5 == 0) rets += "\ndouble ";
|
---|
| 612 | else rets += ",";
|
---|
[2663] | 613 | rets += mNames[i];
|
---|
[763] | 614 | }
|
---|
| 615 | rets += "; \n";
|
---|
| 616 | if (nomx) {
|
---|
| 617 | char buff[256];
|
---|
| 618 | for(i=0; i<mNVar; i++) {
|
---|
[2663] | 619 | sprintf(buff,"%s=%s[%d]; ", mNames[i].c_str(), nomx, i);
|
---|
[763] | 620 | rets += buff;
|
---|
| 621 | if ( (i%3 == 0) && (i > 0) ) rets += "\n";
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | return(rets);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 |
|
---|
| 629 | /* --Methode-- */
|
---|
| 630 | //++
|
---|
| 631 | string NTuple::LineHeaderToString() const
|
---|
| 632 | // Retourne une chaine de caracteres avec la liste des noms de
|
---|
| 633 | // variables, utilisables pour une impression
|
---|
| 634 | //--
|
---|
| 635 | {
|
---|
| 636 | char buff[32];
|
---|
| 637 | string rets=" Num ";
|
---|
| 638 | for(int i=0; i<mNVar; i++) {
|
---|
[2663] | 639 | sprintf(buff, "%8s ", mNames[i].c_str());
|
---|
[763] | 640 | rets += buff;
|
---|
| 641 | }
|
---|
| 642 | rets += '\n';
|
---|
| 643 | return(rets);
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | /* --Methode-- */
|
---|
| 647 | //++
|
---|
[2682] | 648 | string NTuple::LineToString(sa_size_t n) const
|
---|
[763] | 649 | // Retourne une chaine de caracteres avec le contenu de la ligne "n"
|
---|
| 650 | // utilisable pour une impression
|
---|
| 651 | //--
|
---|
| 652 | {
|
---|
| 653 | char buff[32];
|
---|
| 654 | double* val;
|
---|
| 655 | val = GetLineD(n);
|
---|
| 656 | sprintf(buff,"%6d: ",n);
|
---|
| 657 | string rets=buff;
|
---|
| 658 | int i;
|
---|
| 659 | for(i=0; i<mNVar; i++) {
|
---|
| 660 | sprintf(buff, "%8.3g ", val[i]);
|
---|
| 661 | rets += buff;
|
---|
| 662 | }
|
---|
| 663 | rets += '\n';
|
---|
| 664 | return(rets);
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[1405] | 667 | /*!
|
---|
| 668 | \class SOPHYA::ObjFileIO<NTuple>
|
---|
| 669 | \ingroup HiStats
|
---|
| 670 | Persistence (serialisation) handler for class NTuple
|
---|
| 671 | */
|
---|
[763] | 672 |
|
---|
[846] | 673 | /* --Methode-- */
|
---|
[763] | 674 | //++
|
---|
[2341] | 675 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[763] | 676 | void ObjFileIO<NTuple>::WriteSelf(POutPersist& s) const
|
---|
| 677 | //
|
---|
| 678 | // Ecriture ppersist du ntuple.
|
---|
| 679 | //--
|
---|
| 680 | {
|
---|
[1155] | 681 | if (dobj == NULL) return;
|
---|
| 682 |
|
---|
[2663] | 683 | // On ecrit cette chaine pour compatibilite avec les anciennes version (1,2)
|
---|
[1155] | 684 | string strg = "NTuple";
|
---|
| 685 | s.Put(strg);
|
---|
| 686 |
|
---|
| 687 | // On ecrit 3 uint_4 ....
|
---|
[2663] | 688 | // [0]: Numero de version ;
|
---|
| 689 | // [1] : bit1 non nul -> has info, bit 2 non nul mFgDouble=true
|
---|
| 690 | // [2] : reserve
|
---|
[1155] | 691 | uint_4 itab[3];
|
---|
[2663] | 692 | itab[0] = 3; // Numero de version a 3
|
---|
[1155] | 693 | itab[1] = itab[2] = 0;
|
---|
| 694 | if (dobj->mInfo) itab[1] = 1;
|
---|
[2663] | 695 | if (dobj->mFgDouble) itab[1] += 2;
|
---|
[1155] | 696 | s.Put(itab, 3);
|
---|
| 697 |
|
---|
| 698 | s.Put(dobj->mNVar);
|
---|
[2663] | 699 | for(int k=0; k<dobj->mNVar; k++) s.Put(dobj->mNames[k]);
|
---|
[1155] | 700 | s.Put(dobj->mNEnt);
|
---|
| 701 | s.Put(dobj->mBlk);
|
---|
| 702 | s.Put(dobj->mNBlk);
|
---|
[2663] | 703 |
|
---|
[763] | 704 | if (dobj->mInfo) s << (*(dobj->mInfo));
|
---|
| 705 | int jb;
|
---|
[2663] | 706 | if (dobj->mFgDouble)
|
---|
| 707 | for(jb=0; jb<dobj->mNBlk; jb++)
|
---|
| 708 | s.Put(dobj->mPtrD[jb], dobj->mNVar*dobj->mBlk);
|
---|
| 709 | else
|
---|
| 710 | for(jb=0; jb<dobj->mNBlk; jb++)
|
---|
| 711 | s.Put(dobj->mPtr[jb], dobj->mNVar*dobj->mBlk);
|
---|
[763] | 712 | return;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | /* --Methode-- */
|
---|
| 716 | //++
|
---|
[2341] | 717 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
[763] | 718 | void ObjFileIO<NTuple>::ReadSelf(PInPersist& s)
|
---|
| 719 | //
|
---|
| 720 | // Lecture ppersist du ntuple.
|
---|
| 721 | //--
|
---|
| 722 | {
|
---|
| 723 |
|
---|
[1155] | 724 | if (dobj == NULL) dobj = new NTuple;
|
---|
| 725 | else dobj->Clean();
|
---|
[763] | 726 |
|
---|
| 727 | bool hadinfo = false;
|
---|
[1155] | 728 | string strg;
|
---|
| 729 | s.Get(strg);
|
---|
[2663] | 730 | uint_4 itab[3] = {0,0,0};
|
---|
[1155] | 731 | if (strg == "NTuple") {
|
---|
| 732 | s.Get(itab, 3);
|
---|
[2663] | 733 | if ( ((itab[0] < 3) && (itab[1] != 0)) ||
|
---|
| 734 | ((itab[0] >= 3) && ((itab[1]&1) == 1)) ) hadinfo = true;
|
---|
[1155] | 735 | }
|
---|
| 736 | else {
|
---|
| 737 | // Ancienne version de PPF NTuple - Pour savoir s'il y avait un DVList Info associe
|
---|
| 738 | char buff[256];
|
---|
| 739 | strcpy(buff, strg.c_str());
|
---|
| 740 | if (strncmp(buff+strlen(buff)-7, "HasInfo", 7) == 0) hadinfo = true;
|
---|
| 741 | }
|
---|
[2663] | 742 | if (itab[0] < 3) { // Lecture version anterieures V= 1 , 2
|
---|
| 743 | s.Get(dobj->mNVar);
|
---|
| 744 | dobj->mVar = new r_4[dobj->mNVar];
|
---|
| 745 | dobj->mVarD = new r_8[dobj->mNVar];
|
---|
| 746 |
|
---|
| 747 | char * names = new char[dobj->mNVar*LENNAME1];
|
---|
| 748 | s.GetBytes(names, dobj->mNVar*LENNAME1);
|
---|
| 749 | for(int k=0; k<dobj->mNVar; k++) dobj->mNames.push_back(names+k*LENNAME1);
|
---|
| 750 | s.Get(dobj->mNEnt);
|
---|
| 751 | s.Get(dobj->mBlk);
|
---|
| 752 | s.Get(dobj->mNBlk);
|
---|
| 753 | if (hadinfo) { // Lecture eventuelle du DVList Info
|
---|
| 754 | if (dobj->mInfo == NULL) dobj->mInfo = new DVList;
|
---|
| 755 | s >> (*(dobj->mInfo));
|
---|
[763] | 756 | }
|
---|
[2663] | 757 | // Il n'y avait que des NTuple avec data float pour V < 3
|
---|
| 758 | dobj->mFgDouble = false;
|
---|
| 759 | int jb;
|
---|
| 760 | for(jb=0; jb<dobj->mNBlk; jb++) {
|
---|
| 761 | r_4* pt = new r_4[dobj->mNVar*dobj->mBlk];
|
---|
| 762 | dobj->mPtr.push_back(pt);
|
---|
| 763 | s.Get(dobj->mPtr[jb], dobj->mNVar*dobj->mBlk);
|
---|
| 764 | }
|
---|
[763] | 765 | }
|
---|
[2663] | 766 | else { // Lecture version V 3
|
---|
| 767 | s.Get(dobj->mNVar);
|
---|
| 768 | dobj->mVar = new r_4[dobj->mNVar];
|
---|
| 769 | dobj->mVarD = new r_8[dobj->mNVar];
|
---|
| 770 | string nom;
|
---|
| 771 | for(int k=0; k<dobj->mNVar; k++) {
|
---|
| 772 | s.Get(nom);
|
---|
| 773 | dobj->mNames.push_back(nom);
|
---|
| 774 | }
|
---|
| 775 | s.Get(dobj->mNEnt);
|
---|
| 776 | s.Get(dobj->mBlk);
|
---|
| 777 | s.Get(dobj->mNBlk);
|
---|
| 778 | if (hadinfo) { // Lecture eventuelle du DVList Info
|
---|
| 779 | if (dobj->mInfo == NULL) dobj->mInfo = new DVList;
|
---|
| 780 | s >> (*(dobj->mInfo));
|
---|
| 781 | }
|
---|
| 782 | // Il n'y avait que des NTuple avec data float pour V < 3
|
---|
| 783 | dobj->mFgDouble = ((itab[1]&2) == 2) ? true : false;
|
---|
| 784 | int jb;
|
---|
| 785 | if (dobj->mFgDouble) {
|
---|
| 786 | for(jb=0; jb<dobj->mNBlk; jb++) {
|
---|
| 787 | r_8* pt = new r_8[dobj->mNVar*dobj->mBlk];
|
---|
| 788 | dobj->mPtrD.push_back(pt);
|
---|
| 789 | s.Get(dobj->mPtrD[jb], dobj->mNVar*dobj->mBlk);
|
---|
| 790 | }
|
---|
| 791 | }
|
---|
| 792 | else {
|
---|
| 793 | for(jb=0; jb<dobj->mNBlk; jb++) {
|
---|
| 794 | r_4* pt = new r_4[dobj->mNVar*dobj->mBlk];
|
---|
| 795 | dobj->mPtr.push_back(pt);
|
---|
| 796 | s.Get(dobj->mPtr[jb], dobj->mNVar*dobj->mBlk);
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 | } // Fin lecture V3
|
---|
[763] | 800 |
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[846] | 803 |
|
---|
[763] | 804 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 805 | #pragma define_template ObjFileIO<NTuple>
|
---|
| 806 | #endif
|
---|
| 807 |
|
---|
| 808 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 809 | template class ObjFileIO<NTuple>;
|
---|
| 810 | #endif
|
---|