ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
getClass.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file getClass.h
1 #ifndef PHOOL_GETCLASS_H
2 #define PHOOL_GETCLASS_H
3 
4 #include "PHDataNode.h"
5 #include "PHIODataNode.h"
6 #include "PHNode.h"
7 #include "PHNodeIterator.h"
8 
9 #include <TObject.h>
10 
11 #include <string>
12 
13 class PHCompositeNode;
14 
15 namespace findNode
16 {
17 template <class T>
18 T *getClass(PHCompositeNode *top, const std::string &name)
19 {
20  PHNodeIterator iter(top);
21  PHNode *FoundNode = iter.findFirst(name.c_str()); // returns pointer to PHNode
22  if (!FoundNode)
23  {
24  return nullptr;
25  }
26  // first test if it is a PHDataNode
27  PHDataNode<T> *DNode = dynamic_cast<PHDataNode<T> *>(FoundNode);
28  if (DNode)
29  {
30  T *object = dynamic_cast<T *>(DNode->getData());
31  if (object)
32  {
33  return object;
34  }
35  }
36 
37  // We make a static cast for PHIODataNode<TObject> since all
38  // PHIODataNodes have to contain a TObject (otherwise it cannot be
39  // written out and it should be a PHDataNode. dynamic cast does not
40  // work (see some explanation in PHTypedNodeIterator.h)
41 
42  PHIODataNode<TObject> *IONode = static_cast<PHIODataNode<TObject> *>(FoundNode);
43  if (IONode)
44  {
45  T *object = dynamic_cast<T *>(IONode->getData());
46  if (!object)
47  {
48  return nullptr;
49  }
50  else
51  {
52  return object;
53  }
54  }
55 
56  return nullptr;
57 }
58 } // namespace findNode
59 
60 #endif