forked from JeffersonLab/JANA2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JFactoryPodioT.h
211 lines (173 loc) · 7.4 KB
/
JFactoryPodioT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2023, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.
#ifndef JANA2_JFACTORYPODIOT_H
#define JANA2_JFACTORYPODIOT_H
#include <JANA/JFactoryT.h>
#include <podio/Frame.h>
template <typename S> struct PodioTypeMap;
/// The point of this additional base class is to allow us _untyped_ access to the underlying PODIO collection,
/// at the cost of some weird multiple inheritance. The JEvent can trigger the untyped factory using Create(), then
///
class JFactoryPodio {
protected:
const podio::CollectionBase* mCollection = nullptr;
bool mIsSubsetCollection = false;
podio::Frame* mFrame = nullptr;
private:
// Meant to be called internally, from JMultifactory
friend class JMultifactory;
void SetFrame(podio::Frame* frame) { mFrame = frame; }
// Meant to be called internally, from JEvent:
friend class JEvent;
const podio::CollectionBase* GetCollection() { return mCollection; }
public:
// Meant to be called from ctor, or externally, if we are creating a dummy factory such as a multifactory helper
void SetSubsetCollection(bool isSubsetCollection=true) { mIsSubsetCollection = isSubsetCollection; }
};
template <typename T>
class JFactoryPodioT : public JFactoryT<T>, public JFactoryPodio {
public:
using CollectionT = typename PodioTypeMap<T>::collection_t;
private:
// mCollection is owned by the frame.
// mFrame is owned by the JFactoryT<podio::Frame>.
// mData holds lightweight value objects which hold a pointer into mCollection.
// This factory owns these value objects.
public:
explicit JFactoryPodioT();
~JFactoryPodioT() override;
void Init() override {}
void BeginRun(const std::shared_ptr<const JEvent>&) override {}
void ChangeRun(const std::shared_ptr<const JEvent>&) override {}
void Process(const std::shared_ptr<const JEvent>&) override {}
void EndRun() override {}
void Finish() override {}
void Create(const std::shared_ptr<const JEvent>& event) final;
std::type_index GetObjectType() const final { return std::type_index(typeid(T)); }
std::size_t GetNumObjects() const final { return mCollection->size(); }
void ClearData() final;
void SetCollection(CollectionT&& collection);
void SetCollection(std::unique_ptr<CollectionT> collection);
void Set(const std::vector<T*>& aData) final;
void Set(std::vector<T*>&& aData) final;
void Insert(T* aDatum) final;
private:
// This is meant to be called by JEvent::Insert
friend class JEvent;
void SetCollectionAlreadyInFrame(const CollectionT* collection);
};
template <typename T>
JFactoryPodioT<T>::JFactoryPodioT() = default;
template <typename T>
JFactoryPodioT<T>::~JFactoryPodioT() {
// Ownership of mData, mCollection, and mFrame is complicated, so we always handle it via ClearData()
ClearData();
}
template <typename T>
void JFactoryPodioT<T>::SetCollection(typename PodioTypeMap<T>::collection_t&& collection) {
/// Provide a PODIO collection. Note that PODIO assumes ownership of this collection, and the
/// collection pointer should be assumed to be invalid after this call
if (this->mFrame == nullptr) {
throw JException("JFactoryPodioT: Unable to add collection to frame as frame is missing!");
}
const auto& moved = this->mFrame->put(std::move(collection), this->GetTag());
this->mCollection = &moved;
for (const T& item : moved) {
T* clone = new T(item);
this->mData.push_back(clone);
}
this->mStatus = JFactory::Status::Inserted;
this->mCreationStatus = JFactory::CreationStatus::Inserted;
}
template <typename T>
void JFactoryPodioT<T>::SetCollection(std::unique_ptr<typename PodioTypeMap<T>::collection_t> collection) {
/// Provide a PODIO collection. Note that PODIO assumes ownership of this collection, and the
/// collection pointer should be assumed to be invalid after this call
if (this->mFrame == nullptr) {
throw JException("JFactoryPodioT: Unable to add collection to frame as frame is missing!");
}
this->mFrame->put(std::move(collection), this->GetTag());
const auto* moved = &this->mFrame->template get<typename PodioTypeMap<T>::collection_t>(this->GetTag());
this->mCollection = moved;
for (const T& item : *moved) {
T* clone = new T(item);
this->mData.push_back(clone);
}
this->mStatus = JFactory::Status::Inserted;
this->mCreationStatus = JFactory::CreationStatus::Inserted;
}
template <typename T>
void JFactoryPodioT<T>::ClearData() {
if (this->mStatus == JFactory::Status::Uninitialized) {
return;
}
for (auto p : this->mData) {
// Avoid potentially invalid call to ObjBase::release(). The frame and
// all the collections and all Obj may have been deallocated at this point.
p->unlink();
delete p;
}
this->mData.clear();
this->mCollection = nullptr; // Collection is owned by the Frame, so we ignore here
this->mFrame = nullptr; // Frame is owned by the JEvent, so we ignore here
this->mStatus = JFactory::Status::Unprocessed;
this->mCreationStatus = JFactory::CreationStatus::NotCreatedYet;
}
template <typename T>
void JFactoryPodioT<T>::SetCollectionAlreadyInFrame(const CollectionT* collection) {
for (const T& item : *collection) {
T* clone = new T(item);
this->mData.push_back(clone);
}
this->mCollection = collection;
this->mStatus = JFactory::Status::Inserted;
this->mCreationStatus = JFactory::CreationStatus::Inserted;
}
// This free function is used to break the dependency loop between JFactoryPodioT and JEvent.
podio::Frame* GetOrCreateFrame(const std::shared_ptr<const JEvent>& event);
template <typename T>
void JFactoryPodioT<T>::Create(const std::shared_ptr<const JEvent>& event) {
mFrame = GetOrCreateFrame(event);
try {
JFactory::Create(event);
}
catch (...) {
if (mCollection == nullptr) {
// If calling Create() excepts, we still create an empty collection
// so that podio::ROOTFrameWriter doesn't segfault on the null mCollection pointer
SetCollection(typename PodioTypeMap<T>::collection_t());
}
throw;
}
if (mCollection == nullptr) {
SetCollection(typename PodioTypeMap<T>::collection_t());
// If calling Process() didn't result in a call to Set() or SetCollection(), we create an empty collection
// so that podio::ROOTFrameWriter doesn't segfault on the null mCollection pointer
}
}
template <typename T>
void JFactoryPodioT<T>::Set(const std::vector<T*>& aData) {
typename PodioTypeMap<T>::collection_t collection;
if (mIsSubsetCollection) collection.setSubsetCollection(true);
for (T* item : aData) {
collection.push_back(*item);
}
SetCollection(std::move(collection));
}
template <typename T>
void JFactoryPodioT<T>::Set(std::vector<T*>&& aData) {
typename PodioTypeMap<T>::collection_t collection;
if (mIsSubsetCollection) collection.setSubsetCollection(true);
for (T* item : aData) {
collection.push_back(*item);
}
SetCollection(std::move(collection));
}
template <typename T>
void JFactoryPodioT<T>::Insert(T* aDatum) {
typename PodioTypeMap<T>::collection_t collection;
if (mIsSubsetCollection) collection->setSubsetCollection(true);
collection->push_back(*aDatum);
SetCollection(std::move(collection));
}
#endif //JANA2_JFACTORYPODIOT_H