PandaRoot
PndSTDOutputContainer.h
Go to the documentation of this file.
1 
14 #ifndef PNDSTDOUTPUTCONTAINER_HH
15 #define PNDSTDOUTPUTCONTAINER_HH
16 
17 #include <stdexcept>
18 #include <vector>
19 
20 #include "TString.h"
21 
22 #include "FairLogger.h"
23 #include "FairRootManager.h"
24 
25 #include "PndOutputContainerI.h"
26 
34 template <class T>
36  public:
43  PndSTDOutputContainer(const TString &t_foldername = "", Bool_t t_persistency = kTRUE) : PndOutputContainerI<T>(t_foldername, t_persistency) {}
44 
50 
56  virtual Bool_t Init(const TString &t_branchname) final
57  {
58  this->fBranchName = t_branchname;
59  FairRootManager *ioman = FairRootManager::Instance();
60  if (ioman == nullptr) {
61  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
62  } else {
63  if (ioman->CheckBranch(t_branchname) == 0) {
64  this->fSTD = new std::vector<T>{};
65  ioman->RegisterAny(t_branchname, this->fSTD, this->fPersistency);
66  } else {
67  // throw std::runtime_error(("PndSTDOutputContainer<" + TString{T().ClassName()} + ">::Init(" + t_branchname + "): A branch with that name already exists.").Data());
68  LOG(error) << "PndSTDOutputContainer<" << T().ClassName() << ">::Init(" << t_branchname << "): No " << T().ClassName() << " array with name " << t_branchname
69  << " created!";
70  return kFALSE;
71  }
72  }
73  return kTRUE;
74  }
75 
82  virtual T *GetElement(Int_t t_idx) const final
83  {
84  if (fSTD != nullptr) {
85  return &(fSTD->at(t_idx));
86  }
87  return nullptr;
88  }
89 
95  virtual ssize_t GetSize() const final
96  {
97  if (fSTD != nullptr) {
98  return fSTD->size();
99  }
100  return 0;
101  }
102 
109  virtual T *CreateCopy(const T &t_element)
110  {
111  if (fSTD != nullptr) {
112  fSTD->push_back(T{t_element});
113  } else {
114  LOG(ERROR) << "PndSTDInputContainer::CreateCopy() no underlying std::vector";
115  return nullptr;
116  }
117  return GetElement(GetSize() - 1);
118  }
119 
125  virtual void RemoveAt(Int_t t_index) final
126  {
127  if (this->fSTD != nullptr) {
128  this->fSTD->erase(this->fSTD->begin() + t_index);
129  }
130  }
131 
136  virtual void Compress() {}
137 
142  virtual void Reset() final
143  {
144  if (this->fSTD != nullptr) {
145  this->fSTD->clear();
146  }
147  }
148 
154  void SetData(std::vector<T> *t_container) { fSTD = t_container; }
155 
161  std::vector<T> *GetData() { return fSTD; }
162 
163  private:
164  std::vector<T> *fSTD{nullptr};
165 };
166 
167 #endif /*PNDSTDOUTPUTCONTAINER_HH*/
std::vector< T > * GetData()
Get the Data.
TString fBranchName
Definition: PndContainerI.h:84
virtual void Reset() final
"Delete" all elements
virtual T * GetElement(Int_t t_idx) const final
Get the t_idx Element.
PndSTDOutputContainer(const TString &t_foldername="", Bool_t t_persistency=kTRUE)
Construct a new PndSTDOutputContainer object.
virtual void RemoveAt(Int_t t_index) final
remove t_index element
virtual ssize_t GetSize() const final
Get the number of elements.
void SetData(std::vector< T > *t_container)
Set the Data.
virtual void Compress()
Compress.
virtual ~PndSTDOutputContainer()
Destroy the PndSTDOutputContainer object.
virtual T * CreateCopy(const T &t_element)
Create a Copy object of t_element and return pointer to copy.
PndSTDOutputContainer implementation of PndOutputContainerI<T> for FairRootManager and std::vector<T>...
virtual Bool_t Init(const TString &t_branchname) final
Register t_branchname as std::vector<T> * with the FairRootManager.