Skip to content

Commit 6ccf3b3

Browse files
committed
Implement HepMC reading randomisation
1 parent 54753b2 commit 6ccf3b3

6 files changed

Lines changed: 641 additions & 44 deletions

File tree

Generators/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ if(doBuildSimulation)
139139
LABELS generator
140140
PUBLIC_LINK_LIBRARIES O2::Generators)
141141

142+
if(HepMC3_FOUND)
143+
o2_add_test(GeneratorHepMCIndexed NAME test_Generator_test_GeneratorHepMCIndexed
144+
SOURCES test/test_GeneratorHepMCIndexed.cxx
145+
COMPONENT_NAME Generator
146+
LABELS generator
147+
PUBLIC_LINK_LIBRARIES O2::Generators)
148+
endif()
149+
142150
# o2_add_test(GeneratorPythia8Param NAME test_Generator_test_GeneratorPythia8Param
143151
# SOURCES test/test_GeneratorPythia8Param.cxx
144152
# COMPONENT_NAME Generator

Generators/include/Generators/GeneratorHepMC.h

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include "Generators/GeneratorFileOrCmd.h"
1919
#include "Generators/GeneratorHepMCParam.h"
2020
#include "Generators/GeneratorFileOrCmdParam.h"
21+
#include <iosfwd>
22+
#include <memory>
23+
#include <string>
24+
#include <vector>
2125

2226
#ifdef GENERATORS_WITH_HEPMC3_DEPRECATED
2327
namespace HepMC
@@ -68,7 +72,7 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd
6872
* simulation configuration. This is implemented as a member
6973
* function so as to better facilitate changes. */
7074
void setup(const GeneratorFileOrCmdParam& param0,
71-
const GeneratorHepMCParam& param,
75+
const HepMCGenConfig& param,
7276
const conf::SimConfig& config);
7377
// Generator configuration from external local parameters
7478
void setup(const FileOrCmdGenConfig& param0,
@@ -108,8 +112,19 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd
108112

109113
/** methods that can be overridded **/
110114
void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override;
111-
/** Make our reader */
115+
/** Apply the HepMC-specific configuration */
116+
void setupHepMC(const HepMCGenConfig& param);
117+
/** Make our reader, taking the next file off the list of file names */
112118
bool makeReader();
119+
/** Fix the order in which the entries of the input file are served */
120+
void establishEventOrder();
121+
/** Index the events of a file by byte offset and open the reader on it, so
122+
* that any entry can later be reached with a single seek. Available only for ASCII format */
123+
bool buildIndex(const std::string& filename);
124+
/** Read the given entry of the indexed file */
125+
bool readEntry(int entry);
126+
/** Generate an event following the established event order in random mode */
127+
Bool_t generateEventOrdered();
113128

114129
/** Type of function to select particles to keep when pruning
115130
* events */
@@ -127,8 +142,34 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd
127142
HepMC3::GenEvent* mEvent = nullptr;
128143
/** Option whether to prune event */
129144
bool mPrune; //!
130-
131-
ClassDefOverride(GeneratorHepMC, 1);
145+
/** Name of the file the reader is attached to, needed to re-open it */
146+
std::string mCurrentFileName; //!
147+
/** Order in which the entries of the input file are served */
148+
std::vector<int> mEventOrder; //!
149+
/** Events already delivered in the current pass over the file */
150+
int mEventCounter = 0; //!
151+
/** Events delivered in total */
152+
int mEventsServed = 0; //!
153+
/** Events contained in the input file */
154+
int mEventsAvailable = 0; //!
155+
/** Entry the reader is currently positioned on, -1 if none */
156+
int mLastEntryRead = -1; //!
157+
/** Option whether to serve the events in random order */
158+
bool mRandomize = false; //!
159+
/** Option whether to start over once all events have been used */
160+
bool mRoundRobin = false; //!
161+
/** Option to have a new order when round-robin enabled */
162+
bool mReshuffleOnRepeat = true; //!
163+
/** Randomizer seed, 0 to leave gRandom alone */
164+
unsigned int mRngSeed = 0; //!
165+
/** Whether the indexed input is HepMC2 rather than HepMC3 ASCII */
166+
bool mIndexedHepMC2 = false; //!
167+
/** The stream the reader is attached to, ours so that we may seek in it */
168+
std::shared_ptr<std::istream> mIndexedStream; //!
169+
/** Byte offset at which every entry of the input file starts */
170+
std::vector<std::streamoff> mEventOffsets; //!
171+
172+
ClassDefOverride(GeneratorHepMC, 2);
132173

133174
}; /** class GeneratorHepMC **/
134175

Generators/include/Generators/GeneratorHepMCParam.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace eventgen
2929
** allow the user to modify them
3030
**/
3131

32-
struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorHepMCParam> {
32+
struct HepMCGenConfig {
3333
/** Version number of event structure to decode. Note, when reading
3434
* from a file, this key is ignored. The interface will figure out
3535
* the version automatically. When reading from a pipe, and the
@@ -51,15 +51,19 @@ struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorH
5151
* event generator producing the event. Use with caution, as it may
5252
* corrupt the event record. */
5353
bool prune = false;
54-
O2ParamDef(GeneratorHepMCParam, "HepMC");
54+
/** Serve once the events of the input file in random order */
55+
bool randomize = false;
56+
/** Restart reading events */
57+
bool roundRobin = false;
58+
/** Draw a fresh random order on each new pass over the input file */
59+
bool reshuffleOnRepeat = true;
60+
/** Randomizer seed, 0 for random value. */
61+
unsigned int rngseed = 0;
5562
};
5663

57-
struct HepMCGenConfig {
58-
// Same parameters as GeneratorHepMCParam
59-
int version = 0;
60-
uint64_t eventsToSkip = 0;
61-
std::string fileName = "";
62-
bool prune = false;
64+
// construct a configurable param singleton out of the HepMCGenConfig struct
65+
struct GeneratorHepMCParam : public o2::conf::ConfigurableParamPromoter<GeneratorHepMCParam, HepMCGenConfig> {
66+
O2ParamDef(GeneratorHepMCParam, "HepMC");
6367
};
6468

6569
} // end namespace eventgen

0 commit comments

Comments
 (0)