ATLAS Offline Software
CalibrationOracleFileIOTool.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 // this
7 
8 // MdtCalibUtils
11 
12 // MdtCalibData
19 
20 // MdtCalibRt
22 
23 // MuonCalibStandAloneBase
26 
27 // MuonCalibMath
29 
30 // c - c++
31 #include <dirent.h>
32 #include <sys/types.h>
33 
34 #include "fstream"
35 #include "iostream"
36 #include "string"
37 
38 namespace MuonCalib {
39 
40  CalibrationOracleFileIOTool::CalibrationOracleFileIOTool(const std::string &t, const std::string &n, const IInterface *p) :
41  AthAlgTool(t, n, p), m_calib_dir("oracle_calib") {
42  declareInterface<ICalibrationIOTool>(this);
43  declareProperty("outputLocation", m_calib_dir);
44  }
45 
47  int iov_end) {
48  // create directory
49  system(("mkdir -p " + m_calib_dir + "/t0s").c_str());
50  // open file
51  std::string t0_dbfile_name(m_calib_dir + "/t0s/DB_t0" + station_id.regionId() + ".dat");
52  FILE *db_file;
53  db_file = fopen(t0_dbfile_name.c_str(), "w");
54  if (!db_file) {
55  ATH_MSG_FATAL("Cannot open output file!");
56  return StatusCode::FAILURE;
57  }
58  TubeDataFromFile t0_file;
59  t0_file.setNRegions(1);
60  t0_file.addTubes(0, t0_output);
61  t0_file.write_forDB(db_file, -9999, iov_start, iov_end);
62  fclose(db_file);
63  return StatusCode::SUCCESS;
64  }
65 
66  StatusCode CalibrationOracleFileIOTool::WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr<const IRtResolution> resolution,
67  const NtupleStationId &station_id, int iov_start, int iov_end, bool /*real_rt*/,
68  bool /*real_resolution*/) {
69  RtDataFromFile rt_data;
70  std::shared_ptr<const IRtRelation> new_rt = rt_relation->rt();
71  std::unique_ptr<RtDataFromFile::RtRelation> rt = std::make_unique<RtDataFromFile::RtRelation>();
72  int rt_region_id(0);
73  rt_data.setNRts(1);
74  rt_data.addRt(rt_region_id, rt.get(), rt_relation->fullInfo().get());
75  if (!fill_rt(rt, new_rt, resolution)) return StatusCode::FAILURE;
76  // write out the r-t relationship //
77  std::string location = "location";
78  int mdt_rt_id = 99;
79  std::string mdt_rt_map_t_id = "mdt_rt_map_t_id";
80  int mdt_rt_map_r_id = 99;
81  int mdt_rt_map_s_id = 99;
82  system(("mkdir -p " + m_calib_dir + "/rts").c_str());
83  std::string rt_dbfile_name(m_calib_dir + "/rts/DBrt_" + station_id.regionId() + ".dat");
84  std::string rtt_dbfile_name(m_calib_dir + "/rts/DBrt_" + station_id.regionId() + "_t.dat");
85  std::string rtr_dbfile_name(m_calib_dir + "/rts/DBrt_" + station_id.regionId() + "_r.dat");
86  std::string rts_dbfile_name(m_calib_dir + "/rts/DBrt_" + station_id.regionId() + "_s.dat");
87  FILE *db_rt_file;
88  db_rt_file = fopen(rt_dbfile_name.c_str(), "w");
89  FILE *db_rtt_file;
90  db_rtt_file = fopen(rtt_dbfile_name.c_str(), "w");
91  FILE *db_rtr_file;
92  db_rtr_file = fopen(rtr_dbfile_name.c_str(), "w");
93  FILE *db_rts_file;
94  db_rts_file = fopen(rts_dbfile_name.c_str(), "w");
95  if (db_rt_file == nullptr || db_rtt_file == nullptr || db_rtr_file == nullptr || db_rts_file == nullptr) {
96  ATH_MSG_FATAL("Cannot open output files");
97  if (db_rt_file) fclose(db_rt_file);
98  if (db_rtt_file) fclose(db_rtt_file);
99  if (db_rtr_file) fclose(db_rtr_file);
100  if (db_rts_file) fclose(db_rts_file);
101  return StatusCode::FAILURE;
102  }
103  ATH_MSG_INFO("Writing out r-t relationships in the files for calibration db.");
104  // for the time being the variables:
105  // mdt_rt_id,mdt_rt_map_t_id,mdt_rt_map_r_id,
106  // mdt_rt_map_s_id and location
107  // are dummy.
108  fprintf(db_rt_file, " %d,%d,%d,%d,%d,%s,", mdt_rt_id, station_id.FixedId(), -9999, iov_start, iov_end, location.c_str());
109  fprintf(db_rtt_file, " %s,%d,%d,", mdt_rt_map_t_id.c_str(), mdt_rt_id, mdt_rt_id);
110  fprintf(db_rtr_file, " %d,%d,%s,", mdt_rt_map_r_id, mdt_rt_id, mdt_rt_map_t_id.c_str());
111  fprintf(db_rts_file, " %d,%d,", mdt_rt_map_s_id, mdt_rt_id);
112  rt_data.write_forDB(db_rt_file, db_rtt_file, db_rtr_file, db_rts_file, rt_region_id);
113  ATH_MSG_INFO("r-t relationships wrote in the files for calibration db.");
114  fclose(db_rt_file);
115  fclose(db_rtt_file);
116  fclose(db_rtr_file);
117  fclose(db_rts_file);
118  return StatusCode::SUCCESS;
119  }
120 
121  bool CalibrationOracleFileIOTool::fill_rt(std::unique_ptr<RtDataFromFile::RtRelation> &rt, const std::shared_ptr<const IRtRelation>& new_rt,
122  const std::shared_ptr<const MuonCalib::IRtResolution>& resolut) {
124  // VARIABLES //
126  const CalibFunc::ParVec &rt_param = new_rt->parameters();
127  // parameters of the r-t relationship to be copied
128  std::shared_ptr<const RtChebyshev> rt_Chebyshev = std::dynamic_pointer_cast<const RtChebyshev>(new_rt);
129  std::shared_ptr<const RtRelationLookUp> rt_lookup = std::dynamic_pointer_cast<const RtRelationLookUp>(new_rt);
130 
132  // FILL THE r-t CLASS //
134 
135  // case 1: r-t relationship is stored in the class RtChebyshev //
136  if (rt_Chebyshev) {
137  unsigned int nb_points(100);
138  double t_length(rt_Chebyshev->tUpper() - rt_Chebyshev->tLower());
139  double bin_size(t_length / static_cast<double>(nb_points - 1));
140  for (unsigned int k = 0; k < nb_points; k++) {
141  double time(rt_Chebyshev->tLower() + k * bin_size);
142  double radius(rt_Chebyshev->radius(time));
143  double resol(0.0);
144  resol = resolut->resolution(time);
145  if (std::isnan(time) || std::isnan(radius) || std::isnan(resol)) {
146  ATH_MSG_FATAL("Filling nan into rt relation!");
147  // return false;
148  }
149  rt->addEntry(time, radius, resol);
150  }
151  }
152 
153  // case 2: r-t relationship is stored in the class RtRelationLookUp //
154  if (rt_lookup) {
155  double t_min(rt_param[0]);
156  double bin_size = rt_param[1];
157  unsigned int nb_points(rt_lookup->nPar() - 2);
158  for (unsigned int k = 0; k < nb_points; k++) {
159  double radius(rt_param[k + 2]);
160  double resol(0.0);
161 
162  resol = resolut->resolution(t_min + bin_size * k);
163  if (std::isnan(radius) || std::isnan(resol)) {
164  ATH_MSG_FATAL("Filling nan into rt relation!");
165  // return false;
166  }
167  rt->addEntry(t_min + bin_size * k, rt_param[k + 2], resol);
168  }
169  }
170 
172  // DECLARE THE NEW r-t RELATIONSHIP VALID //
174  rt->isValid(1);
175 
176  return true;
177  } // CalibrationOracleFileIOTool::fill_rt
178 
179 } // namespace MuonCalib
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
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MuonCalib::CalibrationOracleFileIOTool::m_calib_dir
std::string m_calib_dir
path to calibration directory - job option
Definition: CalibrationOracleFileIOTool.h:36
MuonCalib::NtupleStationId::regionId
std::string regionId() const
return the region id string
Definition: NtupleStationId.cxx:69
MuonCalib::CalibrationOracleFileIOTool::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: CalibrationOracleFileIOTool.cxx:121
MuonCalib::CalibrationOracleFileIOTool::WriteRt
StatusCode WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr< const IRtResolution > resolution, const NtupleStationId &station_id, int iov_start, int iov_end, bool, bool) override
write rt
Definition: CalibrationOracleFileIOTool.cxx:66
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MuonCalib::NtupleStationId
Definition: NtupleStationId.h:36
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
MdtTubeFitContainer.h
MuonCalib::NtupleStationId::FixedId
int FixedId() const
Definition: NtupleStationId.cxx:93
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
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
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::TubeDataFromFile::write_forDB
std::ostream & write_forDB(std::ostream &ftube, int mdt_head, int lowrun, int uprun) const
Definition: TubeDataFromFile.cxx:80
IRtResolution.h
TubeDataFromFile.h
MuonCalib::RtDataFromFile::addRt
bool addRt(int regionId, RtRelation *rt)
RtDataFromFile takes ownership of rt.
Definition: RtDataFromFile.h:51
MuonCalib::CalibrationOracleFileIOTool::WriteT0
StatusCode WriteT0(const MdtTubeFitContainer *t0_output, const NtupleStationId &station_id, int iov_start, int iov_end) override
initialisation
Definition: CalibrationOracleFileIOTool.cxx:46
MuonCalib::RtDataFromFile::setNRts
void setNRts(unsigned int nrts)
set total number of regions
Definition: RtDataFromFile.h:44
RtResolutionFromPoints.h
MuonCalib::RtCalibrationOutput::rt
std::shared_ptr< const IRtRelation > rt() const
access to private attributes
Definition: RtCalibrationOutput.h:27
MuonCalib::CalibrationOracleFileIOTool::CalibrationOracleFileIOTool
CalibrationOracleFileIOTool(const std::string &t, const std::string &n, const IInterface *p)
constructor
Definition: CalibrationOracleFileIOTool.cxx:40
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::RtDataFromFile::write_forDB
void write_forDB(FILE *frt, FILE *frtt, FILE *frtr, FILE *frts, int region) const
Definition: RtDataFromFile.cxx:86
CalibrationOracleFileIOTool.h
RtChebyshev.h
PixelModuleFeMask_create_db.iov_end
list iov_end
Definition: PixelModuleFeMask_create_db.py:75
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
PixelModuleFeMask_create_db.iov_start
list iov_start
Definition: PixelModuleFeMask_create_db.py:74
SamplePoint.h
AthAlgTool
Definition: AthAlgTool.h:26
fitman.k
k
Definition: fitman.py:528
RtDataFromFile.h