ATLAS Offline Software
Loading...
Searching...
No Matches
SolenoidTest.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5/*
6 * Masahiro Morii, Harvard University
7 */
8
9// class include
10#include "SolenoidTest.h"
11
12// root
13#include "TFile.h"
14#include "TRandom3.h"
15#include "TTree.h"
16
17//
18#include <cmath>
19#include <vector>
20
22// Public methods:
24
25// Constructor
28 ISvcLocator* pSvcLocator)
29 : AthAlgorithm(name, pSvcLocator), m_thistSvc("THistSvc/THistSvc", name) {}
30
31// Athena hook:
33 ATH_CHECK(m_fieldCacheKey.initialize());
34 // retrieve the histogram service
35 if (m_thistSvc.retrieve().isFailure()) {
36 ATH_MSG_ERROR("Unable to retrieve HistogramSvc");
37 return StatusCode::FAILURE;
38 }
39 // Create the prefix of histogram names for the THistSvc
40 std::string prefix = "/" + m_histStream + "/";
41
42 std::string treeName = m_treeName.value();
43 // the ROOT tree
44 m_tree = new TTree(treeName.c_str(), "Magnetic field in the solenoid");
45 m_tree->Branch("pos", &m_xyzt, "x/D:y/D:z/D");
46 m_tree->Branch("fieldFull", &m_field, "Bx/D:By/D:Bz/D");
47 m_tree->Branch("deriv", &m_deriv,
48 "dBxdx/D:dBxdy/D:dBxdz/D:dBydx/D:dBydy/D:dBydz/"
49 "D:dBzdx/D:dBzdy/D:dBzdz/D");
50 m_tree->Branch("fieldFast", &m_fieldZR, "Bx2/D:By2/D:Bz2/D");
51 m_tree->Branch("derivZR", &m_derivZR,
52 "dBxdx2/D:dBxdy2/D:dBxdz2/D:dBydx2/D:dBydy2/D:dBydz2/"
53 "D:dBzdx2/D:dBzdy2/D:dBzdz2/D");
54 // register this ROOT TTree to the THistSvc
55 if (m_thistSvc->regTree(prefix + treeName, m_tree).isFailure()) {
56 ATH_MSG_ERROR("Unable to register TTree to THistSvc");
57 return StatusCode::FAILURE;
58 }
59
60 return StatusCode::SUCCESS;
61}
62
65 Gaudi::Hive::currentContext()};
66 const AtlasFieldCacheCondObj* fieldCondObj{*rh};
67 if (fieldCondObj == nullptr) {
68 ATH_MSG_ERROR("Failed to retrieve AtlasFieldCacheCondObj with key "
69 << m_fieldCacheKey.key());
70 return StatusCode::FAILURE;
71 }
72
74 fieldCondObj->getInitializedCache(fieldCache);
75
76 if (m_eventsSeen == m_event) {
77
78 double deltaZ = (m_maxZ - m_minZ) / (m_stepsZ - 1);
79 double deltaR = (m_maxR - m_minR) / m_stepsR - 1;
80 double deltaPhi = (2 * M_PI) / (m_stepsPhi - 1);
81
82 for (int k = 0; k < m_stepsPhi; k++) { // loop over phi
83 double phi = deltaPhi * k;
84 for (int j = 0; j < m_stepsZ; j++) { // loop over Z
85 m_xyzt[2] = m_minZ + deltaZ * j;
86 for (int i = 0; i < m_stepsR; i++) { // loop over R
87 double r = m_minR + deltaR * i;
88 m_xyzt[0] = r * cos(phi);
89 m_xyzt[1] = r * sin(phi);
90
91 fieldCache.getField(m_xyzt, m_field, m_deriv);
92 fieldCache.getFieldZR(m_xyzt, m_fieldZR, nullptr);
93 ATH_MSG_INFO("Field at r,phi " << r << ", " << phi << ", "
94 << " with xyz: " << m_xyzt[0] << ", "
95 << m_xyzt[1] << ", " << m_xyzt[2]
96 << " with value " << m_field[0] << ", "
97 << m_field[1] << ", " << m_field[2]);
98
99 m_tree->Fill();
100 }
101 }
102 }
103 }
104 m_eventsSeen++;
105 return StatusCode::SUCCESS;
106}
107
#define M_PI
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
Scalar deltaR(const MatrixBase< Derived > &vec) const
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
void getInitializedCache(MagField::AtlasFieldCache &cache) const
get B field cache for evaluation as a function of 2-d or 3-d position.
Local cache for magnetic field (based on MagFieldServices/AtlasFieldSvcTLS.h)
void getField(const double *ATH_RESTRICT xyz, double *ATH_RESTRICT bxyz, double *ATH_RESTRICT deriv=nullptr)
get B field value at given position xyz[3] is in mm, bxyz[3] is in kT if deriv[9] is given,...
void getFieldZR(const double *ATH_RESTRICT xyz, double *ATH_RESTRICT bxyz, double *ATH_RESTRICT deriv=nullptr)
get B field valaue on the z-r plane at given position works only inside the solenoid.
virtual StatusCode initialize() override final
Gaudi::Property< double > m_minR
TTree * m_tree
the ROOT tree containing the output
Gaudi::Property< int > m_stepsZ
Gaudi::Property< double > m_minZ
SolenoidTest(const std::string &name, ISvcLocator *pSvcLocator)
std::atomic< long int > m_eventsSeen
event counter
double m_derivZR[9]
stores derivatives
double m_xyzt[4]
Variable to write out.
Gaudi::Property< int > m_event
Gaudi::Property< double > m_maxZ
Gaudi::Property< std::string > m_treeName
Gaudi::Property< double > m_maxR
double m_deriv[9]
stores derivatives
ServiceHandle< ITHistSvc > m_thistSvc
Histogram Service.
Gaudi::Property< std::string > m_histStream
Gaudi::Property< int > m_stepsR
Gaudi::Property< int > m_stepsPhi
double m_field[3]
stores the field components
double m_fieldZR[3]
stores the 2d field components
virtual StatusCode execute() override final
SG::ReadCondHandleKey< AtlasFieldCacheCondObj > m_fieldCacheKey
int r
Definition globals.cxx:22