Skip to content

Commit ecec4fc

Browse files
jackal1-66sawenzel
authored andcommitted
Custom fifo names feature + jetscape usage example
1 parent 789968c commit ecec4fc

File tree

8 files changed

+297
-19
lines changed

8 files changed

+297
-19
lines changed

Generators/include/Generators/GeneratorFileOrCmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct GeneratorFileOrCmd {
141141
* @return true if the temporary file name was generated
142142
* successfully.
143143
*/
144-
virtual bool makeTemp();
144+
virtual bool makeTemp(const bool&);
145145
/**
146146
* Remove the temporary file if it was set and it exists.
147147
*

Generators/src/GeneratorFileOrCmd.cxx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,40 @@ bool GeneratorFileOrCmd::executeCmdLine(const std::string& cmd) const
126126
return true;
127127
}
128128
// -----------------------------------------------------------------
129-
bool GeneratorFileOrCmd::makeTemp()
130-
{
131-
mFileNames.clear();
132-
char buf[] = "generatorFifoXXXXXX";
133-
auto fp = mkstemp(buf);
134-
if (fp < 0) {
135-
LOG(fatal) << "Failed to make temporary file: "
136-
<< std::strerror(errno);
137-
return false;
129+
bool GeneratorFileOrCmd::makeTemp(const bool& fromName)
130+
{
131+
if (fromName) {
132+
if (mFileNames.empty()) {
133+
LOG(fatal) << "No file names to make temporary file from";
134+
return false;
135+
} else if (mFileNames.size() > 1) {
136+
LOG(warning) << "More than one file name to make temporary file from";
137+
LOG(warning) << "Using the first one: " << mFileNames.front();
138+
LOG(warning) << "Removing all the others";
139+
mFileNames.erase(++mFileNames.begin(), mFileNames.end());
140+
} else {
141+
LOG(debug) << "Making temporary file from: " << mFileNames.front();
142+
}
143+
std::ofstream ofs(mFileNames.front().c_str());
144+
if (!ofs) {
145+
LOG(fatal) << "Failed to create temporary file: " << mFileNames.front();
146+
return false;
147+
}
148+
mTemporary = std::string(mFileNames.front());
149+
ofs.close();
150+
} else {
151+
mFileNames.clear();
152+
char buf[] = "generatorFifoXXXXXX";
153+
auto fp = mkstemp(buf);
154+
if (fp < 0) {
155+
LOG(fatal) << "Failed to make temporary file: "
156+
<< std::strerror(errno);
157+
return false;
158+
}
159+
mTemporary = std::string(buf);
160+
mFileNames.push_back(mTemporary);
161+
close(fp);
138162
}
139-
mTemporary = std::string(buf);
140-
mFileNames.push_back(mTemporary);
141-
close(fp);
142163
return true;
143164
}
144165
// -----------------------------------------------------------------

Generators/src/GeneratorHepMC.cxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,16 @@ Bool_t GeneratorHepMC::Init()
575575
// All of this can conviniently be achieved via a wrapper script
576576
// around the actual EG program.
577577
if (not mCmd.empty()) {
578-
// Set filename to be a temporary name
579-
if (not makeTemp()) {
580-
return false;
578+
if (mFileNames.empty()) {
579+
// Set filename to be a temporary name
580+
if (not makeTemp(false)) {
581+
return false;
582+
}
583+
} else {
584+
// Use the first filename as output for cmd line
585+
if (not makeTemp(true)) {
586+
return false;
587+
}
581588
}
582589

583590
// Make a fifo

Generators/src/GeneratorTParticle.cxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ Bool_t GeneratorTParticle::Init()
5454
mChain->SetBranchAddress(mBranchName.c_str(), &mTParticles);
5555

5656
if (not mCmd.empty()) {
57-
// Set filename to be a temporary name
58-
if (not makeTemp()) {
59-
return false;
57+
if (mFileNames.empty()) {
58+
// Set filename to be a temporary name
59+
if (not makeTemp(false)) {
60+
return false;
61+
}
62+
} else {
63+
// Use the first filename as output for cmd line
64+
if (not makeTemp(true)) {
65+
return false;
66+
}
6067
}
6168

6269
// Build command line, Assumes command line parameter
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!-- doxy
2+
\page refrunSimExamplesHepMC_JETSCAPE Example HepMC_JETSCAPE
3+
/doxy -->
4+
5+
The usage of JETSCAPE with the O2 machinery is presented in this short manual.
6+
An in-depth explanation of the mechanisms behind the HepMC(3) data handling can be found in the
7+
HepMC_fifo folder of the MC examples. The scripts use the `cmd` parameter of `GeneratorHepMC`
8+
to spawn the JETSCAPE generation via the `jetscape.sh` script. It is important to turn on the
9+
HepMC3 output format in the xml configuration file, as done in jetscape_user_example.xml, otherwise
10+
the simulation will not work.
11+
12+
# Scripts description
13+
14+
Two scripts are available to run the simulations
15+
- **jetscape.sh** &rarr; starts the actual JETSCAPE generation
16+
- **runo2sim.sh** &rarr; allows the generation of events using o2-sim
17+
18+
In addition an jetscape_user_example.xml file is provided to start JETSCAPE with user parameters.
19+
The user could easily create scripts similar to the one provided for the EPOS4 tutorial for DPL or O2DPG
20+
based simulations.
21+
22+
## jetscape.sh
23+
24+
It can be run without the help of the other scripts to simply generate an .hepmc file.
25+
This example shows all the functionalities of the script (which are implemented in a similar way inside
26+
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.
27+
28+
## runo2sim.sh
29+
30+
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.
31+
Few flags are available to change the settings of the generation:
32+
- **-m , --more** &rarr; feeds the simulation with advanced parameters provided to the configuration key flags
33+
- **-n , --nevents** &rarr; changes the number of events in the .xml file or gets the one in the file if no events are provided
34+
- **-i , --input** &rarr; .xml filename to feed JETSCAPE, no extension must be set in the filename
35+
- **-j , --jobs** &rarr; sets the number of workers (jobs)
36+
- **-h , --help** &rarr; prints usage instructions
37+
38+
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.
39+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
# Script based on EPOS4 example
3+
# This script is used to run JETSCAPE with the given XML file
4+
# setting the seed and HepMC output filename. Contrary to the
5+
# epos example, the HepMC output is generated in a custom named file
6+
# not passing from the stdout.
7+
8+
xml="example"
9+
seed=$RANDOM
10+
hepmc="jetout.hepmc"
11+
12+
usage()
13+
{
14+
cat <<EOF
15+
Usage: $0 [OPTIONS]
16+
17+
Options:
18+
19+
-i,--input INPUT XML user file fed to JETSCAPE ($xml)
20+
-o,--output OUTPUT HepMC output file ($hepmc)
21+
-s,--seed SEED RNG seed ($seed)
22+
-h,--help Print these instructions
23+
-- Rest of command line sent to o2-sim
24+
25+
EOF
26+
}
27+
28+
while test $# -gt 0 ; do
29+
case $1 in
30+
-i|--input) xml=$2 ; shift ;;
31+
-o|--output) hepmc=$2 ; shift ;;
32+
-s|--seed) seed=$2 ; shift ;;
33+
-h|--help) usage; exit 0 ;;
34+
esac
35+
shift
36+
done
37+
38+
if [ ! -f $xml.xml ]; then
39+
echo "Error: Options file $xml.xml not found"
40+
exit 1
41+
fi
42+
43+
if [ $seed -eq 0 ]; then
44+
echo "Seed can't be 0, random number will be used"
45+
seed=$RANDOM
46+
else
47+
if grep -Fq "<seed>" $xml.xml; then
48+
sed -i "/<seed>/c\ <seed>$seed</seed>" $xml.xml
49+
else
50+
sed -i "/<\/jetscape>/i\ <Random>\n <seed>$seed</seed>\n </Random>" $xml.xml
51+
fi
52+
echo "Seed set to $seed"
53+
fi
54+
55+
# Check if hepmc output has been set
56+
if [ ! -z "$hepmc" ]; then
57+
# Remove extension
58+
newhep=$(echo $hepmc | sed 's/.hepmc//')
59+
if grep -Fq "<outputFilename>" $xml.xml; then
60+
sed -i "/<outputFilename>/c\ <outputFilename>$newhep</outputFilename>" $xml.xml
61+
else
62+
sed -i "/<jetscape>/a\ <outputFilename>$newhep</outputFilename>" $xml.xml
63+
fi
64+
echo "HepMC output file set to $hepmc"
65+
else
66+
echo "Error: HepMC output file not set"
67+
exit 2
68+
fi
69+
70+
# Master XML file pulled directly from the JETSCAPE directory
71+
runJetscape $xml.xml $JETSCAPE_ROOT/config/jetscape_master.xml
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0"?>
2+
3+
<jetscape>
4+
5+
<nEvents>1000</nEvents>
6+
7+
<Random>
8+
<seed>1</seed>
9+
</Random>
10+
11+
<outputFilename>jetscape</outputFilename>
12+
13+
<!--Do not delete HepMC setting-->
14+
<JetScapeWriterHepMC> on </JetScapeWriterHepMC>
15+
16+
<!-- Hard Process -->
17+
<Hard>
18+
<PythiaGun>
19+
<pTHatMin>235</pTHatMin>
20+
<pTHatMax>1000</pTHatMax>
21+
<eCM>5020</eCM>
22+
</PythiaGun>
23+
</Hard>
24+
25+
<!--Eloss Modules -->
26+
<Eloss>
27+
<Matter>
28+
<Q0> 1.0 </Q0>
29+
<in_vac> 1 </in_vac>
30+
<vir_factor> 0.25 </vir_factor>
31+
<recoil_on> 0 </recoil_on>
32+
<broadening_on> 0 </broadening_on>
33+
<brick_med> 0 </brick_med>
34+
</Matter>
35+
</Eloss>
36+
37+
<!-- Jet Hadronization Module -->
38+
<JetHadronization>
39+
<name>colorless</name>
40+
</JetHadronization>
41+
42+
</jetscape>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This is a simple simulation example showing how to
4+
# start JETSCAPE generation automatically using cmd with hepmc output on FIFO
5+
# and simultaneosly use o2-sim for transport
6+
7+
# JETSCAPE and O2 must be loaded
8+
set -x
9+
if [ ! "${JETSCAPE_ROOT}" ]; then
10+
echo "This needs JETSCAPE loaded; alienv enter ..."
11+
exit 1
12+
fi
13+
14+
[ ! "${O2_ROOT}" ] && echo "Error: This needs O2 loaded" && exit 2
15+
16+
cmd="$PWD/jetscape.sh"
17+
NEV=-1
18+
more=""
19+
xml="example"
20+
JOBS=2
21+
22+
usage()
23+
{
24+
cat <<EOF
25+
Usage: $0 [OPTIONS]
26+
27+
Options:
28+
29+
-m,--more CONFIG More configurations ($more)
30+
-n,--nevents EVENTS Number of events ($nev)
31+
-i,--input INPUT XML configuration file fed to JETSCAPE ($xml)
32+
-j,--jobs JOBS Number of jobs ($JOBS)
33+
-h,--help Print these instructions
34+
-- Rest of command line sent to o2-sim
35+
36+
COMMAND must be quoted if it contains spaces or other special
37+
characters
38+
39+
Below follows the help output of o2-sim
40+
41+
EOF
42+
}
43+
44+
if [ "$#" -lt 2 ]; then
45+
echo "Running with default values"
46+
fi
47+
48+
while test $# -gt 0 ; do
49+
case $1 in
50+
-m|--more) more="$2" ; shift ;;
51+
-n|--nevents) NEV=$2 ; shift ;;
52+
-i|--input) xml=$2 ; shift ;;
53+
-j|--jobs) JOBS=$2 ; shift ;;
54+
-h|--help) usage; o2-sim --help full ; exit 0 ;;
55+
--) shift ; break ;;
56+
*) echo "Unknown option '$1', did you forget '--'?" >/dev/stderr
57+
exit 3
58+
;;
59+
esac
60+
shift
61+
done
62+
63+
echo "XML User file: $xml"
64+
65+
if [ ! -f $xml.xml ]; then
66+
echo "Error: Options file $xml.xml not found"
67+
exit 4
68+
fi
69+
70+
# Set number of events in the XML file
71+
if [ ! $NEV -eq -1 ]; then
72+
echo "Setting number of events to $NEV"
73+
if grep -Fq "<nEvents>" $xml.xml; then
74+
sed -i "/<nEvents>/c\ <nEvents>$NEV</nEvents>" $xml.xml
75+
else
76+
sed -i "/<jetscape>/a\ <nEvents>$NEV</nEvents>" $xml.xml
77+
fi
78+
else
79+
echo "Number of events not set, checking xml file..."
80+
if grep -Fq "<nEvents>" $xml.xml; then
81+
NEV=$(grep -F "<nEvents>" $xml.xml | awk '{print $2}')
82+
echo "Number of events set to $NEV"
83+
else
84+
echo "Error: Number of events not set in JETSCAPE"
85+
exit 5
86+
fi
87+
fi
88+
89+
# Starting simulation
90+
o2-sim -j $JOBS -n ${NEV} -g hepmc --seed $RANDOM \
91+
--configKeyValues "GeneratorFileOrCmd.cmd=$cmd -i $xml;GeneratorFileOrCmd.fileNames=test_out.hepmc;GeneratorFileOrCmd.outputSwitch=-o;GeneratorFileOrCmd.bMaxSwitch=none;GeneratorFileOrCmd.nEventsSwitch=none;${more}"

0 commit comments

Comments
 (0)