Fork me on GitHub

source: svn/trunk/classes/DelphesPileUpReader.cc

Last change on this file was 1394, checked in by Pavel Demin, 11 years ago

replace fseek with fseeko in DelphesPileUpReader

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision Date
File size: 3.2 KB
Line 
1
2/** \class DelphesPileUpReader
3 *
4 * Reads pile-up binary file
5 *
6 *
7 * $Date: 2014-05-06 09:29:55 +0000 (Tue, 06 May 2014) $
8 * $Revision: 1394 $
9 *
10 *
11 * \author P. Demin - UCL, Louvain-la-Neuve
12 *
13 */
14
15#include "classes/DelphesPileUpReader.h"
16
17#include <stdexcept>
18#include <iostream>
19#include <sstream>
20
21#include <stdio.h>
22#include <rpc/types.h>
23#include <rpc/xdr.h>
24
25using namespace std;
26
27static const int kIndexSize = 10000000;
28static const int kBufferSize = 1000000;
29static const int kRecordSize = 9;
30
31//------------------------------------------------------------------------------
32
33DelphesPileUpReader::DelphesPileUpReader(const char *fileName) :
34 fEntries(0), fEntrySize(0), fCounter(0),
35 fPileUpFile(0), fIndex(0), fBuffer(0),
36 fInputXDR(0), fIndexXDR(0), fBufferXDR(0)
37{
38 stringstream message;
39
40 fIndex = new char[kIndexSize*8];
41 fBuffer = new char[kBufferSize*kRecordSize*4];
42 fInputXDR = new XDR;
43 fIndexXDR = new XDR;
44 fBufferXDR = new XDR;
45 xdrmem_create(fIndexXDR, fIndex, kIndexSize*8, XDR_DECODE);
46 xdrmem_create(fBufferXDR, fBuffer, kBufferSize*kRecordSize*4, XDR_DECODE);
47
48 fPileUpFile = fopen(fileName, "r");
49
50 if(fPileUpFile == NULL)
51 {
52 message << "can't open pile-up file " << fileName;
53 throw runtime_error(message.str());
54 }
55
56 xdrstdio_create(fInputXDR, fPileUpFile, XDR_DECODE);
57
58 // read number of events
59 fseeko(fPileUpFile, -8, SEEK_END);
60 xdr_hyper(fInputXDR, &fEntries);
61
62 if(fEntries >= kIndexSize)
63 {
64 message << "too many events in pile-up file " << fileName;
65 throw runtime_error(message.str());
66 }
67
68 // read index of events
69 fseeko(fPileUpFile, -8 - 8*fEntries, SEEK_END);
70 xdr_opaque(fInputXDR, fIndex, fEntries*8);
71}
72
73//------------------------------------------------------------------------------
74
75DelphesPileUpReader::~DelphesPileUpReader()
76{
77 xdr_destroy(fInputXDR);
78 if(fPileUpFile) fclose(fPileUpFile);
79 xdr_destroy(fBufferXDR);
80 xdr_destroy(fIndexXDR);
81 if(fBufferXDR) delete fBufferXDR;
82 if(fIndexXDR) delete fIndexXDR;
83 if(fInputXDR) delete fInputXDR;
84 if(fBuffer) delete[] fBuffer;
85 if(fIndex) delete[] fIndex;
86}
87
88//------------------------------------------------------------------------------
89
90bool DelphesPileUpReader::ReadParticle(int &pid,
91 float &x, float &y, float &z, float &t,
92 float &px, float &py, float &pz, float &e)
93{
94 if(fCounter >= fEntrySize) return false;
95
96 xdr_int(fBufferXDR, &pid);
97 xdr_float(fBufferXDR, &x);
98 xdr_float(fBufferXDR, &y);
99 xdr_float(fBufferXDR, &z);
100 xdr_float(fBufferXDR, &t);
101 xdr_float(fBufferXDR, &px);
102 xdr_float(fBufferXDR, &py);
103 xdr_float(fBufferXDR, &pz);
104 xdr_float(fBufferXDR, &e);
105
106 ++fCounter;
107
108 return true;
109}
110
111//------------------------------------------------------------------------------
112
113bool DelphesPileUpReader::ReadEntry(quad_t entry)
114{
115 quad_t offset;
116
117 if(entry >= fEntries) return false;
118
119 // read event position
120 xdr_setpos(fIndexXDR, 8*entry);
121 xdr_hyper(fIndexXDR, &offset);
122
123 // read event
124 fseeko(fPileUpFile, offset, SEEK_SET);
125 xdr_int(fInputXDR, &fEntrySize);
126
127 if(fEntrySize >= kBufferSize)
128 {
129 throw runtime_error("too many particles in pile-up event");
130 }
131
132 xdr_opaque(fInputXDR, fBuffer, fEntrySize*kRecordSize*4);
133 xdr_setpos(fBufferXDR, 0);
134 fCounter = 0;
135
136 return true;
137}
138
139//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.