Project
Loading...
Searching...
No Matches
CTPRateFetcher.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
13
14#include <map>
15#include <vector>
17
18using namespace o2::ctp;
19double CTPRateFetcher::fetch(o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, int runNumber, std::string sourceName)
20{
21 auto triggerRate = fetchNoPuCorr(ccdb, timeStamp, runNumber, sourceName);
22 if (triggerRate >= 0) {
23 return pileUpCorrection(triggerRate);
24 }
25 return -1;
26}
27double CTPRateFetcher::fetchNoPuCorr(o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, int runNumber, std::string sourceName)
28{
29 setupRun(runNumber, ccdb, timeStamp, 1);
30 if (sourceName.find("ZNC") != std::string::npos) {
31 if (runNumber < 544448) {
32 return fetchCTPratesInputsNoPuCorr(timeStamp, 25) / (sourceName.find("hadronic") != std::string::npos ? 28. : 1.);
33 } else {
34 return fetchCTPratesClassesNoPuCorr(timeStamp, "C1ZNC-B-NOPF-CRU", 6) / (sourceName.find("hadronic") != std::string::npos ? 28. : 1.);
35 }
36 } else if (sourceName == "T0CE") {
37 return fetchCTPratesClassesNoPuCorr(timeStamp, "CMTVXTCE-B-NOPF");
38 } else if (sourceName == "T0SC") {
39 return fetchCTPratesClassesNoPuCorr(timeStamp, "CMTVXTSC-B-NOPF");
40 } else if (sourceName == "T0VTX") {
41 if (runNumber < 534202) {
42 return fetchCTPratesClassesNoPuCorr(timeStamp, "minbias_TVX_L0", 3); // 2022
43 } else {
44 double ret = fetchCTPratesClassesNoPuCorr(timeStamp, "CMTVX-B-NOPF");
45 if (ret == -2.) {
46 LOG(info) << "Trying different class";
47 ret = fetchCTPratesClassesNoPuCorr(timeStamp, "CMTVX-NONE");
48 if (ret < 0) {
49 LOG(error) << "None of the classes used for lumi found";
50 return -1.;
51 }
52 }
53 return ret;
54 }
55 }
56 LOG(error) << "CTP rate for " << sourceName << " not available";
57 return -1.;
58}
60{
61 mScalers = scalers;
62 mScalers.convertRawToO2();
63}
64//
65int CTPRateFetcher::getRates(std::array<double, 3>& rates, o2::ccdb::BasicCCDBManager* ccdb, int runNumber, const std::string sourceName) // rates at start,stop and middle of the run
66{
67 setupRun(runNumber, ccdb, 0, 1);
68 mOrbit = 1;
69 mOutsideLimits = 1;
70 auto orbitlimits = mScalers.getOrbitLimit();
71 // std::cout << "1st orbit:" << orbitlimits.first << " last:" << orbitlimits.second << " Middle:" << (orbitlimits.first + orbitlimits.second)/2 << std::endl;
72 double rate0 = fetch(ccdb, orbitlimits.first, mRunNumber, sourceName);
73 double rateLast = fetch(ccdb, orbitlimits.second, mRunNumber, sourceName);
74 double rateM = fetch(ccdb, (orbitlimits.first + orbitlimits.second) / 2, mRunNumber, sourceName);
75 // std::cout << rate0 << " " << rateLast << " " << rateM << std::endl;
76 rates[0] = rate0;
77 rates[1] = rateLast;
78 rates[2] = rateM;
79 return 0;
80}
81double CTPRateFetcher::getLumiNoPuCorr(const std::string& classname, int type)
82{
83 if (classname == "zncinp") {
84 return mScalers.getLumiNoPuCorr(26, 7);
85 }
86 std::vector<ctp::CTPClass>& ctpcls = mConfig.getCTPClasses();
87 std::vector<int> clslist = mConfig.getTriggerClassList();
88 int classIndex = -1;
89 for (size_t i = 0; i < clslist.size(); i++) {
90 if (ctpcls[i].name.find(classname) != std::string::npos) {
91 classIndex = i;
92 break;
93 }
94 }
95 if (classIndex == -1) {
96 LOG(warn) << "Trigger class " << classname << " not found in CTPConfiguration";
97 return -1;
98 }
99 return mScalers.getLumiNoPuCorr(classIndex, type);
100}
101double CTPRateFetcher::getLumiWPuCorr(const std::string& classname, int type)
102{
103 std::vector<std::pair<double, double>> scals;
104 if (classname == "zncinp") {
105 scals = mScalers.getRatesForIndex(26, 7);
106 } else {
107 std::vector<ctp::CTPClass>& ctpcls = mConfig.getCTPClasses();
108 std::vector<int> clslist = mConfig.getTriggerClassList();
109 int classIndex = -1;
110 for (size_t i = 0; i < clslist.size(); i++) {
111 if (ctpcls[i].name.find(classname) != std::string::npos) {
112 classIndex = i;
113 break;
114 }
115 }
116 if (classIndex == -1) {
117 LOG(warn) << "Trigger class " << classname << " not found in CTPConfiguration";
118 return -1;
119 }
120 scals = mScalers.getRatesForIndex(classIndex, type);
121 }
122 double lumi = 0;
123 for (auto const& ss : scals) {
124 // std::cout << ss.first << " " << ss.second << " " << pileUpCorrection(ss.first/ss.second) << std::endl;
125 lumi += pileUpCorrection(ss.first / ss.second) * ss.second;
126 }
127 return lumi;
128}
129double CTPRateFetcher::getLumi(const std::string& classname, int type, int puCorr)
130{
131 if (puCorr) {
132 return getLumiWPuCorr(classname, type);
133 } else {
134 return getLumiNoPuCorr(classname, type);
135 }
136}
137
138double CTPRateFetcher::getLumi(o2::ccdb::BasicCCDBManager* ccdb, int runNumber, const std::string sourceName, int puCorr)
139{
140 // setupRun(runNumber, ccdb, timeStamp, 1);
141 if (sourceName.find("ZNC") != std::string::npos) {
142 if (runNumber < 544448) {
143 return getLumi("zncinp", 1, puCorr) / (sourceName.find("hadronic") != std::string::npos ? 28. : 1.);
144 } else {
145 return getLumi("C1ZNC-B-NOPF-CRU", 6, puCorr) / (sourceName.find("hadronic") != std::string::npos ? 28. : 1.);
146 }
147 } else if (sourceName == "T0CE") {
148 return getLumi("CMTVXTCE-B-NOPF", 1, puCorr);
149 } else if (sourceName == "T0SC") {
150 return getLumi("CMTVXTSC-B-NOPF", 1, puCorr);
151 } else if (sourceName == "T0VTX") {
152 if (runNumber < 534202) {
153 return getLumi("minbias_TVX_L0", 3, puCorr); // 2022
154 } else {
155 double ret = getLumi("CMTVX-B-NOPF", 1, puCorr);
156 if (ret == -1.) {
157 LOG(info) << "Trying different class";
158 ret = getLumi("CMTVX-NONE", 1, puCorr);
159 if (ret < 0) {
160 LOG(fatal) << "None of the classes used for lumi found";
161 }
162 }
163 return ret;
164 }
165 }
166 LOG(error) << "CTP Lumi for " << sourceName << " not available";
167 return 0;
168}
169//
170double CTPRateFetcher::fetchCTPratesClasses(uint64_t timeStamp, const std::string& className, int inputType)
171{
172 auto triggerRate = fetchCTPratesClassesNoPuCorr(timeStamp, className, inputType);
173 if (triggerRate >= 0) {
174 return pileUpCorrection(triggerRate);
175 }
176 return -1;
177}
178double CTPRateFetcher::fetchCTPratesClassesNoPuCorr(uint64_t timeStamp, const std::string& className, int inputType)
179{
180 std::vector<ctp::CTPClass>& ctpcls = mConfig.getCTPClasses();
181 std::vector<int> clslist = mConfig.getTriggerClassList();
182 int classIndex = -1;
183 for (size_t i = 0; i < clslist.size(); i++) {
184 if (ctpcls[i].name.find(className) != std::string::npos) {
185 classIndex = i;
186 break;
187 }
188 }
189 if (classIndex == -1) {
190 LOG(warn) << "Trigger class " << className << " not found in CTPConfiguration";
191 return -2.;
192 }
193 if (mOrbit) {
194 auto rate{mScalers.getRate((uint32_t)timeStamp, classIndex, inputType, mOutsideLimits)};
195 return rate.second;
196 } else {
197 auto rate{mScalers.getRateGivenT(timeStamp * 1.e-3, classIndex, inputType, mOutsideLimits)};
198 return rate.second;
199 }
200}
201double CTPRateFetcher::fetchCTPratesInputs(uint64_t timeStamp, int input)
202{
203 std::vector<ctp::CTPScalerRecordO2>& recs = mScalers.getScalerRecordO2();
204 if (recs[0].scalersInps.size() == 48) {
205 if (mOrbit) {
206 return pileUpCorrection(mScalers.getRate((uint32_t)timeStamp, input, 7, mOutsideLimits).second);
207 } else {
208 return pileUpCorrection(mScalers.getRateGivenT(timeStamp * 1.e-3, input, 7, mOutsideLimits).second);
209 }
210 } else {
211 LOG(error) << "Inputs not available";
212 return -1.;
213 }
214}
215double CTPRateFetcher::fetchCTPratesInputsNoPuCorr(uint64_t timeStamp, int input)
216{
217 std::vector<ctp::CTPScalerRecordO2>& recs = mScalers.getScalerRecordO2();
218 if (recs[0].scalersInps.size() == 48) {
219 if (mOrbit) {
220 return mScalers.getRate((uint32_t)timeStamp, input, 7, mOutsideLimits).second;
221 } else {
222 return mScalers.getRateGivenT(timeStamp * 1.e-3, input, 7, mOutsideLimits).second; // qc flag implemented only for time
223 }
224 } else {
225 LOG(error) << "Inputs not available";
226 return -1.;
227 }
228}
229
230double CTPRateFetcher::pileUpCorrection(double triggerRate)
231{
232 if (mLHCIFdata.getFillNumber() == 0) {
233 LOG(fatal) << "No filling" << std::endl;
234 }
235 auto bfilling = mLHCIFdata.getBunchFilling();
236 std::vector<int> bcs = bfilling.getFilledBCs();
237 double nbc = bcs.size();
238 double nTriggersPerFilledBC = triggerRate / nbc / constants::lhc::LHCRevFreq;
239 double mu = -std::log(1 - nTriggersPerFilledBC);
240 return mu * nbc * constants::lhc::LHCRevFreq;
241}
242
243void CTPRateFetcher::setupRun(int runNumber, o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, bool initScalers)
244{
245 if (runNumber == mRunNumber) {
246 return;
247 }
248 mRunNumber = runNumber;
249 LOG(info) << "Setting up CTP scalers for run " << mRunNumber << " and timestamp : " << timeStamp;
250 auto ptrLHCIFdata = ccdb->getSpecific<parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", timeStamp);
251 if (ptrLHCIFdata == nullptr) {
252 LOG(error) << "GRPLHCIFData not in database, timestamp:" << timeStamp;
253 return;
254 }
255 mLHCIFdata = *ptrLHCIFdata;
256 std::map<string, string> metadata;
257 metadata["runNumber"] = std::to_string(mRunNumber);
258 auto ptrConfig = ccdb->getSpecific<ctp::CTPConfiguration>("CTP/Config/Config", timeStamp, metadata);
259 if (ptrConfig == nullptr) {
260 LOG(error) << "CTPRunConfig not in database, timestamp:" << timeStamp;
261 return;
262 }
263 mConfig = *ptrConfig;
264 if (initScalers) {
265 auto ptrScalers = ccdb->getSpecific<ctp::CTPRunScalers>("CTP/Calib/Scalers", timeStamp, metadata);
266 if (ptrScalers) {
267 mScalers = *ptrScalers;
268 mScalers.convertRawToO2();
269 } else {
270 LOG(error) << "CTPRunScalers not in database, timestamp:" << timeStamp;
271 }
272 }
273}
int32_t i
Header to collect LHC related constants.
std::vector< int > getFilledBCs(int dir=-1) const
T * getSpecific(std::string const &path, long timestamp=-1, MD metaData=MD())
retrieve an object of type T from CCDB as stored under path, timestamp and metaData
std::vector< CTPClass > & getCTPClasses()
std::vector< int > getTriggerClassList() const
int getRates(std::array< double, 3 > &rates, o2::ccdb::BasicCCDBManager *ccdb, int runNumber, const std::string sourceName)
double getLumiWPuCorr(const std::string &classname, int type=1)
double getLumi(o2::ccdb::BasicCCDBManager *ccdb, int runNumber, const std::string sourceName, int puCorr=0)
void setupRun(int runNumber, o2::ccdb::BasicCCDBManager *ccdb, uint64_t timeStamp, bool initScalers)
double fetchNoPuCorr(o2::ccdb::BasicCCDBManager *ccdb, uint64_t timeStamp, int runNumber, const std::string sourceName)
double getLumiNoPuCorr(const std::string &classname, int type=1)
void updateScalers(ctp::CTPRunScalers &scalers)
double fetch(o2::ccdb::BasicCCDBManager *ccdb, uint64_t timeStamp, int runNumber, const std::string sourceName)
uint64_t getLumiNoPuCorr(int classindex, int type) const
retrieves integral - same interface as getRate, no pileup correction
Definition Scalers.cxx:661
std::pair< double, double > getRate(uint32_t orbit, int classindex, int type, bool qc=0) const
Definition Scalers.cxx:735
std::pair< double, double > getRateGivenT(double timestamp, int classindex, int type, bool qc=0) const
same with absolute timestamp (not orbit) as argument
Definition Scalers.cxx:809
std::pair< unsigned long, unsigned long > getOrbitLimit() const
retrieves orbit boundaries of this scaler object from O2
Definition Scalers.h:161
std::vector< CTPScalerRecordO2 > & getScalerRecordO2()
Definition Scalers.h:106
std::vector< std::pair< double_t, double_t > > getRatesForIndex(int classindex, int type) const
retrieves vector of counters - same interface as getRate, needed for
Definition Scalers.cxx:693
const o2::BunchFilling & getBunchFilling() const
int32_t getFillNumber() const
GLuint GLenum * rate
Definition glcorearb.h:5735
GLuint const GLchar * name
Definition glcorearb.h:781
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLuint GLsizei const GLenum * rates
Definition glcorearb.h:5738
constexpr double LHCRevFreq
constexpr float mu
Definition SpecsV2.h:27
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
LumiInfo lumi