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