PandaRoot
PndProcessTask.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 #ifndef PNDPROCESSTASK_H
14 #define PNDPROCESSTASK_H
15 
16 #include <algorithm>
17 #include <iterator>
18 #include <stdexcept>
19 #include <string>
20 #include <vector>
21 
22 #include "TClonesArray.h"
23 #include "TString.h"
24 
25 #include "FairLogger.h"
26 #include "FairRun.h"
27 #include "FairRuntimeDb.h"
28 
29 #include "PndConstContainer.h"
30 #include "PndMutableContainer.h"
31 #include "PndPersistencyTask.h"
32 #include "PndProcess.h"
33 #include "PndProcessHandler.h"
34 
43  public:
44  PndProcessTask(const std::string &t_detectorName);
45  virtual ~PndProcessTask();
46 
53  virtual InitStatus Init() /*override*/ final;
54 
59  virtual void Exec(Option_t *t_opt = nullptr) /*override*/;
60 
65  virtual void Finish() /*override*/;
66 
67  protected:
72  void SetParContainers() /*override*/ final;
73 
78  virtual void PreProcessing(Option_t * /*unused*/);
79 
84  virtual void PostProcessing(Option_t * /*unused*/);
85 
90  virtual void SetupDataArrays() = 0;
91 
92  template <class T>
93  Bool_t DispatchRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
94  {
95  if (t_container.fClassType == T().ClassName()) {
96  if (!HandledRequest<T>(t_container, t_register, t_defaultName)) {
97  LOG(error) << "PndProcessTask::DispatchRequest(): " << this->fDetectorName << " - No " << t_container.fClassType << " branch found. Aborting.";
98  throw std::exception(); // Exception is catched in PndProcessTask::Init(), which will return kERROR InitStatus
99  }
100  return true;
101  }
102  return false;
103  }
104 
105  TClonesArray *GetInputArray(const TString &t_branchname, const TString &t_classtype)
106  {
107  TClonesArray *tca{nullptr};
108  FairRootManager *ioman = FairRootManager::Instance();
109  if (ioman == nullptr) {
110  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
111  } else {
112  tca = dynamic_cast<TClonesArray *>(ioman->GetObject(t_branchname));
113  if (tca == nullptr) {
114  LOG(error) << "PndTCAConstContainer<" << t_classtype << ">::Init(" << t_branchname << "): No " << t_classtype << " array with name " << t_branchname << " found!";
115  return tca;
116  }
117  }
118  return tca;
119  }
120 
121  TClonesArray *GetOutputArray(const TString &t_branchname, const TString &t_classtype, const TString &t_foldername, Bool_t t_persistenxy)
122  {
123  TClonesArray *tca{nullptr};
124  FairRootManager *ioman = FairRootManager::Instance();
125  if (ioman == nullptr) {
126  throw std::runtime_error("FairRootManager::Instance() returned nullptr");
127  } else {
128  if (ioman->CheckBranch(t_branchname) == 0) {
129  LOG(debug) << "Attempting to register " << t_branchname << " as a container for " << t_classtype << " in folder " << t_foldername;
130  tca = ioman->Register(t_branchname, t_classtype, t_foldername, t_persistenxy);
131  } else {
132  tca = dynamic_cast<TClonesArray *>(ioman->GetObject(t_branchname));
133  if (tca == nullptr) {
134  LOG(error) << "PndTCAMutableContainer<" << t_classtype << ">::Init(" << t_branchname << "): No " << t_classtype << " array with name " << t_branchname
135  << " already exists but could not be fetched!";
136 
137  return tca;
138  }
139  LOG(debug) << "Found a branch already called " << t_branchname << ". Using this for container for " << t_classtype << " in folder " << t_foldername;
140  }
141  }
142  return tca;
143  }
144 
145  template <class T>
146  Bool_t HandledRequest(const PndContainerRegister::PndContainerType_t &t_container, PndContainerRegister *t_register, const TString &t_defaultName)
147  {
148 
149  TString branchname = (t_container.RequestsSpecificBranch()) ? t_container.fBranchname : t_defaultName;
150  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType;
151 
152  if (t_register->IsBranchSet(branchname)) {
153  return true;
154  }
155  Bool_t status = kFALSE;
156  if (t_container.fIsOutput) {
157 
159  output->SetPersistency(t_container.fIsPersistent);
160  output->SetTCA(GetOutputArray(branchname, t_container.fClassType, "Emc" + fDetectorName, t_container.fIsPersistent));
161  output->SetBranchName(branchname);
162 
163  t_register->SetOutput(branchname, output);
164  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType << " as output container.";
165  status = kTRUE;
166  } else {
168  input->SetTCA(GetInputArray(branchname, t_container.fClassType));
169  input->SetBranchName(branchname);
170 
171  t_register->SetInput(branchname, input);
172  LOG(debug) << "PndProcessTask::HandledRequest " << t_container.fBranchname << "(" << branchname << ") for " << t_container.fClassType << " as input container.";
173  status = kTRUE;
174  }
175  return status;
176  }
177 
178  private:
179  ClassDef(PndProcessTask, 1);
180 };
181 
182 #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:48
virtual InitStatus Init() final
Initialise this task. This must be called AFTER SetParContainers(), otherwise we will try to pass the...