1 | /*
|
---|
2 | * Delphes: a framework for fast simulation of a generic collider experiment
|
---|
3 | * Copyright (C) 2012-2014 Universite catholique de Louvain (UCL), Belgium
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /** \class DelphesPileUpReader
|
---|
21 | *
|
---|
22 | * Reads pile-up binary file
|
---|
23 | *
|
---|
24 | * \author P. Demin - UCL, Louvain-la-Neuve
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "classes/DelphesPileUpReader.h"
|
---|
29 |
|
---|
30 | #include <stdexcept>
|
---|
31 | #include <iostream>
|
---|
32 | #include <sstream>
|
---|
33 |
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stdint.h>
|
---|
36 |
|
---|
37 | #include "classes/DelphesXDRReader.h"
|
---|
38 |
|
---|
39 | using namespace std;
|
---|
40 |
|
---|
41 | static const int kIndexSize = 10000000;
|
---|
42 | static const int kBufferSize = 1000000;
|
---|
43 | static const int kRecordSize = 9;
|
---|
44 |
|
---|
45 | //------------------------------------------------------------------------------
|
---|
46 |
|
---|
47 | DelphesPileUpReader::DelphesPileUpReader(const char *fileName) :
|
---|
48 | fEntries(0), fEntrySize(0), fCounter(0),
|
---|
49 | fPileUpFile(0), fIndex(0), fBuffer(0),
|
---|
50 | fInputReader(0), fIndexReader(0), fBufferReader(0)
|
---|
51 | {
|
---|
52 | stringstream message;
|
---|
53 |
|
---|
54 | fIndex = new uint8_t[kIndexSize*8];
|
---|
55 | fBuffer = new uint8_t[kBufferSize*kRecordSize*4];
|
---|
56 | fInputReader = new DelphesXDRReader;
|
---|
57 | fIndexReader = new DelphesXDRReader;
|
---|
58 | fBufferReader = new DelphesXDRReader;
|
---|
59 |
|
---|
60 | fIndexReader->SetBuffer(fIndex);
|
---|
61 | fBufferReader->SetBuffer(fBuffer);
|
---|
62 |
|
---|
63 | fPileUpFile = fopen(fileName, "rb");
|
---|
64 |
|
---|
65 | if(fPileUpFile == NULL)
|
---|
66 | {
|
---|
67 | message << "can't open pile-up file " << fileName;
|
---|
68 | throw runtime_error(message.str());
|
---|
69 | }
|
---|
70 |
|
---|
71 | fInputReader->SetFile(fPileUpFile);
|
---|
72 |
|
---|
73 | // read number of events
|
---|
74 | fseeko(fPileUpFile, -8, SEEK_END);
|
---|
75 | fInputReader->ReadValue(&fEntries, 8);
|
---|
76 |
|
---|
77 | if(fEntries >= kIndexSize)
|
---|
78 | {
|
---|
79 | message << "too many events in pile-up file " << fileName;
|
---|
80 | throw runtime_error(message.str());
|
---|
81 | }
|
---|
82 |
|
---|
83 | // read index of events
|
---|
84 | fseeko(fPileUpFile, -8 - 8*fEntries, SEEK_END);
|
---|
85 | fInputReader->ReadRaw(fIndex, fEntries*8);
|
---|
86 | }
|
---|
87 |
|
---|
88 | //------------------------------------------------------------------------------
|
---|
89 |
|
---|
90 | DelphesPileUpReader::~DelphesPileUpReader()
|
---|
91 | {
|
---|
92 | if(fPileUpFile) fclose(fPileUpFile);
|
---|
93 | if(fBufferReader) delete fBufferReader;
|
---|
94 | if(fIndexReader) delete fIndexReader;
|
---|
95 | if(fInputReader) delete fInputReader;
|
---|
96 | if(fBuffer) delete[] fBuffer;
|
---|
97 | if(fIndex) delete[] fIndex;
|
---|
98 | }
|
---|
99 |
|
---|
100 | //------------------------------------------------------------------------------
|
---|
101 |
|
---|
102 | bool DelphesPileUpReader::ReadParticle(int32_t &pid,
|
---|
103 | float &x, float &y, float &z, float &t,
|
---|
104 | float &px, float &py, float &pz, float &e)
|
---|
105 | {
|
---|
106 | if(fCounter >= fEntrySize) return false;
|
---|
107 |
|
---|
108 | fBufferReader->ReadValue(&pid, 4);
|
---|
109 | fBufferReader->ReadValue(&x, 4);
|
---|
110 | fBufferReader->ReadValue(&y, 4);
|
---|
111 | fBufferReader->ReadValue(&z, 4);
|
---|
112 | fBufferReader->ReadValue(&t, 4);
|
---|
113 | fBufferReader->ReadValue(&px, 4);
|
---|
114 | fBufferReader->ReadValue(&py, 4);
|
---|
115 | fBufferReader->ReadValue(&pz, 4);
|
---|
116 | fBufferReader->ReadValue(&e, 4);
|
---|
117 |
|
---|
118 | ++fCounter;
|
---|
119 |
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | //------------------------------------------------------------------------------
|
---|
124 |
|
---|
125 | bool DelphesPileUpReader::ReadEntry(int64_t entry)
|
---|
126 | {
|
---|
127 | int64_t offset;
|
---|
128 |
|
---|
129 | if(entry >= fEntries) return false;
|
---|
130 |
|
---|
131 | // read event position
|
---|
132 | fIndexReader->SetOffset(8*entry);
|
---|
133 | fIndexReader->ReadValue(&offset, 8);
|
---|
134 |
|
---|
135 | // read event
|
---|
136 | fseeko(fPileUpFile, offset, SEEK_SET);
|
---|
137 | fInputReader->ReadValue(&fEntrySize, 4);
|
---|
138 |
|
---|
139 | if(fEntrySize >= kBufferSize)
|
---|
140 | {
|
---|
141 | throw runtime_error("too many particles in pile-up event");
|
---|
142 | }
|
---|
143 |
|
---|
144 | fInputReader->ReadRaw(fBuffer, fEntrySize*kRecordSize*4);
|
---|
145 | fBufferReader->SetOffset(0);
|
---|
146 | fCounter = 0;
|
---|
147 |
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 |
|
---|
151 | //------------------------------------------------------------------------------
|
---|