ATLAS Offline Software
Loading...
Searching...
No Matches
StandardFieldSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "StandardFieldSvc.h"
7
8// Field map
10
11// PathResolver
13
14// ROOT
15#include "TFile.h"
16#include "TTree.h"
17
18
19//-----------------------------------------------------------------------------
20// Constructor for the StandardFieldSvc
21//-----------------------------------------------------------------------------
22StandardFieldSvc::StandardFieldSvc(const std::string& name,
23 ISvcLocator* pSvcLocator)
24 : G4MagFieldSvcBase(name, pSvcLocator)
25{}
26
27
28//-----------------------------------------------------------------------------
29// Initialize the service
30//-----------------------------------------------------------------------------
32{
33 ATH_MSG_INFO( "Initializing " << name() );
34
35 // Either initialize the field map - used for solenoid and toroid, or the field service for the forward field
36 if (m_useMagFieldSvc) {
37 ATH_CHECK( m_magFieldSvc.retrieve() );
38 ATH_MSG_INFO( "initialize: using field service " << m_magFieldSvc.name() );
39 }
40 else {
41 // Create field map
43 ATH_MSG_INFO( "initialize: using created map for field cache ");
44 }
45
46 return StatusCode::SUCCESS;
47}
48
49//-----------------------------------------------------------------------------
50// Create and retrieve the magnetic field
51//-----------------------------------------------------------------------------
53{
54 ATH_MSG_INFO( "StandardFieldSvc::makeField" );
55
56 AtlasField* af{nullptr};
57
58 // Either initialize the field map - used for solenoid and toroid, or the field service for the forward field
59 if (m_useMagFieldSvc) {
60 af = new AtlasField( &*m_magFieldSvc );
61 }
62 else {
63 const double scaleSolenoid = m_mapSoleCurrent>0 ? m_useSoleCurrent / m_mapSoleCurrent : 1. ;
64 const double scaleToroid = m_mapToroCurrent>0 ? m_useToroCurrent / m_mapToroCurrent : 1. ;
65 af = new AtlasField(scaleSolenoid, scaleToroid, m_fieldMap.get());
66 }
67
68 return (af);
69}
70
71
72//-----------------------------------------------------------------------------
73// Create field map
74//-----------------------------------------------------------------------------
76{
77 ATH_MSG_INFO( "StandardFieldSvc::createFieldMap" );
78
79
80 // Select map file according to the value of the currents which indicate which map is 'on'
81
82 // determine the map to load
83 std::string mapFile;
84 if ( solenoidOn() && toroidOn() ) mapFile = m_fullMapFilename;
85 else if ( solenoidOn() ) mapFile = m_soleMapFilename;
86 else if ( toroidOn() ) mapFile = m_toroMapFilename;
87 else {
88 // all magnets OFF. no need to read map
89 return StatusCode::SUCCESS;
90 }
91
92 ATH_MSG_INFO ( "StandardFieldSvc::createFieldMap: Set map currents from FieldSvc: solenoid/toroid "
94 ATH_MSG_INFO ( "StandardFieldSvc::createFieldMap: Use map file " << mapFile);
95
96 // find the path to the map file
97 std::string resolvedMapFile = PathResolver::find_file( mapFile, "DATAPATH" );
98 if ( resolvedMapFile.empty() ) {
99 ATH_MSG_ERROR( "StandardFieldSvc::createFieldMap: Field map file " << mapFile << " not found" );
100 return StatusCode::FAILURE;
101 }
102 // Do checks and extract root file to initialize the map
103 if ( resolvedMapFile.find(".root") == std::string::npos ) {
104 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: input file name '" << resolvedMapFile << "' does not end with .root");
105 return StatusCode::FAILURE;
106 }
107
108 std::unique_ptr<TFile> rootfile = std::make_unique<TFile>(resolvedMapFile.c_str(), "OLD");
109 if ( ! rootfile.get() ) {
110 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: failed to open " << resolvedMapFile);
111 return StatusCode::FAILURE;
112 }
113 if ( !rootfile->cd() ) {
114 // could not make it current directory
115 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: unable to cd() into the ROOT field map TFile");
116 rootfile->Close();
117 return StatusCode::FAILURE;
118 }
119 // open the tree
120 TTree* tree = (TTree*)rootfile->Get("BFieldMap");
121 if ( tree == nullptr ) {
122 // no tree
123 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: TTree 'BFieldMap' does not exist in ROOT field map");
124 rootfile->Close();
125 return StatusCode::FAILURE;
126 }
127
128 // create map
129 m_fieldMap = std::make_unique<MagField::AtlasFieldMap>();
130
131 // initialize map
132 if (!m_fieldMap->initializeMap( rootfile.get(), m_useSoleCurrent , m_useToroCurrent) ) {
133 // failed to initialize the map
134 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: unable to initialize the map for AtlasFieldMap for file " << resolvedMapFile);
135 rootfile->Close();
136 return StatusCode::FAILURE;
137 }
138
139 rootfile->Close();
140
141 ATH_MSG_INFO( "StandardFieldSvc::createFieldMap: Initialized the field map from " << resolvedMapFile );
142 return StatusCode::SUCCESS;
143}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
G4 wrapper around the main ATLAS magnetic field cache or field svc for forward field.
G4MagFieldSvcBase(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
StandardFieldSvc(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
Gaudi::Property< double > m_useSoleCurrent
configurable current for map scaling
Gaudi::Property< double > m_mapSoleCurrent
nominal current for the maps
Gaudi::Property< std::string > m_fullMapFilename
map file names
std::unique_ptr< MagField::AtlasFieldMap > m_fieldMap
Gaudi::Property< double > m_useToroCurrent
StatusCode initialize() override final
Athena method. called at initialization time, being customized here.
ServiceHandle< MagField::IMagFieldSvc > m_magFieldSvc
Handle to the the Forward ATLAS magnetic field service.
Gaudi::Property< std::string > m_toroMapFilename
Gaudi::Property< std::string > m_soleMapFilename
StatusCode createFieldMap()
Gaudi::Property< double > m_mapToroCurrent
G4MagneticField * makeField() override final
Create/retrieve the AtlasField object.
Gaudi::Property< bool > m_useMagFieldSvc
static std::vector< std::string > rootfile
Definition iLumiCalc.h:30
TChain * tree