Skip to content

Commit 0d29ba9

Browse files
committed
temp commit, saving progress on using smart pointers on data
1 parent 15f0802 commit 0d29ba9

45 files changed

Lines changed: 516 additions & 412 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build_gemc_fedora.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616
jobs:
1717
build-on-fedora:
1818
runs-on: ubuntu-latest
19-
container: jeffersonlab/geant4:g4v11.3.1-fedora36
19+
container: jeffersonlab/geant4:g4v11.3.1-fedora40
2020

2121
steps:
2222
- name: Checkout

.github/workflows/doxygen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
jobs:
1515
build-doxygen:
1616
runs-on: ubuntu-latest
17-
container: jeffersonlab/base:fedora36
17+
container: jeffersonlab/base:fedora40
1818
steps:
1919
- name: Checkout
2020
uses: actions/checkout@main

.github/workflows/sanitize.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ jobs:
2121
container:
2222
- name: ubuntu24
2323
image: jeffersonlab/geant4:g4v11.3.1-ubuntu24
24-
- name: fedora36
25-
image: jeffersonlab/geant4:g4v11.3.1-fedora36
26-
- name: almalinux93
24+
- name: fedora40
25+
image: jeffersonlab/geant4:g4v11.3.1-fedora40
26+
- name: almalinux94
2727
image: jeffersonlab/geant4:g4v11.3.1-almalinux94
2828
# NOTE: additional packages needed for the sanitizer: libasan libubsan libtsan
2929
# MacOS: address, thread, undefined

ci/doxygen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Purpose: creates the doxygen documentation for the selected classes
44

55
# Container run:
6-
# docker_run_image jeffersonlab/base:fedora36
6+
# docker_run_image jeffersonlab/base:fedora40
77
#
88
# local build:
99
# git clone http://github.com/gemc/src /root/src && cd /root/src

ci/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Purpose: compiles, installs gemc, run tests
44

55
# Container run:
6-
# docker_run_image jeffersonlab/geant4:g4v11.3.0-almalinux93
6+
# docker_run_image jeffersonlab/geant4:g4v11.3.0-almalinux94
77
#
88
# local build:
99
# git clone http://github.com/gemc/src /root/src && cd /root/src

gdata/event/gDataCollection.h

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,72 @@
1010

1111
#include "gTrueInfoData.h"
1212
#include "gDigitizedData.h"
13+
14+
// c++
15+
#include <memory>
1316
#include <vector>
1417

18+
1519
class GDataCollection {
1620
public:
1721
/**
1822
* \brief Constructs a GDataCollection.
1923
* \param logger Pointer to a GLogger instance.
2024
*/
21-
explicit GDataCollection(std::shared_ptr<GLogger> logger) : log(logger) {
22-
log->debug(CONSTRUCTOR, "GDataCollection");
23-
trueInfosData = new std::vector<GTrueInfoData*>;
24-
digitizedData = new std::vector<GDigitizedData*>;
25-
}
25+
explicit GDataCollection(std::shared_ptr<GLogger> logger)
26+
: log(logger) { log->debug(CONSTRUCTOR, "GDataCollection"); }
2627

2728
/**
2829
* \brief Destructor for GDataCollection.
2930
*
30-
* Deletes all stored hit data and the associated containers.
31+
* Smart pointers clean up automatically.
3132
*/
32-
~GDataCollection() {
33-
for (auto* hit : (*trueInfosData)) { delete hit; }
34-
for (auto* hit : (*digitizedData)) { delete hit; }
35-
log->debug(DESTRUCTOR, "GDataCollection");
36-
delete trueInfosData;
37-
delete digitizedData;
38-
}
33+
~GDataCollection() { if (log) log->debug(DESTRUCTOR, "GDataCollection"); }
3934

4035
/**
4136
* \brief Adds true hit information data.
42-
* \param data Pointer to GTrueInfoData.
37+
* \param data Unique pointer to GTrueInfoData.
4338
*/
44-
void addTrueInfoData(GTrueInfoData* data) const {
39+
void addTrueInfoData(std::unique_ptr<GTrueInfoData> data) {
4540
log->debug(NORMAL, " adding hit to trueInfosData with identity: ", data->getIdentityString());
46-
trueInfosData->push_back(data);
41+
trueInfosData.push_back(std::move(data)); // taking ownership of the unique_ptr
4742
}
4843

4944
/**
5045
* \brief Adds digitized hit data.
51-
* \param data Pointer to GDigitizedData.
46+
* \param data Unique pointer to GDigitizedData.
5247
*/
53-
void addDigitizedData(GDigitizedData* data) const {
48+
void addDigitizedData(std::unique_ptr<GDigitizedData> data) {
5449
log->debug(NORMAL, " adding hit to digitizedData with identity: ", data->getIdentityString());
55-
digitizedData->push_back(data);
50+
digitizedData.push_back(std::move(data)); // taking ownership of the unique_ptr
5651
}
5752

5853
/**
59-
* \brief Returns the vector of true hit information data.
60-
* \return Pointer to the vector of GTrueInfoData pointers.
54+
* \brief Provides read-only access to the stored true hit data.
55+
*
56+
* Returns a constant reference to the internal vector of unique pointers
57+
* to GTrueInfoData objects. Ownership of the data remains with this class.
58+
* Callers may inspect the data via the pointers but must not modify or
59+
* take ownership of them.
60+
*
61+
* \return Const reference to the vector of unique_ptr<GTrueInfoData>.
6162
*/
62-
[[nodiscard]] inline const std::vector<GTrueInfoData*>* getTrueInfoData() const { return trueInfosData; }
63+
[[nodiscard]] inline const std::vector<std::unique_ptr<GTrueInfoData>>& getTrueInfoData() const { return trueInfosData; }
6364

6465
/**
65-
* \brief Returns the vector of digitized hit data.
66-
* \return Pointer to the vector of GDigitizedData pointers.
66+
* \brief Provides read-only access to the stored digitized hit data.
67+
*
68+
* Returns a constant reference to the internal vector of unique pointers
69+
* to GDigitizedData objects. Ownership of the data remains with this class.
70+
* Callers may read the data but must not modify or transfer ownership.
71+
*
72+
* \return Const reference to the vector of unique_ptr<GDigitizedData>.
6773
*/
68-
[[nodiscard]] inline const std::vector<GDigitizedData*>* getDigitizedData() const { return digitizedData; }
74+
[[nodiscard]] inline const std::vector<std::unique_ptr<GDigitizedData>>& getDigitizedData() const { return digitizedData; }
6975

7076
private:
71-
std::vector<GTrueInfoData*>* trueInfosData = nullptr; ///< Vector of true hit data.
72-
std::vector<GDigitizedData*>* digitizedData = nullptr; ///< Vector of digitized hit data.
73-
std::shared_ptr<GLogger> log; ///< Logger instance
74-
};
77+
std::vector<std::unique_ptr<GTrueInfoData>> trueInfosData; ///< Vector of true hit data.
78+
std::vector<std::unique_ptr<GDigitizedData>> digitizedData; ///< Vector of digitized hit data.
79+
std::shared_ptr<GLogger> log; ///< Logger instance
7580

81+
};

gdata/event/gDigitizedData.cc

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,38 @@
55

66
#include "gDigitizedData.h"
77
#include "gdataConventions.h"
8+
9+
// c++
810
#include <string>
911
#include <map>
1012
#include <vector>
1113

12-
GDigitizedData::GDigitizedData(GHit *ghit, const std::shared_ptr<GLogger>& logger) : log(logger) {
14+
std::atomic<int> GDigitizedData::globalDigitizedDataCounter{0};
15+
16+
GDigitizedData::GDigitizedData(const std::unique_ptr<GHit>& ghit, const std::shared_ptr<GLogger>& logger) : log(logger) {
1317
log->debug(CONSTRUCTOR, "GDigitizedData");
1418
gidentity = ghit->getGID();
1519
}
1620

1721
std::map<std::string, int> GDigitizedData::getIntObservablesMap(int which) const {
1822
std::map<std::string, int> filteredIntObservablesMap;
19-
for (const auto& [varName, value] : intObservablesMap) {
20-
if (validVarName(varName, which)) {
21-
filteredIntObservablesMap[varName] = value;
22-
}
23-
}
23+
for (const auto& [varName, value] : intObservablesMap) { if (validVarName(varName, which)) { filteredIntObservablesMap[varName] = value; } }
2424
log->debug(NORMAL, " getting ", which, " from intObservablesMap.");
2525
return filteredIntObservablesMap;
2626
}
2727

2828
std::map<std::string, double> GDigitizedData::getDblObservablesMap(int which) const {
2929
std::map<std::string, double> filteredDblObservablesMap;
30-
for (const auto& [varName, value] : doubleObservablesMap) {
31-
if (validVarName(varName, which)) {
32-
filteredDblObservablesMap[varName] = value;
33-
}
34-
}
30+
for (const auto& [varName, value] : doubleObservablesMap) { if (validVarName(varName, which)) { filteredDblObservablesMap[varName] = value; } }
3531
log->debug(NORMAL, " getting ", which, " from doubleObservablesMap.");
3632
return filteredDblObservablesMap;
3733
}
3834

3935
bool GDigitizedData::validVarName(const std::string& varName, int which) {
4036
bool isSROVar = (varName == CRATESTRINGID || varName == SLOTSTRINGID || varName == CHANNELSTRINGID ||
41-
varName == CHARGEATELECTRONICS || varName == TIMEATELECTRONICS);
42-
if (which == 0) {
43-
if (isSROVar) {
44-
return false;
45-
}
46-
} else if (which == 1) {
47-
if (!isSROVar) {
48-
return false;
49-
}
50-
}
37+
varName == CHARGEATELECTRONICS || varName == TIMEATELECTRONICS);
38+
if (which == 0) { if (isSROVar) { return false; } }
39+
else if (which == 1) { if (!isSROVar) { return false; } }
5140
return true;
5241
}
5342

@@ -62,9 +51,7 @@ void GDigitizedData::includeVariable(const std::string& vname, double value) {
6251
}
6352

6453
int GDigitizedData::getTimeAtElectronics() {
65-
if (intObservablesMap.find(TIMEATELECTRONICS) == intObservablesMap.end()) {
66-
return TIMEATELECTRONICSNOTDEFINED;
67-
}
54+
if (intObservablesMap.find(TIMEATELECTRONICS) == intObservablesMap.end()) { return TIMEATELECTRONICSNOTDEFINED; }
6855
log->debug(NORMAL, "Getting TIMEATELECTRONICS from intObservablesMap.");
6956
return intObservablesMap[TIMEATELECTRONICS];
7057
}
@@ -85,9 +72,7 @@ double GDigitizedData::getDblObservable(const std::string& varName) {
8572

8673
std::string GDigitizedData::getIdentityString() {
8774
std::string identifierString;
88-
for (size_t i = 0; i < gidentity.size() - 1; i++) {
89-
identifierString += gidentity[i].getName() + "->" + std::to_string(gidentity[i].getValue()) + ", ";
90-
}
75+
for (size_t i = 0; i < gidentity.size() - 1; i++) { identifierString += gidentity[i].getName() + "->" + std::to_string(gidentity[i].getValue()) + ", "; }
9176
identifierString += gidentity.back().getName() + "->" + std::to_string(gidentity.back().getValue());
9277
return identifierString;
9378
}

gdata/event/gDigitizedData.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@
88
* It maintains maps for single-value and array observables keyed by variable names.
99
*/
1010

11+
// c++
1112
#include <string>
1213
#include <map>
1314
#include <vector>
15+
#include <atomic>
16+
17+
// gemc
1418
#include "ghit.h"
1519
#include "glogger.h"
20+
#include "gdataConventions.h"
1621

1722
class GDigitizedData {
1823
public:
1924
/**
2025
* \brief Constructs a GDigitizedData object from a GHit.
21-
* \param ghit Pointer to the GHit from which identity information is extracted.
26+
* \param ghit unique_ptr GHit from which identity information is extracted.
2227
* \param logger Pointer to a GLogger instance.
2328
*/
24-
GDigitizedData(GHit* ghit, const std::shared_ptr<GLogger>& logger);
29+
GDigitizedData(const std::unique_ptr<GHit>& ghit, const std::shared_ptr<GLogger>& logger);
2530

2631
/**
2732
* \brief Destructor for GDigitizedData.
@@ -77,6 +82,20 @@ class GDigitizedData {
7782
*/
7883
[[nodiscard]] inline std::map<std::string, std::vector<double>> getArrayDblObservablesMap() const { return arrayDoubleObservablesMap; }
7984

85+
86+
static std::unique_ptr<GDigitizedData> create(std::shared_ptr<GLogger> logger) {
87+
auto hit = GHit::create(logger);
88+
auto digi_data = std::make_unique<GDigitizedData>(hit, logger);
89+
auto counter = globalDigitizedDataCounter.fetch_add(1, std::memory_order_relaxed);
90+
91+
digi_data->includeVariable(CRATESTRINGID, counter % 10);
92+
digi_data->includeVariable(SLOTSTRINGID, counter % 20);
93+
digi_data->includeVariable(CHANNELSTRINGID, counter);
94+
digi_data->includeVariable(TIMEATELECTRONICS, counter * 5);
95+
digi_data->includeVariable("adc", counter * 0.1);
96+
return digi_data;
97+
}
98+
8099
private:
81100
std::map<std::string, int> intObservablesMap; ///< Map of integer observables.
82101
std::map<std::string, double> doubleObservablesMap; ///< Map of double observables.
@@ -85,4 +104,7 @@ class GDigitizedData {
85104
std::vector<GIdentifier> gidentity; ///< Identity extracted from the hit.
86105
[[nodiscard]] static bool validVarName(const std::string& varName, int which); ///< Validates variable names.
87106
std::shared_ptr<GLogger> log; ///< Logger instance
107+
108+
/// Static thread-safe event counter - used for testing only
109+
static std::atomic<int> globalDigitizedDataCounter;
88110
};

gdata/event/gEventDataCollection.cc

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
44
*/
55

66
#include "gEventDataCollection.h"
7+
#include "gEventHeader.h"
78

8-
void GEventDataCollection::addDetectorTrueInfoData(std::string sdName, GTrueInfoData* data) {
9+
std::atomic<int> GEventDataCollection::globalEventDataCollectionCounter{1};
10+
std::atomic<int> GEventHeader::globalEventHeaderCounter{1};
11+
12+
void GEventDataCollection::addDetectorTrueInfoData(std::string sdName, std::unique_ptr<GTrueInfoData> data) {
913
if (gdataCollectionMap.find(sdName) == gdataCollectionMap.end()) { gdataCollectionMap[sdName] = std::make_unique<GDataCollection>(log); }
10-
gdataCollectionMap[sdName]->addTrueInfoData(data);
14+
gdataCollectionMap[sdName]->addTrueInfoData(std::move(data));
1115
log->info(2, "GEventDataCollection: added new detector TrueInfoData for ", sdName);
1216
}
1317

14-
void GEventDataCollection::addDetectorDigitizedData(std::string sdName, GDigitizedData* data) {
18+
void GEventDataCollection::addDetectorDigitizedData(std::string sdName, std::unique_ptr<GDigitizedData> data) {
1519
if (gdataCollectionMap.find(sdName) == gdataCollectionMap.end()) { gdataCollectionMap[sdName] = std::make_unique<GDataCollection>(log); }
16-
gdataCollectionMap[sdName]->addDigitizedData(data);
20+
gdataCollectionMap[sdName]->addDigitizedData(std::move(data));
1721
log->info(2, "GEventDataCollection: added new detector DigitizedData for ", sdName);
1822
}
1923

20-
21-
const std::vector<GTrueInfoData*>* GEventDataCollection::getTrueInfoDataForDetector(std::string detector) const {
22-
auto it = gdataCollectionMap.find(detector);
23-
if (it != gdataCollectionMap.end()) { return it->second->getTrueInfoData(); }
24-
return nullptr;
25-
}
26-
27-
const std::vector<GDigitizedData*>* GEventDataCollection::getDigitizedDataForDetector(std::string detector) const {
28-
auto it = gdataCollectionMap.find(detector);
29-
if (it != gdataCollectionMap.end()) { return it->second->getDigitizedData(); }
30-
return nullptr;
31-
}

0 commit comments

Comments
 (0)