2121#include < TRandom3.h>
2222#include < TGrid.h>
2323#include < random>
24+ #include < string>
25+ #include < vector>
2426
2527class TBranch ;
2628class TFile ;
@@ -66,13 +68,18 @@ class GeneratorFromFile : public FairGenerator
6668};
6769
6870// / This class implements a generic FairGenerator which
69- // / reads the particles from an external O2 sim kinematics file .
71+ // / reads the particles from one or more external O2 sim kinematics files .
7072class GeneratorFromO2Kine : public o2 ::eventgen::Generator
7173{
7274 public:
7375 GeneratorFromO2Kine () = default ;
76+ // / name may be a single file or a comma-separated list of files to be read one after the other
7477 GeneratorFromO2Kine (const char * name);
78+ GeneratorFromO2Kine (std::vector<std::string> const & filenames);
7579 GeneratorFromO2Kine (O2KineGenConfig const & pars);
80+ // / same as above but with an explicit list of files. Used for event pools
81+ GeneratorFromO2Kine (O2KineGenConfig const & pars, std::vector<std::string> const & filenames);
82+ ~GeneratorFromO2Kine () override ;
7683
7784 bool Init () override ;
7885
@@ -91,24 +98,58 @@ class GeneratorFromO2Kine : public o2::eventgen::Generator
9198 void updateHeader (o2::dataformats::MCEventHeader* eventHeader) override ;
9299 const o2::dataformats::MCEventHeader* getOrigMCEventHeader () const { return mOrigMCEventHeader .get (); }
93100
101+ // / number of events available in the file that is currently open
102+ int getEventsAvailable () const { return mEventsAvailable ; }
103+ // / number of input files this generator can go through
104+ int getNumberOfFiles () const { return (int )mFileNames .size (); }
105+ // / index (within the file list) of the file currently open, -1 if none
106+ int getCurrentFileIndex () const { return mCurrentFileIndex ; }
107+ // / name of the file currently open, empty if none
108+ std::string getCurrentFileName () const ;
109+ // / number of opened files so far (including the current one)
110+ int getNumberOfFilesUsed () const { return mFilesUsed ; }
111+ // / total number of events delivered so far
112+ int getEventsServed () const { return mEventsServed ; }
113+
114+ // / helper splitting a comma-separated list of file names into its components
115+ static std::vector<std::string> splitFileNames (std::string const & filenames);
116+
94117 private:
95- TFile* mEventFile = nullptr ; // ! the file containing the persistent events
96- TBranch* mEventBranch = nullptr ; // ! the branch containing the persistent events
97- TBranch* mMCHeaderBranch = nullptr ; // ! branch containing MC event headers
98- int mEventCounter = 0 ;
99- int mEventsAvailable = 0 ;
100- bool mSkipNonTrackable = true ; // ! whether to pass non-trackable (decayed particles) to the MC stack
101- bool mContinueMode = false ; // ! whether we want to continue simulation of previously inhibited tracks
102- bool mRoundRobin = false ; // ! whether we want to take events from file in a round robin fashion
103- bool mRandomize = false ; // ! whether we want to randomize the order of events in the input file
104- unsigned int mRngSeed = 0 ; // ! randomizer seed, 0 for random value
105- bool mRandomPhi = false ; // ! whether we want to randomize the phi angle of the particles
106- TGrid* mAlienInstance = nullptr ; // a cached connection to TGrid (needed for Alien locations)
118+ // / closes the currently opened file currently
119+ void closeCurrentFile ();
120+ // / fixes the order in which the events of the current file are served
121+ void establishEventOrder ();
122+ // / opens the file at the given index of the file list.
123+ // / Returns false in case the file cannot be used
124+ bool openFile (int index);
125+ // / moves on to the next usable file of the list; wraps around in round robin mode.
126+ // / Returns false when no further file is available
127+ bool openNextFile (bool wrapAround);
128+
129+ std::vector<std::string> mFileNames ; // ! the list of input files, read one after the other
130+ int mCurrentFileIndex = -1 ; // ! index of the file currently open
131+ int mFilesUsed = 0 ; // ! how many files have been opened so far
132+ TFile* mCurrentFile = nullptr ; // ! the file currently open
133+ TBranch* mEventBranch = nullptr ; // ! the branch containing the persistent events
134+ TBranch* mMCHeaderBranch = nullptr ; // ! branch containing MC event headers
135+ std::vector<int > mEventOrder ; // ! order in which the entries of the current file are served
136+ int mEventCounter = 0 ; // ! events already delivered from the current file
137+ int mEventsServed = 0 ; // ! events delivered in total, across all files
138+ int mEventsAvailable = 0 ; // ! events contained in the current file
139+ int mStartEvent = 0 ; // ! event to start from in the very first file
140+ int mLastEntryRead = -1 ; // ! entry of the current event within the current file
141+ bool mSkipNonTrackable = true ; // ! whether to pass non-trackable (decayed particles) to the MC stack
142+ bool mContinueMode = false ; // ! whether we want to continue simulation of previously inhibited tracks
143+ bool mRoundRobin = false ; // ! whether we want to take events from file in a round robin fashion
144+ bool mRandomize = false ; // ! whether we want to randomize the order of events in the input file
145+ unsigned int mRngSeed = 0 ; // ! randomizer seed, 0 for random value
146+ bool mRandomPhi = false ; // ! whether we want to randomize the phi angle of the particles
147+ TGrid* mAlienInstance = nullptr ; // a cached connection to TGrid (needed for Alien locations)
107148 std::unique_ptr<O2KineGenConfig> mConfig ; // ! Configuration object
108149
109150 std::unique_ptr<o2::dataformats::MCEventHeader> mOrigMCEventHeader ; // ! the MC event header of the original file
110151
111- ClassDefOverride (GeneratorFromO2Kine, 2 );
152+ ClassDefOverride (GeneratorFromO2Kine, 3 );
112153};
113154
114155// / Special generator for event pools.
@@ -167,15 +208,24 @@ class GeneratorFromEventPool : public o2::eventgen::Generator
167208
168209 std::vector<std::string> const & getFileUniverse () const { return mPoolFilesAvailable ; }
169210
211+ // / the file universe, in the order this generator instance will go through it
212+ std::vector<std::string> const & getChosenFiles () const { return mFilesChosen ; }
213+
214+ // / shuffles the given universe of pool files into the order this instance will use
215+ std::vector<std::string> selectFiles (std::vector<std::string> const & universe);
216+
217+ // / access to the underlying kinematics generator
218+ o2::eventgen::GeneratorFromO2Kine const * getO2KineGenerator () const { return mO2KineGenerator .get (); }
219+
170220 private:
171221 EventPoolGenConfig mConfig ; // ! Configuration object
172222 std::unique_ptr<o2::eventgen::GeneratorFromO2Kine> mO2KineGenerator = nullptr ; // ! actual generator doing the work
173223 std::vector<std::string> mPoolFilesAvailable ; // ! container keeping the collection of files in the event pool
174- std::string mFileChosen ; // ! the file chosen for the pool
224+ std::vector<std:: string> mFilesChosen ; // ! the file(s) chosen from the pool
175225 // random number generator to determine a concrete file name
176226 std::mt19937 mRandomEngine ; // !
177227
178- ClassDefOverride (GeneratorFromEventPool, 1 );
228+ ClassDefOverride (GeneratorFromEventPool, 2 );
179229};
180230
181231} // end namespace eventgen
0 commit comments