File indexing completed on 2025-01-18 10:17:32
0001
0002
0003
0004
0005
0006 #pragma once
0007 #include <JANA/Calibrations/JCalibration.h>
0008 #include <JANA/Calibrations/JCalibrationFile.h>
0009 #include <JANA/Calibrations/JCalibrationGenerator.h>
0010
0011 #include <JANA/JService.h>
0012
0013 #include <algorithm>
0014 #include "JANA/Services/JParameterManager.h"
0015 #include "JResource.h"
0016
0017 class JCalibrationManager : public JService {
0018
0019 vector<JCalibration *> m_calibrations;
0020 vector<JResource *> m_resource_managers;
0021 vector<JCalibrationGenerator *> m_calibration_generators;
0022
0023 pthread_mutex_t m_calibration_mutex;
0024 pthread_mutex_t m_resource_manager_mutex;
0025
0026 std::shared_ptr<JParameterManager> m_params;
0027
0028 std::string m_url = "file://./";
0029 std::string m_context = "default";
0030
0031 public:
0032
0033 JCalibrationManager() {
0034 pthread_mutex_init(&m_calibration_mutex, nullptr);
0035 pthread_mutex_init(&m_resource_manager_mutex, nullptr);
0036
0037 SetPrefix("jana");
0038 }
0039
0040 void acquire_services(JServiceLocator* sl) {
0041
0042
0043
0044
0045 if (getenv("JANA_CALIB_URL") != nullptr) m_url = getenv("JANA_CALIB_URL");
0046 if (getenv("JANA_CALIB_CONTEXT") != nullptr) m_context = getenv("JANA_CALIB_CONTEXT");
0047
0048 m_params = sl->get<JParameterManager>();
0049 m_params->SetDefaultParameter("JANA:CALIB_URL", m_url, "URL used to access calibration constants");
0050 m_params->SetDefaultParameter("JANA:CALIB_CONTEXT", m_context,
0051 "Calibration context to pass on to concrete JCalibration derived class");
0052 }
0053
0054 void AddCalibrationGenerator(JCalibrationGenerator *generator) {
0055 m_calibration_generators.push_back(generator);
0056 };
0057
0058 void RemoveCalibrationGenerator(JCalibrationGenerator *generator) {
0059
0060 vector<JCalibrationGenerator *> &f = m_calibration_generators;
0061 vector<JCalibrationGenerator *>::iterator iter = std::find(f.begin(), f.end(), generator);
0062 if (iter != f.end())f.erase(iter);
0063 }
0064
0065 vector<JCalibrationGenerator *> GetCalibrationGenerators() { return m_calibration_generators; }
0066
0067 void GetJCalibrations(vector<JCalibration *> &calibs) { calibs = m_calibrations; }
0068
0069 JCalibration *GetJCalibration(unsigned int run_number) {
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 pthread_mutex_lock(&m_calibration_mutex);
0082
0083 vector<JCalibration *>::iterator iter = m_calibrations.begin();
0084 for (; iter != m_calibrations.end(); iter++) {
0085 if ((*iter)->GetRun() != (int) run_number)continue;
0086 if ((*iter)->GetURL() != m_url)continue;
0087 if ((*iter)->GetContext() != m_context)continue;
0088
0089 JCalibration *g = *iter;
0090 pthread_mutex_unlock(&m_calibration_mutex);
0091 return g;
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101 JCalibrationGenerator *gen = nullptr;
0102 double liklihood = 0.0;
0103 for (unsigned int i = 0; i < m_calibration_generators.size(); i++) {
0104 double my_liklihood = m_calibration_generators[i]->CheckOpenable(m_url, run_number, m_context);
0105 if (my_liklihood > liklihood) {
0106 liklihood = my_liklihood;
0107 gen = m_calibration_generators[i];
0108 }
0109 }
0110
0111
0112 JCalibration *g = nullptr;
0113 if (gen) {
0114 g = gen->MakeJCalibration(m_url, run_number, m_context);
0115 }
0116 if (gen == nullptr && (m_url.find("file://") == 0)) {
0117 g = new JCalibrationFile(m_url, run_number, m_context);
0118 }
0119 if (g) {
0120 m_calibrations.push_back(g);
0121 LOG_INFO(m_logger)
0122 << "Created JCalibration object of type: " << g->className() << "\n"
0123 << " Generated via: "
0124 << (gen == nullptr ? "fallback creation of JCalibrationFile" : gen->Description())
0125 << "\n"
0126 << " Run: " << g->GetRun() << "\n"
0127 << " URL: " << g->GetURL() << "\n"
0128 << " context: " << g->GetContext()
0129 << LOG_END;
0130 } else {
0131 JLogMessage m(m_logger, JLogger::Level::ERROR);
0132 m << "Unable to create JCalibration object!\n"
0133 << " Run: " << run_number << "\n"
0134 << " URL: " << m_url << "\n"
0135 << " context: " << m_context << "\n";
0136
0137 if (gen) {
0138 m << " Attempted to use generator: " << gen->Description();
0139 } else {
0140 m << " No appropriate generators found. Attempted JCalibrationFile";
0141 }
0142 std::move(m) << LOG_END;
0143 }
0144
0145
0146 pthread_mutex_unlock(&m_calibration_mutex);
0147 return g;
0148 }
0149
0150 template<class T>
0151 bool GetCalib(unsigned int run_number, unsigned int event_number, string namepath, map<string, T> &vals) {
0152
0153
0154
0155
0156
0157
0158
0159 vals.clear();
0160 JCalibration *calib = GetJCalibration(run_number);
0161 if (!calib) {
0162 LOG_ERROR(m_logger) << "Unable to get JCalibration object for run " << run_number << LOG_END;
0163 return true;
0164 }
0165 return calib->Get(namepath, vals, event_number);
0166 }
0167
0168 template<class T>
0169 bool GetCalib(unsigned int run_number, unsigned int event_number, string namepath, vector<T> &vals) {
0170
0171
0172
0173 vals.clear();
0174 JCalibration *calib = GetJCalibration(run_number);
0175 if (!calib) {
0176 LOG_ERROR(m_logger) << "Unable to get JCalibration object for run " << run_number << LOG_END;
0177 return true;
0178 }
0179 return calib->Get(namepath, vals, event_number);
0180 }
0181
0182 JResource* GetResource(unsigned int run_number = 0) {
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 if (run_number == 0) {
0197 pthread_mutex_lock(&m_resource_manager_mutex);
0198 if (m_resource_managers.empty()) {
0199 if (m_calibrations.empty()) {
0200 m_resource_managers.push_back(new JResource(m_params, nullptr));
0201 } else {
0202 m_resource_managers.push_back(new JResource(m_params, m_calibrations[0]));
0203 }
0204 }
0205 pthread_mutex_unlock(&m_resource_manager_mutex);
0206
0207 return m_resource_managers[0];
0208 }
0209
0210
0211 JCalibration *jcalib = GetJCalibration(run_number);
0212 for (unsigned int i = 0; i < m_resource_managers.size(); i++) {
0213 if (m_resource_managers[i]->GetJCalibration() == jcalib)return m_resource_managers[i];
0214 }
0215
0216
0217 JResource *resource_manager = new JResource(m_params, jcalib);
0218 pthread_mutex_lock(&m_resource_manager_mutex);
0219 m_resource_managers.push_back(resource_manager);
0220 pthread_mutex_unlock(&m_resource_manager_mutex);
0221
0222 return resource_manager;
0223
0224 }
0225
0226 [[deprecated("Replaced with GetResource()")]]
0227 JResource *GetLargeCalibration(unsigned int run_number = 0) {
0228 return GetResource(run_number);
0229 }
0230
0231 };
0232
0233