Skip to content

Commit

Permalink
Custom fifo names feature + jetscape usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
jackal1-66 authored and sawenzel committed Dec 19, 2024
1 parent 789968c commit ecec4fc
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Generators/include/Generators/GeneratorFileOrCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct GeneratorFileOrCmd {
* @return true if the temporary file name was generated
* successfully.
*/
virtual bool makeTemp();
virtual bool makeTemp(const bool&);
/**
* Remove the temporary file if it was set and it exists.
*
Expand Down
45 changes: 33 additions & 12 deletions Generators/src/GeneratorFileOrCmd.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,40 @@ bool GeneratorFileOrCmd::executeCmdLine(const std::string& cmd) const
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::makeTemp()
{
mFileNames.clear();
char buf[] = "generatorFifoXXXXXX";
auto fp = mkstemp(buf);
if (fp < 0) {
LOG(fatal) << "Failed to make temporary file: "
<< std::strerror(errno);
return false;
bool GeneratorFileOrCmd::makeTemp(const bool& fromName)
{
if (fromName) {
if (mFileNames.empty()) {
LOG(fatal) << "No file names to make temporary file from";
return false;
} else if (mFileNames.size() > 1) {
LOG(warning) << "More than one file name to make temporary file from";
LOG(warning) << "Using the first one: " << mFileNames.front();
LOG(warning) << "Removing all the others";
mFileNames.erase(++mFileNames.begin(), mFileNames.end());
} else {
LOG(debug) << "Making temporary file from: " << mFileNames.front();
}
std::ofstream ofs(mFileNames.front().c_str());
if (!ofs) {
LOG(fatal) << "Failed to create temporary file: " << mFileNames.front();
return false;
}
mTemporary = std::string(mFileNames.front());
ofs.close();
} else {
mFileNames.clear();
char buf[] = "generatorFifoXXXXXX";
auto fp = mkstemp(buf);
if (fp < 0) {
LOG(fatal) << "Failed to make temporary file: "
<< std::strerror(errno);
return false;
}
mTemporary = std::string(buf);
mFileNames.push_back(mTemporary);
close(fp);
}
mTemporary = std::string(buf);
mFileNames.push_back(mTemporary);
close(fp);
return true;
}
// -----------------------------------------------------------------
Expand Down
13 changes: 10 additions & 3 deletions Generators/src/GeneratorHepMC.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,16 @@ Bool_t GeneratorHepMC::Init()
// All of this can conviniently be achieved via a wrapper script
// around the actual EG program.
if (not mCmd.empty()) {
// Set filename to be a temporary name
if (not makeTemp()) {
return false;
if (mFileNames.empty()) {
// Set filename to be a temporary name
if (not makeTemp(false)) {
return false;
}
} else {
// Use the first filename as output for cmd line
if (not makeTemp(true)) {
return false;
}
}

// Make a fifo
Expand Down
13 changes: 10 additions & 3 deletions Generators/src/GeneratorTParticle.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ Bool_t GeneratorTParticle::Init()
mChain->SetBranchAddress(mBranchName.c_str(), &mTParticles);

if (not mCmd.empty()) {
// Set filename to be a temporary name
if (not makeTemp()) {
return false;
if (mFileNames.empty()) {
// Set filename to be a temporary name
if (not makeTemp(false)) {
return false;
}
} else {
// Use the first filename as output for cmd line
if (not makeTemp(true)) {
return false;
}
}

// Build command line, Assumes command line parameter
Expand Down
39 changes: 39 additions & 0 deletions run/SimExamples/HepMC_JETSCAPE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- doxy
\page refrunSimExamplesHepMC_JETSCAPE Example HepMC_JETSCAPE
/doxy -->

The usage of JETSCAPE with the O2 machinery is presented in this short manual.
An in-depth explanation of the mechanisms behind the HepMC(3) data handling can be found in the
HepMC_fifo folder of the MC examples. The scripts use the `cmd` parameter of `GeneratorHepMC`
to spawn the JETSCAPE generation via the `jetscape.sh` script. It is important to turn on the
HepMC3 output format in the xml configuration file, as done in jetscape_user_example.xml, otherwise
the simulation will not work.

# Scripts description

Two scripts are available to run the simulations
- **jetscape.sh** &rarr; starts the actual JETSCAPE generation
- **runo2sim.sh** &rarr; allows the generation of events using o2-sim

In addition an jetscape_user_example.xml file is provided to start JETSCAPE with user parameters.
The user could easily create scripts similar to the one provided for the EPOS4 tutorial for DPL or O2DPG
based simulations.

## jetscape.sh

It can be run without the help of the other scripts to simply generate an .hepmc file.
This example shows all the functionalities of the script (which are implemented in a similar way inside
the generation steering scripts). In particular the `-i` flag allows to provide the .xml user configuration file to JETSCAPE, `-s` feeds the generator with a user seed, and the HepMC output filename is set using the `-o` flag. The script edits automatically some specific parts of the provided input XML file.

## runo2sim.sh

This script works only with O2sim versions containing the FIFO custom name creation fix (the specific build will be added here in the future) otherwise it will crash or not complete the simulation.
Few flags are available to change the settings of the generation:
- **-m , --more** &rarr; feeds the simulation with advanced parameters provided to the configuration key flags
- **-n , --nevents** &rarr; changes the number of events in the .xml file or gets the one in the file if no events are provided
- **-i , --input** &rarr; .xml filename to feed JETSCAPE, no extension must be set in the filename
- **-j , --jobs** &rarr; sets the number of workers (jobs)
- **-h , --help** &rarr; prints usage instructions

The last few lines of the script contain the execution of o2-sim, so this part can be modified by the users following their requirements. It's important not to delete from the configuration keys `GeneratorFileOrCmd.cmd=$cmd -i $xml;GeneratorFileOrCmd.fileNames=test_out.hepmc;GeneratorFileOrCmd.outputSwitch=-o;GeneratorFileOrCmd.bMaxSwitch=none;GeneratorFileOrCmd.nEventsSwitch=none;` because the script might not work anymore, and it would be better to provide additional configurations via the -m flag.

71 changes: 71 additions & 0 deletions run/SimExamples/HepMC_JETSCAPE/jetscape.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/sh
# Script based on EPOS4 example
# This script is used to run JETSCAPE with the given XML file
# setting the seed and HepMC output filename. Contrary to the
# epos example, the HepMC output is generated in a custom named file
# not passing from the stdout.

xml="example"
seed=$RANDOM
hepmc="jetout.hepmc"

usage()
{
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-i,--input INPUT XML user file fed to JETSCAPE ($xml)
-o,--output OUTPUT HepMC output file ($hepmc)
-s,--seed SEED RNG seed ($seed)
-h,--help Print these instructions
-- Rest of command line sent to o2-sim
EOF
}

while test $# -gt 0 ; do
case $1 in
-i|--input) xml=$2 ; shift ;;
-o|--output) hepmc=$2 ; shift ;;
-s|--seed) seed=$2 ; shift ;;
-h|--help) usage; exit 0 ;;
esac
shift
done

if [ ! -f $xml.xml ]; then
echo "Error: Options file $xml.xml not found"
exit 1
fi

if [ $seed -eq 0 ]; then
echo "Seed can't be 0, random number will be used"
seed=$RANDOM
else
if grep -Fq "<seed>" $xml.xml; then
sed -i "/<seed>/c\ <seed>$seed</seed>" $xml.xml
else
sed -i "/<\/jetscape>/i\ <Random>\n <seed>$seed</seed>\n </Random>" $xml.xml
fi
echo "Seed set to $seed"
fi

# Check if hepmc output has been set
if [ ! -z "$hepmc" ]; then
# Remove extension
newhep=$(echo $hepmc | sed 's/.hepmc//')
if grep -Fq "<outputFilename>" $xml.xml; then
sed -i "/<outputFilename>/c\ <outputFilename>$newhep</outputFilename>" $xml.xml
else
sed -i "/<jetscape>/a\ <outputFilename>$newhep</outputFilename>" $xml.xml
fi
echo "HepMC output file set to $hepmc"
else
echo "Error: HepMC output file not set"
exit 2
fi

# Master XML file pulled directly from the JETSCAPE directory
runJetscape $xml.xml $JETSCAPE_ROOT/config/jetscape_master.xml
42 changes: 42 additions & 0 deletions run/SimExamples/HepMC_JETSCAPE/jetscape_user_example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>

<jetscape>

<nEvents>1000</nEvents>

<Random>
<seed>1</seed>
</Random>

<outputFilename>jetscape</outputFilename>

<!--Do not delete HepMC setting-->
<JetScapeWriterHepMC> on </JetScapeWriterHepMC>

<!-- Hard Process -->
<Hard>
<PythiaGun>
<pTHatMin>235</pTHatMin>
<pTHatMax>1000</pTHatMax>
<eCM>5020</eCM>
</PythiaGun>
</Hard>

<!--Eloss Modules -->
<Eloss>
<Matter>
<Q0> 1.0 </Q0>
<in_vac> 1 </in_vac>
<vir_factor> 0.25 </vir_factor>
<recoil_on> 0 </recoil_on>
<broadening_on> 0 </broadening_on>
<brick_med> 0 </brick_med>
</Matter>
</Eloss>

<!-- Jet Hadronization Module -->
<JetHadronization>
<name>colorless</name>
</JetHadronization>

</jetscape>
91 changes: 91 additions & 0 deletions run/SimExamples/HepMC_JETSCAPE/runo2sim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
#
# This is a simple simulation example showing how to
# start JETSCAPE generation automatically using cmd with hepmc output on FIFO
# and simultaneosly use o2-sim for transport

# JETSCAPE and O2 must be loaded
set -x
if [ ! "${JETSCAPE_ROOT}" ]; then
echo "This needs JETSCAPE loaded; alienv enter ..."
exit 1
fi

[ ! "${O2_ROOT}" ] && echo "Error: This needs O2 loaded" && exit 2

cmd="$PWD/jetscape.sh"
NEV=-1
more=""
xml="example"
JOBS=2

usage()
{
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-m,--more CONFIG More configurations ($more)
-n,--nevents EVENTS Number of events ($nev)
-i,--input INPUT XML configuration file fed to JETSCAPE ($xml)
-j,--jobs JOBS Number of jobs ($JOBS)
-h,--help Print these instructions
-- Rest of command line sent to o2-sim
COMMAND must be quoted if it contains spaces or other special
characters
Below follows the help output of o2-sim
EOF
}

if [ "$#" -lt 2 ]; then
echo "Running with default values"
fi

while test $# -gt 0 ; do
case $1 in
-m|--more) more="$2" ; shift ;;
-n|--nevents) NEV=$2 ; shift ;;
-i|--input) xml=$2 ; shift ;;
-j|--jobs) JOBS=$2 ; shift ;;
-h|--help) usage; o2-sim --help full ; exit 0 ;;
--) shift ; break ;;
*) echo "Unknown option '$1', did you forget '--'?" >/dev/stderr
exit 3
;;
esac
shift
done

echo "XML User file: $xml"

if [ ! -f $xml.xml ]; then
echo "Error: Options file $xml.xml not found"
exit 4
fi

# Set number of events in the XML file
if [ ! $NEV -eq -1 ]; then
echo "Setting number of events to $NEV"
if grep -Fq "<nEvents>" $xml.xml; then
sed -i "/<nEvents>/c\ <nEvents>$NEV</nEvents>" $xml.xml
else
sed -i "/<jetscape>/a\ <nEvents>$NEV</nEvents>" $xml.xml
fi
else
echo "Number of events not set, checking xml file..."
if grep -Fq "<nEvents>" $xml.xml; then
NEV=$(grep -F "<nEvents>" $xml.xml | awk '{print $2}')
echo "Number of events set to $NEV"
else
echo "Error: Number of events not set in JETSCAPE"
exit 5
fi
fi

# Starting simulation
o2-sim -j $JOBS -n ${NEV} -g hepmc --seed $RANDOM \
--configKeyValues "GeneratorFileOrCmd.cmd=$cmd -i $xml;GeneratorFileOrCmd.fileNames=test_out.hepmc;GeneratorFileOrCmd.outputSwitch=-o;GeneratorFileOrCmd.bMaxSwitch=none;GeneratorFileOrCmd.nEventsSwitch=none;${more}"

0 comments on commit ecec4fc

Please sign in to comment.