Skip to content

Commit 60f0dd4

Browse files
authored
ctpdev (#13458)
* devL ctpRateFetcher copied from O2Physics to O2 * clang * fixes * clang * dev: fixes according to andrea + demo macro * clang * fix: pointer protection * fix: added fetcher to linkdef * fix: ctpRateFetcher renamed to CTPRateFetcher * fix: pointers to ccdb object changed to instances * clang * fix: references instead of copy * clang
1 parent 18b692e commit 60f0dd4

File tree

8 files changed

+278
-15
lines changed

8 files changed

+278
-15
lines changed

DataFormats/Detectors/CTP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ o2_add_library(DataFormatsCTP
1515
src/CTF.cxx
1616
src/TriggerOffsetsParam.cxx
1717
src/LumiInfo.cxx
18+
src/CTPRateFetcher.cxx
1819
PUBLIC_LINK_LIBRARIES O2::CommonDataFormat
1920
O2::Headers
2021
O2::CommonUtils
@@ -27,5 +28,6 @@ o2_target_root_dictionary(DataFormatsCTP
2728
include/DataFormatsCTP/Configuration.h
2829
include/DataFormatsCTP/Scalers.h
2930
include/DataFormatsCTP/LumiInfo.h
31+
include/DataFormatsCTP/CTPRateFetcher.h
3032
include/DataFormatsCTP/TriggerOffsetsParam.h)
3133

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
12+
#ifndef COMMON_CCDB_CTPRATEFETCHER_H_
13+
#define COMMON_CCDB_CTPRATEFETCHER_H_
14+
15+
#include <string>
16+
17+
#include "CCDB/BasicCCDBManager.h"
18+
#include "DataFormatsParameters/GRPLHCIFData.h"
19+
#include "DataFormatsCTP/Configuration.h"
20+
#include "DataFormatsCTP/Scalers.h"
21+
22+
namespace o2
23+
{
24+
namespace ctp
25+
{
26+
27+
class CTPRateFetcher
28+
{
29+
public:
30+
CTPRateFetcher() = default;
31+
double fetch(o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, int runNumber, const std::string sourceName);
32+
double fetchNoPuCorr(o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, int runNumber, const std::string sourceName);
33+
void setupRun(int runNumber, o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, bool initScalers);
34+
void updateScalers(ctp::CTPRunScalers& scalers);
35+
36+
private:
37+
double fetchCTPratesInputs(uint64_t timeStamp, int input);
38+
double fetchCTPratesClasses(uint64_t timeStamp, const std::string& className, int inputType = 1);
39+
double fetchCTPratesInputsNoPuCorr(uint64_t timeStamp, int input);
40+
double fetchCTPratesClassesNoPuCorr(uint64_t timeStamp, const std::string& className, int inputType = 1);
41+
42+
double pileUpCorrection(double rate);
43+
int mRunNumber = -1;
44+
o2::ctp::CTPConfiguration mConfig{};
45+
o2::ctp::CTPRunScalers mScalers{};
46+
o2::parameters::GRPLHCIFData mLHCIFdata{};
47+
ClassDefNV(CTPRateFetcher, 1);
48+
};
49+
} // namespace ctp
50+
} // namespace o2
51+
52+
#endif // COMMON_CCDB_CTPRATEFETCHER_H_

DataFormats/Detectors/CTP/include/DataFormatsCTP/Scalers.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ struct CTPScalerRecordO2 {
8787
class CTPRunScalers
8888
{
8989
public:
90+
//
91+
// static constexpr uint32_t NCOUNTERS = 1052;
92+
// v1
93+
// static constexpr uint32_t NCOUNTERS = 1070;
94+
// v2 - orbitid added at the end
95+
static constexpr uint32_t NCOUNTERSv2 = 1071;
96+
static constexpr uint32_t NCOUNTERS = 1085;
97+
static std::vector<std::string> scalerNames;
9098
CTPRunScalers() = default;
91-
9299
void printStream(std::ostream& stream) const;
93100
void printO2(std::ostream& stream) const;
94101
void printFromZero(std::ostream& stream) const;
95102
void printClasses(std::ostream& stream) const;
96103
std::vector<uint32_t> getClassIndexes() const;
104+
uint32_t getRunNumber() { return mRunNumber; };
97105
int getScalerIndexForClass(uint32_t cls) const;
98106
std::vector<CTPScalerRecordO2>& getScalerRecordO2() { return mScalerRecordO2; };
99107
std::vector<CTPScalerRecordRaw>& getScalerRecordRaw() { return mScalerRecordRaw; };
@@ -106,7 +114,6 @@ class CTPRunScalers
106114
void setDetectorMask(o2::detectors::DetID::mask_t mask) { mDetectorMask = mask; };
107115
void setRunNumber(uint32_t rnumber) { mRunNumber = rnumber; };
108116
void addScalerRacordRaw(CTPScalerRecordRaw& scalerrecordraw) { mScalerRecordRaw.push_back(scalerrecordraw); };
109-
uint32_t getRunNumber() { return mRunNumber; };
110117
int printRates();
111118
int printIntegrals();
112119
int printInputRateAndIntegral(int inp);
@@ -115,16 +122,7 @@ class CTPRunScalers
115122
//
116123
int addOrbitOffset(uint32_t offset);
117124
//
118-
// static constexpr uint32_t NCOUNTERS = 1052;
119-
// v1
120-
// static constexpr uint32_t NCOUNTERS = 1070;
121-
// v2 - orbitid added at the end
122-
static constexpr uint32_t NCOUNTERSv2 = 1071;
123-
static constexpr uint32_t NCOUNTERS = 1085;
124-
static std::vector<std::string> scalerNames;
125-
126125
void printLMBRateVsT() const; // prints LMB interaction rate vs time for debugging
127-
128126
// returns the pair of global (levelled) interaction rate, as well as interpolated
129127
// rate in Hz at a certain orbit number within the run
130128
std::pair<double, double> getRate(uint32_t orbit, int classindex, int type) const;
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
12+
#include "DataFormatsCTP/CTPRateFetcher.h"
13+
14+
#include <map>
15+
#include <vector>
16+
#include "CommonConstants/LHCConstants.h"
17+
18+
using namespace o2::ctp;
19+
double 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+
}
27+
double 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_t 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(fatal) << "None of the classes used for lumi found";
50+
}
51+
}
52+
return ret;
53+
}
54+
}
55+
LOG(error) << "CTP rate for " << sourceName << " not available";
56+
return -1.;
57+
}
58+
void CTPRateFetcher::updateScalers(ctp::CTPRunScalers& scalers)
59+
{
60+
mScalers = scalers;
61+
mScalers.convertRawToO2();
62+
}
63+
//
64+
double CTPRateFetcher::fetchCTPratesClasses(uint64_t timeStamp, const std::string& className, int inputType)
65+
{
66+
auto triggerRate = fetchCTPratesClassesNoPuCorr(timeStamp, className, inputType);
67+
if (triggerRate >= 0) {
68+
return pileUpCorrection(triggerRate);
69+
}
70+
return -1;
71+
}
72+
double CTPRateFetcher::fetchCTPratesClassesNoPuCorr(uint64_t timeStamp, const std::string& className, int inputType)
73+
{
74+
std::vector<ctp::CTPClass>& ctpcls = mConfig.getCTPClasses();
75+
std::vector<int> clslist = mConfig.getTriggerClassList();
76+
int classIndex = -1;
77+
for (size_t i = 0; i < clslist.size(); i++) {
78+
if (ctpcls[i].name.find(className) != std::string::npos) {
79+
classIndex = i;
80+
break;
81+
}
82+
}
83+
if (classIndex == -1) {
84+
LOG(warn) << "Trigger class " << className << " not found in CTPConfiguration";
85+
return -2.;
86+
}
87+
auto rate{mScalers.getRateGivenT(timeStamp * 1.e-3, classIndex, inputType)};
88+
return rate.second;
89+
}
90+
double CTPRateFetcher::fetchCTPratesInputs(uint64_t timeStamp, int input)
91+
{
92+
std::vector<ctp::CTPScalerRecordO2>& recs = mScalers.getScalerRecordO2();
93+
if (recs[0].scalersInps.size() == 48) {
94+
return pileUpCorrection(mScalers.getRateGivenT(timeStamp * 1.e-3, input, 7).second);
95+
} else {
96+
LOG(error) << "Inputs not available";
97+
return -1.;
98+
}
99+
}
100+
double CTPRateFetcher::fetchCTPratesInputsNoPuCorr(uint64_t timeStamp, int input)
101+
{
102+
std::vector<ctp::CTPScalerRecordO2>& recs = mScalers.getScalerRecordO2();
103+
if (recs[0].scalersInps.size() == 48) {
104+
return mScalers.getRateGivenT(timeStamp * 1.e-3, input, 7).second;
105+
} else {
106+
LOG(error) << "Inputs not available";
107+
return -1.;
108+
}
109+
}
110+
111+
double CTPRateFetcher::pileUpCorrection(double triggerRate)
112+
{
113+
if (mLHCIFdata.getFillNumber() == 0) {
114+
LOG(fatal) << "No filling" << std::endl;
115+
}
116+
auto bfilling = mLHCIFdata.getBunchFilling();
117+
std::vector<int> bcs = bfilling.getFilledBCs();
118+
double nbc = bcs.size();
119+
double nTriggersPerFilledBC = triggerRate / nbc / constants::lhc::LHCRevFreq;
120+
double mu = -std::log(1 - nTriggersPerFilledBC);
121+
return mu * nbc * constants::lhc::LHCRevFreq;
122+
}
123+
124+
void CTPRateFetcher::setupRun(int runNumber, o2::ccdb::BasicCCDBManager* ccdb, uint64_t timeStamp, bool initScalers)
125+
{
126+
if (runNumber == mRunNumber) {
127+
return;
128+
}
129+
mRunNumber = runNumber;
130+
LOG(debug) << "Setting up CTP scalers for run " << mRunNumber;
131+
std::map<string, string> metadata;
132+
auto ptrLHCIFdata = ccdb->getSpecific<parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", timeStamp, metadata);
133+
if (ptrLHCIFdata == nullptr) {
134+
LOG(fatal) << "GRPLHCIFData not in database, timestamp:" << timeStamp;
135+
}
136+
mLHCIFdata = *ptrLHCIFdata;
137+
metadata["runNumber"] = std::to_string(mRunNumber);
138+
auto ptrConfig = ccdb->getSpecific<ctp::CTPConfiguration>("CTP/Config/Config", timeStamp, metadata);
139+
if (ptrConfig == nullptr) {
140+
LOG(fatal) << "CTPRunConfig not in database, timestamp:" << timeStamp;
141+
}
142+
mConfig = *ptrConfig;
143+
if (initScalers) {
144+
auto ptrScalers = ccdb->getSpecific<ctp::CTPRunScalers>("CTP/Calib/Scalers", timeStamp, metadata);
145+
if (ptrScalers) {
146+
mScalers = *ptrScalers;
147+
mScalers.convertRawToO2();
148+
} else {
149+
LOG(fatal) << "CTPRunScalers not in database, timestamp:" << timeStamp;
150+
}
151+
}
152+
}

DataFormats/Detectors/CTP/src/DataFormatsCTPLinkDef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#pragma link C++ class o2::ctp::CTPRunScalers + ;
4747
#pragma link C++ class o2::ctp::LumiInfo + ;
4848
#pragma link C++ class vector < o2::ctp::LumiInfo> + ;
49+
#pragma link C++ class o2::ctp::CTPRateFetcher + ;
4950

5051
#pragma link C++ struct o2::ctp::CTFHeader + ;
5152
#pragma link C++ struct o2::ctp::CTF + ;

DataFormats/Detectors/CTP/src/Scalers.cxx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ int CTPRunScalers::convertRawToO2()
308308
for (uint32_t i = 1; i < mScalerRecordRaw.size(); i++) {
309309
//update overflows
310310
int ret = updateOverflows(mScalerRecordRaw[i - 1], mScalerRecordRaw[i], overflows);
311+
// for(int k = 0; k < mClassMask.size(); k++) {
312+
// if(mClassMask[k]) {
313+
// LOG(info) << i << " " << k << " " << overflows[k][0] << " " << overflows[k][1] << " " << overflows[k][2] << " " << overflows[k][3] << " " << overflows[k][4] << " " << overflows[k][5];
314+
// }
315+
// }
311316
//
312317
if (ret == 0) {
313318
CTPScalerRecordO2 o2rec;
@@ -396,7 +401,7 @@ int CTPRunScalers::checkConsistency(const CTPScalerO2& scal0, const CTPScalerO2&
396401
// LMB >= LMA >= L0B >= L0A >= L1B >= L1A: 5 relations
397402
// broken for classes started at L0
398403
//
399-
int64_t difThres = 2;
404+
int64_t difThres = 6;
400405
int64_t dif = (scal1.lmAfter - scal0.lmAfter) - (scal1.lmBefore - scal0.lmBefore);
401406
if (dif <= difThres) {
402407
eCnts.lmBlmAd1++;
@@ -432,12 +437,14 @@ int CTPRunScalers::checkConsistency(const CTPScalerO2& scal0, const CTPScalerO2&
432437
// ret++;
433438
}
434439
dif = (scal1.l1Before - scal0.l1Before) - (scal1.l0After - scal0.l0After);
440+
// LOG(info) << "L1B L0A " << dif << " " << scal1.l1Before << " " << scal1.l0After << " " << scal0.l1Before << " " << scal0.l0After;
435441
if (dif <= difThres) {
436442
eCnts.l0Al1Bd1++;
437443
} else if (dif > difThres) {
438444
eCnts.l0Al1B++;
439445
if (eCnts.l0Al1B < eCnts.MAXPRINT) {
440-
LOG(error) << "L1B > L0A Before error:" << dif;
446+
// LOG(error) << "L1B > L0A Before error:" << dif << " " << scal1.l1Before << " " << scal1.l0After << " " << scal0.l1Before << " " << scal0.l0After;
447+
LOG(warning) << "L1B > L0A Before error:" << dif;
441448
}
442449
ret++;
443450
}
@@ -466,10 +473,11 @@ int CTPRunScalers::updateOverflows(const CTPScalerRecordRaw& rec0, const CTPScal
466473
}
467474
int CTPRunScalers::checkConsistency(const CTPScalerRecordO2& rec0, const CTPScalerRecordO2& rec1, errorCounters& eCnts) const
468475
{
476+
int ret = 0;
469477
for (uint32_t i = 0; i < rec0.scalers.size(); i++) {
470-
checkConsistency(rec0.scalers[i], rec1.scalers[i], eCnts);
478+
ret += checkConsistency(rec0.scalers[i], rec1.scalers[i], eCnts);
471479
}
472-
return 0;
480+
return ret;
473481
}
474482
int CTPRunScalers::updateOverflows(const CTPScalerRaw& scal0, const CTPScalerRaw& scal1, std::array<uint32_t, 6>& overflow) const
475483
{

Detectors/CTP/macro/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ o2_add_test_root_macro(PlotOrbit.C
6565
PUBLIC_LINK_LIBRARIES O2::DataFormatsCTP
6666
O2::CCDB
6767
LABELS ctp)
68+
o2_add_test_root_macro(TestFetcher.C
69+
PUBLIC_LINK_LIBRARIES O2::DataFormatsCTP
70+
O2::CCDB
71+
LABELS ctp)
6872

Detectors/CTP/macro/TestFetcher.C

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
12+
#if !defined(__CLING__) || defined(__ROOTCLING__)
13+
#include <CCDB/BasicCCDBManager.h>
14+
#include <DataFormatsCTP/Configuration.h>
15+
#include <DataFormatsCTP/CTPRateFetcher.h>
16+
#endif
17+
using namespace o2::ctp;
18+
19+
void TestFetcher(int runNumber = 557251)
20+
{
21+
auto& ccdb = o2::ccdb::BasicCCDBManager::instance();
22+
std::pair<int64_t, int64_t> pp = ccdb.getRunDuration(runNumber);
23+
long ts = pp.first + 60;
24+
std::cout << "Run duration:" << pp.first << " " << pp.second << std::endl;
25+
// Opening run
26+
CTPRateFetcher fetcher;
27+
fetcher.setupRun(runNumber, &ccdb, ts, 0);
28+
ccdb.setURL("http://ali-qcdb-gpn.cern.ch:8083/");
29+
std::string QCDBPathCTPScalers = "qc/CTP/Scalers";
30+
map<string, string> metadata; // can be empty
31+
std::string run = std::to_string(runNumber);
32+
metadata["runNumber"] = run;
33+
CTPRunScalers* ctpscalers = ccdb.getSpecific<CTPRunScalers>(QCDBPathCTPScalers, ts, metadata);
34+
auto tt = ctpscalers->getTimeLimitFromRaw();
35+
std::cout << "1st scalers duration:" << tt.first << " " << tt.second << std::endl;
36+
fetcher.updateScalers(*ctpscalers);
37+
auto rate = fetcher.fetchNoPuCorr(&ccdb, ts, runNumber, "T0VTX");
38+
std::cout << "1st rate:" << rate << std::endl;
39+
// Running on the same run
40+
ts = ts + 5 * 1000 * 3600;
41+
ctpscalers = ccdb.getSpecific<CTPRunScalers>(QCDBPathCTPScalers, ts, metadata);
42+
std::cout << "Later scalers duration:" << tt.first << " " << tt.second << std::endl;
43+
fetcher.updateScalers(*ctpscalers);
44+
rate = fetcher.fetchNoPuCorr(&ccdb, ts, runNumber, "T0VTX");
45+
std::cout << "Later rate:" << rate << std::endl;
46+
}

0 commit comments

Comments
 (0)