PandaRoot
PndContainerRegister.h
Go to the documentation of this file.
1 #ifndef PNDCONTAINERREGISTER_HH
2 #define PNDCONTAINERREGISTER_HH
3 
4 #include <RtypesCore.h>
5 #include <map>
6 
7 #include "PndMutableContainer.h"
8 #include "TString.h"
9 
10 #include "FairLogger.h"
11 
12 #include "PndConstContainerI.h"
13 #include "PndContainerBaseI.h"
14 #include "PndMutableContainerI.h"
15 
17  public:
19  TString fBranchname{""};
20  TString fClassType{""};
21  Bool_t fIsOutput{kFALSE};
22  Bool_t fIsPersistent{kTRUE};
23  Bool_t RequestsSpecificBranch() const { return fBranchname != ""; }
24  TString GetKey() const { return fBranchname + fClassType; }
25  };
26 
27  void Request(const PndContainerType_t &t_container)
28  {
29  auto pos = fRequests.find(t_container.GetKey());
30  if (pos != fRequests.end()) {
31  if ((*pos).second.fClassType != t_container.fClassType) {
32  LOG(fatal) << "Attempting to request branch " << t_container.fBranchname << " for " << t_container.fClassType << " but branchname already used for "
33  << (*pos).second.fClassType << ". Ignoring this request!";
34  } else {
35  (*pos).second.fIsOutput |= t_container.fIsOutput;
36  (*pos).second.fIsPersistent |= t_container.fIsPersistent;
37  LOG(debug) << "Setting request for an output branch " << t_container.fBranchname << " for " << t_container.fClassType;
38  }
39  } else {
40  fRequests[t_container.GetKey()] = t_container;
41  LOG(debug) << "Setting request for branch " << t_container.fBranchname << " for " << t_container.fClassType;
42  }
43  }
44 
45  Bool_t IsBranchSet(const TString &t_branchname) const
46  {
47  auto pos = fInputs.find(t_branchname);
48  if (pos != fInputs.end()) {
49  return (pos->second) != nullptr;
50  }
51  auto opos = fOutputs.find(t_branchname);
52  if (opos != fOutputs.end()) {
53  return (opos->second) != nullptr;
54  }
55  return false;
56  }
57 
58  template <class T>
59  PndMutableContainerI<T> *GetOutput(const TString &t_branchname)
60  {
61  LOG(debug) << "PndContainerRegister::GetOutput(" << t_branchname << ") for " << T{}.ClassName();
62 
63  if (t_branchname == "" || fDefaultBranches[T{}.ClassName()] == t_branchname) {
64  LOG(debug) << "PndContainerRegister::GetOutput(" << t_branchname << ") from default register";
65  auto *defaultBranch = GetDefaultBranch<T>();
66  if (dynamic_cast<PndMutableContainerI<T> *>(defaultBranch)) {
67  return dynamic_cast<PndMutableContainerI<T> *>(defaultBranch);
68  } else {
69  LOG(error) << "Attempted to cast a InputContainer " << t_branchname << ") for " << T{}.ClassName() << " to PndMutableContainerI! Returning nullptr!";
70  return nullptr;
71  }
72  }
73  return dynamic_cast<PndMutableContainerI<T> *>(fOutputs[t_branchname]);
74  }
75 
76  template <class T>
77  void SetOutput(const TString &t_branchname, PndContainerI<T> *t_ptr)
78  {
79  const TString classname = t_ptr->GetClassName();
80  fOutputs[t_branchname] = t_ptr;
81  if (fDefaultBranches.find(classname) == fDefaultBranches.end()) {
82  SetAsDefaultBranchFor(t_branchname, classname);
83  }
84  }
85 
86  template <class T>
87  PndContainerI<T> *GetInput(const TString &t_branchname)
88  {
89  LOG(debug) << "PndContainerRegister::GetInput(" << t_branchname << ") for " << T{}.ClassName();
90  if (t_branchname == "" || fDefaultBranches[T{}.ClassName()] == t_branchname) {
91  LOG(debug) << "PndContainerRegister::GetInput(" << t_branchname << ") from default register";
92 
93  return dynamic_cast<PndContainerI<T> *>(GetDefaultBranch<T>());
94  }
95  return dynamic_cast<PndContainerI<T> *>(fInputs[t_branchname]);
96  }
97 
98  template <class T>
99  void SetInput(const TString &t_branchname, PndContainerI<T> *t_ptr)
100  {
101  const TString classname = t_ptr->GetClassName();
102  fInputs[t_branchname] = t_ptr;
103  if (fDefaultBranches.find(classname) == fDefaultBranches.end()) {
104  SetAsDefaultBranchFor(t_branchname, classname);
105  }
106  }
107 
108  void SetAsDefaultBranchFor(const TString &t_branchname, const TString &t_class)
109  {
110  LOG(info) << "PndContainerRegister::SetAsDefaultBranchFor() Setting " << t_branchname << " as active branch for " << t_class;
111  fDefaultBranches[t_class] = t_branchname;
112  }
113 
114  template <class T>
116  {
117  const TString classname = T{}.ClassName();
118  const TString &defaultBranch = fDefaultBranches[classname];
119  LOG(debug) << "PndContainerRegister::GetDefaultBranch() Returning default branch " << defaultBranch << " for " << classname;
120 
121  auto pos = fInputs.find(defaultBranch);
122  if (pos != fInputs.end()) {
123  return dynamic_cast<PndContainerI<T> *>(pos->second);
124  }
125  return dynamic_cast<PndContainerI<T> *>(fOutputs[defaultBranch]);
126  }
127 
128  const TString &GetCurrentDefaultBranchName(const TString &t_classname) const { return fDefaultBranches.at(t_classname); }
129 
130  template <class T>
131  const TString &GetCurrentDefaultBranchName() const
132  {
133  const TString classname = T{}.ClassName();
134  return GetCurrentDefaultBranchName(classname);
135  }
136 
137  const std::map<TString, PndContainerType_t> &GetRequests() { return fRequests; }
138  const std::map<TString, TString> &GetBranches() { return fContainerOfType; }
139  std::map<TString, PndContainerBaseI *> &Inputs() { return fInputs; }
140  std::map<TString, PndContainerBaseI *> &Outputs() { return fOutputs; }
141 
142  private:
143  std::map<TString, PndContainerType_t> fRequests{};
144  std::map<TString, TString> fDefaultBranches{};
145  std::map<TString, TString> fContainerOfType{};
146  std::map<TString, PndContainerBaseI *> fInputs{};
147  std::map<TString, PndContainerBaseI *> fOutputs{};
148 };
149 
150 #endif /*PNDCONTAINERREGISTER_HH*/
PndContainerI< T > * GetDefaultBranch()
const std::map< TString, PndContainerType_t > & GetRequests()
void SetInput(const TString &t_branchname, PndContainerI< T > *t_ptr)
Container to wrap PndTCA/STDMutableContainer (not needed anymore)
void SetOutput(const TString &t_branchname, PndContainerI< T > *t_ptr)
void Request(const PndContainerType_t &t_container)
Interface to a datacontainer to be used in PandaROOT.
const TString & GetCurrentDefaultBranchName(const TString &t_classname) const
std::map< TString, PndContainerBaseI * > & Inputs()
TString GetClassName() const
Definition: PndContainerI.h:63
const std::map< TString, TString > & GetBranches()
PndContainerI< T > * GetInput(const TString &t_branchname)
const TString & GetCurrentDefaultBranchName() const
PndMutableContainerI< T > * GetOutput(const TString &t_branchname)
std::map< TString, PndContainerBaseI * > & Outputs()
Bool_t IsBranchSet(const TString &t_branchname) const
void SetAsDefaultBranchFor(const TString &t_branchname, const TString &t_class)