Skip to content

Commit 9ccc1b2

Browse files
committed
Refactoring to HepMC and TParticle Generators
The classes `GeneratorHepMC` and `GeneratorTParticle` is refactored to derive from the (second) base class `GeneratorFileOrCmd`. `GeneratorFileOrCmd` provides common infrastructure to specify - File(s) to read events from (ROOT files in case of `GeneratorTParticle`, and HepMC files in case of `GeneratorHepMC`), _or_ - Which commmand to execute and with which options. It also provides infrastructure to make unique temporary names, a FIFO to read from (child program writes to), and so on. These are all configured through configuration keys prefixed by `FileOrCmd.`. Other changes include - `GeneratorHepMC` will open _any_ file that HepMC supports - ASCII, compressed ASCII, HEPEVT, etc. - Through the use of `GeneratorFileOrCmd` the command line option flags for specifying seed (default: `-s`), number of events (default `-n`), largest impact parameter (defautl: `-b`), output (default: `>`), and so on can be configured via configuration key values - `GeneratorHepMC` and `GeneratorTParticle` are passed the `GeneratorFileOrCmdParam` as well as specific `GeneratorHepMCParam` and `GeneratorTParticleParam`, respectively, objects by `GeneratorFactor` and sets the internal parameters accordingly. This hides the specifics of the parameters from `GeneratorFactory`.
1 parent 6f6f2c3 commit 9ccc1b2

13 files changed

+746
-246
lines changed

Generators/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ o2_add_library(Generators
2929
src/GeneratorExternalParam.cxx
3030
src/GeneratorFromFile.cxx
3131
src/GeneratorFromO2KineParam.cxx
32+
src/GeneratorFileOrCmd.cxx
33+
src/GeneratorFileOrCmdParam.cxx
3234
src/PrimaryGenerator.cxx
3335
src/PrimaryGeneratorParam.cxx
3436
src/TriggerExternalParam.cxx
@@ -48,6 +50,7 @@ o2_add_library(Generators
4850
$<$<BOOL:${pythia_FOUND}>:src/DecayerPythia8Param.cxx>
4951
$<$<BOOL:${HepMC3_FOUND}>:src/GeneratorHepMC.cxx>
5052
$<$<BOOL:${HepMC3_FOUND}>:src/GeneratorHepMCParam.cxx>
53+
$<$<BOOL:${HepMC3_FOUND}>:src/AODToHepMC.cxx>
5154
PUBLIC_LINK_LIBRARIES FairRoot::Base O2::SimConfig O2::CommonUtils O2::DetectorsBase O2::ZDCBase
5255
O2::SimulationDataFormat ${pythia6Target} ${pythiaTarget} ${hepmcTarget}
5356
FairRoot::Gen
@@ -73,6 +76,8 @@ set(headers
7376
include/Generators/GeneratorExternalParam.h
7477
include/Generators/GeneratorFromFile.h
7578
include/Generators/GeneratorFromO2KineParam.h
79+
include/Generators/GeneratorFileOrCmd.h
80+
include/Generators/GeneratorFileOrCmdParam.h
7681
include/Generators/PrimaryGenerator.h
7782
include/Generators/PrimaryGeneratorParam.h
7883
include/Generators/TriggerExternalParam.h
@@ -102,6 +107,7 @@ endif()
102107
if(HepMC3_FOUND)
103108
list(APPEND headers include/Generators/GeneratorHepMC.h)
104109
list(APPEND headers include/Generators/GeneratorHepMCParam.h)
110+
list(APPEND headers include/Generators/AODToHepMC.h)
105111
endif()
106112

107113
o2_target_root_dictionary(Generators HEADERS ${headers})
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
/// @author Christian Holm Christensen <cholm@nbi.dk>
13+
14+
#ifndef ALICEO2_EVENTGEN_GENERATORFILEORCMD_H_
15+
#define ALICEO2_EVENTGEN_GENERATORFILEORCMD_H_
16+
#include <Generators/GeneratorFileOrCmdParam.h>
17+
#include <list>
18+
#include <string>
19+
20+
namespace o2
21+
{
22+
namespace conf
23+
{
24+
class SimConfig;
25+
}
26+
namespace eventgen
27+
{
28+
29+
/** Service class for either reading from a file or executing a
30+
program writing to a specific file */
31+
struct GeneratorFileOrCmd {
32+
/**
33+
* Configure the generator from parameters and the general
34+
* simulation configuration. This is implemented as a member
35+
* function so as to better facilitate changes. */
36+
void setup(const GeneratorFileOrCmdParam& param,
37+
const conf::SimConfig& config);
38+
/**
39+
* Set command to execute in bacground rather than reading from
40+
* existing file(s)
41+
*
42+
* @param cmd Command line. Can include options for the program to
43+
* execute, but should not include pipes.
44+
*/
45+
void setCmd(const std::string& cmd) { mCmd = cmd; }
46+
/**
47+
* Set the number of events that a background command should
48+
* generate. This should come from @c SimConfig::getNEents.
49+
*
50+
* @param nev Number of events to generate. This is passed via @c
51+
* mNEventsSwitch to the command line.
52+
*/
53+
void setNEvents(unsigned int nev) { mNEvents = nev; }
54+
/**
55+
* Set the random number seed that a background command should use.
56+
* This should come from @c SimConfig::getStartSeed
57+
*
58+
* @param seed Random number seed. Will be passed to the
59+
* commandline using the @c mSeedSwitch.
60+
*/
61+
void setSeed(unsigned long seed) { mSeed = seed; }
62+
/**
63+
* Set the maximum impact parameter to sample by a background
64+
* command. This should come from @c SimConfig::getBMax()
65+
*
66+
* @param bmax Maximum impact parameter, in fm, to sample. This is
67+
* passed to the command line via the @c mBmaxSwitch.
68+
*/
69+
void setBmax(float bmax) { mBmax = bmax; }
70+
/**
71+
* Set the file names to read from.
72+
*
73+
* @param filenames A comma seperated list of files to read from.
74+
*/
75+
void setFileNames(const std::string& filenames);
76+
/**
77+
* Set the output switch.
78+
*
79+
* @param opt Command line switch (e.g., @c -o) to specify output
80+
* file name. */
81+
void setOutputSwitch(const std::string& opt) { mOutputSwitch = opt; }
82+
/**
83+
* Set the seed switch.
84+
*
85+
* @param opt Command line switch (e.g., @c -s) to specify the
86+
* random number seed to use when generating events.
87+
*/
88+
void setSeedSwitch(const std::string& opt) { mSeedSwitch = opt; }
89+
/**
90+
* Set the nevents switch.
91+
*
92+
* @param opt Command line switch (e.g., @c -n) to specify the
93+
* number of events to generate.
94+
*/
95+
void setNEventsSwitch(const std::string& opt) { mNEventsSwitch = opt; }
96+
/**
97+
* Set the maximum impact parameter switch.
98+
*
99+
* @param opt Command line switch (e.g., @c -b) to specify the
100+
* maximum impact parameter (in fm) to sample when generating
101+
* events.
102+
*/
103+
void setBmaxSwitch(const std::string& opt) { mBmaxSwitch = opt; }
104+
/**
105+
* Set the background switch.
106+
*
107+
* @param opt Command line switch (e.g., @c &) to detach and send
108+
* the event generator program into the background.
109+
*/
110+
void setBackgroundSwitch(const std::string& opt) { mBackgroundSwitch = opt; }
111+
/** Set the wait time, in miliseconds, when waiting for data */
112+
void setWait(int miliseconds = 500) { mWait = miliseconds; }
113+
114+
protected:
115+
/**
116+
* Format a command line using the set command line, option flags,
117+
* and option values.
118+
*
119+
* @return formatted command line.
120+
*/
121+
virtual std::string makeCmdLine() const;
122+
/**
123+
* Execute a command line (presumably formatted by @c makeCmdLine).
124+
* If the command failed to execute, then make it a fatal error.
125+
*
126+
* @param cmd Command line to execute, presumabley formatted by @c
127+
* makeCmdLine.
128+
129+
* @return true if the background command line was executed, false
130+
* otherwise.
131+
*/
132+
virtual bool executeCmdLine(const std::string& cmd) const;
133+
/**
134+
* Create a temporary file (and close it immediately). On success,
135+
* the list of file names is cleared and the name of the temporary
136+
* file set as the sole element in that list.
137+
*
138+
* @return true if the temporary file name was generated
139+
* successfully.
140+
*/
141+
virtual bool makeTemp();
142+
/**
143+
* Remove the temporary file if it was set and it exists.
144+
*
145+
* @return true if the temporary file was removed.
146+
*/
147+
virtual bool removeTemp() const;
148+
/**
149+
* Make a fifo at the location of the first element of the list of
150+
* file names (presumably a temporary file as created by
151+
* makeTemp).
152+
*
153+
* @return true if the FIFo was made correctly
154+
*/
155+
virtual bool makeFifo() const;
156+
/**
157+
* Ensure that all files in the list of file names exists. If @e
158+
* any of te file names point to a net resource (e.g., @c alien://or
159+
* @c https://) then this member function should @e not be called
160+
*
161+
* The file names registered are replaced with their canonical path
162+
* equivalents.
163+
*
164+
* @return true if all currently registered file names can be found.
165+
*/
166+
virtual bool ensureFiles();
167+
/**
168+
* Wait for data to be available in case we're executing a
169+
* background command
170+
*/
171+
virtual void waitForData(const std::string& filename) const;
172+
/**
173+
* Possible command line to execute. The command executed must
174+
* accept the switches defined below. Note if @c mOutputSwitch is
175+
* set to @c ">", then the program @e must write data to standard
176+
* output
177+
*/
178+
std::string mCmd = "";
179+
/**
180+
* List of file names to read. In case we're executing a command,
181+
* then there will only be one file name in the list
182+
*/
183+
std::list<std::string> mFileNames;
184+
/**
185+
* Name of temporary file, if it was created.
186+
*/
187+
std::string mTemporary;
188+
/**
189+
* Number of events to generate in case we're executing a command.
190+
* This is passed to the program via the switch @c
191+
* mNEventsSwitch
192+
*/
193+
unsigned int mNEvents = 0;
194+
/**
195+
* Random number seed to use in case we're executing a command.
196+
* This is passed to the program via the switch @c
197+
* mSeedSwitch
198+
*/
199+
unsigned long mSeed = 0;
200+
/**
201+
* Maximum impact parameter to sample in case we're executing a
202+
* command. IF negative, then it is not passed to the command.
203+
* This is passed to the command via the switch @c mBmaxSwitch
204+
*/
205+
float mBmax = -1.f;
206+
/**
207+
* Switch to direct output to specified file. In case of a fifo,
208+
* this should often be @c ">" - i.e., the standard output of the
209+
* program is redirected to the fifo.
210+
*/
211+
std::string mOutputSwitch = ">";
212+
/**
213+
* The switch specify the random number seed to the program
214+
* executed
215+
*/
216+
std::string mSeedSwitch = "-s";
217+
/**
218+
* The switch to specify the number of events to generate to the
219+
* program being executed
220+
*/
221+
std::string mNEventsSwitch = "-n";
222+
/**
223+
* The switch to specify maximum impact parameter to sample by the
224+
* program being executed.
225+
*/
226+
std::string mBmaxSwitch = "-b";
227+
/**
228+
* The "switch" to put the program being executed in the
229+
* background.
230+
*/
231+
std::string mBackgroundSwitch = "&";
232+
/**
233+
* Time in miliseconds between each wait for data
234+
*/
235+
int mWait = 500;
236+
};
237+
238+
} // namespace eventgen
239+
} // namespace o2
240+
#endif
241+
// Local Variables:
242+
// mode: C++
243+
// End:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023-2099 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
/// @author Christian Holm Christensen <cholm@nbi.dk>
13+
14+
#ifndef ALICEO2_EVENTGEN_GENERATORFILEORCMDPARAM_H_
15+
#define ALICEO2_EVENTGEN_GENERATORFILEORCMDPARAM_H_
16+
17+
#include "CommonUtils/ConfigurableParam.h"
18+
#include "CommonUtils/ConfigurableParamHelper.h"
19+
#include <string>
20+
21+
namespace o2
22+
{
23+
namespace eventgen
24+
{
25+
26+
/**
27+
** a parameter class/struct to keep the settings of
28+
** the Fileorcmd event generator and
29+
** allow the user to modify them
30+
**/
31+
struct GeneratorFileOrCmdParam : public o2::conf::ConfigurableParamHelper<GeneratorFileOrCmdParam> {
32+
std::string fileNames = "";
33+
std::string cmd = ""; // Program command line to spawn
34+
std::string outputSwitch = ">";
35+
std::string seedSwitch = "-s";
36+
std::string bMaxSwitch = "-b";
37+
std::string nEventsSwitch = "-n";
38+
std::string backgroundSwitch = "&";
39+
O2ParamDef(GeneratorFileOrCmdParam, "FileOrCmd");
40+
};
41+
42+
} // end namespace eventgen
43+
} // end namespace o2
44+
45+
#endif // ALICEO2_EVENTGEN_GENERATORFILEORCMDPARAM_H_

0 commit comments

Comments
 (0)