Skip to content

Commit c7deb27

Browse files
authored
Extend caching and improve performance of timestamp task (#121)
1 parent f25c3ba commit c7deb27

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

Common/TableProducer/timestamp.cxx

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12-
//
13-
// A task to fill the timestamp table from run number.
14-
// Uses headers from CCDB
15-
//
16-
// Author: Nicolo' Jacazio on 2020-06-22
12+
///
13+
/// \file timestamp.cxx
14+
/// \author Nicolò Jacazio
15+
/// \since 2020-06-22
16+
/// \brief A task to fill the timestamp table from run number.
17+
/// Uses headers from CCDB
18+
///
1719

1820
#include "Framework/runDataProcessing.h"
1921
#include "Framework/AnalysisTask.h"
@@ -27,19 +29,23 @@ using namespace o2::header;
2729
using namespace o2;
2830

2931
struct TimestampTask {
30-
Produces<aod::Timestamps> ts_table; /// Table with SOR timestamps produced by the task
32+
Produces<aod::Timestamps> timestampTable; /// Table with SOR timestamps produced by the task
3133
Service<o2::ccdb::BasicCCDBManager> ccdb; /// Object manager in CCDB
3234
o2::ccdb::CcdbApi ccdb_api; /// API to access CCDB
3335
std::map<int, int>* mapStartOrbit = nullptr; /// Map of the starting orbit for the run
34-
std::pair<int, long> lastCall; /// Last run number processed and its timestamp, needed for caching
3536
std::map<int, long> mapRunToTimestamp; /// Cache of processed run numbers
37+
int lastRunNumber = 0; /// Last run number processed
38+
long runNumberTimeStamp = 0; /// Timestamp of the run number, used in the process function to work out the timestamp of the BC
39+
uint32_t initialOrbit = 0; /// Index of the first orbit of the run number, used in the process function to evaluate the offset with respect to the starting of the run
40+
static constexpr uint16_t initialBC = 0; /// Index of the initial bc, exact bc number not relevant due to ms precision of timestamps
41+
InteractionRecord initialIR; /// Initial interaction record, used to compute the delta with respect to the start of the run
3642

3743
// Configurables
3844
Configurable<bool> verbose{"verbose", false, "verbose mode"};
3945
Configurable<std::string> rct_path{"rct-path", "RCT/RunInformation/", "path to the ccdb RCT objects for the SOR timestamps"};
4046
Configurable<std::string> start_orbit_path{"start-orbit-path", "GRP/StartOrbit", "path to the ccdb SOR orbit objects"};
4147
Configurable<std::string> url{"ccdb-url", "http://alice-ccdb.cern.ch", "URL of the CCDB database"};
42-
Configurable<bool> isMC{"isMC", 0, "0 - data, 1 - MC"};
48+
Configurable<bool> isMC{"isMC", false, "Running mode: enabled for MC. If enabled this flag is used to compute the timestamp according to the input data. The timestamp of the BC is computed from initialBC and initialOrbit"};
4349

4450
void init(o2::framework::InitContext&)
4551
{
@@ -56,15 +62,30 @@ struct TimestampTask {
5662
}
5763
}
5864

65+
void makeInitialOrbit(aod::BC const& bunchCrossing)
66+
{
67+
if (!mapStartOrbit->count(bunchCrossing.runNumber())) {
68+
LOGF(fatal, "Cannot find run %i in mapStartOrbit map", bunchCrossing.runNumber());
69+
}
70+
initialOrbit = mapStartOrbit->at(bunchCrossing.runNumber());
71+
initialIR.bc = initialBC;
72+
initialIR.orbit = initialOrbit;
73+
// Setting lastCall
74+
LOGF(debug, "Setting the last call of the timestamp for run %i to %llu", bunchCrossing.runNumber(), runNumberTimeStamp);
75+
lastRunNumber = bunchCrossing.runNumber(); // Setting latest run number information
76+
}
77+
5978
void process(aod::BC const& bc)
6079
{
61-
long ts = 0;
62-
if (bc.runNumber() == lastCall.first) { // The run number coincides to the last run processed
63-
LOGF(debug, "Getting timestamp from last call");
64-
ts = lastCall.second;
80+
// First: we need to set the timestamp from the run number.
81+
// This is done with caching if the run number of the BC was already processed before
82+
// If not the timestamp of the run number from BC is queried from CCDB and added to the cache
83+
if (bc.runNumber() == lastRunNumber) { // The run number coincides to the last run processed
84+
LOGF(debug, "Using timestamp from last call");
6585
} else if (mapRunToTimestamp.count(bc.runNumber())) { // The run number was already requested before: getting it from cache!
6686
LOGF(debug, "Getting timestamp from cache");
67-
ts = mapRunToTimestamp[bc.runNumber()];
87+
runNumberTimeStamp = mapRunToTimestamp[bc.runNumber()];
88+
makeInitialOrbit(bc);
6889
} else { // The run was not requested before: need to acccess CCDB!
6990
LOGF(debug, "Getting timestamp from CCDB");
7091
std::map<std::string, std::string> metadata, headers;
@@ -73,34 +94,25 @@ struct TimestampTask {
7394
if (headers.count("SOR") == 0) {
7495
LOGF(fatal, "Cannot find run-number to timestamp in path '%s'.", run_path.data());
7596
}
76-
ts = atol(headers["SOR"].c_str()); // timestamp of the SOR in ms
97+
runNumberTimeStamp = atol(headers["SOR"].c_str()); // timestamp of the SOR in ms
7798

7899
// Adding the timestamp to the cache map
79100
std::pair<std::map<int, long>::iterator, bool> check;
80-
check = mapRunToTimestamp.insert(std::pair<int, long>(bc.runNumber(), ts));
101+
check = mapRunToTimestamp.insert(std::pair<int, long>(bc.runNumber(), runNumberTimeStamp));
81102
if (!check.second) {
82103
LOGF(fatal, "Run number %i already existed with a timestamp of %llu", bc.runNumber(), check.first->second);
83104
}
84-
LOGF(info, "Add new run %i with timestamp %llu to cache", bc.runNumber(), ts);
105+
makeInitialOrbit(bc);
106+
LOGF(info, "Add new run number %i with timestamp %llu to cache", bc.runNumber(), runNumberTimeStamp);
85107
}
86108

87-
// Setting latest run information
88-
lastCall = std::make_pair(bc.runNumber(), ts);
89-
90109
if (verbose.value) {
91-
LOGF(info, "Run-number to timestamp found! %i %llu ms", bc.runNumber(), ts);
92-
}
93-
const uint16_t initialBC = 0; // exact bc number not relevant due to ms precision of timestamps
94-
if (!mapStartOrbit->count(bc.runNumber())) {
95-
LOGF(fatal, "Cannot find run %i in mapStartOrbit map", bc.runNumber());
110+
LOGF(info, "Run-number to timestamp found! %i %llu ms", bc.runNumber(), runNumberTimeStamp);
96111
}
97-
const uint32_t initialOrbit = mapStartOrbit->at(bc.runNumber());
98-
const uint16_t currentBC = isMC ? initialBC : bc.globalBC() % o2::constants::lhc::LHCMaxBunches;
99-
const uint32_t currentOrbit = isMC ? initialOrbit : bc.globalBC() / o2::constants::lhc::LHCMaxBunches;
100-
const InteractionRecord current(currentBC, currentOrbit);
101-
const InteractionRecord initial(initialBC, initialOrbit);
102-
ts += (current - initial).bc2ns() / 1000000;
103-
ts_table(ts);
112+
const uint16_t currentBC = isMC ? initialBC : (bc.globalBC() % o2::constants::lhc::LHCMaxBunches);
113+
const uint32_t currentOrbit = isMC ? initialOrbit : (bc.globalBC() / o2::constants::lhc::LHCMaxBunches);
114+
const InteractionRecord currentIR(currentBC, currentOrbit);
115+
timestampTable(runNumberTimeStamp + (currentIR - initialIR).bc2ns() * 1e-6);
104116
}
105117
};
106118

0 commit comments

Comments
 (0)