Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Interfaces.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Interfaces.h
1 // ----------------------------------------------------------------------------
2 // 'Interfaces.h'
3 // Derek Anderson
4 // 12.29.2023
5 //
6 // Various interfaces between sPHENIX Cold QCD Correlator
7 // Analysis objects and data structures (e.g. TTree's) are
8 // collected here.
9 // ----------------------------------------------------------------------------
10 
11 #pragma once
12 
13 // c++ utilities
14 #include <string>
15 #include <limits>
16 #include <vector>
17 #include <optional>
18 // root libraries
19 #include <TTree.h>
20 #include <TChain.h>
21 #include <TNtuple.h>
22 // phool libraries
23 #include <phool/phool.h>
24 #include <phool/getClass.h>
25 #include <phool/PHIODataNode.h>
26 #include <phool/PHNodeIterator.h>
27 #include <phool/PHCompositeNode.h>
28 // analyis utilities
29 #include "TrkTools.h"
30 #include "CalTools.h"
31 
32 // make common namespaces implicit
33 using namespace std;
34 
35 
36 
37 namespace SColdQcdCorrelatorAnalysis {
38  namespace SCorrelatorUtilities {
39 
40  // correlator object-data structure interfaces ----------------------------
41 
42  /* TODO will go here */
43 
44 
45 
46  // f4a interfaces ---------------------------------------------------------
47 
48  // remove forbidden characters from a node name
49  void CleanseNodeName(string& nameToClean) {
50 
51  for (const auto& [bad, good] : MapBadOntoGoodStrings) {
52  size_t position;
53  while ((position = nameToClean.find(bad)) != string::npos) {
54  nameToClean.replace(position, 1, good);
55  }
56  } // end bad-good pair loop
57  return;
58 
59  } // end 'CleanseNodeName(string&)'
60 
61 
62 
63  template <typename T> void CreateNode(PHCompositeNode* topNode, string newNodeName, T& objectInNode) {
64 
65  // make sure node name is okay
66  CleanseNodeName(newNodeName);
67 
68  // find DST node
69  PHNodeIterator itNode(topNode);
70  PHCompositeNode* dstNode = dynamic_cast<PHCompositeNode*>(itNode.findFirst("PHCompositeNode", "DST"));
71 
72  // create node and exit
73  PHIODataNode<PHObject>* newNode = new PHIODataNode<PHObject>(objectInNode, newNodeName.c_str(), "PHObject");
74  dstNode -> addNode(newNode);
75  return;
76 
77  } // end 'CreateNode(PHCompositeNode*, string, T&)'
78 
79 
80 
81  // TTree interfaces -------------------------------------------------------
82 
83  // get entry from TTree, TChain, or TNtuple
84  template <typename T> int64_t GetEntry(T* tree, const uint64_t entry) {
85 
87  if (!tree) {
88  status = 0;
89  } else {
90  status = tree -> GetEntry(entry);
91  }
92  return status;
93 
94  } // end 'GetEntry(T*, uint64_t)'
95 
96  template int64_t GetEntry(TTree* tree, const uint64_t entry);
97  template int64_t GetEntry(TChain* tree, const uint64_t entry);
98  template int64_t GetEntry(TNtuple* tree, const uint64_t entry);
99 
100 
101 
102  // load TTree, TChain, or TNtuple
103  template <typename T> int64_t LoadTree(T* tree, const uint64_t entry, int& current) {
104 
105  // check for tree & load
106  int number = numeric_limits<int>::min();
108  if (!tree) {
109  status = -5;
110  } else {
111  number = tree -> GetTreeNumber();
112  status = tree -> LoadTree(entry);
113  }
114 
115  // update current tree number if need be
116  const bool isStatusGood = (status >= 0);
117  const bool isNotCurrent = (number != current);
118  if (isStatusGood && isNotCurrent) {
119  current = tree -> GetTreeNumber();
120  }
121  return status;
122 
123  } // end 'LoadTree(uint64_t)'
124 
125  template int64_t LoadTree(TTree* tree, const uint64_t entry, int& current);
126  template int64_t LoadTree(TChain* tree, const uint64_t entry, int& current);
127  template int64_t LoadTree(TNtuple* tree, const uint64_t entry, int& current);
128 
129 
130 
131  // data member-to-string methods ------------------------------------------
132 
133  // append a tag to a vector of leaf names
134  void AddTagToLeaves(const string tag, vector<string>& leaves) {
135 
136  for (string& leaf : leaves) {
137  leaf.append(tag);
138  }
139  return;
140 
141  } // end 'AddTagToLeaves(vector<string>)'
142 
143 
144 
145  // flatten list of leaves into a colon-separated list
146  string FlattenLeafList(const vector<string>& leaves) {
147 
148  string flattened("");
149  for (size_t iLeaf = 0; iLeaf < leaves.size(); iLeaf++) {
150  flattened.append(leaves[iLeaf]);
151  if ((iLeaf + 1) != leaves.size()) {
152  flattened.append(":");
153  }
154  }
155  return flattened;
156 
157  } // end 'FlattenLeafList(vector<string>&)'
158 
159 
160 
161  // generic function to create vector of leaf names
162  template <typename T> vector<string> MakeLeafVector(optional<string> tag = nullopt) {
163 
164  vector<string> leaves = T::GetListOfMembers();
165  if (tag.has_value()) {
166  AddTagToLeaves(tag.value(), leaves);
167  }
168  return leaves;
169 
170  } // end 'template <> MakeLeafVector(optional<string>)'
171 
172 
173 
174  // generic function to append leaf names onto a vector
175  template <typename T> void AddLeavesToVector(vector<string>& vecToAddTo, optional<string> tag = nullopt) {
176 
177  vector<string> addends = MakeLeafVector<T>(tag);
178  for (auto addend : addends) {
179  vecToAddTo.push_back(addend);
180  }
181  return;
182 
183  } // end 'template <> AddLeavesToVector(vector<string>&, optional<string>)'
184 
185  } // end SCorrelatorUtilities namespace
186 } // end SColdQcdCorrealtorAnalysis namespace
187 
188 // end ------------------------------------------------------------------------