ATLAS Offline Software
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 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
53 G4MagneticField* StandardFieldSvc::makeField()
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 "
94  << m_useSoleCurrent << "," << m_useToroCurrent );
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 }
StandardFieldSvc::toroidOn
bool toroidOn()
Definition: StandardFieldSvc.h:105
StandardFieldSvc::m_toroMapFilename
Gaudi::Property< std::string > m_toroMapFilename
Definition: StandardFieldSvc.h:114
StandardFieldSvc::m_magFieldSvc
ServiceHandle< MagField::IMagFieldSvc > m_magFieldSvc
Handle to the the Forward ATLAS magnetic field service.
Definition: StandardFieldSvc.h:99
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
StandardFieldSvc::m_useMagFieldSvc
Gaudi::Property< bool > m_useMagFieldSvc
Definition: StandardFieldSvc.h:95
tree
TChain * tree
Definition: tile_monitor.h:30
StandardFieldSvc::m_mapSoleCurrent
Gaudi::Property< double > m_mapSoleCurrent
nominal current for the maps
Definition: StandardFieldSvc.h:118
StandardFieldSvc::m_fieldMap
std::unique_ptr< MagField::AtlasFieldMap > m_fieldMap
Definition: StandardFieldSvc.h:129
StandardFieldSvc::m_mapToroCurrent
Gaudi::Property< double > m_mapToroCurrent
Definition: StandardFieldSvc.h:120
LArCalib_HVScale2NtupleConfig.rootfile
string rootfile
Definition: LArCalib_HVScale2NtupleConfig.py:74
ReadCondHandle.h
G4MagFieldSvcBase
Definition: G4MagFieldSvcBase.h:29
StandardFieldSvc::StandardFieldSvc
StandardFieldSvc(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
Definition: StandardFieldSvc.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
AtlasField
G4 wrapper around the main ATLAS magnetic field cache or field svc for forward field.
Definition: StandardFieldSvc.h:32
StandardFieldSvc::m_useToroCurrent
Gaudi::Property< double > m_useToroCurrent
Definition: StandardFieldSvc.h:125
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
StandardFieldSvc::m_fullMapFilename
Gaudi::Property< std::string > m_fullMapFilename
map file names
Definition: StandardFieldSvc.h:108
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
StandardFieldSvc::makeField
G4MagneticField * makeField() override final
Create/retrieve the AtlasField object.
Definition: StandardFieldSvc.cxx:53
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
AtlasFieldMap.h
StandardFieldSvc::solenoidOn
bool solenoidOn()
Definition: StandardFieldSvc.h:104
StandardFieldSvc::createFieldMap
StatusCode createFieldMap()
Definition: StandardFieldSvc.cxx:76
StandardFieldSvc::m_soleMapFilename
Gaudi::Property< std::string > m_soleMapFilename
Definition: StandardFieldSvc.h:111
StandardFieldSvc::initialize
StatusCode initialize() override final
Athena method. called at initialization time, being customized here.
Definition: StandardFieldSvc.cxx:32
StandardFieldSvc.h
StandardFieldSvc::m_useSoleCurrent
Gaudi::Property< double > m_useSoleCurrent
configurable current for map scaling
Definition: StandardFieldSvc.h:123