Project
Loading...
Searching...
No Matches
dpl_eventgen.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"
22#include <TStopwatch.h> // simple timer from ROOT
25#include <TFile.h>
26#include <memory>
27
28using namespace o2::framework;
29
31 // For readability to indicate where counting certain things (such as events or timeframes) should be of the same order of magnitude
33 Configurable<std::string> generator{"generator", "boxgen", "Name of generator"};
34 Configurable<GenCount> eventNum{"nEvents", 1, "Number of events"};
35 Configurable<std::string> trigger{"trigger", "", "Trigger type"}; //
36 Configurable<std::string> iniFile{"configFile", "", "INI file containing configurable parameters"};
37 Configurable<std::string> params{"configKeyValues", "", "configurable params - configuring event generation internals"};
38 Configurable<long> seed{"seed", 0, "(TRandom) Seed"};
39 Configurable<int> aggregate{"aggregate-timeframe", 300, "Number of events to put in a timeframe"};
40 Configurable<std::string> vtxModeArg{"vertexMode", "kDiamondParam", "Where the beam-spot vertex should come from. Must be one of kNoVertex, kDiamondParam, kCCDB"};
41 Configurable<int64_t> ttl{"time-limit", -1, "Maximum run time limit in seconds (default no limit)"};
42 Configurable<std::string> outputPrefix{"output", "", "Optional prefix for kinematics files written on disc. If non-empty, files <prefix>_Kine.root + <prefix>_MCHeader.root will be created."};
46 std::unique_ptr<TFile> outfile{};
47 std::unique_ptr<TTree> outtree{};
48
49 // a pointer because object should only be constructed in the device (not during DPL workflow setup)
50 std::unique_ptr<o2::eventgen::GeneratorService> genservice;
51 TStopwatch timer;
52
53 std::vector<o2::pmr::vector<o2::MCTrack>*> mctracks_vector;
54 std::vector<o2::dataformats::MCEventHeader*> mcheader_vector;
55
57 {
61 // helper to parse vertex option; returns true if parsing ok, false if failure
64 LOG(error) << "Could not parse vtxMode";
65 }
66
67 // update config key params
70 // set the number of events in the static Generator variable gTotalNEvents.
71 // Variable is unset if nEvents exceeds the uint maximum value
72 if (nEvents <= std::numeric_limits<unsigned int>::max()) {
73 unsigned int castNEvents = static_cast<unsigned int>(nEvents);
75 }
76 // initialize the service
79 } else if (vtxmode == o2::conf::VertexMode::kNoVertex) {
81 } else if (vtxmode == o2::conf::VertexMode::kCCDB) {
82 LOG(warn) << "Not yet supported. This needs definition of a timestamp and fetching of the MeanVertex CCDB object";
83 }
84 timer.Start();
85
86 if (outputPrefix->size() > 0 && !outfile.get()) {
87 auto kineoutfilename = o2::base::NameConf::getMCKinematicsFileName(outputPrefix->c_str());
88 outfile.reset(new TFile(kineoutfilename.c_str(), "RECREATE"));
89 outtree.reset(new TTree("o2sim", "o2sim"));
90 }
91
94 }
95
97 {
98 mctracks_vector.clear();
99 mcheader_vector.clear();
100
101 auto batch = std::min((GenCount)aggregate, nEvents - eventCounter);
102 for (auto i = 0U; i < batch; ++i) {
103 mctracks_vector.push_back(&pc.outputs().make<o2::pmr::vector<o2::MCTrack>>(Output{"MC", "MCTRACKS", 0}));
104 auto& mctracks = mctracks_vector.back();
105 mcheader_vector.push_back(&pc.outputs().make<o2::dataformats::MCEventHeader>(Output{"MC", "MCHEADER", 0}));
106 auto& mcheader = mcheader_vector.back();
107 genservice->generateEvent_MCTracks(*mctracks, *mcheader);
108 ++eventCounter;
109
110 auto mctrack_ptr = mctracks;
111 if (outfile.get()) {
112 auto br = o2::base::getOrMakeBranch(*outtree, "MCTrack", &mctrack_ptr);
113 br->SetAddress(&mctrack_ptr);
114 }
115
116 if (outfile.get() && outtree.get()) {
117 outtree->Fill();
118 }
119 }
120
121 // report number of TFs injected for the rate limiter to work
122 ++tfCounter;
123 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));
124
125 bool time_expired = false;
126 if (ttl > 0) {
127 timer.Stop();
128 time_expired = timer.RealTime() > ttl;
129 timer.Start(false);
130 if (time_expired) {
131 LOG(info) << "TTL expired after " << eventCounter << " events ... sending end-of-stream";
132 }
133 }
134 if (eventCounter >= nEvents || time_expired) {
135 // terminate and reap external generator subprocesses here: device
136 // teardown is not guaranteed to run the generators' destructors
137 genservice->stopGenerators();
138 pc.services().get<ControlService>().endOfStream();
139 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
140
141 // write out data to disk if asked
142 if (outfile.get()) {
143 outtree->SetEntries(eventCounter);
144 outtree->Write();
145 outfile->Close();
146 }
147 }
148 }
149};
150
152{
153 auto spec = adaptAnalysisTask<GeneratorTask>(cfgc);
154 spec.outputs.emplace_back("MC", "MCHEADER", 0, Lifetime::Timeframe);
155 spec.outputs.emplace_back("MC", "MCTRACKS", 0, Lifetime::Timeframe);
156 spec.requiredServices.push_back(o2::framework::ArrowSupport::arrowBackendSpec());
157 spec.algorithm = CommonDataProcessors::wrapWithRateLimiting(spec.algorithm);
158 return {spec};
159}
Definition of the Detector class.
Definition of the Names Generator class.
int32_t i
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
char error[CURL_ERROR_SIZE]
Definition of the MCTrack class.
static std::string getMCKinematicsFileName(const std::string_view prefix=STANDARDSIMPREFIX)
Definition NameConf.h:46
static void updateFromFile(std::string const &, std::string const &paramsList="", bool unchangedOnly=false)
static void updateFromString(std::string const &)
static bool parseVertexModeString(std::string const &vertexstring, o2::conf::VertexMode &mode)
A class offering convenient generator configuration and encapsulation of lower level classes....
static void setTotalNEvents(unsigned int &n)
Definition Generator.h:98
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.
static ULong_t setGRandomSeed(ULong_t seed=0)
Definition RngHelper.h:37
WorkflowSpec defineDataProcessing(ConfigContext const &cfgc)
This function hooks up the the workflow specifications into the DPL driver.
GLenum const GLfloat * params
Definition glcorearb.h:272
TBranch * getOrMakeBranch(TTree &tree, const char *brname, T *ptr)
Definition Detector.h:313
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::vector< DataProcessorSpec > WorkflowSpec
std::vector< T, fair::mq::pmr::polymorphic_allocator< T > > vector
uint64_t GenCount
Configurable< int > aggregate
Configurable< std::string > outputPrefix
void run(o2::framework::ProcessingContext &pc)
std::vector< o2::dataformats::MCEventHeader * > mcheader_vector
std::unique_ptr< TTree > outtree
std::unique_ptr< o2::eventgen::GeneratorService > genservice
std::vector< o2::pmr::vector< o2::MCTrack > * > mctracks_vector
Configurable< long > seed
Configurable< std::string > trigger
Configurable< GenCount > eventNum
GenCount eventCounter
Configurable< int64_t > ttl
TStopwatch timer
Configurable< std::string > iniFile
Configurable< std::string > generator
void init(o2::framework::InitContext &)
GenCount tfCounter
Configurable< std::string > vtxModeArg
std::unique_ptr< TFile > outfile
static ServiceSpec arrowBackendSpec()
static AlgorithmSpec wrapWithRateLimiting(AlgorithmSpec spec)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"