Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HepMC3Example.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HepMC3Example.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018-2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
16 
17 #include <fstream>
18 
19 #include <HepMC3/GenEvent.h>
20 #include <HepMC3/ReaderAscii.h>
21 
25 int main(int argc, char** argv) {
26  // Declare the supported program options.
27  // Setup and parse options
30  auto vm = ActsExamples::Options::parse(desc, argc, argv);
31  if (vm.empty()) {
32  return EXIT_FAILURE;
33  }
34 
36 
37  // Create the reader
38  auto hepMC3ReaderConfig = ActsExamples::Options::readHepMC3ReaderOptions(vm);
39  ActsExamples::HepMC3AsciiReader simReader(hepMC3ReaderConfig, logLevel);
40 
41  std::cout << "Preparing reader " << std::flush;
42  HepMC3::ReaderAscii reader("test.hepmc3");
43  if (simReader.status(reader)) {
44  std::cout << "successful" << std::endl;
45  } else {
46  std::cout << "failed" << std::endl;
47  }
48 
49  HepMC3::GenEvent genevt;
50 
51  std::cout << "Reading event " << std::flush;
52  if (simReader.readEvent(reader, genevt)) {
53  std::cout << "successful" << std::endl;
54  } else {
55  std::cout << "failed" << std::endl;
56  }
57  std::cout << std::endl;
58 
59  using namespace ActsExamples::HepMC3Event;
60  std::cout << "Event data:" << std::endl;
61  std::cout << "Units: ";
62  if (momentumUnit(genevt) == Acts::UnitConstants::GeV) {
63  std::cout << "[GEV], ";
64  } else if (momentumUnit(genevt) == Acts::UnitConstants::MeV) {
65  std::cout << "[MeV], ";
66  }
67  if (lengthUnit(genevt) == Acts::UnitConstants::mm) {
68  std::cout << "[mm]" << std::endl;
69  } else if (lengthUnit(genevt) == Acts::UnitConstants::cm) {
70  std::cout << "[cm]" << std::endl;
71  }
72  Acts::Vector3 evtPos = eventPos(genevt);
73  std::cout << "Event position: " << evtPos(0) << ", " << evtPos(1) << ", "
74  << evtPos(2) << std::endl;
75  std::cout << "Event time: " << eventTime(genevt) << std::endl;
76 
77  std::cout << "Beam particles: ";
78  std::vector<ActsExamples::SimParticle> beam = beams(genevt);
79  if (beam.empty()) {
80  std::cout << "none" << std::endl;
81  } else {
82  for (auto& pbeam : beam) {
83  std::cout << Acts::findName(static_cast<Acts::PdgParticle>(pbeam.pdg()))
84  .value_or("invalid")
85  << " ";
86  }
87  std::cout << std::endl;
88  }
89 
90  std::cout << std::endl << "Vertices: ";
91  std::vector<std::unique_ptr<ActsExamples::SimVertex>> vertices =
93  if (vertices.empty()) {
94  std::cout << "none" << std::endl;
95  } else {
96  std::cout << std::endl;
97  for (auto& vertex : vertices) {
98  for (auto& particle : vertex->incoming) {
99  std::cout << Acts::findName(
100  static_cast<Acts::PdgParticle>(particle.pdg()))
101  .value_or("invalid")
102  << " ";
103  }
104  std::cout << "-> ";
105  for (auto& particle : vertex->outgoing) {
106  std::cout << Acts::findName(
107  static_cast<Acts::PdgParticle>(particle.pdg()))
108  .value_or("invalid")
109  << " ";
110  }
111  std::cout << "\t@(" << vertex->time() << ", " << vertex->position()(0)
112  << ", " << vertex->position()(1) << ", "
113  << vertex->position()(2) << ")" << std::endl;
114  }
115  std::cout << std::endl;
116  }
117 
118  std::cout << "Total particle record:" << std::endl;
119  std::vector<ActsExamples::SimParticle> particles =
121  for (auto& particle : particles) {
122  std::cout << Acts::findName(static_cast<Acts::PdgParticle>(particle.pdg()))
123  .value_or("invalid")
124  << "\tID:" << particle.particleId() << ", momentum: ("
125  << particle.fourMomentum()(0) << ", "
126  << particle.fourMomentum()(1) << ", "
127  << particle.fourMomentum()(2) << "), mass: " << particle.mass()
128  << std::endl;
129  }
130 
131  std::cout << std::endl << "Initial to final state: ";
132  std::vector<ActsExamples::SimParticle> fState = finalState(genevt);
133  for (auto& pbeam : beam) {
134  std::cout << Acts::findName(static_cast<Acts::PdgParticle>(pbeam.pdg()))
135  .value_or("invalid")
136  << " ";
137  }
138  std::cout << "-> ";
139  for (auto& fs : fState) {
140  std::cout << Acts::findName(static_cast<Acts::PdgParticle>(fs.pdg()))
141  .value_or("invalid")
142  << " ";
143  }
144  std::cout << std::endl;
145 }