Skip to content

Commit 344146e

Browse files
committed
put the scalers in their own class
1 parent c23a052 commit 344146e

File tree

7 files changed

+183
-90
lines changed

7 files changed

+183
-90
lines changed

Framework/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ add_library(O2QualityControl
9090
src/TaskRunner.cxx
9191
src/TaskRunnerFactory.cxx
9292
src/TaskInterface.cxx
93+
src/CtpScalers.cxx
9394
src/UserCodeInterface.cxx
9495
src/RepositoryBenchmark.cxx
9596
src/RepoPathUtils.cxx
@@ -181,6 +182,7 @@ add_root_dictionary(O2QualityControl
181182
HEADERS
182183
include/QualityControl/CheckInterface.h
183184
include/QualityControl/TaskInterface.h
185+
include/QualityControl/CtpScalers.h
184186
include/QualityControl/UserCodeInterface.h
185187
include/QualityControl/AggregatorInterface.h
186188
include/QualityControl/PostProcessingInterface.h
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
///
13+
/// \file CtpScalers.h
14+
/// \author Barthelemy von Haller
15+
///
16+
17+
#ifndef QUALITYCONTROL_CTPSCALERS_H
18+
#define QUALITYCONTROL_CTPSCALERS_H
19+
20+
#include <Rtypes.h>
21+
#include "QualityControl/DatabaseInterface.h"
22+
23+
namespace o2::ctp
24+
{
25+
class CTPRateFetcher;
26+
}
27+
28+
namespace o2::quality_control::core
29+
{
30+
31+
class CtpScalers
32+
{
33+
public:
34+
CtpScalers() = default;
35+
virtual ~CtpScalers() = default;
36+
37+
/// \brief Call it to enable the retrieval of CTP scalers and use `getScalers` later
38+
void enableCtpScalers(size_t runNumber, std::string ccdbUrl);
39+
/// \brief Get the scalers's value for the given source
40+
double getScalersValue(std::string sourceName, size_t runNumber);
41+
42+
void setDatabase(std::shared_ptr<repository::DatabaseInterface> database)
43+
{
44+
mDatabase = database;
45+
}
46+
47+
private:
48+
/// \brief Retrieve fresh scalers from the QCDB (with cache)
49+
/// \return true if success, false if failure
50+
bool updateScalers(size_t runNumber);
51+
52+
std::shared_ptr<o2::ctp::CTPRateFetcher> mCtpFetcher;
53+
std::chrono::steady_clock::time_point mScalersLastUpdate;
54+
bool mScalersEnabled = false;
55+
std::shared_ptr<repository::DatabaseInterface> mDatabase; //! the repository used by the Framework
56+
57+
private:
58+
ClassDef(CtpScalers, 1)
59+
};
60+
61+
} // namespace o2::quality_control::core
62+
63+
#endif // QUALITYCONTROL_USERCODEINTERFACE_H

Framework/include/QualityControl/DatabaseInterface.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#define QC_REPOSITORY_DATABASEINTERFACE_H
1919

2020
#include <string>
21-
#include <memory>
2221
#include <vector>
2322
#include <unordered_map>
2423

Framework/include/QualityControl/LinkDef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma link C++ namespace o2::quality_control::checker;
88
#pragma link C++ namespace o2::quality_control::postprocessing;
99

10+
#pragma link C++ class o2::quality_control::core::CtpScalers + ;
1011
#pragma link C++ class o2::quality_control::core::UserCodeInterface + ;
1112
#pragma link C++ class o2::quality_control::checker::CheckInterface + ;
1213
#pragma link C++ class o2::quality_control::core::TaskInterface + ;

Framework/include/QualityControl/UserCodeInterface.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818
#define QUALITYCONTROL_USERCODEINTERFACE_H
1919

2020
#include <string>
21-
#include <map>
2221
#include <Rtypes.h>
2322

2423
#include "QualityControl/ConditionAccess.h"
2524
#include "QualityControl/CustomParameters.h"
2625
#include "QualityControl/DatabaseInterface.h"
27-
28-
namespace o2::ctp
29-
{
30-
class CTPRateFetcher;
31-
}
26+
#include "QualityControl/CcdbDatabase.h"
27+
#include "QualityControl/CtpScalers.h"
3228

3329
namespace o2::quality_control::core
3430
{
@@ -56,14 +52,6 @@ class UserCodeInterface : public ConditionAccess
5652
void setName(const std::string& name);
5753
void setDatabase(std::unordered_map<std::string, std::string> dbConfig);
5854

59-
private:
60-
/// \brief Retrieve fresh scalers from the QCDB (with cache)
61-
/// \return true if success, false if failure
62-
bool updateScalers(size_t runNumber);
63-
std::shared_ptr<o2::ctp::CTPRateFetcher> mCtpFetcher;
64-
std::chrono::steady_clock::time_point mScalersLastUpdate;
65-
bool mScalersEnabled = false;
66-
6755
protected:
6856
/// \brief Call it to enable the retrieval of CTP scalers and use `getScalers` later
6957
void enableCtpScalers(size_t runNumber, std::string ccdbUrl);
@@ -72,7 +60,8 @@ class UserCodeInterface : public ConditionAccess
7260

7361
CustomParameters mCustomParameters;
7462
std::string mName;
75-
std::shared_ptr<o2::quality_control::repository::DatabaseInterface> mDatabase;
63+
std::shared_ptr<repository::DatabaseInterface> mDatabase; //! the repository used by the Framework
64+
CtpScalers mCtpScalers;
7665

7766
ClassDef(UserCodeInterface, 5)
7867
};

Framework/src/CtpScalers.cxx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
///
13+
/// \file CtpScalers.cxx
14+
/// \author Barthelemy von Haller
15+
///
16+
17+
#include "QualityControl/CtpScalers.h"
18+
#include <DataFormatsCTP/CTPRateFetcher.h>
19+
#include "QualityControl/QcInfoLogger.h"
20+
#include "QualityControl/DatabaseFactory.h"
21+
22+
using namespace o2::ccdb;
23+
using namespace std;
24+
25+
namespace o2::quality_control::core
26+
{
27+
void CtpScalers::enableCtpScalers(size_t runNumber, std::string ccdbUrl)
28+
{
29+
// bail if we are in async
30+
auto deploymentMode = framework::DefaultsHelpers::deploymentMode();
31+
if (deploymentMode == framework::DeploymentMode::Grid) {
32+
ILOG(Info, Ops) << "Async mode detected, CTP scalers cannot be enabled." << ENDM;
33+
return;
34+
}
35+
36+
ILOG(Debug, Devel) << "Enabling CTP scalers" << ENDM;
37+
mCtpFetcher = make_shared<o2::ctp::CTPRateFetcher>();
38+
mScalersEnabled = true;
39+
auto& ccdbManager = o2::ccdb::BasicCCDBManager::instance();
40+
ccdbManager.setURL(ccdbUrl);
41+
mCtpFetcher->setupRun(runNumber, &ccdbManager, /*1726300234140*/ getCurrentTimestamp(), false);
42+
43+
mScalersLastUpdate = std::chrono::steady_clock::time_point::min();
44+
if (updateScalers(runNumber)) { // initial value
45+
ILOG(Debug, Devel) << "Enabled CTP scalers" << ENDM;
46+
} else {
47+
ILOG(Debug, Devel) << "CTP scalers not enabled, failure to get them." << ENDM;
48+
}
49+
}
50+
51+
bool CtpScalers::updateScalers(size_t runNumber)
52+
{
53+
if (!mScalersEnabled) {
54+
ILOG(Error, Ops) << "CTP scalers not enabled, impossible to update them." << ENDM;
55+
return false;
56+
}
57+
ILOG(Debug, Devel) << "Updating scalers." << ENDM;
58+
59+
if (!mDatabase) {
60+
ILOG(Error, Devel) << "Database not set ! Cannot update scalers." << ENDM;
61+
mScalersEnabled = false;
62+
return false;
63+
}
64+
65+
auto now = std::chrono::steady_clock::now();
66+
auto minutesSinceLast = std::chrono::duration_cast<std::chrono::minutes>(now - mScalersLastUpdate);
67+
68+
// TODO get the interval from config
69+
if (minutesSinceLast.count() >= 0 /*first time it is neg*/ && minutesSinceLast.count() < 5) {
70+
ILOG(Debug, Devel) << "getScalers was called less than 5 minutes ago, use the cached value" << ENDM;
71+
return true;
72+
}
73+
74+
std::map<std::string, std::string> meta;
75+
meta["runNumber"] = std::to_string(runNumber);
76+
std::map<std::string, std::string> headers;
77+
auto validity = mDatabase->getLatestObjectValidity("qc/CTP/Scalers", meta);
78+
void* rawResult = mDatabase->retrieveAny(typeid(o2::ctp::CTPRunScalers), "qc/CTP/Scalers", meta, validity.getMax() - 1, &headers);
79+
if (!rawResult) {
80+
ILOG(Error, Devel) << "Could not retrieve the CTP Scalers" << ENDM;
81+
return false;
82+
} else {
83+
ILOG(Debug, Devel) << "object retrieved" << ENDM;
84+
}
85+
86+
o2::ctp::CTPRunScalers* ctpScalers = static_cast<o2::ctp::CTPRunScalers*>(rawResult);
87+
mCtpFetcher->updateScalers(*ctpScalers);
88+
mScalersLastUpdate = now;
89+
ILOG(Debug, Devel) << "Scalers updated." << ENDM;
90+
return true;
91+
}
92+
93+
double CtpScalers::getScalersValue(std::string sourceName, size_t runNumber)
94+
{
95+
if (!mScalersEnabled) {
96+
ILOG(Error, Ops) << "CTP scalers not enabled, impossible to get the value." << ENDM;
97+
return 0;
98+
}
99+
if (!updateScalers(runNumber)) { // from QCDB
100+
ILOG(Debug, Devel) << "Could not update the scalers, returning 0" << ENDM;
101+
return 0;
102+
}
103+
auto& ccdbManager = o2::ccdb::BasicCCDBManager::instance();
104+
auto result = mCtpFetcher->fetchNoPuCorr(&ccdbManager, getCurrentTimestamp() * 1000, runNumber, sourceName);
105+
ILOG(Debug, Devel) << "Returning scaler value : " << result << ENDM;
106+
return result;
107+
}
108+
109+
} // namespace o2::quality_control::core

Framework/src/UserCodeInterface.cxx

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -43,84 +43,12 @@ void UserCodeInterface::setName(const std::string& name)
4343

4444
void UserCodeInterface::enableCtpScalers(size_t runNumber, std::string ccdbUrl)
4545
{
46-
// bail if we are in async
47-
auto deploymentMode = framework::DefaultsHelpers::deploymentMode();
48-
if (deploymentMode == framework::DeploymentMode::Grid) {
49-
ILOG(Info, Ops) << "Async mode detected, CTP scalers cannot be enabled." << ENDM;
50-
return;
51-
}
52-
53-
ILOG(Debug, Devel) << "Enabling CTP scalers" << ENDM;
54-
mCtpFetcher = make_shared<o2::ctp::CTPRateFetcher>();
55-
mScalersEnabled = true;
56-
auto& ccdbManager = o2::ccdb::BasicCCDBManager::instance();
57-
ccdbManager.setURL(ccdbUrl);
58-
mCtpFetcher->setupRun(runNumber, &ccdbManager, /*1726300234140*/ getCurrentTimestamp(), false);
59-
60-
mScalersLastUpdate = std::chrono::steady_clock::time_point::min();
61-
if (updateScalers(runNumber)) { // initial value
62-
ILOG(Debug, Devel) << "Enabled CTP scalers" << ENDM;
63-
} else {
64-
ILOG(Debug, Devel) << "CTP scalers not enabled, failure to get them." << ENDM;
65-
}
66-
}
67-
68-
bool UserCodeInterface::updateScalers(size_t runNumber)
69-
{
70-
if (!mScalersEnabled) {
71-
ILOG(Error, Ops) << "CTP scalers not enabled, impossible to update them." << ENDM;
72-
return false;
73-
}
74-
ILOG(Debug, Devel) << "Updating scalers." << ENDM;
75-
76-
if (!mDatabase) {
77-
ILOG(Error, Devel) << "Database not set ! Cannot update scalers." << ENDM;
78-
mScalersEnabled = false;
79-
return false;
80-
}
81-
82-
auto now = std::chrono::steady_clock::now();
83-
auto minutesSinceLast = std::chrono::duration_cast<std::chrono::minutes>(now - mScalersLastUpdate);
84-
85-
// TODO get the interval from config
86-
if (minutesSinceLast.count() >= 0 /*first time it is neg*/ && minutesSinceLast.count() < 5) {
87-
ILOG(Debug, Devel) << "getScalers was called less than 5 minutes ago, use the cached value" << ENDM;
88-
return true;
89-
}
90-
91-
std::map<std::string, std::string> meta;
92-
meta["runNumber"] = std::to_string(runNumber);
93-
std::map<std::string, std::string> headers;
94-
auto validity = mDatabase->getLatestObjectValidity("qc/CTP/Scalers", meta);
95-
void* rawResult = mDatabase->retrieveAny(typeid(o2::ctp::CTPRunScalers), "qc/CTP/Scalers", meta, validity.getMax() - 1, &headers);
96-
if (!rawResult) {
97-
ILOG(Error, Devel) << "Could not retrieve the CTP Scalers" << ENDM;
98-
return false;
99-
} else {
100-
ILOG(Debug, Devel) << "object retrieved" << ENDM;
101-
}
102-
103-
o2::ctp::CTPRunScalers* ctpScalers = static_cast<o2::ctp::CTPRunScalers*>(rawResult);
104-
mCtpFetcher->updateScalers(*ctpScalers);
105-
mScalersLastUpdate = now;
106-
ILOG(Debug, Devel) << "Scalers updated." << ENDM;
107-
return true;
46+
mCtpScalers.enableCtpScalers(runNumber, ccdbUrl);
10847
}
10948

11049
double UserCodeInterface::getScalersValue(std::string sourceName, size_t runNumber)
11150
{
112-
if (!mScalersEnabled) {
113-
ILOG(Error, Ops) << "CTP scalers not enabled, impossible to get the value." << ENDM;
114-
return 0;
115-
}
116-
if (!updateScalers(runNumber)) { // from QCDB
117-
ILOG(Debug, Devel) << "Could not update the scalers, returning 0" << ENDM;
118-
return 0;
119-
}
120-
auto& ccdbManager = o2::ccdb::BasicCCDBManager::instance();
121-
auto result = mCtpFetcher->fetchNoPuCorr(&ccdbManager, getCurrentTimestamp() * 1000, runNumber, sourceName);
122-
ILOG(Debug, Devel) << "Returning scaler value : " << result << ENDM;
123-
return result;
51+
return mCtpScalers.getScalersValue( sourceName, runNumber);
12452
}
12553

12654
void UserCodeInterface::setDatabase(std::unordered_map<std::string, std::string> dbConfig)
@@ -133,6 +61,8 @@ void UserCodeInterface::setDatabase(std::unordered_map<std::string, std::string>
13361
mDatabase = repository::DatabaseFactory::create(dbConfig.at("implementation"));
13462
mDatabase->connect(dbConfig);
13563
ILOG(Debug, Devel) << "Database that is going to be used > Implementation : " << dbConfig.at("implementation") << " / Host : " << dbConfig.at("host") << ENDM;
64+
65+
mCtpScalers.setDatabase(mDatabase);
13666
}
13767

13868
} // namespace o2::quality_control::core

0 commit comments

Comments
 (0)