Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a911276
Introduce pre-defined keys for MC event header info
cholmcc Sep 19, 2023
dadc5c6
Export _full_ header information to MC event header
cholmcc Sep 19, 2023
2f36720
Full header read-in and external program
cholmcc Sep 19, 2023
ae319c5
New generator GeneratorTParticle
cholmcc Sep 19, 2023
0558c4b
Please consider the following formatting changes
alibuild Sep 19, 2023
a94f5f1
Merge pull request #1 from alibuild/alibot-cleanup-11913
cholmcc Sep 19, 2023
2e413a3
Fixes for copyright stuff - sigh!
cholmcc Sep 19, 2023
3d657ef
One more comment
cholmcc Sep 19, 2023
f423011
Please consider the following formatting changes
alibuild Sep 19, 2023
7313685
Merge pull request #2 from alibuild/alibot-cleanup-11913
cholmcc Sep 19, 2023
233e2e0
Whitespace
cholmcc Sep 19, 2023
d65a0c9
untabify
cholmcc Sep 19, 2023
ee59676
Merge branch 'AliceO2Group:dev' into cholmcc_generatos
cholmcc Oct 6, 2023
6f6f2c3
Added documentation
cholmcc Oct 6, 2023
05ad595
Changed key prefix
cholmcc Oct 6, 2023
5115a6e
Add new sub-dirs
cholmcc Oct 6, 2023
595d843
Formatting fixes
cholmcc Oct 6, 2023
4715d37
Renamed _script_ (not macro) to end in .macro (sigh)
cholmcc Oct 6, 2023
0434a6e
Renamed _script_ (not macro) to end in .macro (sigh)
cholmcc Oct 6, 2023
e1db4b0
Fix formatting
cholmcc Oct 6, 2023
eb308c0
Fix formatting
cholmcc Oct 6, 2023
67e5ba9
Fix formatting
cholmcc Oct 6, 2023
f27aaf8
Fix formatting
cholmcc Oct 6, 2023
5b62d47
Fix formatting - that checker is ridiculously pedantic about things a…
cholmcc Oct 6, 2023
43ea6d4
Stupid annoying formatting checker - it is really f**cking ridiculous…
cholmcc Oct 6, 2023
73c7490
Undo last faulty commit and fix formatting
cholmcc Oct 6, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,67 @@ namespace dataformats

class GeneratorHeader;

/** Common keys for information in MC event header */
struct MCInfoKeys {
/** @{
@name HepMC3 heavy-ion fields */
static constexpr const char* impactParameter = "Bimpact";
static constexpr const char* nPart = "Npart";
static constexpr const char* nPartProjectile = "Npart_proj";
static constexpr const char* nPartTarget = "Npart_targ";
static constexpr const char* nColl = "Ncoll";
static constexpr const char* nCollHard = "Ncoll_hard";
static constexpr const char* nCollNNWounded = "NColl_NNw";
static constexpr const char* nCollNWoundedN = "NColl_NwN";
static constexpr const char* nCollNWoundedNwounded = "NColl_NwNW";
static constexpr const char* planeAngle = "eventPsi";
static constexpr const char* sigmaInelNN = "sigmaInelNN";
static constexpr const char* centrality = "centrality";
static constexpr const char* nSpecProjectileProton = "Nspec_proj_p";
static constexpr const char* nSpecProjectileNeutron = "Nspec_proj_n";
static constexpr const char* nSpecTargetProton = "Nspec_targ_p";
static constexpr const char* nSpecTargetNeutron = "Nspec_targ_n";
/** @} */
/** @{
@name HepMC3 PDF information

In principle a header can have many of these. In that case,
each set should be prefixed with "_<X>" where "<X>" is a
serial number.
*/
static constexpr const char* pdfParton1Id = "pdf_parton_1_id";
static constexpr const char* pdfParton2Id = "pdf_parton_2_id";
static constexpr const char* pdfX1 = "pdf_x1";
static constexpr const char* pdfX2 = "pdf_x2";
static constexpr const char* pdfScale = "pdf_scale";
static constexpr const char* pdfXF1 = "pdf_par_x1";
static constexpr const char* pdfXF2 = "pdf_par_x2";
static constexpr const char* pdfCode1 = "pdf_lhc_1_id";
static constexpr const char* pdfCode2 = "pdf_lhc_2_id";
/** @} */
/** @{
@name HepMC3 cross-section information

In principle we can have one cross section per weight. In that
case, each should be post-fixed by "_<X>" where "<X>" is a
serial number. These should then matcht possible names of
weights.
*/
static constexpr const char* acceptedEvents = "accepted_events";
static constexpr const char* attemptedEvents = "attempted_events";
static constexpr const char* xSection = "cross_section";
static constexpr const char* xSectionError = "cross_section_error";
/** @} */
/** @{
@name Common fields */
static constexpr const char* generator = "generator";
static constexpr const char* generatorVersion = "version";
static constexpr const char* processName = "processName";
static constexpr const char* processCode = "processCode";
static constexpr const char* weight = "weight";
/** @} */
};

/*****************************************************************/
/*****************************************************************/

Expand Down
4 changes: 4 additions & 0 deletions Generators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ o2_add_library(Generators
src/GenCosmicsParam.cxx
src/GeneratorFactory.cxx
src/GeneratorGeantinos.cxx
src/GeneratorTParticle.cxx
src/GeneratorTParticleParam.cxx
$<$<BOOL:${pythia6_FOUND}>:src/GeneratorPythia6.cxx>
$<$<BOOL:${pythia6_FOUND}>:src/GeneratorPythia6Param.cxx>
$<$<BOOL:${pythia_FOUND}>:src/GeneratorPythia8.cxx>
Expand Down Expand Up @@ -79,6 +81,8 @@ set(headers
include/Generators/QEDGenParam.h
include/Generators/GenCosmicsParam.h
include/Generators/GeneratorGeantinos.h
include/Generators/GeneratorTParticle.h
include/Generators/GeneratorTParticleParam.h
)

if (pythia6_FOUND)
Expand Down
7 changes: 7 additions & 0 deletions Generators/include/Generators/GeneratorHepMC.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class GeneratorHepMC : public Generator
/** setters **/
void setVersion(Int_t val) { mVersion = val; };
void setFileName(std::string val) { mFileName = val; };
void setProgCmd(std::string val) { mProgCmd = val; };
void setEventsToSkip(uint64_t val) { mEventsToSkip = val; };
void setNEvents(unsigned int val) { mNEvents = val; }

protected:
/** copy constructor **/
Expand All @@ -77,11 +79,16 @@ class GeneratorHepMC : public Generator
const HepMC3::FourVector getBoostedVector(const HepMC3::FourVector& vector, Double_t boost);
#endif

/** methods that can be overridded **/
void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override;

/** HepMC interface **/
std::ifstream mStream; //!
std::string mFileName;
std::string mProgCmd;
Int_t mVersion;
uint64_t mEventsToSkip;
unsigned int mNEvents;
#ifdef GENERATORS_WITH_HEPMC3_DEPRECATED
HepMC::Reader* mReader; //!
HepMC::GenEvent* mEvent; //!
Expand Down
1 change: 1 addition & 0 deletions Generators/include/Generators/GeneratorHepMCParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace eventgen

struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorHepMCParam> {
std::string fileName = "";
std::string progCmd = ""; // Program command line to spawn, must write HepMC on stdout
int version = 2;
uint64_t eventsToSkip = 0;
O2ParamDef(GeneratorHepMCParam, "HepMC");
Expand Down
114 changes: 114 additions & 0 deletions Generators/include/Generators/GeneratorTParticle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2023-2099 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: Christian Holm Christensen <cholm@nbi.dk>
#ifndef ALICEO2_GENERATORTPARTICLE_H_
#define ALICEO2_GENERATORTPARTICLE_H_
#include <FairGenerator.h>
#include <Generators/Generator.h>
#include <list>

// Forward decls
class TChain;
class TParticle;
class TClonesArray;

namespace o2
{
namespace eventgen
{
/// A class that reads in particles of class @c TParticle from a
/// branch in a @c TChain.
///
/// Optionally, a program that generates such a @c TTree can be
/// spawn, and the @c TParticles written to a file from which this
/// object reads them in. This is done with
///
/// --configKeyValues "TParticle.progCmd=<eg program and options>"
///
/// which will execute the specified EG program with the given
/// options. The EG program _must_ support the command line options
///
/// -n NUMBER Number of events to generate
/// -o FILENAME Name of file to write to
///
/// The tree name and particle branch names can be configured.
///
/// --configKeyValues "TParticle.treeName=T,TParticle.branchName=P"
///
/// File(s) to read are also configurable
///
/// --configKeyValues "TParticle.fileNames=foo.root,bar.root"
///
class GeneratorTParticle : public Generator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit unsure about this one: We already have a generator "GeneratorFromFile" which reads TParticles from a Run2 - like ROOT file.

A quick check if we still need this one would good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a difference between the GeneratorFromFile and GeneratorTParticle, mainly in that GeneratorTParticle

  • is more flexible
  • can read data from external program rather than from file
  • can read from a TChain of files
  • assumes all events are in the same TTree, unlike GeneratorFromFile which assumes there's one TTree for each event in the TDirectory named Event<_event-no_> in the single input file.

As such, the two classes GeneratorFromFile and `GeneratorTParticle serves different needs

  • GeneratorFromFile is designed to read from an AliROOT Kinematics.root file
  • GeneratorTParticle is designed to read from a general ROOT event tree, for example produced by a TGenerator.

Hope that clarifies the differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and another difference. Using the key progCmd, one could execute a TGenerator as a background process. Say we have the script MyEG.C

class MyGenerator : public TGenerator 
{
   ...
};
 
void MyEG(const char* output, Int_t nEv, Int_t seed, Float_t bmax)
{
   gRandom->SetSeed(seed);
  
   MyGenerator generator(bMax);
  TFile* file = TFile::Open(output, "RECREATE");
  TTree* tree = new TTree("T");
  TClonesArray* particles = new TClonesArray("TParticle");
  tree->Branch("Particles", &particles);
  tree->SetDirectory(file);

  for (Int_t iEv = 0; iEv < nEv; iEv++) {
     generator.GenerateEvent();
     generator.ImportParticles(particles);
     tree->Fill();
     tree->AutoSave("SaveSelf FlushBaskets Overwrite");
  }
  file->Write();
  file->Close();
}

and a simple script like myeg.sh

#!/bin/sh 

nev=1
out=myeg.root 
seed=0
bmax=0

while test $# -gt 0; do 
   case $1 in 
   -n|--nevents) nev=$2 ; shift ;;
   -o|--output)   out=$2 ; shift ;;
   -b|--bMax)     bmax=$2; shift;;  # Future proof 
   -s|--seed)       seed=$2; shift;;
  --help) echo "Usage: $0 [-n NEV] [-o OUTPUT] [-s SEED] [-b bmax]"; exit 0;;
  esac 
root MyEG.C++\( \"$out\",$nev,$seed,$bmax\)

then one can do

o2-sim -g tparticle --configKeyValues "TParticle.progCmd=myeg.sh"

which means all the old AliGenerators can be used more or less directly :-)

{
public:
/** CTOR */
GeneratorTParticle() = default;
/** CTOR */
GeneratorTParticle(const std::string& name)
: Generator(name.c_str(), "ALICEo2 TParticle Generator")
{
}
/** DTOR */
virtual ~GeneratorTParticle();

/** Initialize this generator. This will set up the chain.
Optionally, if a command line was specified by @c
TParticle.progCmd then that command line is executed in the
background and events are read from the output file of that
program */
Bool_t Init() override;

/** Read in the next entry from the chain. Returns false in
case of errors or no more entries to read. */
Bool_t generateEvent() override;

/** Import the read-in particles into the steer particle
stack */
Bool_t importParticles() override;

/** Set the names of files to read, separated by commas */
void setFileNames(const std::string& val);
/** Set the name of the tree in the files. The tree _must_
reside in the top-level directory of the files. */
void setTreeName(const std::string& val) { mTreeName = val; }
/** Set the branch name of the branch that holds a @c
TClonesArray of @c TParticle objects */
void setBranchName(const std::string& val) { mBranchName = val; }
/** Set child program command line to (optionally) execute */
void setProgCmd(const std::string& val) { mProgCmd = val; }
/** Set the number of events to generate. */
void setNEvents(unsigned int nev) { mNEvents = nev; }

protected:
std::string mTreeName = "T";
std::string mBranchName = "Particles";
std::string mProgCmd = "";
std::list<std::string> mFileNames;
unsigned int mNEvents = 0;
unsigned int mEntry = 0;
TChain* mChain;
TClonesArray* mTParticles;

void waitForData();

ClassDefOverride(GeneratorTParticle, 1);
};
} // namespace eventgen
} // namespace o2
#endif
// Local Variables:
// mode: C++
// End:
//
// EOF
//
41 changes: 41 additions & 0 deletions Generators/include/Generators/GeneratorTParticleParam.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023-2099 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: Christian Holm Christensen <cholm@nbi.dk>
#ifndef ALICEO2_EVENTGEN_GENERATORTPARTICLEPARAM_H_
#define ALICEO2_EVENTGEN_GENERATORTPARTICLEPARAM_H_

#include "CommonUtils/ConfigurableParam.h"
#include "CommonUtils/ConfigurableParamHelper.h"
#include <string>

namespace o2
{
namespace eventgen
{

/**
a parameter class/struct to keep the settings of the TGenerator
event generator and allow the user to modify them */
struct GeneratorTParticleParam : public o2::conf::ConfigurableParamHelper<GeneratorTParticleParam> {
std::string treeName = "T";
std::string branchName = "Particles";
std::string fileNames = "tparticle.root";
std::string progCmd = "";
O2ParamDef(GeneratorTParticleParam, "GeneratorTParticle");
};
} // end namespace eventgen
} // end namespace o2

#endif // ALICEO2_EVENTGEN_GENERATORHEPMCPARAM_H_
// Local Variables:
// mode: C++
// End:
21 changes: 20 additions & 1 deletion Generators/src/GeneratorFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <fairlogger/Logger.h>
#include <SimConfig/SimConfig.h>
#include <Generators/GeneratorFromFile.h>
#include <Generators/GeneratorTParticle.h>
#include <Generators/GeneratorTParticleParam.h>
#ifdef GENERATORS_WITH_PYTHIA6
#include <Generators/GeneratorPythia6.h>
#include <Generators/GeneratorPythia6Param.h>
Expand Down Expand Up @@ -86,6 +88,7 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair

o2::O2DatabasePDG::addALICEParticles(TDatabasePDG::Instance());
auto genconfig = conf.getGenerator();
LOG(info) << "** Generator to use: '" << genconfig << "'";
if (genconfig.compare("boxgen") == 0) {
// a simple "box" generator configurable via BoxGunparam
auto& boxparam = BoxGunParam::Instance();
Expand Down Expand Up @@ -156,16 +159,32 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
}
}
LOG(info) << "using external O2 kinematics";
} else if (genconfig.compare("tparticle") == 0) {
// External ROOT file(s) with tree of TParticle in clones array,
// or external program generating such a file
auto& param = GeneratorTParticleParam::Instance();
LOG(info) << "Init 'GeneratorTParticle' with the following parameters";
LOG(info) << param;
auto tgen = new o2::eventgen::GeneratorTParticle();
tgen->setFileNames(param.fileNames);
tgen->setProgCmd(param.progCmd);
tgen->setTreeName(param.treeName);
tgen->setBranchName(param.branchName);
tgen->setNEvents(conf.getNEvents());
primGen->AddGenerator(tgen);
#ifdef GENERATORS_WITH_HEPMC3
} else if (genconfig.compare("hepmc") == 0) {
// external HepMC file
// external HepMC file, or external program writing HepMC event
// records to standard output.
auto& param = GeneratorHepMCParam::Instance();
LOG(info) << "Init \'GeneratorHepMC\' with following parameters";
LOG(info) << param;
auto hepmcGen = new o2::eventgen::GeneratorHepMC();
hepmcGen->setFileName(param.fileName);
hepmcGen->setProgCmd(param.progCmd);
hepmcGen->setVersion(param.version);
hepmcGen->setEventsToSkip(param.eventsToSkip);
hepmcGen->setNEvents(conf.getNEvents());
primGen->AddGenerator(hepmcGen);
#endif
#ifdef GENERATORS_WITH_PYTHIA6
Expand Down
Loading