Fork me on GitHub

source: git/classes/DelphesSTDHEPReader.cc@ c749957

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

add DelphesXDRReader and DelphesXDRWriter

  • Property mode set to 100644
File size: 11.7 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 <stdint.h>
37#include <string.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#include "classes/DelphesXDRReader.h"
48
49#include "ExRootAnalysis/ExRootTreeBranch.h"
50
51using namespace std;
52
53static const int kBufferSize = 1000000;
54
55//---------------------------------------------------------------------------
56
57DelphesSTDHEPReader::DelphesSTDHEPReader() :
58 fInputFile(0), fBuffer(0), fPDG(0), fBlockType(-1)
59{
60 fBuffer = new uint8_t[kBufferSize*96 + 24];
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;
77 fReader[0].SetFile(inputFile);
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{
101 fReader[0].ReadValue(&fBlockType, 4);
102
103 if(feof(fInputFile)) return kFALSE;
104
105 SkipBytes(4);
106
107 if(fBlockType == FILEHEADER)
108 {
109 ReadFileHeader();
110 }
111 else if(fBlockType == EVENTTABLE)
112 {
113 ReadEventTable();
114 }
115 else if(fBlockType == EVENTHEADER)
116 {
117 ReadEventHeader();
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 {
132 ReadSTDHEP();
133 AnalyzeParticles(factory, allParticleOutputArray,
134 stableParticleOutputArray, partonOutputArray);
135 ReadSTDHEP4();
136 }
137 else
138 {
139 throw runtime_error("Unsupported block type.");
140 }
141
142 return kTRUE;
143}
144
145//---------------------------------------------------------------------------
146
147void DelphesSTDHEPReader::SkipBytes(int size)
148{
149 int rc;
150 int rndup;
151
152 rndup = size % 4;
153 if(rndup > 0)
154 {
155 rndup = 4 - rndup;
156 }
157
158 rc = fseek(fInputFile, size + rndup, SEEK_CUR);
159
160 if(rc != 0 && errno == ESPIPE)
161 {
162 fReader[0].ReadRaw(fBuffer, size);
163 }
164}
165
166//---------------------------------------------------------------------------
167
168void DelphesSTDHEPReader::SkipArray(int elsize)
169{
170 uint32_t size;
171 fReader[0].ReadValue(&size, 4);
172 SkipBytes(size*elsize);
173}
174
175//---------------------------------------------------------------------------
176
177void DelphesSTDHEPReader::ReadFileHeader()
178{
179 uint32_t i;
180 enum STDHEPVersion {UNKNOWN, V1, V2, V21} version;
181
182 // version
183 fReader[0].ReadString(fBuffer, 100);
184 if(fBuffer[0] == '\0' || fBuffer[1] == '\0') version = UNKNOWN;
185 else if(fBuffer[0] == '1') version = V1;
186 else if(strncmp((char *)fBuffer, "2.01", 4) == 0) version = V21;
187 else if(fBuffer[0] == '2') version = V2;
188 else version = UNKNOWN;
189
190 if(version == UNKNOWN)
191 {
192 throw runtime_error("Unknown file format version.");
193 }
194
195 SkipArray(1);
196 SkipArray(1);
197 SkipArray(1);
198
199 if(version == V21)
200 {
201 SkipArray(1);
202 }
203
204 // Expected number of events
205 SkipBytes(4);
206
207 // Number of events
208 fReader[0].ReadValue(&fEntries, 4);
209
210 SkipBytes(8);
211
212 // Number of blocks
213 uint32_t nBlocks = 0;
214 fReader[0].ReadValue(&nBlocks, 4);
215
216 // Number of NTuples
217 uint32_t nNTuples = 0;
218 if(version != V1)
219 {
220 fReader[0].ReadValue(&nNTuples, 4);
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{
244 // version
245 fReader[0].ReadString(fBuffer, 100);
246 if(strncmp((char *)fBuffer, "1.00", 4) == 0)
247 {
248 SkipBytes(8);
249
250 SkipArray(4);
251 SkipArray(4);
252 SkipArray(4);
253 SkipArray(4);
254 SkipArray(4);
255 }
256 else if(strncmp((char *)fBuffer, "2.00", 4) == 0)
257 {
258 SkipBytes(12);
259
260 SkipArray(4);
261 SkipArray(4);
262 SkipArray(4);
263 SkipArray(4);
264 SkipArray(8);
265 }
266}
267
268//---------------------------------------------------------------------------
269
270void DelphesSTDHEPReader::ReadEventHeader()
271{
272 bool skipNTuples = false;
273 int skipSize = 4;
274
275 // version
276 fReader[0].ReadString(fBuffer, 100);
277 if(strncmp((char *)fBuffer, "2.00", 4) == 0)
278 {
279 skipNTuples = true;
280 }
281 else if(strncmp((char *)fBuffer, "3.00", 4) == 0)
282 {
283 skipNTuples = true;
284 skipSize = 8;
285 }
286
287 SkipBytes(20);
288
289 uint32_t dimBlocks = 0;
290 fReader[0].ReadValue(&dimBlocks, 4);
291
292 uint32_t dimNTuples = 0;
293 if(skipNTuples)
294 {
295 SkipBytes(4);
296 fReader[0].ReadValue(&dimNTuples, 4);
297 }
298
299 // Processing blocks extraction
300 if(dimBlocks > 0)
301 {
302 SkipArray(4);
303 SkipArray(skipSize);
304 }
305
306 // Processing blocks extraction
307 if(skipNTuples && dimNTuples > 0)
308 {
309 SkipArray(4);
310 SkipArray(skipSize);
311 }
312}
313
314//---------------------------------------------------------------------------
315
316void DelphesSTDHEPReader::ReadSTDCM1()
317{
318 // version
319 fReader[0].ReadString(fBuffer, 100);
320
321 // skip 5*4 + 2*8 = 36 bytes
322 SkipBytes(36);
323
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))
327 {
328 return;
329 }
330
331 SkipArray(1);
332 SkipArray(1);
333
334 if(strncmp((char *)fBuffer, "5.01", 4) == 0)
335 {
336 return;
337 }
338
339 SkipBytes(4);
340}
341
342//---------------------------------------------------------------------------
343
344void DelphesSTDHEPReader::ReadSTDHEP()
345{
346 uint32_t idhepSize, isthepSize, jmohepSize, jdahepSize, phepSize, vhepSize;
347
348 // version
349 fReader[0].ReadString(fBuffer, 100);
350
351 // Extracting the event number
352 fReader[0].ReadValue(&fEventNumber, 4);
353
354 // Extracting the number of particles
355 fReader[0].ReadValue(&fEventSize, 4);
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
365 fReader[0].ReadRaw(fBuffer, 96*fEventSize + 24);
366
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);
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{
400 uint32_t number;
401
402 // Extracting the event weight
403 fReader[0].ReadValue(&fWeight, 8);
404
405 // Extracting alpha QED
406 fReader[0].ReadValue(&fAlphaQED, 8);
407
408 // Extracting alpha QCD
409 fReader[0].ReadValue(&fAlphaQCD, 8);
410
411 // Extracting the event scale
412 fReader[0].ReadValue(&fScaleSize, 4);
413 for(number = 0; number < fScaleSize; ++number)
414 {
415 fReader[0].ReadValue(&fScale[number], 8);
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;
458 int32_t pid, status, m1, m2, d1, d2;
459 double px, py, pz, e, mass;
460 double x, y, z, t;
461
462 for(number = 0; number < fEventSize; ++number)
463 {
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);
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;
497 candidate->Mass = mass;
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
507 if(status == 1)
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.