Fork me on GitHub

source: git/classes/DelphesHepMC3Reader.cc@ b30245b

Last change on this file since b30245b was b4786d3, checked in by Pavel Demin <pavel.demin@…>, 3 years ago

add DelphesHepMC3

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/*
2 * Delphes: a framework for fast simulation of a generic collider experiment
3 * Copyright (C) 2012-2021 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 DelphesHepMC3Reader
20 *
21 * Reads HepMC file
22 *
23 * \author P. Demin - UCL, Louvain-la-Neuve
24 *
25 */
26
27#include "classes/DelphesHepMC3Reader.h"
28
29#include <iostream>
30#include <sstream>
31#include <stdexcept>
32
33#include <map>
34#include <vector>
35
36#include <stdio.h>
37
38#include "TDatabasePDG.h"
39#include "TLorentzVector.h"
40#include "TObjArray.h"
41#include "TParticlePDG.h"
42#include "TStopwatch.h"
43
44#include "classes/DelphesClasses.h"
45#include "classes/DelphesFactory.h"
46#include "classes/DelphesStream.h"
47
48#include "ExRootAnalysis/ExRootTreeBranch.h"
49
50using namespace std;
51
52static const int kBufferSize = 16384;
53
54//---------------------------------------------------------------------------
55
56DelphesHepMC3Reader::DelphesHepMC3Reader() :
57 fInputFile(0), fBuffer(0), fPDG(0),
58 fVertexCounter(-1), fParticleCounter(-1)
59{
60 fBuffer = new char[kBufferSize];
61
62 fPDG = TDatabasePDG::Instance();
63}
64
65//---------------------------------------------------------------------------
66
67DelphesHepMC3Reader::~DelphesHepMC3Reader()
68{
69 if(fBuffer) delete[] fBuffer;
70}
71
72//---------------------------------------------------------------------------
73
74void DelphesHepMC3Reader::SetInputFile(FILE *inputFile)
75{
76 fInputFile = inputFile;
77}
78
79//---------------------------------------------------------------------------
80
81void DelphesHepMC3Reader::Clear()
82{
83 fWeight.clear();
84 fMomentumCoefficient = 1.0;
85 fPositionCoefficient = 1.0;
86 fVertexCounter = -1;
87 fParticleCounter = -1;
88 fVertexMap.clear();
89 fDaughterMap.clear();
90}
91
92//---------------------------------------------------------------------------
93
94bool DelphesHepMC3Reader::EventReady()
95{
96 return (fParticleCounter == 0);
97}
98
99//---------------------------------------------------------------------------
100
101bool DelphesHepMC3Reader::ReadBlock(DelphesFactory *factory,
102 TObjArray *allParticleOutputArray,
103 TObjArray *stableParticleOutputArray,
104 TObjArray *partonOutputArray)
105{
106 map<int, pair<int, int> >::iterator itDaughterMap;
107 char key, momentumUnit[4], positionUnit[3];
108 int rc, code;
109 double weight;
110
111 if(!fgets(fBuffer, kBufferSize, fInputFile)) return kFALSE;
112
113 DelphesStream bufferStream(fBuffer + 1);
114
115 key = fBuffer[0];
116
117 if(key == 'E')
118 {
119 Clear();
120
121 fX = 0.0;
122 fY = 0.0;
123 fZ = 0.0;
124 fT = 0.0;
125
126 rc = bufferStream.ReadInt(fEventNumber)
127 && bufferStream.ReadInt(fVertexCounter)
128 && bufferStream.ReadInt(fParticleCounter);
129
130 if(!rc)
131 {
132 cerr << "** ERROR: "
133 << "invalid event format" << endl;
134 return kFALSE;
135 }
136 }
137 else if(key == 'U')
138 {
139 rc = sscanf(fBuffer + 1, "%3s %2s", momentumUnit, positionUnit);
140
141 if(rc != 2)
142 {
143 cerr << "** ERROR: "
144 << "invalid units format" << endl;
145 return kFALSE;
146 }
147
148 if(strncmp(momentumUnit, "GEV", 3) == 0)
149 {
150 fMomentumCoefficient = 1.0;
151 }
152 else if(strncmp(momentumUnit, "MEV", 3) == 0)
153 {
154 fMomentumCoefficient = 0.001;
155 }
156
157 if(strncmp(positionUnit, "MM", 3) == 0)
158 {
159 fPositionCoefficient = 1.0;
160 }
161 else if(strncmp(positionUnit, "CM", 3) == 0)
162 {
163 fPositionCoefficient = 10.0;
164 }
165 }
166 else if(key == 'W')
167 {
168 while(bufferStream.ReadDbl(weight))
169 {
170 fWeight.push_back(weight);
171 }
172 }
173 else if(key == 'A' && bufferStream.FindStr("mpi"))
174 {
175 rc = bufferStream.ReadInt(fMPI);
176
177 if(!rc)
178 {
179 cerr << "** ERROR: "
180 << "invalid MPI format" << endl;
181 return kFALSE;
182 }
183 }
184 else if(key == 'A' && bufferStream.FindStr("signal_process_id"))
185 {
186 rc = bufferStream.ReadInt(fProcessID);
187
188 if(!rc)
189 {
190 cerr << "** ERROR: "
191 << "invalid process ID format" << endl;
192 return kFALSE;
193 }
194 }
195 else if(key == 'A' && bufferStream.FindStr("event_scale"))
196 {
197 rc = bufferStream.ReadDbl(fScale);
198
199 if(!rc)
200 {
201 cerr << "** ERROR: "
202 << "invalid event scale format" << endl;
203 return kFALSE;
204 }
205 }
206 else if(key == 'A' && bufferStream.FindStr("alphaQCD"))
207 {
208 rc = bufferStream.ReadDbl(fAlphaQCD);
209
210 if(!rc)
211 {
212 cerr << "** ERROR: "
213 << "invalid alphaQCD format" << endl;
214 return kFALSE;
215 }
216 }
217 else if(key == 'A' && bufferStream.FindStr("alphaQED"))
218 {
219 rc = bufferStream.ReadDbl(fAlphaQED);
220
221 if(!rc)
222 {
223 cerr << "** ERROR: "
224 << "invalid alphaQED format" << endl;
225 return kFALSE;
226 }
227 }
228 else if(key == 'A' && bufferStream.FindStr("GenCrossSection"))
229 {
230 rc = bufferStream.ReadDbl(fCrossSection)
231 && bufferStream.ReadDbl(fCrossSectionError);
232
233 if(!rc)
234 {
235 cerr << "** ERROR: "
236 << "invalid cross section format" << endl;
237 return kFALSE;
238 }
239 }
240 else if(key == 'A' && bufferStream.FindStr("GenPdfInfo"))
241 {
242 rc = bufferStream.ReadInt(fID1)
243 && bufferStream.ReadInt(fID2)
244 && bufferStream.ReadDbl(fX1)
245 && bufferStream.ReadDbl(fX2)
246 && bufferStream.ReadDbl(fScalePDF)
247 && bufferStream.ReadDbl(fPDF1)
248 && bufferStream.ReadDbl(fPDF2);
249
250 if(!rc)
251 {
252 cerr << "** ERROR: "
253 << "invalid PDF format" << endl;
254 return kFALSE;
255 }
256 }
257 else if(key == 'V')
258 {
259 fX = 0.0;
260 fY = 0.0;
261 fZ = 0.0;
262 fT = 0.0;
263
264 fM1 = 0;
265 fM2 = 0;
266
267 rc = bufferStream.ReadInt(fVertexCode)
268 && bufferStream.ReadInt(fVertexStatus);
269
270 if(!rc)
271 {
272 cerr << "** ERROR: "
273 << "invalid vertex format" << endl;
274 return kFALSE;
275 }
276
277 rc = bufferStream.FindChr('[');
278
279 if(!rc)
280 {
281 cerr << "** ERROR: "
282 << "invalid vertex format" << endl;
283 return kFALSE;
284 }
285
286 while(bufferStream.ReadInt(code))
287 {
288 if(code < fM1 || fM1 == 0) fM1 = code;
289 if(code > fM2) fM2 = code;
290 fVertexMap[code] = fVertexCode;
291 }
292
293 if(fM1 == fM2) fM2 = 0;
294
295 if(bufferStream.FindChr('@'))
296 {
297 rc = bufferStream.ReadDbl(fX)
298 && bufferStream.ReadDbl(fY)
299 && bufferStream.ReadDbl(fZ)
300 && bufferStream.ReadDbl(fT);
301
302 if(!rc)
303 {
304 cerr << "** ERROR: "
305 << "invalid vertex format" << endl;
306 return kFALSE;
307 }
308 }
309 }
310 else if(key == 'P' && fParticleCounter > 0)
311 {
312 --fParticleCounter;
313
314 rc = bufferStream.ReadInt(fParticleCode)
315 && bufferStream.ReadInt(fOutVertexCode)
316 && bufferStream.ReadInt(fPID)
317 && bufferStream.ReadDbl(fPx)
318 && bufferStream.ReadDbl(fPy)
319 && bufferStream.ReadDbl(fPz)
320 && bufferStream.ReadDbl(fE)
321 && bufferStream.ReadDbl(fMass)
322 && bufferStream.ReadInt(fParticleStatus);
323
324 if(!rc)
325 {
326 cerr << "** ERROR: "
327 << "invalid particle format" << endl;
328 return kFALSE;
329 }
330
331 itDaughterMap = fDaughterMap.find(fOutVertexCode);
332 if(itDaughterMap == fDaughterMap.end())
333 {
334 fDaughterMap[fOutVertexCode] = make_pair(fParticleCode, fParticleCode);
335 }
336 else
337 {
338 itDaughterMap->second.second = fParticleCode;
339 }
340
341 AnalyzeParticle(factory, allParticleOutputArray,
342 stableParticleOutputArray, partonOutputArray);
343 }
344
345 if(EventReady())
346 {
347 FinalizeParticles(allParticleOutputArray);
348 }
349
350 return kTRUE;
351}
352
353//---------------------------------------------------------------------------
354
355void DelphesHepMC3Reader::AnalyzeEvent(ExRootTreeBranch *branch, long long eventNumber,
356 TStopwatch *readStopWatch, TStopwatch *procStopWatch)
357{
358 HepMCEvent *element;
359
360 element = static_cast<HepMCEvent *>(branch->NewEntry());
361 element->Number = fEventNumber;
362
363 element->ProcessID = fProcessID;
364 element->MPI = fMPI;
365 element->Weight = fWeight.size() > 0 ? fWeight[0] : 1.0;
366 element->CrossSection = fCrossSection;
367 element->CrossSectionError = fCrossSectionError;
368 element->Scale = fScale;
369 element->AlphaQED = fAlphaQED;
370 element->AlphaQCD = fAlphaQCD;
371
372 element->ID1 = fID1;
373 element->ID2 = fID2;
374 element->X1 = fX1;
375 element->X2 = fX2;
376 element->ScalePDF = fScalePDF;
377 element->PDF1 = fPDF1;
378 element->PDF2 = fPDF2;
379
380 element->ReadTime = readStopWatch->RealTime();
381 element->ProcTime = procStopWatch->RealTime();
382}
383
384//---------------------------------------------------------------------------
385
386void DelphesHepMC3Reader::AnalyzeWeight(ExRootTreeBranch *branch)
387{
388 Weight *element;
389 vector<double>::const_iterator itWeight;
390
391 for(itWeight = fWeight.begin(); itWeight != fWeight.end(); ++itWeight)
392 {
393 element = static_cast<Weight *>(branch->NewEntry());
394
395 element->Weight = *itWeight;
396 }
397}
398
399//---------------------------------------------------------------------------
400
401void DelphesHepMC3Reader::AnalyzeParticle(DelphesFactory *factory,
402 TObjArray *allParticleOutputArray,
403 TObjArray *stableParticleOutputArray,
404 TObjArray *partonOutputArray)
405{
406 Candidate *candidate;
407 TParticlePDG *pdgParticle;
408 int pdgCode;
409
410 candidate = factory->NewCandidate();
411
412 candidate->PID = fPID;
413 pdgCode = TMath::Abs(candidate->PID);
414
415 candidate->Status = fParticleStatus;
416
417 pdgParticle = fPDG->GetParticle(fPID);
418 candidate->Charge = pdgParticle ? int(pdgParticle->Charge() / 3.0) : -999;
419 candidate->Mass = fMass;
420
421 candidate->Momentum.SetPxPyPzE(fPx, fPy, fPz, fE);
422 if(fMomentumCoefficient != 1.0)
423 {
424 candidate->Momentum *= fMomentumCoefficient;
425 }
426
427 candidate->Position.SetXYZT(fX, fY, fZ, fT);
428 if(fPositionCoefficient != 1.0)
429 {
430 candidate->Position *= fPositionCoefficient;
431 }
432
433 candidate->D1 = -1;
434 candidate->D2 = -1;
435
436 if(fOutVertexCode < 0)
437 {
438 candidate->M1 = fM1 - 1;
439 candidate->M2 = fM2 - 1;
440 }
441 else
442 {
443 candidate->M1 = fOutVertexCode - 1;
444 candidate->M2 = -1;
445 }
446
447 allParticleOutputArray->Add(candidate);
448
449 if(!pdgParticle) return;
450
451 if(fParticleStatus == 1)
452 {
453 stableParticleOutputArray->Add(candidate);
454 }
455 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
456 {
457 partonOutputArray->Add(candidate);
458 }
459}
460
461//---------------------------------------------------------------------------
462
463void DelphesHepMC3Reader::FinalizeParticles(TObjArray *allParticleOutputArray)
464{
465 Candidate *candidate;
466 map<int, int >::iterator itVertexMap;
467 map<int, pair<int, int> >::iterator itDaughterMap;
468 int i, index;
469
470 for(i = 0; i < allParticleOutputArray->GetEntriesFast(); ++i)
471 {
472 candidate = static_cast<Candidate *>(allParticleOutputArray->At(i));
473
474 index = i + 1;
475
476 itVertexMap = fVertexMap.find(index);
477 if(itVertexMap != fVertexMap.end())
478 {
479 index = itVertexMap->second;
480 }
481
482 itDaughterMap = fDaughterMap.find(index);
483 if(itDaughterMap == fDaughterMap.end())
484 {
485 candidate->D1 = -1;
486 candidate->D2 = -1;
487 }
488 else
489 {
490 candidate->D1 = itDaughterMap->second.first - 1;
491 candidate->D2 = itDaughterMap->second.second - 1;
492 }
493 }
494}
495
496//---------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.