ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
MuonSpectrometer
MuonCalib
MdtCalib
MdtCalibSvc
src
MdtCalibrationShiftMapBase.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#include "
MdtCalibrationShiftMapBase.h
"
6
#include "
PathResolver/PathResolver.h
"
7
8
#include <sys/stat.h>
9
#include <fstream>
10
11
12
MdtCalibrationShiftMapBase::MdtCalibrationShiftMapBase
(
const
std::string&
type
,
const
std::string& name,
const
IInterface* parent)
13
: base_class (
type
, name, parent) {}
14
15
StatusCode
MdtCalibrationShiftMapBase::initialize
() {
16
ATH_CHECK
(
m_idHelperSvc
.retrieve());
17
ATH_CHECK
(
m_mdtCab
.initialize());
18
ATH_CHECK
(
initializeMap
());
19
return
StatusCode::SUCCESS;
20
}
21
22
StatusCode
MdtCalibrationShiftMapBase::dumpMapAsFile
() {
23
/* initialize map if it's not there */
24
if
(!
m_mapIsInitialized
) {
25
ATH_CHECK
(
initializeMap
());
26
}
27
28
/* write the map to a file */
29
{
30
std::ofstream
file
(
m_mapFileName
.value().c_str(), std::ios::out | std::ios::trunc);
31
32
/* see if opening the file was successful */
33
if
(!
file
.is_open()) {
34
ATH_MSG_FATAL
(
35
"Cannot open map output file for writing. Tried accessing file at \"./"
36
<<
m_mapFileName
<<
"\""
);
37
return
StatusCode::FAILURE;
38
}
39
40
/* write header, comments can start with '#' or '//' */
41
file
<<
"# This file contains shift values for MDT tubes\n"
;
42
file
<<
"# Each Identifier is mapped to a float\n"
;
43
file
<<
"# Below are comma separated values with formatting\n"
;
44
file
<<
"# Identifier.get_compact(),float\n"
;
45
file
<<
"# ------------------------------------------------\n"
;
46
47
/* dump map contents */
48
for
(
auto
shift :
m_shiftValues
) {
49
Identifier::value_type identifierCompact = shift.first.get_compact();
50
float
shiftValue = shift.second;
51
file
<< identifierCompact;
52
file
<<
","
;
53
file
<< shiftValue;
54
file
<<
"\n"
;
55
}
56
}
// '}' flushes file
57
return
StatusCode::SUCCESS;
58
}
59
60
StatusCode
MdtCalibrationShiftMapBase::loadMapFromFile
() {
61
/* check if map was already initialized */
62
if
(
m_mapIsInitialized
) {
63
ATH_MSG_WARNING
(
"Map already initialized, won't overwrite it."
);
64
return
StatusCode::SUCCESS;
65
}
66
67
/* resolve path */
68
std::string fileWithPath =
PathResolver::find_file
(
m_mapFileName
,
"DATAPATH"
);
69
if
(fileWithPath.empty()) {
70
ATH_MSG_ERROR
(
"Cannot find file "
<<
m_mapFileName
<<
" in $DATAPATH"
);
71
return
StatusCode::FAILURE;
72
}
73
74
/* check if the file exists */
75
struct
stat buffer{};
76
if
(stat(fileWithPath.c_str(), &buffer) != 0) {
77
ATH_MSG_ERROR
(
"Cannot stat the file \""
78
<< fileWithPath.c_str()
79
<<
"\" -> map can not be initialized from this file."
);
80
return
StatusCode::FAILURE;
81
}
82
83
/* check if the map is already stored */
84
std::ifstream fin(fileWithPath.c_str(), std::ios::in);
85
std::string line;
86
bool
initializedWithWarnings =
false
;
87
// get all lines in file
88
while
(std::getline(fin, line)) {
89
// check if file is empty, begins with '#', or '//'
90
if
(line.empty() || line.compare(0, 1,
"#"
) == 0 ||
91
line.compare(0, 2,
"//"
)) {
92
continue
;
93
}
94
// need a stringstream for readline
95
std::stringstream lineStream(line);
96
// get all csv tokens in line
97
std::string token;
98
std::vector<std::string> tokenVector;
99
while
(std::getline(lineStream, token,
','
)) {
100
tokenVector.push_back(token);
101
}
102
// expect exactly two tokens: identifier and value
103
if
(tokenVector.size() == 2) {
104
// we are careful about the type here
105
// watch out for compiler warnings warning about casting ull to value_type
106
// they might occur of Identifier::value_type is changed at some point
107
Identifier::value_type identifierCompact = std::stoull(tokenVector[0]);
108
Identifier
id(identifierCompact);
109
float
shift = std::stof(tokenVector[1]);
110
m_shiftValues
[id] = shift;
111
}
else
{
112
ATH_MSG_WARNING
(
"Unexpected input format in shift map file "
113
<< fileWithPath.c_str());
114
ATH_MSG_WARNING
(
"Expected 2 tokens, got "
<< tokenVector.size());
115
ATH_MSG_WARNING
(
"Broken line: \""
<< line.c_str() <<
"\""
);
116
initializedWithWarnings =
true
;
117
}
118
}
119
m_mapIsInitialized
=
true
;
120
if
(initializedWithWarnings) {
121
ATH_MSG_WARNING
(
"Initialized shift map WITH WARNINGS from file \""
122
<< fileWithPath.c_str() <<
"\""
);
123
}
else
{
124
ATH_MSG_INFO
(
"Successfully initialized shift map from file \""
125
<< fileWithPath.c_str() <<
"\""
);
126
}
127
return
StatusCode::SUCCESS;
128
}
129
130
float
MdtCalibrationShiftMapBase::getValue
(
const
Identifier
&
id
)
const
{
131
std::map<Identifier, float>::const_iterator itr =
m_shiftValues
.find(
id
);
132
if
(itr ==
m_shiftValues
.end()){
133
ATH_MSG_FATAL
(
"The identifier "
<<
m_idHelperSvc
->toString(
id
)<<
" is not known for a T0 smearing"
);
134
throw
std::runtime_error(
"MdtT0Calibration - Invalid identifier"
);
135
}
136
return
itr->second;
137
}
ATH_CHECK
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition
AthCheckMacros.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition
AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition
AthMsgStreamMacros.h:32
MdtCalibrationShiftMapBase.h
PathResolver.h
MdtCalibrationShiftMapBase::getValue
float getValue(const Identifier &id) const override
Definition
MdtCalibrationShiftMapBase.cxx:130
MdtCalibrationShiftMapBase::m_mapFileName
Gaudi::Property< std::string > m_mapFileName
Definition
MdtCalibrationShiftMapBase.h:52
MdtCalibrationShiftMapBase::m_shiftValues
std::map< Identifier, float > m_shiftValues
Definition
MdtCalibrationShiftMapBase.h:57
MdtCalibrationShiftMapBase::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition
MdtCalibrationShiftMapBase.h:60
MdtCalibrationShiftMapBase::m_mapIsInitialized
bool m_mapIsInitialized
Definition
MdtCalibrationShiftMapBase.h:58
MdtCalibrationShiftMapBase::loadMapFromFile
StatusCode loadMapFromFile()
Definition
MdtCalibrationShiftMapBase.cxx:60
MdtCalibrationShiftMapBase::initialize
virtual StatusCode initialize() override
Definition
MdtCalibrationShiftMapBase.cxx:15
MdtCalibrationShiftMapBase::m_mdtCab
SG::ReadCondHandleKey< MuonMDT_CablingMap > m_mdtCab
Definition
MdtCalibrationShiftMapBase.h:61
MdtCalibrationShiftMapBase::dumpMapAsFile
StatusCode dumpMapAsFile()
Definition
MdtCalibrationShiftMapBase.cxx:22
MdtCalibrationShiftMapBase::initializeMap
virtual StatusCode initializeMap()=0
MdtCalibrationShiftMapBase::MdtCalibrationShiftMapBase
MdtCalibrationShiftMapBase(const std::string &type, const std::string &name, const IInterface *parent)
Definition
MdtCalibrationShiftMapBase.cxx:12
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
Definition
PathResolver.cxx:220
Identifier
Definition
IdentifierFieldParser.cxx:14
type
file
TFile * file
Definition
tile_monitor.h:29
Generated on
for ATLAS Offline Software by
1.17.0