gdata
Loading...
Searching...
No Matches
gEventHeader.h
Go to the documentation of this file.
1#pragma once
2
3// gemc
4#include "gbase.h"
5
6// C++
7#include <atomic>
8#include <string>
9
27constexpr const char* GDATAEVENTHEADER_LOGGER = "event_header";
28
29namespace geventheader {
30
44
45} // namespace geventheader
46
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(1, "\n",
88 TPOINTITEM, " Event Number: ", g4localEventNumber, "\n",
89 TPOINTITEM, " Thread ID: ", threadID, "\n",
90 TPOINTITEM, " Time Stamp: ", timeStamp);
91 }
92
105 static std::unique_ptr<GEventHeader> create(const std::shared_ptr<GOptions>& gopts, int tid = -1) {
106 int eventNumber = globalEventHeaderCounter.fetch_add(1, std::memory_order_relaxed);
107 int threadID_ = tid;
108 if (threadID_ < 0) {
109 threadID_ = eventNumber % 8;
110 }
111 return std::make_unique<GEventHeader>(gopts, eventNumber, threadID_);
112 }
113
119 [[nodiscard]] inline std::string getTimeStamp() const { return timeStamp; }
120
129 [[nodiscard]] inline int getG4LocalEvn() const { return g4localEventNumber; }
130
136 [[nodiscard]] inline int getThreadID() const { return threadID; }
137
138private:
140 int g4localEventNumber;
141
143 int threadID;
144
156 static std::string assignTimeStamp() {
157 time_t now = time(nullptr);
158 struct tm* ptm = localtime(&now);
159 char buffer[32];
160 strftime(buffer, 32, "%a %m.%d.%Y %H:%M:%S", ptm);
161 return {buffer};
162 }
163
165 std::string timeStamp;
166
168 static std::atomic<int> globalEventHeaderCounter;
169};
std::shared_ptr< GLogger > log
Stores minimal metadata for one event.
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.
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
constexpr const char * GDATAEVENTHEADER_LOGGER
CONSTRUCTOR
#define TPOINTITEM
GOptions defineOptions()
Defines the options subtree used by the event-header logger domain.