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
10
19constexpr const char* GDATAHEADER_LOGGER = "event_header";
20
21namespace gheader {
22inline GOptions defineOptions() {
23 auto goptions = GOptions(GDATAHEADER_LOGGER);
24 return goptions;
25}
26}
27
28
29class GEventHeader : public GBase<GEventHeader> {
30public:
40 GEventHeader(const std::shared_ptr<GOptions>& gopts, int n, int tid) :
41 GBase(gopts, GDATAHEADER_LOGGER),
42 g4localEventNumber(n),
43 threadID(tid) {
44 timeStamp = assignTimeStamp();
45 log->debug(CONSTRUCTOR, "GEventDataCollectionHeader");
46 log->info(1, "\n",
47 TPOINTITEM, " Event Number: ", g4localEventNumber, "\n",
48 TPOINTITEM, " Thread ID: ", threadID, "\n",
49 TPOINTITEM, " Time Stamp: ", timeStamp);
50 }
51
57 static std::unique_ptr<GEventHeader> create(const std::shared_ptr<GOptions>& gopts, int tid = -1) {
58 int eventNumber = globalEventHeaderCounter.fetch_add(1, std::memory_order_relaxed);
59 int threadID = tid; // Example: cycle through 3 thread IDs
60 if (threadID < 0) {
61 threadID = eventNumber % 8; // default to 8 threads if not provided
62 }
63
64 return std::make_unique<GEventHeader>(gopts, eventNumber, threadID);
65 }
66
71 [[nodiscard]] inline std::string getTimeStamp() const { return timeStamp; }
72
77 [[nodiscard]] inline int getG4LocalEvn() const { return g4localEventNumber; }
78
83 [[nodiscard]] inline int getThreadID() const { return threadID; }
84
85private:
86 int g4localEventNumber;
87 int threadID;
88
96 static std::string assignTimeStamp() {
97 time_t now = time(nullptr);
98 struct tm* ptm = localtime(&now);
99 char buffer[32];
100 // Format: Mo, 15.06.2009 20:20:00
101 strftime(buffer, 32, "%a, %m.%d.%Y %H:%M:%S", ptm);
102 return {buffer};
103 }
104
105 std::string timeStamp;
106
108 static std::atomic<int> globalEventHeaderCounter;
109};
static std::unique_ptr< GEventHeader > create(const std::shared_ptr< GOptions > &gopts, int tid=-1)
Factory method to create a GEventDataCollectionHeader with a unique event number.
std::string getTimeStamp() const
Gets the timestamp.
int getThreadID() const
Gets the thread ID.
GEventHeader(const std::shared_ptr< GOptions > &gopts, int n, int tid)
Constructs a GEventDataCollectionHeader.
int getG4LocalEvn() const
Gets the local event number.
constexpr const char * GDATAHEADER_LOGGER
GOptions defineOptions()