1 | /*
|
---|
2 | * Delphes: a framework for fast simulation of a generic collider experiment
|
---|
3 | * Copyright (C) 2018 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 DelphesXDRReader
|
---|
21 | *
|
---|
22 | * Reads XDR
|
---|
23 | *
|
---|
24 | * \author P. Demin - UCL, Louvain-la-Neuve
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "classes/DelphesXDRReader.h"
|
---|
29 |
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdint.h>
|
---|
32 | #include <string.h>
|
---|
33 |
|
---|
34 | //------------------------------------------------------------------------------
|
---|
35 |
|
---|
36 | DelphesXDRReader::DelphesXDRReader() :
|
---|
37 | fFile(0), fBuffer(0), fOffset(0)
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | //------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | void DelphesXDRReader::SetFile(FILE *file)
|
---|
44 | {
|
---|
45 | fFile = file;
|
---|
46 | }
|
---|
47 |
|
---|
48 | //------------------------------------------------------------------------------
|
---|
49 |
|
---|
50 | void DelphesXDRReader::SetBuffer(void *buffer)
|
---|
51 | {
|
---|
52 | fBuffer = (uint8_t *)buffer;
|
---|
53 | fOffset = 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | //------------------------------------------------------------------------------
|
---|
57 |
|
---|
58 | void DelphesXDRReader::SetOffset(int offset)
|
---|
59 | {
|
---|
60 | fOffset = offset;
|
---|
61 | }
|
---|
62 |
|
---|
63 | //------------------------------------------------------------------------------
|
---|
64 |
|
---|
65 | void DelphesXDRReader::ReadRaw(void *value, int size)
|
---|
66 | {
|
---|
67 | int rndup;
|
---|
68 |
|
---|
69 | rndup = size % 4;
|
---|
70 | if(rndup > 0)
|
---|
71 | {
|
---|
72 | rndup = 4 - rndup;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if(fFile)
|
---|
76 | {
|
---|
77 | fread(value, 1, size + rndup, fFile);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | //------------------------------------------------------------------------------
|
---|
82 |
|
---|
83 | void DelphesXDRReader::ReadValue(void *value, int size)
|
---|
84 | {
|
---|
85 | int i;
|
---|
86 | uint8_t *dst, buffer[8];
|
---|
87 |
|
---|
88 | dst = (uint8_t *)value;
|
---|
89 |
|
---|
90 | if(fBuffer)
|
---|
91 | {
|
---|
92 | fOffset += size;
|
---|
93 | for(i = 0; i < size; ++i) dst[i] = fBuffer[fOffset - 1 - i];
|
---|
94 | }
|
---|
95 | else if(fFile)
|
---|
96 | {
|
---|
97 | ReadRaw(buffer, size);
|
---|
98 | for(i = 0; i < size; ++i) dst[i] = buffer[size - 1 - i];
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | //------------------------------------------------------------------------------
|
---|
103 |
|
---|
104 | void DelphesXDRReader::ReadString(void *value, int maxSize)
|
---|
105 | {
|
---|
106 | int32_t size;
|
---|
107 |
|
---|
108 | ReadValue(&size, 4);
|
---|
109 |
|
---|
110 | if(size > maxSize) size = maxSize;
|
---|
111 |
|
---|
112 | if(fBuffer)
|
---|
113 | {
|
---|
114 | memcpy(value, fBuffer + fOffset, size);
|
---|
115 | fOffset += size;
|
---|
116 | }
|
---|
117 | else if(fFile)
|
---|
118 | {
|
---|
119 | ReadRaw(value, size);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | //------------------------------------------------------------------------------
|
---|