21#include "FairPrimaryGenerator.h"
22#include <fairlogger/Logger.h>
24#include "TClonesArray.h"
30#ifdef GENERATORS_WITH_TPCLOOPERS
41unsigned int Generator::gTotalNEvents = 0;
51#ifdef GENERATORS_WITH_TPCLOOPERS
54 if (!loopersParam.loopersVeto) {
55 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
57 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
59 if (initTPCLoopersGen()) {
60 mAddTPCLoopers = kTRUE;
63 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
67 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
80#ifdef GENERATORS_WITH_TPCLOOPERS
83 if (!loopersParam.loopersVeto) {
84 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
86 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
88 if (initTPCLoopersGen()) {
89 mAddTPCLoopers = kTRUE;
92 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
96 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
106#ifdef GENERATORS_WITH_TPCLOOPERS
107 if (mTPCLoopersGen) {
108 delete mTPCLoopersGen;
109 mTPCLoopersGen =
nullptr;
115#ifdef GENERATORS_WITH_TPCLOOPERS
116bool Generator::initTPCLoopersGen()
120 std::string model_pairs = gSystem->ExpandPathName(loopersParam.model_pairs.c_str());
121 std::string model_compton = gSystem->ExpandPathName(loopersParam.model_compton.c_str());
122 std::string nclxrate = gSystem->ExpandPathName(loopersParam.nclxrate.c_str());
123 const auto& scaler_pair = gSystem->ExpandPathName(loopersParam.scaler_pair.c_str());
124 const auto& scaler_compton = gSystem->ExpandPathName(loopersParam.scaler_compton.c_str());
125 const auto& poisson = gSystem->ExpandPathName(loopersParam.poisson.c_str());
126 const auto& gauss = gSystem->ExpandPathName(loopersParam.gauss.c_str());
127 const auto& flat_gas = loopersParam.flat_gas;
128 const auto& colsys = loopersParam.colsys;
130 if (colsys !=
"PbPb" && colsys !=
"pp") {
131 LOG(warning) <<
"Automatic background loopers configuration supports only 'pp' and 'PbPb' systems.";
132 LOG(warning) <<
"Fast loopers generator will remain OFF.";
135 bool isContext = std::filesystem::exists(
"collisioncontext.root");
137 LOG(warning) <<
"Warning: No collisioncontext.root file found!";
138 LOG(warning) <<
"Loopers will be kept OFF.";
142 std::array<float, 2> multiplier = {loopersParam.multiplier[0], loopersParam.multiplier[1]};
143 unsigned int nLoopersPairs = loopersParam.fixedNLoopers[0];
144 unsigned int nLoopersCompton = loopersParam.fixedNLoopers[1];
145 const std::array<std::string, 3> models = {model_pairs, model_compton, nclxrate};
146 const std::array<std::string, 3> local_names = {
"WGANpair.onnx",
"WGANcompton.onnx",
"nclxrate.root"};
147 const std::array<bool, 3> isAlien = {models[0].starts_with(
"alien://"), models[1].starts_with(
"alien://"), models[2].starts_with(
"alien://")};
148 const std::array<bool, 3> isCCDB = {models[0].starts_with(
"ccdb://"), models[1].starts_with(
"ccdb://"), models[2].starts_with(
"ccdb://")};
149 if (std::any_of(isAlien.begin(), isAlien.end(), [](
bool v) { return v; })) {
151 TGrid::Connect(
"alien://");
153 LOG(fatal) <<
"AliEn connection failed, check token.";
157 for (
size_t i = 0;
i < models.size(); ++
i) {
158 if (isAlien[
i] && !TFile::Cp(models[
i].c_str(), local_names[
i].c_str())) {
159 LOG(fatal) <<
"Error: Model file " << models[
i] <<
" does not exist!";
164 if (std::any_of(isCCDB.begin(), isCCDB.end(), [](
bool v) { return v; })) {
166 ccdb.setURL(
"http://alice-ccdb.cern.ch");
168 auto& ccdb_api = ccdb.getCCDBAccessor();
169 for (
size_t i = 0;
i < models.size(); ++
i) {
171 auto model_path = models[
i].substr(7);
173 auto extension = model_path.find(
".onnx");
174 if (extension != std::string::npos) {
175 auto last_slash = model_path.find_last_of(
'/');
176 model_path = model_path.substr(0, last_slash);
178 std::map<std::string, std::string>
filter;
180 LOG(fatal) <<
"Error: issues in retrieving " << model_path <<
" from CCDB!";
186 model_pairs = isAlien[0] || isCCDB[0] ? local_names[0] : model_pairs;
187 model_compton = isAlien[1] || isCCDB[1] ? local_names[1] : model_compton;
188 nclxrate = isAlien[2] || isCCDB[2] ? local_names[2] : nclxrate;
191 mTPCLoopersGen =
new o2::eventgen::GenTPCLoopers(model_pairs, model_compton, poisson, gauss, scaler_pair, scaler_compton);
192 const auto& intrate = loopersParam.intrate;
196 mTPCLoopersGen->SetRate(nclxrate, (colsys ==
"PbPb") ?
true : false, intrate);
197 mTPCLoopersGen->SetAdjust(loopersParam.adjust_flatgas);
202 mTPCLoopersGen->SetNLoopers(nLoopersPairs, nLoopersCompton);
203 mTPCLoopersGen->SetMultiplier(multiplier);
205 LOG(info) <<
"TPC Loopers generator initialized successfully";
206 }
catch (
const std::exception& e) {
207 LOG(error) <<
"Failed to initialize TPC Loopers generator: " << e.what();
208 delete mTPCLoopersGen;
209 mTPCLoopersGen =
nullptr;
232#ifdef GENERATORS_WITH_TPCLOOPERS
233 if (mAddTPCLoopers) {
234 if (!mTPCLoopersGen) {
235 LOG(error) <<
"Loopers generator not initialized";
240 if (!mTPCLoopersGen->generateEvent()) {
241 LOG(error) <<
"Failed to generate loopers event";
244 if (mTPCLoopersGen->getNLoopers() == 0) {
245 LOG(warning) <<
"No loopers generated for this event";
248 const auto& looperParticles = mTPCLoopersGen->importParticles();
249 if (looperParticles.empty()) {
250 LOG(error) <<
"Failed to import loopers particles";
256 LOG(
debug) <<
"Added " << looperParticles.size() <<
" looper particles";
277 mSubGeneratorId = -1;
281 LOG(error) <<
"ReadEvent failed in generateEvent";
287 LOG(error) <<
"ReadEvent failed in importParticles";
293 LOG(error) <<
"ReadEvent failed in finalizeEvent";
297 if (mSubGeneratorsIdToDesc.empty() && mSubGeneratorId > -1) {
298 LOG(fatal) <<
"ReadEvent failed because no SubGenerator description given";
301 if (!mSubGeneratorsIdToDesc.empty() && mSubGeneratorId < 0) {
302 LOG(fatal) <<
"ReadEvent failed because SubGenerator description given but sub-generator not set";
316 LOG(error) <<
"ReadEvent failed in addTracks";
321 auto header = primGen->GetEvent();
324 LOG(fatal) <<
"MC event header is not a 'o2::dataformats::MCEventHeader' object";
328 updateSubGeneratorInformation(o2header);
343 LOG(fatal) <<
"PrimaryGenerator is not a o2::eventgen::PrimaryGenerator";
349 o2primGen->
AddTrack(particle.GetPdgCode(),
356 particle.GetMother(0),
357 particle.GetMother(1),
358 particle.GetDaughter(0),
359 particle.GetDaughter(1),
363 particle.GetWeight(),
364 (TMCProcess)particle.GetUniqueID(),
365 particle.GetStatusCode());
437 if (subGeneratorId < 0) {
438 LOG(fatal) <<
"Sub-generator IDs must be >= 0, instead, passed value is " << subGeneratorId;
440 mSubGeneratorsIdToDesc.insert({subGeneratorId, subGeneratorDescription});
447 if (mSubGeneratorId < 0) {
ClassImp(o2::eventgen::Generator)
static BasicCCDBManager & instance()
static const GenTPCLoopersParam & Instance()
static SimConfig & Instance()
virtual void updateHeader(o2::dataformats::MCEventHeader *eventHeader)
std::string mInterfaceName
Bool_t ReadEvent(FairPrimaryGenerator *primGen) final
static std::atomic< int > InstanceCounter
std::vector< Trigger > mTriggers
std::function< void(std::vector< TParticle > const &p, int eventCount)> mTriggerOkHook
virtual Bool_t generateEvent()=0
std::vector< DeepTrigger > mDeepTriggers
std::function< void(std::vector< TParticle > const &p, int eventCount)> mTriggerFalseHook
void addSubGenerator(int subGeneratorId, std::string const &subGeneratorDescription)
ETriggerMode_t mTriggerMode
Bool_t addTracks(FairPrimaryGenerator *primGen)
std::vector< TParticle > mParticles
virtual Bool_t importParticles()=0
void AddTrack(Int_t pdgid, Double_t px, Double_t py, Double_t pz, Double_t vx, Double_t vy, Double_t vz, Int_t mother1=-1, Int_t mother2=-1, Int_t daughter1=-1, Int_t daughter2=-1, Bool_t wanttracking=true, Double_t e=-9e9, Double_t tof=0., Double_t weight=0., TMCProcess proc=kPPrimary, Int_t generatorStatus=0)
static constexpr Property SUBGENERATORID
static constexpr Property SUBGENERATORDESCRIPTIONMAP
GLuint const GLchar * name
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
long getCurrentTimestamp()
returns the timestamp in long corresponding to "now"
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"