Project
Loading...
Searching...
No Matches
PressureTemperatureHelper.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
19#include "DataFormatsTPC/DCS.h"
25
26using namespace o2::tpc;
27using namespace o2::framework;
28
30{
31 pc.inputs().get<dcs::Pressure*>("pressure");
32 pc.inputs().get<dcs::Temperature*>("temperature");
33}
34
36{
37 // getForTimeStamp() is cheap to call every time; compare the returned pointer, not ccdb's own TTL-based cache
38 // validity, since ccdb only swaps in a new pointer once the content actually changes.
39 const auto pressurePath = CDBTypeMap.at(CDBType::CalPressure);
40 if (auto* pressure = ccdb.getForTimeStamp<dcs::Pressure>(pressurePath, timestampMS)) {
41 if (pressure != mLastPressureObj) {
42 accountCCDBInputs(ConcreteDataMatcher(o2::header::gDataOriginTPC, "PRESSURECCDB", 0), const_cast<dcs::Pressure*>(pressure));
43 mLastPressureObj = pressure;
44 }
45 }
46 const auto temperaturePath = CDBTypeMap.at(CDBType::CalTemperature);
47 if (auto* temperature = ccdb.getForTimeStamp<dcs::Temperature>(temperaturePath, timestampMS)) {
48 if (temperature != mLastTemperatureObj) {
49 accountCCDBInputs(ConcreteDataMatcher(o2::header::gDataOriginTPC, "TEMPERATURECCDB", 0), const_cast<dcs::Temperature*>(temperature));
50 mLastTemperatureObj = temperature;
51 }
52 }
53}
54
56{
57 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "PRESSURECCDB", 0)) {
58 LOGP(info, "Updating pressure");
59 const auto& pressure = ((dcs::Pressure*)obj);
60 mPressure.second = pressure->robustPressure.time;
61 mPressure.first = pressure->robustPressure.robustPressure;
62 return true;
63 }
64
65 if (matcher == ConcreteDataMatcher(o2::header::gDataOriginTPC, "TEMPERATURECCDB", 0)) {
66 LOGP(info, "Updating temperature");
67 auto temp = *(dcs::Temperature*)obj;
68 temp.fitTemperature(o2::tpc::Side::A, mFitIntervalMS, false);
69 temp.fitTemperature(o2::tpc::Side::C, mFitIntervalMS, false);
70
71 mTemperatureA.first.clear();
72 mTemperatureC.first.clear();
73 mTemperatureA.second.clear();
74 mTemperatureC.second.clear();
75
76 for (const auto& dp : temp.statsA.data) {
77 mTemperatureA.first.emplace_back(toKelvin(dp.value.mean));
78 mTemperatureA.second.emplace_back(dp.time);
79 }
80
81 for (const auto& dp : temp.statsC.data) {
82 mTemperatureC.first.emplace_back(toKelvin(dp.value.mean));
83 mTemperatureC.second.emplace_back(dp.time);
84 }
85
86 // check if temperature data is available
87 if (mTemperatureA.first.empty() && mTemperatureC.first.empty()) {
88 float temperature = toKelvin(temp.getMeanTempRaw());
89 mTemperatureA.first.emplace_back(temperature);
90 mTemperatureA.second.emplace_back(0);
91 mTemperatureC.first.emplace_back(temperature);
92 mTemperatureC.second.emplace_back(0);
93 LOGP(warning, "No temperature data available from fit. Using average temperature {} K", temperature);
94 }
95
96 return true;
97 }
98 return false;
99}
100
101void PressureTemperatureHelper::requestCCDBInputs(std::vector<InputSpec>& inputs)
102{
103 addInput(inputs, {"pressure", o2::header::gDataOriginTPC, "PRESSURECCDB", 0, Lifetime::Condition, ccdbParamSpec(CDBTypeMap.at(CDBType::CalPressure), {}, 1)});
104 addInput(inputs, {"temperature", o2::header::gDataOriginTPC, "TEMPERATURECCDB", 0, Lifetime::Condition, ccdbParamSpec(CDBTypeMap.at(CDBType::CalTemperature), {}, 1)});
105}
106
107void PressureTemperatureHelper::addInput(std::vector<InputSpec>& inputs, InputSpec&& isp)
108{
109 if (std::find(inputs.begin(), inputs.end(), isp) == inputs.end()) {
110 inputs.emplace_back(isp);
111 }
112}
113
119
120void PressureTemperatureHelper::addOutput(std::vector<OutputSpec>& outputs, OutputSpec&& osp)
121{
122 if (std::find(outputs.begin(), outputs.end(), osp) == outputs.end()) {
123 outputs.emplace_back(osp);
124 }
125}
126
127float PressureTemperatureHelper::interpolate(const std::vector<ULong64_t>& timestamps, const std::vector<float>& values, ULong64_t timestamp) const
128{
129 if (auto idxClosest = o2::math_utils::findClosestIndices(timestamps, timestamp)) {
130 auto [idxLeft, idxRight] = *idxClosest;
131 if (idxRight > idxLeft) {
132 const auto x0 = timestamps[idxLeft];
133 const auto x1 = timestamps[idxRight];
134 const float y0 = values[idxLeft];
135 const float y1 = values[idxRight];
136 const float y = (y0 * (x1 - timestamp) + y1 * (timestamp - x0)) / (x1 - x0);
137 return y;
138 } else {
139 return values[idxLeft];
140 }
141 }
142 return 0; // this should never happen
143}
144
146{
147 const float pressure = getPressure(timestamp);
148 const auto temp = getTemperature(timestamp);
149 LOGP(info, "Sending pressure {}, temperature A {} and temperature C {} for timestamp {}", pressure, temp.first, temp.second, timestamp);
152}
153
154float PressureTemperatureHelper::getTP(int64_t ts) const
155{
156 const float pressure = getPressure(ts);
157 const auto temp = getMeanTemperature(ts);
158 if (pressure <= 0) {
159 LOGP(error, "Pressure {} is zero or negative, cannot compute T/P ratio for timestamp {}", pressure, ts);
160 return 0;
161 }
162 const float tp = temp / pressure;
163 return tp;
164}
165
166float PressureTemperatureHelper::getMeanTemperature(const ULong64_t timestamp) const
167{
168 const auto temp = getTemperature(timestamp);
169
170 float sumT = 0;
171 int w = 0;
172 constexpr float minTemp = toKelvin(15);
173 constexpr float maxTemp = toKelvin(25);
174 if (auto t = temp.first; t > minTemp && t < maxTemp) {
175 sumT += t;
176 ++w;
177 }
178 if (auto t = temp.second; t > minTemp && t < maxTemp) {
179 sumT += t;
180 ++w;
181 }
182
183 if (w == 0) {
184 constexpr float defaultTemp = toKelvin(19.6440f);
185 LOGP(info, "Returning default temperature of {}K", defaultTemp);
186 return defaultTemp;
187 }
188
189 const float meanT = sumT / w;
190 return meanT;
191}
192
193std::pair<ULong64_t, ULong64_t> PressureTemperatureHelper::getMinMaxTime() const
194{
195 ULong64_t minTime = std::numeric_limits<ULong64_t>::max();
196 ULong64_t maxTime = 0;
197
198 if (!mPressure.first.empty()) {
199 minTime = std::min(minTime, mPressure.second.front());
200 maxTime = std::max(maxTime, mPressure.second.back());
201 }
202 if (!mTemperatureA.first.empty()) {
203 minTime = std::min(minTime, mTemperatureA.second.front());
204 maxTime = std::max(maxTime, mTemperatureA.second.back());
205 }
206 if (!mTemperatureC.first.empty()) {
207 minTime = std::min(minTime, mTemperatureC.second.front());
208 maxTime = std::max(maxTime, mTemperatureC.second.back());
209 }
210
211 return {minTime, maxTime};
212}
CDB Type definitions for TPC.
DCS data point data formats.
Helper class to extract pressure and temperature.
T * getForTimeStamp(std::string const &path, long timestamp, std::map< std::string, std::string > *headers=nullptr)
retrieve an object of type T from CCDB as stored under path and timestamp. Optional to get the header...
void snapshot(const Output &spec, T const &object)
decltype(auto) get(R binding, int part=0) const
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
const void * mLastTemperatureObj
last pressure object accounted for via BasicCCDBManager, for dedup only, not streamed
static void requestCCDBInputs(std::vector< o2::framework::InputSpec > &inputs)
float getPressure(const ULong64_t timestamp) const
get pressure for given time stamp in ms
void extractCCDBInputs(o2::framework::ProcessingContext &pc) const
trigger checking for CCDB objects
std::pair< ULong64_t, ULong64_t > getMinMaxTime() const
get minimum and maximum time stamps of the pressure and temperature data
static constexpr o2::header::DataDescription getDataDescriptionTemperature()
dataformats::Pair< float, float > getTemperature(const ULong64_t timestamp) const
get temperature for given time stamp in ms
static void addOutput(std::vector< o2::framework::OutputSpec > &outputs, o2::framework::OutputSpec &&osp)
std::pair< std::vector< float >, std::vector< ULong64_t > > mTemperatureA
temperature values A-side
void sendPTForTS(o2::framework::ProcessingContext &pc, const ULong64_t timestamp) const
send temperature and pressure for given time stamp
int mFitIntervalMS
fit interval for the temperature
float getMeanTemperature(const ULong64_t timestamp) const
get mean temperature over A and C side
std::pair< std::vector< float >, std::vector< ULong64_t > > mPressure
pressure values for both measurements
static constexpr o2::header::DataDescription getDataDescriptionPressure()
static void addInput(std::vector< o2::framework::InputSpec > &inputs, o2::framework::InputSpec &&isp)
bool accountCCDBInputs(const o2::framework::ConcreteDataMatcher &matcher, void *obj)
check for new CCDB objects
static constexpr float toKelvin(float celsius)
std::pair< std::vector< float >, std::vector< ULong64_t > > mTemperatureC
temperature values C-side
static void setOutputs(std::vector< o2::framework::OutputSpec > &outputs)
define outputs in case pressure and temperature will be send
float interpolate(const std::vector< ULong64_t > &timestamps, const std::vector< float > &values, ULong64_t timestamp) const
interpolate input values for given timestamp
GLuint GLfloat GLfloat GLfloat GLfloat y1
Definition glcorearb.h:5034
GLuint GLfloat GLfloat GLfloat x1
Definition glcorearb.h:5034
GLint y
Definition glcorearb.h:270
GLenum GLsizei GLsizei GLint * values
Definition glcorearb.h:1576
GLuint GLfloat x0
Definition glcorearb.h:5034
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
GLuint GLfloat GLfloat y0
Definition glcorearb.h:5034
constexpr o2::header::DataOrigin gDataOriginTPC
Definition DataHeader.h:576
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::vector< ConfigParamSpec > ccdbParamSpec(std::string const &path, int runDependent, std::vector< CCDBMetadata > metadata={}, int qrate=0)
std::optional< std::pair< size_t, size_t > > findClosestIndices(const std::vector< DataTimeType > &timestamps, DataTime timestamp)
Definition fit.h:796
Global TPC definitions and constants.
Definition SimTraits.h:172
const std::unordered_map< CDBType, const std::string > CDBTypeMap
Storage name in CCDB for each calibration and parameter type.
Definition CDBTypes.h:98
@ A
Definition Defs.h:37
@ C
Definition Defs.h:38
@ CalPressure
DCS pressure measurements.
@ CalTemperature
DCS temperature measurements.