Fork me on GitHub

source: git/classes/DelphesSTDHEPReader.cc@ 1d1f6a4

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 1d1f6a4 was cab38f6, checked in by Pavel Demin <pavel.demin@…>, 10 years ago

remove svn tags and fix formatting

  • Property mode set to 100644
File size: 12.1 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
20/** \class DelphesSTDHEPReader
21 *
22 * Reads STDHEP file
23 *
24 * \author P. Demin - UCL, Louvain-la-Neuve
25 *
26 */
27
28#include "classes/DelphesSTDHEPReader.h"
29
30#include <stdexcept>
31#include <iostream>
32#include <sstream>
33
34#include <stdio.h>
35#include <errno.h>
36#include <rpc/types.h>
37#include <rpc/xdr.h>
38
39#include "TObjArray.h"
40#include "TStopwatch.h"
41#include "TDatabasePDG.h"
42#include "TParticlePDG.h"
43#include "TLorentzVector.h"
44
45#include "classes/DelphesClasses.h"
46#include "classes/DelphesFactory.h"
47
48#include "ExRootAnalysis/ExRootTreeBranch.h"
49
50using namespace std;
51
52static const int kBufferSize = 1000000;
53
54//---------------------------------------------------------------------------
55
56DelphesSTDHEPReader::DelphesSTDHEPReader() :
57 fInputFile(0), fInputXDR(0), fBuffer(0), fPDG(0), fBlockType(-1)
58{
59 fInputXDR = new XDR;
60 fBuffer = new char[kBufferSize*96 + 24];
61
62 fPDG = TDatabasePDG::Instance();
63}
64
65//---------------------------------------------------------------------------
66
67DelphesSTDHEPReader::~DelphesSTDHEPReader()
68{
69 if(fBuffer) delete fBuffer;
70 if(fInputXDR) delete fInputXDR;
71}
72
73//---------------------------------------------------------------------------
74
75void DelphesSTDHEPReader::SetInputFile(FILE *inputFile)
76{
77 fInputFile = inputFile;
78 xdrstdio_create(fInputXDR, inputFile, XDR_DECODE);
79 ReadFileHeader();
80}
81
82//---------------------------------------------------------------------------
83
84void DelphesSTDHEPReader::Clear()
85{
86 fBlockType = -1;
87}
88
89//---------------------------------------------------------------------------
90
91bool DelphesSTDHEPReader::EventReady()
92{
93 return (fBlockType == MCFIO_STDHEP) || (fBlockType == MCFIO_STDHEP4);
94}
95
96//---------------------------------------------------------------------------
97
98bool DelphesSTDHEPReader::ReadBlock(DelphesFactory *factory,
99 TObjArray *allParticleOutputArray,
100 TObjArray *stableParticleOutputArray,
101 TObjArray *partonOutputArray)
102{
103 if(feof(fInputFile)) return kFALSE;
104
105 xdr_int(fInputXDR, &fBlockType);
106
107 SkipBytes(4);
108
109 if(fBlockType == EVENTTABLE)
110 {
111 ReadEventTable();
112 }
113 else if(fBlockType == EVENTHEADER)
114 {
115 ReadEventHeader();
116 }
117 else if(fBlockType == MCFIO_STDHEPBEG ||
118 fBlockType == MCFIO_STDHEPEND)
119 {
120 ReadSTDCM1();
121 }
122 else if(fBlockType == MCFIO_STDHEP)
123 {
124 ReadSTDHEP();
125 AnalyzeParticles(factory, allParticleOutputArray,
126 stableParticleOutputArray, partonOutputArray);
127 }
128 else if(fBlockType == MCFIO_STDHEP4)
129 {
130 ReadSTDHEP();
131 AnalyzeParticles(factory, allParticleOutputArray,
132 stableParticleOutputArray, partonOutputArray);
133 ReadSTDHEP4();
134 }
135 else
136 {
137 throw runtime_error("Unsupported block type.");
138 }
139
140 return kTRUE;
141}
142
143//---------------------------------------------------------------------------
144
145void DelphesSTDHEPReader::SkipBytes(u_int size)
146{
147 int rc;
148 u_int rndup;
149
150 rndup = size % 4;
151 if(rndup > 0)
152 {
153 rndup = 4 - rndup;
154 }
155
156 rc = fseek(fInputFile, size + rndup, SEEK_CUR);
157
158 if(rc != 0 && errno == ESPIPE)
159 {
160 xdr_opaque(fInputXDR, fBuffer, size);
161 }
162}
163
164//---------------------------------------------------------------------------
165
166void DelphesSTDHEPReader::SkipArray(u_int elsize)
167{
168 u_int size;
169 xdr_u_int(fInputXDR, &size);
170 SkipBytes(size*elsize);
171}
172
173//---------------------------------------------------------------------------
174
175void DelphesSTDHEPReader::ReadFileHeader()
176{
177 u_int i;
178 enum STDHEPVersion {UNKNOWN, V1, V2, V21} version;
179
180 xdr_int(fInputXDR, &fBlockType);
181 if (fBlockType != FILEHEADER)
182 {
183 throw runtime_error("Header block not found. File is probably corrupted.");
184 }
185
186 SkipBytes(4);
187
188 // version
189 xdr_string(fInputXDR, &fBuffer, 100);
190 if(fBuffer[0] == '\0' || fBuffer[1] == '\0') version = UNKNOWN;
191 else if(fBuffer[0] == '1') version = V1;
192 else if(strncmp(fBuffer, "2.01", 4) == 0) version = V21;
193 else if(fBuffer[0] == '2') version = V2;
194 else version = UNKNOWN;
195
196 if(version == UNKNOWN)
197 {
198 throw runtime_error("Unknown file format version.");
199 }
200
201 SkipArray(1);
202 SkipArray(1);
203 SkipArray(1);
204
205 if(version == V21)
206 {
207 SkipArray(1);
208 }
209
210 // Expected number of events
211 SkipBytes(4);
212
213 // Number of events
214 xdr_u_int(fInputXDR, &fEntries);
215
216 SkipBytes(8);
217
218 // Number of blocks
219 u_int nBlocks = 0;
220 xdr_u_int(fInputXDR, &nBlocks);
221
222 // Number of NTuples
223 u_int nNTuples = 0;
224 if(version != V1)
225 {
226 xdr_u_int(fInputXDR, &nNTuples);
227 }
228
229 if(nNTuples != 0)
230 {
231 throw runtime_error("Files containing n-tuples are not supported.");
232 }
233
234 // Processing blocks extraction
235 if(nBlocks != 0)
236 {
237 SkipArray(4);
238
239 for(i = 0; i < nBlocks; i++)
240 {
241 SkipArray(1);
242 }
243 }
244}
245
246//---------------------------------------------------------------------------
247
248void DelphesSTDHEPReader::ReadEventTable()
249{
250 // version
251 xdr_string(fInputXDR, &fBuffer, 100);
252 if(strncmp(fBuffer, "1.00", 4) == 0)
253 {
254 SkipBytes(8);
255
256 SkipArray(4);
257 SkipArray(4);
258 SkipArray(4);
259 SkipArray(4);
260 SkipArray(4);
261 }
262 else if(strncmp(fBuffer, "2.00", 4) == 0)
263 {
264 SkipBytes(12);
265
266 SkipArray(4);
267 SkipArray(4);
268 SkipArray(4);
269 SkipArray(4);
270 SkipArray(8);
271 }
272}
273
274//---------------------------------------------------------------------------
275
276void DelphesSTDHEPReader::ReadEventHeader()
277{
278 bool skipNTuples = false;
279 u_int skipSize = 4;
280
281 // version
282 xdr_string(fInputXDR, &fBuffer, 100);
283 if(strncmp(fBuffer, "2.00", 4) == 0)
284 {
285 skipNTuples = true;
286 }
287 else if(strncmp(fBuffer, "3.00", 4) == 0)
288 {
289 skipNTuples = true;
290 skipSize = 8;
291 }
292
293 SkipBytes(20);
294
295 u_int dimBlocks = 0;
296 xdr_u_int(fInputXDR, &dimBlocks);
297
298 u_int dimNTuples = 0;
299 if(skipNTuples)
300 {
301 SkipBytes(4);
302 xdr_u_int(fInputXDR, &dimNTuples);
303 }
304
305 // Processing blocks extraction
306 if(dimBlocks > 0)
307 {
308 SkipArray(4);
309 SkipArray(skipSize);
310 }
311
312 // Processing blocks extraction
313 if(skipNTuples && dimNTuples > 0)
314 {
315 SkipArray(4);
316 SkipArray(skipSize);
317 }
318}
319
320//---------------------------------------------------------------------------
321
322void DelphesSTDHEPReader::ReadSTDCM1()
323{
324 // version
325 xdr_string(fInputXDR, &fBuffer, 100);
326
327 // skip 5*4 + 2*8 = 36 bytes
328 SkipBytes(36);
329
330 if((strncmp(fBuffer, "1.", 2) == 0) || (strncmp(fBuffer, "2.", 2) == 0) ||
331 (strncmp(fBuffer, "3.", 2) == 0) || (strncmp(fBuffer, "4.", 2) == 0) ||
332 (strncmp(fBuffer, "5.00", 4) == 0))
333 {
334 return;
335 }
336
337 SkipArray(1);
338 SkipArray(1);
339
340 if(strncmp(fBuffer, "5.01", 4) == 0)
341 {
342 return;
343 }
344
345 SkipBytes(4);
346}
347
348//---------------------------------------------------------------------------
349
350void DelphesSTDHEPReader::ReadSTDHEP()
351{
352 u_int idhepSize, isthepSize, jmohepSize, jdahepSize, phepSize, vhepSize;
353
354 // version
355 xdr_string(fInputXDR, &fBuffer, 100);
356
357 // Extracting the event number
358 xdr_int(fInputXDR, &fEventNumber);
359
360 // Extracting the number of particles
361 xdr_int(fInputXDR, &fEventSize);
362
363 if(fEventSize >= kBufferSize)
364 {
365 throw runtime_error("too many particles in event");
366 }
367
368 // 4*n + 4*n + 8*n + 8*n + 40*n + 32*n +
369 // 4 + 4 + 4 + 4 + 4 + 4 = 96*n + 24
370
371 xdr_opaque(fInputXDR, fBuffer, 96*fEventSize + 24);
372
373 idhepSize = ntohl(*(u_int*)(fBuffer));
374 isthepSize = ntohl(*(u_int*)(fBuffer + 4*1 + 4*1*fEventSize));
375 jmohepSize = ntohl(*(u_int*)(fBuffer + 4*2 + 4*2*fEventSize));
376 jdahepSize = ntohl(*(u_int*)(fBuffer + 4*3 + 4*4*fEventSize));
377 phepSize = ntohl(*(u_int*)(fBuffer + 4*4 + 4*6*fEventSize));
378 vhepSize = ntohl(*(u_int*)(fBuffer + 4*5 + 4*16*fEventSize));
379
380 if(fEventSize < 0 ||
381 fEventSize != (int)idhepSize || fEventSize != (int)isthepSize ||
382 (2*fEventSize) != (int)jmohepSize || (2*fEventSize) != (int)jdahepSize ||
383 (5*fEventSize) != (int)phepSize || (4*fEventSize) != (int)vhepSize)
384 {
385 throw runtime_error("Inconsistent size of arrays. File is probably corrupted.");
386 }
387
388 fWeight = 1.0;
389 fAlphaQED = 0.0;
390 fAlphaQCD = 0.0;
391 fScaleSize = 0;
392 memset(fScale, 0, 10*sizeof(double));
393}
394
395//---------------------------------------------------------------------------
396
397void DelphesSTDHEPReader::ReadSTDHEP4()
398{
399 u_int number;
400
401 // Extracting the event weight
402 xdr_double(fInputXDR, &fWeight);
403
404 // Extracting alpha QED
405 xdr_double(fInputXDR, &fAlphaQED);
406
407 // Extracting alpha QCD
408 xdr_double(fInputXDR, &fAlphaQCD);
409
410 // Extracting the event scale
411 xdr_u_int(fInputXDR, &fScaleSize);
412 for(number = 0; number < fScaleSize; ++number)
413 {
414 xdr_double(fInputXDR, &fScale[number]);
415 }
416
417 SkipArray(8);
418 SkipArray(4);
419
420 SkipBytes(4);
421}
422
423//---------------------------------------------------------------------------
424
425void DelphesSTDHEPReader::AnalyzeEvent(ExRootTreeBranch *branch, long long eventNumber,
426 TStopwatch *readStopWatch, TStopwatch *procStopWatch)
427{
428 LHEFEvent *element;
429
430 element = static_cast<LHEFEvent *>(branch->NewEntry());
431
432 element->Number = fEventNumber;
433
434 element->ProcessID = 0;
435
436 element->Weight = fWeight;
437 element->ScalePDF = fScale[0];
438 element->AlphaQED = fAlphaQED;
439 element->AlphaQCD = fAlphaQCD;
440
441 element->ReadTime = readStopWatch->RealTime();
442 element->ProcTime = procStopWatch->RealTime();
443}
444
445//---------------------------------------------------------------------------
446
447void DelphesSTDHEPReader::AnalyzeParticles(DelphesFactory *factory,
448 TObjArray *allParticleOutputArray,
449 TObjArray *stableParticleOutputArray,
450 TObjArray *partonOutputArray)
451{
452 Candidate *candidate;
453 TParticlePDG *pdgParticle;
454 int pdgCode;
455
456 int number;
457 int pid, status, m1, m2, d1, d2;
458 double px, py, pz, e, mass;
459 double x, y, z, t;
460
461 XDR bufferXDR[6];
462 xdrmem_create(&bufferXDR[0], fBuffer + 4*1, 4*fEventSize, XDR_DECODE);
463 xdrmem_create(&bufferXDR[1], fBuffer + 4*2 + 4*1*fEventSize, 4*fEventSize, XDR_DECODE);
464 xdrmem_create(&bufferXDR[2], fBuffer + 4*3 + 4*2*fEventSize, 8*fEventSize, XDR_DECODE);
465 xdrmem_create(&bufferXDR[3], fBuffer + 4*4 + 4*4*fEventSize, 8*fEventSize, XDR_DECODE);
466 xdrmem_create(&bufferXDR[4], fBuffer + 4*5 + 4*6*fEventSize, 40*fEventSize, XDR_DECODE);
467 xdrmem_create(&bufferXDR[5], fBuffer + 4*6 + 4*16*fEventSize, 32*fEventSize, XDR_DECODE);
468
469 for(number = 0; number < fEventSize; ++number)
470 {
471 xdr_int(&bufferXDR[0], &status);
472 xdr_int(&bufferXDR[1], &pid);
473 xdr_int(&bufferXDR[2], &m1);
474 xdr_int(&bufferXDR[2], &m2);
475 xdr_int(&bufferXDR[3], &d1);
476 xdr_int(&bufferXDR[3], &d2);
477
478 xdr_double(&bufferXDR[4], &px);
479 xdr_double(&bufferXDR[4], &py);
480 xdr_double(&bufferXDR[4], &pz);
481 xdr_double(&bufferXDR[4], &e);
482 xdr_double(&bufferXDR[4], &mass);
483
484 xdr_double(&bufferXDR[5], &x);
485 xdr_double(&bufferXDR[5], &y);
486 xdr_double(&bufferXDR[5], &z);
487 xdr_double(&bufferXDR[5], &t);
488
489 candidate = factory->NewCandidate();
490
491 candidate->PID = pid;
492 pdgCode = TMath::Abs(candidate->PID);
493
494 candidate->Status = status;
495
496 candidate->M1 = m1 - 1;
497 candidate->M2 = m2 - 1;
498
499 candidate->D1 = d1 - 1;
500 candidate->D2 = d2 - 1;
501
502 pdgParticle = fPDG->GetParticle(pid);
503 candidate->Charge = pdgParticle ? int(pdgParticle->Charge()/3.0) : -999;
504 candidate->Mass = mass;
505
506 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
507
508 candidate->Position.SetXYZT(x, y, z, t);
509
510 allParticleOutputArray->Add(candidate);
511
512 if(!pdgParticle) continue;
513
514 if(status == 1 && pdgParticle->Stable())
515 {
516 stableParticleOutputArray->Add(candidate);
517 }
518 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
519 {
520 partonOutputArray->Add(candidate);
521 }
522 }
523}
524
525//---------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.