21#include "FairPrimaryGenerator.h"
22#include <fairlogger/Logger.h>
24#include "TClonesArray.h"
37unsigned int Generator::gTotalNEvents = 0;
47#ifdef GENERATORS_WITH_TPCLOOPERS
50 if (!loopersParam.loopersVeto) {
51 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
53 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
55 if (initTPCLoopersGen()) {
56 mAddTPCLoopers = kTRUE;
59 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
63 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
76#ifdef GENERATORS_WITH_TPCLOOPERS
79 if (!loopersParam.loopersVeto) {
80 bool transport = (simConfig.getMCEngine() !=
"O2TrivialMCEngine");
82 bool tpcActive = (std::find(simConfig.getReadoutDetectors().begin(), simConfig.getReadoutDetectors().end(),
"TPC") != simConfig.getReadoutDetectors().end());
84 if (initTPCLoopersGen()) {
85 mAddTPCLoopers = kTRUE;
88 LOG(info) <<
"TPC not active in readout detectors: loopers fast generator disabled.";
92 LOG(info) <<
"Loopers fast generator turned OFF with veto flag.";
98#ifdef GENERATORS_WITH_TPCLOOPERS
99bool Generator::initTPCLoopersGen()
103 std::string model_pairs = gSystem->ExpandPathName(loopersParam.model_pairs.c_str());
104 std::string model_compton = gSystem->ExpandPathName(loopersParam.model_compton.c_str());
105 std::string nclxrate = gSystem->ExpandPathName(loopersParam.nclxrate.c_str());
106 const auto& scaler_pair = gSystem->ExpandPathName(loopersParam.scaler_pair.c_str());
107 const auto& scaler_compton = gSystem->ExpandPathName(loopersParam.scaler_compton.c_str());
108 const auto& poisson = gSystem->ExpandPathName(loopersParam.poisson.c_str());
109 const auto& gauss = gSystem->ExpandPathName(loopersParam.gauss.c_str());
110 const auto& flat_gas = loopersParam.flat_gas;
111 const auto& colsys = loopersParam.colsys;
113 if (colsys !=
"PbPb" && colsys !=
"pp") {
114 LOG(warning) <<
"Automatic background loopers configuration supports only 'pp' and 'PbPb' systems.";
115 LOG(warning) <<
"Fast loopers generator will remain OFF.";
118 bool isContext = std::filesystem::exists(
"collisioncontext.root");
120 LOG(warning) <<
"Warning: No collisioncontext.root file found!";
121 LOG(warning) <<
"Loopers will be kept OFF.";
125 std::array<float, 2> multiplier = {loopersParam.multiplier[0], loopersParam.multiplier[1]};
126 unsigned int nLoopersPairs = loopersParam.fixedNLoopers[0];
127 unsigned int nLoopersCompton = loopersParam.fixedNLoopers[1];
128 const std::array<std::string, 3> models = {model_pairs, model_compton, nclxrate};
129 const std::array<std::string, 3> local_names = {
"WGANpair.onnx",
"WGANcompton.onnx",
"nclxrate.root"};
130 const std::array<bool, 3> isAlien = {models[0].starts_with(
"alien://"), models[1].starts_with(
"alien://"), models[2].starts_with(
"alien://")};
131 const std::array<bool, 3> isCCDB = {models[0].starts_with(
"ccdb://"), models[1].starts_with(
"ccdb://"), models[2].starts_with(
"ccdb://")};
132 if (std::any_of(isAlien.begin(), isAlien.end(), [](
bool v) { return v; })) {
134 TGrid::Connect(
"alien://");
136 LOG(fatal) <<
"AliEn connection failed, check token.";
140 for (
size_t i = 0;
i < models.size(); ++
i) {
141 if (isAlien[
i] && !TFile::Cp(models[
i].c_str(), local_names[
i].c_str())) {
142 LOG(fatal) <<
"Error: Model file " << models[
i] <<
" does not exist!";
147 if (std::any_of(isCCDB.begin(), isCCDB.end(), [](
bool v) { return v; })) {
149 ccdb.setURL(
"http://alice-ccdb.cern.ch");
151 auto& ccdb_api = ccdb.getCCDBAccessor();
152 for (
size_t i = 0;
i < models.size(); ++
i) {
154 auto model_path = models[
i].substr(7);
156 auto extension = model_path.find(
".onnx");
157 if (extension != std::string::npos) {
158 auto last_slash = model_path.find_last_of(
'/');
159 model_path = model_path.substr(0, last_slash);
161 std::map<std::string, std::string>
filter;
163 LOG(fatal) <<
"Error: issues in retrieving " << model_path <<
" from CCDB!";
169 model_pairs = isAlien[0] || isCCDB[0] ? local_names[0] : model_pairs;
170 model_compton = isAlien[1] || isCCDB[1] ? local_names[1] : model_compton;
171 nclxrate = isAlien[2] || isCCDB[2] ? local_names[2] : nclxrate;
174 mTPCLoopersGen = std::make_unique<o2::eventgen::GenTPCLoopers>(model_pairs, model_compton, poisson, gauss, scaler_pair, scaler_compton);
175 const auto& intrate = loopersParam.intrate;
179 mTPCLoopersGen->SetRate(nclxrate, (colsys ==
"PbPb") ?
true : false, intrate);
180 mTPCLoopersGen->SetAdjust(loopersParam.adjust_flatgas);
185 mTPCLoopersGen->SetNLoopers(nLoopersPairs, nLoopersCompton);
186 mTPCLoopersGen->SetMultiplier(multiplier);
188 LOG(info) <<
"TPC Loopers generator initialized successfully";
189 }
catch (
const std::exception& e) {
190 LOG(error) <<
"Failed to initialize TPC Loopers generator: " << e.what();
191 mTPCLoopersGen.reset();
213#ifdef GENERATORS_WITH_TPCLOOPERS
214 if (mAddTPCLoopers) {
215 if (!mTPCLoopersGen) {
216 LOG(error) <<
"Loopers generator not initialized";
221 if (!mTPCLoopersGen->generateEvent()) {
222 LOG(error) <<
"Failed to generate loopers event";
225 if (mTPCLoopersGen->getNLoopers() == 0) {
226 LOG(warning) <<
"No loopers generated for this event";
229 const auto& looperParticles = mTPCLoopersGen->importParticles();
230 if (looperParticles.empty()) {
231 LOG(error) <<
"Failed to import loopers particles";
237 LOG(
debug) <<
"Added " << looperParticles.size() <<
" looper particles";
258 mSubGeneratorId = -1;
262 LOG(error) <<
"ReadEvent failed in generateEvent";
268 LOG(error) <<
"ReadEvent failed in importParticles";
274 LOG(error) <<
"ReadEvent failed in finalizeEvent";
278 if (mSubGeneratorsIdToDesc.empty() && mSubGeneratorId > -1) {
279 LOG(fatal) <<
"ReadEvent failed because no SubGenerator description given";
282 if (!mSubGeneratorsIdToDesc.empty() && mSubGeneratorId < 0) {
283 LOG(fatal) <<
"ReadEvent failed because SubGenerator description given but sub-generator not set";
297 LOG(error) <<
"ReadEvent failed in addTracks";
302 auto header = primGen->GetEvent();
305 LOG(fatal) <<
"MC event header is not a 'o2::dataformats::MCEventHeader' object";
309 updateSubGeneratorInformation(o2header);
324 LOG(fatal) <<
"PrimaryGenerator is not a o2::eventgen::PrimaryGenerator";
330 o2primGen->
AddTrack(particle.GetPdgCode(),
337 particle.GetMother(0),
338 particle.GetMother(1),
339 particle.GetDaughter(0),
340 particle.GetDaughter(1),
344 particle.GetWeight(),
345 (TMCProcess)particle.GetUniqueID(),
346 particle.GetStatusCode());
418 if (subGeneratorId < 0) {
419 LOG(fatal) <<
"Sub-generator IDs must be >= 0, instead, passed value is " << subGeneratorId;
421 mSubGeneratorsIdToDesc.insert({subGeneratorId, subGeneratorDescription});
428 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"