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
0 commit comments