~maddevelopers/mg5amcnlo/2.9.4

« back to all changes in this revision

Viewing changes to vendor/StdHEP/mcfio/src/mcfio_Block.c

pass to v2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
*                                                                              *
 
3
* mcfio_Block.c --  Utility routines for the McFast Monte-Carlo                  *
 
4
*               The routine to encode/decode a block                           *
 
5
*                                                                              *
 
6
* Copyright (c) 1994 Universities Research Association, Inc.                   *
 
7
* All rights reserved.                                                         *
 
8
*                                                                              *
 
9
* This material resulted from work developed under a Government Contract and   *
 
10
* is subject to the following license:  The Government retains a paid-up,      *
 
11
* nonexclusive, irrevocable worldwide license to reproduce, prepare derivative *
 
12
* works, perform publicly and display publicly by or for the Government,       *
 
13
* including the right to distribute to other Government contractors.  Neither  *
 
14
* the United States nor the United States Department of Energy, nor any of     *
 
15
* their employees, makes any warranty, express or implied, or assumes any      *
 
16
* legal liability or responsibility for the accuracy, completeness, or         *
 
17
* usefulness of any information, apparatus, product, or process disclosed, or  *
 
18
* represents that its use would not infringe privately owned rights.           *
 
19
*                                                                              *
 
20
*                                                                              *
 
21
* Written by Paul Lebrun                                                       *
 
22
*                                                                              *
 
23
*                                                                              *
 
24
*******************************************************************************/
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <sys/param.h>
 
28
#include <rpc/types.h>
 
29
#include <sys/types.h>
 
30
#include <rpc/xdr.h>
 
31
#include <limits.h>
 
32
#include <stdlib.h>
 
33
#include <time.h>
 
34
#ifdef SUNOS
 
35
#include <floatingpoint.h>
 
36
#else /* SUNOS */
 
37
#include <float.h>
 
38
#endif /* SUNOS */
 
39
#include "mcf_nTupleDescript.h"
 
40
#include "mcf_xdr.h"
 
41
#include "mcf_xdr_Ntuple.h"
 
42
#include "mcfio_Dict.h"
 
43
#include "mcfio_Util1.h"
 
44
#include "mcf_NTuIOUtils.h"
 
45
#include "mcfio_Direct.h"
 
46
#include "mcfio_Block.h"
 
47
#ifndef FALSE
 
48
#define FALSE 0
 
49
#endif
 
50
#ifndef TRUE
 
51
#define TRUE 1
 
52
#endif
 
53
 
 
54
int mcfioC_Block(int stream, int blkid, 
 
55
 bool_t xdr_filtercode(XDR *xdrs, int *blockid, int *ntot, char **version))
 
56
/*
 
57
** Routine to decode or encode a particular Block. Return 1 if O.K, 
 
58
** -1 if a problem or unknow block.  
 
59
**
 
60
** Adding Ntuple instances ... October 1995.
 
61
*/
 
62
 
63
  int i, j, jstr, idtmp, ntot, nbuff;
 
64
  bool_t ok;
 
65
  u_int p1;
 
66
  mcfStream *str;
 
67
   
 
68
  if (McfStreamPtrList == NULL) { 
 
69
     fprintf(stderr,
 
70
  " mcfio_Block: You must first initialize by calling mcfio_Init.\n"); 
 
71
     return -1;
 
72
  }
 
73
  jstr = stream-1;
 
74
  if (McfStreamPtrList[jstr] == NULL) { 
 
75
     fprintf(stderr,
 
76
 " mcfio_Block: First, declare the stream by calling mcfio_Open...\n"); 
 
77
     return -1;
 
78
  }
 
79
  str = McfStreamPtrList[jstr];
 
80
  if ((str->row == MCFIO_WRITE) && 
 
81
      (str->fhead->nBlocks == str->ehead->nBlocks)) {
 
82
     fprintf(stderr,
 
83
 " mcfio_Block: Maximum number of Blocks reached for stream %d ...\n", stream);
 
84
     fprintf(stderr,
 
85
 "              Please upgrade the declaration mcfio_Open statement \n");
 
86
     return -1;
 
87
  }
 
88
     
 
89
  if (str->row == MCFIO_READ) {
 
90
      for(i=0, j=-1; i<str->ehead->nBlocks; i++) {
 
91
           if (str->ehead->blockIds[i] == blkid) j = i;
 
92
        }
 
93
      if (j == -1) {
 
94
        fprintf(stderr,
 
95
 " mcfio_Block: Unable to find block i.d. %d in Stream %d \n", blkid, stream);
 
96
          return -1;  
 
97
      }
 
98
      if (xdr_setpos(str->xdr,str->ehead->ptrBlocks[j]) == FALSE) {
 
99
        fprintf(stderr,
 
100
         " mcfio_Block: Unable to position stream at block %d \n", blkid);
 
101
          return -1;  
 
102
      }
 
103
      str->currentPos = str->ehead->ptrBlocks[j];
 
104
  } else if (str->row == MCFIO_WRITE)  {
 
105
      idtmp = blkid;
 
106
      /*
 
107
      ** if to Sequential media, one first has to make sure we have 
 
108
      ** enough room in the buffer.
 
109
      */
 
110
      if (str->dos == MCFIO_SEQUENTIAL) {
 
111
         str->xdr->x_op = XDR_MCFIOCODE;
 
112
         ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
 
113
         str->xdr->x_op = XDR_ENCODE;
 
114
         if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
 
115
          /*
 
116
          ** Once again, I don't trust realloc, got to copy to the second 
 
117
          ** buffer. 
 
118
          */
 
119
             nbuff = 1 + 
 
120
                    (((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
 
121
                       str->maxlrec);
 
122
             str->buffer2 = 
 
123
                 (char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
 
124
             memcpy(str->buffer2, str->buffer, 
 
125
                       (str->currentPos - str->firstPos));
 
126
             free(str->buffer);
 
127
             str->buffer = str->buffer2;
 
128
             str->buffer2 = NULL;
 
129
             str->bufferSize = str->maxlrec * nbuff;
 
130
             xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
 
131
             if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
 
132
                 fprintf(stderr,
 
133
             " mcfio_Block:\n\
 
134
 Unable to position stream %d at block %d after realocation.\n", stream, blkid);
 
135
                 return -1; 
 
136
             } 
 
137
          }
 
138
       }
 
139
   }
 
140
   p1 = str->currentPos;
 
141
   ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
 
142
   if (ok == FALSE) {
 
143
        fprintf(stderr,
 
144
         " mcfio_Block: Unable to encode or decode block I.D. %d \n", blkid);
 
145
         j = str->ehead->nBlocks;
 
146
         if (xdr_setpos(str->xdr,p1) == FALSE) 
 
147
           fprintf(stderr,
 
148
         " mcfio_Block: Unable to position stream at block %d \n", blkid);
 
149
         return -1;
 
150
      }
 
151
   if(blkid != idtmp) {
 
152
        fprintf(stderr,
 
153
         " mcfio_Block: Unexpected I.D = %d found instead of I.D. %d \n",
 
154
              idtmp, blkid);
 
155
        return -1;
 
156
      }
 
157
    if (str->row == MCFIO_WRITE)  {  
 
158
      str->ehead->blockIds[str->ehead->nBlocks] = blkid;
 
159
      str->ehead->ptrBlocks[str->ehead->nBlocks] = p1;
 
160
      str->ehead->nBlocks++; 
 
161
    }
 
162
    str->currentPos = xdr_getpos(str->xdr);    
 
163
    str->numWordsC += (ntot/4);
 
164
    str->numWordsT += ((str->currentPos-p1)/4);
 
165
    return 1;
 
166
        
 
167
}
 
168
int mcfioC_NTuple(int stream, int nTupleId, char * version)
 
169
 
170
  int i, j, jstr, idtmp, ntot, nbuff;
 
171
  bool_t ok;
 
172
  u_int p1;
 
173
  mcfStream *str;
 
174
  nTuDDL *ddl;
 
175
  descrGenNtuple *dNTu;
 
176
     
 
177
  if (McfStreamPtrList == NULL) { 
 
178
     fprintf(stderr,
 
179
  " mcfio_NTuple: You must first initialize by calling mcfio_Init.\n"); 
 
180
     return -1;
 
181
  }
 
182
  jstr = stream-1;
 
183
  if (McfStreamPtrList[jstr] == NULL) { 
 
184
     fprintf(stderr,
 
185
 " mcfio_NTuple: First, declare the stream by calling mcfio_Open...\n"); 
 
186
     return -1;
 
187
  }
 
188
  
 
189
  ddl = mcf_GetNTuByStreamID(stream, nTupleId);
 
190
  if (ddl == NULL) {
 
191
     fprintf(stderr,
 
192
 " mcfio_NTuple: Illegal or inexistant NTuple Id %d for stream %d \n", 
 
193
     nTupleId, stream); 
 
194
     return -1;
 
195
  }
 
196
  if (ddl->reference == NULL) dNTu = ddl->descrNtu;
 
197
  else dNTu = ddl->reference->descrNtu;
 
198
  str = McfStreamPtrList[jstr];
 
199
  if ((str->row == MCFIO_WRITE) && 
 
200
      (str->fhead->nNTuples == str->ehead->nNTuples)) {
 
201
     fprintf(stderr,
 
202
" mcfio_NTuple: Maximum number of NTuples reached for stream %d ...\n", stream);
 
203
     fprintf(stderr,
 
204
 "              Please upgrade the Ntuple declarations statements. \n");
 
205
     return -1;
 
206
  }
 
207
     
 
208
  if (str->row == MCFIO_READ) {
 
209
      for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
 
210
           if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
 
211
        }
 
212
      if (j == -1) {
 
213
        fprintf(stderr,
 
214
 " mcfio_NTuple: Unable to find NTuple i.d. %d in Stream %d \n",
 
215
          nTupleId, stream);
 
216
          return -1;  
 
217
      }
 
218
      if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
 
219
        fprintf(stderr,
 
220
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
221
          return -1;  
 
222
      }
 
223
      str->currentPos = str->ehead->ptrNTuples[j];
 
224
  } else if (str->row == MCFIO_WRITE)  {
 
225
      /*
 
226
      ** if to Sequential media, one first has to make sure we have 
 
227
      ** enough room in the buffer.
 
228
      */
 
229
      if (str->dos == MCFIO_SEQUENTIAL) {
 
230
         str->xdr->x_op = XDR_MCFIOCODE;
 
231
         ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot,
 
232
                                 ddl->seqNTuId, version);
 
233
         str->xdr->x_op = XDR_ENCODE;
 
234
         if (ok == FALSE) {
 
235
             fprintf(stderr,
 
236
 "mcfio_NTuple: can not Encode or Decode Ntuple id % on Seq. Stream %d ", 
 
237
             nTupleId, stream);
 
238
             return -1;
 
239
         }
 
240
         if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
 
241
          /*
 
242
          ** Once again, I don't trust realloc, got to copy to the second 
 
243
          ** buffer. 
 
244
          */
 
245
             nbuff = 1 + 
 
246
                    (((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
 
247
                       str->maxlrec);
 
248
             str->buffer2 = 
 
249
                 (char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
 
250
             memcpy(str->buffer2, str->buffer, 
 
251
                       (str->currentPos - str->firstPos));
 
252
             free(str->buffer);
 
253
             str->buffer = str->buffer2;
 
254
             str->buffer2 = NULL;
 
255
             str->bufferSize = str->maxlrec * nbuff;
 
256
             xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
 
257
             if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
 
258
                 fprintf(stderr,
 
259
             " mcfio_NTuple:\n\
 
260
 Unable to position stream %d at Ntuple %d after realocation.\n",
 
261
                 stream, nTupleId);
 
262
                 return -1; 
 
263
             } 
 
264
          }
 
265
       }
 
266
   }
 
267
   p1 = str->currentPos;
 
268
   ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot, ddl->seqNTuId, version);
 
269
   if (ok == FALSE) {
 
270
        fprintf(stderr,
 
271
         " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
 
272
             nTupleId);
 
273
         j = str->ehead->nNTuples;
 
274
         if (xdr_setpos(str->xdr,p1) == FALSE) 
 
275
           fprintf(stderr,
 
276
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
277
         return -1;
 
278
      }
 
279
    if (str->row == MCFIO_WRITE)  {  
 
280
      str->ehead->nTupleIds[str->ehead->nNTuples] = ddl->seqNTuId;
 
281
      str->ehead->ptrNTuples[str->ehead->nNTuples] = p1;
 
282
      str->ehead->nNTuples++; 
 
283
    }
 
284
    str->currentPos = xdr_getpos(str->xdr);    
 
285
    str->numWordsC += (ntot/4);
 
286
    str->numWordsT += ((str->currentPos-p1)/4);
 
287
    return 1;
 
288
        
 
289
}
 
290
/*
 
291
** Optimized version used exclusively to read the multiplicity value 
 
292
** within an NTuple. It is assumed that the stream is open read direct 
 
293
** access (No checks!), and the event table is available, and the 
 
294
** NTuple is accessible.  Once again, No checks! Use at your onw risk.
 
295
** Also, we do not keep record of the number of byte Read.  
 
296
*/
 
297
int mcfioC_NTupleMult(int stream, int nTupleId, char * version)
 
298
 
299
  int i, j, jstr, idtmp, ntot, nbuff;
 
300
  bool_t ok;
 
301
  mcfStream *str;
 
302
  nTuDDL *ddl;
 
303
  descrGenNtuple *dNTu;
 
304
     
 
305
  jstr = stream-1;
 
306
  ddl = mcf_GetNTuByStreamID(stream, nTupleId);
 
307
  if (ddl->reference == NULL) dNTu = ddl->descrNtu;
 
308
  else dNTu = ddl->reference->descrNtu;
 
309
  str = McfStreamPtrList[jstr];
 
310
  for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
 
311
           if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
 
312
   }
 
313
  if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
 
314
        fprintf(stderr,
 
315
    " mcfio_NTupleMult: Unable to position stream at NTuple %d \n", nTupleId);
 
316
          return -1;  
 
317
      }
 
318
  str->currentPos = str->ehead->ptrNTuples[j];
 
319
  if (dNTu->multXDROffset == 0) 
 
320
      ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
 
321
                                   ddl->seqNTuId, version);
 
322
   else ok = xdr_mcfast_NTupleMult(str, dNTu, version);
 
323
   if (ok == FALSE) {
 
324
        fprintf(stderr,
 
325
         " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
 
326
             nTupleId);
 
327
         j = str->ehead->nNTuples;
 
328
         if (xdr_setpos(str->xdr, str->currentPos) == FALSE) 
 
329
           fprintf(stderr,
 
330
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
331
         return -1;
 
332
      }
 
333
      /*
 
334
      ** This probably could be optimized away. Note the that the current 
 
335
      ** position of the stream strored in str->currentPos is no longer 
 
336
      ** valied exiting this routine. However, there is enough redundancy 
 
337
      ** in the data structure to figure out where we could go..
 
338
      */
 
339
     /*  xdr_setpos(str->xdr, str->currentPos);   */ 
 
340
    return TRUE;
 
341
        
 
342
}
 
343
            
 
344
/*
 
345
** Optimized version used exclusively to read a specific variable  
 
346
** within an NTuple. Valid only if the variable is of fixed size 
 
347
** (e.g. not indexed by multiplicity) or if the data structure organization is
 
348
** of type parallel array. It is assumed that the stream is open read direct 
 
349
** access (No checks!), and the event table is available, and the 
 
350
** NTuple is accessible.  Once again, No checks! Use at your own risk.
 
351
*/
 
352
int mcfioC_NTupleVar(int stream, int nTupleId, int ivar, char * version)
 
353
 
354
  int i, j, jstr, idtmp, ntot, nbuff;
 
355
  bool_t ok;
 
356
  mcfStream *str;
 
357
  nTuDDL *ddl;
 
358
  descrGenNtuple *dNTu;
 
359
     
 
360
  jstr = stream-1;
 
361
  ddl = mcf_GetNTuByStreamID(stream, nTupleId);
 
362
  if (ddl->reference == NULL) dNTu = ddl->descrNtu;
 
363
  else dNTu = ddl->reference->descrNtu;
 
364
  str = McfStreamPtrList[jstr];
 
365
  for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
 
366
           if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
 
367
   }
 
368
  if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
 
369
        fprintf(stderr,
 
370
    " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
 
371
          return -1;  
 
372
      }
 
373
  str->currentPos = str->ehead->ptrNTuples[j];
 
374
  if (dNTu->multXDROffset == 0) 
 
375
      ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
 
376
                                   ddl->seqNTuId, version);
 
377
   else ok = xdr_mcfast_NTupleVar(str, dNTu, ivar, version);
 
378
   if (ok == FALSE) {
 
379
        fprintf(stderr,
 
380
         " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
 
381
             nTupleId);
 
382
         j = str->ehead->nNTuples;
 
383
         if (xdr_setpos(str->xdr, str->currentPos) == FALSE) 
 
384
           fprintf(stderr,
 
385
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
386
         return -1;
 
387
      }
 
388
    return TRUE;
 
389
        
 
390
}
 
391
/*
 
392
** Optimized version used exclusively to read a specific variable within a  
 
393
** substructure within an NTuple. Valid only if of type indexed  
 
394
** and if the data structure organization is
 
395
** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct 
 
396
** access (No checks!), and the event table is available, and the 
 
397
** NTuple is accessible.  Once again, No checks! Use at your own risk.
 
398
*/
 
399
int mcfioC_NTupleSubVar(int stream, int nTupleId, int ivar, int multIndex,
 
400
                               char * version)
 
401
 
402
  int i, j, jstr, idtmp, ntot, nbuff;
 
403
  bool_t ok;
 
404
  mcfStream *str;
 
405
  nTuDDL *ddl;
 
406
  descrGenNtuple *dNTu;
 
407
     
 
408
  jstr = stream-1;
 
409
  ddl = mcf_GetNTuByStreamID(stream, nTupleId);
 
410
  if (ddl->reference == NULL) dNTu = ddl->descrNtu;
 
411
  else dNTu = ddl->reference->descrNtu;
 
412
  str = McfStreamPtrList[jstr];
 
413
  for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
 
414
           if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
 
415
   }
 
416
  if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
 
417
        fprintf(stderr,
 
418
    " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
 
419
          return -1;  
 
420
      }
 
421
  str->currentPos = str->ehead->ptrNTuples[j];
 
422
  if (dNTu->multXDROffset == 0) 
 
423
      ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
 
424
                                   ddl->seqNTuId, version);
 
425
   else ok = xdr_mcfast_NTupleSubVar(str, dNTu, ivar, multIndex, version);
 
426
   if (ok == FALSE) {
 
427
        fprintf(stderr,
 
428
         " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
 
429
             nTupleId);
 
430
         j = str->ehead->nNTuples;
 
431
         if (xdr_setpos(str->xdr, str->currentPos) == FALSE) 
 
432
           fprintf(stderr,
 
433
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
434
         return -1;
 
435
      }
 
436
    return TRUE;
 
437
        
 
438
}
 
439
/*
 
440
** Optimized version used exclusively to read a specific   
 
441
** substructure within an NTuple. Valid only if of type indexed  
 
442
** and if the data structure organization is
 
443
** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct 
 
444
** access (No checks!), and the event table is available, and the 
 
445
** NTuple is accessible.  Once again, No checks! Use at your own risk.
 
446
*/
 
447
int mcfioC_NTupleSubStruct(int stream, int nTupleId, int multIndex,
 
448
                               char * version)
 
449
 
450
  int i, j, jstr, idtmp, ntot, nbuff;
 
451
  bool_t ok;
 
452
  mcfStream *str;
 
453
  nTuDDL *ddl;
 
454
  descrGenNtuple *dNTu;
 
455
     
 
456
  jstr = stream-1;
 
457
  ddl = mcf_GetNTuByStreamID(stream, nTupleId);
 
458
  if (ddl->reference == NULL) dNTu = ddl->descrNtu;
 
459
  else dNTu = ddl->reference->descrNtu;
 
460
  str = McfStreamPtrList[jstr];
 
461
  for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
 
462
           if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
 
463
   }
 
464
  if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
 
465
        fprintf(stderr,
 
466
    " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
 
467
          return -1;  
 
468
      }
 
469
  str->currentPos = str->ehead->ptrNTuples[j];
 
470
  if (dNTu->multXDROffset == 0) 
 
471
      ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
 
472
                                   ddl->seqNTuId, version);
 
473
   else ok = xdr_mcfast_NTupleSubStruct(str, dNTu, multIndex, version);
 
474
   if (ok == FALSE) {
 
475
        fprintf(stderr,
 
476
         " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
 
477
             nTupleId);
 
478
         j = str->ehead->nNTuples;
 
479
         if (xdr_setpos(str->xdr, str->currentPos) == FALSE) 
 
480
           fprintf(stderr,
 
481
         " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
 
482
         return -1;
 
483
      }
 
484
    return TRUE;
 
485
        
 
486
}