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"
6#include "GaudiKernel/ThreadLocalContext.h"
8
9// Field map
11
12// PathResolver
14
15// ROOT
16#include "TFile.h"
17#include "TTree.h"
18
19
20//-----------------------------------------------------------------------------
21// Constructor for the StandardFieldSvc
22//-----------------------------------------------------------------------------
23StandardFieldSvc::StandardFieldSvc(const std::string& name,
24 ISvcLocator* pSvcLocator)
25 : G4MagFieldSvcBase(name, pSvcLocator)
26{}
27
28
29//-----------------------------------------------------------------------------
30// Initialize the service
31//-----------------------------------------------------------------------------
33{
34 ATH_MSG_INFO( "Initializing " << name() );
35
36 // Either initialize the field map - used for solenoid and toroid, or the field service for the forward field
37 if (m_useMagFieldSvc) {
38 ATH_CHECK( m_magFieldSvc.retrieve() );
39 ATH_MSG_INFO( "initialize: using field service " << m_magFieldSvc.name() );
40 }
41 else {
42 // Create field map
44 ATH_MSG_INFO( "initialize: using created map for field cache ");
45 }
46
47 return StatusCode::SUCCESS;
48}
49
50//-----------------------------------------------------------------------------
51// Create and retrieve the magnetic field
52//-----------------------------------------------------------------------------
54{
55 ATH_MSG_INFO( "StandardFieldSvc::makeField" );
56
57 AtlasField* af{nullptr};
58
59 // Either initialize the field map - used for solenoid and toroid, or the field service for the forward field
60 if (m_useMagFieldSvc) {
61 af = new AtlasField( &*m_magFieldSvc );
62 }
63 else {
64 const double scaleSolenoid = m_mapSoleCurrent>0 ? m_useSoleCurrent / m_mapSoleCurrent : 1. ;
65 const double scaleToroid = m_mapToroCurrent>0 ? m_useToroCurrent / m_mapToroCurrent : 1. ;
66 af = new AtlasField(scaleSolenoid, scaleToroid, m_fieldMap.get());
67 }
68
69 return (af);
70}
71
72
73//-----------------------------------------------------------------------------
74// Create field map
75//-----------------------------------------------------------------------------
77{
78 ATH_MSG_INFO( "StandardFieldSvc::createFieldMap" );
79
80
81 // Select map file according to the value of the currents which indicate which map is 'on'
82
83 // determine the map to load
84 std::string mapFile;
85 if ( solenoidOn() && toroidOn() ) mapFile = m_fullMapFilename;
86 else if ( solenoidOn() ) mapFile = m_soleMapFilename;
87 else if ( toroidOn() ) mapFile = m_toroMapFilename;
88 else {
89 // all magnets OFF. no need to read map
90 return StatusCode::SUCCESS;
91 }
92
93 ATH_MSG_INFO ( "StandardFieldSvc::createFieldMap: Set map currents from FieldSvc: solenoid/toroid "
95 ATH_MSG_INFO ( "StandardFieldSvc::createFieldMap: Use map file " << mapFile);
96
97 // find the path to the map file
98 std::string resolvedMapFile = PathResolver::find_file( mapFile, "DATAPATH" );
99 if ( resolvedMapFile.empty() ) {
100 ATH_MSG_ERROR( "StandardFieldSvc::createFieldMap: Field map file " << mapFile << " not found" );
101 return StatusCode::FAILURE;
102 }
103 // Do checks and extract root file to initialize the map
104 if ( resolvedMapFile.find(".root") == std::string::npos ) {
105 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: input file name '" << resolvedMapFile << "' does not end with .root");
106 return StatusCode::FAILURE;
107 }
108
109 std::unique_ptr<TFile> rootfile = std::make_unique<TFile>(resolvedMapFile.c_str(), "OLD");
110 if ( ! rootfile.get() ) {
111 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: failed to open " << resolvedMapFile);
112 return StatusCode::FAILURE;
113 }
114 if ( !rootfile->cd() ) {
115 // could not make it current directory
116 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: unable to cd() into the ROOT field map TFile");
117 rootfile->Close();
118 return StatusCode::FAILURE;
119 }
120 // open the tree
121 TTree* tree = (TTree*)rootfile->Get("BFieldMap");
122 if ( tree == nullptr ) {
123 // no tree
124 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: TTree 'BFieldMap' does not exist in ROOT field map");
125 rootfile->Close();
126 return StatusCode::FAILURE;
127 }
128
129 // create map
130 m_fieldMap = std::make_unique<MagField::AtlasFieldMap>();
131
132 // initialize map
133 if (!m_fieldMap->initializeMap( rootfile.get(), m_useSoleCurrent , m_useToroCurrent) ) {
134 // failed to initialize the map
135 ATH_MSG_ERROR("StandardFieldSvc::createFieldMap: unable to initialize the map for AtlasFieldMap for file " << resolvedMapFile);
136 rootfile->Close();
137 return StatusCode::FAILURE;
138 }
139
140 rootfile->Close();
141
142 ATH_MSG_INFO( "StandardFieldSvc::createFieldMap: Initialized the field map from " << resolvedMapFile );
143 return StatusCode::SUCCESS;
144}
#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