LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lStdHep.cc
Go to the documentation of this file.
1 //
3 // This class is based on the light-weight XDR class lXDR,
4 // and parses/writes StdHep files. It was mainly written,
5 // to provide a faster alternative to the more cumbersome
6 // methods using mcfio in StdHep.
7 //
8 // W.G.J. Langeveld, 24 May 2002
9 //
11 #include "UTIL/lStdHep.hh"
12 #include "string.h"
13 #include "stdlib.h"
14 
15 namespace UTIL{
16 
18 //
19 // The main lStdHep class.
20 //
22 
23 //
24 // Constructors, destructor
25 // ------------------------
26 // Constructor opens file, destructor closes file. Once opened for
27 // reading, the file cannot be written to, and v.v.
28 //
29 lStdHep::lStdHep(const char *filename, bool open_for_write) :
30  lXDR(filename, open_for_write),
31  ntot(0),version(0),title(0),comment(0),date(0),closingDate(0),numevts_expect(0),numevts(0),
32  firstTable(0),dimTable(0),nNTuples(0),nBlocks(0),blockIds(0),blockNames(0)
33 {
34  if (open_for_write) {
36  }
37  else {
39  }
40  return;
41 }
42 
44 {
45  delete [] version;
46  delete [] date;
47  delete [] closingDate;
48  delete [] comment;
49  delete [] title;
50  delete [] blockIds;
51  if (blockNames) {
52  for (int i = 0; i < nBlocks; i++) {
53  delete [] blockNames[i];
54  }
55  delete [] blockNames;
56  }
57  return;
58 }
59 
61 {
62  if (fp == 0) fp = stdout;
63 
64  fprintf(fp, "====== File Header ===========\n");
65  fprintf(fp, " total blocks: %ld\n", ntot);
66  fprintf(fp, " version: %s\n", version);
67  fprintf(fp, " title: %s\n", title);
68  fprintf(fp, " comment: %s\n", comment);
69  fprintf(fp, " date: %s", date);
70  fprintf(fp, " closing date: %s", closingDate);
71  fprintf(fp, " expected events: %ld\n", numevts_expect);
72  fprintf(fp, " events: %ld\n", numevts);
73  fprintf(fp, " firstTable: %ld\n", firstTable);
74  fprintf(fp, " dimTable: %ld\n", dimTable);
75  fprintf(fp, " nNTuples: %ld\n", nNTuples);
76  fprintf(fp, " nBlocks: %ld\n", nBlocks);
77  if (nBlocks) fprintf(fp, " block names:\n");
78  for (int i = 0; i < nBlocks; i++) {
79  fprintf(fp, " : %s\n", blockNames[i]);
80  }
81  fprintf(fp, "=============================\n");
82  return;
83 }
84 
86 {
87  if (fp == 0) fp = stdout;
88  eventTable.print(fp);
89  return;
90 }
91 
93 {
94  if (fp == 0) fp = stdout;
95  event.printHeader(fp);
96  return;
97 }
98 
99 void lStdHep::printEvent(FILE *fp)
100 {
101  if (fp == 0) fp = stdout;
102  event.print(fp);
103  return;
104 }
105 
106 void lStdHep::printTrack(int i, FILE *fp)
107 {
108  if (fp == 0) fp = stdout;
109  if (i < event.nhep) {
110  fprintf(fp, " Track: id: %ld, vtx: (%g, %g, %g, %g), mom: (%g, %g, %g, %g, %g)\n",
111  pid(i), X(i), Y(i), Z(i), T(i), Px(i), Py(i), Pz(i), E(i), M(i));
112  fprintf(fp, " Track: wgt: %g, alpha QED: %g, alpha QCD: %g, idrup: %ld\n",
113  eventweight(), alphaQED(), alphaQCD(), idrup());
114  }
115  return;
116 }
117 
119 {
120  if (fp == 0) fp = stdout;
121  return;
122 }
123 
125 {
126  if (fp == 0) fp = stdout;
127  return;
128 }
129 
130 bool lStdHep::more(void)
131 {
132  return(getError() == LSH_SUCCESS);
133 }
134 
136 {
137 //
138 // Look for an event or an event table
139 //
140  event.isEmpty = 1;
141  while (1) {
145 
146  if (event.read(*this) != LSH_SUCCESS) return(getError());
147  eventTable.ievt++;
148 
149  if (event.isEmpty) continue;
150  return(getError());
151  }
152 
153  eventTable.isEmpty = 1;
154  while (eventTable.isEmpty) {
155  if (eventTable.nextlocator == -2) {
156 //
157 // This was the last event table, signal quitting. Not an error.
158 //
160  return(getError());
161  }
162  else if (eventTable.nextlocator == -1) {
164  return(getError());
165  }
166 //
167 // Go to the next event table
168 //
170  if (eventTable.read(*this) != LSH_SUCCESS) return(getError());
171  }
172  }
173  return(getError());
174 }
175 
176 long lStdHep::getEvent(lStdEvent &lse) const
177 {
178  if (long stat = getError() != LSH_SUCCESS) return(stat);
179 
180  lse.evtNum = event.nevhep;
181 
182  lse.clear();
183  for (int i = 0; i < event.nhep; i++) {
184  lStdTrack lst;
185  lst.X = X(i);
186  lst.Y = Y(i);
187  lst.Z = Z(i);
188  lst.T = T(i);
189  lst.Px = Px(i);
190  lst.Py = Py(i);
191  lst.Pz = Pz(i);
192  lst.E = E(i);
193  lst.M = M(i);
194  lst.pid = pid(i);
195  lst.status = status(i);
196  lst.mother1 = mother1(i);
197  lst.mother2 = mother2(i);
198  lst.daughter1 = daughter1(i);
199  lst.daughter2 = daughter2(i);
200  lse.push_back(lst);
201  }
202  return(LSH_SUCCESS);
203 }
204 
206 {
207  long stat = readEvent();
208  if (stat != LSH_SUCCESS) return(stat);
209  return(getEvent(lse));
210 }
211 
213 {
214  long len, blockid;
215 
216  blockid = readLong();
217  if (blockid != LSH_FILEHEADER) {
219  return(getError());
220  }
221  ntot = readLong();
222  version = readString(len);
223 
224  title = readString(len);
225  comment = readString(len);
226  date = readString(len);
227  if ((strcmp(version, "2.00") == 0) || (strcmp(version, "1.00") == 0)) {
228  closingDate = new char[len + 1];
229  strcpy((char *) closingDate, date);
230  }
231  else {
232  closingDate = readString(len);
233  }
234 
236  numevts = readLong();
237  firstTable = readLong();
238  dimTable = readLong();
239  nBlocks = readLong();
240  if (*version != '2') {
241  nNTuples = 0;
242  }
243  else {
244  nNTuples = readLong();
245  }
246 
248  blockNames = new const char *[nBlocks];
249  for (int i = 0; i < nBlocks; i++) blockNames[i] = readString(len);
251 //
252 // Read the first event table
253 //
254  eventTable.read(*this);
255  return(getError());
256 }
257 
259 {
260  return(LSH_NOTSUPPORTED);
261 }
262 
263 long lStdHep::setEvent(const lStdEvent &lse)
264 {
265 // ***************set up event buffer!
266  setNTracks(lse.size());
267  setEvtNum(lse.evtNum);
268 
269  for (int i = 0; i < event.nhep; i++) {
270  setX (i, lse[i].X);
271  setY (i, lse[i].Y);
272  setZ (i, lse[i].Z);
273  setT (i, lse[i].T);
274  setPx (i, lse[i].Px);
275  setPy (i, lse[i].Py);
276  setPz (i, lse[i].Pz);
277  setE (i, lse[i].E);
278  setM (i, lse[i].M);
279  setPid (i, lse[i].pid);
280  setStatus (i, lse[i].status);
281  setMother1 (i, lse[i].mother1);
282  setMother2 (i, lse[i].mother2);
283  setDaughter1(i, lse[i].daughter1);
284  setDaughter2(i, lse[i].daughter2);
285  }
286  return(LSH_SUCCESS);
287 }
288 
290 {
291  long stat = writeEvent();
292  if (stat != LSH_SUCCESS) return(stat);
293  return(setEvent(lse));
294 }
295 
297  isEmpty(1), ievt(0), blockid(0), ntot(0), version(0),
298  nextlocator(-3), numEvts(0), evtnums(0),
299  storenums(0), runnums(0), trigMasks(0), ptrEvents(0)
300 {
301  return;
302 }
303 
305 {
306  cleanup();
307  return;
308 }
309 
311 {
312  delete [] version; version = 0;
313  delete [] evtnums; evtnums = 0;
314  delete [] storenums; storenums = 0;
315  delete [] runnums; runnums = 0;
316  delete [] trigMasks; trigMasks = 0;
317  delete [] ptrEvents; ptrEvents = 0;
318  isEmpty = 1;
319  ievt = ntot = blockid = numEvts = 0; // leave nextlocator alone!
320  return;
321 }
322 
324 {
325  long len;
326 
327  cleanup();
328 
329  blockid = ls.readLong();
330  ntot = ls.readLong();
331  version = ls.readString(len);
332 
333  if (blockid != LSH_EVENTTABLE) {
335  return(ls.getError());
336  }
337  nextlocator = ls.readLong();
338  numEvts = ls.readLong();
339  evtnums = ls.readLongArray(len);
340  storenums = ls.readLongArray(len);
341  runnums = ls.readLongArray(len);
342  trigMasks = ls.readLongArray(len);
343  ptrEvents = ls.readLongArray(len);
344  if (numEvts > 0) isEmpty = 0;
345  return(ls.getError());
346 }
347 
349 {
350  fprintf(fp, " EventTable: blockid: %ld, ntot: %ld, version: %s\n", blockid, ntot, version);
351  fprintf(fp, " EventTable: nextlocator: %ld, numEvts: %ld\n", nextlocator, numEvts);
352  for (int i = 0; i < numEvts; i++) {
353  fprintf(fp, " EventTable: %d: evtnums %ld storenums %ld runnums %ld trigMasks %ld ptrEvents %ld\n",
354  i, evtnums[i], storenums[i], runnums[i], trigMasks[i], ptrEvents[i]);
355  if (i == 10) {
356  fprintf(fp, " EventTable: etc.\n");
357  break;
358  }
359  }
360  return(0);
361 }
362 
364  isEmpty(0), blockid(0),ntot(0),version(0),evtnum(0),storenum(0),runnum(0),
365  trigMask(0),nBlocks(0),dimBlocks(0),nNTuples(0),dimNTuples(0),blockIds(0),
366  ptrBlocks(0),nevhep(0),nhep(0),isthep(0),idhep(0),jmohep(0),jdahep(0),phep(0),
367  vhep(0),eventweight(0),alphaqed(0),alphaqcd(0),scale(0),spin(0),colorflow(0),idrup(0),
368  bnevtreq(0),bnevtgen(0),bnevtwrt(0),bstdecom(0),bstdxsec(0),bstdseed1(0),bstdseed2(0),
369  enevtreq(0),enevtgen(0),enevtwrt(0),estdecom(0),estdxsec(0),estdseed1(0),estdseed2(0)
370 
371 {
372  return;
373 }
374 
376 {
377  cleanup();
378 }
379 
381 {
382  delete [] version; version = 0;
383  delete [] ptrBlocks; ptrBlocks = 0;
384  delete [] blockIds; blockIds = 0;
385  delete [] isthep; isthep = 0;
386  delete [] idhep; idhep = 0;
387  delete [] jmohep; jmohep = 0;
388  delete [] jdahep; jdahep = 0;
389  delete [] phep; phep = 0;
390  delete [] vhep; vhep = 0;
391  delete [] scale; scale = 0;
392  delete [] spin; spin = 0;
393  delete [] colorflow; colorflow = 0;
394  blockid = ntot = nevhep = nhep = 0;
395  isEmpty = 1;
396  return;
397 }
398 
400 {
401 //
402 // Read event header
403 //
404  long len;
405 
406  cleanup();
407 
408  blockid = ls.readLong();
409  ntot = ls.readLong();
410  version = ls.readString(len);
411  if (blockid != LSH_EVENTHEADER) ls.setError(LSH_NOEVENT);
412 
413  evtnum = ls.readLong();
414  storenum = ls.readLong();
415  runnum = ls.readLong();
416  trigMask = ls.readLong();
417  nBlocks = ls.readLong();
418  dimBlocks = ls.readLong();
419 
420  if (*version == '2') {
421  nNTuples = ls.readLong();
422  dimNTuples = ls.readLong();
423  if (dimBlocks) {
424  blockIds = ls.readLongArray(len);
425  ptrBlocks = ls.readLongArray(len);
426  }
427  if (dimNTuples) {
429  return(ls.getError());
430  }
431  }
432  else {
433  nNTuples = 0;
434  dimNTuples = 0;
435  blockIds = ls.readLongArray(len);
436  ptrBlocks = ls.readLongArray(len);
437  }
438 //
439 // Read event
440 //
441  for (int i = 0; i < nBlocks; i++) {
442  blockid = ls.readLong();
443  ntot = ls.readLong();
444  if (version) delete [] version;
445  version = ls.readString(len);
446 
447  isEmpty = 0;
448  switch (blockIds[i]) {
449  case LSH_STDHEP : // 101
450  nevhep = ls.readLong();
451  nhep = ls.readLong();
452  if (isthep) delete [] isthep;
453  isthep = ls.readLongArray(len);
454  if (idhep) delete [] idhep;
455  idhep = ls.readLongArray(len);
456  if (jmohep) delete [] jmohep;
457  jmohep = ls.readLongArray(len);
458  if (jdahep) delete [] jdahep;
459  jdahep = ls.readLongArray(len);
460  if (phep) delete [] phep;
461  phep = ls.readDoubleArray(len);
462  if (vhep) delete [] vhep;
463  vhep = ls.readDoubleArray(len);
464  break;
465  case LSH_STDHEPEV4 : // 201
466  nevhep = ls.readLong();
467  nhep = ls.readLong();
468  if (isthep) delete [] isthep;
469  isthep = ls.readLongArray(len);
470  if (idhep) delete [] idhep;
471  idhep = ls.readLongArray(len);
472  if (jmohep) delete [] jmohep;
473  jmohep = ls.readLongArray(len);
474  if (jdahep) delete [] jdahep;
475  jdahep = ls.readLongArray(len);
476  if (phep) delete [] phep;
477  phep = ls.readDoubleArray(len);
478  if (vhep) delete [] vhep;
479  vhep = ls.readDoubleArray(len);
480 //
481 // New stuff for STDHEPEV4:
482 //
483  eventweight = ls.readDouble();
484  alphaqed = ls.readDouble();
485  alphaqcd = ls.readDouble();
486  if (scale) delete [] scale;
487  scale = ls.readDoubleArray(len);
488  if (spin) delete [] spin;
489  spin = ls.readDoubleArray(len);
490  if (colorflow) delete [] colorflow;
491  colorflow = ls.readLongArray(len);
492  idrup = ls.readLong();
493  break;
494  case LSH_OFFTRACKARRAYS : // 102
495  case LSH_OFFTRACKSTRUCT : // 103
496  case LSH_TRACEARRAYS : // 104
497  case LSH_STDHEPM : // 105
498  break;
499  case LSH_STDHEPBEG : // 106
500  bnevtreq = ls.readLong();
501  bnevtgen = ls.readLong();
502  bnevtwrt = ls.readLong();
503  bstdecom = ls.readFloat();
504  bstdxsec = ls.readFloat();
505  bstdseed1 = ls.readDouble();
506  bstdseed2 = ls.readDouble();
507  isEmpty = 1;
508  break;
509  case LSH_STDHEPEND : // 107
510  enevtreq = ls.readLong();
511  enevtgen = ls.readLong();
512  enevtwrt = ls.readLong();
513  estdecom = ls.readFloat();
514  estdxsec = ls.readFloat();
515  estdseed1 = ls.readDouble();
516  estdseed2 = ls.readDouble();
517  isEmpty = 1;
518  break;
519  case LSH_STDHEPCXX : // 108
520  break;
521  }
522  }
523  return(ls.getError());
524 }
525 
527 {
528  fprintf(fp, " EventHeader: blockid: %ld, ntot: %ld, version: %s\n", blockid, ntot, version);
529  fprintf(fp, " : evtnum: %ld, storenum: %ld, runnum: %ld, trigMask: %ld, nBlocks: %ld, dimBlocks: %ld\n",
530  evtnum, storenum, runnum, trigMask, nBlocks, dimBlocks);
531  fprintf(fp, " : nNTuples: %ld, dimNTuples: %ld\n", nNTuples, dimNTuples);
532 
533  for (int i = 0; i < nBlocks; i++) {
534  const char *labels[10] = {"Event", "Off-track arrays", "Off-track struct", "Trace arrays",
535  "Event with multiple interactions", "Begin run", "End run", "StdHepCXX",
536  "EventV4", "Unknown" };
537  int j = blockIds[i] - 101;
538  if (blockIds[i] == LSH_STDHEPEV4) j = 8;
539  if ((j < 0) || (j > 9)) j = 9;
540  fprintf(fp, " : %d: blockIds %ld (%s) ptrBlocks %ld\n",
541  i, blockIds[i], labels[j], ptrBlocks[i]);
542  }
543  return(0);
544 }
545 
546 long lStdHep::Event::print(FILE *fp)
547 {
548  fprintf(fp, " Event: nevhep: %ld, nhep: %ld\n", nevhep, nhep);
549  return(0);
550 }
551 
552 }
double Y(int i) const
Definition: lStdHep.hh:190
long mother2(int i) const
Definition: lStdHep.hh:201
#define LSH_NOEVENTTABLE
Definition: lStdHep.hh:56
void setDaughter1(int i, long n)
Definition: lStdHep.hh:250
double alphaQCD(void) const
Definition: lStdHep.hh:206
long filePosition(long pos=-1)
Definition: lXDR.cc:338
long readLong(void)
Definition: lXDR.cc:152
bool more(void)
Definition: lStdHep.cc:130
long daughter1
Definition: lStdHep.hh:95
long getEvent(lStdEvent &lse) const
Definition: lStdHep.cc:176
long daughter1(int i) const
Definition: lStdHep.hh:202
double X(int i) const
Definition: lStdHep.hh:189
void printBeginRunRecord(FILE *fp=0)
Definition: lStdHep.cc:118
void printFileHeader(FILE *fp=0)
Definition: lStdHep.cc:60
void setPid(int i, long aPid)
Definition: lStdHep.hh:246
long mother1(int i) const
Definition: lStdHep.hh:200
double scale(int i, int j) const
Definition: lStdHep.hh:207
Event event
Definition: lStdHep.hh:406
void cleanup(void)
Definition: lStdHep.cc:380
void printTrack(int i, FILE *fp=0)
Definition: lStdHep.cc:106
double readFloat(void)
Definition: lXDR.cc:166
long daughter2(int i) const
Definition: lStdHep.hh:203
#define LSH_OFFTRACKSTRUCT
Definition: lStdHep.hh:66
double alphaQED(void) const
Definition: lStdHep.hh:205
long status(int i) const
Definition: lStdHep.hh:199
void printEventTable(FILE *fp=0)
Definition: lStdHep.cc:85
long readEvent(void)
Definition: lStdHep.cc:135
void printEvent(FILE *fp=0)
Definition: lStdHep.cc:99
long idrup(void) const
Definition: lStdHep.hh:212
#define LSH_STDHEPCXX
Definition: lStdHep.hh:71
void setPx(int i, double px)
Definition: lStdHep.hh:241
#define LSH_EVENTTABLE
Definition: lStdHep.hh:47
double Z(int i) const
Definition: lStdHep.hh:191
void setT(int i, double t)
Definition: lStdHep.hh:240
void setError(long error)
Definition: lXDR.hh:110
#define LSH_STDHEPM
Definition: lStdHep.hh:68
#define LSH_NOTSUPPORTED
Definition: lStdHep.hh:58
long print(FILE *fp)
Definition: lStdHep.cc:546
long daughter2
Definition: lStdHep.hh:96
#define LSH_FILEHEADER
Definition: lStdHep.hh:46
long nNTuples
Definition: lStdHep.hh:279
long pid(int i) const
Definition: lStdHep.hh:198
void setDaughter2(int i, long n)
Definition: lStdHep.hh:251
void setNTracks(long n)
Definition: lStdHep.hh:234
void printEndRunRecord(FILE *fp=0)
Definition: lStdHep.cc:124
#define LSH_STDHEPEND
Definition: lStdHep.hh:70
#define LSH_NOEVENT
Definition: lStdHep.hh:57
long printHeader(FILE *fp)
Definition: lStdHep.cc:526
#define LSH_STDHEPBEG
Definition: lStdHep.hh:69
void setY(int i, double y)
Definition: lStdHep.hh:238
lStdHep(const char *filename=0, bool open_for_write=false)
Definition: lStdHep.cc:29
void setX(int i, double x)
Definition: lStdHep.hh:237
T push_back(T...args)
long * readLongArray(long &length)
Definition: lXDR.cc:188
const char ** blockNames
Definition: lStdHep.hh:282
#define LSH_ENDOFFILE
Definition: lStdHep.hh:60
const char * date
Definition: lStdHep.hh:272
#define LSH_EVTABLECORRUPT
Definition: lStdHep.hh:59
void setM(int i, double m)
Definition: lStdHep.hh:245
void setE(int i, double e)
Definition: lStdHep.hh:244
const char * version
Definition: lStdHep.hh:269
long read(lStdHep &ls)
Definition: lStdHep.cc:399
double * readDoubleArray(long &length)
Definition: lXDR.cc:220
#define LSH_BLOCKERROR
Definition: lStdHep.hh:55
#define LSH_TRACEARRAYS
Definition: lStdHep.hh:67
double Px(int i) const
Definition: lStdHep.hh:193
void setPz(int i, double pz)
Definition: lStdHep.hh:243
long readFileHeader(void)
Definition: lStdHep.cc:212
void setPy(int i, double py)
Definition: lStdHep.hh:242
#define LSH_EVENTHEADER
Definition: lStdHep.hh:49
double E(int i) const
Definition: lStdHep.hh:196
virtual ~lStdHep()
Definition: lStdHep.cc:43
long read(lStdHep &ls)
Definition: lStdHep.cc:323
T clear(T...args)
long print(FILE *fp)
Definition: lStdHep.cc:348
#define LSH_STDHEP
Definition: lStdHep.hh:64
void setZ(int i, double z)
Definition: lStdHep.hh:239
double Py(int i) const
Definition: lStdHep.hh:194
void setEvtNum(long n)
Definition: lStdHep.hh:235
const char * closingDate
Definition: lStdHep.hh:273
T size(T...args)
long firstTable
Definition: lStdHep.hh:277
EventTable eventTable
Definition: lStdHep.hh:325
void setStatus(int i, long s)
Definition: lStdHep.hh:247
long nBlocks
Definition: lStdHep.hh:280
long setEvent(const lStdEvent &lse)
Definition: lStdHep.cc:263
long * blockIds
Definition: lStdHep.hh:281
void setMother1(int i, long n)
Definition: lStdHep.hh:248
double readDouble(void)
Definition: lXDR.cc:159
double M(int i) const
Definition: lStdHep.hh:197
double Pz(int i) const
Definition: lStdHep.hh:195
#define LSH_OFFTRACKARRAYS
Definition: lStdHep.hh:65
long getError(void) const
Definition: lXDR.hh:72
#define LSH_SUCCESS
Definition: lStdHep.hh:54
long dimTable
Definition: lStdHep.hh:278
#define LSH_STDHEPEV4
Definition: lStdHep.hh:72
long numevts_expect
Definition: lStdHep.hh:275
long numevts
Definition: lStdHep.hh:276
double T(int i) const
Definition: lStdHep.hh:192
const char * title
Definition: lStdHep.hh:270
const char * comment
Definition: lStdHep.hh:271
const char * readString(long &length)
Definition: lXDR.cc:173
long colorflow(int i, int j) const
Definition: lStdHep.hh:211
void printEventHeader(FILE *fp=0)
Definition: lStdHep.cc:92
double eventweight(void) const
Definition: lStdHep.hh:204
long writeEvent(void)
Definition: lStdHep.cc:258
void setMother2(int i, long n)
Definition: lStdHep.hh:249