PandaRoot
PndProcessTask.h
Go to the documentation of this file.
1 #ifndef PNDPROCESSTASK_H
2 #define PNDPROCESSTASK_H
3 
4 #include <algorithm>
5 #include <iterator>
6 #include <stdexcept>
7 #include <string>
8 #include <vector>
9 
10 #include "TClonesArray.h"
11 #include "TString.h"
12 
13 #include "FairLogger.h"
14 #include "FairRun.h"
15 #include "FairRuntimeDb.h"
16 
17 #include "PndConstContainer.h"
18 #include "PndMutableContainer.h"
19 #include "PndPersistencyTask.h"
20 #include "PndProcess.h"
21 #include "PndProcessHandler.h"
22 
31  public:
32  PndProcessTask(const std::string &t_detectorName);
33  virtual ~PndProcessTask();
34 
41  virtual InitStatus Init() /*override*/ final;
42 
47  virtual void Exec(Option_t *t_opt = nullptr) /*override*/;
48 
53  virtual void Finish() /*override*/;
54 
55  protected:
60  void SetParContainers() /*override*/ final;
61 
66  virtual void PreProcessing(Option_t * /*unused*/);
67 
72  virtual void PostProcessing(Option_t * /*unused*/);
73 
78  virtual void SetupDataArrays() = 0;
79 
80  template <class T>
81  Bool_t DispatchRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
82  {
83  if (t_container.fClassType == T().ClassName()) {
84  if (!HandledRequest<T>(t_container, t_register, t_defaultName)) {
85  LOG(error) << "PndProcessTask::DispatchRequest(): " << this->fDetectorName << " - No " << t_container.fClassType << " branch found. Aborting.";
86  throw std::exception(); // Exception is catched in PndProcessTask::Init(), which will return kERROR InitStatus
87  }
88  return true;
89  }
90  return false;
91  }
92 
93  TClonesArray *GetInputArray(const TString &t_branchname, const TString &t_classtype)
94  {
95  TClonesArray *tca{nullptr};
96  FairRootManager *ioman = FairRootManager::Instance();
97  if (ioman == nullptr) {
98  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
99  } else {
100  tca = dynamic_cast<TClonesArray *>(ioman->GetObject(t_branchname));
101  if (tca == nullptr) {
102  LOG(error) << "PndTCAConstContainer<" << t_classtype << ">::Init(" << t_branchname << "): No " << t_classtype << " array with name " << t_branchname << " found!";
103  return tca;
104  }
105  }
106  return tca;
107  }
108 
109  TClonesArray *GetOutputArray(const TString &t_branchname, const TString &t_classtype, const TString &t_foldername, Bool_t t_persistenxy)
110  {
111  TClonesArray *tca{nullptr};
112  FairRootManager *ioman = FairRootManager::Instance();
113  if (ioman == nullptr) {
114  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
115  } else {
116  if (ioman->CheckBranch(t_branchname) == 0) {
117  LOG(debug) << "Attempting to register " << t_branchname << " as a container for " << t_classtype << " in folder " << t_foldername;
118  tca = ioman->Register(t_branchname, t_classtype, t_foldername, t_persistenxy);
119  } else {
120  tca = dynamic_cast<TClonesArray *>(ioman->GetObject(t_branchname));
121  if (tca == nullptr) {
122  LOG(error) << "PndTCAMutableContainer<" << t_classtype << ">::Init(" << t_branchname << "): No " << t_classtype << " array with name " << t_branchname
123  << " already exists but could not be fetched!";
124 
125  return tca;
126  }
127  LOG(debug) << "Found a branch already called " << t_branchname << ". Using this for container for " << t_classtype << " in folder " << t_foldername;
128  }
129  }
130  return tca;
131  }
132 
133  template <class T>
134  Bool_t HandledRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
135  {
136 
137  TString branchname = (t_container.RequestsSpecificBranch()) ? t_container.fBranchname : t_defaultName;
138  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType;
139 
140  if (t_register->IsBranchSet(branchname)) {
141  return true;
142  }
143  Bool_t status = kFALSE;
144  if (t_container.fIsOutput) {
145 
147  output->SetPersistency(t_container.fIsPersistent);
148  output->SetTCA(GetOutputArray(branchname, t_container.fClassType, "Emc" + fDetectorName, t_container.fIsPersistent));
149  output->SetBranchName(branchname);
150 
151  t_register->SetOutput(branchname, output);
152  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType << " as output container.";
153  status = kTRUE;
154  } else {
156  input->SetTCA(GetInputArray(branchname, t_container.fClassType));
157  input->SetBranchName(branchname);
158 
159  t_register->SetInput(branchname, input);
160  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType << " as input container.";
161  status = kTRUE;
162  }
163  return status;
164  }
165 
166  private:
167  ClassDef(PndProcessTask, 1);
168 };
169 
170 #endif /*PNDPROCESSTASK_H*/
Task based version of a PndProcessHandler.
virtual void SetupDataArrays()=0
Define which data containers need to be fetched from the FairRootManager, pass the data container add...
virtual void Exec(Option_t *t_opt=nullptr)
Prepare Task for processing of PndProcess (e.g. reset data arrays), process the PndProcesses and do c...
void SetTCA(TClonesArray *t_tca)
Set the TClonesArray address.
void SetInput(const TString &t_branchname, PndContainerI< T > *t_ptr)
virtual void PreProcessing(Option_t *)
Do some task preparation before PndProcesses are run once per event.
Container to wrap PndTCA/STDMutableContainer (not needed anymore)
void SetOutput(const TString &t_branchname, PndContainerI< T > *t_ptr)
void SetTCA(TClonesArray *t_tca)
Set the TClonesArray address.
void SetParContainers() final
Fetch the FairRuntimeDb and populate the PndParameterRegister with all requested parameter. Has to be called before Init()
Bool_t HandledRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
virtual void PostProcessing(Option_t *)
Do some task post clean up after PndProcesses are run once per event.
virtual ~PndProcessTask()
Bool_t DispatchRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
void SetPersistency(Bool_t t_persistency)
Set the Persistency of the data.
virtual void Finish()
End the task, call TearDown for all PndProcesses.
PndTCAMutableContainer implementation of PndMutableContainerI<T> for FairRootManager and TClonesArray...
void SetBranchName(const TString &t_branchname)
Set the Branch Name.
TClonesArray * GetOutputArray(const TString &t_branchname, const TString &t_classtype, const TString &t_foldername, Bool_t t_persistenxy)
PndTCAConstContainer implementation of PndConstContainerI<T> for FairRootManager and TClonesArray...
TClonesArray * GetInputArray(const TString &t_branchname, const TString &t_classtype)
Container to wrap PndTCA/STDConstContainer (not needed anymore)
Base class for classes handling a set of PndProcesses.
PndProcessTask(const std::string &t_detectorName)
std::string fDetectorName
Bool_t IsBranchSet(const TString &t_branchname) const
int status[10]
Definition: f_Init.h:24
virtual InitStatus Init() final
Initialise this task. This must be called AFTER SetParContainers(), otherwise we will try to pass the...