Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of hybrid generator #13699

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Generators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ o2_add_library(Generators
src/GeneratorTGenerator.cxx
src/GeneratorExternalParam.cxx
src/GeneratorFromFile.cxx
src/GeneratorHybrid.cxx
src/GeneratorHybridParam.cxx
src/GeneratorFromO2KineParam.cxx
src/GeneratorFileOrCmd.cxx
src/GeneratorFileOrCmdParam.cxx
Expand Down Expand Up @@ -68,6 +70,8 @@ set(headers
include/Generators/GeneratorTGenerator.h
include/Generators/GeneratorExternalParam.h
include/Generators/GeneratorFromFile.h
include/Generators/GeneratorHybrid.h
include/Generators/GeneratorHybridParam.h
include/Generators/GeneratorFromO2KineParam.h
include/Generators/GeneratorFileOrCmd.h
include/Generators/GeneratorFileOrCmdParam.h
Expand Down
8 changes: 8 additions & 0 deletions Generators/include/Generators/BoxGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Generators/Generator.h"
#include "TParticle.h"
#include <vector>
#include <Generators/BoxGunParam.h>

namespace o2::eventgen
{
Expand Down Expand Up @@ -45,6 +46,13 @@ class BoxGenerator : public Generator
SetPhiRange(phimin, phimax);
}

BoxGenerator(BoxGenConfig const& config) : mPDG{config.pdg}, mMult{config.number}
{
SetEtaRange(config.eta[0], config.eta[1]);
SetPRange(config.prange[0], config.prange[1]);
SetPhiRange(config.phirange[0], config.phirange[1]);
}

void SetPRange(Double32_t pmin = 0, Double32_t pmax = 10)
{
mPMin = pmin;
Expand Down
8 changes: 8 additions & 0 deletions Generators/include/Generators/BoxGunParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ struct BoxGunParam : public o2::conf::ConfigurableParamHelper<BoxGunParam> {
O2ParamDef(BoxGunParam, "BoxGun");
};

struct BoxGenConfig {
int pdg = 211; // which particle (default pion); could make this an enum
int number = 10; // how many particles
double eta[2] = {-1, 1}; // eta range
double prange[2] = {0.1, 5}; // energy range min, max in GeV
double phirange[2] = {0., 360.}; // phi range
};

} // end namespace eventgen
} // end namespace o2

Expand Down
5 changes: 5 additions & 0 deletions Generators/include/Generators/GeneratorExternalParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ struct GeneratorExternalParam : public o2::conf::ConfigurableParamHelper<Generat
O2ParamDef(GeneratorExternalParam, "GeneratorExternal");
};

struct ExternalGenConfig {
std::string fileName = "";
std::string funcName = "";
};

} // end namespace eventgen
} // end namespace o2

Expand Down
3 changes: 3 additions & 0 deletions Generators/include/Generators/GeneratorFileOrCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct GeneratorFileOrCmd {
* function so as to better facilitate changes. */
void setup(const GeneratorFileOrCmdParam& param,
const conf::SimConfig& config);
// Configure with local parameters
void setup(const FileOrCmdGenConfig& param,
const conf::SimConfig& config);
/**
* Set command to execute in bacground rather than reading from
* existing file(s)
Expand Down
5 changes: 5 additions & 0 deletions Generators/include/Generators/GeneratorFileOrCmdParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ struct GeneratorFileOrCmdParam : public o2::conf::ConfigurableParamHelper<Genera
O2ParamDef(GeneratorFileOrCmdParam, "GeneratorFileOrCmd");
};

struct FileOrCmdGenConfig {
std::string fileNames = "";
std::string cmd = ""; // Program command line to spawn
};

} // end namespace eventgen
} // end namespace o2

Expand Down
4 changes: 4 additions & 0 deletions Generators/include/Generators/GeneratorFromFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "FairGenerator.h"
#include "Generators/Generator.h"
#include "Generators/GeneratorFromO2KineParam.h"
#include <TRandom3.h>
#include <TGrid.h>

Expand Down Expand Up @@ -69,6 +70,7 @@ class GeneratorFromO2Kine : public o2::eventgen::Generator
public:
GeneratorFromO2Kine() = default;
GeneratorFromO2Kine(const char* name);
GeneratorFromO2Kine(O2KineGenConfig const& pars);

bool Init() override;

Expand Down Expand Up @@ -100,6 +102,8 @@ class GeneratorFromO2Kine : public o2::eventgen::Generator
unsigned int mRngSeed = 0; //! randomizer seed, 0 for random value
bool mRandomPhi = false; //! whether we want to randomize the phi angle of the particles
TGrid* mAlienInstance = nullptr; // a cached connection to TGrid (needed for Alien locations)
bool mGlobal = true; // whether to use the global configuration
std::unique_ptr<O2KineGenConfig> mConfig; //! Local configuration of the generator

std::unique_ptr<o2::dataformats::MCEventHeader> mOrigMCEventHeader; //! the MC event header of the original file

Expand Down
10 changes: 10 additions & 0 deletions Generators/include/Generators/GeneratorFromO2KineParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ struct GeneratorFromO2KineParam : public o2::conf::ConfigurableParamHelper<Gener
O2ParamDef(GeneratorFromO2KineParam, "GeneratorFromO2Kine");
};

struct O2KineGenConfig {
bool skipNonTrackable = true;
bool continueMode = false;
bool roundRobin = false; // read events with period boundary conditions
bool randomize = false; // randomize the order of events
unsigned int rngseed = 0; // randomizer seed, 0 for random value
bool randomphi = false; // randomize phi angle
std::string fileName = ""; // filename to read from - takes precedence over SimConfig if given
};

} // end namespace eventgen
} // end namespace o2

Expand Down
6 changes: 6 additions & 0 deletions Generators/include/Generators/GeneratorHepMC.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Generators/Generator.h"
#include "Generators/GeneratorFileOrCmd.h"
#include "Generators/GeneratorHepMCParam.h"
#include "Generators/GeneratorFileOrCmdParam.h"

#ifdef GENERATORS_WITH_HEPMC3_DEPRECATED
namespace HepMC
Expand Down Expand Up @@ -69,6 +70,10 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd
void setup(const GeneratorFileOrCmdParam& param0,
const GeneratorHepMCParam& param,
const conf::SimConfig& config);
// Generator configuration from external local parameters
void setup(const FileOrCmdGenConfig& param0,
const HepMCGenConfig& param,
const conf::SimConfig& config);
/**
* Generate a single event. The event is read in from the current
* input file. Returns false if a new event could not be read.
Expand All @@ -83,6 +88,7 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd

/** setters **/
void setEventsToSkip(uint64_t val) { mEventsToSkip = val; };
void setVersion(const int& ver) { mVersion = ver; };

protected:
/** copy constructor **/
Expand Down
8 changes: 8 additions & 0 deletions Generators/include/Generators/GeneratorHepMCParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorH
O2ParamDef(GeneratorHepMCParam, "HepMC");
};

struct HepMCGenConfig {
// Same parameters as GeneratorHepMCParam
int version = 0;
uint64_t eventsToSkip = 0;
std::string fileName = "";
bool prune = false;
};

} // end namespace eventgen
} // end namespace o2

Expand Down
90 changes: 90 additions & 0 deletions Generators/include/Generators/GeneratorHybrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

/// \author M. Giacalone - October 2024

#ifndef ALICEO2_EVENTGEN_GENERATORHYBRID_H_
#define ALICEO2_EVENTGEN_GENERATORHYBRID_H_

#include "Generators/Generator.h"
#include "Generators/BoxGenerator.h"
#include <Generators/GeneratorPythia8.h>
#include <Generators/GeneratorHepMC.h>
#include <Generators/GeneratorFromFile.h>
#include "SimulationDataFormat/MCEventHeader.h"
#include "SimulationDataFormat/MCGenProperties.h"
#include "SimulationDataFormat/ParticleStatus.h"
#include "Generators/GeneratorHybridParam.h"
#include "Generators/GeneratorHepMCParam.h"
#include "Generators/GeneratorPythia8Param.h"
#include "Generators/GeneratorFileOrCmdParam.h"
#include "Generators/GeneratorFromO2KineParam.h"
#include "Generators/GeneratorExternalParam.h"
#include <TRandom3.h>
#include "CommonUtils/ConfigurationMacroHelper.h"
#include "FairGenerator.h"
#include <DetectorsBase/Stack.h>
#include <SimConfig/SimConfig.h>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/writer.h>
#include "TBufferJSON.h"

namespace o2
{
namespace eventgen
{

class GeneratorHybrid : public Generator
{

public:
GeneratorHybrid() = default;
GeneratorHybrid(const std::string& inputgens);
~GeneratorHybrid() = default;

Bool_t Init() override;
Bool_t generateEvent() override;
Bool_t importParticles() override;

Bool_t parseJSON(const std::string& path);
template <typename T>
std::string jsonValueToString(const T& value);

private:
o2::eventgen::Generator* currentgen = nullptr;
std::vector<std::unique_ptr<o2::eventgen::Generator>> gens;
const std::vector<std::string> generatorNames = {"extkinO2", "boxgen", "external", "hepmc", "pythia8", "pythia8pp", "pythia8hi", "pythia8hf", "pythia8powheg"};
std::vector<std::string> mInputGens;
std::vector<std::string> mGens;
std::vector<std::string> mConfigs;
std::vector<std::string> mConfsPythia8;

// Parameters configurations
std::vector<std::unique_ptr<o2::eventgen::BoxGenConfig>> mBoxGenConfigs;
std::vector<std::unique_ptr<o2::eventgen::Pythia8GenConfig>> mPythia8GenConfigs;
std::vector<std::unique_ptr<o2::eventgen::O2KineGenConfig>> mO2KineGenConfigs;
std::vector<std::unique_ptr<o2::eventgen::ExternalGenConfig>> mExternalGenConfigs;
std::vector<std::unique_ptr<o2::eventgen::FileOrCmdGenConfig>> mFileOrCmdGenConfigs;
std::vector<std::unique_ptr<o2::eventgen::HepMCGenConfig>> mHepMCGenConfigs;

bool mRandomize = false;
std::vector<int> mFractions;
int mseqCounter = 0;
int mCurrentFraction = 0;
int mIndex = 0;
};

} // namespace eventgen
} // namespace o2

#endif
40 changes: 40 additions & 0 deletions Generators/include/Generators/GeneratorHybridParam.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

/// \author M. Giacalone - October 2024

#ifndef ALICEO2_EVENTGEN_GENERATORHYBRIDPARAM_H_
#define ALICEO2_EVENTGEN_GENERATORHYBRIDPARAM_H_

#include "CommonUtils/ConfigurableParam.h"
#include "CommonUtils/ConfigurableParamHelper.h"

namespace o2
{
namespace eventgen
{

/**
** a parameter class/struct to keep the settings of
** the Hybrid event generator and
** allow the user to modify them
**/

struct GeneratorHybridParam : public o2::conf::ConfigurableParamHelper<GeneratorHybridParam> {
std::string configFile = ""; // JSON configuration file for the generators
bool randomize = false; // randomize the order of the generators, if not generator using fractions
O2ParamDef(GeneratorHybridParam, "GeneratorHybrid");
};

} // end namespace eventgen
} // end namespace o2

#endif // ALICEO2_EVENTGEN_GENERATORHYBRIDPARAM_H_
5 changes: 5 additions & 0 deletions Generators/include/Generators/GeneratorPythia8.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Generators/Generator.h"
#include "Pythia8/Pythia.h"
#include <functional>
#include "Generators/GeneratorPythia8Param.h"

namespace o2
{
Expand Down Expand Up @@ -89,6 +90,8 @@ class GeneratorPythia8 : public Generator
/** default constructor **/
GeneratorPythia8();
/** constructor **/
GeneratorPythia8(Pythia8GenConfig const& pars);
/** constructor **/
GeneratorPythia8(const Char_t* name, const Char_t* title = "ALICEo2 Pythia8 Generator");
/** destructor **/
~GeneratorPythia8() override = default;
Expand Down Expand Up @@ -282,6 +285,8 @@ class GeneratorPythia8 : public Generator
long mInitialRNGSeed = -1; // initial seed for Pythia random number state;
// will be transported to Pythia in the Init function through the Pythia::readString("Random:seed") mechanism.
// Value of -1 means unitialized; 0 will be time-dependent and values >1 <= MAX_SEED concrete reproducible seeding
bool mGlobalParam = true; // if true the use of the global parameters is foreseen, otherwise it means the parametric constructor has been called
std::unique_ptr<Pythia8GenConfig> mGenConfig; // local configuration

constexpr static long MAX_SEED = 900000000;

Expand Down
9 changes: 9 additions & 0 deletions Generators/include/Generators/GeneratorPythia8Param.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ struct GeneratorPythia8Param : public o2::conf::ConfigurableParamHelper<Generato
O2ParamDef(GeneratorPythia8Param, "GeneratorPythia8");
};

struct Pythia8GenConfig {
std::string config = "";
std::string hooksFileName = "";
std::string hooksFuncName = "";
bool includePartonEvent = false; // whether to keep the event before hadronization
std::string particleFilter = ""; // user particle filter
int verbose = 0; // verbose control (if > 0 may show more info messages about what is going on)
};

} // end namespace eventgen
} // end namespace o2

Expand Down
18 changes: 18 additions & 0 deletions Generators/src/GeneratorFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <fairlogger/Logger.h>
#include <SimConfig/SimConfig.h>
#include <Generators/GeneratorFromFile.h>
#include <Generators/GeneratorHybrid.h>
#include <Generators/GeneratorTParticle.h>
#include <Generators/GeneratorTParticleParam.h>
#ifdef GENERATORS_WITH_PYTHIA8
Expand All @@ -26,6 +27,7 @@
#endif
#include <Generators/GeneratorTGenerator.h>
#include <Generators/GeneratorExternalParam.h>
#include <Generators/GeneratorHybridParam.h>
#include "Generators/GeneratorFromO2KineParam.h"
#ifdef GENERATORS_WITH_HEPMC3
#include <Generators/GeneratorHepMC.h>
Expand Down Expand Up @@ -240,6 +242,22 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
primGen->AddGenerator(boxGen);
}
}
} else if (genconfig.compare("hybrid") == 0) { // hybrid using multiple generators
LOG(info) << "Init hybrid generator";
auto& hybridparam = GeneratorHybridParam::Instance();
std::string config = hybridparam.configFile;
// check if config string points to an existing and not empty file
if (config.empty()) {
LOG(fatal) << "No configuration file provided for hybrid generator";
return;
}
// check if file named config exists and it's not empty
else if (gSystem->AccessPathName(config.c_str())) {
LOG(fatal) << "Configuration file for hybrid generator does not exist";
return;
}
auto hybrid = new o2::eventgen::GeneratorHybrid(config);
primGen->AddGenerator(hybrid);
} else {
LOG(fatal) << "Invalid generator";
}
Expand Down
Loading
Loading