gdata
Loading...
Searching...
No Matches
gEventHeader.h
Go to the documentation of this file.
1#pragma once
2
3// gemc
4#include <gemc/gbase/gbase.h>
5
6// C++
7#include <atomic>
8#include <string>
9
26
27constexpr const char* GDATAEVENTHEADER_LOGGER = "event_header";
28
29namespace geventheader {
30
44
45} // namespace geventheader
46
55
69class GEventHeader : public GBase<GEventHeader>
70{
71public:
83 GEventHeader(const std::shared_ptr<GOptions>& gopts, int n, int tid)
84 : GBase(gopts, GDATAEVENTHEADER_LOGGER), g4localEventNumber(n), threadID(tid) {
85 timeStamp = assignTimeStamp();
86 log->debug(CONSTRUCTOR, "GEventHeader");
87 log->info(
88 1, "\n%1 Event Number: %2\n%3 Thread ID: %4\n%5 Time Stamp: %6", guts::TPOINTITEM,
89 g4localEventNumber, guts::TPOINTITEM, threadID, guts::TPOINTITEM, timeStamp);
90 }
91
104 static std::unique_ptr<GEventHeader> create(const std::shared_ptr<GOptions>& gopts, int tid = -1) {
105 int eventNumber = globalEventHeaderCounter.fetch_add(1, std::memory_order_relaxed);
106 int threadID_ = tid;
107 if (threadID_ < 0) {
108 threadID_ = eventNumber % 8;
109 }
110 return std::make_unique<GEventHeader>(gopts, eventNumber, threadID_);
111 }
112
118 [[nodiscard]] inline std::string getTimeStamp() const { return timeStamp; }
119
128 [[nodiscard]] inline int getG4LocalEvn() const { return g4localEventNumber; }
129
135 [[nodiscard]] inline int getThreadID() const { return threadID; }
136
137private:
139 int g4localEventNumber;
140
142 int threadID;
143
155 static std::string assignTimeStamp() {
156 time_t now = time(nullptr);
157 struct tm* ptm = localtime(&now);
158 char buffer[32];
159 strftime(buffer, 32, "%a %m.%d.%Y %H:%M:%S", ptm);
160 return {buffer};
161 }
162
164 std::string timeStamp;
165
167 static std::atomic<int> globalEventHeaderCounter;
168};
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
static std::unique_ptr< GEventHeader > create(const std::shared_ptr< GOptions > &gopts, int tid=-1)
Creates a header with a unique event number for tests and examples.
std::string getTimeStamp() const
Returns the formatted timestamp string.
int getThreadID() const
Returns the thread identifier associated with the event.
GEventHeader(const std::shared_ptr< GOptions > &gopts, int n, int tid)
Constructs an event header with explicit event and thread identifiers.
int getG4LocalEvn() const
Returns the local event number.
constexpr const char * GDATAEVENTHEADER_LOGGER
CONSTRUCTOR
GOptions defineOptions()
Defines the options subtree used by the event-header logger domain.
constexpr char TPOINTITEM[]