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#include <atomic>
23
24using namespace o2::tpc;
25
26void TPCScaler::dumpToFile(const char* file, const char* name)
27{
28 TFile out(file, "RECREATE");
29 TTree tree(name, name);
30 tree.SetAutoSave(0);
31 tree.Branch("TPCScaler", this);
32 tree.Fill();
33 out.WriteObject(&tree, name);
34}
35
36void TPCScaler::dumpToFile(const char* file, const char* name, double startTimeMS, double endTimeMS)
37{
38 TPCScaler scaler;
39 scaler.mIonDriftTimeMS = mIonDriftTimeMS;
40 scaler.mRun = mRun;
41 scaler.mFirstTFOrbit = (startTimeMS <= 0) ? mFirstTFOrbit : (mFirstTFOrbit + (startTimeMS - mTimeStampMS) / (o2::constants::lhc::LHCOrbitMUS * 0.001));
42 scaler.mTimeStampMS = (startTimeMS <= 0) ? mTimeStampMS : startTimeMS;
43 scaler.mIntegrationTimeMS = mIntegrationTimeMS;
44
45 const int dataIdx = (startTimeMS <= 0) ? 0 : getDataIdx(startTimeMS);
46 const int idxDataStart = (startTimeMS <= 0) ? 0 : dataIdx;
47 const int idxDataEndA = ((endTimeMS < 0) || (dataIdx > getNValues(Side::A))) ? getNValues(Side::A) : dataIdx;
48 const int idxDataEndC = ((endTimeMS < 0) || (dataIdx > getNValues(Side::C))) ? getNValues(Side::C) : dataIdx;
49 scaler.mScalerA = std::vector<float>(mScalerA.begin() + idxDataStart, mScalerA.begin() + idxDataEndA);
50 scaler.mScalerC = std::vector<float>(mScalerC.begin() + idxDataStart, mScalerC.begin() + idxDataEndC);
51 scaler.dumpToFile(file, name);
52}
53
54void TPCScaler::dumpToFileSlices(const char* file, const char* name, int minutesPerObject, float marginMS, float marginCCDBMinutes)
55{
57 LOGP(error, "Number of points stored for A-side and C-side is different");
58 return;
59 }
60 const long marginCCDBMS = marginCCDBMinutes * 60 * 1000;
61 const float msPerObjectTmp = minutesPerObject * 60 * 1000;
62 const int valuesPerSlice = static_cast<int>(msPerObjectTmp / mIntegrationTimeMS);
63 const int marginPerSlice = static_cast<int>(marginMS / mIntegrationTimeMS);
64 int nSlices = getNValues(Side::A) / valuesPerSlice; // number of output objects
65
66 LOGP(info, "Producing {} objects with a CCDB margin of {} ms and {} margin per slice", nSlices, marginCCDBMS, marginPerSlice);
67 if (nSlices == 0) {
68 nSlices = 1;
69 }
70
71 for (int i = 0; i < nSlices; ++i) {
72 const int idxDataStart = (i == 0) ? 0 : (i * valuesPerSlice - marginPerSlice);
73 int idxDataEnd = (i == nSlices - 1) ? getNValues(Side::A) : ((i + 1) * valuesPerSlice + marginPerSlice);
74 if (idxDataEnd > getNValues(Side::A)) {
75 idxDataEnd = getNValues(Side::A);
76 }
77
78 TPCScaler scaler;
79 scaler.mIonDriftTimeMS = mIonDriftTimeMS;
80 scaler.mRun = mRun;
81 scaler.mTimeStampMS = (i == 0) ? mTimeStampMS : (mTimeStampMS + idxDataStart * static_cast<double>(mIntegrationTimeMS));
82 scaler.mFirstTFOrbit = mFirstTFOrbit + (scaler.mTimeStampMS - mTimeStampMS) / (o2::constants::lhc::LHCOrbitMUS * 0.001);
83 scaler.mIntegrationTimeMS = mIntegrationTimeMS;
84 scaler.mScalerA = std::vector<float>(mScalerA.begin() + idxDataStart, mScalerA.begin() + idxDataEnd);
85 scaler.mScalerC = std::vector<float>(mScalerC.begin() + idxDataStart, mScalerC.begin() + idxDataEnd);
86
87 const long timePerSliceMS = valuesPerSlice * mIntegrationTimeMS;
88 const long tsCCDBStart = mTimeStampMS + i * timePerSliceMS;
89 const long tsCCDBStartMargin = (i == 0) ? (tsCCDBStart - marginCCDBMS) : tsCCDBStart;
90 const long tsCCDBEnd = (i == nSlices - 1) ? (getEndTimeStampMS(o2::tpc::Side::A) + marginCCDBMS) : (tsCCDBStart + timePerSliceMS);
91 const std::string fileOut = fmt::format("{}_{}_{}_{}.root", file, i, tsCCDBStartMargin, tsCCDBEnd);
92 scaler.dumpToFile(fileOut.data(), name);
93 }
94}
95
96void TPCScaler::loadFromFile(const char* inpf, const char* name)
97{
98 TFile out(inpf, "READ");
99 TTree* tree = (TTree*)out.Get(name);
101}
102
103void TPCScaler::setFromTree(TTree& tpcScalerTree)
104{
105 TPCScaler* scalerTmp = this;
106 tpcScalerTree.SetBranchAddress("TPCScaler", &scalerTmp);
107 const int entries = tpcScalerTree.GetEntries();
108 if (entries > 0) {
109 tpcScalerTree.GetEntry(0);
110 } else {
111 LOGP(error, "TPCScaler not found in input file");
112 }
113 tpcScalerTree.SetBranchAddress("TPCScaler", nullptr);
114}
115
116float TPCScaler::getMeanScaler(double timestamp, o2::tpc::Side side) const
117{
118 // index to data buffer
119 const int idxData = getDataIdx(timestamp);
120 const int nVals = getNValuesIonDriftTime();
121 const int nValues = getNValues(side);
122 if ((nVals == 0) || (nVals > nValues)) {
123 return -1;
124 LOGP(error, "Empty data provided {}", nValues);
125 }
126
127 // clamp indices to min and max
128 const int lastIdx = std::clamp(idxData, nVals, nValues);
129 const int firstIdx = (lastIdx == nValues) ? (nValues - nVals) : std::clamp(idxData - nVals, 0, nValues);
130
131 // sump up values from last ion drift time
132 float sum = 0;
133 float sumW = 0;
134 int nOutOfRange = 0;
135 const bool useWeights = mUseWeights && getScalerWeights().isValid();
136 for (int i = firstIdx; i < lastIdx; ++i) {
137 float weight = 1;
138 if (useWeights) {
139 const double relTSMS = mTimeStampMS + i * mIntegrationTimeMS - timestamp;
140 nOutOfRange += !getScalerWeights().isInRange(relTSMS);
141 weight = getScalerWeights().getWeight(relTSMS);
142 }
143 sum += getScalers(i, side) * weight;
144 sumW += weight;
145 }
146
147 // values outside the stored weights (e.g. timestamp within the first ion drift time of this object, where the clamped range extends
148 // beyond the timestamp) are used with weight 1: reported once per call and at most MaxOutOfRangeReports times per process
149 if (nOutOfRange > 0) {
150 constexpr int MaxOutOfRangeReports = 20;
151 static std::atomic<int> nReports{0};
152 const int report = nReports++;
153 if (report < MaxOutOfRangeReports) {
154 LOGP(error, "{} of {} scaler values for timestamp {} outside the weights (first timestamp {} ms, sampling {} ms): weight 1 used{}", nOutOfRange,
155 lastIdx - firstIdx, timestamp, getScalerWeights().mFirstTimeStampMS, getScalerWeights().mSamplingTimeMS,
156 (report + 1 == MaxOutOfRangeReports) ? ", further messages suppressed" : "");
157 }
158 }
159
160 if (sumW != 0) {
161 return (sum / sumW);
162 }
163 return 0;
164}
165
166bool TPCScalerWeights::isInRange(float deltaTime) const
167{
168 const int idx = (deltaTime - mFirstTimeStampMS) / mSamplingTimeMS;
169 return (idx >= 0) && (idx < static_cast<int>(mWeights.size()));
170}
171
172float TPCScalerWeights::getWeight(float deltaTime) const
173{
174 const float idxF = (deltaTime - mFirstTimeStampMS) / mSamplingTimeMS;
175 const int idx = idxF;
176 if (!isInRange(deltaTime)) {
177 // 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 (reported in getMeanScaler)
178 return 1;
179 }
180
181 if ((idxF == idx) || (idx == mWeights.size() - 1)) {
182 // no interpolation required
183 return mWeights[idx];
184 } else {
185 // interpolate scaler
186 const float y0 = mWeights[idx];
187 const float y1 = mWeights[idx + 1];
188 const float x0 = idx;
189 const float x1 = idx + 1;
190 const float x = idxF;
191 const float y = ((y0 * (x1 - x)) + y1 * (x - x0)) / (x1 - x0);
192 return y;
193 }
194}
195
196void TPCScaler::clampScalers(float minThreshold, float maxThreshold, Side side)
197{
198 auto& scaler = (side == o2::tpc::Side::A) ? mScalerA : mScalerC;
199 std::transform(std::begin(scaler), std::end(scaler), std::begin(scaler), [minThreshold, maxThreshold](auto val) { return std::clamp(val, minThreshold, maxThreshold); });
200}
std::vector< long > entries
std::vector< double > sum
int32_t i
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines the history is shortened to this when it reaches twice the size Time in seconds an info message fades out after setInfoTime h OpenGL display display Font size Do not use Freetype for font Renderer for event Frontend to use for event ROOT macros used as track filter Console script file
char error[CURL_ERROR_SIZE]
Header to collect LHC related constants.
uint32_t side
Definition RawData.h:0
int getNValuesIonDriftTime() const
Definition TPCScaler.h:124
void dumpToFileSlices(const char *file, const char *name, int minutesPerObject, float marginMS=500, float marginCCDBMinutes=1)
Definition TPCScaler.cxx:54
float getScalers(unsigned int idx, o2::tpc::Side side) const
Definition TPCScaler.h:100
float getMeanScaler(double timestamp, o2::tpc::Side side) const
int getNValues(o2::tpc::Side side) const
Definition TPCScaler.h:54
void loadFromFile(const char *inpf, const char *name)
Definition TPCScaler.cxx:96
void clampScalers(float minThreshold, float maxThreshold, Side side)
double getEndTimeStampMS(o2::tpc::Side side) const
Definition TPCScaler.h:118
void dumpToFile(const char *file, const char *name)
Definition TPCScaler.cxx:26
void useWeights(bool useWeights)
enable usage of weights
Definition TPCScaler.h:145
void setFromTree(TTree &tpcScalerTree)
set this object from input tree
const auto & getScalerWeights() const
Definition TPCScaler.h:142
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
void report(gsl::span< o2::InteractionTimeRecord > irs, int threshold, bool verbose)
Global TPC definitions and constants.
Side
TPC readout sidE.
Definition Defs.h:37
@ A
Definition Defs.h:37
@ C
Definition Defs.h:38
float mSamplingTimeMS
sampling of the stored weights
Definition TPCScaler.h:37
std::vector< float > mWeights
stored weights
Definition TPCScaler.h:39
float mFirstTimeStampMS
first timestamp
Definition TPCScaler.h:38
float getWeight(float deltaTime) const
bool isInRange(float deltaTime) const
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))