Project
Loading...
Searching...
No Matches
o2sim_hepmc_publisher.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "../Framework/Core/src/ArrowSupport.h"
14#include "Monitoring/Monitoring.h"
18
19#include "HepMC3/GenEvent.h"
20#include "HepMC3/GenParticle.h"
21#include "HepMC3/GenVertex.h"
22#include "HepMC3/ReaderAscii.h"
23#include "HepMC3/ReaderAsciiHepMC2.h"
24
26
27using namespace o2::framework;
28using namespace o2::dataformats;
29
31 Configurable<std::string> hepmcFileName{"hepmc", "input.hepmc", "name of the input file with HepMC events"};
32 Configurable<int> aggregate{"aggregate-timeframe", 300, "Number of events to put in a timeframe"};
33 Configurable<int> maxEvents{"nevents", -1, "Maximum number of events to convert"};
34 Configurable<bool> hepmcv2{"v2", false, "If the input is HepMCv2"};
35
36 int eventCounter = 0;
37 int tfCounter = 0;
38 std::shared_ptr<HepMC3::Reader> hepMCReader;
39 bool eos = false;
40 std::vector<o2::MCTrack> mcTracks;
41
43 {
44 if (hepmcv2) {
45 hepMCReader = std::make_shared<HepMC3::ReaderAsciiHepMC2>((std::string)hepmcFileName);
46 } else {
47 hepMCReader = std::make_shared<HepMC3::ReaderAscii>((std::string)hepmcFileName);
48 }
49 if (hepMCReader->failed()) {
50 LOGP(fatal, "Cannot open HEPMC kine file {}", (std::string)hepmcFileName);
51 }
52 // allocate the memory upfront to prevent reallocations later
53 mcTracks.reserve(1e3 * aggregate);
54 }
55
57 {
58 HepMC3::GenEvent event;
59 for (auto i = 0; i < (int)aggregate; ++i) {
60 // read next entry
61 hepMCReader->read_event(event);
62 if (hepMCReader->failed()) {
63 LOGP(warn, "Failed to read from HEPMC input file");
64 eos = true;
65 break;
66 }
67
68 // create O2 MCHeader and MCtracks vector out of HEPMC event
70 mcHeader.SetEventID(event.event_number());
71 mcHeader.SetVertex(event.event_pos().px(), event.event_pos().py(), event.event_pos().pz());
72 auto xsecInfo = event.cross_section();
73 if (xsecInfo != nullptr) {
74 mcHeader.putInfo(MCInfoKeys::acceptedEvents, (uint64_t)xsecInfo->get_accepted_events());
75 mcHeader.putInfo(MCInfoKeys::attemptedEvents, (uint64_t)xsecInfo->get_attempted_events());
76 mcHeader.putInfo(MCInfoKeys::xSection, (float)xsecInfo->xsec());
77 mcHeader.putInfo(MCInfoKeys::xSectionError, (float)xsecInfo->xsec_err());
78 }
79 auto scale = event.attribute<HepMC3::DoubleAttribute>(MCInfoKeys::eventScale);
80 if (scale != nullptr) {
81 mcHeader.putInfo(MCInfoKeys::eventScale, (float)scale->value());
82 }
83 auto nMPI = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::mpi);
84 if (nMPI != nullptr) {
85 mcHeader.putInfo(MCInfoKeys::mpi, nMPI->value());
86 }
87 auto sid = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::processCode);
88 auto scode = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::processID); // default pythia8 hepmc3 interface uses signal_process_id
89 if (sid != nullptr) {
90 mcHeader.putInfo(MCInfoKeys::processCode, sid->value());
91 } else if (scode != nullptr) {
92 mcHeader.putInfo(MCInfoKeys::processCode, scode->value());
93 }
94 auto pdfInfo = event.pdf_info();
95 if (pdfInfo != nullptr) {
96 mcHeader.putInfo(MCInfoKeys::pdfParton1Id, pdfInfo->parton_id[0]);
97 mcHeader.putInfo(MCInfoKeys::pdfParton2Id, pdfInfo->parton_id[1]);
98 mcHeader.putInfo(MCInfoKeys::pdfCode1, pdfInfo->pdf_id[0]);
99 mcHeader.putInfo(MCInfoKeys::pdfCode2, pdfInfo->pdf_id[1]);
100 mcHeader.putInfo(MCInfoKeys::pdfX1, (float)pdfInfo->x[0]);
101 mcHeader.putInfo(MCInfoKeys::pdfX2, (float)pdfInfo->x[1]);
102 mcHeader.putInfo(MCInfoKeys::pdfScale, (float)pdfInfo->scale);
103 mcHeader.putInfo(MCInfoKeys::pdfXF1, (float)pdfInfo->xf[0]);
104 mcHeader.putInfo(MCInfoKeys::pdfXF2, (float)pdfInfo->xf[1]);
105 }
106 auto heavyIon = event.heavy_ion();
107 if (heavyIon != nullptr) {
108 mcHeader.putInfo(MCInfoKeys::nCollHard, heavyIon->Ncoll_hard);
109 mcHeader.putInfo(MCInfoKeys::nPartProjectile, heavyIon->Npart_proj);
110 mcHeader.putInfo(MCInfoKeys::nPartTarget, heavyIon->Npart_targ);
111 mcHeader.putInfo(MCInfoKeys::nColl, heavyIon->Ncoll);
112 mcHeader.putInfo(MCInfoKeys::nCollNNWounded, heavyIon->N_Nwounded_collisions);
113 mcHeader.putInfo(MCInfoKeys::nCollNWoundedN, heavyIon->Nwounded_N_collisions);
114 mcHeader.putInfo(MCInfoKeys::nCollNWoundedNwounded, heavyIon->Nwounded_Nwounded_collisions);
115 mcHeader.putInfo(MCInfoKeys::nSpecProjectileNeutron, heavyIon->Nspec_proj_n);
116 mcHeader.putInfo(MCInfoKeys::nSpecProjectileProton, heavyIon->Nspec_proj_p);
117 mcHeader.putInfo(MCInfoKeys::nSpecTargetNeutron, heavyIon->Nspec_targ_n);
118 mcHeader.putInfo(MCInfoKeys::nSpecTargetProton, heavyIon->Nspec_targ_p);
119 mcHeader.putInfo(MCInfoKeys::impactParameter, (float)heavyIon->impact_parameter);
120 mcHeader.putInfo(MCInfoKeys::planeAngle, (float)heavyIon->event_plane_angle);
121 mcHeader.putInfo("eccentricity", (float)heavyIon->eccentricity);
122 mcHeader.putInfo(MCInfoKeys::sigmaInelNN, (float)heavyIon->sigma_inel_NN);
123 mcHeader.putInfo(MCInfoKeys::centrality, (float)heavyIon->centrality);
124 }
125
126 auto particles = event.particles();
127 for (auto const& particle : particles) {
128 auto parents = particle->parents();
129 auto has_parents = parents.size() > 0;
130 auto children = particle->children();
131 auto has_children = children.size() > 0;
132 auto p = particle->momentum();
133 auto v = particle->production_vertex();
134 mcTracks.emplace_back(
135 particle->pid(),
136 has_parents ? parents.front()->id() : -1, has_parents ? parents.back()->id() : -1,
137 has_children ? children.front()->id() : -1, has_children ? children.back()->id() : -1,
138 p.px(), p.py(), p.pz(),
139 v->position().x(), v->position().y(), v->position().z(),
140 v->position().t(), 0);
141 }
142
143 // add to the message
144 pc.outputs().snapshot(Output{"MC", "MCHEADER", 0}, mcHeader);
145 pc.outputs().snapshot(Output{"MC", "MCTRACKS", 0}, mcTracks);
146 mcTracks.clear();
147 ++eventCounter;
148 }
149
150 // report number of TFs injected for the rate limiter to work
151 ++tfCounter;
152 pc.services().get<o2::monitoring::Monitoring>().send(o2::monitoring::Metric{(uint64_t)tfCounter, "df-sent"}.addTag(o2::monitoring::tags::Key::Subsystem, o2::monitoring::tags::Value::DPL));
153 if (eos || (maxEvents > 0 && eventCounter == maxEvents)) {
154 pc.services().get<ControlService>().endOfStream();
155 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
156 }
157 }
158};
159
161{
162 auto spec = adaptAnalysisTask<O2simHepmcPublisher>(cfgc);
163 spec.outputs.emplace_back("MC", "MCHEADER", 0, Lifetime::Timeframe);
164 spec.outputs.emplace_back("MC", "MCTRACKS", 0, Lifetime::Timeframe);
165 spec.requiredServices.push_back(o2::framework::ArrowSupport::arrowBackendSpec());
166 spec.algorithm = CommonDataProcessors::wrapWithRateLimiting(spec.algorithm);
167 return {spec};
168}
int32_t i
Definition of the MCTrack class.
void putInfo(std::string const &key, T const &value)
void snapshot(const Output &spec, T const &object)
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
ServiceRegistryRef services()
The services registry associated with this processing context.
struct _cl_event * event
Definition glcorearb.h:2982
const GLdouble * v
Definition glcorearb.h:832
Definition of a container to keep/associate and arbitrary number of labels associated to an index wit...
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< DataProcessorSpec > WorkflowSpec
WorkflowSpec defineDataProcessing(ConfigContext const &cfgc)
This function hooks up the the workflow specifications into the DPL driver.
Configurable< int > aggregate
Configurable< std::string > hepmcFileName
void run(o2::framework::ProcessingContext &pc)
void init(o2::framework::InitContext &)
std::vector< o2::MCTrack > mcTracks
std::shared_ptr< HepMC3::Reader > hepMCReader
Configurable< bool > hepmcv2
Configurable< int > maxEvents
static constexpr const char * impactParameter
static constexpr const char * xSection
static constexpr const char * planeAngle
static constexpr const char * nPartTarget
static constexpr const char * nSpecTargetProton
static constexpr const char * pdfCode2
static constexpr const char * pdfScale
static constexpr const char * pdfXF2
static constexpr const char * nSpecProjectileNeutron
static constexpr const char * sigmaInelNN
static constexpr const char * processID
static constexpr const char * attemptedEvents
static constexpr const char * nPartProjectile
static constexpr const char * processCode
static constexpr const char * nCollNWoundedNwounded
static constexpr const char * pdfX2
static constexpr const char * nSpecTargetNeutron
static constexpr const char * pdfParton1Id
static constexpr const char * pdfParton2Id
static constexpr const char * xSectionError
static constexpr const char * nSpecProjectileProton
static constexpr const char * pdfCode1
static constexpr const char * acceptedEvents
static constexpr const char * centrality
static constexpr const char * nCollNNWounded
static constexpr const char * nColl
static constexpr const char * eventScale
static constexpr const char * pdfXF1
static constexpr const char * mpi
static constexpr const char * nCollHard
static constexpr const char * pdfX1
static constexpr const char * nCollNWoundedN
static ServiceSpec arrowBackendSpec()
static AlgorithmSpec wrapWithRateLimiting(AlgorithmSpec spec)