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
32constexpr const char* GDATAEVENTHEADER_LOGGER = "event_header";
33
34namespace geventheader {
47} // namespace geventheader
48
59class GEventHeader : public GBase<GEventHeader>
60{
61public:
74 GEventHeader(const std::shared_ptr<GOptions>& gopts, int n, int tid)
75 : GBase(gopts, GDATAEVENTHEADER_LOGGER), g4localEventNumber(n), threadID(tid) {
76 timeStamp = assignTimeStamp();
77 log->debug(CONSTRUCTOR, "GEventHeader");
78 log->info(1, "\n",
79 TPOINTITEM, " Event Number: ", g4localEventNumber, "\n",
80 TPOINTITEM, " Thread ID: ", threadID, "\n",
81 TPOINTITEM, " Time Stamp: ", timeStamp);
82 }
83
98 static std::unique_ptr<GEventHeader> create(const std::shared_ptr<GOptions>& gopts, int tid = -1) {
99 int eventNumber = globalEventHeaderCounter.fetch_add(1, std::memory_order_relaxed);
100 int threadID_ = tid;
101 if (threadID_ < 0) {
102 threadID_ = eventNumber % 8; // default to 8 threads if not provided
103 }
104 return std::make_unique<GEventHeader>(gopts, eventNumber, threadID_);
105 }
106
111 [[nodiscard]] inline std::string getTimeStamp() const { return timeStamp; }
112
121 [[nodiscard]] inline int getG4LocalEvn() const { return g4localEventNumber; }
122
127 [[nodiscard]] inline int getThreadID() const { return threadID; }
128
129private:
130 int g4localEventNumber;
131 int threadID;
132
145 static std::string assignTimeStamp() {
146 time_t now = time(nullptr);
147 struct tm* ptm = localtime(&now);
148 char buffer[32];
149 strftime(buffer, 32, "%a %m.%d.%Y %H:%M:%S", ptm);
150 return {buffer};
151 }
152
153 std::string timeStamp;
154
156 static std::atomic<int> globalEventHeaderCounter;
157};
std::shared_ptr< GLogger > log
Minimal event metadata header: event number, thread id, and timestamp.
static std::unique_ptr< GEventHeader > create(const std::shared_ptr< GOptions > &gopts, int tid=-1)
Factory method used by examples/tests to create a header with a unique event number.
std::string getTimeStamp() const
Get the formatted timestamp string.
int getThreadID() const
Get the thread ID associated with this event.
GEventHeader(const std::shared_ptr< GOptions > &gopts, int n, int tid)
Construct an event header with explicit values.
int getG4LocalEvn() const
Get 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 GOptions for the event-header logger domain.