PandaRoot
PndTCAOutputContainer.h
Go to the documentation of this file.
1 
14 #ifndef PNDTCAOUTPUTCONTAINER_HH
15 #define PNDTCAOUTPUTCONTAINER_HH
16 
17 #include <stdexcept>
18 #include <vector>
19 
20 #include "TClonesArray.h"
21 #include "TString.h"
22 
23 #include "FairLogger.h"
24 #include "FairRootManager.h"
25 
26 #include "PndOutputContainerI.h"
27 
35 template <class T>
37  public:
44  PndTCAOutputContainer(const TString &t_foldername = "", Bool_t t_persistency = kTRUE) : PndOutputContainerI<T>(t_foldername, t_persistency) {}
45 
51 
57  virtual Bool_t Init(const TString &t_branchname) final
58  {
59  this->fBranchName = t_branchname;
60  FairRootManager *ioman = FairRootManager::Instance();
61  if (ioman == nullptr) {
62  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
63  } else {
64  if (ioman->CheckBranch(t_branchname) == 0) {
65  LOG(debug) << "Attempting to register " << t_branchname << " as a container for " << T().ClassName() << " in folder " << this->fFolderName;
66  this->fTCA = ioman->Register(t_branchname, T().ClassName(), this->fFolderName, this->fPersistency);
67  } else {
68  this->fTCA = dynamic_cast<TClonesArray *>(ioman->GetObject(t_branchname));
69  if (this->fTCA == nullptr) {
70  // throw std::runtime_error(
71  // ("PndTCAInputContainer<" + TString{T().ClassName()} + ">::Init(" + t_branchname + "): No " + TString{T().ClassName()} + " array with name " + t_branchname + "
72  // found!")
73  // .Data());
74  LOG(error) << "PndTCAOutputContainer<" << T().ClassName() << ">::Init(" << t_branchname << "): No " << T().ClassName() << " array with name " << t_branchname
75  << " already exists but could not be fetched!";
76 
77  return kFALSE;
78  }
79  LOG(debug) << "Found a branch already called " << t_branchname << ". Using this for container for " << T().ClassName() << " in folder " << this->fFolderName;
80  }
81  }
82  return kTRUE;
83  }
84 
89  virtual void Reset() final
90  {
91  if (this->fTCA != nullptr) {
92  this->fTCA->Delete();
93  }
94  }
101  virtual T *CreateCopy(const T &t_element)
102  {
103  if (fTCA != nullptr) {
104  new ((*fTCA)[fTCA->GetEntriesFast()]) T{t_element};
105  } else {
106  LOG(ERROR) << "PndTCAOutputContainer::CreateCopy() no underlying std::vector";
107  return nullptr;
108  }
109  return GetElement(GetSize() - 1);
110  }
111 
117  virtual void RemoveAt(Int_t t_index)
118  {
119  if (this->fTCA != nullptr) {
120  this->fTCA->RemoveAt(t_index);
121  }
122  }
123 
128  virtual void Compress()
129  {
130  if (this->fTCA != nullptr) {
131  this->fTCA->Compress();
132  }
133  }
134 
141  virtual T *GetElement(Int_t t_idx) const final
142  {
143  if (fTCA != nullptr) {
144  return static_cast<T *>(fTCA->At(t_idx));
145  }
146  return nullptr;
147  }
148 
154  virtual ssize_t GetSize() const final
155  {
156  if (fTCA != nullptr) {
157  return fTCA->GetEntriesFast();
158  }
159  return 0;
160  }
161 
167  void SetTCA(TClonesArray *t_tca) { fTCA = t_tca; }
168 
174  TClonesArray *GetTCA() { return fTCA; }
175 
176  protected:
177  TClonesArray *fTCA{nullptr};
178 };
179 
180 #endif /*PNDTCAOUTPUTCONTAINER_HH*/
virtual Bool_t Init(const TString &t_branchname) final
Register the data t_branchname in form of TClonesArray * with the FairRootManager.
virtual void Compress()
Compress.
TString fBranchName
Definition: PndContainerI.h:84
void SetTCA(TClonesArray *t_tca)
Set the TClonesArray address.
virtual T * CreateCopy(const T &t_element)
Create a copy of t_element in the TClonesArray and return a pointer to it.
virtual void Reset() final
Delete all elements.
virtual void RemoveAt(Int_t t_index)
remove the t_index-th element
PndTCAOutputContainer(const TString &t_foldername="", Bool_t t_persistency=kTRUE)
Construct a new PndTCAOutputContainer object.
PndTCAOutputContainer implementation of PndOutputContainerI<T> for FairRootManager and TClonesArray...
virtual ssize_t GetSize() const final
Get the number of elements.
virtual ~PndTCAOutputContainer()
Destroy the PndTCAOutput Container object.
virtual T * GetElement(Int_t t_idx) const final
Get pointer to the t_idx-th element.
TClonesArray * GetTCA()
Get the TClonesArray address.