|
| 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: |
0 commit comments