Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHFieldUtility.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHFieldUtility.cc
1 #include "PHFieldUtility.h"
2 
3 #include "PHField.h"
4 #include "PHField2D.h"
5 #include "PHField3DCartesian.h"
6 #include "PHField3DCylindrical.h"
7 #include "PHFieldConfig.h"
8 #include "PHFieldConfigv1.h"
9 #include "PHFieldUniform.h"
10 
11 #include <fun4all/Fun4AllServer.h>
12 
13 #include <phool/PHCompositeNode.h>
14 #include <phool/PHDataNode.h>
15 #include <phool/PHIODataNode.h>
16 #include <phool/PHNodeIterator.h>
17 #include <phool/PHObject.h>
18 #include <phool/getClass.h>
19 #include <phool/phool.h> // for PHWHERE
20 
21 #include <TSystem.h>
22 
23 #include <cassert>
24 #include <cstdlib> // for getenv
25 #include <iostream>
26 
27 PHField *
28 PHFieldUtility::BuildFieldMap(const PHFieldConfig *field_config, float inner_radius, float outer_radius, float size_z, const int verbosity)
29 {
30  assert(field_config);
31 
32  if (verbosity)
33  {
34  std::cout << "PHFieldUtility::BuildFieldMap - construction field with configuration: ";
35  field_config->identify();
36  }
37 
38  PHField *field(nullptr);
39 
40  switch (field_config->get_field_config())
41  {
43  // return "Constant field";
44 
45  field = new PHFieldUniform(
46  field_config->get_field_mag_x(),
47  field_config->get_field_mag_y(),
48  field_config->get_field_mag_z());
49 
50  break;
52  // return "2D field map expressed in cylindrical coordinates";
53  field = new PHField2D(
54  field_config->get_filename(),
55  verbosity,
56  field_config->get_magfield_rescale());
57  break;
58 
60  // return "3D field map expressed in cylindrical coordinates";
61  field = new PHField3DCylindrical(
62  field_config->get_filename(),
63  verbosity,
64  field_config->get_magfield_rescale());
65  break;
66 
68  // return "3D field map expressed in Cartesian coordinates";
69  field = new PHField3DCartesian(
70  field_config->get_filename(),
71  field_config->get_magfield_rescale(),
74  size_z);
75  break;
76 
77  default:
78  std::cout << "PHFieldUtility::BuildFieldMap - Invalid Field Configuration: " << field_config->get_field_config() << std::endl;
79  gSystem->Exit(1);
80  }
81  assert(field); // Check for Invalid Field
82  return field;
83 }
84 
90 {
91  char *calibrationroot = getenv("CALIBRATIONROOT");
92  std::string fieldmap = "sphenix3dbigmapxyz_gap_rebuild.root";
93  if (calibrationroot != nullptr)
94  {
95  fieldmap = std::string(calibrationroot) + "/Field/Map/" + fieldmap;
96  }
97  return new PHFieldConfigv1(PHFieldConfigv1::Field3DCartesian, fieldmap, 1.);
98 }
99 
101 PHField *
102 PHFieldUtility::GetFieldMapNode(const PHFieldConfig *default_config, PHCompositeNode *topNode, const int verbosity)
103 {
104  if (topNode == nullptr)
105  {
106  topNode = Fun4AllServer::instance()->topNode();
107  }
108  assert(topNode);
109  PHNodeIterator iter(topNode);
110 
111  // Looking for the RUN node
112  PHCompositeNode *parNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "PAR"));
113  if (!parNode)
114  {
115  std::cout << PHWHERE << ": PAR Node missing, request aborting.";
116  gSystem->Exit(1);
117  }
118 
119  PHField *field = findNode::getClass<PHField>(parNode, GetDSTFieldMapNodeName());
120  if (!field)
121  {
122  PHFieldConfig *field_config = GetFieldConfigNode(default_config, topNode, verbosity);
123  assert(field_config);
124 
125  field = BuildFieldMap(field_config, verbosity > 0 ? verbosity - 1 : verbosity);
126  assert(field);
127 
128  parNode->addNode(new PHDataNode<PHField>(field, GetDSTFieldMapNodeName()));
129  }
130 
131  return field;
132 }
133 
137 {
138  if (topNode == nullptr)
139  {
140  topNode = Fun4AllServer::instance()->topNode();
141  }
142  assert(topNode);
143 
144  PHNodeIterator iter(topNode);
145 
146  // Looking for the RUN node
147  PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
148  if (!runNode)
149  {
150  std::cout << PHWHERE << ": RUN Node missing, aborting.";
151  gSystem->Exit(1);
152  }
153 
154  PHFieldConfig *field = findNode::getClass<PHFieldConfig>(runNode, GetDSTConfigNodeName());
155  if (!field)
156  {
157  if (!default_config)
158  {
159  field = DefaultFieldConfig();
160  if (verbosity)
161  {
162  std::cout << "PHFieldUtility::GetFieldConfigNode - field map with configuration from build-in default: ";
163  field->identify();
164  }
165  }
166  else
167  {
168  field = static_cast<PHFieldConfig *>(default_config->CloneMe());
169  if (verbosity)
170  {
171  std::cout << "PHFieldUtility::GetFieldConfigNode - field map with configuration from input default: ";
172  field->identify();
173  }
174  }
175 
176  assert(field);
177  runNode->addNode(new PHIODataNode<PHObject>(field, GetDSTConfigNodeName(), "PHObject"));
178  }
179  else
180  {
181  if (verbosity)
182  {
183  std::cout << "PHFieldUtility::GetFieldConfigNode - field map with configuration from DST/RUN: ";
184  field->identify();
185  }
186  }
187 
188  return field;
189 }