ATLAS Offline Software
CalibrationFileIOTool.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 // this
7 
8 // Framework
10 
11 // MdtCalibUtils
14 
15 // MdtCalibData
22 
23 // MdtCalibRt
25 
26 // MuonCalibStandAloneBase
29 
30 // MuonCalibMath
32 
33 // c - c++
34 #include <dirent.h>
35 #include <sys/types.h>
36 
37 #include "fstream"
38 #include "iostream"
39 #include "string"
40 
41 namespace MuonCalib {
42 
43  CalibrationFileIOTool::CalibrationFileIOTool(const std::string &t, const std::string &n, const IInterface *p) :
44  AthAlgTool(t, n, p), m_calib_dir("calibration"), m_use_fixed_id(false), m_rt_lookup(true) {
45  declareInterface<ICalibrationIOTool>(this);
46  declareProperty("outputLocation", m_calib_dir);
47  declareProperty("UseFixedId", m_use_fixed_id);
48  declareProperty("RtRelationLookup", m_rt_lookup);
49  }
50 
51  StatusCode CalibrationFileIOTool::WriteT0(const MdtTubeFitContainer *t0_output, const NtupleStationId &station_id, int /*iov_start*/,
52  int /*iov_end*/) {
53  // create directory
54  system(("mkdir -p " + m_calib_dir + "/t0s").c_str());
55  // open file
56  std::string t0_file_name(m_calib_dir + "/t0s/T0" + station_id.regionId() + ".dat");
57  std::ofstream output_file(t0_file_name.c_str());
58  if (output_file.fail()) {
59  ATH_MSG_ERROR("Could not open t0 file " << t0_file_name << "!");
60  return StatusCode::FAILURE;
61  }
62  // write
63  TubeDataFromFile t0_file;
64  t0_file.setNRegions(1);
65  t0_file.addTubes(0, t0_output);
66  t0_file.write(output_file);
67  return StatusCode::SUCCESS;
68  }
69 
70  StatusCode CalibrationFileIOTool::WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr<const IRtResolution> resolution,
71  const NtupleStationId &station_id, int /*iov_start*/, int /*iov_end*/, bool /*real_rt*/,
72  bool /*real_resolution*/) {
73  RtDataFromFile rt_data;
74  std::shared_ptr<const IRtRelation> new_rt = rt_relation->rt();
75  std::unique_ptr<RtDataFromFile::RtRelation> rt = std::make_unique<RtDataFromFile::RtRelation>();
76  if (m_use_fixed_id) {
77  rt->setRegionId(station_id.FixedId());
78  rt_data.setVersion(2, 0);
79  } else {
80  if (station_id.RegionHash() != 0) { rt->setRegionId(station_id.RegionHash()); }
81  }
82  int rt_region_id(0);
83  rt_data.setNRts(1);
84  rt_data.addRt(rt_region_id, rt.get(), rt_relation->fullInfo().get());
85  if (!fill_rt(rt, new_rt, resolution)) return StatusCode::FAILURE;
86  // write out the r-t relationship //
87  system(("mkdir -p " + m_calib_dir + "/rts").c_str());
88  std::string rt_file_name(m_calib_dir + "/rts/Rt_" + station_id.regionId() + ".dat");
89  std::ofstream output_file(rt_file_name.c_str());
90  rt_data.write(output_file, rt_region_id);
91  return StatusCode::SUCCESS;
92  }
93 
94  StatusCode CalibrationFileIOTool::LoadT0 (std::map<NtupleStationId, MdtStationT0Container *> &t0s, int /*iov_id*/) {
95  ATH_MSG_INFO("Reading calibration from '" << (m_calib_dir + "/t0s") << "'");
96  t0s.clear();
97  DIR *directory(opendir((m_calib_dir + "/t0s").c_str()));
98  if (directory == nullptr) return StatusCode::SUCCESS;
99  // According to `man readdir` this is thread-safe in "modern glibc" if
100  // used on different directory streams, which would be the case here:
101  struct dirent *dent ATLAS_THREAD_SAFE;
102  // loop on all files in directory
103  while ((dent = readdir(directory)) != nullptr) {
104  std::string nm(dent->d_name);
105  std::string station_str;
106  int eta, phi, ml;
107  // interpret filename and check if it is an t0 file
108  if (!interpret_chamber_name(nm, "T0", station_str, eta, phi, ml)) continue;
109  // store
111  if (!id.Initialize(station_str, eta, phi, ml)) continue;
112  ATH_MSG_INFO("Reading t0 for " << station_str << "_" << eta << "_" << phi);
113  MuonCalib::MdtStationT0Container *t0_container = new MuonCalib::MdtStationT0Container((m_calib_dir + "/t0s/" + nm).c_str());
114  if (t0_container->t0_loaded()) {
115  t0s[id] = t0_container;
116  ATH_MSG_INFO("Read t0 for " << id.regionId());
117  } else {
118  ATH_MSG_ERROR("t0 file for " << id.regionId() << " is corrupted!");
119  delete t0_container;
120  }
121  }
122  closedir(directory);
123  return StatusCode::SUCCESS;
124  }
125 
126  StatusCode CalibrationFileIOTool::LoadRt(std::map<NtupleStationId, IRtRelation *> &rts, std::map<NtupleStationId, IRtResolution *> &res,
127  int /*iov_id*/) {
128  rts.clear();
129  res.clear();
130  DIR *directory(opendir((m_calib_dir + "/rts").c_str()));
131  if (directory == nullptr) return StatusCode::SUCCESS;
132  // According to `man readdir` this is thread-safe in "modern glibc" if
133  // used on different directory streams, which would be the case here:
134  struct dirent *dent ATLAS_THREAD_SAFE;
135  // loop on all files in directory
136  while ((dent = readdir(directory)) != nullptr) {
137  std::string nm(dent->d_name);
138  std::string station_str;
139  int eta, phi, ml;
140  // interpret filename and check if it is an t0 file
141  if (!interpret_chamber_name(nm, "Rt_", station_str, eta, phi, ml)) continue;
143  if (!id.Initialize(station_str, eta, phi, ml)) continue;
144  ATH_MSG_INFO("Reading rt for " << station_str << "_" << eta << "_" << phi << "_" << ml);
145  // read rt ralation
146  read_rt_relation((m_calib_dir + "/rts/" + nm), rts, res, id);
147  ATH_MSG_INFO("Read rt for " << id.regionId());
148  }
149  closedir(directory);
150  return StatusCode::SUCCESS;
151  }
152 
153  bool CalibrationFileIOTool::fill_rt(std::unique_ptr<RtDataFromFile::RtRelation> &rt, const std::shared_ptr<const IRtRelation>& new_rt,
154  const std::shared_ptr<const MuonCalib::IRtResolution>& resolut) {
156  // VARIABLES //
158  const CalibFunc::ParVec &rt_param = new_rt->parameters();
159  // parameters of the r-t relationship to be copied
160  std::shared_ptr<const RtChebyshev> rt_Chebyshev = std::dynamic_pointer_cast<const RtChebyshev>(new_rt);
161  std::shared_ptr<const RtRelationLookUp> rt_lookup = std::dynamic_pointer_cast<const RtRelationLookUp>(new_rt);
162 
164  // FILL THE r-t CLASS //
166 
167  // case 1: r-t relationship is stored in the class RtChebyshev //
168  if (rt_Chebyshev) {
169  unsigned int nb_points(100);
170  double t_length(rt_Chebyshev->tUpper() - rt_Chebyshev->tLower());
171  double bin_size(t_length / static_cast<double>(nb_points - 1));
172  for (unsigned int k = 0; k < nb_points; k++) {
173  double time(rt_Chebyshev->tLower() + k * bin_size);
174  double radius(rt_Chebyshev->radius(time));
175  double resol(0.0);
176  resol = resolut->resolution(time);
177  if (std::isnan(time) || std::isnan(radius) || std::isnan(resol)) {
178  ATH_MSG_FATAL("Filling nan into rt relation!");
179  // return false;
180  }
181  rt->addEntry(time, radius, resol);
182  }
183  }
184 
185  // case 2: r-t relationship is stored in the class RtRelationLookUp //
186  if (rt_lookup) {
187  double t_min(rt_param[0]);
188  double bin_size = rt_param[1];
189  unsigned int nb_points(rt_lookup->nPar() - 2);
190  for (unsigned int k = 0; k < nb_points; k++) {
191  double radius(rt_param[k + 2]);
192  double resol(0.0);
193 
194  resol = resolut->resolution(t_min + bin_size * k);
195  if (std::isnan(radius) || std::isnan(resol)) {
196  ATH_MSG_FATAL("Filling nan into rt relation!");
197  // return false;
198  }
199  rt->addEntry(t_min + bin_size * k, rt_param[k + 2], resol);
200  }
201  }
202 
204  // DECLARE THE NEW r-t RELATIONSHIP VALID //
206  if (new_rt->HasTmaxDiff()) rt->addEntry(new_rt->GetTmaxDiff(), -9999, 0);
207  rt->isValid(1);
208 
209  return true;
210  } // end CalibrationFileIOTool::fill_rt
211 
212  inline bool CalibrationFileIOTool::interpret_chamber_name(const std::string &nm, const char *prefix, std::string &station, int &eta,
213  int &phi, int &ml) {
214  // check if name begins with the prefix
215  std::string prefix_st(prefix);
216  if (!nm.starts_with( prefix_st)) return false;
217  // check if filename ends in .dat
218  if (!nm.ends_with( ".dat")) return false;
219  // cut prefix and suffix from filename
220  std::string cutout(nm, prefix_st.size(), nm.size() - 4 - prefix_st.size());
221  // extract station name
222  int uscore_pos(cutout.find('_'));
223  if (uscore_pos <= 0) return false;
224  station = std::string(cutout, 0, uscore_pos);
225  // get eta, phi, and multilayer
226  std::string cutout2(cutout, uscore_pos + 1);
227  int count_items(sscanf(cutout2.c_str(), "%80d_%80d_%80d", &phi, &eta, &ml));
228  if (count_items < 2) return false;
229  if (count_items != 3) ml = 0;
230  return true;
231  }
232 
233  inline void CalibrationFileIOTool::read_rt_relation(const std::string &fname, std::map<NtupleStationId, IRtRelation *> &rts,
234  std::map<NtupleStationId, IRtResolution *> &res_map,
235  const MuonCalib::NtupleStationId &id) {
236  // open file
237  std::ifstream infile(fname.c_str());
238  if (infile.fail()) {
239  ATH_MSG_ERROR("Cannot open file '" << fname << "' for reading!");
240  return;
241  }
242  // sample points
243  std::vector<double> r, t, res;
244  std::string sdummy;
245  double dummy;
246  // ignore the first line
247  getline(infile, sdummy);
248  // read table
249  float multilayer_diff(9e9);
250  while (!(infile.eof() || infile.fail())) {
251  infile >> dummy;
252  if (infile.eof() || infile.fail() || dummy > 15.0) break;
253  if (dummy < -9998) {
254  infile >> multilayer_diff;
255  infile >> dummy;
256  } else {
257  r.push_back(dummy);
258  infile >> dummy;
259  t.push_back(dummy);
260  infile >> dummy;
261  res.push_back(dummy);
262  }
263  }
264  if (r.size() < 3) {
265  ATH_MSG_WARNING("Not enough good rt points for " << id.regionId() << "!");
266  return;
267  }
268  std::vector<MuonCalib::SamplePoint> point(r.size());
269  for (unsigned int k = 0; k < point.size(); k++) {
270  point[k].set_x1(t[k]);
271  point[k].set_x2(r[k]);
272  point[k].set_error(1.0);
273  }
274  // convert rt relation
275  MuonCalib::RtFromPoints rt_from_points;
276  if (m_rt_lookup) {
278  } else {
280  }
281  if (multilayer_diff < 8e8) rts[id]->SetTmaxDiff(multilayer_diff);
282  // create resolution function
283  MuonCalib::RtResolutionFromPoints res_from_points;
284  for (unsigned int k = 0; k < point.size(); k++) {
285  point[k].set_x1(t[k]);
286  point[k].set_x2(res[k]);
287  point[k].set_error(1.0);
288  }
290  } // end CalibrationFileIOTool::read_rt_relation
291 
292 } // namespace MuonCalib
MuonCalib::CalibrationFileIOTool::fill_rt
bool fill_rt(std::unique_ptr< RtDataFromFile::RtRelation > &rt, const std::shared_ptr< const IRtRelation > &new_rt, const std::shared_ptr< const MuonCalib::IRtResolution > &resolut)
fill rt relation
Definition: CalibrationFileIOTool.cxx:153
MuonCalib::CalibrationFileIOTool::WriteT0
StatusCode WriteT0(const MdtTubeFitContainer *t0_output, const NtupleStationId &station_id, int, int) override
write out t0
Definition: CalibrationFileIOTool.cxx:51
beamspotman.r
def r
Definition: beamspotman.py:676
MuonCalib::CalibrationFileIOTool::read_rt_relation
void read_rt_relation(const std::string &fname, std::map< NtupleStationId, IRtRelation * > &rts, std::map< NtupleStationId, IRtResolution * > &res, const MuonCalib::NtupleStationId &id)
create the rt relation and resolution
Definition: CalibrationFileIOTool.cxx:233
MuonCalib::RtCalibrationOutput
Definition: RtCalibrationOutput.h:21
RtCalibrationOutput.h
MuonCalib::IRtResolution::resolution
virtual double resolution(double t, double bgRate=0.0) const =0
returns resolution for a give time and background rate
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
run.infile
string infile
Definition: run.py:13
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MuonCalib::CalibrationFileIOTool::m_use_fixed_id
bool m_use_fixed_id
use fixed identifier if set tot true - job option
Definition: CalibrationFileIOTool.h:37
MuonCalib::NtupleStationId::regionId
std::string regionId() const
return the region id string
Definition: NtupleStationId.cxx:69
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
MuonCalib::RtDataFromFile::write
std::ostream & write(std::ostream &os, int region) const
Definition: RtDataFromFile.cxx:55
CalibrationFileIOTool.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MuonCalib::RtResolutionFromPoints::getRtResolutionChebyshev
static RtResolutionChebyshev getRtResolutionChebyshev(const std::vector< SamplePoint > &sample_points, const unsigned int &order)
< get an RtResolutionChebyshev resembling the sigma(t) function as described by the sample points in ...
Definition: RtResolutionFromPoints.cxx:38
TRTCalib_Extractor.rts
dictionary rts
Definition: TRTCalib_Extractor.py:15
TRTCalib_cfilter.t0s
list t0s
Definition: TRTCalib_cfilter.py:110
MuonCalib::NtupleStationId
Definition: NtupleStationId.h:36
MuonCalib::MdtStationT0Container
Definition: MdtStationT0Container.h:24
MuonCalib::TubeDataFromFile
Definition: TubeDataFromFile.h:24
MuonCalib::RtDataFromFile
Manages the I/O of the Rt realtions from/to file.
Definition: RtDataFromFile.h:21
MdtStationT0Container.h
RtRelationLookUp.h
MuonCalib::RtChebyshev
Definition: RtChebyshev.h:29
MdtTubeFitContainer.h
MuonCalib::NtupleStationId::FixedId
int FixedId() const
Definition: NtupleStationId.cxx:93
MuonCalib::NtupleStationId::RegionHash
int RegionHash() const
Definition: NtupleStationId.h:201
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
MuonCalib::RtRelationLookUp
Equidistant look up table for rt-relations with the time as key.
Definition: RtRelationLookUp.h:23
Dedxcorrection::resolution
double resolution[nGasTypes][nParametersResolution]
Definition: TRT_ToT_Corrections.h:46
NtupleStationId.h
MuonCalib::TubeDataFromFile::setNRegions
void setNRegions(unsigned int n)
set total number of regions
Definition: TubeDataFromFile.h:47
MuonCalib::RtFromPoints::getRtRelationLookUp
static RtRelationLookUp getRtRelationLookUp(const std::vector< SamplePoint > &sample_points)
Definition: RtFromPoints.cxx:76
MuonCalib::TubeDataFromFile::write
std::ostream & write(std::ostream &os) const
Definition: TubeDataFromFile.cxx:17
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DeMoScan.directory
string directory
Definition: DeMoScan.py:80
beamspotman.n
n
Definition: beamspotman.py:731
RtFromPoints.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonCalib::CalibrationFileIOTool::m_calib_dir
std::string m_calib_dir
path to calibration directory - job option
Definition: CalibrationFileIOTool.h:35
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
MuonCalib::CalibFunc::ParVec
std::vector< double > ParVec
Definition: CalibFunc.h:35
MuonCalib::RtFromPoints::getRtChebyshev
static RtChebyshev getRtChebyshev(const std::vector< SamplePoint > &sample_points, const unsigned int &order)
< get an RtChebyshev resembling the r(t) function as described by the sample points in the vector "sa...
Definition: RtFromPoints.cxx:39
python.xAODType.dummy
dummy
Definition: xAODType.py:4
mergePhysValFiles.output_file
output_file
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:27
IRtResolution.h
TubeDataFromFile.h
MuonCalib::RtDataFromFile::addRt
bool addRt(int regionId, RtRelation *rt)
RtDataFromFile takes ownership of rt.
Definition: RtDataFromFile.h:51
MuonCalib::RtDataFromFile::setNRts
void setNRts(unsigned int nrts)
set total number of regions
Definition: RtDataFromFile.h:44
MuonCalib::RtFromPoints
Definition: RtFromPoints.h:30
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
MuonCalib::MdtStationT0Container::t0_loaded
bool t0_loaded() const
Definition: MdtStationT0Container.h:64
RtResolutionFromPoints.h
MuonCalib::RtCalibrationOutput::rt
std::shared_ptr< const IRtRelation > rt() const
access to private attributes
Definition: RtCalibrationOutput.h:27
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
MuonCalib::CalibrationFileIOTool::LoadRt
StatusCode LoadRt(std::map< NtupleStationId, IRtRelation * > &rts, std::map< NtupleStationId, IRtResolution * > &res, int) override
load rts
Definition: CalibrationFileIOTool.cxx:126
MuonCalib::TubeDataFromFile::addTubes
bool addTubes(int regionId, const MdtTubeFitContainer *tubes)
TubeDataFromFile takes ownership of the MdtTubeFitContainer.
Definition: TubeDataFromFile.h:53
ParticleGun_SamplingFraction.radius
radius
Definition: ParticleGun_SamplingFraction.py:96
MuonCalib::RtResolutionChebyshev
Definition: RtResolutionChebyshev.h:34
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
MuonCalib::RtDataFromFile::setVersion
void setVersion(int major, int minor)
Definition: RtDataFromFile.h:77
CalibCoolCompareRT.nm
nm
Definition: CalibCoolCompareRT.py:110
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
MuonCalib::CalibrationFileIOTool::CalibrationFileIOTool
CalibrationFileIOTool(const std::string &t, const std::string &n, const IInterface *p)
constructor
Definition: CalibrationFileIOTool.cxx:43
MuonCalib::RtResolutionFromPoints
Definition: RtResolutionFromPoints.h:30
RtChebyshev.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonCalib::CalibrationFileIOTool::LoadT0
StatusCode LoadT0(std::map< NtupleStationId, MdtStationT0Container * > &t0s, int) override
load t0s
Definition: CalibrationFileIOTool.cxx:94
MuonCalib::MdtTubeFitContainer
Holds single-tube full calibration information of one chamber.
Definition: MdtTubeFitContainer.h:16
MuonCalib::RtCalibrationOutput::fullInfo
std::shared_ptr< const RtFullInfo > fullInfo() const
Definition: RtCalibrationOutput.h:28
MuonCalib::CalibrationFileIOTool::WriteRt
StatusCode WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr< const IRtResolution > resolution, const NtupleStationId &station_id, int, int, bool, bool) override
write rt
Definition: CalibrationFileIOTool.cxx:70
SamplePoint.h
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
AthAlgTool
Definition: AthAlgTool.h:26
checker_macros.h
Define macros for attributes used to control the static checker.
MuonCalib::CalibrationFileIOTool::interpret_chamber_name
static bool interpret_chamber_name(const std::string &nm, const char *prefix, std::string &station, int &eta, int &phi, int &ml)
extract station identifier from file name
Definition: CalibrationFileIOTool.cxx:212
fitman.k
k
Definition: fitman.py:528
MuonCalib::CalibrationFileIOTool::m_rt_lookup
bool m_rt_lookup
create rt relation as lookup table if set tot true - job option
Definition: CalibrationFileIOTool.h:39
RtDataFromFile.h