Fork me on GitHub

source: git/classes/DelphesHepMC3Reader.cc@ dfdad8b

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

add DelphesHepMC3

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[b443089]1/*
2 * Delphes: a framework for fast simulation of a generic collider experiment
[b4786d3]3 * Copyright (C) 2012-2021 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
[b4786d3]19/** \class DelphesHepMC3Reader
[d7d2da3]20 *
21 * Reads HepMC file
22 *
23 * \author P. Demin - UCL, Louvain-la-Neuve
24 *
25 */
26
[b4786d3]27#include "classes/DelphesHepMC3Reader.h"
[d7d2da3]28
29#include <iostream>
30#include <sstream>
[341014c]31#include <stdexcept>
[d7d2da3]32
33#include <map>
[59abd43]34#include <vector>
[d7d2da3]35
36#include <stdio.h>
37
38#include "TDatabasePDG.h"
39#include "TLorentzVector.h"
[341014c]40#include "TObjArray.h"
41#include "TParticlePDG.h"
42#include "TStopwatch.h"
[d7d2da3]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
[c6667c0]52static const int kBufferSize = 16384;
[d7d2da3]53
54//---------------------------------------------------------------------------
55
[b4786d3]56DelphesHepMC3Reader::DelphesHepMC3Reader() :
[d7d2da3]57 fInputFile(0), fBuffer(0), fPDG(0),
[b4786d3]58 fVertexCounter(-1), fParticleCounter(-1)
[d7d2da3]59{
60 fBuffer = new char[kBufferSize];
61
62 fPDG = TDatabasePDG::Instance();
63}
64
65//---------------------------------------------------------------------------
66
[b4786d3]67DelphesHepMC3Reader::~DelphesHepMC3Reader()
[d7d2da3]68{
69 if(fBuffer) delete[] fBuffer;
70}
71
72//---------------------------------------------------------------------------
73
[b4786d3]74void DelphesHepMC3Reader::SetInputFile(FILE *inputFile)
[d7d2da3]75{
76 fInputFile = inputFile;
77}
78
79//---------------------------------------------------------------------------
80
[b4786d3]81void DelphesHepMC3Reader::Clear()
[d7d2da3]82{
[59abd43]83 fWeight.clear();
[cf22deb]84 fMomentumCoefficient = 1.0;
85 fPositionCoefficient = 1.0;
[d7d2da3]86 fVertexCounter = -1;
[b4786d3]87 fParticleCounter = -1;
88 fVertexMap.clear();
[d7d2da3]89 fDaughterMap.clear();
90}
91
92//---------------------------------------------------------------------------
93
[b4786d3]94bool DelphesHepMC3Reader::EventReady()
[d7d2da3]95{
[b4786d3]96 return (fParticleCounter == 0);
[d7d2da3]97}
98
99//---------------------------------------------------------------------------
100
[b4786d3]101bool DelphesHepMC3Reader::ReadBlock(DelphesFactory *factory,
[d7d2da3]102 TObjArray *allParticleOutputArray,
103 TObjArray *stableParticleOutputArray,
104 TObjArray *partonOutputArray)
105{
[77e9ae1]106 map<int, pair<int, int> >::iterator itDaughterMap;
[cf22deb]107 char key, momentumUnit[4], positionUnit[3];
[b4786d3]108 int rc, code;
[59abd43]109 double weight;
[d7d2da3]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
[b4786d3]121 fX = 0.0;
122 fY = 0.0;
123 fZ = 0.0;
124 fT = 0.0;
125
[d7d2da3]126 rc = bufferStream.ReadInt(fEventNumber)
[59abd43]127 && bufferStream.ReadInt(fVertexCounter)
[b4786d3]128 && bufferStream.ReadInt(fParticleCounter);
[59abd43]129
130 if(!rc)
131 {
[341014c]132 cerr << "** ERROR: "
133 << "invalid event format" << endl;
[59abd43]134 return kFALSE;
135 }
[b4786d3]136 }
137 else if(key == 'U')
138 {
139 rc = sscanf(fBuffer + 1, "%3s %2s", momentumUnit, positionUnit);
[59abd43]140
[b4786d3]141 if(rc != 2)
[59abd43]142 {
[b4786d3]143 cerr << "** ERROR: "
144 << "invalid units format" << endl;
145 return kFALSE;
[59abd43]146 }
147
[b4786d3]148 if(strncmp(momentumUnit, "GEV", 3) == 0)
[59abd43]149 {
[b4786d3]150 fMomentumCoefficient = 1.0;
151 }
152 else if(strncmp(momentumUnit, "MEV", 3) == 0)
153 {
154 fMomentumCoefficient = 0.001;
[59abd43]155 }
156
[b4786d3]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))
[59abd43]169 {
170 fWeight.push_back(weight);
171 }
[b4786d3]172 }
173 else if(key == 'A' && bufferStream.FindStr("mpi"))
174 {
175 rc = bufferStream.ReadInt(fMPI);
[d7d2da3]176
177 if(!rc)
178 {
[341014c]179 cerr << "** ERROR: "
[b4786d3]180 << "invalid MPI format" << endl;
[d7d2da3]181 return kFALSE;
182 }
183 }
[b4786d3]184 else if(key == 'A' && bufferStream.FindStr("signal_process_id"))
[cf22deb]185 {
[b4786d3]186 rc = bufferStream.ReadInt(fProcessID);
[cf22deb]187
[b4786d3]188 if(!rc)
[cf22deb]189 {
[341014c]190 cerr << "** ERROR: "
[b4786d3]191 << "invalid process ID format" << endl;
[cf22deb]192 return kFALSE;
193 }
[b4786d3]194 }
195 else if(key == 'A' && bufferStream.FindStr("event_scale"))
196 {
197 rc = bufferStream.ReadDbl(fScale);
[cf22deb]198
[b4786d3]199 if(!rc)
[cf22deb]200 {
[b4786d3]201 cerr << "** ERROR: "
202 << "invalid event scale format" << endl;
203 return kFALSE;
[cf22deb]204 }
[b4786d3]205 }
206 else if(key == 'A' && bufferStream.FindStr("alphaQCD"))
207 {
208 rc = bufferStream.ReadDbl(fAlphaQCD);
[341014c]209
[b4786d3]210 if(!rc)
[cf22deb]211 {
[b4786d3]212 cerr << "** ERROR: "
213 << "invalid alphaQCD format" << endl;
214 return kFALSE;
[cf22deb]215 }
[b4786d3]216 }
217 else if(key == 'A' && bufferStream.FindStr("alphaQED"))
218 {
219 rc = bufferStream.ReadDbl(fAlphaQED);
220
221 if(!rc)
[cf22deb]222 {
[b4786d3]223 cerr << "** ERROR: "
224 << "invalid alphaQED format" << endl;
225 return kFALSE;
[cf22deb]226 }
227 }
[b4786d3]228 else if(key == 'A' && bufferStream.FindStr("GenCrossSection"))
[edeb0f0]229 {
230 rc = bufferStream.ReadDbl(fCrossSection)
231 && bufferStream.ReadDbl(fCrossSectionError);
[341014c]232
[b4786d3]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"))
[d7d2da3]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 {
[341014c]252 cerr << "** ERROR: "
253 << "invalid PDF format" << endl;
[d7d2da3]254 return kFALSE;
255 }
256 }
[b4786d3]257 else if(key == 'V')
[d7d2da3]258 {
[b4786d3]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('[');
[d7d2da3]278
279 if(!rc)
280 {
[341014c]281 cerr << "** ERROR: "
282 << "invalid vertex format" << endl;
[d7d2da3]283 return kFALSE;
284 }
[b4786d3]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 }
[d7d2da3]309 }
[b4786d3]310 else if(key == 'P' && fParticleCounter > 0)
[d7d2da3]311 {
[b4786d3]312 --fParticleCounter;
313
[d7d2da3]314 rc = bufferStream.ReadInt(fParticleCode)
[b4786d3]315 && bufferStream.ReadInt(fOutVertexCode)
[d7d2da3]316 && bufferStream.ReadInt(fPID)
317 && bufferStream.ReadDbl(fPx)
318 && bufferStream.ReadDbl(fPy)
319 && bufferStream.ReadDbl(fPz)
320 && bufferStream.ReadDbl(fE)
321 && bufferStream.ReadDbl(fMass)
[b4786d3]322 && bufferStream.ReadInt(fParticleStatus);
[d7d2da3]323
324 if(!rc)
325 {
[341014c]326 cerr << "** ERROR: "
327 << "invalid particle format" << endl;
[d7d2da3]328 return kFALSE;
329 }
330
[b4786d3]331 itDaughterMap = fDaughterMap.find(fOutVertexCode);
332 if(itDaughterMap == fDaughterMap.end())
[d7d2da3]333 {
[b4786d3]334 fDaughterMap[fOutVertexCode] = make_pair(fParticleCode, fParticleCode);
[d7d2da3]335 }
[b4786d3]336 else
[d7d2da3]337 {
[b4786d3]338 itDaughterMap->second.second = fParticleCode;
[d7d2da3]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
[b4786d3]355void DelphesHepMC3Reader::AnalyzeEvent(ExRootTreeBranch *branch, long long eventNumber,
[d7d2da3]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;
[59abd43]365 element->Weight = fWeight.size() > 0 ? fWeight[0] : 1.0;
[edeb0f0]366 element->CrossSection = fCrossSection;
367 element->CrossSectionError = fCrossSectionError;
[d7d2da3]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
[b4786d3]386void DelphesHepMC3Reader::AnalyzeWeight(ExRootTreeBranch *branch)
[d203a37]387{
388 Weight *element;
[341014c]389 vector<double>::const_iterator itWeight;
[d203a37]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
[b4786d3]401void DelphesHepMC3Reader::AnalyzeParticle(DelphesFactory *factory,
[d7d2da3]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
[b4786d3]415 candidate->Status = fParticleStatus;
[d7d2da3]416
417 pdgParticle = fPDG->GetParticle(fPID);
[341014c]418 candidate->Charge = pdgParticle ? int(pdgParticle->Charge() / 3.0) : -999;
[80d4a34]419 candidate->Mass = fMass;
[d7d2da3]420
421 candidate->Momentum.SetPxPyPzE(fPx, fPy, fPz, fE);
[cf22deb]422 if(fMomentumCoefficient != 1.0)
423 {
424 candidate->Momentum *= fMomentumCoefficient;
425 }
[d7d2da3]426
[b4786d3]427 candidate->Position.SetXYZT(fX, fY, fZ, fT);
428 if(fPositionCoefficient != 1.0)
[d7d2da3]429 {
[b4786d3]430 candidate->Position *= fPositionCoefficient;
[d7d2da3]431 }
[b4786d3]432
433 candidate->D1 = -1;
434 candidate->D2 = -1;
435
436 if(fOutVertexCode < 0)
[d7d2da3]437 {
[b4786d3]438 candidate->M1 = fM1 - 1;
439 candidate->M2 = fM2 - 1;
[d7d2da3]440 }
441 else
442 {
[b4786d3]443 candidate->M1 = fOutVertexCode - 1;
444 candidate->M2 = -1;
[d7d2da3]445 }
446
447 allParticleOutputArray->Add(candidate);
448
449 if(!pdgParticle) return;
450
[b4786d3]451 if(fParticleStatus == 1)
[d7d2da3]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
[b4786d3]463void DelphesHepMC3Reader::FinalizeParticles(TObjArray *allParticleOutputArray)
[d7d2da3]464{
465 Candidate *candidate;
[b4786d3]466 map<int, int >::iterator itVertexMap;
[77e9ae1]467 map<int, pair<int, int> >::iterator itDaughterMap;
[b4786d3]468 int i, index;
[d7d2da3]469
470 for(i = 0; i < allParticleOutputArray->GetEntriesFast(); ++i)
471 {
472 candidate = static_cast<Candidate *>(allParticleOutputArray->At(i));
473
[b4786d3]474 index = i + 1;
475
476 itVertexMap = fVertexMap.find(index);
477 if(itVertexMap != fVertexMap.end())
[d7d2da3]478 {
[b4786d3]479 index = itVertexMap->second;
[d7d2da3]480 }
[b4786d3]481
482 itDaughterMap = fDaughterMap.find(index);
483 if(itDaughterMap == fDaughterMap.end())
[d7d2da3]484 {
485 candidate->D1 = -1;
486 candidate->D2 = -1;
487 }
488 else
489 {
[b4786d3]490 candidate->D1 = itDaughterMap->second.first - 1;
491 candidate->D2 = itDaughterMap->second.second - 1;
[d7d2da3]492 }
493 }
494}
495
496//---------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.