diff --git a/Generators/include/Generators/GeneratorHybrid.h b/Generators/include/Generators/GeneratorHybrid.h index 061eed088e5d3..ad68107d7625d 100644 --- a/Generators/include/Generators/GeneratorHybrid.h +++ b/Generators/include/Generators/GeneratorHybrid.h @@ -106,6 +106,8 @@ class GeneratorHybrid : public Generator // Cocktail mode bool mCocktailMode = false; std::vector> mGroups; + // Sub-generator ID reported by each entry of the generators list + std::vector mGroupSubGenIds; // Trigger configuration std::vector mTriggerModes; // trigger mode for each generator diff --git a/Generators/src/GeneratorHybrid.cxx b/Generators/src/GeneratorHybrid.cxx index c19dccf5eb6ec..96dce849223f1 100644 --- a/Generators/src/GeneratorHybrid.cxx +++ b/Generators/src/GeneratorHybrid.cxx @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -73,18 +74,12 @@ GeneratorHybrid::GeneratorHybrid(const std::string& inputgens) } } int index = 0; + // Parallel mode ignores the fractions. + if (mGenerationMode != GenMode::kParallel && mGroups.size() != mFractions.size()) { + LOG(fatal) << "Number of fractions does not match the number of entries in the generators list"; + return; + } if (!(mRandomize || mGenerationMode == GenMode::kParallel)) { - if (mCocktailMode) { - if (mGroups.size() != mFractions.size()) { - LOG(fatal) << "Number of groups does not match the number of fractions"; - return; - } - } else { - if (mFractions.size() != mInputGens.size()) { - LOG(fatal) << "Number of fractions does not match the number of generators"; - return; - } - } // Check if all elements of mFractions are 0 if (std::all_of(mFractions.begin(), mFractions.end(), [](int i) { return i == 0; })) { LOG(fatal) << "All fractions provided are 0, no simulation will be performed"; @@ -245,7 +240,6 @@ Bool_t GeneratorHybrid::Init() dynamic_cast(gens[count].get())->setConfig(config); } gens[count]->Init(); // TODO: move this to multi-threaded - addSubGenerator(count, gen); if (mTriggerModes[count] != o2::eventgen::Generator::kTriggerOFF) { gens[count]->setTriggerMode(mTriggerModes[count]); LOG(info) << "Setting Trigger mode of generator " << gen << " to: " << mTriggerModes[count]; @@ -286,6 +280,23 @@ Bool_t GeneratorHybrid::Init() } count++; } + // A sub-generator ID identifies an entry of the generators list rather than a single generator: + // an entry can be served by several generators (a cocktail) or share one with another entry (a + // 'ref'). Descriptions are therefore registered per entry, under the ID that entry reports, so + // that every ID written to the event header has one. Entries sharing an ID (a 'ref' and the + // entry it points to) are served by the same generators, and addSubGenerator keeps the first. + for (size_t entry = 0; entry < mGroups.size(); ++entry) { + std::string description; + for (const auto generator : mGroups[entry]) { + if (!description.empty()) { + description += "+"; + } + description += mGens[generator]; + } + LOG(debug) << "Sub-generator " << mGroupSubGenIds[entry] << " (entry " << entry << "): " << description; + addSubGenerator(mGroupSubGenIds[entry], description); + } + // Label for groups: concatenation of the names of the generators in the group, separated by '+'. // Currently used only if randomisation is enabled auto groupLabel = [this](int k) { @@ -445,7 +456,7 @@ bool GeneratorHybrid::generateEvent() } mIndex = mCurrentFraction; } - notifySubGenerator(mIndex); + notifySubGenerator(mGroupSubGenIds[mIndex]); } return true; } @@ -453,24 +464,15 @@ bool GeneratorHybrid::generateEvent() bool GeneratorHybrid::importParticles() { int genIndex = -1; - std::vector subGenIndex = {}; if (mIndex == -1) { // this means parallel mode ---> we have a common queue mResultQueue[0].pop(genIndex); - } else { - // need to pop from a particular queue - if (!mCocktailMode) { - mResultQueue[mIndex].pop(genIndex); - } else { - // in cocktail mode we need to pop from the group queue - subGenIndex.resize(mGroups[mIndex].size()); - for (size_t pos = 0; pos < mGroups[mIndex].size(); ++pos) { - int subIndex = mGroups[mIndex][pos]; - LOG(info) << "Getting generator " << mGens[subIndex] << " from cocktail group " << mIndex; - mResultQueue[subIndex].pop(subGenIndex[pos]); - } - } + } else if (!mCocktailMode) { + // need to pop from the queue of the generator serving this entry of the list + mResultQueue[mGroups[mIndex].front()].pop(genIndex); } + // in cocktail mode the events of the group members are popped one by one while they are + // imported, so that one generator shared by two members of the same group can serve both auto unit_transformer = [](auto& p, auto pos_unit, auto time_unit, auto en_unit, auto mom_unit) { p.SetMomentum(p.Px() * mom_unit, p.Py() * mom_unit, p.Pz() * mom_unit, p.Energy() * en_unit); @@ -503,8 +505,14 @@ bool GeneratorHybrid::importParticles() const bool queueNextEvent = getTotalNEvents() == 0 || mEventCounter + 1 < getTotalNEvents(); if (mCocktailMode) { // in cocktail mode we need to merge the particles from the different generators - for (auto subIndex : subGenIndex) { + const auto& group = mGroups[mIndex]; + for (size_t pos = 0; pos < group.size(); ++pos) { + const auto member = group[pos]; + int subIndex = -1; + // wait for the event of this generator; the queue index is the generator index + mResultQueue[member].pop(subIndex); if (!mIsHyperloop) { + LOG(info) << "Getting generator " << mGens[subIndex] << " from cocktail group " << mIndex; LOG(info) << "Importing particles for task " << subIndex; } auto subParticles = gens[subIndex]->getParticles(); @@ -531,7 +539,12 @@ bool GeneratorHybrid::importParticles() gens[subIndex]->updateHeader(&mMCEventHeader); mHeaderGeneratorIndex = subIndex; // store index of generator updating the header } - if (queueNextEvent) { + // One generator can serve several members of the same group on the reference/label mechanism, + // and each of them consumes one event of it. The remaining members would then wait forever for an event nobody + // prepares, so such a generator is queued again even when the event budget is over. + // The event is consumed before this loop ends, hence nothing is left running. + const bool neededAgainInThisGroup = std::find(group.begin() + pos + 1, group.end(), member) != group.end(); + if (queueNextEvent || neededAgainInThisGroup) { mInputTaskQueue.push(subIndex); mTasksStarted++; } @@ -749,19 +762,105 @@ Bool_t GeneratorHybrid::parseJSON(const std::string& path) // Put the generator names in mInputGens if (doc.HasMember("generators")) { + // A generator, or a whole cocktail, can be given a name with 'label' and reused by any later + // entry with {"ref": "