Skip to content

Commit 7f12fad

Browse files
jackal1-66sawenzel
authored andcommitted
Added static variable for number of events in Generator
1 parent 359b736 commit 7f12fad

File tree

9 files changed

+26
-6
lines changed

9 files changed

+26
-6
lines changed

Generators/include/Generators/Generator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ class Generator : public FairGenerator
8585
void setTriggerMode(ETriggerMode_t val) { mTriggerMode = val; };
8686
void addTrigger(Trigger trigger) { mTriggers.push_back(trigger); };
8787
void addDeepTrigger(DeepTrigger trigger) { mDeepTriggers.push_back(trigger); };
88+
// setter for global number of events
89+
static void setTotalNEvents(unsigned int& n) { gTotalNEvents = n; }
8890

8991
/** getters **/
9092
const std::vector<TParticle>& getParticles() const { return mParticles; }; //!
93+
static unsigned int getTotalNEvents() { return gTotalNEvents; };
9194

9295
/** other **/
9396
void clearParticles() { mParticles.clear(); };
@@ -152,6 +155,9 @@ class Generator : public FairGenerator
152155
// the current ID of the sub-generator used in the current event (if applicable)
153156
int mSubGeneratorId = -1;
154157

158+
// global static information about (upper limit of) number of events to be generated
159+
static unsigned int gTotalNEvents;
160+
155161
ClassDefOverride(Generator, 2);
156162

157163
}; /** class Generator **/

Generators/include/Generators/GeneratorHybrid.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class GeneratorHybrid : public Generator
6363
Bool_t importParticles() override;
6464
void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override;
6565

66-
void setNEvents(int n) { mNEvents = n; }
67-
6866
Bool_t parseJSON(const std::string& path);
6967
Bool_t confSetter(const auto& gen);
7068
template <typename T>
@@ -116,7 +114,6 @@ class GeneratorHybrid : public Generator
116114
std::atomic<bool> mStopFlag;
117115
bool mIsInitialized = false;
118116

119-
int mNEvents = -1; // the number of events to be done, if known (helps initiating cleanup)
120117
o2::dataformats::MCEventHeader mMCEventHeader; // to capture event headers
121118

122119
enum class GenMode {

Generators/src/Generator.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace eventgen
2929
{
3030

3131
std::atomic<int> Generator::InstanceCounter{0};
32-
32+
unsigned int Generator::gTotalNEvents = 0;
3333
/*****************************************************************/
3434
/*****************************************************************/
3535

Generators/src/GeneratorFactory.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
285285
return;
286286
}
287287
auto hybrid = new o2::eventgen::GeneratorHybrid(config);
288-
hybrid->setNEvents(conf.getNEvents());
289288
primGen->AddGenerator(hybrid);
290289
#endif
291290
} else {

Generators/src/GeneratorHybrid.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ bool GeneratorHybrid::importParticles()
408408

409409
mseqCounter++;
410410
mEventCounter++;
411-
if (mEventCounter == mNEvents) {
411+
if (mEventCounter == getTotalNEvents()) {
412412
LOG(info) << "HybridGen: Stopping TBB task pool";
413413
mStopFlag = true;
414414
}

Generators/src/GeneratorService.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Generators/GeneratorService.h"
1313
#include "Generators/GeneratorFactory.h"
1414
#include "SimConfig/SimConfig.h"
15+
#include "Generators/Generator.h"
1516
#include "DataFormatsCalibration/MeanVertexObject.h"
1617

1718
using namespace o2::eventgen;
@@ -23,6 +24,7 @@ void GeneratorService::initService(std::string const& genName,
2324
auto localSimConfig = o2::conf::SimConfig::make();
2425
localSimConfig.getConfigData().mGenerator = genName;
2526
localSimConfig.getConfigData().mTrigger = triggerName;
27+
localSimConfig.getConfigData().mNEvents = o2::eventgen::Generator::getTotalNEvents();
2628

2729
o2::eventgen::GeneratorFactory::setPrimaryGenerator(localSimConfig, &mPrimGen);
2830

macro/o2sim.C

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#if !defined(__CLING__) || defined(__ROOTCLING__)
1414
#include <Generators/PrimaryGenerator.h>
1515
#include <Generators/GeneratorFactory.h>
16+
#include <Generators/Generator.h>
1617
#include "SimulationDataFormat/O2DatabasePDG.h"
1718
#include "SimulationDataFormat/MCEventHeader.h"
1819
#include <SimConfig/SimConfig.h>
@@ -61,6 +62,9 @@ void check_notransport()
6162
FairRunSim* o2sim_init(bool asservice, bool evalmat = false)
6263
{
6364
auto& confref = o2::conf::SimConfig::Instance();
65+
// set the global information about the number of events to be generated
66+
unsigned int nTotalEvents = confref.getNEvents();
67+
o2::eventgen::Generator::setTotalNEvents(nTotalEvents);
6468
// initialize CCDB service
6569
auto& ccdbmgr = o2::ccdb::BasicCCDBManager::instance();
6670
// fix the timestamp early

run/O2PrimaryServerDevice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <SimulationDataFormat/PrimaryChunk.h>
2828
#include <Generators/GeneratorFromFile.h>
2929
#include <Generators/PrimaryGenerator.h>
30+
#include <Generators/Generator.h>
3031
#include <SimConfig/SimConfig.h>
3132
#include <CommonUtils/ConfigurableParam.h>
3233
#include <CommonUtils/RngHelper.h>
@@ -87,6 +88,10 @@ class O2PrimaryServerDevice final : public fair::mq::Device
8788
ccdbmgr.setURL(conf.getConfigData().mCCDBUrl);
8889
ccdbmgr.setTimestamp(conf.getTimestamp());
8990

91+
// set the global information about the number of events to be generated
92+
unsigned int nTotalEvents = conf.getNEvents();
93+
o2::eventgen::Generator::setTotalNEvents(nTotalEvents);
94+
9095
// init magnetic field as it might be needed by the generator
9196
if (TGeoGlobalMagField::Instance()->GetField() == nullptr) {
9297
TGeoGlobalMagField::Instance()->SetField(o2::base::SimFieldUtils::createMagField());

run/dpl_eventgen.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SimulationDataFormat/MCTrack.h"
1717
#include "Framework/runDataProcessing.h"
1818
#include <Generators/GeneratorService.h>
19+
#include <Generators/Generator.h>
1920
#include <CommonUtils/ConfigurableParam.h>
2021
#include <CommonUtils/RngHelper.h>
2122
#include <TStopwatch.h> // simple timer from ROOT
@@ -63,6 +64,12 @@ struct GeneratorTask {
6364
// update config key params
6465
o2::conf::ConfigurableParam::updateFromFile(iniFile);
6566
o2::conf::ConfigurableParam::updateFromString((std::string)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);
71+
o2::eventgen::Generator::setTotalNEvents(castNEvents);
72+
}
6673
// initialize the service
6774
if (vtxmode == o2::conf::VertexMode::kDiamondParam) {
6875
genservice->initService(generator, trigger, o2::eventgen::DiamondParamVertexOption());

0 commit comments

Comments
 (0)