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
32 typedef uint64_t GenCount;
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
54 {
58 // helper to parse vertex option; returns true if parsing ok, false if failure
61 LOG(error) << "Could not parse vtxMode";
62 }
63
64 // update config key params
67 // set the number of events in the static Generator variable gTotalNEvents.
68 // Variable is unset if nEvents exceeds the uint maximum value
69 if (nEvents <= std::numeric_limits<unsigned int>::max()) {
70 unsigned int castNEvents = static_cast<unsigned int>(nEvents);
72 }
73 // initialize the service
76 } else if (vtxmode == o2::conf::VertexMode::kNoVertex) {
78 } else if (vtxmode == o2::conf::VertexMode::kCCDB) {
79 LOG(warn) << "Not yet supported. This needs definition of a timestamp and fetching of the MeanVertex CCDB object";
80 }
81 timer.Start();
82
83 if (outputPrefix->size() > 0 && !outfile.get()) {
84 auto kineoutfilename = o2::base::NameConf::getMCKinematicsFileName(outputPrefix->c_str());
85 outfile.reset(new TFile(kineoutfilename.c_str(), "RECREATE"));
86 outtree.reset(new TTree("o2sim", "o2sim"));
87 }
88 }
89
91 {
92 std::vector<o2::MCTrack> mctracks;
94 auto mctrack_ptr = &mctracks;
95 if (outfile.get()) {
96 auto br = o2::base::getOrMakeBranch(*outtree, "MCTrack", &mctrack_ptr);
97 br->SetAddress(&mctrack_ptr);
98 }
99
100 for (auto i = 0; i < std::min((GenCount)aggregate, nEvents - eventCounter); ++i) {
101 mctracks.clear();
102 genservice->generateEvent_MCTracks(mctracks, mcheader);
103 pc.outputs().snapshot(Output{"MC", "MCHEADER", 0}, mcheader);
104 pc.outputs().snapshot(Output{"MC", "MCTRACKS", 0}, mctracks);
105 ++eventCounter;
106
107 if (outfile.get() && outtree.get()) {
108 outtree->Fill();
109 }
110 }
111
112 // report number of TFs injected for the rate limiter to work
113 ++tfCounter;
114 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));
115 bool time_expired = false;
116 if (ttl > 0) {
117 timer.Stop();
118 time_expired = timer.RealTime() > ttl;
119 timer.Start(false);
120 if (time_expired) {
121 LOG(info) << "TTL expired after " << eventCounter << " events ... sending end-of-stream";
122 }
123 }
124 if (eventCounter >= nEvents || time_expired) {
125 pc.services().get<ControlService>().endOfStream();
126 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
127
128 // write out data to disc if asked
129 if (outfile.get()) {
130 outtree->SetEntries(eventCounter);
131 outtree->Write();
132 outfile->Close();
133 }
134 }
135 }
136};
137
139{
140 auto spec = adaptAnalysisTask<GeneratorTask>(cfgc);
141 spec.outputs.emplace_back("MC", "MCHEADER", 0, Lifetime::Timeframe);
142 spec.outputs.emplace_back("MC", "MCTRACKS", 0, Lifetime::Timeframe);
143 spec.requiredServices.push_back(o2::framework::ArrowSupport::arrowBackendSpec());
144 spec.algorithm = CommonDataProcessors::wrapWithRateLimiting(spec.algorithm);
145 return {spec};
146}
Definition of the Detector class.
Definition of the Names Generator class.
int32_t i
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:89
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.
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:281
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< DataProcessorSpec > WorkflowSpec
uint64_t GenCount
Configurable< int > aggregate
Configurable< std::string > outputPrefix
void run(o2::framework::ProcessingContext &pc)
std::unique_ptr< TTree > outtree
std::unique_ptr< o2::eventgen::GeneratorService > genservice
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"