Fork me on GitHub

source: git/classes/DelphesSTDHEPReader.cc@ 3b3d0ad

ImprovedOutputFile Timing llp
Last change on this file since 3b3d0ad was 1716933, checked in by Pavel Demin <pavel-demin@…>, 7 years ago

add DelphesXDRReader and DelphesXDRWriter

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