R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
TCAConnector.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#ifndef TCACONNECTOR
15#define TCACONNECTOR
16
17#include "FairRootManager.h"
18#include "TClonesArray.h"
19#include "TString.h"
20#include <exception>
21#include <utility>
22#include <vector>
23
24template <typename T>
26{
27 private:
28 TString fBranchName;
29 TString fClassName;
30 TClonesArray* fTCA; // non-owning
31
32 public:
34 : fBranchName(std::move(b))
35 , fClassName(T().ClassName())
36 , fTCA(nullptr)
37 {
38 }
39
40 void Init()
41 {
42 auto ioman = FairRootManager::Instance();
43 if (ioman == nullptr)
44 {
45 throw std::runtime_error("TCAInputConnector: No FairRootManager");
46 }
47 fTCA = (TClonesArray*)ioman->GetObject(fBranchName);
48 if (fTCA == nullptr)
49 {
50 throw std::runtime_error(("TCAInputConnector: No TClonesArray called " + fBranchName +
51 " could be obtained from the FairRootManager")
52 .Data());
53 }
54 if (!TString(fTCA->GetClass()->GetName()).EqualTo(fClassName))
55 {
56 throw std::runtime_error(
57 ("TCAInputConnector: TClonesArray " + fBranchName + " does not contain elements of type " + fClassName)
58 .Data());
59 }
60 }
61
62 std::vector<T*> Retrieve() const
63 {
64 std::vector<T*> fV;
65 if (fTCA == nullptr)
66 {
67 throw std::runtime_error(
68 ("TCAInputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
69 }
70
71 const Int_t n = fTCA->GetEntries();
72 fV.reserve(n);
73 for (Int_t i = 0; i < n; i++)
74 {
75 fV.emplace_back((T*)fTCA->At(i));
76 }
77 return fV;
78 }
79
80 std::vector<T> RetrieveObjects() const
81 {
82 std::vector<T> fV;
83 if (fTCA == nullptr)
84 {
85 throw std::runtime_error(
86 ("TCAInputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
87 }
88
89 const Int_t n = fTCA->GetEntries();
90 fV.reserve(n);
91 for (Int_t i = 0; i < n; i++)
92 {
93 fV.emplace_back(*(T*)fTCA->At(i));
94 }
95 return fV;
96 }
97};
98
99template <typename T>
101{
102 private:
103 TString fBranchName;
104 TString fClassName;
105 TClonesArray* fTCA; // non-owning
106
107 public:
109 : fBranchName(std::move(b))
110 , fClassName(T().ClassName())
111 , fTCA(nullptr)
112 {
113 }
114
115 void Init()
116 {
117 auto ioman = FairRootManager::Instance();
118 if (ioman == nullptr)
119 {
120 throw std::runtime_error("TCAInputConnector: No FairRootManager");
121 }
122 fTCA = (TClonesArray*)ioman->GetObject(fBranchName);
123 if (fTCA != nullptr && !TString(fTCA->GetClass()->GetName()).EqualTo(fClassName))
124 {
125 throw std::runtime_error(
126 ("TCAInputConnector: TClonesArray " + fBranchName + " does not contain elements of type " + fClassName)
127 .Data());
128 }
129 }
130
131 std::vector<T*> Retrieve() const
132 {
133 std::vector<T*> fV;
134 if (fTCA == nullptr)
135 {
136 return fV;
137 }
138
139 const Int_t n = fTCA->GetEntries();
140 fV.reserve(n);
141 for (Int_t i = 0; i < n; i++)
142 {
143 fV.emplace_back((T*)fTCA->At(i));
144 }
145 return fV;
146 }
147
148 std::vector<T> RetrieveObjects() const
149 {
150 std::vector<T> fV;
151 if (fTCA == nullptr)
152 {
153 return fV;
154 }
155
156 const Int_t n = fTCA->GetEntries();
157 fV.reserve(n);
158 for (Int_t i = 0; i < n; i++)
159 {
160 fV.emplace_back(*(T*)fTCA->At(i));
161 }
162 return fV;
163 }
164};
165
166template <typename T>
168{
169 private:
170 TString fBranchName;
171 TString fClassName;
172 TClonesArray* fTCA; // non-owning
173
174 public:
176 : fBranchName(std::move(b))
177 , fClassName(T().ClassName())
178 , fTCA(nullptr)
179 {
180 }
181
182 void Init()
183 {
184 auto ioman = FairRootManager::Instance();
185 if (ioman == nullptr)
186 {
187 throw std::runtime_error("TCAOutputConnector: No FairRootManager");
188 }
189 fTCA = ioman->Register(fBranchName, fClassName, "", kTRUE);
190
191 if (fTCA == nullptr)
192 {
193 throw std::runtime_error(("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName +
194 "s could not be provided by the FairRootManager")
195 .Data());
196 }
197 }
198
199 void Reset()
200 {
201 if (fTCA == nullptr)
202 {
203 throw std::runtime_error(
204 ("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
205 }
206 fTCA->Clear("C");
207 }
208
209 Int_t Size()
210 {
211 if (fTCA == nullptr)
212 {
213 throw std::runtime_error(
214 ("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
215 }
216 return fTCA->GetEntries();
217 }
218
219 void Insert(T t)
220 {
221 if (fTCA == nullptr)
222 {
223 throw std::runtime_error(
224 ("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
225 }
226 new ((*fTCA)[fTCA->GetEntries()]) T(std::move(t));
227 }
228
229 void Insert(T* t)
230 {
231 if (fTCA == nullptr)
232 {
233 throw std::runtime_error(
234 ("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
235 }
236 if (t != nullptr)
237 {
238 new ((*fTCA)[fTCA->GetEntries()]) T(*t);
239 }
240 }
241
242 void Insert(std::vector<T>& v)
243 {
244 if (fTCA == nullptr)
245 {
246 throw std::runtime_error(
247 ("TCAOutputConnector: TClonesArray " + fBranchName + " of " + fClassName + "s not available").Data());
248 }
249 for (auto& o : v)
250 {
251 new ((*fTCA)[fTCA->GetEntries()]) T(std::move(o));
252 }
253 v.clear();
254 }
255};
256
257#endif // TCACONNECTOR
TCAInputConnector(TString b)
std::vector< T * > Retrieve() const
std::vector< T > RetrieveObjects() const
std::vector< T * > Retrieve() const
std::vector< T > RetrieveObjects() const
TCAOptionalInputConnector(TString b)
void Insert(std::vector< T > &v)
TCAOutputConnector(TString b)