ATLAS Offline Software
SolenoidTest.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // SolenoidTest.cxx, (c) ATLAS Detector software
8 /*
9  * Masahiro Morii, Harvard University
10  */
11 // class include
12 #include "SolenoidTest.h"
13 
14 // Framework
15 #include "GaudiKernel/ITHistSvc.h"
16 
17 // CLHEP
18 #include "CLHEP/Vector/ThreeVector.h"
19 #include "CLHEP/Units/SystemOfUnits.h"
20 
21 // MagneticField
23 
24 // floating point precision
25 #include <limits>
26 
27 //root
28 #include "TTree.h"
29 #include "TFile.h"
30 
31 #include "TRandom3.h"
32 
33 // performance test
34 #include <ctime>
35 #include <vector>
36 
38 // Public methods:
40 
41 // Constructor
43 MagField::SolenoidTest::SolenoidTest( const std::string& name, ISvcLocator* pSvcLocator ) :
44  ::AthAlgorithm( name, pSvcLocator ),
45  m_magFieldSvc( "AtlasFieldSvc", name ),
46  m_thistSvc( "THistSvc", name ),
47  m_tree(nullptr),
48  m_event(0)
49 {
50  // histogram service
51  declareProperty( "THistService", m_thistSvc, "The HistogramService" );
52  declareProperty( "HistogramStreamName", m_histStream = "SolenoidTest",
53  "Name of the THistSvc output stream" );
54  // TTree object name
55  declareProperty( "ROOTTreeName", m_treeName = "field",
56  "Name of the TTree object in the output file." );
57  // boundaries for the magfield validation
58  declareProperty( "MinimumR", m_minR = 0., "minimum R" );
59  declareProperty( "MaximumR", m_maxR = 1075., "maximum R" );
60  declareProperty( "MinimumZ", m_minZ = -2820., "minimum Z" );
61  declareProperty( "MaximumZ", m_maxZ = 2820., "maximum Z" );
62  // number of steps in each dimension (granularity)
63  declareProperty( "StepsR", m_stepsR = 200,
64  "Number of steps along radius (granularity)");
65  declareProperty( "StepsZ", m_stepsZ = 200,
66  "Number of steps along z (granularity)");
67  declareProperty( "StepsPhi", m_stepsPhi = 200,
68  "Number of steps along phi (granularity)");
69  declareProperty( "UseFullField", m_useFullField = true, "compute the full 3D field");
70  declareProperty( "UseFastField", m_useFastField = true, "compute the fast 2D field");
71  declareProperty( "WriteNtuple", m_writeNtuple = true, "produce the output ntuple");
72  declareProperty( "Derivatives", m_derivatives = false, "compute the derivatives");
73 }
74 
75 // Destructor
78 {;}
79 
80 // Athena hook:
82 {
83  ATH_MSG_INFO("entering initialize()...");
84 
85  if ( m_writeNtuple ) {
86  // retrieve the histogram service
87  if ( m_thistSvc.retrieve().isSuccess() ) {
88 
89  // Create the prefix of histogram names for the THistSvc
90  std::string prefix = "/" + m_histStream + "/";
91 
92  // the ROOT tree
93  m_tree = new TTree( m_treeName.c_str(), "Magnetic field in the solenoid" );
94  m_tree->Branch( "pos", &m_xyzt, "x/D:y/D:z/D" );
95  if ( m_useFullField ) {
96  m_tree->Branch( "fieldFull", &m_field, "Bx/D:By/D:Bz/D" );
97  if ( m_derivatives ) {
98  m_tree->Branch( "deriv", &m_deriv,
99  "dBxdx/D:dBxdy/D:dBxdz/D:dBydx/D:dBydy/D:dBydz/D:dBzdx/D:dBzdy/D:dBzdz/D" );
100  }
101  }
102  if ( m_useFastField ) {
103  m_tree->Branch( "fieldFast", &m_fieldZR, "Bx2/D:By2/D:Bz2/D" );
104  if ( m_derivatives ) {
105  m_tree->Branch( "derivZR", &m_derivZR,
106  "dBxdx2/D:dBxdy2/D:dBxdz2/D:dBydx2/D:dBydy2/D:dBydz2/D:dBzdx2/D:dBzdy2/D:dBzdz2/D" );
107  }
108  }
109  // register this ROOT TTree to the THistSvc
110  if ( m_thistSvc->regTree(prefix + m_treeName, m_tree).isFailure() ) {
111  ATH_MSG_ERROR("Unable to register TTree to THistSvc");
112  return StatusCode::FAILURE;
113  }
114  } else { // failure in THistSvc retrieve
115  ATH_MSG_ERROR("Unable to retrieve HistogramSvc");
116  return StatusCode::FAILURE;
117  }
118  }
119 
120  // success
121  ATH_MSG_INFO("end of initialize()");
122  return StatusCode::SUCCESS;
123 }
124 
125 // Athena hook:
127  ATH_MSG_INFO("entering finalize()...");
128  ATH_MSG_INFO("end of finalize()");
129  return StatusCode::SUCCESS;
130 }
131 
132 // Athena hook:
134 
135  if( m_event==0 ) { // event #0
136  // call field service once for initialization
137  m_xyzt[0] = m_xyzt[1] = m_xyzt[2] = 0;
138  if ( m_useFullField ) m_magFieldSvc->getField( m_xyzt, m_field, nullptr );
139  if ( m_useFastField ) m_magFieldSvc->getFieldZR( m_xyzt, m_fieldZR, nullptr );
140  // check the field status
141  ATH_MSG_INFO("New service solenoidOn = " << m_magFieldSvc->solenoidOn());
142  ATH_MSG_INFO("New service toroidOn = " << m_magFieldSvc->toroidOn());
143  }
144  else if ( m_event==1 ) { // event #1
145 
146  ATH_MSG_INFO( "solenoid field service test in progress." );
147 
148  // initialize performance timer
149  time_t seconds;
150  seconds = time(nullptr);
151 
152  for ( int k = 0; k < m_stepsPhi; k++ ) { // loop over phi
153  double phi = 2.*M_PI*double(k)/double(m_stepsPhi);
154  for ( int j = 0; j < m_stepsZ; j++ ) { // loop over Z
155  m_xyzt[2] = (m_maxZ-m_minZ)*double(j)/double(m_stepsZ-1) + m_minZ;
156  for ( int i = 0; i < m_stepsR; i++ ) { // loop over R
157  double r = (m_maxR-m_minR)*double(i)/double(m_stepsR-1) + m_minR;
158  m_xyzt[0] = r*cos(phi);
159  m_xyzt[1] = r*sin(phi);
160 
161  // compute the field
162  if ( m_useFullField ) {
163  if ( m_derivatives )
164  m_magFieldSvc->getField( m_xyzt, m_field, m_deriv );
165  else
166  m_magFieldSvc->getField( m_xyzt, m_field, nullptr );
167  }
168  if ( m_useFastField ) {
169  if ( m_derivatives )
170  m_magFieldSvc->getFieldZR( m_xyzt, m_fieldZR, m_derivZR );
171  else
172  m_magFieldSvc->getFieldZR( m_xyzt, m_fieldZR, nullptr );
173  }
174 
175  // fill the ROOT Tree
176  if ( m_writeNtuple ) m_tree->Fill();
177  }
178  }
179  }
180  seconds = time(nullptr) - seconds;
181  ATH_MSG_INFO( "accessing " << m_stepsR * m_stepsZ * m_stepsPhi
182  << " values in a grid took " << seconds << " seconds." );
183  }
184  m_event++;
185  return StatusCode::SUCCESS;
186 }
187 
beamspotman.r
def r
Definition: beamspotman.py:676
MagField::SolenoidTest::m_maxZ
double m_maxZ
maximum z to scan in mm
Definition: SolenoidTest.h:54
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MagField::SolenoidTest::m_thistSvc
ServiceHandle< ITHistSvc > m_thistSvc
the histogram service
Definition: SolenoidTest.h:45
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MagField::SolenoidTest::finalize
StatusCode finalize()
Definition: SolenoidTest.cxx:126
MagField::SolenoidTest::m_minZ
double m_minZ
minimum z to scan in mm
Definition: SolenoidTest.h:53
MagField::SolenoidTest::m_useFastField
bool m_useFastField
Definition: SolenoidTest.h:71
M_PI
#define M_PI
Definition: ActiveFraction.h:11
MagField::SolenoidTest::m_maxR
double m_maxR
maximum radius to scan in mm
Definition: SolenoidTest.h:52
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
MagField::SolenoidTest::SolenoidTest
SolenoidTest(const std::string &name, ISvcLocator *pSvcLocator)
Definition: SolenoidTest.cxx:43
MagField::SolenoidTest::m_stepsR
int m_stepsR
the number of steps in R
Definition: SolenoidTest.h:56
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IMagFieldSvc.h
SolenoidTest.h
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
MagField::SolenoidTest::m_stepsZ
int m_stepsZ
the number of steps in Z
Definition: SolenoidTest.h:57
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
python.LArCalib_HVCorrConfig.seconds
seconds
Definition: LArCalib_HVCorrConfig.py:86
AthAlgorithm
Definition: AthAlgorithm.h:47
MagField::SolenoidTest::m_minR
double m_minR
minimum radius to scan in mm
Definition: SolenoidTest.h:51
MagField::SolenoidTest::m_useFullField
bool m_useFullField
Definition: SolenoidTest.h:70
MagField::SolenoidTest::m_writeNtuple
bool m_writeNtuple
Definition: SolenoidTest.h:73
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MagField::SolenoidTest::m_treeName
std::string m_treeName
name of the Tree object
Definition: SolenoidTest.h:49
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
MagField::SolenoidTest::m_derivatives
bool m_derivatives
Definition: SolenoidTest.h:74
MagField::SolenoidTest::initialize
StatusCode initialize()
Definition: SolenoidTest.cxx:81
MagField::SolenoidTest::m_histStream
std::string m_histStream
THistSvc stream name.
Definition: SolenoidTest.h:46
MagField::SolenoidTest::m_stepsPhi
int m_stepsPhi
the number of steps in phi
Definition: SolenoidTest.h:58
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
MagField::SolenoidTest::~SolenoidTest
virtual ~SolenoidTest()
Definition: SolenoidTest.cxx:77
MagField::SolenoidTest::execute
StatusCode execute()
Definition: SolenoidTest.cxx:133
fitman.k
k
Definition: fitman.py:528