Project
Loading...
Searching...
No Matches
TPCScaler.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
16
18#include <TFile.h>
19#include <TTree.h>
20#include "Framework/Logger.h"
22
23using namespace o2::tpc;
24
25void TPCScaler::dumpToFile(const char* file, const char* name)
26{
27 TFile out(file, "RECREATE");
28 TTree tree(name, name);
29 tree.SetAutoSave(0);
30 tree.Branch("TPCScaler", this);
31 tree.Fill();
32 out.WriteObject(&tree, name);
33}
34
35void TPCScaler::dumpToFile(const char* file, const char* name, double startTimeMS, double endTimeMS)
36{
37 TPCScaler scaler;
38 scaler.mIonDriftTimeMS = mIonDriftTimeMS;
39 scaler.mRun = mRun;
40 scaler.mFirstTFOrbit = (startTimeMS <= 0) ? mFirstTFOrbit : (mFirstTFOrbit + (startTimeMS - mTimeStampMS) / (o2::constants::lhc::LHCOrbitMUS * 0.001));
41 scaler.mTimeStampMS = (startTimeMS <= 0) ? mTimeStampMS : startTimeMS;
42 scaler.mIntegrationTimeMS = mIntegrationTimeMS;
43
44 const int dataIdx = (startTimeMS <= 0) ? 0 : getDataIdx(startTimeMS);
45 const int idxDataStart = (startTimeMS <= 0) ? 0 : dataIdx;
46 const int idxDataEndA = ((endTimeMS < 0) || (dataIdx > getNValues(Side::A))) ? getNValues(Side::A) : dataIdx;
47 const int idxDataEndC = ((endTimeMS < 0) || (dataIdx > getNValues(Side::C))) ? getNValues(Side::C) : dataIdx;
48 scaler.mScalerA = std::vector<float>(mScalerA.begin() + idxDataStart, mScalerA.begin() + idxDataEndA);
49 scaler.mScalerC = std::vector<float>(mScalerC.begin() + idxDataStart, mScalerC.begin() + idxDataEndC);
50 scaler.dumpToFile(file, name);
51}
52
53void TPCScaler::dumpToFileSlices(const char* file, const char* name, int minutesPerObject, float marginMS, float marginCCDBMinutes)
54{
56 LOGP(error, "Number of points stored for A-side and C-side is different");
57 return;
58 }
59 const long marginCCDBMS = marginCCDBMinutes * 60 * 1000;
60 const float msPerObjectTmp = minutesPerObject * 60 * 1000;
61 const int valuesPerSlice = static_cast<int>(msPerObjectTmp / mIntegrationTimeMS);
62 const int marginPerSlice = static_cast<int>(marginMS / mIntegrationTimeMS);
63 int nSlices = getNValues(Side::A) / valuesPerSlice; // number of output objects
64
65 LOGP(info, "Producing {} objects with a CCDB margin of {} ms and {} margin per slice", nSlices, marginCCDBMS, marginPerSlice);
66 if (nSlices == 0) {
67 nSlices = 1;
68 }
69
70 for (int i = 0; i < nSlices; ++i) {
71 const int idxDataStart = (i == 0) ? 0 : (i * valuesPerSlice - marginPerSlice);
72 int idxDataEnd = (i == nSlices - 1) ? getNValues(Side::A) : ((i + 1) * valuesPerSlice + marginPerSlice);
73 if (idxDataEnd > getNValues(Side::A)) {
74 idxDataEnd = getNValues(Side::A);
75 }
76
77 TPCScaler scaler;
78 scaler.mIonDriftTimeMS = mIonDriftTimeMS;
79 scaler.mRun = mRun;
80 scaler.mTimeStampMS = (i == 0) ? mTimeStampMS : (mTimeStampMS + idxDataStart * static_cast<double>(mIntegrationTimeMS));
81 scaler.mFirstTFOrbit = mFirstTFOrbit + (scaler.mTimeStampMS - mTimeStampMS) / (o2::constants::lhc::LHCOrbitMUS * 0.001);
82 scaler.mIntegrationTimeMS = mIntegrationTimeMS;
83 scaler.mScalerA = std::vector<float>(mScalerA.begin() + idxDataStart, mScalerA.begin() + idxDataEnd);
84 scaler.mScalerC = std::vector<float>(mScalerC.begin() + idxDataStart, mScalerC.begin() + idxDataEnd);
85
86 const long timePerSliceMS = valuesPerSlice * mIntegrationTimeMS;
87 const long tsCCDBStart = mTimeStampMS + i * timePerSliceMS;
88 const long tsCCDBStartMargin = (i == 0) ? (tsCCDBStart - marginCCDBMS) : tsCCDBStart;
89 const long tsCCDBEnd = (i == nSlices - 1) ? (getEndTimeStampMS(o2::tpc::Side::A) + marginCCDBMS) : (tsCCDBStart + timePerSliceMS);
90 const std::string fileOut = fmt::format("{}_{}_{}_{}.root", file, i, tsCCDBStartMargin, tsCCDBEnd);
91 scaler.dumpToFile(fileOut.data(), name);
92 }
93}
94
95void TPCScaler::loadFromFile(const char* inpf, const char* name)
96{
97 TFile out(inpf, "READ");
98 TTree* tree = (TTree*)out.Get(name);
100}
101
102void TPCScaler::setFromTree(TTree& tpcScalerTree)
103{
104 TPCScaler* scalerTmp = this;
105 tpcScalerTree.SetBranchAddress("TPCScaler", &scalerTmp);
106 const int entries = tpcScalerTree.GetEntries();
107 if (entries > 0) {
108 tpcScalerTree.GetEntry(0);
109 } else {
110 LOGP(error, "TPCScaler not found in input file");
111 }
112 tpcScalerTree.SetBranchAddress("TPCScaler", nullptr);
113}
114
115float TPCScaler::getMeanScaler(double timestamp, o2::tpc::Side side) const
116{
117 // index to data buffer
118 const int idxData = getDataIdx(timestamp);
119 const int nVals = getNValuesIonDriftTime();
120 const int nValues = getNValues(side);
121 if ((nVals == 0) || (nVals > nValues)) {
122 return -1;
123 LOGP(error, "Empty data provided {}", nValues);
124 }
125
126 // clamp indices to min and max
127 const int lastIdx = std::clamp(idxData, nVals, nValues);
128 const int firstIdx = (lastIdx == nValues) ? (nValues - nVals) : std::clamp(idxData - nVals, 0, nValues);
129
130 // sump up values from last ion drift time
131 float sum = 0;
132 float sumW = 0;
133 const bool useWeights = mUseWeights && getScalerWeights().isValid();
134 for (int i = firstIdx; i < lastIdx; ++i) {
135 float weight = 1;
136 if (useWeights) {
137 const double relTSMS = mTimeStampMS + i * mIntegrationTimeMS - timestamp;
138 weight = getScalerWeights().getWeight(relTSMS);
139 }
140 sum += getScalers(i, side) * weight;
141 sumW += weight;
142 }
143 if (sumW != 0) {
144 return (sum / sumW);
145 }
146 return 0;
147}
148
149float TPCScalerWeights::getWeight(float deltaTime) const
150{
151 const float idxF = (deltaTime - mFirstTimeStampMS) / mSamplingTimeMS;
152 const int idx = idxF;
153 if ((idx < 0) || (idx > mWeights.size() - 1)) {
154 LOGP(error, "Index out of range for deltaTime: {} mFirstTimeStampMS: {} mSamplingTimeMS: {}", deltaTime, mFirstTimeStampMS, mSamplingTimeMS);
155 // set weight 1 to in case it is out of range. This can only happen if the TPC scaler is not valid for given time
156 return 1;
157 }
158
159 if ((idxF == idx) || (idx == mWeights.size() - 1)) {
160 // no interpolation required
161 return mWeights[idx];
162 } else {
163 // interpolate scaler
164 const float y0 = mWeights[idx];
165 const float y1 = mWeights[idx + 1];
166 const float x0 = idx;
167 const float x1 = idx + 1;
168 const float x = idxF;
169 const float y = ((y0 * (x1 - x)) + y1 * (x - x0)) / (x1 - x0);
170 return y;
171 }
172}
173
174void TPCScaler::clampScalers(float minThreshold, float maxThreshold, Side side)
175{
176 auto& scaler = (side == o2::tpc::Side::A) ? mScalerA : mScalerC;
177 std::transform(std::begin(scaler), std::end(scaler), std::begin(scaler), [minThreshold, maxThreshold](auto val) { return std::clamp(val, minThreshold, maxThreshold); });
178}
int32_t i
Header to collect LHC related constants.
uint32_t side
Definition RawData.h:0
int getNValuesIonDriftTime() const
Definition TPCScaler.h:121
void dumpToFileSlices(const char *file, const char *name, int minutesPerObject, float marginMS=500, float marginCCDBMinutes=1)
Definition TPCScaler.cxx:53
float getScalers(unsigned int idx, o2::tpc::Side side) const
Definition TPCScaler.h:97
float getMeanScaler(double timestamp, o2::tpc::Side side) const
int getNValues(o2::tpc::Side side) const
Definition TPCScaler.h:51
void loadFromFile(const char *inpf, const char *name)
Definition TPCScaler.cxx:95
void clampScalers(float minThreshold, float maxThreshold, Side side)
double getEndTimeStampMS(o2::tpc::Side side) const
Definition TPCScaler.h:115
void dumpToFile(const char *file, const char *name)
Definition TPCScaler.cxx:25
void useWeights(bool useWeights)
enable usage of weights
Definition TPCScaler.h:142
void setFromTree(TTree &tpcScalerTree)
set this object from input tree
const auto & getScalerWeights() const
Definition TPCScaler.h:139
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint GLfloat GLfloat GLfloat GLfloat y1
Definition glcorearb.h:5034
GLuint GLfloat GLfloat GLfloat x1
Definition glcorearb.h:5034
GLuint const GLchar * name
Definition glcorearb.h:781
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
GLint y
Definition glcorearb.h:270
GLuint GLfloat x0
Definition glcorearb.h:5034
GLuint GLfloat * val
Definition glcorearb.h:1582
GLuint GLfloat GLfloat y0
Definition glcorearb.h:5034
constexpr double LHCOrbitMUS
Global TPC definitions and constants.
Definition SimTraits.h:167
Side
TPC readout sidE.
Definition Defs.h:35
@ A
Definition Defs.h:35
@ C
Definition Defs.h:36
DataT sum(const Vector< DataT, N > &a)
Definition Vector.h:107
float mSamplingTimeMS
sampling of the stored weights
Definition TPCScaler.h:34
std::vector< float > mWeights
stored weights
Definition TPCScaler.h:36
float mFirstTimeStampMS
first timestamp
Definition TPCScaler.h:35
float getWeight(float deltaTime) const
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))