21#include "FairPrimaryGenerator.h"
22#include <fairlogger/Logger.h>
24#include "TClonesArray.h"
31#ifdef GENERATORS_WITH_TPCLOOPERS
42unsigned int Generator::gTotalNEvents = 0;
52#ifdef GENERATORS_WITH_TPCLOOPERS
55 if (!loopersParam.loopersVeto) {
56 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
58 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
60 if (initTPCLoopersGen()) {
61 mAddTPCLoopers = kTRUE;
64 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
68 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
81#ifdef GENERATORS_WITH_TPCLOOPERS
84 if (!loopersParam.loopersVeto) {
85 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
87 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
89 if (initTPCLoopersGen()) {
90 mAddTPCLoopers = kTRUE;
93 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
97 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
107#ifdef GENERATORS_WITH_TPCLOOPERS
108 if (mTPCLoopersGen) {
109 delete mTPCLoopersGen;
110 mTPCLoopersGen =
nullptr;
116#ifdef GENERATORS_WITH_TPCLOOPERS
117bool Generator::initTPCLoopersGen()
121 auto expandPathName = [](
const std::string&
path) {
122 TString expandedPath =
path;
123 gSystem->ExpandPathName(expandedPath);
124 return std::string(expandedPath.Data());
126 std::string model_pairs = expandPathName(loopersParam.model_pairs);
127 std::string model_compton = expandPathName(loopersParam.model_compton);
128 std::string nclxrate = expandPathName(loopersParam.nclxrate);
129 const std::string scaler_pair = expandPathName(loopersParam.scaler_pair);
130 const std::string scaler_compton = expandPathName(loopersParam.scaler_compton);
131 const std::string poisson = expandPathName(loopersParam.poisson);
132 const std::string gauss = expandPathName(loopersParam.gauss);
133 const auto& flat_gas = loopersParam.flat_gas;
134 const auto& colsys = loopersParam.colsys;
136 if (colsys !=
"PbPb" && colsys !=
"pp") {
137 LOG(warning) <<
"Automatic background loopers configuration supports only 'pp' and 'PbPb' systems.";
138 LOG(warning) <<
"Fast loopers generator will remain OFF.";
141 bool isContext = std::filesystem::exists(
"collisioncontext.root");
143 LOG(warning) <<
"Warning: No collisioncontext.root file found!";
144 LOG(warning) <<
"Loopers will be kept OFF.";
148 std::array<float, 2> multiplier = {loopersParam.multiplier[0], loopersParam.multiplier[1]};
149 unsigned int nLoopersPairs = loopersParam.fixedNLoopers[0];
150 unsigned int nLoopersCompton = loopersParam.fixedNLoopers[1];
151 const std::array<std::string, 3> models = {model_pairs, model_compton, nclxrate};
152 const std::array<std::string, 3> local_names = {
"WGANpair.onnx",
"WGANcompton.onnx",
"nclxrate.root"};
153 const std::array<bool, 3> isAlien = {models[0].starts_with(
"alien://"), models[1].starts_with(
"alien://"), models[2].starts_with(
"alien://")};
154 const std::array<bool, 3> isCCDB = {models[0].starts_with(
"ccdb://"), models[1].starts_with(
"ccdb://"), models[2].starts_with(
"ccdb://")};
155 if (std::any_of(isAlien.begin(), isAlien.end(), [](
bool v) { return v; })) {
157 TGrid::Connect(
"alien://");
159 LOG(fatal) <<
"AliEn connection failed, check token.";
163 for (
size_t i = 0;
i < models.size(); ++
i) {
164 if (isAlien[
i] && !TFile::Cp(models[
i].c_str(), local_names[
i].c_str())) {
165 LOG(fatal) <<
"Error: Model file " << models[
i] <<
" does not exist!";
170 if (std::any_of(isCCDB.begin(), isCCDB.end(), [](
bool v) { return v; })) {
172 ccdb.setURL(
"http://alice-ccdb.cern.ch");
174 auto& ccdb_api = ccdb.getCCDBAccessor();
175 for (
size_t i = 0;
i < models.size(); ++
i) {
177 auto model_path = models[
i].substr(7);
179 auto extension = model_path.find(
".onnx");
180 if (extension != std::string::npos) {
181 auto last_slash = model_path.find_last_of(
'/');
182 model_path = model_path.substr(0, last_slash);
184 std::map<std::string, std::string>
filter;
186 LOG(fatal) <<
"Error: issues in retrieving " << model_path <<
" from CCDB!";
192 model_pairs = isAlien[0] || isCCDB[0] ? local_names[0] : model_pairs;
193 model_compton = isAlien[1] || isCCDB[1] ? local_names[1] : model_compton;
194 nclxrate = isAlien[2] || isCCDB[2] ? local_names[2] : nclxrate;
197 mTPCLoopersGen =
new o2::eventgen::GenTPCLoopers(model_pairs, model_compton, poisson, gauss, scaler_pair, scaler_compton);
198 const auto& intrate = loopersParam.intrate;
202 mTPCLoopersGen->SetRate(nclxrate, (colsys ==
"PbPb") ?
true : false, intrate);
203 mTPCLoopersGen->SetAdjust(loopersParam.adjust_flatgas);
208 mTPCLoopersGen->SetNLoopers(nLoopersPairs, nLoopersCompton);
209 mTPCLoopersGen->SetMultiplier(multiplier);
211 LOG(info) <<
"TPC Loopers generator initialized successfully";
212 }
catch (
const std::exception& e) {
213 LOG(error) <<
"Failed to initialize TPC Loopers generator: " << e.what();
214 delete mTPCLoopersGen;
215 mTPCLoopersGen =
nullptr;
238#ifdef GENERATORS_WITH_TPCLOOPERS
239 if (mAddTPCLoopers) {
240 if (!mTPCLoopersGen) {
241 LOG(error) <<
"Loopers generator not initialized";
246 if (!mTPCLoopersGen->generateEvent()) {
247 LOG(error) <<
"Failed to generate loopers event";
250 if (mTPCLoopersGen->getNLoopers() == 0) {
251 LOG(warning) <<
"No loopers generated for this event";
254 const auto& looperParticles = mTPCLoopersGen->importParticles();
255 if (looperParticles.empty()) {
256 LOG(error) <<
"Failed to import loopers particles";
262 LOG(
debug) <<
"Added " << looperParticles.size() <<
" looper particles";
283 mSubGeneratorId = -1;
287 LOG(error) <<
"ReadEvent failed in generateEvent";
293 LOG(error) <<
"ReadEvent failed in importParticles";
299 LOG(error) <<
"ReadEvent failed in finalizeEvent";
303 if (mSubGeneratorsIdToDesc.empty() && mSubGeneratorId > -1) {
304 LOG(fatal) <<
"ReadEvent failed because no SubGenerator description given";
307 if (!mSubGeneratorsIdToDesc.empty() && mSubGeneratorId < 0) {
308 LOG(fatal) <<
"ReadEvent failed because SubGenerator description given but sub-generator not set";
322 LOG(error) <<
"ReadEvent failed in addTracks";
327 auto header = primGen->GetEvent();
330 LOG(fatal) <<
"MC event header is not a 'o2::dataformats::MCEventHeader' object";
334 updateSubGeneratorInformation(o2header);
349 LOG(fatal) <<
"PrimaryGenerator is not a o2::eventgen::PrimaryGenerator";
355 o2primGen->
AddTrack(particle.GetPdgCode(),
362 particle.GetMother(0),
363 particle.GetMother(1),
364 particle.GetDaughter(0),
365 particle.GetDaughter(1),
369 particle.GetWeight(),
370 (TMCProcess)particle.GetUniqueID(),
371 particle.GetStatusCode());
443 if (subGeneratorId < 0) {
444 LOG(fatal) <<
"Sub-generator IDs must be >= 0, instead, passed value is " << subGeneratorId;
446 mSubGeneratorsIdToDesc.insert({subGeneratorId, subGeneratorDescription});
453 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
GLsizei const GLchar *const * path
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"