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
41 std::vector<o2::pmr::vector<o2::MCTrack>*> mctracks_vector;
42 std::vector<o2::dataformats::MCEventHeader*> mcheader_vector;
43
45 {
46 if (hepmcv2) {
47 hepMCReader = std::make_shared<HepMC3::ReaderAsciiHepMC2>((std::string)hepmcFileName);
48 } else {
49 hepMCReader = std::make_shared<HepMC3::ReaderAscii>((std::string)hepmcFileName);
50 }
51 if (hepMCReader->failed()) {
52 LOGP(fatal, "Cannot open HEPMC kine file {}", (std::string)hepmcFileName);
53 }
54 // allocate the memory upfront to prevent reallocations later
57 }
58
60 {
61 HepMC3::GenEvent event;
62 auto batch = maxEvents > 0 ? std::min((int)aggregate, (int)maxEvents - eventCounter) : (int)aggregate;
63 for (auto i = 0; i < batch; ++i) {
64 mctracks_vector.push_back(&pc.outputs().make<o2::pmr::vector<o2::MCTrack>>(Output{"MC", "MCTRACKS", 0}));
65 auto& mctracks = mctracks_vector.back();
66 mcheader_vector.push_back(&pc.outputs().make<o2::dataformats::MCEventHeader>(Output{"MC", "MCHEADER", 0}));
67 auto& mcheader = mcheader_vector.back();
68 // read next entry
69 hepMCReader->read_event(event);
70 if (hepMCReader->failed()) {
71 LOGP(warn, "Failed to read from HEPMC input file");
72 eos = true;
73 break;
74 }
75
76 // create O2 MCHeader and MCtracks vector out of HEPMC event
77 mcheader->SetEventID(event.event_number());
78 mcheader->SetVertex(event.event_pos().px(), event.event_pos().py(), event.event_pos().pz());
79 auto xsecInfo = event.cross_section();
80 if (xsecInfo != nullptr) {
81 mcheader->putInfo(MCInfoKeys::acceptedEvents, (uint64_t)xsecInfo->get_accepted_events());
82 mcheader->putInfo(MCInfoKeys::attemptedEvents, (uint64_t)xsecInfo->get_attempted_events());
83 mcheader->putInfo(MCInfoKeys::xSection, (float)xsecInfo->xsec());
84 mcheader->putInfo(MCInfoKeys::xSectionError, (float)xsecInfo->xsec_err());
85 }
86 auto scale = event.attribute<HepMC3::DoubleAttribute>(MCInfoKeys::eventScale);
87 if (scale != nullptr) {
88 mcheader->putInfo(MCInfoKeys::eventScale, (float)scale->value());
89 }
90 auto nMPI = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::mpi);
91 if (nMPI != nullptr) {
92 mcheader->putInfo(MCInfoKeys::mpi, nMPI->value());
93 }
94 auto sid = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::processCode);
95 auto scode = event.attribute<HepMC3::IntAttribute>(MCInfoKeys::processID); // default pythia8 hepmc3 interface uses signal_process_id
96 if (sid != nullptr) {
97 mcheader->putInfo(MCInfoKeys::processCode, sid->value());
98 } else if (scode != nullptr) {
99 mcheader->putInfo(MCInfoKeys::processCode, scode->value());
100 }
101 auto pdfInfo = event.pdf_info();
102 if (pdfInfo != nullptr) {
103 mcheader->putInfo(MCInfoKeys::pdfParton1Id, pdfInfo->parton_id[0]);
104 mcheader->putInfo(MCInfoKeys::pdfParton2Id, pdfInfo->parton_id[1]);
105 mcheader->putInfo(MCInfoKeys::pdfCode1, pdfInfo->pdf_id[0]);
106 mcheader->putInfo(MCInfoKeys::pdfCode2, pdfInfo->pdf_id[1]);
107 mcheader->putInfo(MCInfoKeys::pdfX1, (float)pdfInfo->x[0]);
108 mcheader->putInfo(MCInfoKeys::pdfX2, (float)pdfInfo->x[1]);
109 mcheader->putInfo(MCInfoKeys::pdfScale, (float)pdfInfo->scale);
110 mcheader->putInfo(MCInfoKeys::pdfXF1, (float)pdfInfo->xf[0]);
111 mcheader->putInfo(MCInfoKeys::pdfXF2, (float)pdfInfo->xf[1]);
112 }
113 auto heavyIon = event.heavy_ion();
114 if (heavyIon != nullptr) {
115 mcheader->putInfo(MCInfoKeys::nCollHard, heavyIon->Ncoll_hard);
116 mcheader->putInfo(MCInfoKeys::nPartProjectile, heavyIon->Npart_proj);
117 mcheader->putInfo(MCInfoKeys::nPartTarget, heavyIon->Npart_targ);
118 mcheader->putInfo(MCInfoKeys::nColl, heavyIon->Ncoll);
119 mcheader->putInfo(MCInfoKeys::nCollNNWounded, heavyIon->N_Nwounded_collisions);
120 mcheader->putInfo(MCInfoKeys::nCollNWoundedN, heavyIon->Nwounded_N_collisions);
121 mcheader->putInfo(MCInfoKeys::nCollNWoundedNwounded, heavyIon->Nwounded_Nwounded_collisions);
122 mcheader->putInfo(MCInfoKeys::nSpecProjectileNeutron, heavyIon->Nspec_proj_n);
123 mcheader->putInfo(MCInfoKeys::nSpecProjectileProton, heavyIon->Nspec_proj_p);
124 mcheader->putInfo(MCInfoKeys::nSpecTargetNeutron, heavyIon->Nspec_targ_n);
125 mcheader->putInfo(MCInfoKeys::nSpecTargetProton, heavyIon->Nspec_targ_p);
126 mcheader->putInfo(MCInfoKeys::impactParameter, (float)heavyIon->impact_parameter);
127 mcheader->putInfo(MCInfoKeys::planeAngle, (float)heavyIon->event_plane_angle);
128 mcheader->putInfo("eccentricity", (float)heavyIon->eccentricity);
129 mcheader->putInfo(MCInfoKeys::sigmaInelNN, (float)heavyIon->sigma_inel_NN);
130 mcheader->putInfo(MCInfoKeys::centrality, (float)heavyIon->centrality);
131 }
132
133 auto particles = event.particles();
134 for (auto const& particle : particles) {
135 auto parents = particle->parents();
136 auto has_parents = parents.size() > 0;
137 auto children = particle->children();
138 auto has_children = children.size() > 0;
139 auto p = particle->momentum();
140 auto v = particle->production_vertex();
141 mctracks->emplace_back(
142 particle->pid(),
143 has_parents ? parents.front()->id() : -1, has_parents ? parents.back()->id() : -1,
144 has_children ? children.front()->id() : -1, has_children ? children.back()->id() : -1,
145 p.px(), p.py(), p.pz(),
146 v->position().x(), v->position().y(), v->position().z(),
147 v->position().t(), 0);
148 }
149 ++eventCounter;
150 }
151
152 // report number of TFs injected for the rate limiter to work
153 ++tfCounter;
154 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));
155 if (eos || (maxEvents > 0 && eventCounter >= maxEvents)) {
156 pc.services().get<ControlService>().endOfStream();
157 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
158 }
159 }
160};
161
163{
164 auto spec = adaptAnalysisTask<O2simHepmcPublisher>(cfgc);
165 spec.outputs.emplace_back("MC", "MCHEADER", 0, Lifetime::Timeframe);
166 spec.outputs.emplace_back("MC", "MCTRACKS", 0, Lifetime::Timeframe);
167 spec.requiredServices.push_back(o2::framework::ArrowSupport::arrowBackendSpec());
168 spec.algorithm = CommonDataProcessors::wrapWithRateLimiting(spec.algorithm);
169 return {spec};
170}
int32_t i
Definition of the MCTrack class.
decltype(auto) make(const Output &spec, Args... args)
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 ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::vector< DataProcessorSpec > WorkflowSpec
std::vector< T, fair::mq::pmr::polymorphic_allocator< T > > vector
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)
std::vector< o2::pmr::vector< o2::MCTrack > * > mctracks_vector
void init(o2::framework::InitContext &)
std::vector< o2::dataformats::MCEventHeader * > mcheader_vector
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)