Fork me on GitHub

source: git/classes/DelphesPileUpReader.cc@ 77e9ae1

ImprovedOutputFile Timing llp
Last change on this file since 77e9ae1 was 341014c, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

apply .clang-format to all .h, .cc and .cpp files

  • Property mode set to 100644
File size: 4.0 KB
Line 
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/** \class DelphesPileUpReader
20 *
21 * Reads pile-up binary file
22 *
23 * \author P. Demin - UCL, Louvain-la-Neuve
24 *
25 */
26
27#include "classes/DelphesPileUpReader.h"
28
29#include <iostream>
30#include <sstream>
31#include <stdexcept>
32
33#include <stdint.h>
34#include <stdio.h>
35
36#include "classes/DelphesXDRReader.h"
37
38using namespace std;
39
40static const int kIndexSize = 10000000;
41static const int kBufferSize = 1000000;
42static const int kRecordSize = 9;
43
44//------------------------------------------------------------------------------
45
46DelphesPileUpReader::DelphesPileUpReader(const char *fileName) :
47 fEntries(0), fEntrySize(0), fCounter(0),
48 fPileUpFile(0), fIndex(0), fBuffer(0),
49 fInputReader(0), fIndexReader(0), fBufferReader(0)
50{
51 stringstream message;
52
53 fIndex = new uint8_t[kIndexSize * 8];
54 fBuffer = new uint8_t[kBufferSize * kRecordSize * 4];
55 fInputReader = new DelphesXDRReader;
56 fIndexReader = new DelphesXDRReader;
57 fBufferReader = new DelphesXDRReader;
58
59 fIndexReader->SetBuffer(fIndex);
60 fBufferReader->SetBuffer(fBuffer);
61
62 fPileUpFile = fopen(fileName, "rb");
63
64 if(fPileUpFile == NULL)
65 {
66 message << "can't open pile-up file " << fileName;
67 throw runtime_error(message.str());
68 }
69
70 fInputReader->SetFile(fPileUpFile);
71
72 // read number of events
73 fseeko(fPileUpFile, -8, SEEK_END);
74 fInputReader->ReadValue(&fEntries, 8);
75
76 if(fEntries >= kIndexSize)
77 {
78 message << "too many events in pile-up file " << fileName;
79 throw runtime_error(message.str());
80 }
81
82 // read index of events
83 fseeko(fPileUpFile, -8 - 8 * fEntries, SEEK_END);
84 fInputReader->ReadRaw(fIndex, fEntries * 8);
85}
86
87//------------------------------------------------------------------------------
88
89DelphesPileUpReader::~DelphesPileUpReader()
90{
91 if(fPileUpFile) fclose(fPileUpFile);
92 if(fBufferReader) delete fBufferReader;
93 if(fIndexReader) delete fIndexReader;
94 if(fInputReader) delete fInputReader;
95 if(fBuffer) delete[] fBuffer;
96 if(fIndex) delete[] fIndex;
97}
98
99//------------------------------------------------------------------------------
100
101bool DelphesPileUpReader::ReadParticle(int32_t &pid,
102 float &x, float &y, float &z, float &t,
103 float &px, float &py, float &pz, float &e)
104{
105 if(fCounter >= fEntrySize) return false;
106
107 fBufferReader->ReadValue(&pid, 4);
108 fBufferReader->ReadValue(&x, 4);
109 fBufferReader->ReadValue(&y, 4);
110 fBufferReader->ReadValue(&z, 4);
111 fBufferReader->ReadValue(&t, 4);
112 fBufferReader->ReadValue(&px, 4);
113 fBufferReader->ReadValue(&py, 4);
114 fBufferReader->ReadValue(&pz, 4);
115 fBufferReader->ReadValue(&e, 4);
116
117 ++fCounter;
118
119 return true;
120}
121
122//------------------------------------------------------------------------------
123
124bool DelphesPileUpReader::ReadEntry(int64_t entry)
125{
126 int64_t offset;
127
128 if(entry >= fEntries) return false;
129
130 // read event position
131 fIndexReader->SetOffset(8 * entry);
132 fIndexReader->ReadValue(&offset, 8);
133
134 // read event
135 fseeko(fPileUpFile, offset, SEEK_SET);
136 fInputReader->ReadValue(&fEntrySize, 4);
137
138 if(fEntrySize >= kBufferSize)
139 {
140 throw runtime_error("too many particles in pile-up event");
141 }
142
143 fInputReader->ReadRaw(fBuffer, fEntrySize * kRecordSize * 4);
144 fBufferReader->SetOffset(0);
145 fCounter = 0;
146
147 return true;
148}
149
150//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.