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